17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
523a1cceRoger A. Faulkner * Common Development and Distribution License (the "License").
623a1cceRoger A. Faulkner * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
2123a1cceRoger A. Faulkner
227c478bdstevel@tonic-gate/*
2323a1cceRoger A. Faulkner * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bdstevel@tonic-gate */
2523a1cceRoger A. Faulkner
267c478bdstevel@tonic-gate/*
277c478bdstevel@tonic-gate * For SUNWnskit - version 1.1
287c478bdstevel@tonic-gate */
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate#include <stdlib.h>
317c478bdstevel@tonic-gate#include <unistd.h>
327c478bdstevel@tonic-gate#include <string.h>
337c478bdstevel@tonic-gate#include <stdio.h>
347c478bdstevel@tonic-gate#include <ctype.h>
357c478bdstevel@tonic-gate#include <pwd.h>
367c478bdstevel@tonic-gate#include <rpcsvc/ypclnt.h>
377c478bdstevel@tonic-gate#include "util.h"
387c478bdstevel@tonic-gate#include "table.h"
397c478bdstevel@tonic-gate#include "getgroup.h"
407c478bdstevel@tonic-gate
417c478bdstevel@tonic-gate#define	MAXDOMAINLEN 256
427c478bdstevel@tonic-gate#define	MAXGROUPLEN 1024
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gate/*
457c478bdstevel@tonic-gate * Reverse the netgroup file. A flag of "-u" means reverse by username,
467c478bdstevel@tonic-gate * one of "-h" means reverse by hostname. Each line in the output file
477c478bdstevel@tonic-gate * will begin with a key formed by concatenating the host or user name
487c478bdstevel@tonic-gate * with the domain name. The key will be followed by a tab, then the
497c478bdstevel@tonic-gate * comma-separated, newline-terminated list of groups to which the
507c478bdstevel@tonic-gate * user or host belongs.
517c478bdstevel@tonic-gate *
527c478bdstevel@tonic-gate * Exception: Groups to which everyone belongs (universal groups) will
537c478bdstevel@tonic-gate * not be included in the list.  The universal groups will be listed under
547c478bdstevel@tonic-gate * the special name "*".
557c478bdstevel@tonic-gate *
567c478bdstevel@tonic-gate * Thus to find out all the groups that user "foo" of domain "bar" is in,
577c478bdstevel@tonic-gate * lookup the groups under  foo.bar, foo.*, *.bar and *.*.
587c478bdstevel@tonic-gate *
597c478bdstevel@tonic-gate */
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate/* Stores a list of strings */
647c478bdstevel@tonic-gatetypedef struct stringnode *stringlist;
657c478bdstevel@tonic-gatestruct stringnode {
667c478bdstevel@tonic-gate    char *str;
677c478bdstevel@tonic-gate    stringlist next;
687c478bdstevel@tonic-gate};
697c478bdstevel@tonic-gatetypedef struct stringnode stringnode;
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gate/* Stores a list of (name,list-of-groups) */
747c478bdstevel@tonic-gatetypedef struct groupentrynode *groupentrylist;
757c478bdstevel@tonic-gatestruct groupentrynode {
767c478bdstevel@tonic-gate    char *name;
777c478bdstevel@tonic-gate    stringlist groups;
787c478bdstevel@tonic-gate    groupentrylist next;
797c478bdstevel@tonic-gate};
807c478bdstevel@tonic-gatetypedef struct groupentrynode groupentrynode;
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gatestringtable ngtable;
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gatestatic groupentrylist grouptable[TABLESIZE];
857c478bdstevel@tonic-gate
867c478bdstevel@tonic-gatestatic char *nextgroup(void);
877c478bdstevel@tonic-gatestatic void storegroup(char *group, struct grouplist *glist, int byuser);
887c478bdstevel@tonic-gatestatic void enter(char *name, char *group);
897c478bdstevel@tonic-gatestatic void appendgroup(groupentrylist grlist, char *group);
907c478bdstevel@tonic-gatestatic groupentrylist newentry(char *name, char *group);
917c478bdstevel@tonic-gatestatic void loadtable(FILE *nf);
927c478bdstevel@tonic-gatestatic void dumptable(void);
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gateint
957c478bdstevel@tonic-gatemain(argc, argv)
967c478bdstevel@tonic-gate    int argc;
977c478bdstevel@tonic-gate    char *argv[];
987c478bdstevel@tonic-gate{
997c478bdstevel@tonic-gate	char *group;
1007c478bdstevel@tonic-gate	struct grouplist *glist;
1017c478bdstevel@tonic-gate	int byuser;
1027c478bdstevel@tonic-gate
1037c478bdstevel@tonic-gate	loadtable(stdin);
1047c478bdstevel@tonic-gate	if (argc == 2 && argv[1][0] == '-' &&
1057c478bdstevel@tonic-gate			(argv[1][1] == 'u' || argv[1][1] == 'h')) {
1067c478bdstevel@tonic-gate		byuser = (argv[1][1] == 'u');
1077c478bdstevel@tonic-gate	} else {
1087c478bdstevel@tonic-gate		(void) fprintf(stderr,
1097c478bdstevel@tonic-gate				"usage: %s -h (by host), %s -u (by user)\n",
1107c478bdstevel@tonic-gate				argv[0], argv[0]);
1117c478bdstevel@tonic-gate		exit(1);
1127c478bdstevel@tonic-gate	}
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gate	while (group = nextgroup()) {
1157c478bdstevel@tonic-gate		glist = my_getgroup(group);
1167c478bdstevel@tonic-gate		storegroup(group, glist, byuser);
1177c478bdstevel@tonic-gate	}
1187c478bdstevel@tonic-gate	dumptable();
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gate	return (0);
1217c478bdstevel@tonic-gate}
1227c478bdstevel@tonic-gate
1237c478bdstevel@tonic-gate/*
1247c478bdstevel@tonic-gate *	Get the next netgroup from /etc/netgroup
1257c478bdstevel@tonic-gate */
1267c478bdstevel@tonic-gatestatic char *
1277c478bdstevel@tonic-gatenextgroup(void)
1287c478bdstevel@tonic-gate{
1297c478bdstevel@tonic-gate	static int index = -1;
1307c478bdstevel@tonic-gate	static tablelist cur = NULL;
1317c478bdstevel@tonic-gate	char *group;
1327c478bdstevel@tonic-gate
1337c478bdstevel@tonic-gate	while (cur == NULL) {
1347c478bdstevel@tonic-gate		if (++index == TABLESIZE) {
1357c478bdstevel@tonic-gate			return (NULL);
1367c478bdstevel@tonic-gate		}
1377c478bdstevel@tonic-gate		cur = ngtable[index];
1387c478bdstevel@tonic-gate	}
1397c478bdstevel@tonic-gate	group = cur->key;
1407c478bdstevel@tonic-gate	cur = cur->next;
1417c478bdstevel@tonic-gate	return (group);
1427c478bdstevel@tonic-gate}
1437c478bdstevel@tonic-gate
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate
1467c478bdstevel@tonic-gate/*
1477c478bdstevel@tonic-gate * Dump out all of the stored info into a file
1487c478bdstevel@tonic-gate */
1497c478bdstevel@tonic-gatestatic void
1507c478bdstevel@tonic-gatedumptable(void)
1517c478bdstevel@tonic-gate{
1527c478bdstevel@tonic-gate	int i;
1537c478bdstevel@tonic-gate	groupentrylist entry;
1547c478bdstevel@tonic-gate	stringlist groups;
1557c478bdstevel@tonic-gate
1567c478bdstevel@tonic-gate	for (i = 0; i < TABLESIZE; i++) {
1577c478bdstevel@tonic-gate		if (entry = grouptable[i]) {
1587c478bdstevel@tonic-gate			while (entry) {
1597c478bdstevel@tonic-gate				fputs(entry->name, stdout);
1607c478bdstevel@tonic-gate				putc('\t', stdout);
1617c478bdstevel@tonic-gate				for (groups = entry->groups; groups;
1627c478bdstevel@tonic-gate						groups = groups->next) {
1637c478bdstevel@tonic-gate					fputs(groups->str, stdout);
1647c478bdstevel@tonic-gate					if (groups->next) {
1657c478bdstevel@tonic-gate						putc(',', stdout);
1667c478bdstevel@tonic-gate					}
1677c478bdstevel@tonic-gate				}
1687c478bdstevel@tonic-gate				putc('\n', stdout);
1697c478bdstevel@tonic-gate				entry = entry->next;
1707c478bdstevel@tonic-gate			}
1717c478bdstevel@tonic-gate		}
1727c478bdstevel@tonic-gate	}
1737c478bdstevel@tonic-gate}
1747c478bdstevel@tonic-gate
1757c478bdstevel@tonic-gate
1767c478bdstevel@tonic-gate
1777c478bdstevel@tonic-gate
1787c478bdstevel@tonic-gate/*
1797c478bdstevel@tonic-gate *	Add a netgroup to a user's list of netgroups
1807c478bdstevel@tonic-gate */
1817c478bdstevel@tonic-gatestatic void
1827c478bdstevel@tonic-gatestoregroup(char *group, struct grouplist *glist, int byuser)
1837c478bdstevel@tonic-gate{
1847c478bdstevel@tonic-gate	char *name;	/* username or hostname */
1857c478bdstevel@tonic-gate	char *domain;
1867c478bdstevel@tonic-gate	char *key;
1877c478bdstevel@tonic-gate	static char *universal = "*";
1887c478bdstevel@tonic-gate
1897c478bdstevel@tonic-gate	for (; glist; glist = glist->gl_nxt) {
1907c478bdstevel@tonic-gate		name = byuser ? glist->gl_name : glist->gl_machine;
1917c478bdstevel@tonic-gate		if (!name) {
1927c478bdstevel@tonic-gate		    name = universal;
1937c478bdstevel@tonic-gate		} else if (!isalnum(*name) && *name != '_') {
1947c478bdstevel@tonic-gate		    continue;
1957c478bdstevel@tonic-gate		}
1967c478bdstevel@tonic-gate		domain = glist->gl_domain;
1977c478bdstevel@tonic-gate		if (!domain) {
1987c478bdstevel@tonic-gate		    domain = universal;
1997c478bdstevel@tonic-gate		}
2007c478bdstevel@tonic-gate		key = malloc((unsigned) (strlen(name)+strlen(domain)+2));
2017c478bdstevel@tonic-gate		(void) sprintf(key, "%s.%s", name, domain);
2027c478bdstevel@tonic-gate		enter(key, group);
2037c478bdstevel@tonic-gate	}
2047c478bdstevel@tonic-gate}
2057c478bdstevel@tonic-gate
2067c478bdstevel@tonic-gate
2077c478bdstevel@tonic-gate
2087c478bdstevel@tonic-gatestatic groupentrylist
2097c478bdstevel@tonic-gatenewentry(char *name, char *group)
2107c478bdstevel@tonic-gate{
2117c478bdstevel@tonic-gate	groupentrylist new;
2127c478bdstevel@tonic-gate
2137c478bdstevel@tonic-gate	new = MALLOC(groupentrynode);
2147c478bdstevel@tonic-gate
2157c478bdstevel@tonic-gate	STRCPY(new->name, name);
2167c478bdstevel@tonic-gate
2177c478bdstevel@tonic-gate	new->groups = MALLOC(stringnode);
2187c478bdstevel@tonic-gate	new->groups->str = group;
2197c478bdstevel@tonic-gate	new->groups->next = NULL;
2207c478bdstevel@tonic-gate
2217c478bdstevel@tonic-gate	new->next = NULL;
2227c478bdstevel@tonic-gate	return (new);
2237c478bdstevel@tonic-gate}
2247c478bdstevel@tonic-gate
2257c478bdstevel@tonic-gatestatic void
2267c478bdstevel@tonic-gateappendgroup(groupentrylist grlist, char *group)
2277c478bdstevel@tonic-gate{
2287c478bdstevel@tonic-gate	stringlist cur, prev;
2297c478bdstevel@tonic-gate
2307c478bdstevel@tonic-gate	for (cur = grlist->groups; cur; prev = cur, cur = cur->next) {
2317c478bdstevel@tonic-gate		if (strcmp(group, cur->str) == 0) {
2327c478bdstevel@tonic-gate		    return;
2337c478bdstevel@tonic-gate		}
2347c478bdstevel@tonic-gate	}
2357c478bdstevel@tonic-gate	prev->next = MALLOC(stringnode);
2367c478bdstevel@tonic-gate	cur = prev->next;
2377c478bdstevel@tonic-gate	cur->str = group;
2387c478bdstevel@tonic-gate	cur->next = NULL;
2397c478bdstevel@tonic-gate}
2407c478bdstevel@tonic-gate
2417c478bdstevel@tonic-gatestatic void
2427c478bdstevel@tonic-gateenter(char *name, char *group)
2437c478bdstevel@tonic-gate{
2447c478bdstevel@tonic-gate	int key;
2457c478bdstevel@tonic-gate	groupentrylist gel;
2467c478bdstevel@tonic-gate	groupentrylist gelprev;
2477c478bdstevel@tonic-gate
2487c478bdstevel@tonic-gate	key = tablekey(name);
2497c478bdstevel@tonic-gate	if (grouptable[key] == NULL) {
2507c478bdstevel@tonic-gate		grouptable[key] = newentry(name, group);
2517c478bdstevel@tonic-gate	} else {
2527c478bdstevel@tonic-gate		gel = grouptable[key];
2537c478bdstevel@tonic-gate		while (gel && strcmp(gel->name, name)) {
2547c478bdstevel@tonic-gate		    gelprev = gel;
2557c478bdstevel@tonic-gate		    gel = gel->next;
2567c478bdstevel@tonic-gate		}
2577c478bdstevel@tonic-gate		if (gel) {
2587c478bdstevel@tonic-gate		    appendgroup(gel, group);
2597c478bdstevel@tonic-gate		} else {
2607c478bdstevel@tonic-gate		    gelprev->next = newentry(name, group);
2617c478bdstevel@tonic-gate		}
2627c478bdstevel@tonic-gate	}
2637c478bdstevel@tonic-gate}
2647c478bdstevel@tonic-gate
2657c478bdstevel@tonic-gate/*
2667c478bdstevel@tonic-gate * Load up a hash table with the info in /etc/netgroup
2677c478bdstevel@tonic-gate */
2687c478bdstevel@tonic-gatestatic void
2697c478bdstevel@tonic-gateloadtable(FILE *nf)
2707c478bdstevel@tonic-gate{
2717c478bdstevel@tonic-gate	char buf[MAXGROUPLEN];
2727c478bdstevel@tonic-gate	char *p;
2737c478bdstevel@tonic-gate	char *group;
2747c478bdstevel@tonic-gate	char *line;
2757c478bdstevel@tonic-gate
27623a1cceRoger A. Faulkner	while (getaline(buf, MAXGROUPLEN, nf)) {
2777c478bdstevel@tonic-gate		for (p = buf; *p && isspace((int)*p); p++)
2787c478bdstevel@tonic-gate			;	/* skip leading blanks */
2797c478bdstevel@tonic-gate		for (; *p && *p != '#' && *p != ' ' && *p != '\t'; p++)
2807c478bdstevel@tonic-gate			;
2817c478bdstevel@tonic-gate		if (*p == EOS || *p == '#')
2827c478bdstevel@tonic-gate			continue;
2837c478bdstevel@tonic-gate		*p++ = EOS;
2847c478bdstevel@tonic-gate
2857c478bdstevel@tonic-gate		while (*p == ' ' || *p == '\t') {
2867c478bdstevel@tonic-gate			p++;
2877c478bdstevel@tonic-gate		}
2887c478bdstevel@tonic-gate		if (*p == EOS || *p == '#')
2897c478bdstevel@tonic-gate			continue;
2907c478bdstevel@tonic-gate
2917c478bdstevel@tonic-gate		STRCPY(group, buf);
2927c478bdstevel@tonic-gate		STRCPY(line, p);
2937c478bdstevel@tonic-gate		store(ngtable, group, line);
2947c478bdstevel@tonic-gate	}
2957c478bdstevel@tonic-gate}
296