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