1 /* $OpenBSD: revnetgroup.c,v 1.9 2009/10/27 23:59:58 deraadt 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 /* Default location of netgroup file. */ 51 char *netgroup = "/etc/netgroup"; 52 53 /* Stored hash table version of 'forward' netgroup database. */ 54 struct group_entry *gtable[TABLESIZE]; 55 56 /* 57 * Stored hash table of 'reverse' netgroup member database 58 * which we will construct. 59 */ 60 struct member_entry *mtable[TABLESIZE]; 61 62 static void 63 usage(void) 64 { 65 fprintf (stderr,"usage: revnetgroup -h | -u [-f netgroup_file]\n"); 66 exit(1); 67 } 68 69 int 70 main(int argc, char *argv[]) 71 { 72 FILE *fp; 73 char *readbuf; 74 struct group_entry *gcur; 75 struct member_entry *mcur; 76 char *host, *user, *domain; 77 extern char *optarg; 78 int ch; 79 char *key = NULL, *data = NULL; 80 int hosts = -1, i; 81 82 if (argc < 2) 83 usage(); 84 85 while ((ch = getopt(argc, argv, "uhf:")) != -1) { 86 switch (ch) { 87 case 'u': 88 if (hosts != -1) { 89 warnx("please use only one of -h or -u"); 90 usage(); 91 } 92 hosts = 0; 93 break; 94 case 'h': 95 if (hosts != -1) { 96 warnx("please use only one of -u or -h"); 97 usage(); 98 } 99 hosts = 1; 100 break; 101 case 'f': 102 netgroup = optarg; 103 break; 104 default: 105 usage(); 106 break; 107 } 108 } 109 110 if (hosts == -1) 111 usage(); 112 113 if (strcmp(netgroup, "-")) { 114 if ((fp = fopen(netgroup, "r")) == NULL) { 115 err(1, "%s", netgroup); 116 } 117 } else { 118 fp = stdin; 119 } 120 121 /* Stuff all the netgroup names and members into a hash table. */ 122 while ((readbuf = fparseln(fp, NULL, NULL, NULL, 0)) != NULL) { 123 data = strpbrk(readbuf, " \t"); 124 if (data == NULL) { 125 free(readbuf); 126 continue; 127 } 128 *data = '\0'; 129 ++data; 130 key = readbuf; 131 ngstore(gtable, key, data); 132 free(readbuf); 133 } 134 135 fclose(fp); 136 137 /* 138 * Find all members of each netgroup and keep track of which 139 * group they belong to. 140 */ 141 for (i = 0; i < TABLESIZE; i++) { 142 gcur = gtable[i]; 143 while (gcur) { 144 __setnetgrent(gcur->key); 145 while (__getnetgrent(&host, &user, &domain) != NULL) { 146 if (hosts) { 147 if (!(host && !strcmp(host,"-"))) { 148 mstore(mtable, 149 host ? host : "*", 150 gcur->key, 151 domain ? domain : "*"); 152 } 153 } else { 154 if (!(user && !strcmp(user,"-"))) { 155 mstore(mtable, 156 user ? user : "*", 157 gcur->key, 158 domain ? domain : "*"); 159 } 160 } 161 } 162 gcur = gcur->next; 163 } 164 } 165 166 /* Release resources used by the netgroup parser code. */ 167 __endnetgrent(); 168 169 /* Spew out the results. */ 170 for (i = 0; i < TABLESIZE; i++) { 171 mcur = mtable[i]; 172 while (mcur) { 173 struct grouplist *tmp; 174 printf ("%s.%s\t", mcur->key, mcur->domain); 175 tmp = mcur->groups; 176 while (tmp) { 177 printf ("%s", tmp->groupname); 178 tmp = tmp->next; 179 if (tmp) 180 printf(","); 181 } 182 mcur = mcur->next; 183 printf ("\n"); 184 } 185 } 186 187 /* Let the OS free all our resources. */ 188 exit(0); 189 } 190