HAMMER commit - MFC ref count related panics when a HAMMER mount fails, etc.
[dragonfly.git] / libexec / mknetid / mknetid.c
blobd37e659e1983b47c516c902337ce115dfb011e5c
1 /*
2 * Copyright (c) 1995, 1996
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * netid map generator program
34 * Written by Bill Paul <wpaul@ctr.columbia.edu>
35 * Center for Telecommunications Research
36 * Columbia University, New York City
38 * $FreeBSD: src/libexec/mknetid/mknetid.c,v 1.11.2.1 2002/11/27 11:03:02 maxim Exp $
39 * $DragonFly: src/libexec/mknetid/mknetid.c,v 1.3 2003/11/14 03:54:30 dillon Exp $
42 #include <sys/types.h>
44 #include <rpc/rpc.h>
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
48 #include <err.h>
49 #include <grp.h>
50 #include <netdb.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
57 #include "hash.h"
59 #define LINSIZ 1024
60 #define OPSYS "unix"
62 /* Default location of group file. */
63 char *groupfile = _PATH_GROUP;
64 /* Default location of master.passwd file. */
65 char *passfile = _PATH_PASSWD;
66 /* Default location of hosts file. */
67 char *hostsfile = _PATH_HOSTS;
68 /* Default location of netid file */
69 char *netidfile = "/etc/netid";
72 * Stored hash table of 'reverse' group member database
73 * which we will construct.
75 struct member_entry *mtable[TABLESIZE];
78 * Dupe table: used to keep track of entries so we don't
79 * print the same thing twice.
81 struct member_entry *dtable[TABLESIZE];
83 extern struct group *_getgrent ( void );
84 extern int _setgrent ( void );
85 extern void _endgrent ( void );
87 static void
88 usage()
90 fprintf (stderr, "%s\n%s\n",
91 "usage: mknetid [-q] [-g group_file] [-p passwd_file] [-h hosts_file]",
92 " [-n netid_file] [-d domain]");
93 exit(1);
96 extern FILE *_gr_fp;
98 int
99 main(argc, argv)
100 int argc;
101 char *argv[];
103 FILE *gfp, *pfp, *hfp, *nfp;
104 char readbuf[LINSIZ];
105 char writebuf[LINSIZ];
106 struct group *gr;
107 struct grouplist *glist;
108 char *domain;
109 int ch;
110 gid_t i;
111 char *ptr, *pidptr, *gidptr, *hptr;
112 int quiet = 0;
114 domain = NULL;
115 while ((ch = getopt(argc, argv, "g:p:h:n:d:q")) != -1) {
116 switch(ch) {
117 case 'g':
118 groupfile = optarg;
119 break;
120 case 'p':
121 passfile = optarg;
122 break;
123 case 'h':
124 hostsfile = optarg;
125 break;
126 case 'n':
127 netidfile = optarg;
128 break;
129 case 'd':
130 domain = optarg;
131 break;
132 case 'q':
133 quiet++;
134 break;
135 default:
136 usage();
137 break;
141 if (domain == NULL) {
142 if (yp_get_default_domain(&domain))
143 errx(1, "no domain name specified and default \
144 domain not set");
147 if ((gfp = fopen(groupfile, "r")) == NULL) {
148 err(1, "%s", groupfile);
151 if ((pfp = fopen(passfile, "r")) == NULL) {
152 err(1, "%s", passfile);
155 if ((hfp = fopen(hostsfile, "r")) == NULL) {
156 err(1, "%s", hostsfile);
159 if ((nfp = fopen(netidfile, "r")) == NULL) {
160 /* netid is optional -- just continue */
161 nfp = NULL;
164 _gr_fp = gfp;
166 /* Load all the group membership info into a hash table. */
168 _setgrent();
169 while((gr = _getgrent()) != NULL) {
170 while(*gr->gr_mem) {
171 mstore(mtable, *gr->gr_mem, gr->gr_gid, 0);
172 gr->gr_mem++;
176 fclose(gfp);
177 _endgrent();
180 * Now parse the passwd database, spewing out the extra
181 * group information we just stored if necessary.
183 while(fgets(readbuf, LINSIZ, pfp)) {
184 /* Ignore comments: ^[ \t]*# */
185 for (ptr = readbuf; *ptr != '\0'; ptr++)
186 if (*ptr != ' ' && *ptr != '\t')
187 break;
188 if (*ptr == '#' || *ptr == '\0')
189 continue;
190 if ((ptr = strchr(readbuf, ':')) == NULL) {
191 warnx("bad passwd file entry: %s", readbuf);
192 continue;
194 *ptr = '\0';
195 ptr++;
196 if ((ptr = strchr(ptr, ':')) == NULL) {
197 warnx("bad passwd file entry: %s", readbuf);
198 continue;
200 *ptr = '\0';
201 ptr++;
202 pidptr = ptr;
203 if ((ptr = strchr(ptr, ':')) == NULL) {
204 warnx("bad passwd file entry: %s", readbuf);
205 continue;
207 *ptr = '\0';
208 ptr++;
209 gidptr = ptr;
210 if ((ptr = strchr(ptr, ':')) == NULL) {
211 warnx("bad passwd file entry: %s", readbuf);
212 continue;
214 *ptr = '\0';
215 i = atol(gidptr);
217 snprintf(writebuf, sizeof(writebuf), "%s.%s@%s", OPSYS,
218 pidptr, domain);
220 if (lookup(dtable, writebuf)) {
221 if (!quiet)
222 warnx("duplicate netid '%s.%s@%s' -- skipping",
223 OPSYS, pidptr, domain);
224 continue;
225 } else {
226 mstore(dtable, writebuf, 0, 1);
228 printf("%s.%s@%s %s:%s", OPSYS, pidptr, domain, pidptr, gidptr);
229 if ((glist = lookup(mtable, (char *)&readbuf)) != NULL) {
230 while(glist) {
231 if (glist->groupid != i)
232 printf(",%lu", (u_long)glist->groupid);
233 glist = glist->next;
236 printf ("\n");
239 fclose(pfp);
242 * Now parse the hosts database (this part sucks).
245 while ((ptr = fgets(readbuf, LINSIZ, hfp))) {
246 if (*ptr == '#')
247 continue;
248 if (!(hptr = strpbrk(ptr, "#\n")))
249 continue;
250 *hptr = '\0';
251 if (!(hptr = strpbrk(ptr, " \t")))
252 continue;
253 *hptr++ = '\0';
254 ptr = hptr;
255 while (*ptr == ' ' || *ptr == '\t')
256 ptr++;
257 if (!(hptr = strpbrk(ptr, " \t")))
258 continue;
259 *hptr++ = '\0';
260 snprintf(writebuf, sizeof(writebuf), "%s.%s@%s", OPSYS,
261 ptr, domain);
262 if (lookup(dtable, (char *)&writebuf)) {
263 if (!quiet)
264 warnx("duplicate netid '%s' -- skipping",
265 writebuf);
266 continue;
267 } else {
268 mstore(dtable, (char *)&writebuf, 0, 1);
270 printf ("%s.%s@%s 0:%s\n", OPSYS, ptr, domain, ptr);
273 fclose(hfp);
276 * Lastly, copy out any extra information in the netid
277 * file. If it's not open, just ignore it: it's optional anyway.
280 if (nfp != NULL) {
281 while(fgets(readbuf, LINSIZ, nfp)) {
282 if (readbuf[0] == '#')
283 continue;
284 if ((ptr = strpbrk((char*)&readbuf, " \t")) == NULL) {
285 warnx("bad netid entry: '%s'", readbuf);
286 continue;
289 writebuf[0] = *ptr;
290 *ptr = '\0';
291 if (lookup(dtable, (char *)&readbuf)) {
292 if (!quiet)
293 warnx("duplicate netid '%s' -- skipping",
294 readbuf);
295 continue;
296 } else {
297 mstore(dtable, (char *)&readbuf, 0, 1);
299 *ptr = writebuf[0];
300 printf("%s",readbuf);
302 fclose(nfp);
305 exit(0);