xref: /illumos-gate/usr/src/cmd/oamuser/lib/vlogin.c (revision 7d7f5f75)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
230a1278f2SGary Mills  * Copyright (c) 2013 Gary Mills
240a1278f2SGary Mills  *
257c478bd9Sstevel@tonic-gate  * Copyright (c) 1997, by Sun Microsystems, Inc.
267c478bd9Sstevel@tonic-gate  * All rights reserved.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
30*7d7f5f75SToomas Soome /*	  All Rights Reserved	*/
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include	<sys/types.h>
357c478bd9Sstevel@tonic-gate #include	<stdio.h>
367c478bd9Sstevel@tonic-gate #include	<ctype.h>
377c478bd9Sstevel@tonic-gate #include	<userdefs.h>
387c478bd9Sstevel@tonic-gate #include	<users.h>
390a1278f2SGary Mills #include	<deflt.h>
407c478bd9Sstevel@tonic-gate #include	<limits.h>
417c478bd9Sstevel@tonic-gate 
420a1278f2SGary Mills /* Defaults file */
430a1278f2SGary Mills #define	DEFAULT_USERADD "/etc/default/useradd"
440a1278f2SGary Mills 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * validate string given as login name.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate int
valid_login(char * login,struct passwd ** pptr,int * warning)497c478bd9Sstevel@tonic-gate valid_login(char *login, struct passwd **pptr, int *warning)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	struct passwd *t_pptr;
527c478bd9Sstevel@tonic-gate 	char *ptr = login;
537c478bd9Sstevel@tonic-gate 	int bad1char, badc, clower, len;
547c478bd9Sstevel@tonic-gate 	char c;
550a1278f2SGary Mills 	char action;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	len = 0; clower = 0; badc = 0; bad1char = 0;
587c478bd9Sstevel@tonic-gate 	*warning = 0;
597c478bd9Sstevel@tonic-gate 	if (!login || !*login)
607c478bd9Sstevel@tonic-gate 		return (INVALID);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	c = *ptr;
637c478bd9Sstevel@tonic-gate 	if (!isalpha(c))
647c478bd9Sstevel@tonic-gate 		bad1char++;
65*7d7f5f75SToomas Soome 	for (; c != '\0'; ptr++, c = *ptr) {
667c478bd9Sstevel@tonic-gate 		len++;
677c478bd9Sstevel@tonic-gate 		if (!isprint(c) || (c == ':') || (c == '\n'))
687c478bd9Sstevel@tonic-gate 			return (INVALID);
697c478bd9Sstevel@tonic-gate 		if (!isalnum(c) && c != '_' && c != '-' && c != '.')
707c478bd9Sstevel@tonic-gate 			badc++;
717c478bd9Sstevel@tonic-gate 		if (islower(c))
727c478bd9Sstevel@tonic-gate 			clower++;
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 
750a1278f2SGary Mills 	action = 'w';
760a1278f2SGary Mills 	if (defopen(DEFAULT_USERADD) == 0) {
770a1278f2SGary Mills 		char *defptr;
780a1278f2SGary Mills 
790a1278f2SGary Mills 		if ((defptr = defread("EXCEED_TRAD=")) != NULL) {
800a1278f2SGary Mills 			char let = tolower(*defptr);
810a1278f2SGary Mills 
820a1278f2SGary Mills 			switch (let) {
830a1278f2SGary Mills 			case 'w':	/* warning */
840a1278f2SGary Mills 			case 'e':	/* error */
850a1278f2SGary Mills 			case 's':	/* silent */
860a1278f2SGary Mills 				action = let;
870a1278f2SGary Mills 				break;
880a1278f2SGary Mills 			}
890a1278f2SGary Mills 		}
900a1278f2SGary Mills 		(void) defopen((char *)NULL);
910a1278f2SGary Mills 	}
920a1278f2SGary Mills 
937c478bd9Sstevel@tonic-gate 	if (len > LOGNAME_MAX)
940a1278f2SGary Mills 		return (LONGNAME);
950a1278f2SGary Mills 
960a1278f2SGary Mills 	if (len > LOGNAME_MAX_TRAD) {
970a1278f2SGary Mills 		if (action == 'w')
980a1278f2SGary Mills 			*warning = *warning | WARN_NAME_TOO_LONG;
990a1278f2SGary Mills 		else if (action == 'e')
1000a1278f2SGary Mills 			return (LONGNAME);
1010a1278f2SGary Mills 	}
1020a1278f2SGary Mills 
1037c478bd9Sstevel@tonic-gate 	if (clower == 0)
1047c478bd9Sstevel@tonic-gate 		*warning = *warning | WARN_NO_LOWERCHAR;
1057c478bd9Sstevel@tonic-gate 	if (badc != 0)
1067c478bd9Sstevel@tonic-gate 		*warning = *warning | WARN_BAD_LOGNAME_CHAR;
1077c478bd9Sstevel@tonic-gate 	if (bad1char != 0)
1087c478bd9Sstevel@tonic-gate 		*warning = *warning | WARN_BAD_LOGNAME_FIRST;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if ((t_pptr = getpwnam(login)) != NULL) {
1117c478bd9Sstevel@tonic-gate 		if (pptr) *pptr = t_pptr;
1127c478bd9Sstevel@tonic-gate 		return (NOTUNIQUE);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	return (UNIQUE);
1157c478bd9Sstevel@tonic-gate }
116