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