xref: /onnv-gate/usr/src/cmd/ypcmd/mknetid/mknetid.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM  * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*13093SRoger.Faulkner@Oracle.COM 
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Network name to unix credential database generator.
280Sstevel@tonic-gate  * Uses /etc/passwd, /etc/group, /etc/hosts and /etc/netid to
290Sstevel@tonic-gate  * create the database.
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * If some user appears in passwd, they get an entry like:
320Sstevel@tonic-gate  *	sun.<uid>@<domainname>	<uid>:<gid1>,<gid2>,...
330Sstevel@tonic-gate  * If some host appears in hosts, it gets an entry like:
340Sstevel@tonic-gate  *	sun.<hostname>@<domainname>	0:<hostname>
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * The file /etc/netid is used to add other domains (possibly non-unix)
370Sstevel@tonic-gate  * to the database.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <pwd.h>
410Sstevel@tonic-gate #include <limits.h>
420Sstevel@tonic-gate #include <sys/param.h>
430Sstevel@tonic-gate #include <rpc/rpc.h>
440Sstevel@tonic-gate #include <rpc/key_prot.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define	MAXNAMELEN	256
480Sstevel@tonic-gate #define	MAXLINELEN	1024
490Sstevel@tonic-gate #define	MAXDOMAINLEN	32
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #define	GRPTABSIZE	256		/* size of group table */
520Sstevel@tonic-gate #define	PRNTABSIZE	4096		/* size of printed item table */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define	NUMGIDS	(NGROUPS_MAX + 1)	/* group-access-list + gid */
550Sstevel@tonic-gate 
56*13093SRoger.Faulkner@Oracle.COM extern char **getaline();
570Sstevel@tonic-gate extern char *malloc();
580Sstevel@tonic-gate extern char *strcpy();
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * The group list
620Sstevel@tonic-gate  * Store username and groups to which he/she belongs
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate struct group_list {
650Sstevel@tonic-gate 	char *user;
660Sstevel@tonic-gate 	int group_len;
670Sstevel@tonic-gate 	int groups[NUMGIDS];
680Sstevel@tonic-gate 	struct group_list *next;
690Sstevel@tonic-gate };
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * General purpose list of strings
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate struct string_list {
750Sstevel@tonic-gate 	char *str;
760Sstevel@tonic-gate 	struct string_list *next;
770Sstevel@tonic-gate };
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static FILE *openfile();
800Sstevel@tonic-gate static char *scanargs();
810Sstevel@tonic-gate static int atoi();
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static char *cmdname;	/* name of this program */
840Sstevel@tonic-gate static int quietmode;	/* quiet mode: don't print error messages */
850Sstevel@tonic-gate static char *curfile;	/* name of file we are parsing */
860Sstevel@tonic-gate static int curline;		/* current line parsed in this file */
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static struct group_list *groups[GRPTABSIZE];	/* group table */
890Sstevel@tonic-gate static struct string_list *printed[PRNTABSIZE];	/* printed item table */
900Sstevel@tonic-gate static char domain[MAXDOMAINLEN];	/* name of our domain */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static char PASSWD[] = "/etc/passwd";	/* default passwd database */
930Sstevel@tonic-gate static char IDMAP[] = "/etc/idmap";	/* default net-id map database */
940Sstevel@tonic-gate static char GROUP[] = "/etc/group";	/* default group database */
950Sstevel@tonic-gate static char HOSTS[] = "/etc/hosts";	/* default hosts database */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static char *pwdfile = PASSWD;	/* password file to parse */
980Sstevel@tonic-gate static char *grpfile = GROUP;	/* group file */
990Sstevel@tonic-gate static char *hostfile = HOSTS;	/* hosts file */
1000Sstevel@tonic-gate static char *mapfile = IDMAP;	/* network id file */
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * Various separaters
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate static char WHITE[] = "\t ";
1060Sstevel@tonic-gate static char COLON[] = ":";
1070Sstevel@tonic-gate static char COMMA[] = ",";
1080Sstevel@tonic-gate 
109702Sth160488 void domapfile(char *, FILE *);
110702Sth160488 void dogrpfile(char *, FILE *);
111702Sth160488 void dopwdfile(char *, FILE *);
112702Sth160488 void dohostfile(char *, FILE *);
113702Sth160488 static int Atoi(char *);
114702Sth160488 void check_getname(char **, char *, char *, char *, char *);
115702Sth160488 void multdef(char *);
116702Sth160488 static int wasprinted(char *);
117702Sth160488 void storegid(int, char *);
118702Sth160488 void printgroups(char *, int);
119702Sth160488 int parseargs(int, char *[]);
120702Sth160488 void put_s(char *);
121702Sth160488 void put_d(int);
122702Sth160488 
123702Sth160488 
124702Sth160488 int
main(argc,argv)1250Sstevel@tonic-gate main(argc, argv)
1260Sstevel@tonic-gate 	int argc;
1270Sstevel@tonic-gate 	char *argv[];
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	FILE *pf, *mf, *gf, *hf;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	cmdname = argv[0];
1320Sstevel@tonic-gate 	if (!parseargs(argc, argv)) {
1330Sstevel@tonic-gate 		(void) fprintf(stderr,
1340Sstevel@tonic-gate 			"usage: %s [-q] [-pghm filename]\n", cmdname);
1350Sstevel@tonic-gate 		exit(1);
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 	(void) getdomainname(domain, sizeof (domain));
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	pf = openfile(pwdfile);
1400Sstevel@tonic-gate 	gf = openfile(grpfile);
1410Sstevel@tonic-gate 	hf = openfile(hostfile);
1420Sstevel@tonic-gate 	mf = fopen(mapfile, "r");
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if (mf != NULL) {
1460Sstevel@tonic-gate 		domapfile(mapfile, mf);
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 	dogrpfile(grpfile, gf);
1490Sstevel@tonic-gate 	dopwdfile(pwdfile, pf);
1500Sstevel@tonic-gate 	dohostfile(hostfile, hf);
1510Sstevel@tonic-gate 
152702Sth160488 	return (0);
1530Sstevel@tonic-gate 	/* NOTREACHED */
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate  * Parse the network id mapping file
1580Sstevel@tonic-gate  */
159702Sth160488 void
domapfile(mapfile,mf)1600Sstevel@tonic-gate domapfile(mapfile, mf)
1610Sstevel@tonic-gate 	char *mapfile;
1620Sstevel@tonic-gate 	FILE *mf;
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	char **lp;
1650Sstevel@tonic-gate 	char line[MAXLINELEN];
1660Sstevel@tonic-gate 	char name[MAXNAMELEN];
1670Sstevel@tonic-gate 	int uid, gid;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	curfile = mapfile;
1700Sstevel@tonic-gate 	curline = 0;
171*13093SRoger.Faulkner@Oracle.COM 	while (lp = getaline(line, sizeof (line), mf, &curline, "#")) {
1720Sstevel@tonic-gate 		check_getname(lp, name, WHITE, WHITE, "#");
1730Sstevel@tonic-gate 		if (wasprinted(name)) {
1740Sstevel@tonic-gate 			multdef(name);
1750Sstevel@tonic-gate 			continue;
1760Sstevel@tonic-gate 		}
1770Sstevel@tonic-gate 		put_s(name);
1780Sstevel@tonic-gate 		(void) putchar(' ');
1790Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, "#");
1800Sstevel@tonic-gate 		uid = Atoi(name);
1810Sstevel@tonic-gate 		put_d(uid);
1820Sstevel@tonic-gate 		(void) putchar(':');
1830Sstevel@tonic-gate 		if (uid == 0) {
1840Sstevel@tonic-gate 			check_getname(lp, name, WHITE, "\t\n ", "#");
1850Sstevel@tonic-gate 			put_s(name);
1860Sstevel@tonic-gate 			(void) putchar(' ');
1870Sstevel@tonic-gate 		} else {
1880Sstevel@tonic-gate 			check_getname(lp, name, WHITE, ",\n", "#");
1890Sstevel@tonic-gate 			gid = Atoi(name);
1900Sstevel@tonic-gate 			put_d(gid);
1910Sstevel@tonic-gate 			while (getname(name, sizeof (name), WHITE, ",\n", lp,
1920Sstevel@tonic-gate 					"#") >= 0) {
1930Sstevel@tonic-gate 				gid = Atoi(name);
1940Sstevel@tonic-gate 				(void) putchar(',');
1950Sstevel@tonic-gate 				put_d(gid);
1960Sstevel@tonic-gate 			}
1970Sstevel@tonic-gate 		}
1980Sstevel@tonic-gate 		(void) putchar('\n');
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate  * Parse the groups file
2050Sstevel@tonic-gate  */
206702Sth160488 void
dogrpfile(grpfile,gf)2070Sstevel@tonic-gate dogrpfile(grpfile, gf)
2080Sstevel@tonic-gate 	char *grpfile;
2090Sstevel@tonic-gate 	FILE *gf;
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	char **lp;
2120Sstevel@tonic-gate 	char line[MAXLINELEN];
2130Sstevel@tonic-gate 	char name[MAXNAMELEN];
2140Sstevel@tonic-gate 	int gid;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	curfile = grpfile;
2170Sstevel@tonic-gate 	curline = 0;
218*13093SRoger.Faulkner@Oracle.COM 	while (lp = getaline(line, sizeof (line), gf, &curline, "")) {
2190Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, "");
2200Sstevel@tonic-gate 		if (name[0] == '+') {
2210Sstevel@tonic-gate 			continue;
2220Sstevel@tonic-gate 		}
2230Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, ""); /* ignore passwd */
2240Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, "");
2250Sstevel@tonic-gate 		gid = Atoi(name);
2260Sstevel@tonic-gate 		while (getname(name, sizeof (name), WHITE, COMMA, lp,
2270Sstevel@tonic-gate 				"") >= 0) {
2280Sstevel@tonic-gate 			storegid(gid, name);
2290Sstevel@tonic-gate 		}
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate  * Parse the password file
2360Sstevel@tonic-gate  */
237702Sth160488 void
dopwdfile(pwdfile,pf)2380Sstevel@tonic-gate dopwdfile(pwdfile, pf)
2390Sstevel@tonic-gate 	char *pwdfile;
2400Sstevel@tonic-gate 	FILE *pf;
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	char **lp;
2430Sstevel@tonic-gate 	char line[MAXLINELEN];
2440Sstevel@tonic-gate 	char name[MAXNAMELEN];
2450Sstevel@tonic-gate 	char user[MAXNAMELEN];
2460Sstevel@tonic-gate 	int uid, gid;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	curfile = pwdfile;
2490Sstevel@tonic-gate 	curline = 0;
2500Sstevel@tonic-gate 
251*13093SRoger.Faulkner@Oracle.COM 	while (lp = getaline(line, sizeof (line), pf, &curline, "")) {
2520Sstevel@tonic-gate 		check_getname(lp, user, WHITE, COLON, ""); 	/* username */
2530Sstevel@tonic-gate 		if (user[0] == '-' || user[0] == '+') {
2540Sstevel@tonic-gate 			continue;	/* NIS entry */
2550Sstevel@tonic-gate 		}
2560Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, ""); /* ignore passwd */
2570Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, ""); /* but get uid */
2580Sstevel@tonic-gate 		uid = Atoi(name);
2590Sstevel@tonic-gate 		user2netname(name, uid, domain);
2600Sstevel@tonic-gate 		if (wasprinted(name)) {
2610Sstevel@tonic-gate 			multdef(name);
2620Sstevel@tonic-gate 			continue;
2630Sstevel@tonic-gate 		}
2640Sstevel@tonic-gate 		put_s(name);
2650Sstevel@tonic-gate 		(void) putchar(' ');
2660Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, "");
2670Sstevel@tonic-gate 		gid = Atoi(name);
2680Sstevel@tonic-gate 		put_d(uid);
2690Sstevel@tonic-gate 		(void) putchar(':');
2700Sstevel@tonic-gate 		printgroups(user, gid);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate  * Parse the hosts file
2770Sstevel@tonic-gate  */
278702Sth160488 void
dohostfile(hostfile,hf)2790Sstevel@tonic-gate dohostfile(hostfile, hf)
2800Sstevel@tonic-gate 	char *hostfile;
2810Sstevel@tonic-gate 	FILE *hf;
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate 	char **lp;
2840Sstevel@tonic-gate 	char line[MAXLINELEN];
2850Sstevel@tonic-gate 	char name[MAXNAMELEN];
2860Sstevel@tonic-gate 	char netname[MAXNETNAMELEN];
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	curfile = hostfile;
2890Sstevel@tonic-gate 	curline = 0;
290*13093SRoger.Faulkner@Oracle.COM 	while (lp = getaline(line, sizeof (line), hf, &curline, "#")) {
2910Sstevel@tonic-gate 		check_getname(lp, name, WHITE, WHITE, "#");
2920Sstevel@tonic-gate 		if (getname(name, MAXNAMELEN, WHITE, WHITE, lp, "#") != 1) {
2930Sstevel@tonic-gate 			continue;
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 		host2netname(netname, name, domain);
2960Sstevel@tonic-gate 		if (wasprinted(netname)) {
2970Sstevel@tonic-gate 			multdef(netname);
2980Sstevel@tonic-gate 			continue;
2990Sstevel@tonic-gate 		}
3000Sstevel@tonic-gate 		(void) printf("%s 0:%.*s\n", netname, sizeof (name), name);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate /*
3050Sstevel@tonic-gate  * Open a file, exit on failure
3060Sstevel@tonic-gate  */
3070Sstevel@tonic-gate static FILE *
openfile(fname)3080Sstevel@tonic-gate openfile(fname)
3090Sstevel@tonic-gate 	char *fname;
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	FILE *f;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	f = fopen(fname, "r");
3140Sstevel@tonic-gate 	if (f == NULL) {
3150Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: can't open %s\n", cmdname, fname);
3160Sstevel@tonic-gate 		exit(1);
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate 	return (f);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate /*
3220Sstevel@tonic-gate  * Print syntax error message, then exit
3230Sstevel@tonic-gate  */
324702Sth160488 void
syntaxerror()3250Sstevel@tonic-gate syntaxerror()
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: syntax error in file \"%s\", line %d\n",
3280Sstevel@tonic-gate 	    cmdname, curfile, curline);
3290Sstevel@tonic-gate 	exit(1);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate  * an atoi() that prints a message and exits upong failure
3350Sstevel@tonic-gate  */
3360Sstevel@tonic-gate static int
Atoi(str)3370Sstevel@tonic-gate Atoi(str)
3380Sstevel@tonic-gate 	char *str;
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	int res;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	if (!sscanf(str, "%d", &res)) {
3430Sstevel@tonic-gate 		syntaxerror();
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 	return (res);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate  * Attempt to get a token from a file, print a message and exit upon failure
3510Sstevel@tonic-gate  */
352702Sth160488 void
check_getname(lp,name,skip,term,com)3530Sstevel@tonic-gate check_getname(lp, name, skip, term, com)
3540Sstevel@tonic-gate 	char **lp;
3550Sstevel@tonic-gate 	char *name;
3560Sstevel@tonic-gate 	char *skip;
3570Sstevel@tonic-gate 	char *term;
3580Sstevel@tonic-gate 	char *com;
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	if (getname(name, MAXNAMELEN, skip, term, lp, com) != 1) {
3610Sstevel@tonic-gate 		syntaxerror();
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate  * Something was defined more than once
3670Sstevel@tonic-gate  */
368702Sth160488 void
multdef(name)3690Sstevel@tonic-gate multdef(name)
3700Sstevel@tonic-gate 	char *name;
3710Sstevel@tonic-gate {
3720Sstevel@tonic-gate 	if (!quietmode) {
3730Sstevel@tonic-gate 		(void) fprintf(stderr,
3740Sstevel@tonic-gate 			"%s: %s multiply defined, other definitions ignored\n",
3750Sstevel@tonic-gate 			cmdname, name);
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
379702Sth160488 static int
hash(str,size)3800Sstevel@tonic-gate hash(str, size)
3810Sstevel@tonic-gate 	unsigned char *str;
3820Sstevel@tonic-gate 	int size;
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	unsigned val;
3850Sstevel@tonic-gate 	int flip;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	val = 0;
3880Sstevel@tonic-gate 	flip = 0;
3890Sstevel@tonic-gate 	while (*str) {
3900Sstevel@tonic-gate 		if (flip) {
3910Sstevel@tonic-gate 			val ^= (*str++ << 6);
3920Sstevel@tonic-gate 		} else {
3930Sstevel@tonic-gate 			val ^= *str++;
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 		flip = !flip;
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 	return (val % size);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate /*
4020Sstevel@tonic-gate  * Check if an item has been printed
4030Sstevel@tonic-gate  * If not, store the item into the printed item table
4040Sstevel@tonic-gate  */
405702Sth160488 static int
wasprinted(name)4060Sstevel@tonic-gate wasprinted(name)
4070Sstevel@tonic-gate 	char *name;
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	struct string_list *s;
4100Sstevel@tonic-gate 	int val;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	val = hash((unsigned char *) name, PRNTABSIZE);
4130Sstevel@tonic-gate 	for (s = printed[val]; s != NULL && strcmp(s->str, name); s = s->next)
4140Sstevel@tonic-gate 		;
4150Sstevel@tonic-gate 	if (s != NULL) {
4160Sstevel@tonic-gate 		return (1);
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate 	s = (struct string_list *)malloc(sizeof (struct string_list));
4190Sstevel@tonic-gate 	s->str = malloc((unsigned)strlen(name) + 1);
4200Sstevel@tonic-gate 	(void) strcpy(s->str, name);
4210Sstevel@tonic-gate 	s->next = printed[val];
4220Sstevel@tonic-gate 	printed[val] = s;
4230Sstevel@tonic-gate 	return (0);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate /*
4270Sstevel@tonic-gate  * Add gid to the list of a user's groups
4280Sstevel@tonic-gate  */
429702Sth160488 void
storegid(gid,user)4300Sstevel@tonic-gate storegid(gid, user)
4310Sstevel@tonic-gate 	int gid;
4320Sstevel@tonic-gate 	char *user;
4330Sstevel@tonic-gate {
4340Sstevel@tonic-gate 	struct group_list *g;
4350Sstevel@tonic-gate 	int i;
4360Sstevel@tonic-gate 	int val;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	val = hash((unsigned char *) user, GRPTABSIZE);
4390Sstevel@tonic-gate 	for (g = groups[val]; g != NULL && strcmp(g->user, user); g = g->next)
4400Sstevel@tonic-gate 		;
4410Sstevel@tonic-gate 	if (g == NULL) {
4420Sstevel@tonic-gate 		g = (struct group_list *)malloc(sizeof (struct group_list));
4430Sstevel@tonic-gate 		g->user = malloc((unsigned)strlen(user) + 1);
4440Sstevel@tonic-gate 		(void) strcpy(g->user, user);
4450Sstevel@tonic-gate 		g->group_len = 1;
4460Sstevel@tonic-gate 		g->groups[0] = gid;
4470Sstevel@tonic-gate 		g->next = groups[val];
4480Sstevel@tonic-gate 		groups[val] = g;
4490Sstevel@tonic-gate 	} else {
4500Sstevel@tonic-gate 		for (i = 0; i < g->group_len; i++) {
4510Sstevel@tonic-gate 			if (g->groups[i] == gid) {
4520Sstevel@tonic-gate 				return;
4530Sstevel@tonic-gate 			}
4540Sstevel@tonic-gate 		}
4550Sstevel@tonic-gate 		if (g->group_len >= NUMGIDS) {
4560Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s's groups exceed %d\n",
4570Sstevel@tonic-gate 				cmdname, user, NGROUPS_MAX);
4580Sstevel@tonic-gate 			return;
4590Sstevel@tonic-gate 		}
4600Sstevel@tonic-gate 		g->groups[g->group_len++] = gid;
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate /*
4650Sstevel@tonic-gate  * print out a user's groups
4660Sstevel@tonic-gate  */
467702Sth160488 void
printgroups(user,gid)4680Sstevel@tonic-gate printgroups(user, gid)
4690Sstevel@tonic-gate 	char *user;
4700Sstevel@tonic-gate 	int gid;
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	struct group_list *g;
4730Sstevel@tonic-gate 	int i;
4740Sstevel@tonic-gate 	int val;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	val = hash((unsigned char *) user, GRPTABSIZE);
4770Sstevel@tonic-gate 	for (g = groups[val]; g != NULL && strcmp(g->user, user); g = g->next)
4780Sstevel@tonic-gate 		;
4790Sstevel@tonic-gate 	put_d(gid);
4800Sstevel@tonic-gate 	if (g != NULL) {
4810Sstevel@tonic-gate 		for (i = 0; i < g->group_len; i++) {
4820Sstevel@tonic-gate 			if (gid != g->groups[i]) {
4830Sstevel@tonic-gate 				(void) putchar(',');
4840Sstevel@tonic-gate 				put_d(g->groups[i]);
4850Sstevel@tonic-gate 			}
4860Sstevel@tonic-gate 		}
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 	(void) putchar('\n');
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate /*
4930Sstevel@tonic-gate  * Parse command line arguments
4940Sstevel@tonic-gate  */
495702Sth160488 int
parseargs(argc,argv)4960Sstevel@tonic-gate parseargs(argc, argv)
4970Sstevel@tonic-gate 	int argc;
4980Sstevel@tonic-gate 	char *argv[];
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate 	int i;
5010Sstevel@tonic-gate 	int j;
5020Sstevel@tonic-gate 	static struct {
5030Sstevel@tonic-gate 		char letter;
5040Sstevel@tonic-gate 		char *standard;
5050Sstevel@tonic-gate 		char **filename;
5060Sstevel@tonic-gate 	} whattodo[] = {
5070Sstevel@tonic-gate 		{ 'p', PASSWD, &pwdfile },
5080Sstevel@tonic-gate 		{ 'g', GROUP, &grpfile },
5090Sstevel@tonic-gate 		{ 'm', IDMAP, &mapfile },
5100Sstevel@tonic-gate 		{ 'h', HOSTS, &hostfile }
5110Sstevel@tonic-gate 	};
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate #define	TABSIZE  sizeof (whattodo)/sizeof (whattodo[0])
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
5160Sstevel@tonic-gate 		if (argv[i][0] == '-') {
5170Sstevel@tonic-gate 			if (argv[i][2] != 0) {
5180Sstevel@tonic-gate 				return (0);
5190Sstevel@tonic-gate 			}
5200Sstevel@tonic-gate 			if (argv[i][1] == 'q') {
5210Sstevel@tonic-gate 				quietmode = 1;
5220Sstevel@tonic-gate 				continue;
5230Sstevel@tonic-gate 			}
5240Sstevel@tonic-gate 			for (j = 0; j < TABSIZE; j++) {
5250Sstevel@tonic-gate 				if (whattodo[j].letter == argv[i][1]) {
5260Sstevel@tonic-gate 					if (*whattodo[j].filename !=
5270Sstevel@tonic-gate 							whattodo[j].standard) {
5280Sstevel@tonic-gate 						return (0);
5290Sstevel@tonic-gate 					}
5300Sstevel@tonic-gate 					if (++i == argc) {
5310Sstevel@tonic-gate 						return (0);
5320Sstevel@tonic-gate 					}
5330Sstevel@tonic-gate 					*whattodo[j].filename = argv[i];
5340Sstevel@tonic-gate 					break;
5350Sstevel@tonic-gate 				}
5360Sstevel@tonic-gate 			}
5370Sstevel@tonic-gate 			if (j == TABSIZE) {
5380Sstevel@tonic-gate 				return (0);
5390Sstevel@tonic-gate 			}
5400Sstevel@tonic-gate 		}
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 	return (1);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate  * Print a string, quickly
5470Sstevel@tonic-gate  */
548702Sth160488 void
put_s(s)5490Sstevel@tonic-gate put_s(s)
5500Sstevel@tonic-gate 	char *s;
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate 	(void) fwrite(s, strlen(s), 1, stdout);
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate /*
5560Sstevel@tonic-gate  * Print an integer, quickly
5570Sstevel@tonic-gate  */
558702Sth160488 void
put_d(d)5590Sstevel@tonic-gate put_d(d)
5600Sstevel@tonic-gate 	int d;
5610Sstevel@tonic-gate {
5620Sstevel@tonic-gate 	char buf[20];
5630Sstevel@tonic-gate 	char *p;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	if (d == 0) {
5660Sstevel@tonic-gate 		(void) putchar('0');
5670Sstevel@tonic-gate 		return;
5680Sstevel@tonic-gate 	}
5690Sstevel@tonic-gate 	if (d < 0) {
5700Sstevel@tonic-gate 		(void) putchar('-');
5710Sstevel@tonic-gate 		d = -d;
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 	p = buf + sizeof (buf);
5740Sstevel@tonic-gate 	*--p = 0;
5750Sstevel@tonic-gate 	while (d > 0) {
5760Sstevel@tonic-gate 		*--p = (d % 10) + '0';
5770Sstevel@tonic-gate 		d /= 10;
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 	put_s(p);
5800Sstevel@tonic-gate }
581