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