xref: /illumos-gate/usr/src/cmd/grpck/grpck.c (revision f48205be)
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
5*f48205beScasper  * Common Development and Distribution License (the "License").
6*f48205beScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*f48205beScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
2549335bdeSbasabi 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
32*f48205beScasper #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <ctype.h>
397c478bd9Sstevel@tonic-gate #include <pwd.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <locale.h>
427c478bd9Sstevel@tonic-gate #include <limits.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	BADLINE "Too many/few fields"
457c478bd9Sstevel@tonic-gate #define	TOOLONG "Line too long"
467c478bd9Sstevel@tonic-gate #define	NONAME	"No group name"
477c478bd9Sstevel@tonic-gate #define	BADNAME "Bad character(s) in group name"
487c478bd9Sstevel@tonic-gate #define	BADGID  "Invalid GID"
497c478bd9Sstevel@tonic-gate #define	NULLNAME "Null login name"
507c478bd9Sstevel@tonic-gate #define	NOTFOUND "Logname not found in password file"
517c478bd9Sstevel@tonic-gate #define	DUPNAME "Duplicate logname entry"
527c478bd9Sstevel@tonic-gate #define	DUPNAME2 "Duplicate logname entry (gid first occurs in passwd entry)"
537c478bd9Sstevel@tonic-gate #define	NOMEM	"Out of memory"
547c478bd9Sstevel@tonic-gate #define	NGROUPS	"Maximum groups exceeded for logname "
557c478bd9Sstevel@tonic-gate #define	BLANKLINE "Blank line detected. Please remove line"
567c478bd9Sstevel@tonic-gate #define	LONGNAME  "Group name too long"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate int eflag, badchar, baddigit, badlognam, colons, len;
597c478bd9Sstevel@tonic-gate static int longnam = 0;
607c478bd9Sstevel@tonic-gate int code;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	MYBUFSIZE (LINE_MAX)	/* max line length including newline and null */
637c478bd9Sstevel@tonic-gate #define	NUM_COLONS	3
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate char *buf;
667c478bd9Sstevel@tonic-gate char *nptr;
677c478bd9Sstevel@tonic-gate char *cptr;
687c478bd9Sstevel@tonic-gate FILE *fptr;
697c478bd9Sstevel@tonic-gate gid_t gid;
7049335bdeSbasabi void error(char *msg);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate struct group {
737c478bd9Sstevel@tonic-gate 	struct group *nxt;
747c478bd9Sstevel@tonic-gate 	int cnt;
757c478bd9Sstevel@tonic-gate 	gid_t grp;
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate struct node {
797c478bd9Sstevel@tonic-gate 	struct node *next;
807c478bd9Sstevel@tonic-gate 	int ngroups;
817c478bd9Sstevel@tonic-gate 	struct group *groups;
827c478bd9Sstevel@tonic-gate 	char user[1];
837c478bd9Sstevel@tonic-gate };
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate void *
8649335bdeSbasabi emalloc(size_t size)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	void *vp;
897c478bd9Sstevel@tonic-gate 	vp = malloc(size);
907c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
917c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s\n", gettext(NOMEM));
927c478bd9Sstevel@tonic-gate 		exit(1);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 	return (vp);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
9749335bdeSbasabi int
9849335bdeSbasabi main(int argc, char *argv[])
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	struct passwd *pwp;
1017c478bd9Sstevel@tonic-gate 	struct node *root = NULL;
1027c478bd9Sstevel@tonic-gate 	struct node *t;
1037c478bd9Sstevel@tonic-gate 	struct group *gp;
1047c478bd9Sstevel@tonic-gate 	int ngroups_max;
1057c478bd9Sstevel@tonic-gate 	int ngroups = 0;
1067c478bd9Sstevel@tonic-gate 	int listlen;
1077c478bd9Sstevel@tonic-gate 	int i;
1087c478bd9Sstevel@tonic-gate 	int lineno = 0;
1097c478bd9Sstevel@tonic-gate 	char *buf_off, *tmpbuf;
1107c478bd9Sstevel@tonic-gate 	int delim[NUM_COLONS + 1], buf_len, bufsize;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1157c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1167c478bd9Sstevel@tonic-gate #endif
1177c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	code = 0;
1207c478bd9Sstevel@tonic-gate 	ngroups_max = sysconf(_SC_NGROUPS_MAX);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (argc == 1)
1237c478bd9Sstevel@tonic-gate 		argv[1] = "/etc/group";
1247c478bd9Sstevel@tonic-gate 	else if (argc != 2) {
1257c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("usage: %s filename\n"), *argv);
1267c478bd9Sstevel@tonic-gate 		exit(1);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if ((fptr = fopen(argv[1], "r")) == NULL) {
1307c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("cannot open file %s: %s\n"), argv[1],
1317c478bd9Sstevel@tonic-gate 			strerror(errno));
1327c478bd9Sstevel@tonic-gate 		exit(1);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #ifdef ORIG_SVR4
1367c478bd9Sstevel@tonic-gate 	while ((pwp = getpwent()) != NULL) {
1377c478bd9Sstevel@tonic-gate 		t = (struct node *)emalloc(sizeof (*t) + strlen(pwp->pw_name));
1387c478bd9Sstevel@tonic-gate 		t->next = root;
1397c478bd9Sstevel@tonic-gate 		root = t;
1407c478bd9Sstevel@tonic-gate 		strcpy(t->user, pwp->pw_name);
1417c478bd9Sstevel@tonic-gate 		t->ngroups = 1;
1427c478bd9Sstevel@tonic-gate 		if (!ngroups_max)
1437c478bd9Sstevel@tonic-gate 			t->groups = NULL;
1447c478bd9Sstevel@tonic-gate 		else {
1457c478bd9Sstevel@tonic-gate 			t->groups = (struct group *)
1467c478bd9Sstevel@tonic-gate 				emalloc(sizeof (struct group));
1477c478bd9Sstevel@tonic-gate 			t->groups->grp = pwp->pw_gid;
1487c478bd9Sstevel@tonic-gate 			t->groups->cnt = 1;
1497c478bd9Sstevel@tonic-gate 			t->groups->nxt = NULL;
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate #endif
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	bufsize = MYBUFSIZE;
1557c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
1567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(NOMEM));
1577c478bd9Sstevel@tonic-gate 		exit(1);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 	while (!feof(fptr) && !ferror(fptr)) {
1607c478bd9Sstevel@tonic-gate 		buf_len = 0;
1617c478bd9Sstevel@tonic-gate 		buf_off = buf;
1627c478bd9Sstevel@tonic-gate 		while (fgets(buf_off, (bufsize - buf_len), fptr) != NULL) {
1637c478bd9Sstevel@tonic-gate 			buf_len += strlen(buf_off);
1647c478bd9Sstevel@tonic-gate 			if (buf[buf_len - 1] == '\n' || feof(fptr))
1657c478bd9Sstevel@tonic-gate 				break;
1667c478bd9Sstevel@tonic-gate 			tmpbuf = realloc(buf, (bufsize + MYBUFSIZE));
1677c478bd9Sstevel@tonic-gate 			if (tmpbuf == NULL) {
1687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(NOMEM));
1697c478bd9Sstevel@tonic-gate 				exit(1);
1707c478bd9Sstevel@tonic-gate 			}
1717c478bd9Sstevel@tonic-gate 			bufsize += MYBUFSIZE;
1727c478bd9Sstevel@tonic-gate 			buf = tmpbuf;
1737c478bd9Sstevel@tonic-gate 			buf_off = buf + buf_len;
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 		if (buf_len == 0)
1767c478bd9Sstevel@tonic-gate 			continue;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 		/* Report error to be consistent with libc */
1797c478bd9Sstevel@tonic-gate 		if ((buf_len + 1) > LINE_MAX)
1807c478bd9Sstevel@tonic-gate 			error(TOOLONG);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		lineno++;
1837c478bd9Sstevel@tonic-gate 		if (buf[0] == '\n')    /* blank lines are ignored */
1847c478bd9Sstevel@tonic-gate 		{
1857c478bd9Sstevel@tonic-gate 			code = 1;		/* exit with error code = 1 */
1867c478bd9Sstevel@tonic-gate 			eflag = 0;	/* force print of "blank" line */
1877c478bd9Sstevel@tonic-gate 			fprintf(stderr, "\n%s %d\n", gettext(BLANKLINE),
1887c478bd9Sstevel@tonic-gate 				lineno);
1897c478bd9Sstevel@tonic-gate 			continue;
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		if (buf[buf_len - 1] == '\n') {
1937c478bd9Sstevel@tonic-gate 			if ((tmpbuf = strdup(buf)) == NULL) {
1947c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(NOMEM));
1957c478bd9Sstevel@tonic-gate 				exit(1);
1967c478bd9Sstevel@tonic-gate 			}
1977c478bd9Sstevel@tonic-gate 			tmpbuf[buf_len - 1] = ',';
1987c478bd9Sstevel@tonic-gate 		} else {
1997c478bd9Sstevel@tonic-gate 			if ((tmpbuf = malloc(buf_len + 2)) == NULL) {
2007c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(NOMEM));
2017c478bd9Sstevel@tonic-gate 				exit(1);
2027c478bd9Sstevel@tonic-gate 			}
2037c478bd9Sstevel@tonic-gate 			(void) strcpy(tmpbuf, buf);
2047c478bd9Sstevel@tonic-gate 			tmpbuf[buf_len++] = ',';
2057c478bd9Sstevel@tonic-gate 			tmpbuf[buf_len] = '\0';
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		colons = 0;
2097c478bd9Sstevel@tonic-gate 		eflag = 0;
2107c478bd9Sstevel@tonic-gate 		badchar = 0;
2117c478bd9Sstevel@tonic-gate 		baddigit = 0;
2127c478bd9Sstevel@tonic-gate 		badlognam = 0;
213*f48205beScasper 		gid = 0;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		ngroups++;	/* Increment number of groups found */
2167c478bd9Sstevel@tonic-gate 		/* Check that entry is not a nameservice redirection */
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		if (buf[0] == '+' || buf[0] == '-')  {
2197c478bd9Sstevel@tonic-gate 			/*
2207c478bd9Sstevel@tonic-gate 			 * Should set flag here to allow special case checking
2217c478bd9Sstevel@tonic-gate 			 * in the rest of the code,
2227c478bd9Sstevel@tonic-gate 			 * but for now, we'll just ignore this entry.
2237c478bd9Sstevel@tonic-gate 			 */
2247c478bd9Sstevel@tonic-gate 			free(tmpbuf);
2257c478bd9Sstevel@tonic-gate 			continue;
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		/*	Check number of fields	*/
2297c478bd9Sstevel@tonic-gate 
2301316014bSbasabi 		for (i = 0; buf[i] != NULL; i++) {
2311316014bSbasabi 			if (buf[i] == ':') {
2327c478bd9Sstevel@tonic-gate 				delim[colons] = i;
2337c478bd9Sstevel@tonic-gate 				if (++colons > NUM_COLONS)
2347c478bd9Sstevel@tonic-gate 					break;
2357c478bd9Sstevel@tonic-gate 			}
2367c478bd9Sstevel@tonic-gate 		}
2371316014bSbasabi 		if (colons != NUM_COLONS) {
2387c478bd9Sstevel@tonic-gate 			error(BADLINE);
2397c478bd9Sstevel@tonic-gate 			free(tmpbuf);
2407c478bd9Sstevel@tonic-gate 			continue;
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		/* check to see that group name is at least 1 character	*/
2447c478bd9Sstevel@tonic-gate 		/* and that all characters are lowrcase or digits.	*/
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		if (buf[0] == ':')
2477c478bd9Sstevel@tonic-gate 			error(NONAME);
2481316014bSbasabi 		else {
2491316014bSbasabi 			for (i = 0; buf[i] != ':'; i++) {
2507c478bd9Sstevel@tonic-gate 				if (i >= LOGNAME_MAX)
2517c478bd9Sstevel@tonic-gate 					longnam++;
2527c478bd9Sstevel@tonic-gate 				if (!(islower(buf[i]) || isdigit(buf[i])))
2537c478bd9Sstevel@tonic-gate 					badchar++;
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 			if (longnam > 0)
2567c478bd9Sstevel@tonic-gate 				error(LONGNAME);
2577c478bd9Sstevel@tonic-gate 			if (badchar > 0)
2587c478bd9Sstevel@tonic-gate 				error(BADNAME);
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		/*	check that GID is numeric and <= 31 bits	*/
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		len = (delim[2] - delim[1]) - 1;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		if (len > 10 || len < 1)
2667c478bd9Sstevel@tonic-gate 			error(BADGID);
2677c478bd9Sstevel@tonic-gate 		else {
2681316014bSbasabi 			for (i = (delim[1]+1); i < delim[2]; i++) {
2697c478bd9Sstevel@tonic-gate 				if (! (isdigit(buf[i])))
2707c478bd9Sstevel@tonic-gate 					baddigit++;
2717c478bd9Sstevel@tonic-gate 				else if (baddigit == 0)
2727c478bd9Sstevel@tonic-gate 					gid = gid * 10 + (gid_t)(buf[i] - '0');
2737c478bd9Sstevel@tonic-gate 				/* converts ascii GID to decimal */
2747c478bd9Sstevel@tonic-gate 			}
2757c478bd9Sstevel@tonic-gate 			if (baddigit > 0)
2767c478bd9Sstevel@tonic-gate 				error(BADGID);
277*f48205beScasper 			else if (gid > (gid_t)MAXUID)
2787c478bd9Sstevel@tonic-gate 				error(BADGID);
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		/*  check that logname appears in the passwd file  */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		nptr = &tmpbuf[delim[2]];
2847c478bd9Sstevel@tonic-gate 		nptr++;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		listlen = strlen(nptr) - 1;
2877c478bd9Sstevel@tonic-gate 
2881316014bSbasabi 		while ((cptr = strchr(nptr, ',')) != NULL) {
2897c478bd9Sstevel@tonic-gate 			*cptr = NULL;
2901316014bSbasabi 			if (*nptr == NULL) {
2917c478bd9Sstevel@tonic-gate 				if (listlen)
2927c478bd9Sstevel@tonic-gate 					error(NULLNAME);
2937c478bd9Sstevel@tonic-gate 				nptr++;
2947c478bd9Sstevel@tonic-gate 				continue;
2957c478bd9Sstevel@tonic-gate 			}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 			for (t = root; t != NULL; t = t->next) {
2987c478bd9Sstevel@tonic-gate 				if (strcmp(t->user, nptr) == 0)
2997c478bd9Sstevel@tonic-gate 					break;
3007c478bd9Sstevel@tonic-gate 			}
3017c478bd9Sstevel@tonic-gate 			if (t == NULL) {
3027c478bd9Sstevel@tonic-gate #ifndef ORIG_SVR4
3037c478bd9Sstevel@tonic-gate 				/*
3047c478bd9Sstevel@tonic-gate 				 * User entry not found, so check if in
3057c478bd9Sstevel@tonic-gate 				 *  password file
3067c478bd9Sstevel@tonic-gate 				 */
3077c478bd9Sstevel@tonic-gate 				struct passwd *pwp;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 				if ((pwp = getpwnam(nptr)) == NULL) {
3107c478bd9Sstevel@tonic-gate #endif
3117c478bd9Sstevel@tonic-gate 					badlognam++;
3127c478bd9Sstevel@tonic-gate 					error(NOTFOUND);
3137c478bd9Sstevel@tonic-gate 					goto getnext;
3147c478bd9Sstevel@tonic-gate #ifndef ORIG_SVR4
3157c478bd9Sstevel@tonic-gate 				}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 				/* Usrname found, so add entry to user-list */
3187c478bd9Sstevel@tonic-gate 				t = (struct node *)
3197c478bd9Sstevel@tonic-gate 					emalloc(sizeof (*t) + strlen(nptr));
3207c478bd9Sstevel@tonic-gate 				t->next = root;
3217c478bd9Sstevel@tonic-gate 				root = t;
3227c478bd9Sstevel@tonic-gate 				strcpy(t->user, nptr);
3237c478bd9Sstevel@tonic-gate 				t->ngroups = 1;
3247c478bd9Sstevel@tonic-gate 				if (!ngroups_max)
3257c478bd9Sstevel@tonic-gate 					t->groups = NULL;
3267c478bd9Sstevel@tonic-gate 				else {
3277c478bd9Sstevel@tonic-gate 					t->groups = (struct group *)
3287c478bd9Sstevel@tonic-gate 						emalloc(sizeof (struct group));
3297c478bd9Sstevel@tonic-gate 					t->groups->grp = pwp->pw_gid;
3307c478bd9Sstevel@tonic-gate 					t->groups->cnt = 1;
3317c478bd9Sstevel@tonic-gate 					t->groups->nxt = NULL;
3327c478bd9Sstevel@tonic-gate 				}
3337c478bd9Sstevel@tonic-gate 			}
3347c478bd9Sstevel@tonic-gate #endif
3357c478bd9Sstevel@tonic-gate 			if (!ngroups_max)
3367c478bd9Sstevel@tonic-gate 				goto getnext;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 			t->ngroups++;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 			/*
3417c478bd9Sstevel@tonic-gate 			 * check for duplicate logname in group
3427c478bd9Sstevel@tonic-gate 			 */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 			for (gp = t->groups; gp != NULL; gp = gp->nxt) {
3457c478bd9Sstevel@tonic-gate 				if (gid == gp->grp) {
3467c478bd9Sstevel@tonic-gate 					if (gp->cnt++ == 1) {
3477c478bd9Sstevel@tonic-gate 						badlognam++;
3487c478bd9Sstevel@tonic-gate 						if (gp->nxt == NULL)
3497c478bd9Sstevel@tonic-gate 							error(DUPNAME2);
3507c478bd9Sstevel@tonic-gate 						else
3517c478bd9Sstevel@tonic-gate 							error(DUPNAME);
3527c478bd9Sstevel@tonic-gate 					}
3537c478bd9Sstevel@tonic-gate 					goto getnext;
3547c478bd9Sstevel@tonic-gate 				}
3557c478bd9Sstevel@tonic-gate 			}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 			gp = (struct group *)emalloc(sizeof (struct group));
3587c478bd9Sstevel@tonic-gate 			gp->grp = gid;
3597c478bd9Sstevel@tonic-gate 			gp->cnt = 1;
3607c478bd9Sstevel@tonic-gate 			gp->nxt = t->groups;
3617c478bd9Sstevel@tonic-gate 			t->groups = gp;
3627c478bd9Sstevel@tonic-gate getnext:
3637c478bd9Sstevel@tonic-gate 			nptr = ++cptr;
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 		free(tmpbuf);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (ngroups == 0) {
3697c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Group file '%s' is empty\n"), argv[1]);
3707c478bd9Sstevel@tonic-gate 		code = 1;
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	if (ngroups_max) {
3747c478bd9Sstevel@tonic-gate 		for (t = root; t != NULL; t = t->next) {
3757c478bd9Sstevel@tonic-gate 			if (t->ngroups > ngroups_max) {
3767c478bd9Sstevel@tonic-gate 				fprintf(stderr, "\n\n%s%s (%d)\n",
3777c478bd9Sstevel@tonic-gate 				NGROUPS, t->user, t->ngroups);
3787c478bd9Sstevel@tonic-gate 				code = 1;
3797c478bd9Sstevel@tonic-gate 			}
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 	}
38249335bdeSbasabi 	return (code);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate /*	Error printing routine	*/
3867c478bd9Sstevel@tonic-gate 
38749335bdeSbasabi void
38849335bdeSbasabi error(char *msg)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	code = 1;
3911316014bSbasabi 	if (eflag == 0) {
3927c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\n\n%s", buf);
3937c478bd9Sstevel@tonic-gate 		eflag = 1;
3947c478bd9Sstevel@tonic-gate 	}
3951316014bSbasabi 	if (longnam != 0) {
3967c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%s\n", gettext(msg));
3977c478bd9Sstevel@tonic-gate 		longnam = 0;
3987c478bd9Sstevel@tonic-gate 		return;
3997c478bd9Sstevel@tonic-gate 	}
4001316014bSbasabi 	if (badchar != 0) {
4017c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%d %s\n", badchar, gettext(msg));
4027c478bd9Sstevel@tonic-gate 		badchar = 0;
4037c478bd9Sstevel@tonic-gate 		return;
4041316014bSbasabi 	} else if (baddigit != 0) {
4057c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%s\n", gettext(msg));
4067c478bd9Sstevel@tonic-gate 		baddigit = 0;
4077c478bd9Sstevel@tonic-gate 		return;
4081316014bSbasabi 	} else if (badlognam != 0) {
4097c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%s - %s\n", nptr, gettext(msg));
4107c478bd9Sstevel@tonic-gate 		badlognam = 0;
4117c478bd9Sstevel@tonic-gate 		return;
4121316014bSbasabi 	} else {
4137c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%s\n", gettext(msg));
4147c478bd9Sstevel@tonic-gate 		return;
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate }
417