1 /* $NetBSD: revnetgroup.c,v 1.9 2001/02/19 23:22:51 cgd 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.9 2001/02/19 23:22:51 cgd 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 80 fprintf (stderr,"usage: %s -u|-h [-f netgroup file]\n", getprogname()); 81 exit(1); 82 } 83 84 int 85 main(argc, argv) 86 int argc; 87 char *argv[]; 88 { 89 struct group_entry *gcur; 90 struct member_entry *mcur; 91 FILE *fp; 92 char *line, *p, *host, *user, *domain; 93 int ch, i; 94 size_t len; 95 char *key; 96 int hosts = -1; 97 98 if (argc < 2) 99 usage(); 100 101 while ((ch = getopt(argc, argv, "uhf:")) != -1) { 102 switch(ch) { 103 case 'u': 104 if (hosts != -1) { 105 warnx("please use only one of -u or -h"); 106 usage(); 107 } 108 hosts = 0; 109 break; 110 case 'h': 111 if (hosts != -1) { 112 warnx("please use only one of -u or -h"); 113 usage(); 114 } 115 hosts = 1; 116 break; 117 case 'f': 118 netgroup = optarg; 119 break; 120 default: 121 usage(); 122 break; 123 } 124 } 125 126 if (hosts == -1) 127 usage(); 128 129 if (strcmp(netgroup, "-")) { 130 if ((fp = fopen(netgroup, "r")) == NULL) { 131 err(1, "%s", netgroup); 132 } 133 } else { 134 fp = stdin; 135 } 136 137 /* Stuff all the netgroup names and members into a hash table. */ 138 for (; 139 (line = fparseln(fp, &len, NULL, NULL, FPARSELN_UNESCALL)); 140 free(line)) { 141 if (len == 0) 142 continue; 143 p = line; 144 145 for (key = p; *p && isspace(*p) == 0; p++) 146 ; 147 while (*p && isspace(*p)) 148 *p++ = '\0'; 149 store(gtable, key, p); 150 } 151 152 fclose(fp); 153 154 /* 155 * Find all members of each netgroup and keep track of which 156 * group they belong to. 157 */ 158 for (i = 0; i < TABLESIZE; i++) { 159 gcur = gtable[i]; 160 while(gcur) { 161 rng_setnetgrent(gcur->key); 162 while(rng_getnetgrent(&host, &user, &domain) != NULL) { 163 if (hosts) { 164 if (!(host && !strcmp(host,"-"))) { 165 mstore(mtable, 166 host ? host : "*", 167 gcur->key, 168 domain ? domain : "*"); 169 } 170 } else { 171 if (!(user && !strcmp(user,"-"))) { 172 mstore(mtable, 173 user ? user : "*", 174 gcur->key, 175 domain ? domain : "*"); 176 } 177 } 178 } 179 gcur = gcur->next; 180 } 181 } 182 183 /* Release resources used by the netgroup parser code. */ 184 rng_endnetgrent(); 185 186 /* Spew out the results. */ 187 for (i = 0; i < TABLESIZE; i++) { 188 mcur = mtable[i]; 189 while(mcur) { 190 struct grouplist *tmp; 191 printf ("%s.%s\t", mcur->key, mcur->domain); 192 tmp = mcur->groups; 193 while(tmp) { 194 printf ("%s", tmp->groupname); 195 tmp = tmp->next; 196 if (tmp) 197 printf(","); 198 } 199 mcur = mcur->next; 200 printf ("\n"); 201 } 202 } 203 204 /* Let the OS free all our resources. */ 205 exit(0); 206 } 207