Home | History | Annotate | Line # | Download | only in mknetid
mknetid.c revision 1.2
      1 /*	$NetBSD: mknetid.c,v 1.2 1997/07/18 21:57:07 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Mats O Jansson <moj (at) stacken.kth.se>
      5  * 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 Mats O Jansson
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Originally written by Mats O Jansson <moj (at) stacken.kth.se>
     36  * Simplified a bit by Jason R. Thorpe <thorpej (at) NetBSD.ORG>
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/queue.h>
     41 #include <ctype.h>
     42 #include <err.h>
     43 #include <grp.h>
     44 #include <limits.h>
     45 #include <netdb.h>
     46 #include <pwd.h>
     47 #include <stdio.h>
     48 #include <string.h>
     49 #include <stdlib.h>
     50 #include <unistd.h>
     51 
     52 #include <rpcsvc/ypclnt.h>
     53 
     54 #include "protos.h"
     55 
     56 struct user {
     57 	char 	*usr_name;		/* user name */
     58 	int	usr_uid;		/* user uid */
     59 	int	usr_gid;		/* user gid */
     60 	int	gid_count;		/* number of gids */
     61 	int	gid[NGROUPS];		/* additional gids */
     62 	TAILQ_ENTRY(user) read;		/* links in read order */
     63 	TAILQ_ENTRY(user) hash;		/* links in hash order */
     64 };
     65 
     66 #ifdef HOSTS
     67 char *HostFile = HOSTS;
     68 #else
     69 char *HostFile = _PATH_HOSTS;
     70 #endif
     71 
     72 #ifdef PASSWD
     73 char *PasswdFile = PASSWD;
     74 #else
     75 char *PasswdFile = _PATH_PASSWD;
     76 #endif
     77 
     78 #ifdef GROUP
     79 char *GroupFile = GROUP;
     80 #else
     81 char *GroupFile = _PATH_GROUP;
     82 #endif
     83 
     84 #ifdef NETID
     85 char *NetidFile = NETID;
     86 #else
     87 char *NetidFile = "/etc/netid";
     88 #endif
     89 
     90 #define HASHMAX 55
     91 
     92 int	main __P((int, char *[]));
     93 void	print_passwd_group __P((int, char *));
     94 void	print_hosts __P((FILE *, char *, char *));
     95 void	print_netid __P((FILE *, char *));
     96 void	add_user __P((char *, char *, char *));
     97 void	add_group __P((char *, char *));
     98 void	read_passwd __P((FILE *, char *));
     99 void	read_group __P((FILE *, char *));
    100 void	usage __P((void));
    101 int	hashidx __P((char));
    102 int	isgsep __P((char));
    103 
    104 TAILQ_HEAD(user_list, user);
    105 struct user_list root;
    106 struct user_list hroot[HASHMAX];
    107 
    108 extern	char *__progname;		/* from crt0.s */
    109 
    110 int
    111 main(argc, argv)
    112 	int argc;
    113 	char *argv[];
    114 {
    115 	int qflag, ch;
    116 	char *domain;
    117 	FILE *pfile, *gfile, *hfile, *mfile;
    118 
    119 	TAILQ_INIT(&root);
    120 	for (ch = 0; ch < HASHMAX; ch++)
    121 		TAILQ_INIT((&hroot[ch]));
    122 
    123 	qflag = 0;
    124 	domain = NULL;
    125 
    126 	while ((ch = getopt(argc, argv, "d:g:h:m:p:q")) != -1) {
    127 		switch (ch) {
    128 		case 'd':
    129 			domain = optarg;
    130 			break;
    131 
    132 		case 'g':
    133 			GroupFile = optarg;
    134 			break;
    135 
    136 		case 'h':
    137 			HostFile = optarg;
    138 			break;
    139 
    140 		case 'm':
    141 			NetidFile = optarg;
    142 			break;
    143 
    144 		case 'p':
    145 			PasswdFile = optarg;
    146 			break;
    147 
    148 		case 'q':
    149 			qflag++;
    150 			break;
    151 
    152 		default:
    153 			usage();
    154 		}
    155 	}
    156 	argc -= optind; argv += optind;
    157 
    158 	if (argc != 0)
    159 		usage();
    160 
    161 	if (domain == NULL)
    162 		if (yp_get_default_domain(&domain))
    163 			errx(1, "can't get YP domain name");
    164 
    165 	if ((pfile = fopen(PasswdFile, "r")) == NULL)
    166 		err(1, "%s", PasswdFile);
    167 
    168 	if ((gfile = fopen(GroupFile, "r")) == NULL)
    169 		err(1, "%s", GroupFile);
    170 
    171 	if ((hfile = fopen(HostFile, "r")) == NULL)
    172 		err(1, "%s", HostFile);
    173 
    174 	mfile = fopen(NetidFile, "r");
    175 
    176 	read_passwd(pfile, PasswdFile);
    177 	read_group(gfile, GroupFile);
    178 
    179 	print_passwd_group(qflag, domain);
    180 	print_hosts(hfile, HostFile, domain);
    181 
    182 	if (mfile != NULL)
    183 		print_netid(mfile, NetidFile);
    184 
    185 	exit (0);
    186 }
    187 
    188 int
    189 hashidx(key)
    190 	char key;
    191 {
    192 	if (key < 'A')
    193 		return(0);
    194 
    195 	if (key <= 'Z')
    196 		return(1 + key - 'A');
    197 
    198 	if (key < 'a')
    199 		return(27);
    200 
    201 	if (key <= 'z')
    202 		return(28 + key - 'a');
    203 
    204 	return(54);
    205 }
    206 
    207 void
    208 add_user(username, uid, gid)
    209 	char *username, *uid, *gid;
    210 {
    211 	struct user *u;
    212 	int idx;
    213 
    214 	idx = hashidx(username[0]);
    215 
    216 	u = (struct user *)malloc(sizeof(struct user));
    217 	if (u == NULL)
    218 		err(1, "can't allocate user");
    219 	memset(u, 0, sizeof(struct user));
    220 
    221 	u->usr_name = (char *)malloc(strlen(username) + 1);
    222 	if (u->usr_name == NULL)
    223 		err(1, "can't allocate user name");
    224 	strcpy(u->usr_name, username);
    225 
    226 	u->usr_uid = atoi(uid);
    227 	u->usr_gid = atoi(gid);
    228 	u->gid_count = -1;
    229 
    230 	TAILQ_INSERT_TAIL(&root, u, read);
    231 	TAILQ_INSERT_TAIL((&hroot[idx]), u, hash);
    232 }
    233 
    234 void
    235 add_group(username, gid)
    236 	char *username, *gid;
    237 {
    238 	struct user *u;
    239 	int g, idx;
    240 
    241 	g = atoi(gid);
    242 	idx = hashidx(username[0]);
    243 
    244 	for (u = hroot[idx].tqh_first;
    245 	    u != NULL; u = u->hash.tqe_next) {
    246 		if (strcmp(username, u->usr_name) == 0) {
    247 			if (g != u->usr_gid) {
    248 				u->gid_count++;
    249 				if (u->gid_count < NGROUPS)
    250 					u->gid[u->gid_count] = g;
    251 			}
    252 			return;
    253 		}
    254 	}
    255 }
    256 
    257 void
    258 read_passwd(pfile, fname)
    259 	FILE *pfile;
    260 	char *fname;
    261 {
    262 	char  line[_POSIX2_LINE_MAX];
    263 	int line_no = 0, len, colon;
    264 	char  *p, *k, *u, *g;
    265 
    266 	while (read_line(pfile, line, sizeof(line))) {
    267 		line_no++;
    268 		len = strlen(line);
    269 
    270 		if (len > 1) {
    271 			if (line[0] == '#') {
    272 				continue;
    273 			}
    274 		} else
    275 			continue;
    276 
    277 		/*
    278 		 * Check if we have the whole line
    279 		 */
    280 		if (line[len - 1] != '\n') {
    281 			warnx("%s line %d: line to long, skipping",
    282 			    fname, line_no);
    283 			continue;
    284 		} else
    285 			line[len - 1] = '\0';
    286 
    287 		p = line;
    288 
    289 		for (k = p, colon = 0; *k != '\0'; k++)
    290 			if (*k == ':')
    291 				colon++;
    292 
    293 		if (colon > 0) {
    294 			/* terminate key */
    295 			for (k = p; *p != ':'; p++);
    296 			*p++ = '\0';
    297 
    298 			/* If it's a YP entry, skip it. */
    299 			if (strlen(k) == 1)
    300 				if (*k == '+' || *k == '-')
    301 					continue;
    302 		}
    303 
    304 		if (colon < 4) {
    305 			warnx("%s line %d: syntax error",
    306 			    fname, line_no);
    307 			continue;
    308 		}
    309 
    310 		/* terminate password */
    311 		for (; *p != ':'; p++);
    312 		*p++ = '\0';
    313 
    314 		/* terminate uid */
    315 		for (u = p; *p != ':'; p++);
    316 		*p++ = '\0';
    317 
    318 		/* terminate gid */
    319 		for (g = p; *p != ':'; p++);
    320 		*p++ = '\0';
    321 
    322 		add_user(k, u, g);
    323 	}
    324 }
    325 
    326 int
    327 isgsep(ch)
    328 char ch;
    329 {
    330 
    331 	switch (ch) {
    332 	case ',':
    333 	case ' ':
    334 	case '\t':
    335 	case '\0':
    336 		return (1);
    337 	}
    338 
    339 	return (0);
    340 }
    341 
    342 void
    343 read_group(gfile, fname)
    344 	FILE *gfile;
    345 	char *fname;
    346 {
    347 	char line[_POSIX2_LINE_MAX];
    348 	int line_no = 0, len, colon;
    349 	char *p, *k, *u, *g;
    350 
    351 	while (read_line(gfile, line, sizeof(line))) {
    352 		line_no++;
    353 		len = strlen(line);
    354 
    355 		if (len > 1) {
    356 			if (line[0] == '#') {
    357 				continue;
    358 			}
    359 		} else
    360 			continue;
    361 
    362 		/*
    363 		 * Check if we have the whole line
    364 		 */
    365 		if (line[len - 1] != '\n') {
    366 			warnx("%s line %d: line to long, skipping",
    367 			    fname, line_no);
    368 			continue;
    369 		} else
    370 			line[len - 1] = '\0';
    371 
    372 		p = line;
    373 
    374 		for (k = p, colon = 0; *k != '\0'; k++)
    375 			if (*k == ':')
    376 				colon++;
    377 
    378 		if (colon > 0) {
    379 			/* terminate key */
    380 			for (k = p; *p != ':'; p++);
    381 			*p++ = '\0';
    382 
    383 			/* If it's a YP entry, skip it. */
    384 			if (strlen(k) == 1)
    385 				if (*k == '+' || *k == '-')
    386 					continue;
    387 		}
    388 
    389 		if (colon < 3) {
    390 			warnx("%s line %d: syntax error",
    391 			    fname, line_no);
    392 			continue;
    393 		}
    394 
    395 		/* terminate password */
    396 		for (; *p != ':'; p++);
    397 		*p++ = '\0';
    398 
    399 		/* terminate gid */
    400 		for (g = p; *p != ':'; p++);
    401 		*p++ = '\0';
    402 
    403 		/* get the group list */
    404 		for (u = p; *u != '\0'; u = p) {
    405 			/* find separator */
    406 			for (; isgsep(*p) == 0; p++);
    407 
    408 			if (*p != '\0') {
    409 				*p = '\0';
    410 				if (u != p)
    411 					add_group(u, g);
    412 				p++;
    413 			} else if (u != p)
    414 				add_group(u, g);
    415 		}
    416 	}
    417 }
    418 
    419 void
    420 print_passwd_group(qflag, domain)
    421 	int qflag;
    422 	char *domain;
    423 {
    424 	struct user *u, *p;
    425 	int i;
    426 
    427 	for (u = root.tqh_first; u != NULL; u = u->read.tqe_next) {
    428 		for (p = root.tqh_first; p->usr_uid != u->usr_uid;
    429 		    p = p->read.tqe_next)
    430 			/* empty */ ;
    431 		if (p != u) {
    432 			if (!qflag) {
    433 				warnx("unix.%d@%s %s", u->usr_uid, domain,
    434 				 "multiply defined, ignoring duplicate");
    435 			}
    436 		} else {
    437 			printf("unix.%d@%s %d:%d", u->usr_uid, domain,
    438 			    u->usr_uid, u->usr_gid);
    439 			if (u->gid_count >= 0)
    440 				for (i = 0; i <= u->gid_count; i++)
    441 					printf(",%d", u->gid[i]);
    442 			printf("\n");
    443 		}
    444 	}
    445 }
    446 
    447 void
    448 print_hosts(pfile, fname, domain)
    449 	FILE *pfile;
    450 	char *fname, *domain;
    451 {
    452 	char line[_POSIX2_LINE_MAX];
    453 	int line_no = 0, len;
    454 	char *p, *k, *u;
    455 
    456 	while (read_line(pfile, line, sizeof(line))) {
    457 		line_no++;
    458 		len = strlen(line);
    459 
    460 		if (len > 1) {
    461 			if (line[0] == '#') {
    462 				continue;
    463 			}
    464 		} else
    465 			continue;
    466 
    467 		/*
    468 		 * Check if we have the whole line
    469 		 */
    470 		if (line[len - 1] != '\n') {
    471 			warnx("%s line %d: line to long, skipping",
    472 			    fname, line_no);
    473 			continue;
    474 		} else
    475 			line[len - 1] = '\0';
    476 
    477 		p = line;
    478 
    479 		/* Find the key, replace trailing whitespace will <NUL> */
    480 		for (k = p; isspace(*p) == 0; p++);
    481 		while (isspace(*p))
    482 			*p++ = '\0';
    483 
    484 		/* Get first hostname. */
    485 		for (u = p; ; ) {
    486 			/* Check for EOL */
    487 			if (*p == '\0')
    488 				break;
    489 
    490 			if (isspace(*p) == 0)
    491 				p++;
    492 			else {
    493 				/* Got it. */
    494 				*p = '\0';
    495 				break;
    496 			}
    497 		}
    498 
    499 		printf("unix.%s@%s 0:%s\n", u, domain, u);
    500 	}
    501 }
    502 
    503 void
    504 print_netid(mfile, fname)
    505 	FILE *mfile;
    506 	char *fname;
    507 {
    508 	char line[_POSIX2_LINE_MAX];
    509 	int line_no = 0, len;
    510 	char *p, *k, *u;
    511 
    512 	while (read_line(mfile, line, sizeof(line))) {
    513 		line_no++;
    514 		len = strlen(line);
    515 
    516 		if (len > 1)
    517 			if (line[0] == '#')
    518 				continue;
    519 		else
    520 			continue;
    521 
    522 		/*
    523 		 * Check if we have the while line
    524 		 */
    525 		if (line[len - 1] != '\n') {
    526 			warnx("%s line %d: line to long, skipping",
    527 			    fname, line_no);
    528 			continue;
    529 		} else
    530 			line[len - 1] = '\0';
    531 
    532 		p = line;
    533 
    534 		/* Find the key, replace trailing whitespace will <NUL> */
    535 		for (k = p; isspace(*p) == 0; p++);
    536 		while (isspace(*p))
    537 			*p++ = '\0';
    538 
    539 		/* Get netid entry. */
    540 		for (u = p; ; ) {
    541 			/* Check for EOL */
    542 			if (*p == '\0')
    543 				break;
    544 
    545 			if (isspace(*p) == 0)
    546 				p++;
    547 			else {
    548 				/* Got it. */
    549 				*p = '\0';
    550 				break;
    551 			}
    552 		}
    553 
    554 		printf("%s %s\n", k, u);
    555 	}
    556 }
    557 
    558 void
    559 usage()
    560 {
    561 
    562 	fprintf(stderr, "usage: %s %s\n", __progname,
    563 	    "[-d domain] [-q] [-p passwdfile] [-g groupfile]");
    564 	fprintf(stderr, "       %s  %s", __progname,
    565 	    "[-g groupfile] [-h hostfile] [-m netidfile]");
    566 	exit(1);
    567 }
    568