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