xref: /illumos-gate/usr/src/cmd/grpck/grpck.c (revision 869657d0)
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
5f48205beScasper  * Common Development and Distribution License (the "License").
6f48205beScasper  * 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 /*
22f48205beScasper  * 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	*/
27*869657d0SToomas Soome /*	  All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
29f48205beScasper #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <pwd.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <locale.h>
397c478bd9Sstevel@tonic-gate #include <limits.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	BADLINE "Too many/few fields"
427c478bd9Sstevel@tonic-gate #define	TOOLONG "Line too long"
437c478bd9Sstevel@tonic-gate #define	NONAME	"No group name"
447c478bd9Sstevel@tonic-gate #define	BADNAME "Bad character(s) in group name"
457c478bd9Sstevel@tonic-gate #define	BADGID  "Invalid GID"
467c478bd9Sstevel@tonic-gate #define	NULLNAME "Null login name"
477c478bd9Sstevel@tonic-gate #define	NOTFOUND "Logname not found in password file"
487c478bd9Sstevel@tonic-gate #define	DUPNAME "Duplicate logname entry"
497c478bd9Sstevel@tonic-gate #define	DUPNAME2 "Duplicate logname entry (gid first occurs in passwd entry)"
507c478bd9Sstevel@tonic-gate #define	NOMEM	"Out of memory"
517c478bd9Sstevel@tonic-gate #define	NGROUPS	"Maximum groups exceeded for logname "
527c478bd9Sstevel@tonic-gate #define	BLANKLINE "Blank line detected. Please remove line"
537c478bd9Sstevel@tonic-gate #define	LONGNAME  "Group name too long"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate int eflag, badchar, baddigit, badlognam, colons, len;
567c478bd9Sstevel@tonic-gate static int longnam = 0;
577c478bd9Sstevel@tonic-gate int code;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	MYBUFSIZE (LINE_MAX)	/* max line length including newline and null */
607c478bd9Sstevel@tonic-gate #define	NUM_COLONS	3
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate char *buf;
637c478bd9Sstevel@tonic-gate char *nptr;
647c478bd9Sstevel@tonic-gate char *cptr;
657c478bd9Sstevel@tonic-gate FILE *fptr;
667c478bd9Sstevel@tonic-gate gid_t gid;
6749335bdeSbasabi void error(char *msg);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate struct group {
707c478bd9Sstevel@tonic-gate 	struct group *nxt;
717c478bd9Sstevel@tonic-gate 	int cnt;
727c478bd9Sstevel@tonic-gate 	gid_t grp;
737c478bd9Sstevel@tonic-gate };
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate struct node {
767c478bd9Sstevel@tonic-gate 	struct node *next;
777c478bd9Sstevel@tonic-gate 	int ngroups;
787c478bd9Sstevel@tonic-gate 	struct group *groups;
797c478bd9Sstevel@tonic-gate 	char user[1];
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate void *
emalloc(size_t size)8349335bdeSbasabi emalloc(size_t size)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	void *vp;
867c478bd9Sstevel@tonic-gate 	vp = malloc(size);
877c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
887c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s\n", gettext(NOMEM));
897c478bd9Sstevel@tonic-gate 		exit(1);
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 	return (vp);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
9449335bdeSbasabi int
main(int argc,char * argv[])9549335bdeSbasabi main(int argc, char *argv[])
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	struct passwd *pwp;
987c478bd9Sstevel@tonic-gate 	struct node *root = NULL;
997c478bd9Sstevel@tonic-gate 	struct node *t;
1007c478bd9Sstevel@tonic-gate 	struct group *gp;
1017c478bd9Sstevel@tonic-gate 	int ngroups_max;
1027c478bd9Sstevel@tonic-gate 	int ngroups = 0;
1037c478bd9Sstevel@tonic-gate 	int listlen;
1047c478bd9Sstevel@tonic-gate 	int i;
1057c478bd9Sstevel@tonic-gate 	int lineno = 0;
1067c478bd9Sstevel@tonic-gate 	char *buf_off, *tmpbuf;
1077c478bd9Sstevel@tonic-gate 	int delim[NUM_COLONS + 1], buf_len, bufsize;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1127c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1137c478bd9Sstevel@tonic-gate #endif
1147c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	code = 0;
1177c478bd9Sstevel@tonic-gate 	ngroups_max = sysconf(_SC_NGROUPS_MAX);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (argc == 1)
1207c478bd9Sstevel@tonic-gate 		argv[1] = "/etc/group";
1217c478bd9Sstevel@tonic-gate 	else if (argc != 2) {
1227c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("usage: %s filename\n"), *argv);
1237c478bd9Sstevel@tonic-gate 		exit(1);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if ((fptr = fopen(argv[1], "r")) == NULL) {
1277c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("cannot open file %s: %s\n"), argv[1],
128*869657d0SToomas Soome 		    strerror(errno));
1297c478bd9Sstevel@tonic-gate 		exit(1);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #ifdef ORIG_SVR4
1337c478bd9Sstevel@tonic-gate 	while ((pwp = getpwent()) != NULL) {
1347c478bd9Sstevel@tonic-gate 		t = (struct node *)emalloc(sizeof (*t) + strlen(pwp->pw_name));
1357c478bd9Sstevel@tonic-gate 		t->next = root;
1367c478bd9Sstevel@tonic-gate 		root = t;
1377c478bd9Sstevel@tonic-gate 		strcpy(t->user, pwp->pw_name);
1387c478bd9Sstevel@tonic-gate 		t->ngroups = 1;
1397c478bd9Sstevel@tonic-gate 		if (!ngroups_max)
1407c478bd9Sstevel@tonic-gate 			t->groups = NULL;
1417c478bd9Sstevel@tonic-gate 		else {
1427c478bd9Sstevel@tonic-gate 			t->groups = (struct group *)
143*869657d0SToomas Soome 			    emalloc(sizeof (struct group));
1447c478bd9Sstevel@tonic-gate 			t->groups->grp = pwp->pw_gid;
1457c478bd9Sstevel@tonic-gate 			t->groups->cnt = 1;
1467c478bd9Sstevel@tonic-gate 			t->groups->nxt = NULL;
1477c478bd9Sstevel@tonic-gate 		}
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate #endif
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	bufsize = MYBUFSIZE;
1527c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
1537c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(NOMEM));
1547c478bd9Sstevel@tonic-gate 		exit(1);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 	while (!feof(fptr) && !ferror(fptr)) {
1577c478bd9Sstevel@tonic-gate 		buf_len = 0;
1587c478bd9Sstevel@tonic-gate 		buf_off = buf;
1597c478bd9Sstevel@tonic-gate 		while (fgets(buf_off, (bufsize - buf_len), fptr) != NULL) {
1607c478bd9Sstevel@tonic-gate 			buf_len += strlen(buf_off);
1617c478bd9Sstevel@tonic-gate 			if (buf[buf_len - 1] == '\n' || feof(fptr))
1627c478bd9Sstevel@tonic-gate 				break;
1637c478bd9Sstevel@tonic-gate 			tmpbuf = realloc(buf, (bufsize + MYBUFSIZE));
1647c478bd9Sstevel@tonic-gate 			if (tmpbuf == NULL) {
1657c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(NOMEM));
1667c478bd9Sstevel@tonic-gate 				exit(1);
1677c478bd9Sstevel@tonic-gate 			}
1687c478bd9Sstevel@tonic-gate 			bufsize += MYBUFSIZE;
1697c478bd9Sstevel@tonic-gate 			buf = tmpbuf;
1707c478bd9Sstevel@tonic-gate 			buf_off = buf + buf_len;
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 		if (buf_len == 0)
1737c478bd9Sstevel@tonic-gate 			continue;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		/* Report error to be consistent with libc */
1767c478bd9Sstevel@tonic-gate 		if ((buf_len + 1) > LINE_MAX)
1777c478bd9Sstevel@tonic-gate 			error(TOOLONG);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 		lineno++;
1807c478bd9Sstevel@tonic-gate 		if (buf[0] == '\n')    /* blank lines are ignored */
1817c478bd9Sstevel@tonic-gate 		{
1827c478bd9Sstevel@tonic-gate 			code = 1;		/* exit with error code = 1 */
1837c478bd9Sstevel@tonic-gate 			eflag = 0;	/* force print of "blank" line */
1847c478bd9Sstevel@tonic-gate 			fprintf(stderr, "\n%s %d\n", gettext(BLANKLINE),
185*869657d0SToomas Soome 			    lineno);
1867c478bd9Sstevel@tonic-gate 			continue;
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		if (buf[buf_len - 1] == '\n') {
1907c478bd9Sstevel@tonic-gate 			if ((tmpbuf = strdup(buf)) == NULL) {
1917c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(NOMEM));
1927c478bd9Sstevel@tonic-gate 				exit(1);
1937c478bd9Sstevel@tonic-gate 			}
1947c478bd9Sstevel@tonic-gate 			tmpbuf[buf_len - 1] = ',';
1957c478bd9Sstevel@tonic-gate 		} else {
1967c478bd9Sstevel@tonic-gate 			if ((tmpbuf = malloc(buf_len + 2)) == NULL) {
1977c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(NOMEM));
1987c478bd9Sstevel@tonic-gate 				exit(1);
1997c478bd9Sstevel@tonic-gate 			}
2007c478bd9Sstevel@tonic-gate 			(void) strcpy(tmpbuf, buf);
2017c478bd9Sstevel@tonic-gate 			tmpbuf[buf_len++] = ',';
2027c478bd9Sstevel@tonic-gate 			tmpbuf[buf_len] = '\0';
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		colons = 0;
2067c478bd9Sstevel@tonic-gate 		eflag = 0;
2077c478bd9Sstevel@tonic-gate 		badchar = 0;
2087c478bd9Sstevel@tonic-gate 		baddigit = 0;
2097c478bd9Sstevel@tonic-gate 		badlognam = 0;
210f48205beScasper 		gid = 0;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		ngroups++;	/* Increment number of groups found */
2137c478bd9Sstevel@tonic-gate 		/* Check that entry is not a nameservice redirection */
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		if (buf[0] == '+' || buf[0] == '-')  {
2167c478bd9Sstevel@tonic-gate 			/*
2177c478bd9Sstevel@tonic-gate 			 * Should set flag here to allow special case checking
2187c478bd9Sstevel@tonic-gate 			 * in the rest of the code,
2197c478bd9Sstevel@tonic-gate 			 * but for now, we'll just ignore this entry.
2207c478bd9Sstevel@tonic-gate 			 */
2217c478bd9Sstevel@tonic-gate 			free(tmpbuf);
2227c478bd9Sstevel@tonic-gate 			continue;
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		/*	Check number of fields	*/
2267c478bd9Sstevel@tonic-gate 
227*869657d0SToomas Soome 		for (i = 0; buf[i] != '\0'; i++) {
2281316014bSbasabi 			if (buf[i] == ':') {
2297c478bd9Sstevel@tonic-gate 				delim[colons] = i;
2307c478bd9Sstevel@tonic-gate 				if (++colons > NUM_COLONS)
2317c478bd9Sstevel@tonic-gate 					break;
2327c478bd9Sstevel@tonic-gate 			}
2337c478bd9Sstevel@tonic-gate 		}
2341316014bSbasabi 		if (colons != NUM_COLONS) {
2357c478bd9Sstevel@tonic-gate 			error(BADLINE);
2367c478bd9Sstevel@tonic-gate 			free(tmpbuf);
2377c478bd9Sstevel@tonic-gate 			continue;
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		/* check to see that group name is at least 1 character	*/
2417c478bd9Sstevel@tonic-gate 		/* and that all characters are lowrcase or digits.	*/
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		if (buf[0] == ':')
2447c478bd9Sstevel@tonic-gate 			error(NONAME);
2451316014bSbasabi 		else {
2461316014bSbasabi 			for (i = 0; buf[i] != ':'; i++) {
2477c478bd9Sstevel@tonic-gate 				if (i >= LOGNAME_MAX)
2487c478bd9Sstevel@tonic-gate 					longnam++;
2497c478bd9Sstevel@tonic-gate 				if (!(islower(buf[i]) || isdigit(buf[i])))
2507c478bd9Sstevel@tonic-gate 					badchar++;
2517c478bd9Sstevel@tonic-gate 			}
2527c478bd9Sstevel@tonic-gate 			if (longnam > 0)
2537c478bd9Sstevel@tonic-gate 				error(LONGNAME);
2547c478bd9Sstevel@tonic-gate 			if (badchar > 0)
2557c478bd9Sstevel@tonic-gate 				error(BADNAME);
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		/*	check that GID is numeric and <= 31 bits	*/
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		len = (delim[2] - delim[1]) - 1;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		if (len > 10 || len < 1)
2637c478bd9Sstevel@tonic-gate 			error(BADGID);
2647c478bd9Sstevel@tonic-gate 		else {
2651316014bSbasabi 			for (i = (delim[1]+1); i < delim[2]; i++) {
2667c478bd9Sstevel@tonic-gate 				if (! (isdigit(buf[i])))
2677c478bd9Sstevel@tonic-gate 					baddigit++;
2687c478bd9Sstevel@tonic-gate 				else if (baddigit == 0)
2697c478bd9Sstevel@tonic-gate 					gid = gid * 10 + (gid_t)(buf[i] - '0');
2707c478bd9Sstevel@tonic-gate 				/* converts ascii GID to decimal */
2717c478bd9Sstevel@tonic-gate 			}
2727c478bd9Sstevel@tonic-gate 			if (baddigit > 0)
2737c478bd9Sstevel@tonic-gate 				error(BADGID);
274f48205beScasper 			else if (gid > (gid_t)MAXUID)
2757c478bd9Sstevel@tonic-gate 				error(BADGID);
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		/*  check that logname appears in the passwd file  */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		nptr = &tmpbuf[delim[2]];
2817c478bd9Sstevel@tonic-gate 		nptr++;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		listlen = strlen(nptr) - 1;
2847c478bd9Sstevel@tonic-gate 
2851316014bSbasabi 		while ((cptr = strchr(nptr, ',')) != NULL) {
286*869657d0SToomas Soome 			*cptr = '\0';
287*869657d0SToomas Soome 			if (*nptr == '\0') {
2887c478bd9Sstevel@tonic-gate 				if (listlen)
2897c478bd9Sstevel@tonic-gate 					error(NULLNAME);
2907c478bd9Sstevel@tonic-gate 				nptr++;
2917c478bd9Sstevel@tonic-gate 				continue;
2927c478bd9Sstevel@tonic-gate 			}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 			for (t = root; t != NULL; t = t->next) {
2957c478bd9Sstevel@tonic-gate 				if (strcmp(t->user, nptr) == 0)
2967c478bd9Sstevel@tonic-gate 					break;
2977c478bd9Sstevel@tonic-gate 			}
2987c478bd9Sstevel@tonic-gate 			if (t == NULL) {
2997c478bd9Sstevel@tonic-gate #ifndef ORIG_SVR4
3007c478bd9Sstevel@tonic-gate 				/*
3017c478bd9Sstevel@tonic-gate 				 * User entry not found, so check if in
3027c478bd9Sstevel@tonic-gate 				 *  password file
3037c478bd9Sstevel@tonic-gate 				 */
3047c478bd9Sstevel@tonic-gate 				struct passwd *pwp;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 				if ((pwp = getpwnam(nptr)) == NULL) {
3077c478bd9Sstevel@tonic-gate #endif
3087c478bd9Sstevel@tonic-gate 					badlognam++;
3097c478bd9Sstevel@tonic-gate 					error(NOTFOUND);
3107c478bd9Sstevel@tonic-gate 					goto getnext;
3117c478bd9Sstevel@tonic-gate #ifndef ORIG_SVR4
3127c478bd9Sstevel@tonic-gate 				}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 				/* Usrname found, so add entry to user-list */
3157c478bd9Sstevel@tonic-gate 				t = (struct node *)
316*869657d0SToomas Soome 				    emalloc(sizeof (*t) + strlen(nptr));
3177c478bd9Sstevel@tonic-gate 				t->next = root;
3187c478bd9Sstevel@tonic-gate 				root = t;
3197c478bd9Sstevel@tonic-gate 				strcpy(t->user, nptr);
3207c478bd9Sstevel@tonic-gate 				t->ngroups = 1;
3217c478bd9Sstevel@tonic-gate 				if (!ngroups_max)
3227c478bd9Sstevel@tonic-gate 					t->groups = NULL;
3237c478bd9Sstevel@tonic-gate 				else {
3247c478bd9Sstevel@tonic-gate 					t->groups = (struct group *)
325*869657d0SToomas Soome 					    emalloc(sizeof (struct group));
3267c478bd9Sstevel@tonic-gate 					t->groups->grp = pwp->pw_gid;
3277c478bd9Sstevel@tonic-gate 					t->groups->cnt = 1;
3287c478bd9Sstevel@tonic-gate 					t->groups->nxt = NULL;
3297c478bd9Sstevel@tonic-gate 				}
3307c478bd9Sstevel@tonic-gate 			}
3317c478bd9Sstevel@tonic-gate #endif
3327c478bd9Sstevel@tonic-gate 			if (!ngroups_max)
3337c478bd9Sstevel@tonic-gate 				goto getnext;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 			t->ngroups++;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 			/*
3387c478bd9Sstevel@tonic-gate 			 * check for duplicate logname in group
3397c478bd9Sstevel@tonic-gate 			 */
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 			for (gp = t->groups; gp != NULL; gp = gp->nxt) {
3427c478bd9Sstevel@tonic-gate 				if (gid == gp->grp) {
3437c478bd9Sstevel@tonic-gate 					if (gp->cnt++ == 1) {
3447c478bd9Sstevel@tonic-gate 						badlognam++;
3457c478bd9Sstevel@tonic-gate 						if (gp->nxt == NULL)
3467c478bd9Sstevel@tonic-gate 							error(DUPNAME2);
3477c478bd9Sstevel@tonic-gate 						else
3487c478bd9Sstevel@tonic-gate 							error(DUPNAME);
3497c478bd9Sstevel@tonic-gate 					}
3507c478bd9Sstevel@tonic-gate 					goto getnext;
3517c478bd9Sstevel@tonic-gate 				}
3527c478bd9Sstevel@tonic-gate 			}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 			gp = (struct group *)emalloc(sizeof (struct group));
3557c478bd9Sstevel@tonic-gate 			gp->grp = gid;
3567c478bd9Sstevel@tonic-gate 			gp->cnt = 1;
3577c478bd9Sstevel@tonic-gate 			gp->nxt = t->groups;
3587c478bd9Sstevel@tonic-gate 			t->groups = gp;
3597c478bd9Sstevel@tonic-gate getnext:
3607c478bd9Sstevel@tonic-gate 			nptr = ++cptr;
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 		free(tmpbuf);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if (ngroups == 0) {
3667c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Group file '%s' is empty\n"), argv[1]);
3677c478bd9Sstevel@tonic-gate 		code = 1;
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (ngroups_max) {
3717c478bd9Sstevel@tonic-gate 		for (t = root; t != NULL; t = t->next) {
3727c478bd9Sstevel@tonic-gate 			if (t->ngroups > ngroups_max) {
3737c478bd9Sstevel@tonic-gate 				fprintf(stderr, "\n\n%s%s (%d)\n",
374*869657d0SToomas Soome 				    NGROUPS, t->user, t->ngroups);
3757c478bd9Sstevel@tonic-gate 				code = 1;
3767c478bd9Sstevel@tonic-gate 			}
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 	}
37949335bdeSbasabi 	return (code);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate /*	Error printing routine	*/
3837c478bd9Sstevel@tonic-gate 
38449335bdeSbasabi void
error(char * msg)38549335bdeSbasabi error(char *msg)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	code = 1;
3881316014bSbasabi 	if (eflag == 0) {
3897c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\n\n%s", buf);
3907c478bd9Sstevel@tonic-gate 		eflag = 1;
3917c478bd9Sstevel@tonic-gate 	}
3921316014bSbasabi 	if (longnam != 0) {
3937c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%s\n", gettext(msg));
3947c478bd9Sstevel@tonic-gate 		longnam = 0;
3957c478bd9Sstevel@tonic-gate 		return;
3967c478bd9Sstevel@tonic-gate 	}
3971316014bSbasabi 	if (badchar != 0) {
3987c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%d %s\n", badchar, gettext(msg));
3997c478bd9Sstevel@tonic-gate 		badchar = 0;
4007c478bd9Sstevel@tonic-gate 		return;
4011316014bSbasabi 	} else if (baddigit != 0) {
4027c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%s\n", gettext(msg));
4037c478bd9Sstevel@tonic-gate 		baddigit = 0;
4047c478bd9Sstevel@tonic-gate 		return;
4051316014bSbasabi 	} else if (badlognam != 0) {
4067c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%s - %s\n", nptr, gettext(msg));
4077c478bd9Sstevel@tonic-gate 		badlognam = 0;
4087c478bd9Sstevel@tonic-gate 		return;
4091316014bSbasabi 	} else {
4107c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\t%s\n", gettext(msg));
4117c478bd9Sstevel@tonic-gate 		return;
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate }
414