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 /*
23*c241e583SGary Mills * Copyright (c) 2013 Gary Mills
24*c241e583SGary 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 */
307c478bd9Sstevel@tonic-gate /* 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>
39*c241e583SGary Mills #include <deflt.h>
407c478bd9Sstevel@tonic-gate #include <limits.h>
417c478bd9Sstevel@tonic-gate
42*c241e583SGary Mills /* Defaults file */
43*c241e583SGary Mills #define DEFAULT_USERADD "/etc/default/useradd"
44*c241e583SGary 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;
55*c241e583SGary 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++;
657c478bd9Sstevel@tonic-gate for (; c != NULL; 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
75*c241e583SGary Mills action = 'w';
76*c241e583SGary Mills if (defopen(DEFAULT_USERADD) == 0) {
77*c241e583SGary Mills char *defptr;
78*c241e583SGary Mills
79*c241e583SGary Mills if ((defptr = defread("EXCEED_TRAD=")) != NULL) {
80*c241e583SGary Mills char let = tolower(*defptr);
81*c241e583SGary Mills
82*c241e583SGary Mills switch (let) {
83*c241e583SGary Mills case 'w': /* warning */
84*c241e583SGary Mills case 'e': /* error */
85*c241e583SGary Mills case 's': /* silent */
86*c241e583SGary Mills action = let;
87*c241e583SGary Mills break;
88*c241e583SGary Mills }
89*c241e583SGary Mills }
90*c241e583SGary Mills (void) defopen((char *)NULL);
91*c241e583SGary Mills }
92*c241e583SGary Mills
937c478bd9Sstevel@tonic-gate if (len > LOGNAME_MAX)
94*c241e583SGary Mills return (LONGNAME);
95*c241e583SGary Mills
96*c241e583SGary Mills if (len > LOGNAME_MAX_TRAD) {
97*c241e583SGary Mills if (action == 'w')
987c478bd9Sstevel@tonic-gate *warning = *warning | WARN_NAME_TOO_LONG;
99*c241e583SGary Mills else if (action == 'e')
100*c241e583SGary Mills return (LONGNAME);
101*c241e583SGary Mills }
102*c241e583SGary 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