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