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.
247c478bd9Sstevel@tonic-gate  */
2523a1cceaSRoger A. Faulkner 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * For SUNWnskit - version 1.1
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <pwd.h>
367c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
377c478bd9Sstevel@tonic-gate #include "util.h"
387c478bd9Sstevel@tonic-gate #include "table.h"
397c478bd9Sstevel@tonic-gate #include "getgroup.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	MAXDOMAINLEN 256
427c478bd9Sstevel@tonic-gate #define	MAXGROUPLEN 1024
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Reverse the netgroup file. A flag of "-u" means reverse by username,
467c478bd9Sstevel@tonic-gate  * one of "-h" means reverse by hostname. Each line in the output file
477c478bd9Sstevel@tonic-gate  * will begin with a key formed by concatenating the host or user name
487c478bd9Sstevel@tonic-gate  * with the domain name. The key will be followed by a tab, then the
497c478bd9Sstevel@tonic-gate  * comma-separated, newline-terminated list of groups to which the
507c478bd9Sstevel@tonic-gate  * user or host belongs.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * Exception: Groups to which everyone belongs (universal groups) will
537c478bd9Sstevel@tonic-gate  * not be included in the list.  The universal groups will be listed under
547c478bd9Sstevel@tonic-gate  * the special name "*".
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * Thus to find out all the groups that user "foo" of domain "bar" is in,
577c478bd9Sstevel@tonic-gate  * lookup the groups under  foo.bar, foo.*, *.bar and *.*.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* Stores a list of strings */
647c478bd9Sstevel@tonic-gate typedef struct stringnode *stringlist;
657c478bd9Sstevel@tonic-gate struct stringnode {
667c478bd9Sstevel@tonic-gate     char *str;
677c478bd9Sstevel@tonic-gate     stringlist next;
687c478bd9Sstevel@tonic-gate };
697c478bd9Sstevel@tonic-gate typedef struct stringnode stringnode;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /* Stores a list of (name,list-of-groups) */
747c478bd9Sstevel@tonic-gate typedef struct groupentrynode *groupentrylist;
757c478bd9Sstevel@tonic-gate struct groupentrynode {
767c478bd9Sstevel@tonic-gate     char *name;
777c478bd9Sstevel@tonic-gate     stringlist groups;
787c478bd9Sstevel@tonic-gate     groupentrylist next;
797c478bd9Sstevel@tonic-gate };
807c478bd9Sstevel@tonic-gate typedef struct groupentrynode groupentrynode;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate stringtable ngtable;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static groupentrylist grouptable[TABLESIZE];
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static char *nextgroup(void);
877c478bd9Sstevel@tonic-gate static void storegroup(char *group, struct grouplist *glist, int byuser);
887c478bd9Sstevel@tonic-gate static void enter(char *name, char *group);
897c478bd9Sstevel@tonic-gate static void appendgroup(groupentrylist grlist, char *group);
907c478bd9Sstevel@tonic-gate static groupentrylist newentry(char *name, char *group);
917c478bd9Sstevel@tonic-gate static void loadtable(FILE *nf);
927c478bd9Sstevel@tonic-gate static void dumptable(void);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])95*920f538eSToomas Soome main(int argc, char *argv[])
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	char *group;
987c478bd9Sstevel@tonic-gate 	struct grouplist *glist;
997c478bd9Sstevel@tonic-gate 	int byuser;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	loadtable(stdin);
1027c478bd9Sstevel@tonic-gate 	if (argc == 2 && argv[1][0] == '-' &&
103*920f538eSToomas Soome 	    (argv[1][1] == 'u' || argv[1][1] == 'h')) {
1047c478bd9Sstevel@tonic-gate 		byuser = (argv[1][1] == 'u');
1057c478bd9Sstevel@tonic-gate 	} else {
1067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
107*920f538eSToomas Soome 		    "usage: %s -h (by host), %s -u (by user)\n",
108*920f538eSToomas Soome 		    argv[0], argv[0]);
1097c478bd9Sstevel@tonic-gate 		exit(1);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	while (group = nextgroup()) {
1137c478bd9Sstevel@tonic-gate 		glist = my_getgroup(group);
1147c478bd9Sstevel@tonic-gate 		storegroup(group, glist, byuser);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 	dumptable();
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	return (0);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  *	Get the next netgroup from /etc/netgroup
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate static char *
nextgroup(void)1257c478bd9Sstevel@tonic-gate nextgroup(void)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	static int index = -1;
1287c478bd9Sstevel@tonic-gate 	static tablelist cur = NULL;
1297c478bd9Sstevel@tonic-gate 	char *group;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	while (cur == NULL) {
1327c478bd9Sstevel@tonic-gate 		if (++index == TABLESIZE) {
1337c478bd9Sstevel@tonic-gate 			return (NULL);
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 		cur = ngtable[index];
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	group = cur->key;
1387c478bd9Sstevel@tonic-gate 	cur = cur->next;
1397c478bd9Sstevel@tonic-gate 	return (group);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * Dump out all of the stored info into a file
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate static void
dumptable(void)1487c478bd9Sstevel@tonic-gate dumptable(void)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	int i;
1517c478bd9Sstevel@tonic-gate 	groupentrylist entry;
1527c478bd9Sstevel@tonic-gate 	stringlist groups;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	for (i = 0; i < TABLESIZE; i++) {
1557c478bd9Sstevel@tonic-gate 		if (entry = grouptable[i]) {
1567c478bd9Sstevel@tonic-gate 			while (entry) {
1577c478bd9Sstevel@tonic-gate 				fputs(entry->name, stdout);
1587c478bd9Sstevel@tonic-gate 				putc('\t', stdout);
1597c478bd9Sstevel@tonic-gate 				for (groups = entry->groups; groups;
160*920f538eSToomas Soome 				    groups = groups->next) {
1617c478bd9Sstevel@tonic-gate 					fputs(groups->str, stdout);
1627c478bd9Sstevel@tonic-gate 					if (groups->next) {
1637c478bd9Sstevel@tonic-gate 						putc(',', stdout);
1647c478bd9Sstevel@tonic-gate 					}
1657c478bd9Sstevel@tonic-gate 				}
1667c478bd9Sstevel@tonic-gate 				putc('\n', stdout);
1677c478bd9Sstevel@tonic-gate 				entry = entry->next;
1687c478bd9Sstevel@tonic-gate 			}
1697c478bd9Sstevel@tonic-gate 		}
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  *	Add a netgroup to a user's list of netgroups
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate static void
storegroup(char * group,struct grouplist * glist,int byuser)1807c478bd9Sstevel@tonic-gate storegroup(char *group, struct grouplist *glist, int byuser)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	char *name;	/* username or hostname */
1837c478bd9Sstevel@tonic-gate 	char *domain;
1847c478bd9Sstevel@tonic-gate 	char *key;
1857c478bd9Sstevel@tonic-gate 	static char *universal = "*";
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	for (; glist; glist = glist->gl_nxt) {
1887c478bd9Sstevel@tonic-gate 		name = byuser ? glist->gl_name : glist->gl_machine;
1897c478bd9Sstevel@tonic-gate 		if (!name) {
190*920f538eSToomas Soome 			name = universal;
1917c478bd9Sstevel@tonic-gate 		} else if (!isalnum(*name) && *name != '_') {
192*920f538eSToomas Soome 			continue;
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 		domain = glist->gl_domain;
1957c478bd9Sstevel@tonic-gate 		if (!domain) {
196*920f538eSToomas Soome 			domain = universal;
1977c478bd9Sstevel@tonic-gate 		}
198*920f538eSToomas Soome 		key = malloc((strlen(name) + strlen(domain) + 2));
1997c478bd9Sstevel@tonic-gate 		(void) sprintf(key, "%s.%s", name, domain);
2007c478bd9Sstevel@tonic-gate 		enter(key, group);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate static groupentrylist
newentry(char * name,char * group)2077c478bd9Sstevel@tonic-gate newentry(char *name, char *group)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	groupentrylist new;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	new = MALLOC(groupentrynode);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	STRCPY(new->name, name);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	new->groups = MALLOC(stringnode);
2167c478bd9Sstevel@tonic-gate 	new->groups->str = group;
2177c478bd9Sstevel@tonic-gate 	new->groups->next = NULL;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	new->next = NULL;
2207c478bd9Sstevel@tonic-gate 	return (new);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static void
appendgroup(groupentrylist grlist,char * group)2247c478bd9Sstevel@tonic-gate appendgroup(groupentrylist grlist, char *group)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	stringlist cur, prev;
2277c478bd9Sstevel@tonic-gate 
228*920f538eSToomas Soome 	prev = NULL;
2297c478bd9Sstevel@tonic-gate 	for (cur = grlist->groups; cur; prev = cur, cur = cur->next) {
2307c478bd9Sstevel@tonic-gate 		if (strcmp(group, cur->str) == 0) {
231*920f538eSToomas Soome 			return;
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 	}
234*920f538eSToomas Soome 
235*920f538eSToomas Soome 	/* prev is NULL only when grlist->groups is NULL.  */
236*920f538eSToomas Soome 	if (prev == NULL)
237*920f538eSToomas Soome 		return;
238*920f538eSToomas Soome 
2397c478bd9Sstevel@tonic-gate 	prev->next = MALLOC(stringnode);
2407c478bd9Sstevel@tonic-gate 	cur = prev->next;
2417c478bd9Sstevel@tonic-gate 	cur->str = group;
2427c478bd9Sstevel@tonic-gate 	cur->next = NULL;
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate static void
enter(char * name,char * group)2467c478bd9Sstevel@tonic-gate enter(char *name, char *group)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	int key;
2497c478bd9Sstevel@tonic-gate 	groupentrylist gel;
250*920f538eSToomas Soome 	groupentrylist gelprev = NULL;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	key = tablekey(name);
2537c478bd9Sstevel@tonic-gate 	if (grouptable[key] == NULL) {
2547c478bd9Sstevel@tonic-gate 		grouptable[key] = newentry(name, group);
2557c478bd9Sstevel@tonic-gate 	} else {
2567c478bd9Sstevel@tonic-gate 		gel = grouptable[key];
2577c478bd9Sstevel@tonic-gate 		while (gel && strcmp(gel->name, name)) {
258*920f538eSToomas Soome 			gelprev = gel;
259*920f538eSToomas Soome 			gel = gel->next;
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 		if (gel) {
262*920f538eSToomas Soome 			appendgroup(gel, group);
2637c478bd9Sstevel@tonic-gate 		} else {
264*920f538eSToomas Soome 			gelprev->next = newentry(name, group);
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate  * Load up a hash table with the info in /etc/netgroup
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate static void
loadtable(FILE * nf)2737c478bd9Sstevel@tonic-gate loadtable(FILE *nf)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	char buf[MAXGROUPLEN];
2767c478bd9Sstevel@tonic-gate 	char *p;
2777c478bd9Sstevel@tonic-gate 	char *group;
2787c478bd9Sstevel@tonic-gate 	char *line;
2797c478bd9Sstevel@tonic-gate 
28023a1cceaSRoger A. Faulkner 	while (getaline(buf, MAXGROUPLEN, nf)) {
281*920f538eSToomas Soome 		/* skip leading blanks */
2827c478bd9Sstevel@tonic-gate 		for (p = buf; *p && isspace((int)*p); p++)
283*920f538eSToomas Soome 			;
2847c478bd9Sstevel@tonic-gate 		for (; *p && *p != '#' && *p != ' ' && *p != '\t'; p++)
2857c478bd9Sstevel@tonic-gate 			;
2867c478bd9Sstevel@tonic-gate 		if (*p == EOS || *p == '#')
2877c478bd9Sstevel@tonic-gate 			continue;
2887c478bd9Sstevel@tonic-gate 		*p++ = EOS;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		while (*p == ' ' || *p == '\t') {
2917c478bd9Sstevel@tonic-gate 			p++;
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 		if (*p == EOS || *p == '#')
2947c478bd9Sstevel@tonic-gate 			continue;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 		STRCPY(group, buf);
2977c478bd9Sstevel@tonic-gate 		STRCPY(line, p);
2987c478bd9Sstevel@tonic-gate 		store(ngtable, group, line);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate }
301