xref: /onnv-gate/usr/src/cmd/ypcmd/revnetgroup/revnetgroup.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM  * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*13093SRoger.Faulkner@Oracle.COM 
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
25*13093SRoger.Faulkner@Oracle.COM 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * For SUNWnskit - version 1.1
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #include <pwd.h>
360Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
370Sstevel@tonic-gate #include "util.h"
380Sstevel@tonic-gate #include "table.h"
390Sstevel@tonic-gate #include "getgroup.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #define	MAXDOMAINLEN 256
420Sstevel@tonic-gate #define	MAXGROUPLEN 1024
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * Reverse the netgroup file. A flag of "-u" means reverse by username,
460Sstevel@tonic-gate  * one of "-h" means reverse by hostname. Each line in the output file
470Sstevel@tonic-gate  * will begin with a key formed by concatenating the host or user name
480Sstevel@tonic-gate  * with the domain name. The key will be followed by a tab, then the
490Sstevel@tonic-gate  * comma-separated, newline-terminated list of groups to which the
500Sstevel@tonic-gate  * user or host belongs.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * Exception: Groups to which everyone belongs (universal groups) will
530Sstevel@tonic-gate  * not be included in the list.  The universal groups will be listed under
540Sstevel@tonic-gate  * the special name "*".
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * Thus to find out all the groups that user "foo" of domain "bar" is in,
570Sstevel@tonic-gate  * lookup the groups under  foo.bar, foo.*, *.bar and *.*.
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /* Stores a list of strings */
640Sstevel@tonic-gate typedef struct stringnode *stringlist;
650Sstevel@tonic-gate struct stringnode {
660Sstevel@tonic-gate     char *str;
670Sstevel@tonic-gate     stringlist next;
680Sstevel@tonic-gate };
690Sstevel@tonic-gate typedef struct stringnode stringnode;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /* Stores a list of (name,list-of-groups) */
740Sstevel@tonic-gate typedef struct groupentrynode *groupentrylist;
750Sstevel@tonic-gate struct groupentrynode {
760Sstevel@tonic-gate     char *name;
770Sstevel@tonic-gate     stringlist groups;
780Sstevel@tonic-gate     groupentrylist next;
790Sstevel@tonic-gate };
800Sstevel@tonic-gate typedef struct groupentrynode groupentrynode;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate stringtable ngtable;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static groupentrylist grouptable[TABLESIZE];
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static char *nextgroup(void);
870Sstevel@tonic-gate static void storegroup(char *group, struct grouplist *glist, int byuser);
880Sstevel@tonic-gate static void enter(char *name, char *group);
890Sstevel@tonic-gate static void appendgroup(groupentrylist grlist, char *group);
900Sstevel@tonic-gate static groupentrylist newentry(char *name, char *group);
910Sstevel@tonic-gate static void loadtable(FILE *nf);
920Sstevel@tonic-gate static void dumptable(void);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate int
main(argc,argv)950Sstevel@tonic-gate main(argc, argv)
960Sstevel@tonic-gate     int argc;
970Sstevel@tonic-gate     char *argv[];
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	char *group;
1000Sstevel@tonic-gate 	struct grouplist *glist;
1010Sstevel@tonic-gate 	int byuser;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	loadtable(stdin);
1040Sstevel@tonic-gate 	if (argc == 2 && argv[1][0] == '-' &&
1050Sstevel@tonic-gate 			(argv[1][1] == 'u' || argv[1][1] == 'h')) {
1060Sstevel@tonic-gate 		byuser = (argv[1][1] == 'u');
1070Sstevel@tonic-gate 	} else {
1080Sstevel@tonic-gate 		(void) fprintf(stderr,
1090Sstevel@tonic-gate 				"usage: %s -h (by host), %s -u (by user)\n",
1100Sstevel@tonic-gate 				argv[0], argv[0]);
1110Sstevel@tonic-gate 		exit(1);
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	while (group = nextgroup()) {
1150Sstevel@tonic-gate 		glist = my_getgroup(group);
1160Sstevel@tonic-gate 		storegroup(group, glist, byuser);
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 	dumptable();
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	return (0);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  *	Get the next netgroup from /etc/netgroup
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate static char *
nextgroup(void)1270Sstevel@tonic-gate nextgroup(void)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	static int index = -1;
1300Sstevel@tonic-gate 	static tablelist cur = NULL;
1310Sstevel@tonic-gate 	char *group;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	while (cur == NULL) {
1340Sstevel@tonic-gate 		if (++index == TABLESIZE) {
1350Sstevel@tonic-gate 			return (NULL);
1360Sstevel@tonic-gate 		}
1370Sstevel@tonic-gate 		cur = ngtable[index];
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	group = cur->key;
1400Sstevel@tonic-gate 	cur = cur->next;
1410Sstevel@tonic-gate 	return (group);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate  * Dump out all of the stored info into a file
1480Sstevel@tonic-gate  */
1490Sstevel@tonic-gate static void
dumptable(void)1500Sstevel@tonic-gate dumptable(void)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	int i;
1530Sstevel@tonic-gate 	groupentrylist entry;
1540Sstevel@tonic-gate 	stringlist groups;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	for (i = 0; i < TABLESIZE; i++) {
1570Sstevel@tonic-gate 		if (entry = grouptable[i]) {
1580Sstevel@tonic-gate 			while (entry) {
1590Sstevel@tonic-gate 				fputs(entry->name, stdout);
1600Sstevel@tonic-gate 				putc('\t', stdout);
1610Sstevel@tonic-gate 				for (groups = entry->groups; groups;
1620Sstevel@tonic-gate 						groups = groups->next) {
1630Sstevel@tonic-gate 					fputs(groups->str, stdout);
1640Sstevel@tonic-gate 					if (groups->next) {
1650Sstevel@tonic-gate 						putc(',', stdout);
1660Sstevel@tonic-gate 					}
1670Sstevel@tonic-gate 				}
1680Sstevel@tonic-gate 				putc('\n', stdout);
1690Sstevel@tonic-gate 				entry = entry->next;
1700Sstevel@tonic-gate 			}
1710Sstevel@tonic-gate 		}
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate  *	Add a netgroup to a user's list of netgroups
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate static void
storegroup(char * group,struct grouplist * glist,int byuser)1820Sstevel@tonic-gate storegroup(char *group, struct grouplist *glist, int byuser)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	char *name;	/* username or hostname */
1850Sstevel@tonic-gate 	char *domain;
1860Sstevel@tonic-gate 	char *key;
1870Sstevel@tonic-gate 	static char *universal = "*";
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	for (; glist; glist = glist->gl_nxt) {
1900Sstevel@tonic-gate 		name = byuser ? glist->gl_name : glist->gl_machine;
1910Sstevel@tonic-gate 		if (!name) {
1920Sstevel@tonic-gate 		    name = universal;
1930Sstevel@tonic-gate 		} else if (!isalnum(*name) && *name != '_') {
1940Sstevel@tonic-gate 		    continue;
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 		domain = glist->gl_domain;
1970Sstevel@tonic-gate 		if (!domain) {
1980Sstevel@tonic-gate 		    domain = universal;
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 		key = malloc((unsigned) (strlen(name)+strlen(domain)+2));
2010Sstevel@tonic-gate 		(void) sprintf(key, "%s.%s", name, domain);
2020Sstevel@tonic-gate 		enter(key, group);
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate static groupentrylist
newentry(char * name,char * group)2090Sstevel@tonic-gate newentry(char *name, char *group)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	groupentrylist new;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	new = MALLOC(groupentrynode);
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	STRCPY(new->name, name);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	new->groups = MALLOC(stringnode);
2180Sstevel@tonic-gate 	new->groups->str = group;
2190Sstevel@tonic-gate 	new->groups->next = NULL;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	new->next = NULL;
2220Sstevel@tonic-gate 	return (new);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate static void
appendgroup(groupentrylist grlist,char * group)2260Sstevel@tonic-gate appendgroup(groupentrylist grlist, char *group)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	stringlist cur, prev;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	for (cur = grlist->groups; cur; prev = cur, cur = cur->next) {
2310Sstevel@tonic-gate 		if (strcmp(group, cur->str) == 0) {
2320Sstevel@tonic-gate 		    return;
2330Sstevel@tonic-gate 		}
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 	prev->next = MALLOC(stringnode);
2360Sstevel@tonic-gate 	cur = prev->next;
2370Sstevel@tonic-gate 	cur->str = group;
2380Sstevel@tonic-gate 	cur->next = NULL;
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static void
enter(char * name,char * group)2420Sstevel@tonic-gate enter(char *name, char *group)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	int key;
2450Sstevel@tonic-gate 	groupentrylist gel;
2460Sstevel@tonic-gate 	groupentrylist gelprev;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	key = tablekey(name);
2490Sstevel@tonic-gate 	if (grouptable[key] == NULL) {
2500Sstevel@tonic-gate 		grouptable[key] = newentry(name, group);
2510Sstevel@tonic-gate 	} else {
2520Sstevel@tonic-gate 		gel = grouptable[key];
2530Sstevel@tonic-gate 		while (gel && strcmp(gel->name, name)) {
2540Sstevel@tonic-gate 		    gelprev = gel;
2550Sstevel@tonic-gate 		    gel = gel->next;
2560Sstevel@tonic-gate 		}
2570Sstevel@tonic-gate 		if (gel) {
2580Sstevel@tonic-gate 		    appendgroup(gel, group);
2590Sstevel@tonic-gate 		} else {
2600Sstevel@tonic-gate 		    gelprev->next = newentry(name, group);
2610Sstevel@tonic-gate 		}
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate  * Load up a hash table with the info in /etc/netgroup
2670Sstevel@tonic-gate  */
2680Sstevel@tonic-gate static void
loadtable(FILE * nf)2690Sstevel@tonic-gate loadtable(FILE *nf)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	char buf[MAXGROUPLEN];
2720Sstevel@tonic-gate 	char *p;
2730Sstevel@tonic-gate 	char *group;
2740Sstevel@tonic-gate 	char *line;
2750Sstevel@tonic-gate 
276*13093SRoger.Faulkner@Oracle.COM 	while (getaline(buf, MAXGROUPLEN, nf)) {
2770Sstevel@tonic-gate 		for (p = buf; *p && isspace((int)*p); p++)
2780Sstevel@tonic-gate 			;	/* skip leading blanks */
2790Sstevel@tonic-gate 		for (; *p && *p != '#' && *p != ' ' && *p != '\t'; p++)
2800Sstevel@tonic-gate 			;
2810Sstevel@tonic-gate 		if (*p == EOS || *p == '#')
2820Sstevel@tonic-gate 			continue;
2830Sstevel@tonic-gate 		*p++ = EOS;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 		while (*p == ' ' || *p == '\t') {
2860Sstevel@tonic-gate 			p++;
2870Sstevel@tonic-gate 		}
2880Sstevel@tonic-gate 		if (*p == EOS || *p == '#')
2890Sstevel@tonic-gate 			continue;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 		STRCPY(group, buf);
2920Sstevel@tonic-gate 		STRCPY(line, p);
2930Sstevel@tonic-gate 		store(ngtable, group, line);
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate }
296