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 * 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(argc,argv)957c478bd9Sstevel@tonic-gate main(argc, argv)
967c478bd9Sstevel@tonic-gate int argc;
977c478bd9Sstevel@tonic-gate char *argv[];
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate char *group;
1007c478bd9Sstevel@tonic-gate struct grouplist *glist;
1017c478bd9Sstevel@tonic-gate int byuser;
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate loadtable(stdin);
1047c478bd9Sstevel@tonic-gate if (argc == 2 && argv[1][0] == '-' &&
1057c478bd9Sstevel@tonic-gate (argv[1][1] == 'u' || argv[1][1] == 'h')) {
1067c478bd9Sstevel@tonic-gate byuser = (argv[1][1] == 'u');
1077c478bd9Sstevel@tonic-gate } else {
1087c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1097c478bd9Sstevel@tonic-gate "usage: %s -h (by host), %s -u (by user)\n",
1107c478bd9Sstevel@tonic-gate argv[0], argv[0]);
1117c478bd9Sstevel@tonic-gate exit(1);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate while (group = nextgroup()) {
1157c478bd9Sstevel@tonic-gate glist = my_getgroup(group);
1167c478bd9Sstevel@tonic-gate storegroup(group, glist, byuser);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate dumptable();
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate return (0);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate * Get the next netgroup from /etc/netgroup
1257c478bd9Sstevel@tonic-gate */
1267c478bd9Sstevel@tonic-gate static char *
nextgroup(void)1277c478bd9Sstevel@tonic-gate nextgroup(void)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate static int index = -1;
1307c478bd9Sstevel@tonic-gate static tablelist cur = NULL;
1317c478bd9Sstevel@tonic-gate char *group;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate while (cur == NULL) {
1347c478bd9Sstevel@tonic-gate if (++index == TABLESIZE) {
1357c478bd9Sstevel@tonic-gate return (NULL);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate cur = ngtable[index];
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate group = cur->key;
1407c478bd9Sstevel@tonic-gate cur = cur->next;
1417c478bd9Sstevel@tonic-gate return (group);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate * Dump out all of the stored info into a file
1487c478bd9Sstevel@tonic-gate */
1497c478bd9Sstevel@tonic-gate static void
dumptable(void)1507c478bd9Sstevel@tonic-gate dumptable(void)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate int i;
1537c478bd9Sstevel@tonic-gate groupentrylist entry;
1547c478bd9Sstevel@tonic-gate stringlist groups;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate for (i = 0; i < TABLESIZE; i++) {
1577c478bd9Sstevel@tonic-gate if (entry = grouptable[i]) {
1587c478bd9Sstevel@tonic-gate while (entry) {
1597c478bd9Sstevel@tonic-gate fputs(entry->name, stdout);
1607c478bd9Sstevel@tonic-gate putc('\t', stdout);
1617c478bd9Sstevel@tonic-gate for (groups = entry->groups; groups;
1627c478bd9Sstevel@tonic-gate groups = groups->next) {
1637c478bd9Sstevel@tonic-gate fputs(groups->str, stdout);
1647c478bd9Sstevel@tonic-gate if (groups->next) {
1657c478bd9Sstevel@tonic-gate putc(',', stdout);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate putc('\n', stdout);
1697c478bd9Sstevel@tonic-gate entry = entry->next;
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate * Add a netgroup to a user's list of netgroups
1807c478bd9Sstevel@tonic-gate */
1817c478bd9Sstevel@tonic-gate static void
storegroup(char * group,struct grouplist * glist,int byuser)1827c478bd9Sstevel@tonic-gate storegroup(char *group, struct grouplist *glist, int byuser)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate char *name; /* username or hostname */
1857c478bd9Sstevel@tonic-gate char *domain;
1867c478bd9Sstevel@tonic-gate char *key;
1877c478bd9Sstevel@tonic-gate static char *universal = "*";
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate for (; glist; glist = glist->gl_nxt) {
1907c478bd9Sstevel@tonic-gate name = byuser ? glist->gl_name : glist->gl_machine;
1917c478bd9Sstevel@tonic-gate if (!name) {
1927c478bd9Sstevel@tonic-gate name = universal;
1937c478bd9Sstevel@tonic-gate } else if (!isalnum(*name) && *name != '_') {
1947c478bd9Sstevel@tonic-gate continue;
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate domain = glist->gl_domain;
1977c478bd9Sstevel@tonic-gate if (!domain) {
1987c478bd9Sstevel@tonic-gate domain = universal;
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate key = malloc((unsigned) (strlen(name)+strlen(domain)+2));
2017c478bd9Sstevel@tonic-gate (void) sprintf(key, "%s.%s", name, domain);
2027c478bd9Sstevel@tonic-gate enter(key, group);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate static groupentrylist
newentry(char * name,char * group)2097c478bd9Sstevel@tonic-gate newentry(char *name, char *group)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate groupentrylist new;
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate new = MALLOC(groupentrynode);
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate STRCPY(new->name, name);
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate new->groups = MALLOC(stringnode);
2187c478bd9Sstevel@tonic-gate new->groups->str = group;
2197c478bd9Sstevel@tonic-gate new->groups->next = NULL;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate new->next = NULL;
2227c478bd9Sstevel@tonic-gate return (new);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate static void
appendgroup(groupentrylist grlist,char * group)2267c478bd9Sstevel@tonic-gate appendgroup(groupentrylist grlist, char *group)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate stringlist cur, prev;
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate for (cur = grlist->groups; cur; prev = cur, cur = cur->next) {
2317c478bd9Sstevel@tonic-gate if (strcmp(group, cur->str) == 0) {
2327c478bd9Sstevel@tonic-gate return;
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate prev->next = MALLOC(stringnode);
2367c478bd9Sstevel@tonic-gate cur = prev->next;
2377c478bd9Sstevel@tonic-gate cur->str = group;
2387c478bd9Sstevel@tonic-gate cur->next = NULL;
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate static void
enter(char * name,char * group)2427c478bd9Sstevel@tonic-gate enter(char *name, char *group)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate int key;
2457c478bd9Sstevel@tonic-gate groupentrylist gel;
2467c478bd9Sstevel@tonic-gate groupentrylist gelprev;
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate key = tablekey(name);
2497c478bd9Sstevel@tonic-gate if (grouptable[key] == NULL) {
2507c478bd9Sstevel@tonic-gate grouptable[key] = newentry(name, group);
2517c478bd9Sstevel@tonic-gate } else {
2527c478bd9Sstevel@tonic-gate gel = grouptable[key];
2537c478bd9Sstevel@tonic-gate while (gel && strcmp(gel->name, name)) {
2547c478bd9Sstevel@tonic-gate gelprev = gel;
2557c478bd9Sstevel@tonic-gate gel = gel->next;
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate if (gel) {
2587c478bd9Sstevel@tonic-gate appendgroup(gel, group);
2597c478bd9Sstevel@tonic-gate } else {
2607c478bd9Sstevel@tonic-gate gelprev->next = newentry(name, group);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate * Load up a hash table with the info in /etc/netgroup
2677c478bd9Sstevel@tonic-gate */
2687c478bd9Sstevel@tonic-gate static void
loadtable(FILE * nf)2697c478bd9Sstevel@tonic-gate loadtable(FILE *nf)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate char buf[MAXGROUPLEN];
2727c478bd9Sstevel@tonic-gate char *p;
2737c478bd9Sstevel@tonic-gate char *group;
2747c478bd9Sstevel@tonic-gate char *line;
2757c478bd9Sstevel@tonic-gate
276*23a1cceaSRoger A. Faulkner while (getaline(buf, MAXGROUPLEN, nf)) {
2777c478bd9Sstevel@tonic-gate for (p = buf; *p && isspace((int)*p); p++)
2787c478bd9Sstevel@tonic-gate ; /* skip leading blanks */
2797c478bd9Sstevel@tonic-gate for (; *p && *p != '#' && *p != ' ' && *p != '\t'; p++)
2807c478bd9Sstevel@tonic-gate ;
2817c478bd9Sstevel@tonic-gate if (*p == EOS || *p == '#')
2827c478bd9Sstevel@tonic-gate continue;
2837c478bd9Sstevel@tonic-gate *p++ = EOS;
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate while (*p == ' ' || *p == '\t') {
2867c478bd9Sstevel@tonic-gate p++;
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate if (*p == EOS || *p == '#')
2897c478bd9Sstevel@tonic-gate continue;
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate STRCPY(group, buf);
2927c478bd9Sstevel@tonic-gate STRCPY(line, p);
2937c478bd9Sstevel@tonic-gate store(ngtable, group, line);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate }
296