mknetid.c revision 1.10 1 1.10 lukem /* $NetBSD: mknetid.c,v 1.10 1999/07/25 09:01:04 lukem Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1996 Mats O Jansson <moj (at) stacken.kth.se>
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
16 1.1 thorpej * must display the following acknowledgement:
17 1.1 thorpej * This product includes software developed by Mats O Jansson
18 1.1 thorpej * 4. The name of the author may not be used to endorse or promote products
19 1.1 thorpej * derived from this software without specific prior written permission.
20 1.1 thorpej *
21 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 1.1 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.1 thorpej * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 1.1 thorpej * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 thorpej * SUCH DAMAGE.
32 1.1 thorpej */
33 1.1 thorpej
34 1.5 lukem #include <sys/cdefs.h>
35 1.5 lukem #ifndef lint
36 1.10 lukem __RCSID("$NetBSD: mknetid.c,v 1.10 1999/07/25 09:01:04 lukem Exp $");
37 1.5 lukem #endif
38 1.5 lukem
39 1.1 thorpej /*
40 1.1 thorpej * Originally written by Mats O Jansson <moj (at) stacken.kth.se>
41 1.1 thorpej * Simplified a bit by Jason R. Thorpe <thorpej (at) NetBSD.ORG>
42 1.1 thorpej */
43 1.1 thorpej
44 1.1 thorpej #include <sys/param.h>
45 1.1 thorpej #include <sys/queue.h>
46 1.1 thorpej #include <ctype.h>
47 1.1 thorpej #include <err.h>
48 1.1 thorpej #include <grp.h>
49 1.1 thorpej #include <limits.h>
50 1.1 thorpej #include <netdb.h>
51 1.1 thorpej #include <pwd.h>
52 1.1 thorpej #include <stdio.h>
53 1.1 thorpej #include <string.h>
54 1.1 thorpej #include <stdlib.h>
55 1.1 thorpej #include <unistd.h>
56 1.8 lukem #include <util.h>
57 1.1 thorpej
58 1.2 thorpej #include <rpcsvc/ypclnt.h>
59 1.2 thorpej
60 1.1 thorpej #include "protos.h"
61 1.1 thorpej
62 1.1 thorpej struct user {
63 1.1 thorpej char *usr_name; /* user name */
64 1.1 thorpej int usr_uid; /* user uid */
65 1.1 thorpej int usr_gid; /* user gid */
66 1.1 thorpej int gid_count; /* number of gids */
67 1.1 thorpej int gid[NGROUPS]; /* additional gids */
68 1.1 thorpej TAILQ_ENTRY(user) read; /* links in read order */
69 1.1 thorpej TAILQ_ENTRY(user) hash; /* links in hash order */
70 1.1 thorpej };
71 1.1 thorpej
72 1.1 thorpej #define HASHMAX 55
73 1.1 thorpej
74 1.6 lukem void add_group __P((const char *, const char *));
75 1.6 lukem void add_user __P((const char *, const char *, const char *));
76 1.6 lukem int hashidx __P((char));
77 1.6 lukem int isgsep __P((char));
78 1.2 thorpej int main __P((int, char *[]));
79 1.6 lukem void print_hosts __P((const char *, const char *));
80 1.6 lukem void print_netid __P((const char *));
81 1.6 lukem void print_passwd_group __P((int, const char *));
82 1.6 lukem void read_group __P((const char *));
83 1.6 lukem void read_passwd __P((const char *));
84 1.1 thorpej void usage __P((void));
85 1.1 thorpej
86 1.1 thorpej TAILQ_HEAD(user_list, user);
87 1.1 thorpej struct user_list root;
88 1.1 thorpej struct user_list hroot[HASHMAX];
89 1.1 thorpej
90 1.5 lukem extern char *__progname; /* from crt0.o */
91 1.1 thorpej
92 1.1 thorpej int
93 1.1 thorpej main(argc, argv)
94 1.1 thorpej int argc;
95 1.2 thorpej char *argv[];
96 1.1 thorpej {
97 1.6 lukem char *HostFile = _PATH_HOSTS;
98 1.6 lukem char *PasswdFile = _PATH_PASSWD;
99 1.6 lukem char *GroupFile = _PATH_GROUP;
100 1.6 lukem char *NetidFile = "/etc/netid";
101 1.6 lukem
102 1.1 thorpej int qflag, ch;
103 1.1 thorpej char *domain;
104 1.1 thorpej
105 1.1 thorpej TAILQ_INIT(&root);
106 1.1 thorpej for (ch = 0; ch < HASHMAX; ch++)
107 1.1 thorpej TAILQ_INIT((&hroot[ch]));
108 1.1 thorpej
109 1.1 thorpej qflag = 0;
110 1.1 thorpej domain = NULL;
111 1.1 thorpej
112 1.1 thorpej while ((ch = getopt(argc, argv, "d:g:h:m:p:q")) != -1) {
113 1.1 thorpej switch (ch) {
114 1.1 thorpej case 'd':
115 1.1 thorpej domain = optarg;
116 1.1 thorpej break;
117 1.1 thorpej
118 1.1 thorpej case 'g':
119 1.1 thorpej GroupFile = optarg;
120 1.1 thorpej break;
121 1.1 thorpej
122 1.1 thorpej case 'h':
123 1.1 thorpej HostFile = optarg;
124 1.1 thorpej break;
125 1.1 thorpej
126 1.1 thorpej case 'm':
127 1.1 thorpej NetidFile = optarg;
128 1.1 thorpej break;
129 1.1 thorpej
130 1.1 thorpej case 'p':
131 1.1 thorpej PasswdFile = optarg;
132 1.1 thorpej break;
133 1.1 thorpej
134 1.1 thorpej case 'q':
135 1.1 thorpej qflag++;
136 1.1 thorpej break;
137 1.1 thorpej
138 1.1 thorpej default:
139 1.1 thorpej usage();
140 1.1 thorpej }
141 1.1 thorpej }
142 1.6 lukem if (argc != optind)
143 1.1 thorpej usage();
144 1.1 thorpej
145 1.1 thorpej if (domain == NULL)
146 1.1 thorpej if (yp_get_default_domain(&domain))
147 1.3 lukem errx(1, "Can't get YP domain name");
148 1.1 thorpej
149 1.6 lukem read_passwd(PasswdFile);
150 1.6 lukem read_group(GroupFile);
151 1.1 thorpej
152 1.1 thorpej print_passwd_group(qflag, domain);
153 1.6 lukem print_hosts(HostFile, domain);
154 1.6 lukem print_netid(NetidFile);
155 1.1 thorpej
156 1.1 thorpej exit (0);
157 1.1 thorpej }
158 1.1 thorpej
159 1.1 thorpej int
160 1.1 thorpej hashidx(key)
161 1.1 thorpej char key;
162 1.1 thorpej {
163 1.1 thorpej if (key < 'A')
164 1.1 thorpej return(0);
165 1.1 thorpej
166 1.1 thorpej if (key <= 'Z')
167 1.1 thorpej return(1 + key - 'A');
168 1.1 thorpej
169 1.1 thorpej if (key < 'a')
170 1.1 thorpej return(27);
171 1.1 thorpej
172 1.1 thorpej if (key <= 'z')
173 1.1 thorpej return(28 + key - 'a');
174 1.1 thorpej
175 1.1 thorpej return(54);
176 1.1 thorpej }
177 1.1 thorpej
178 1.1 thorpej void
179 1.1 thorpej add_user(username, uid, gid)
180 1.6 lukem const char *username, *uid, *gid;
181 1.1 thorpej {
182 1.1 thorpej struct user *u;
183 1.1 thorpej int idx;
184 1.1 thorpej
185 1.1 thorpej idx = hashidx(username[0]);
186 1.1 thorpej
187 1.1 thorpej u = (struct user *)malloc(sizeof(struct user));
188 1.1 thorpej if (u == NULL)
189 1.1 thorpej err(1, "can't allocate user");
190 1.1 thorpej memset(u, 0, sizeof(struct user));
191 1.1 thorpej
192 1.6 lukem u->usr_name = strdup(username);
193 1.1 thorpej if (u->usr_name == NULL)
194 1.1 thorpej err(1, "can't allocate user name");
195 1.1 thorpej
196 1.1 thorpej u->usr_uid = atoi(uid);
197 1.1 thorpej u->usr_gid = atoi(gid);
198 1.1 thorpej u->gid_count = -1;
199 1.1 thorpej
200 1.1 thorpej TAILQ_INSERT_TAIL(&root, u, read);
201 1.1 thorpej TAILQ_INSERT_TAIL((&hroot[idx]), u, hash);
202 1.1 thorpej }
203 1.1 thorpej
204 1.1 thorpej void
205 1.1 thorpej add_group(username, gid)
206 1.6 lukem const char *username, *gid;
207 1.1 thorpej {
208 1.1 thorpej struct user *u;
209 1.1 thorpej int g, idx;
210 1.1 thorpej
211 1.1 thorpej g = atoi(gid);
212 1.1 thorpej idx = hashidx(username[0]);
213 1.1 thorpej
214 1.1 thorpej for (u = hroot[idx].tqh_first;
215 1.1 thorpej u != NULL; u = u->hash.tqe_next) {
216 1.1 thorpej if (strcmp(username, u->usr_name) == 0) {
217 1.1 thorpej if (g != u->usr_gid) {
218 1.1 thorpej u->gid_count++;
219 1.1 thorpej if (u->gid_count < NGROUPS)
220 1.1 thorpej u->gid[u->gid_count] = g;
221 1.1 thorpej }
222 1.1 thorpej return;
223 1.1 thorpej }
224 1.1 thorpej }
225 1.1 thorpej }
226 1.1 thorpej
227 1.1 thorpej void
228 1.6 lukem read_passwd(fname)
229 1.6 lukem const char *fname;
230 1.1 thorpej {
231 1.6 lukem FILE *pfile;
232 1.9 kleink size_t line_no;
233 1.9 kleink int colon;
234 1.7 thorpej size_t len;
235 1.10 lukem char *line, *p, *k, *u, *g;
236 1.6 lukem
237 1.6 lukem if ((pfile = fopen(fname, "r")) == NULL)
238 1.6 lukem err(1, "%s", fname);
239 1.6 lukem
240 1.6 lukem line_no = 0;
241 1.8 lukem for (;
242 1.10 lukem (line = fparseln(pfile, &len, &line_no, NULL, FPARSELN_UNESCALL));
243 1.10 lukem free(line)) {
244 1.6 lukem if (len == 0) {
245 1.9 kleink warnx("%s line %lu: empty line", fname,
246 1.9 kleink (unsigned long)line_no);
247 1.1 thorpej continue;
248 1.6 lukem }
249 1.1 thorpej
250 1.10 lukem p = line;
251 1.1 thorpej for (k = p, colon = 0; *k != '\0'; k++)
252 1.1 thorpej if (*k == ':')
253 1.1 thorpej colon++;
254 1.1 thorpej
255 1.6 lukem if (colon != 6) {
256 1.9 kleink warnx("%s line %lu: incorrect number of fields",
257 1.9 kleink fname, (unsigned long)line_no);
258 1.6 lukem continue;
259 1.6 lukem }
260 1.1 thorpej
261 1.6 lukem k = p;
262 1.6 lukem p = strchr(p, ':');
263 1.6 lukem *p++ = '\0';
264 1.1 thorpej
265 1.6 lukem /* If it's a YP entry, skip it. */
266 1.6 lukem if (*k == '+' || *k == '-')
267 1.1 thorpej continue;
268 1.1 thorpej
269 1.1 thorpej /* terminate password */
270 1.6 lukem p = strchr(p, ':');
271 1.1 thorpej *p++ = '\0';
272 1.1 thorpej
273 1.1 thorpej /* terminate uid */
274 1.6 lukem u = p;
275 1.6 lukem p = strchr(p, ':');
276 1.1 thorpej *p++ = '\0';
277 1.1 thorpej
278 1.1 thorpej /* terminate gid */
279 1.6 lukem g = p;
280 1.6 lukem p = strchr(p, ':');
281 1.1 thorpej *p++ = '\0';
282 1.1 thorpej
283 1.1 thorpej add_user(k, u, g);
284 1.1 thorpej }
285 1.6 lukem (void)fclose(pfile);
286 1.1 thorpej }
287 1.1 thorpej
288 1.1 thorpej int
289 1.1 thorpej isgsep(ch)
290 1.6 lukem char ch;
291 1.1 thorpej {
292 1.1 thorpej
293 1.1 thorpej switch (ch) {
294 1.1 thorpej case ',':
295 1.1 thorpej case ' ':
296 1.1 thorpej case '\t':
297 1.1 thorpej case '\0':
298 1.1 thorpej return (1);
299 1.1 thorpej }
300 1.1 thorpej
301 1.1 thorpej return (0);
302 1.1 thorpej }
303 1.1 thorpej
304 1.1 thorpej void
305 1.6 lukem read_group(fname)
306 1.6 lukem const char *fname;
307 1.1 thorpej {
308 1.6 lukem FILE *gfile;
309 1.9 kleink size_t line_no;
310 1.9 kleink int colon;
311 1.7 thorpej size_t len;
312 1.10 lukem char *line, *p, *k, *u, *g;
313 1.6 lukem
314 1.6 lukem if ((gfile = fopen(fname, "r")) == NULL)
315 1.6 lukem err(1, "%s", fname);
316 1.6 lukem
317 1.6 lukem line_no = 0;
318 1.8 lukem for (;
319 1.10 lukem (line = fparseln(gfile, &len, &line_no, NULL, FPARSELN_UNESCALL));
320 1.10 lukem free(line)) {
321 1.6 lukem if (len == 0) {
322 1.9 kleink warnx("%s line %lu: empty line", fname,
323 1.9 kleink (unsigned long)line_no);
324 1.1 thorpej continue;
325 1.6 lukem }
326 1.1 thorpej
327 1.10 lukem p = line;
328 1.1 thorpej for (k = p, colon = 0; *k != '\0'; k++)
329 1.1 thorpej if (*k == ':')
330 1.1 thorpej colon++;
331 1.1 thorpej
332 1.6 lukem if (colon != 3) {
333 1.9 kleink warnx("%s line %lu: incorrect number of fields",
334 1.9 kleink fname, (unsigned long)line_no);
335 1.6 lukem continue;
336 1.6 lukem }
337 1.1 thorpej
338 1.6 lukem /* terminate key */
339 1.6 lukem k = p;
340 1.6 lukem p = strchr(p, ':');
341 1.6 lukem *p++ = '\0';
342 1.1 thorpej
343 1.6 lukem if (*k == '+' || *k == '-')
344 1.1 thorpej continue;
345 1.1 thorpej
346 1.1 thorpej /* terminate password */
347 1.6 lukem p = strchr(p, ':');
348 1.1 thorpej *p++ = '\0';
349 1.1 thorpej
350 1.1 thorpej /* terminate gid */
351 1.6 lukem g = p;
352 1.6 lukem p = strchr(p, ':');
353 1.1 thorpej *p++ = '\0';
354 1.1 thorpej
355 1.1 thorpej /* get the group list */
356 1.1 thorpej for (u = p; *u != '\0'; u = p) {
357 1.1 thorpej /* find separator */
358 1.6 lukem for (; isgsep(*p) == 0; p++)
359 1.6 lukem ;
360 1.1 thorpej
361 1.1 thorpej if (*p != '\0') {
362 1.1 thorpej *p = '\0';
363 1.1 thorpej if (u != p)
364 1.1 thorpej add_group(u, g);
365 1.1 thorpej p++;
366 1.1 thorpej } else if (u != p)
367 1.1 thorpej add_group(u, g);
368 1.1 thorpej }
369 1.1 thorpej }
370 1.6 lukem (void)fclose(gfile);
371 1.1 thorpej }
372 1.1 thorpej
373 1.1 thorpej void
374 1.1 thorpej print_passwd_group(qflag, domain)
375 1.1 thorpej int qflag;
376 1.6 lukem const char *domain;
377 1.1 thorpej {
378 1.1 thorpej struct user *u, *p;
379 1.1 thorpej int i;
380 1.1 thorpej
381 1.1 thorpej for (u = root.tqh_first; u != NULL; u = u->read.tqe_next) {
382 1.1 thorpej for (p = root.tqh_first; p->usr_uid != u->usr_uid;
383 1.1 thorpej p = p->read.tqe_next)
384 1.1 thorpej /* empty */ ;
385 1.1 thorpej if (p != u) {
386 1.1 thorpej if (!qflag) {
387 1.1 thorpej warnx("unix.%d@%s %s", u->usr_uid, domain,
388 1.1 thorpej "multiply defined, ignoring duplicate");
389 1.1 thorpej }
390 1.1 thorpej } else {
391 1.1 thorpej printf("unix.%d@%s %d:%d", u->usr_uid, domain,
392 1.1 thorpej u->usr_uid, u->usr_gid);
393 1.1 thorpej if (u->gid_count >= 0)
394 1.1 thorpej for (i = 0; i <= u->gid_count; i++)
395 1.1 thorpej printf(",%d", u->gid[i]);
396 1.1 thorpej printf("\n");
397 1.1 thorpej }
398 1.1 thorpej }
399 1.1 thorpej }
400 1.1 thorpej
401 1.1 thorpej void
402 1.6 lukem print_hosts(fname, domain)
403 1.6 lukem const char *fname, *domain;
404 1.1 thorpej {
405 1.6 lukem FILE *hfile;
406 1.7 thorpej size_t len;
407 1.10 lukem char *line, *p, *k, *u;
408 1.6 lukem
409 1.6 lukem if ((hfile = fopen(fname, "r")) == NULL)
410 1.6 lukem err(1, "%s", fname);
411 1.1 thorpej
412 1.8 lukem for (;
413 1.10 lukem (line = fparseln(hfile, &len, NULL, NULL, FPARSELN_UNESCALL));
414 1.10 lukem free(line)) {
415 1.8 lukem if (len == 0)
416 1.1 thorpej continue;
417 1.1 thorpej
418 1.10 lukem p = line;
419 1.1 thorpej /* Find the key, replace trailing whitespace will <NUL> */
420 1.6 lukem for (k = p; *p && isspace(*p) == 0; p++)
421 1.6 lukem ;
422 1.6 lukem while (*p && isspace(*p))
423 1.1 thorpej *p++ = '\0';
424 1.1 thorpej
425 1.1 thorpej /* Get first hostname. */
426 1.6 lukem for (u = p; *p && !isspace(*p); p++)
427 1.6 lukem ;
428 1.6 lukem *p = '\0';
429 1.1 thorpej
430 1.1 thorpej printf("unix.%s@%s 0:%s\n", u, domain, u);
431 1.1 thorpej }
432 1.6 lukem (void) fclose(hfile);
433 1.1 thorpej }
434 1.1 thorpej
435 1.1 thorpej void
436 1.6 lukem print_netid(fname)
437 1.6 lukem const char *fname;
438 1.1 thorpej {
439 1.6 lukem FILE *mfile;
440 1.7 thorpej size_t len;
441 1.10 lukem char *line, *p, *k, *u;
442 1.6 lukem
443 1.6 lukem mfile = fopen(fname, "r");
444 1.6 lukem if (mfile == NULL)
445 1.6 lukem return;
446 1.1 thorpej
447 1.8 lukem for (;
448 1.10 lukem (line = fparseln(mfile, &len, NULL, NULL, FPARSELN_UNESCALL));
449 1.10 lukem free(line)) {
450 1.8 lukem if (len == 0)
451 1.1 thorpej continue;
452 1.1 thorpej
453 1.10 lukem p = line;
454 1.1 thorpej /* Find the key, replace trailing whitespace will <NUL> */
455 1.6 lukem for (k = p; *p && !isspace(*p); p++)
456 1.6 lukem ;
457 1.6 lukem while (*p && isspace(*p))
458 1.1 thorpej *p++ = '\0';
459 1.1 thorpej
460 1.1 thorpej /* Get netid entry. */
461 1.6 lukem for (u = p; *p && !isspace(*p); p++)
462 1.6 lukem ;
463 1.6 lukem *p = '\0';
464 1.1 thorpej
465 1.1 thorpej printf("%s %s\n", k, u);
466 1.1 thorpej }
467 1.1 thorpej }
468 1.1 thorpej
469 1.1 thorpej void
470 1.1 thorpej usage()
471 1.1 thorpej {
472 1.1 thorpej
473 1.2 thorpej fprintf(stderr, "usage: %s %s\n", __progname,
474 1.1 thorpej "[-d domain] [-q] [-p passwdfile] [-g groupfile]");
475 1.2 thorpej fprintf(stderr, " %s %s", __progname,
476 1.1 thorpej "[-g groupfile] [-h hostfile] [-m netidfile]");
477 1.2 thorpej exit(1);
478 1.1 thorpej }
479