1 /* $OpenBSD: revnetgroup.c,v 1.7 2006/11/22 07:36:01 ray Exp $ */ 2 /* 3 * Copyright (c) 1995 4 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Bill Paul. 17 * 4. Neither the name of the author nor the names of any co-contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * reverse netgroup map generator program 34 * 35 * Written by Bill Paul <wpaul@ctr.columbia.edu> 36 * Center for Telecommunications Research 37 * Columbia University, New York City 38 * 39 * $FreeBSD: revnetgroup.c,v 1.7 1997/03/28 15:48:15 imp Exp $ 40 */ 41 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <util.h> 46 #include <errno.h> 47 #include <err.h> 48 #include "hash.h" 49 50 #ifndef lint 51 static const char rcsid[] = "$OpenBSD: revnetgroup.c,v 1.7 2006/11/22 07:36:01 ray Exp $"; 52 #endif 53 54 /* Default location of netgroup file. */ 55 char *netgroup = "/etc/netgroup"; 56 57 /* Stored hash table version of 'forward' netgroup database. */ 58 struct group_entry *gtable[TABLESIZE]; 59 60 /* 61 * Stored hash table of 'reverse' netgroup member database 62 * which we will construct. 63 */ 64 struct member_entry *mtable[TABLESIZE]; 65 66 static void 67 usage(void) 68 { 69 fprintf (stderr,"usage: revnetgroup -u|-h [-f netgroup file]\n"); 70 exit(1); 71 } 72 73 int 74 main(int argc, char *argv[]) 75 { 76 FILE *fp; 77 char *readbuf; 78 struct group_entry *gcur; 79 struct member_entry *mcur; 80 char *host, *user, *domain; 81 extern char *optarg; 82 int ch; 83 char *key = NULL, *data = NULL; 84 int hosts = -1, i; 85 86 if (argc < 2) 87 usage(); 88 89 while ((ch = getopt(argc, argv, "uhf:")) != -1) { 90 switch (ch) { 91 case 'u': 92 if (hosts != -1) { 93 warnx("please use only one of -u or -h"); 94 usage(); 95 } 96 hosts = 0; 97 break; 98 case 'h': 99 if (hosts != -1) { 100 warnx("please use only one of -u or -h"); 101 usage(); 102 } 103 hosts = 1; 104 break; 105 case 'f': 106 netgroup = optarg; 107 break; 108 default: 109 usage(); 110 break; 111 } 112 } 113 114 if (hosts == -1) 115 usage(); 116 117 if (strcmp(netgroup, "-")) { 118 if ((fp = fopen(netgroup, "r")) == NULL) { 119 err(1, "%s", netgroup); 120 } 121 } else { 122 fp = stdin; 123 } 124 125 /* Stuff all the netgroup names and members into a hash table. */ 126 while ((readbuf = fparseln(fp, NULL, NULL, NULL, 0)) != NULL) { 127 data = strpbrk(readbuf, " \t"); 128 if (data == NULL) { 129 free(readbuf); 130 continue; 131 } 132 *data = '\0'; 133 ++data; 134 key = readbuf; 135 ngstore(gtable, key, data); 136 free(readbuf); 137 } 138 139 fclose(fp); 140 141 /* 142 * Find all members of each netgroup and keep track of which 143 * group they belong to. 144 */ 145 for (i = 0; i < TABLESIZE; i++) { 146 gcur = gtable[i]; 147 while (gcur) { 148 __setnetgrent(gcur->key); 149 while (__getnetgrent(&host, &user, &domain) != NULL) { 150 if (hosts) { 151 if (!(host && !strcmp(host,"-"))) { 152 mstore(mtable, 153 host ? host : "*", 154 gcur->key, 155 domain ? domain : "*"); 156 } 157 } else { 158 if (!(user && !strcmp(user,"-"))) { 159 mstore(mtable, 160 user ? user : "*", 161 gcur->key, 162 domain ? domain : "*"); 163 } 164 } 165 } 166 gcur = gcur->next; 167 } 168 } 169 170 /* Release resources used by the netgroup parser code. */ 171 __endnetgrent(); 172 173 /* Spew out the results. */ 174 for (i = 0; i < TABLESIZE; i++) { 175 mcur = mtable[i]; 176 while (mcur) { 177 struct grouplist *tmp; 178 printf ("%s.%s\t", mcur->key, mcur->domain); 179 tmp = mcur->groups; 180 while (tmp) { 181 printf ("%s", tmp->groupname); 182 tmp = tmp->next; 183 if (tmp) 184 printf(","); 185 } 186 mcur = mcur->next; 187 printf ("\n"); 188 } 189 } 190 191 /* Let the OS free all our resources. */ 192 exit(0); 193 } 194