xref: /freebsd/libexec/mknetid/mknetid.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1995, 1996
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  * netid 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 #include <sys/types.h>
42 
43 #include <rpc/rpc.h>
44 #include <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
46 
47 #include <err.h>
48 #include <grp.h>
49 #include <netdb.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 #include "hash.h"
57 
58 #ifndef lint
59 static const char rcsid[] =
60   "$FreeBSD$";
61 #endif /* not lint */
62 
63 #define LINSIZ 1024
64 #define OPSYS "unix"
65 
66 /* Default location of group file. */
67 char *groupfile = _PATH_GROUP;
68 /* Default location of master.passwd file. */
69 char *passfile = _PATH_PASSWD;
70 /* Default location of hosts file. */
71 char *hostsfile = _PATH_HOSTS;
72 /* Default location of netid file */
73 char *netidfile = "/etc/netid";
74 
75 /*
76  * Stored hash table of 'reverse' group member database
77  * which we will construct.
78  */
79 struct member_entry *mtable[TABLESIZE];
80 
81 /*
82  * Dupe table: used to keep track of entries so we don't
83  * print the same thing twice.
84  */
85 struct member_entry *dtable[TABLESIZE];
86 
87 extern struct group *_getgrent(void);
88 extern int _setgrent(void);
89 extern void _endgrent(void);
90 
91 static void
92 usage(void)
93 {
94 	fprintf (stderr, "%s\n%s\n",
95 	"usage: mknetid [-q] [-g group_file] [-p passwd_file] [-h hosts_file]",
96 	"               [-n netid_file] [-d domain]");
97 	exit(1);
98 }
99 
100 extern FILE *_gr_fp;
101 
102 int
103 main(int argc, char *argv[])
104 {
105 	FILE *gfp, *pfp, *hfp, *nfp;
106 	char readbuf[LINSIZ];
107 	char writebuf[LINSIZ];
108 	struct group *gr;
109 	struct grouplist *glist;
110 	char *domain;
111 	int ch;
112 	gid_t i;
113 	char *ptr, *pidptr, *gidptr, *hptr;
114 	int quiet = 0;
115 
116 	domain = NULL;
117 	while ((ch = getopt(argc, argv, "g:p:h:n:d:q")) != -1) {
118 		switch(ch) {
119 		case 'g':
120 			groupfile = optarg;
121 			break;
122 		case 'p':
123 			passfile = optarg;
124 			break;
125 		case 'h':
126 			hostsfile = optarg;
127 			break;
128 		case 'n':
129 			netidfile = optarg;
130 			break;
131 		case 'd':
132 			domain = optarg;
133 			break;
134 		case 'q':
135 			quiet++;
136 			break;
137 		default:
138 			usage();
139 			break;
140 		}
141 	}
142 
143 	if (domain == NULL) {
144 		if (yp_get_default_domain(&domain))
145 			errx(1, "no domain name specified and default \
146 domain not set");
147 	}
148 
149 	if ((gfp = fopen(groupfile, "r")) == NULL) {
150 		err(1, "%s", groupfile);
151 	}
152 
153 	if ((pfp = fopen(passfile, "r")) == NULL) {
154 		err(1, "%s", passfile);
155 	}
156 
157 	if ((hfp = fopen(hostsfile, "r")) == NULL) {
158 		err(1, "%s", hostsfile);
159 	}
160 
161 	if ((nfp = fopen(netidfile, "r")) == NULL) {
162 		/* netid is optional -- just continue */
163 		nfp = NULL;
164 	}
165 
166 	_gr_fp = gfp;
167 
168 	/* Load all the group membership info into a hash table. */
169 
170 	_setgrent();
171 	while((gr = _getgrent()) != NULL) {
172 		while(*gr->gr_mem) {
173 			mstore(mtable, *gr->gr_mem, gr->gr_gid, 0);
174 			gr->gr_mem++;
175 		}
176 	}
177 
178 	fclose(gfp);
179 	_endgrent();
180 
181 	/*
182 	 * Now parse the passwd database, spewing out the extra
183 	 * group information we just stored if necessary.
184 	 */
185 	while(fgets(readbuf, LINSIZ, pfp)) {
186 		/* Ignore comments: ^[ \t]*# */
187 		for (ptr = readbuf; *ptr != '\0'; ptr++)
188 			if (*ptr != ' ' && *ptr != '\t')
189 				break;
190 		if (*ptr == '#' || *ptr == '\0')
191 			continue;
192 		if ((ptr = strchr(readbuf, ':')) == NULL) {
193 			warnx("bad passwd file entry: %s", readbuf);
194 			continue;
195 		}
196 		*ptr = '\0';
197 		ptr++;
198 		if ((ptr = strchr(ptr, ':')) == NULL) {
199 			warnx("bad passwd file entry: %s", readbuf);
200 			continue;
201 		}
202 		*ptr = '\0';
203 		ptr++;
204 		pidptr = ptr;
205 		if ((ptr = strchr(ptr, ':')) == NULL) {
206 			warnx("bad passwd file entry: %s", readbuf);
207 			continue;
208 		}
209 		*ptr = '\0';
210 		ptr++;
211 		gidptr = ptr;
212 		if ((ptr = strchr(ptr, ':')) == NULL) {
213 			warnx("bad passwd file entry: %s", readbuf);
214 			continue;
215 		}
216 		*ptr = '\0';
217 		i = atol(gidptr);
218 
219 		snprintf(writebuf, sizeof(writebuf), "%s.%s@%s", OPSYS,
220 							pidptr, domain);
221 
222 		if (lookup(dtable, writebuf)) {
223 			if (!quiet)
224 				warnx("duplicate netid '%s.%s@%s' -- skipping",
225 						OPSYS, pidptr, domain);
226 			continue;
227 		} else {
228 			mstore(dtable, writebuf, 0, 1);
229 		}
230 		printf("%s.%s@%s %s:%s", OPSYS, pidptr, domain, pidptr, gidptr);
231 		if ((glist = lookup(mtable, (char *)&readbuf)) != NULL) {
232 			while(glist) {
233 				if (glist->groupid != i)
234 					printf(",%lu", (u_long)glist->groupid);
235 				glist = glist->next;
236 			}
237 		}
238 		printf ("\n");
239 	}
240 
241 	fclose(pfp);
242 
243 	/*
244 	 * Now parse the hosts database (this part sucks).
245 	 */
246 
247 	while ((ptr = fgets(readbuf, LINSIZ, hfp))) {
248 		if (*ptr == '#')
249 			continue;
250 		if (!(hptr = strpbrk(ptr, "#\n")))
251 			continue;
252 		*hptr = '\0';
253 		if (!(hptr = strpbrk(ptr, " \t")))
254 			continue;
255 		*hptr++ = '\0';
256 		ptr = hptr;
257 		while (*ptr == ' ' || *ptr == '\t')
258 			ptr++;
259 		if (!(hptr = strpbrk(ptr, " \t")))
260 			continue;
261 		*hptr++ = '\0';
262 		snprintf(writebuf, sizeof(writebuf), "%s.%s@%s", OPSYS,
263 								ptr, domain);
264 		if (lookup(dtable, (char *)&writebuf)) {
265 			if (!quiet)
266 				warnx("duplicate netid '%s' -- skipping",
267 								writebuf);
268 			continue;
269 		} else {
270 			mstore(dtable, (char *)&writebuf, 0, 1);
271 		}
272 		printf ("%s.%s@%s 0:%s\n", OPSYS, ptr, domain, ptr);
273 	}
274 
275 	fclose(hfp);
276 
277 	/*
278 	 * Lastly, copy out any extra information in the netid
279 	 * file. If it's not open, just ignore it: it's optional anyway.
280 	 */
281 
282 	if (nfp != NULL) {
283 		while(fgets(readbuf, LINSIZ, nfp)) {
284 			if (readbuf[0] == '#')
285 				continue;
286 			if ((ptr = strpbrk((char*)&readbuf, " \t")) == NULL) {
287 				warnx("bad netid entry: '%s'", readbuf);
288 				continue;
289 			}
290 
291 			writebuf[0] = *ptr;
292 			*ptr = '\0';
293 			if (lookup(dtable, (char *)&readbuf)) {
294 				if (!quiet)
295 					warnx("duplicate netid '%s' -- skipping",
296 								readbuf);
297 				continue;
298 			} else {
299 				mstore(dtable, (char *)&readbuf, 0, 1);
300 			}
301 			*ptr = writebuf[0];
302 			printf("%s",readbuf);
303 		}
304 		fclose(nfp);
305 	}
306 
307 	exit(0);
308 }
309