xref: /illumos-gate/usr/src/cmd/ypcmd/mknetid/mknetid.c (revision a506a34c)
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*a506a34cSth  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*a506a34cSth  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate 
28*a506a34cSth #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SMI4.1 1.5 */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Network name to unix credential database generator.
327c478bd9Sstevel@tonic-gate  * Uses /etc/passwd, /etc/group, /etc/hosts and /etc/netid to
337c478bd9Sstevel@tonic-gate  * create the database.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * If some user appears in passwd, they get an entry like:
367c478bd9Sstevel@tonic-gate  *	sun.<uid>@<domainname>	<uid>:<gid1>,<gid2>,...
377c478bd9Sstevel@tonic-gate  * If some host appears in hosts, it gets an entry like:
387c478bd9Sstevel@tonic-gate  *	sun.<hostname>@<domainname>	0:<hostname>
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * The file /etc/netid is used to add other domains (possibly non-unix)
417c478bd9Sstevel@tonic-gate  * to the database.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <pwd.h>
457c478bd9Sstevel@tonic-gate #include <limits.h>
467c478bd9Sstevel@tonic-gate #include <sys/param.h>
477c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
487c478bd9Sstevel@tonic-gate #include <rpc/key_prot.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	MAXNAMELEN	256
527c478bd9Sstevel@tonic-gate #define	MAXLINELEN	1024
537c478bd9Sstevel@tonic-gate #define	MAXDOMAINLEN	32
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	GRPTABSIZE	256		/* size of group table */
567c478bd9Sstevel@tonic-gate #define	PRNTABSIZE	4096		/* size of printed item table */
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	NUMGIDS	(NGROUPS_MAX + 1)	/* group-access-list + gid */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate extern char **getline();
617c478bd9Sstevel@tonic-gate extern char *malloc();
627c478bd9Sstevel@tonic-gate extern char *strcpy();
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * The group list
667c478bd9Sstevel@tonic-gate  * Store username and groups to which he/she belongs
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate struct group_list {
697c478bd9Sstevel@tonic-gate 	char *user;
707c478bd9Sstevel@tonic-gate 	int group_len;
717c478bd9Sstevel@tonic-gate 	int groups[NUMGIDS];
727c478bd9Sstevel@tonic-gate 	struct group_list *next;
737c478bd9Sstevel@tonic-gate };
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * General purpose list of strings
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate struct string_list {
797c478bd9Sstevel@tonic-gate 	char *str;
807c478bd9Sstevel@tonic-gate 	struct string_list *next;
817c478bd9Sstevel@tonic-gate };
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static FILE *openfile();
847c478bd9Sstevel@tonic-gate static char *scanargs();
857c478bd9Sstevel@tonic-gate static int atoi();
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static char *cmdname;	/* name of this program */
887c478bd9Sstevel@tonic-gate static int quietmode;	/* quiet mode: don't print error messages */
897c478bd9Sstevel@tonic-gate static char *curfile;	/* name of file we are parsing */
907c478bd9Sstevel@tonic-gate static int curline;		/* current line parsed in this file */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static struct group_list *groups[GRPTABSIZE];	/* group table */
937c478bd9Sstevel@tonic-gate static struct string_list *printed[PRNTABSIZE];	/* printed item table */
947c478bd9Sstevel@tonic-gate static char domain[MAXDOMAINLEN];	/* name of our domain */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static char PASSWD[] = "/etc/passwd";	/* default passwd database */
977c478bd9Sstevel@tonic-gate static char IDMAP[] = "/etc/idmap";	/* default net-id map database */
987c478bd9Sstevel@tonic-gate static char GROUP[] = "/etc/group";	/* default group database */
997c478bd9Sstevel@tonic-gate static char HOSTS[] = "/etc/hosts";	/* default hosts database */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static char *pwdfile = PASSWD;	/* password file to parse */
1027c478bd9Sstevel@tonic-gate static char *grpfile = GROUP;	/* group file */
1037c478bd9Sstevel@tonic-gate static char *hostfile = HOSTS;	/* hosts file */
1047c478bd9Sstevel@tonic-gate static char *mapfile = IDMAP;	/* network id file */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * Various separaters
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate static char WHITE[] = "\t ";
1107c478bd9Sstevel@tonic-gate static char COLON[] = ":";
1117c478bd9Sstevel@tonic-gate static char COMMA[] = ",";
1127c478bd9Sstevel@tonic-gate 
113*a506a34cSth void domapfile(char *, FILE *);
114*a506a34cSth void dogrpfile(char *, FILE *);
115*a506a34cSth void dopwdfile(char *, FILE *);
116*a506a34cSth void dohostfile(char *, FILE *);
117*a506a34cSth static int Atoi(char *);
118*a506a34cSth void check_getname(char **, char *, char *, char *, char *);
119*a506a34cSth void multdef(char *);
120*a506a34cSth static int wasprinted(char *);
121*a506a34cSth void storegid(int, char *);
122*a506a34cSth void printgroups(char *, int);
123*a506a34cSth int parseargs(int, char *[]);
124*a506a34cSth void put_s(char *);
125*a506a34cSth void put_d(int);
126*a506a34cSth 
127*a506a34cSth 
128*a506a34cSth int
1297c478bd9Sstevel@tonic-gate main(argc, argv)
1307c478bd9Sstevel@tonic-gate 	int argc;
1317c478bd9Sstevel@tonic-gate 	char *argv[];
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	FILE *pf, *mf, *gf, *hf;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	cmdname = argv[0];
1367c478bd9Sstevel@tonic-gate 	if (!parseargs(argc, argv)) {
1377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1387c478bd9Sstevel@tonic-gate 			"usage: %s [-q] [-pghm filename]\n", cmdname);
1397c478bd9Sstevel@tonic-gate 		exit(1);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	(void) getdomainname(domain, sizeof (domain));
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	pf = openfile(pwdfile);
1447c478bd9Sstevel@tonic-gate 	gf = openfile(grpfile);
1457c478bd9Sstevel@tonic-gate 	hf = openfile(hostfile);
1467c478bd9Sstevel@tonic-gate 	mf = fopen(mapfile, "r");
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (mf != NULL) {
1507c478bd9Sstevel@tonic-gate 		domapfile(mapfile, mf);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	dogrpfile(grpfile, gf);
1537c478bd9Sstevel@tonic-gate 	dopwdfile(pwdfile, pf);
1547c478bd9Sstevel@tonic-gate 	dohostfile(hostfile, hf);
1557c478bd9Sstevel@tonic-gate 
156*a506a34cSth 	return (0);
1577c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Parse the network id mapping file
1627c478bd9Sstevel@tonic-gate  */
163*a506a34cSth void
1647c478bd9Sstevel@tonic-gate domapfile(mapfile, mf)
1657c478bd9Sstevel@tonic-gate 	char *mapfile;
1667c478bd9Sstevel@tonic-gate 	FILE *mf;
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	char **lp;
1697c478bd9Sstevel@tonic-gate 	char line[MAXLINELEN];
1707c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN];
1717c478bd9Sstevel@tonic-gate 	int uid, gid;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	curfile = mapfile;
1747c478bd9Sstevel@tonic-gate 	curline = 0;
1757c478bd9Sstevel@tonic-gate 	while (lp = getline(line, sizeof (line), mf, &curline, "#")) {
1767c478bd9Sstevel@tonic-gate 		check_getname(lp, name, WHITE, WHITE, "#");
1777c478bd9Sstevel@tonic-gate 		if (wasprinted(name)) {
1787c478bd9Sstevel@tonic-gate 			multdef(name);
1797c478bd9Sstevel@tonic-gate 			continue;
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 		put_s(name);
1827c478bd9Sstevel@tonic-gate 		(void) putchar(' ');
1837c478bd9Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, "#");
1847c478bd9Sstevel@tonic-gate 		uid = Atoi(name);
1857c478bd9Sstevel@tonic-gate 		put_d(uid);
1867c478bd9Sstevel@tonic-gate 		(void) putchar(':');
1877c478bd9Sstevel@tonic-gate 		if (uid == 0) {
1887c478bd9Sstevel@tonic-gate 			check_getname(lp, name, WHITE, "\t\n ", "#");
1897c478bd9Sstevel@tonic-gate 			put_s(name);
1907c478bd9Sstevel@tonic-gate 			(void) putchar(' ');
1917c478bd9Sstevel@tonic-gate 		} else {
1927c478bd9Sstevel@tonic-gate 			check_getname(lp, name, WHITE, ",\n", "#");
1937c478bd9Sstevel@tonic-gate 			gid = Atoi(name);
1947c478bd9Sstevel@tonic-gate 			put_d(gid);
1957c478bd9Sstevel@tonic-gate 			while (getname(name, sizeof (name), WHITE, ",\n", lp,
1967c478bd9Sstevel@tonic-gate 					"#") >= 0) {
1977c478bd9Sstevel@tonic-gate 				gid = Atoi(name);
1987c478bd9Sstevel@tonic-gate 				(void) putchar(',');
1997c478bd9Sstevel@tonic-gate 				put_d(gid);
2007c478bd9Sstevel@tonic-gate 			}
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * Parse the groups file
2097c478bd9Sstevel@tonic-gate  */
210*a506a34cSth void
2117c478bd9Sstevel@tonic-gate dogrpfile(grpfile, gf)
2127c478bd9Sstevel@tonic-gate 	char *grpfile;
2137c478bd9Sstevel@tonic-gate 	FILE *gf;
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	char **lp;
2167c478bd9Sstevel@tonic-gate 	char line[MAXLINELEN];
2177c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN];
2187c478bd9Sstevel@tonic-gate 	int gid;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	curfile = grpfile;
2217c478bd9Sstevel@tonic-gate 	curline = 0;
2227c478bd9Sstevel@tonic-gate 	while (lp = getline(line, sizeof (line), gf, &curline, "")) {
2237c478bd9Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, "");
2247c478bd9Sstevel@tonic-gate 		if (name[0] == '+') {
2257c478bd9Sstevel@tonic-gate 			continue;
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, ""); /* ignore passwd */
2287c478bd9Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, "");
2297c478bd9Sstevel@tonic-gate 		gid = Atoi(name);
2307c478bd9Sstevel@tonic-gate 		while (getname(name, sizeof (name), WHITE, COMMA, lp,
2317c478bd9Sstevel@tonic-gate 				"") >= 0) {
2327c478bd9Sstevel@tonic-gate 			storegid(gid, name);
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * Parse the password file
2407c478bd9Sstevel@tonic-gate  */
241*a506a34cSth void
2427c478bd9Sstevel@tonic-gate dopwdfile(pwdfile, pf)
2437c478bd9Sstevel@tonic-gate 	char *pwdfile;
2447c478bd9Sstevel@tonic-gate 	FILE *pf;
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	char **lp;
2477c478bd9Sstevel@tonic-gate 	char line[MAXLINELEN];
2487c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN];
2497c478bd9Sstevel@tonic-gate 	char user[MAXNAMELEN];
2507c478bd9Sstevel@tonic-gate 	int uid, gid;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	curfile = pwdfile;
2537c478bd9Sstevel@tonic-gate 	curline = 0;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	while (lp = getline(line, sizeof (line), pf, &curline, "")) {
2567c478bd9Sstevel@tonic-gate 		check_getname(lp, user, WHITE, COLON, ""); 	/* username */
2577c478bd9Sstevel@tonic-gate 		if (user[0] == '-' || user[0] == '+') {
2587c478bd9Sstevel@tonic-gate 			continue;	/* NIS entry */
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, ""); /* ignore passwd */
2617c478bd9Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, ""); /* but get uid */
2627c478bd9Sstevel@tonic-gate 		uid = Atoi(name);
2637c478bd9Sstevel@tonic-gate 		user2netname(name, uid, domain);
2647c478bd9Sstevel@tonic-gate 		if (wasprinted(name)) {
2657c478bd9Sstevel@tonic-gate 			multdef(name);
2667c478bd9Sstevel@tonic-gate 			continue;
2677c478bd9Sstevel@tonic-gate 		}
2687c478bd9Sstevel@tonic-gate 		put_s(name);
2697c478bd9Sstevel@tonic-gate 		(void) putchar(' ');
2707c478bd9Sstevel@tonic-gate 		check_getname(lp, name, WHITE, COLON, "");
2717c478bd9Sstevel@tonic-gate 		gid = Atoi(name);
2727c478bd9Sstevel@tonic-gate 		put_d(uid);
2737c478bd9Sstevel@tonic-gate 		(void) putchar(':');
2747c478bd9Sstevel@tonic-gate 		printgroups(user, gid);
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * Parse the hosts file
2817c478bd9Sstevel@tonic-gate  */
282*a506a34cSth void
2837c478bd9Sstevel@tonic-gate dohostfile(hostfile, hf)
2847c478bd9Sstevel@tonic-gate 	char *hostfile;
2857c478bd9Sstevel@tonic-gate 	FILE *hf;
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	char **lp;
2887c478bd9Sstevel@tonic-gate 	char line[MAXLINELEN];
2897c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN];
2907c478bd9Sstevel@tonic-gate 	char netname[MAXNETNAMELEN];
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	curfile = hostfile;
2937c478bd9Sstevel@tonic-gate 	curline = 0;
2947c478bd9Sstevel@tonic-gate 	while (lp = getline(line, sizeof (line), hf, &curline, "#")) {
2957c478bd9Sstevel@tonic-gate 		check_getname(lp, name, WHITE, WHITE, "#");
2967c478bd9Sstevel@tonic-gate 		if (getname(name, MAXNAMELEN, WHITE, WHITE, lp, "#") != 1) {
2977c478bd9Sstevel@tonic-gate 			continue;
2987c478bd9Sstevel@tonic-gate 		}
2997c478bd9Sstevel@tonic-gate 		host2netname(netname, name, domain);
3007c478bd9Sstevel@tonic-gate 		if (wasprinted(netname)) {
3017c478bd9Sstevel@tonic-gate 			multdef(netname);
3027c478bd9Sstevel@tonic-gate 			continue;
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 		(void) printf("%s 0:%.*s\n", netname, sizeof (name), name);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * Open a file, exit on failure
3107c478bd9Sstevel@tonic-gate  */
3117c478bd9Sstevel@tonic-gate static FILE *
3127c478bd9Sstevel@tonic-gate openfile(fname)
3137c478bd9Sstevel@tonic-gate 	char *fname;
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	FILE *f;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	f = fopen(fname, "r");
3187c478bd9Sstevel@tonic-gate 	if (f == NULL) {
3197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: can't open %s\n", cmdname, fname);
3207c478bd9Sstevel@tonic-gate 		exit(1);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 	return (f);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * Print syntax error message, then exit
3277c478bd9Sstevel@tonic-gate  */
328*a506a34cSth void
3297c478bd9Sstevel@tonic-gate syntaxerror()
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: syntax error in file \"%s\", line %d\n",
3327c478bd9Sstevel@tonic-gate 	    cmdname, curfile, curline);
3337c478bd9Sstevel@tonic-gate 	exit(1);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * an atoi() that prints a message and exits upong failure
3397c478bd9Sstevel@tonic-gate  */
3407c478bd9Sstevel@tonic-gate static int
3417c478bd9Sstevel@tonic-gate Atoi(str)
3427c478bd9Sstevel@tonic-gate 	char *str;
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 	int res;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (!sscanf(str, "%d", &res)) {
3477c478bd9Sstevel@tonic-gate 		syntaxerror();
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 	return (res);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate  * Attempt to get a token from a file, print a message and exit upon failure
3557c478bd9Sstevel@tonic-gate  */
356*a506a34cSth void
3577c478bd9Sstevel@tonic-gate check_getname(lp, name, skip, term, com)
3587c478bd9Sstevel@tonic-gate 	char **lp;
3597c478bd9Sstevel@tonic-gate 	char *name;
3607c478bd9Sstevel@tonic-gate 	char *skip;
3617c478bd9Sstevel@tonic-gate 	char *term;
3627c478bd9Sstevel@tonic-gate 	char *com;
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	if (getname(name, MAXNAMELEN, skip, term, lp, com) != 1) {
3657c478bd9Sstevel@tonic-gate 		syntaxerror();
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate  * Something was defined more than once
3717c478bd9Sstevel@tonic-gate  */
372*a506a34cSth void
3737c478bd9Sstevel@tonic-gate multdef(name)
3747c478bd9Sstevel@tonic-gate 	char *name;
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	if (!quietmode) {
3777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3787c478bd9Sstevel@tonic-gate 			"%s: %s multiply defined, other definitions ignored\n",
3797c478bd9Sstevel@tonic-gate 			cmdname, name);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
383*a506a34cSth static int
3847c478bd9Sstevel@tonic-gate hash(str, size)
3857c478bd9Sstevel@tonic-gate 	unsigned char *str;
3867c478bd9Sstevel@tonic-gate 	int size;
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	unsigned val;
3897c478bd9Sstevel@tonic-gate 	int flip;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	val = 0;
3927c478bd9Sstevel@tonic-gate 	flip = 0;
3937c478bd9Sstevel@tonic-gate 	while (*str) {
3947c478bd9Sstevel@tonic-gate 		if (flip) {
3957c478bd9Sstevel@tonic-gate 			val ^= (*str++ << 6);
3967c478bd9Sstevel@tonic-gate 		} else {
3977c478bd9Sstevel@tonic-gate 			val ^= *str++;
3987c478bd9Sstevel@tonic-gate 		}
3997c478bd9Sstevel@tonic-gate 		flip = !flip;
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 	return (val % size);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate  * Check if an item has been printed
4077c478bd9Sstevel@tonic-gate  * If not, store the item into the printed item table
4087c478bd9Sstevel@tonic-gate  */
409*a506a34cSth static int
4107c478bd9Sstevel@tonic-gate wasprinted(name)
4117c478bd9Sstevel@tonic-gate 	char *name;
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	struct string_list *s;
4147c478bd9Sstevel@tonic-gate 	int val;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	val = hash((unsigned char *) name, PRNTABSIZE);
4177c478bd9Sstevel@tonic-gate 	for (s = printed[val]; s != NULL && strcmp(s->str, name); s = s->next)
4187c478bd9Sstevel@tonic-gate 		;
4197c478bd9Sstevel@tonic-gate 	if (s != NULL) {
4207c478bd9Sstevel@tonic-gate 		return (1);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 	s = (struct string_list *)malloc(sizeof (struct string_list));
4237c478bd9Sstevel@tonic-gate 	s->str = malloc((unsigned)strlen(name) + 1);
4247c478bd9Sstevel@tonic-gate 	(void) strcpy(s->str, name);
4257c478bd9Sstevel@tonic-gate 	s->next = printed[val];
4267c478bd9Sstevel@tonic-gate 	printed[val] = s;
4277c478bd9Sstevel@tonic-gate 	return (0);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate  * Add gid to the list of a user's groups
4327c478bd9Sstevel@tonic-gate  */
433*a506a34cSth void
4347c478bd9Sstevel@tonic-gate storegid(gid, user)
4357c478bd9Sstevel@tonic-gate 	int gid;
4367c478bd9Sstevel@tonic-gate 	char *user;
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	struct group_list *g;
4397c478bd9Sstevel@tonic-gate 	int i;
4407c478bd9Sstevel@tonic-gate 	int val;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	val = hash((unsigned char *) user, GRPTABSIZE);
4437c478bd9Sstevel@tonic-gate 	for (g = groups[val]; g != NULL && strcmp(g->user, user); g = g->next)
4447c478bd9Sstevel@tonic-gate 		;
4457c478bd9Sstevel@tonic-gate 	if (g == NULL) {
4467c478bd9Sstevel@tonic-gate 		g = (struct group_list *)malloc(sizeof (struct group_list));
4477c478bd9Sstevel@tonic-gate 		g->user = malloc((unsigned)strlen(user) + 1);
4487c478bd9Sstevel@tonic-gate 		(void) strcpy(g->user, user);
4497c478bd9Sstevel@tonic-gate 		g->group_len = 1;
4507c478bd9Sstevel@tonic-gate 		g->groups[0] = gid;
4517c478bd9Sstevel@tonic-gate 		g->next = groups[val];
4527c478bd9Sstevel@tonic-gate 		groups[val] = g;
4537c478bd9Sstevel@tonic-gate 	} else {
4547c478bd9Sstevel@tonic-gate 		for (i = 0; i < g->group_len; i++) {
4557c478bd9Sstevel@tonic-gate 			if (g->groups[i] == gid) {
4567c478bd9Sstevel@tonic-gate 				return;
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 		if (g->group_len >= NUMGIDS) {
4607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s's groups exceed %d\n",
4617c478bd9Sstevel@tonic-gate 				cmdname, user, NGROUPS_MAX);
4627c478bd9Sstevel@tonic-gate 			return;
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 		g->groups[g->group_len++] = gid;
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  * print out a user's groups
4707c478bd9Sstevel@tonic-gate  */
471*a506a34cSth void
4727c478bd9Sstevel@tonic-gate printgroups(user, gid)
4737c478bd9Sstevel@tonic-gate 	char *user;
4747c478bd9Sstevel@tonic-gate 	int gid;
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	struct group_list *g;
4777c478bd9Sstevel@tonic-gate 	int i;
4787c478bd9Sstevel@tonic-gate 	int val;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	val = hash((unsigned char *) user, GRPTABSIZE);
4817c478bd9Sstevel@tonic-gate 	for (g = groups[val]; g != NULL && strcmp(g->user, user); g = g->next)
4827c478bd9Sstevel@tonic-gate 		;
4837c478bd9Sstevel@tonic-gate 	put_d(gid);
4847c478bd9Sstevel@tonic-gate 	if (g != NULL) {
4857c478bd9Sstevel@tonic-gate 		for (i = 0; i < g->group_len; i++) {
4867c478bd9Sstevel@tonic-gate 			if (gid != g->groups[i]) {
4877c478bd9Sstevel@tonic-gate 				(void) putchar(',');
4887c478bd9Sstevel@tonic-gate 				put_d(g->groups[i]);
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate  * Parse command line arguments
4987c478bd9Sstevel@tonic-gate  */
499*a506a34cSth int
5007c478bd9Sstevel@tonic-gate parseargs(argc, argv)
5017c478bd9Sstevel@tonic-gate 	int argc;
5027c478bd9Sstevel@tonic-gate 	char *argv[];
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	int i;
5057c478bd9Sstevel@tonic-gate 	int j;
5067c478bd9Sstevel@tonic-gate 	static struct {
5077c478bd9Sstevel@tonic-gate 		char letter;
5087c478bd9Sstevel@tonic-gate 		char *standard;
5097c478bd9Sstevel@tonic-gate 		char **filename;
5107c478bd9Sstevel@tonic-gate 	} whattodo[] = {
5117c478bd9Sstevel@tonic-gate 		{ 'p', PASSWD, &pwdfile },
5127c478bd9Sstevel@tonic-gate 		{ 'g', GROUP, &grpfile },
5137c478bd9Sstevel@tonic-gate 		{ 'm', IDMAP, &mapfile },
5147c478bd9Sstevel@tonic-gate 		{ 'h', HOSTS, &hostfile }
5157c478bd9Sstevel@tonic-gate 	};
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate #define	TABSIZE  sizeof (whattodo)/sizeof (whattodo[0])
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
5207c478bd9Sstevel@tonic-gate 		if (argv[i][0] == '-') {
5217c478bd9Sstevel@tonic-gate 			if (argv[i][2] != 0) {
5227c478bd9Sstevel@tonic-gate 				return (0);
5237c478bd9Sstevel@tonic-gate 			}
5247c478bd9Sstevel@tonic-gate 			if (argv[i][1] == 'q') {
5257c478bd9Sstevel@tonic-gate 				quietmode = 1;
5267c478bd9Sstevel@tonic-gate 				continue;
5277c478bd9Sstevel@tonic-gate 			}
5287c478bd9Sstevel@tonic-gate 			for (j = 0; j < TABSIZE; j++) {
5297c478bd9Sstevel@tonic-gate 				if (whattodo[j].letter == argv[i][1]) {
5307c478bd9Sstevel@tonic-gate 					if (*whattodo[j].filename !=
5317c478bd9Sstevel@tonic-gate 							whattodo[j].standard) {
5327c478bd9Sstevel@tonic-gate 						return (0);
5337c478bd9Sstevel@tonic-gate 					}
5347c478bd9Sstevel@tonic-gate 					if (++i == argc) {
5357c478bd9Sstevel@tonic-gate 						return (0);
5367c478bd9Sstevel@tonic-gate 					}
5377c478bd9Sstevel@tonic-gate 					*whattodo[j].filename = argv[i];
5387c478bd9Sstevel@tonic-gate 					break;
5397c478bd9Sstevel@tonic-gate 				}
5407c478bd9Sstevel@tonic-gate 			}
5417c478bd9Sstevel@tonic-gate 			if (j == TABSIZE) {
5427c478bd9Sstevel@tonic-gate 				return (0);
5437c478bd9Sstevel@tonic-gate 			}
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 	return (1);
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate /*
5507c478bd9Sstevel@tonic-gate  * Print a string, quickly
5517c478bd9Sstevel@tonic-gate  */
552*a506a34cSth void
5537c478bd9Sstevel@tonic-gate put_s(s)
5547c478bd9Sstevel@tonic-gate 	char *s;
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	(void) fwrite(s, strlen(s), 1, stdout);
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate /*
5607c478bd9Sstevel@tonic-gate  * Print an integer, quickly
5617c478bd9Sstevel@tonic-gate  */
562*a506a34cSth void
5637c478bd9Sstevel@tonic-gate put_d(d)
5647c478bd9Sstevel@tonic-gate 	int d;
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	char buf[20];
5677c478bd9Sstevel@tonic-gate 	char *p;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	if (d == 0) {
5707c478bd9Sstevel@tonic-gate 		(void) putchar('0');
5717c478bd9Sstevel@tonic-gate 		return;
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 	if (d < 0) {
5747c478bd9Sstevel@tonic-gate 		(void) putchar('-');
5757c478bd9Sstevel@tonic-gate 		d = -d;
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 	p = buf + sizeof (buf);
5787c478bd9Sstevel@tonic-gate 	*--p = 0;
5797c478bd9Sstevel@tonic-gate 	while (d > 0) {
5807c478bd9Sstevel@tonic-gate 		*--p = (d % 10) + '0';
5817c478bd9Sstevel@tonic-gate 		d /= 10;
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	put_s(p);
5847c478bd9Sstevel@tonic-gate }
585