xref: /illumos-gate/usr/src/cmd/grpck/grpck.c (revision 49335bde)
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 /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
26*49335bdeSbasabi 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
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;
70*49335bdeSbasabi 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 *
86*49335bdeSbasabi 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 
97*49335bdeSbasabi int
98*49335bdeSbasabi 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;
2137c478bd9Sstevel@tonic-gate 		gid = (gid_t)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 
2307c478bd9Sstevel@tonic-gate 		for (i = 0; buf[i] != NULL; i++)
2317c478bd9Sstevel@tonic-gate 		{
2327c478bd9Sstevel@tonic-gate 			if (buf[i] == ':')
2337c478bd9Sstevel@tonic-gate 			{
2347c478bd9Sstevel@tonic-gate 				delim[colons] = i;
2357c478bd9Sstevel@tonic-gate 				if (++colons > NUM_COLONS)
2367c478bd9Sstevel@tonic-gate 					break;
2377c478bd9Sstevel@tonic-gate 			}
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate 		if (colons != NUM_COLONS)
2407c478bd9Sstevel@tonic-gate 		{
2417c478bd9Sstevel@tonic-gate 			error(BADLINE);
2427c478bd9Sstevel@tonic-gate 			free(tmpbuf);
2437c478bd9Sstevel@tonic-gate 			continue;
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		/* check to see that group name is at least 1 character	*/
2477c478bd9Sstevel@tonic-gate 		/* and that all characters are lowrcase or digits.	*/
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 		if (buf[0] == ':')
2507c478bd9Sstevel@tonic-gate 			error(NONAME);
2517c478bd9Sstevel@tonic-gate 		else
2527c478bd9Sstevel@tonic-gate 		{
2537c478bd9Sstevel@tonic-gate 			for (i = 0; buf[i] != ':'; i++)
2547c478bd9Sstevel@tonic-gate 			{
2557c478bd9Sstevel@tonic-gate 				if (i >= LOGNAME_MAX)
2567c478bd9Sstevel@tonic-gate 					longnam++;
2577c478bd9Sstevel@tonic-gate 				if (!(islower(buf[i]) || isdigit(buf[i])))
2587c478bd9Sstevel@tonic-gate 					badchar++;
2597c478bd9Sstevel@tonic-gate 			}
2607c478bd9Sstevel@tonic-gate 			if (longnam > 0)
2617c478bd9Sstevel@tonic-gate 				error(LONGNAME);
2627c478bd9Sstevel@tonic-gate 			if (badchar > 0)
2637c478bd9Sstevel@tonic-gate 				error(BADNAME);
2647c478bd9Sstevel@tonic-gate 		}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		/*	check that GID is numeric and <= 31 bits	*/
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 		len = (delim[2] - delim[1]) - 1;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		if (len > 10 || len < 1)
2717c478bd9Sstevel@tonic-gate 			error(BADGID);
2727c478bd9Sstevel@tonic-gate 		else {
2737c478bd9Sstevel@tonic-gate 			for (i = (delim[1]+1); i < delim[2]; i++)
2747c478bd9Sstevel@tonic-gate 			{
2757c478bd9Sstevel@tonic-gate 				if (! (isdigit(buf[i])))
2767c478bd9Sstevel@tonic-gate 					baddigit++;
2777c478bd9Sstevel@tonic-gate 				else if (baddigit == 0)
2787c478bd9Sstevel@tonic-gate 					gid = gid * 10 + (gid_t)(buf[i] - '0');
2797c478bd9Sstevel@tonic-gate 				/* converts ascii GID to decimal */
2807c478bd9Sstevel@tonic-gate 			}
2817c478bd9Sstevel@tonic-gate 			if (baddigit > 0)
2827c478bd9Sstevel@tonic-gate 				error(BADGID);
2837c478bd9Sstevel@tonic-gate 			else if (gid < (gid_t)0)
2847c478bd9Sstevel@tonic-gate 				error(BADGID);
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		/*  check that logname appears in the passwd file  */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		nptr = &tmpbuf[delim[2]];
2907c478bd9Sstevel@tonic-gate 		nptr++;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		listlen = strlen(nptr) - 1;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		while ((cptr = strchr(nptr, ',')) != NULL)
2957c478bd9Sstevel@tonic-gate 		{
2967c478bd9Sstevel@tonic-gate 			*cptr = NULL;
2977c478bd9Sstevel@tonic-gate 			if (*nptr == NULL)
2987c478bd9Sstevel@tonic-gate 			{
2997c478bd9Sstevel@tonic-gate 				if (listlen)
3007c478bd9Sstevel@tonic-gate 					error(NULLNAME);
3017c478bd9Sstevel@tonic-gate 				nptr++;
3027c478bd9Sstevel@tonic-gate 				continue;
3037c478bd9Sstevel@tonic-gate 			}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 			for (t = root; t != NULL; t = t->next) {
3067c478bd9Sstevel@tonic-gate 				if (strcmp(t->user, nptr) == 0)
3077c478bd9Sstevel@tonic-gate 					break;
3087c478bd9Sstevel@tonic-gate 			}
3097c478bd9Sstevel@tonic-gate 			if (t == NULL) {
3107c478bd9Sstevel@tonic-gate #ifndef ORIG_SVR4
3117c478bd9Sstevel@tonic-gate 				/*
3127c478bd9Sstevel@tonic-gate 				 * User entry not found, so check if in
3137c478bd9Sstevel@tonic-gate 				 *  password file
3147c478bd9Sstevel@tonic-gate 				 */
3157c478bd9Sstevel@tonic-gate 				struct passwd *pwp;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 				if ((pwp = getpwnam(nptr)) == NULL) {
3187c478bd9Sstevel@tonic-gate #endif
3197c478bd9Sstevel@tonic-gate 					badlognam++;
3207c478bd9Sstevel@tonic-gate 					error(NOTFOUND);
3217c478bd9Sstevel@tonic-gate 					goto getnext;
3227c478bd9Sstevel@tonic-gate #ifndef ORIG_SVR4
3237c478bd9Sstevel@tonic-gate 				}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 				/* Usrname found, so add entry to user-list */
3267c478bd9Sstevel@tonic-gate 				t = (struct node *)
3277c478bd9Sstevel@tonic-gate 					emalloc(sizeof (*t) + strlen(nptr));
3287c478bd9Sstevel@tonic-gate 				t->next = root;
3297c478bd9Sstevel@tonic-gate 				root = t;
3307c478bd9Sstevel@tonic-gate 				strcpy(t->user, nptr);
3317c478bd9Sstevel@tonic-gate 				t->ngroups = 1;
3327c478bd9Sstevel@tonic-gate 				if (!ngroups_max)
3337c478bd9Sstevel@tonic-gate 					t->groups = NULL;
3347c478bd9Sstevel@tonic-gate 				else {
3357c478bd9Sstevel@tonic-gate 					t->groups = (struct group *)
3367c478bd9Sstevel@tonic-gate 						emalloc(sizeof (struct group));
3377c478bd9Sstevel@tonic-gate 					t->groups->grp = pwp->pw_gid;
3387c478bd9Sstevel@tonic-gate 					t->groups->cnt = 1;
3397c478bd9Sstevel@tonic-gate 					t->groups->nxt = NULL;
3407c478bd9Sstevel@tonic-gate 				}
3417c478bd9Sstevel@tonic-gate 			}
3427c478bd9Sstevel@tonic-gate #endif
3437c478bd9Sstevel@tonic-gate 			if (!ngroups_max)
3447c478bd9Sstevel@tonic-gate 				goto getnext;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 			t->ngroups++;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 			/*
3497c478bd9Sstevel@tonic-gate 			 * check for duplicate logname in group
3507c478bd9Sstevel@tonic-gate 			 */
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 			for (gp = t->groups; gp != NULL; gp = gp->nxt) {
3537c478bd9Sstevel@tonic-gate 				if (gid == gp->grp) {
3547c478bd9Sstevel@tonic-gate 					if (gp->cnt++ == 1) {
3557c478bd9Sstevel@tonic-gate 						badlognam++;
3567c478bd9Sstevel@tonic-gate 						if (gp->nxt == NULL)
3577c478bd9Sstevel@tonic-gate 							error(DUPNAME2);
3587c478bd9Sstevel@tonic-gate 						else
3597c478bd9Sstevel@tonic-gate 							error(DUPNAME);
3607c478bd9Sstevel@tonic-gate 					}
3617c478bd9Sstevel@tonic-gate 					goto getnext;
3627c478bd9Sstevel@tonic-gate 				}
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 			gp = (struct group *)emalloc(sizeof (struct group));
3667c478bd9Sstevel@tonic-gate 			gp->grp = gid;
3677c478bd9Sstevel@tonic-gate 			gp->cnt = 1;
3687c478bd9Sstevel@tonic-gate 			gp->nxt = t->groups;
3697c478bd9Sstevel@tonic-gate 			t->groups = gp;
3707c478bd9Sstevel@tonic-gate getnext:
3717c478bd9Sstevel@tonic-gate 			nptr = ++cptr;
3727c478bd9Sstevel@tonic-gate 		}
3737c478bd9Sstevel@tonic-gate 		free(tmpbuf);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (ngroups == 0) {
3777c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Group file '%s' is empty\n"), argv[1]);
3787c478bd9Sstevel@tonic-gate 		code = 1;
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	if (ngroups_max) {
3827c478bd9Sstevel@tonic-gate 		for (t = root; t != NULL; t = t->next) {
3837c478bd9Sstevel@tonic-gate 			if (t->ngroups > ngroups_max) {
3847c478bd9Sstevel@tonic-gate 				fprintf(stderr, "\n\n%s%s (%d)\n",
3857c478bd9Sstevel@tonic-gate 				NGROUPS, t->user, t->ngroups);
3867c478bd9Sstevel@tonic-gate 				code = 1;
3877c478bd9Sstevel@tonic-gate 			}
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 	}
390*49335bdeSbasabi 	return (code);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate /*	Error printing routine	*/
3947c478bd9Sstevel@tonic-gate 
395*49335bdeSbasabi void
396*49335bdeSbasabi error(char *msg)
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate 	code = 1;
3997c478bd9Sstevel@tonic-gate 	if (eflag == 0)
4007c478bd9Sstevel@tonic-gate 	{
4017c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\n\n%s", buf);
4027c478bd9Sstevel@tonic-gate 		eflag = 1;
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 	if (longnam != 0)
4057c478bd9Sstevel@tonic-gate 	{
4067c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%s\n", gettext(msg));
4077c478bd9Sstevel@tonic-gate 		longnam = 0;
4087c478bd9Sstevel@tonic-gate 		return;
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	if (badchar != 0)
4117c478bd9Sstevel@tonic-gate 	{
4127c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%d %s\n", badchar, gettext(msg));
4137c478bd9Sstevel@tonic-gate 		badchar = 0;
4147c478bd9Sstevel@tonic-gate 		return;
4157c478bd9Sstevel@tonic-gate 	} else if (baddigit != 0)
4167c478bd9Sstevel@tonic-gate 	{
4177c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%s\n", gettext(msg));
4187c478bd9Sstevel@tonic-gate 		baddigit = 0;
4197c478bd9Sstevel@tonic-gate 		return;
4207c478bd9Sstevel@tonic-gate 	} else if (badlognam != 0)
4217c478bd9Sstevel@tonic-gate 	{
4227c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%s - %s\n", nptr, gettext(msg));
4237c478bd9Sstevel@tonic-gate 		badlognam = 0;
4247c478bd9Sstevel@tonic-gate 		return;
4257c478bd9Sstevel@tonic-gate 	} else
4267c478bd9Sstevel@tonic-gate 	{
4277c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%s\n", gettext(msg));
4287c478bd9Sstevel@tonic-gate 		return;
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate }
431