xref: /openbsd-src/usr.sbin/ypserv/mknetid/mknetid.c (revision 28056f30107dcf32988d70b56824d14f4aced47b)
1*28056f30Sderaadt /*	$OpenBSD: mknetid.c,v 1.22 2015/02/09 23:00:15 deraadt Exp $ */
2bbfaf3a3Sderaadt 
363ab8f9cSdm /*
463ab8f9cSdm  * Copyright (c) 1996 Mats O Jansson <moj@stacken.kth.se>
563ab8f9cSdm  * All rights reserved.
663ab8f9cSdm  *
763ab8f9cSdm  * Redistribution and use in source and binary forms, with or without
863ab8f9cSdm  * modification, are permitted provided that the following conditions
963ab8f9cSdm  * are met:
1063ab8f9cSdm  * 1. Redistributions of source code must retain the above copyright
1163ab8f9cSdm  *    notice, this list of conditions and the following disclaimer.
1263ab8f9cSdm  * 2. Redistributions in binary form must reproduce the above copyright
1363ab8f9cSdm  *    notice, this list of conditions and the following disclaimer in the
1463ab8f9cSdm  *    documentation and/or other materials provided with the distribution.
1563ab8f9cSdm  *
1663ab8f9cSdm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1763ab8f9cSdm  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1863ab8f9cSdm  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1963ab8f9cSdm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
2063ab8f9cSdm  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2163ab8f9cSdm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2263ab8f9cSdm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2363ab8f9cSdm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2463ab8f9cSdm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2563ab8f9cSdm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2663ab8f9cSdm  * SUCH DAMAGE.
2763ab8f9cSdm  */
2863ab8f9cSdm 
2963ab8f9cSdm #include <stdio.h>
3063ab8f9cSdm #include <unistd.h>
3163ab8f9cSdm #include <ctype.h>
32325921adSderaadt #include <string.h>
333f7cd823Sderaadt #include <stdlib.h>
3463ab8f9cSdm #include <pwd.h>
3563ab8f9cSdm #include <grp.h>
36658494f5Sderaadt #include <err.h>
3763ab8f9cSdm #include <netdb.h>
38b9fc9a72Sderaadt #include <limits.h>
3963ab8f9cSdm 
40658494f5Sderaadt #include <rpcsvc/ypclnt.h>
41658494f5Sderaadt 
4263ab8f9cSdm struct user {
4363ab8f9cSdm 	char	*usr_name;		/* user name */
4463ab8f9cSdm 	int	usr_uid;		/* user uid */
4563ab8f9cSdm 	int	usr_gid;		/* user gid */
4663ab8f9cSdm 	int	gid_count;		/* number of gids */
47b9fc9a72Sderaadt 	int	gid[NGROUPS_MAX];	/* additional gids */
4863ab8f9cSdm 	struct user *prev, *next;	/* links in read order */
4963ab8f9cSdm 	struct user *hprev, *hnext;	/* links in hash order */
5063ab8f9cSdm };
5163ab8f9cSdm 
5263ab8f9cSdm char *HostFile = _PATH_HOSTS;
5363ab8f9cSdm char *PasswdFile = _PATH_PASSWD;
54a6d6c217Sschwarze char *MasterPasswdFile = _PATH_MASTERPASSWD;
5563ab8f9cSdm char *GroupFile = _PATH_GROUP;
5663ab8f9cSdm char *NetidFile = "/etc/netid";
5763ab8f9cSdm 
5863ab8f9cSdm #define HASHMAX 55
5963ab8f9cSdm 
6063ab8f9cSdm struct user *root = NULL, *tail = NULL;
6163ab8f9cSdm struct user *hroot[HASHMAX], *htail[HASHMAX];
6263ab8f9cSdm 
635325fcc5Sderaadt static int
read_line(FILE * fp,char * buf,int size)6433dd5606Sderaadt read_line(FILE *fp, char *buf, int size)
6563ab8f9cSdm {
6663ab8f9cSdm 	int done = 0;
6763ab8f9cSdm 
6863ab8f9cSdm 	do {
6963ab8f9cSdm 		while (fgets(buf, size, fp)) {
7063ab8f9cSdm 			int len = strlen(buf);
71cd85bc22Sderaadt 
7263ab8f9cSdm 			done += len;
7363ab8f9cSdm 			if (len > 1 && buf[len-2] == '\\' &&
7463ab8f9cSdm 			    buf[len-1] == '\n') {
7563ab8f9cSdm 				int ch;
76cd85bc22Sderaadt 
7763ab8f9cSdm 				buf += len - 2;
7863ab8f9cSdm 				size -= len - 2;
7963ab8f9cSdm 				*buf = '\n'; buf[1] = '\0';
805bb3a5e4Sderaadt 
8163ab8f9cSdm 				/*
8263ab8f9cSdm 				 * Skip leading white space on next line
8363ab8f9cSdm 				 */
8463ab8f9cSdm 				while ((ch = getc(fp)) != EOF &&
8563ab8f9cSdm 				    isascii(ch) && isspace(ch))
8663ab8f9cSdm 					;
8763ab8f9cSdm 				(void) ungetc(ch, fp);
8863ab8f9cSdm 			} else {
8963ab8f9cSdm 				return done;
9063ab8f9cSdm 			}
9163ab8f9cSdm 		}
9263ab8f9cSdm 	} while (size > 0 && !feof(fp));
9363ab8f9cSdm 
9463ab8f9cSdm 	return done;
9563ab8f9cSdm }
9663ab8f9cSdm 
975325fcc5Sderaadt static int
hashidx(char key)9833dd5606Sderaadt hashidx(char key)
9963ab8f9cSdm {
10063ab8f9cSdm 	if (key < 'A')
10163ab8f9cSdm 		return (0);
10263ab8f9cSdm 	if (key <= 'Z')
10363ab8f9cSdm 		return (1 + key - 'A');
10463ab8f9cSdm 	if (key < 'a')
10563ab8f9cSdm 		return (27);
10663ab8f9cSdm 	if (key <= 'z')
10763ab8f9cSdm 		return (28 + key - 'a');
10863ab8f9cSdm 	return (54);
10963ab8f9cSdm }
11063ab8f9cSdm 
1115325fcc5Sderaadt static void
add_user(char * username,char * uid,char * gid)11233dd5606Sderaadt add_user(char *username, char *uid, char *gid)
11363ab8f9cSdm {
11463ab8f9cSdm 	struct user *u;
11563ab8f9cSdm 	int idx;
11663ab8f9cSdm 
117fb92953eSderaadt 	u = malloc(sizeof(struct user));
1185bb3a5e4Sderaadt 	if (u == NULL)
1195bb3a5e4Sderaadt 		err(1, "malloc");
1205bb3a5e4Sderaadt 	bzero(u, sizeof(struct user));
1215bb3a5e4Sderaadt 	u->usr_name = strdup(username);
1225bb3a5e4Sderaadt 	if (u->usr_name == NULL)
1235bb3a5e4Sderaadt 		err(1, "strdup");
12463ab8f9cSdm 	u->usr_uid = atoi(uid);
12563ab8f9cSdm 	u->usr_gid = atoi(gid);
12663ab8f9cSdm 	u->gid_count = -1;
12763ab8f9cSdm 	if (root == NULL) {
12863ab8f9cSdm 		root = tail = u;
12963ab8f9cSdm 	} else {
13063ab8f9cSdm 		u->prev = tail;
13163ab8f9cSdm 		tail->next = u;
13263ab8f9cSdm 		tail = u;
13363ab8f9cSdm 	}
13463ab8f9cSdm 	idx = hashidx(username[0]);
13563ab8f9cSdm 	if (hroot[idx] == NULL) {
13663ab8f9cSdm 		hroot[idx] = htail[idx] = u;
13763ab8f9cSdm 	} else {
13863ab8f9cSdm 		u->hprev = htail[idx];
13963ab8f9cSdm 		htail[idx]->hnext = u;
14063ab8f9cSdm 		htail[idx] = u;
14163ab8f9cSdm 	}
14263ab8f9cSdm }
14363ab8f9cSdm 
1445325fcc5Sderaadt static void
add_group(char * username,char * gid)1455325fcc5Sderaadt add_group(char *username, char *gid)
14663ab8f9cSdm {
14763ab8f9cSdm 	struct user *u;
14863ab8f9cSdm 	int idx, g;
14963ab8f9cSdm 
15063ab8f9cSdm 	idx = hashidx(username[0]);
15163ab8f9cSdm 	u = hroot[idx];
15263ab8f9cSdm 	g = atoi(gid);
15363ab8f9cSdm 
15463ab8f9cSdm 	while (u != NULL) {
15563ab8f9cSdm 		if (strcmp(username, u->usr_name) == 0) {
15663ab8f9cSdm 			if (g != u->usr_gid) {
15763ab8f9cSdm 				u->gid_count++;
158b9fc9a72Sderaadt 				if (u->gid_count < NGROUPS_MAX)
15963ab8f9cSdm 					u->gid[u->gid_count] = atoi(gid);
16063ab8f9cSdm 			}
16163ab8f9cSdm 			u = htail[idx];
16263ab8f9cSdm 		}
16363ab8f9cSdm 		u = u->hnext;
16463ab8f9cSdm 	}
16563ab8f9cSdm }
16663ab8f9cSdm 
1675325fcc5Sderaadt static void
read_passwd(FILE * pfile,char * fname)16833dd5606Sderaadt read_passwd(FILE *pfile, char *fname)
16963ab8f9cSdm {
170cd85bc22Sderaadt 	char  line[1024], *p, *k, *u, *g;
171cd85bc22Sderaadt 	int   line_no = 0, len, colon;
17263ab8f9cSdm 
17363ab8f9cSdm 	while (read_line(pfile, line, sizeof(line))) {
17463ab8f9cSdm 		line_no++;
17563ab8f9cSdm 		len = strlen(line);
17663ab8f9cSdm 
17763ab8f9cSdm 		if (len > 0) {
17863ab8f9cSdm 			if (line[0] == '#')
17963ab8f9cSdm 				continue;
18063ab8f9cSdm 		}
18163ab8f9cSdm 
18263ab8f9cSdm 		/*
18363ab8f9cSdm 		 * Check if we have the whole line
18463ab8f9cSdm 		 */
18563ab8f9cSdm 		if (line[len-1] != '\n') {
1865bb3a5e4Sderaadt 			fprintf(stderr, "line %d in \"%s\" is too long\n",
18763ab8f9cSdm 			    line_no, fname);
18863ab8f9cSdm 		} else {
18963ab8f9cSdm 			line[len-1] = '\0';
19063ab8f9cSdm 		}
19163ab8f9cSdm 
19263ab8f9cSdm 		p = (char *)&line;
19363ab8f9cSdm 
19463ab8f9cSdm 		k = p; colon = 0;
19563ab8f9cSdm 		while (*k != '\0') {
19663ab8f9cSdm 			if (*k == ':')
19763ab8f9cSdm 				colon++;
19863ab8f9cSdm 			k++;
19963ab8f9cSdm 		}
20063ab8f9cSdm 
20163ab8f9cSdm 		if (colon > 0) {
20263ab8f9cSdm 			k = p;			/* save start of key  */
2035bb3a5e4Sderaadt 			while (*p != ':')
2045bb3a5e4Sderaadt 				p++;		/* find first "colon" */
2055bb3a5e4Sderaadt 			if (*p==':')
2065bb3a5e4Sderaadt 				*p++ = '\0';	/* terminate key */
20763ab8f9cSdm 			if (strlen(k) == 1) {
20863ab8f9cSdm 				if (*k == '+')
20963ab8f9cSdm 					continue;
21063ab8f9cSdm 			}
21163ab8f9cSdm 		}
21263ab8f9cSdm 
21363ab8f9cSdm 		if (colon < 4) {
2145bb3a5e4Sderaadt 			fprintf(stderr, "syntax error at line %d in \"%s\"\n",
21563ab8f9cSdm 			    line_no, fname);
21663ab8f9cSdm 			continue;
21763ab8f9cSdm 		}
21863ab8f9cSdm 
2195bb3a5e4Sderaadt 		while (*p != ':')
2205bb3a5e4Sderaadt 			p++;			/* find second "colon" */
2215bb3a5e4Sderaadt 		if (*p==':')
2225bb3a5e4Sderaadt 			*p++ = '\0';		/* terminate passwd */
22363ab8f9cSdm 		u = p;
2245bb3a5e4Sderaadt 		while (*p != ':')
2255bb3a5e4Sderaadt 			p++;			/* find third "colon" */
2265bb3a5e4Sderaadt 		if (*p==':')
2275bb3a5e4Sderaadt 			*p++ = '\0';		/* terminate uid */
22863ab8f9cSdm 		g = p;
2295bb3a5e4Sderaadt 		while (*p != ':')
2305bb3a5e4Sderaadt 			p++;			/* find fourth "colon" */
2315bb3a5e4Sderaadt 		if (*p==':')
2325bb3a5e4Sderaadt 			*p++ = '\0';		/* terminate gid */
2335bb3a5e4Sderaadt 		while (*p != '\0')
2345bb3a5e4Sderaadt 			p++;	/* find end of string */
23563ab8f9cSdm 
23663ab8f9cSdm 		add_user(k, u, g);
23763ab8f9cSdm 	}
23863ab8f9cSdm }
23963ab8f9cSdm 
2405325fcc5Sderaadt static int
isgsep(char ch)24133dd5606Sderaadt isgsep(char ch)
24263ab8f9cSdm {
24363ab8f9cSdm 	switch (ch)  {
24463ab8f9cSdm 	case ',':
24563ab8f9cSdm 	case ' ':
24663ab8f9cSdm 	case '\t':
24763ab8f9cSdm 	case '\0':
2485bb3a5e4Sderaadt 		return (1);
24963ab8f9cSdm 	default:
2505bb3a5e4Sderaadt 		return (0);
25163ab8f9cSdm 	}
25263ab8f9cSdm }
25363ab8f9cSdm 
2545325fcc5Sderaadt static void
read_group(FILE * gfile,char * fname)25533dd5606Sderaadt read_group(FILE *gfile, char *fname)
25663ab8f9cSdm {
257cd85bc22Sderaadt 	char  line[2048], *p, *k, *u, *g;
258cd85bc22Sderaadt 	int   line_no = 0, len, colon;
25963ab8f9cSdm 
26063ab8f9cSdm 	while (read_line(gfile, line, sizeof(line))) {
26163ab8f9cSdm 		line_no++;
26263ab8f9cSdm 		len = strlen(line);
26363ab8f9cSdm 
26463ab8f9cSdm 		if (len > 0) {
26563ab8f9cSdm 			if (line[0] == '#')
26663ab8f9cSdm 				continue;
26763ab8f9cSdm 		}
26863ab8f9cSdm 
26963ab8f9cSdm 		/*
27063ab8f9cSdm 		 * Check if we have the whole line
27163ab8f9cSdm 		 */
27263ab8f9cSdm 		if (line[len-1] != '\n') {
2735bb3a5e4Sderaadt 			fprintf(stderr, "line %d in \"%s\" is too long\n",
27463ab8f9cSdm 			    line_no, fname);
27563ab8f9cSdm 		} else {
27663ab8f9cSdm 			line[len-1] = '\0';
27763ab8f9cSdm 		}
27863ab8f9cSdm 
27963ab8f9cSdm 		p = (char *)&line;
28063ab8f9cSdm 
28163ab8f9cSdm 		k = p; colon = 0;
28263ab8f9cSdm 		while (*k != '\0') {
28363ab8f9cSdm 			if (*k == ':')
28463ab8f9cSdm 				colon++;
28563ab8f9cSdm 			k++;
28663ab8f9cSdm 		}
28763ab8f9cSdm 
28863ab8f9cSdm 		if (colon > 0) {
28963ab8f9cSdm 			k = p;			/* save start of key  */
2905bb3a5e4Sderaadt 			while (*p != ':')
2915bb3a5e4Sderaadt 				p++;		/* find first "colon" */
2925bb3a5e4Sderaadt 			if (*p==':')
2935bb3a5e4Sderaadt 				*p++ = '\0';	/* terminate key */
29463ab8f9cSdm 			if (strlen(k) == 1) {
29563ab8f9cSdm 				if (*k == '+')
29663ab8f9cSdm 					continue;
29763ab8f9cSdm 			}
29863ab8f9cSdm 		}
29963ab8f9cSdm 
30063ab8f9cSdm 		if (colon < 3) {
3015bb3a5e4Sderaadt 			fprintf(stderr, "syntax error at line %d in \"%s\"\n",
30263ab8f9cSdm 			    line_no, fname);
30363ab8f9cSdm 			continue;
30463ab8f9cSdm 		}
30563ab8f9cSdm 
3065bb3a5e4Sderaadt 		while (*p != ':')
3075bb3a5e4Sderaadt 			p++;			/* find second "colon" */
3085bb3a5e4Sderaadt 		if (*p==':')
3095bb3a5e4Sderaadt 			*p++ = '\0';		/* terminate passwd */
31063ab8f9cSdm 		g = p;
3115bb3a5e4Sderaadt 		while (*p != ':')
3125bb3a5e4Sderaadt 			p++;			/* find third "colon" */
3135bb3a5e4Sderaadt 		if (*p==':')
3145bb3a5e4Sderaadt 			*p++ = '\0';		/* terminate gid */
31563ab8f9cSdm 
31663ab8f9cSdm 		u = p;
31763ab8f9cSdm 
31863ab8f9cSdm 		while (*u != '\0') {
3195bb3a5e4Sderaadt 			while (!isgsep(*p))
3205bb3a5e4Sderaadt 				p++;		/* find separator */
32163ab8f9cSdm 			if (*p != '\0') {
32263ab8f9cSdm 				*p = '\0';
3235bb3a5e4Sderaadt 				if (u != p)
32463ab8f9cSdm 					add_group(u, g);
32563ab8f9cSdm 				p++;
32663ab8f9cSdm 			} else {
3275bb3a5e4Sderaadt 				if (u != p)
32863ab8f9cSdm 					add_group(u, g);
32963ab8f9cSdm 			}
33063ab8f9cSdm 			u = p;
33163ab8f9cSdm 		}
33263ab8f9cSdm 	}
33363ab8f9cSdm }
33463ab8f9cSdm 
3355325fcc5Sderaadt static void
print_passwd_group(int qflag,char * domain)33633dd5606Sderaadt print_passwd_group(int qflag, char *domain)
33763ab8f9cSdm {
33863ab8f9cSdm 	struct user *u, *p;
33963ab8f9cSdm 	int i;
34063ab8f9cSdm 
34163ab8f9cSdm 	u = root;
34263ab8f9cSdm 
34363ab8f9cSdm 	while (u != NULL) {
34463ab8f9cSdm 		p = root;
3455bb3a5e4Sderaadt 		while (p->usr_uid != u->usr_uid)
34663ab8f9cSdm 			p = p->next;
3475bb3a5e4Sderaadt 
34863ab8f9cSdm 		if (p != u) {
34963ab8f9cSdm 			if (!qflag) {
3508ceb539dSderaadt 				fprintf(stderr, "mknetid: unix.%d@%s %s\n",
3518ceb539dSderaadt 				    u->usr_uid, domain,
35263ab8f9cSdm 				    "multiply defined, other definitions ignored");
35363ab8f9cSdm 			}
35463ab8f9cSdm 		} else {
35563ab8f9cSdm 			printf("unix.%d@%s %d:%d",
3565bb3a5e4Sderaadt 			    u->usr_uid, domain, u->usr_uid, u->usr_gid);
35763ab8f9cSdm 			if (u->gid_count >= 0) {
35863ab8f9cSdm 				i = 0;
35963ab8f9cSdm 				while (i <= u->gid_count) {
36063ab8f9cSdm 					printf(",%d", u->gid[i]);
36163ab8f9cSdm 					i++;
36263ab8f9cSdm 				}
36363ab8f9cSdm 			}
36463ab8f9cSdm 			printf("\n");
36563ab8f9cSdm 		}
36663ab8f9cSdm 		u = u->next;
36763ab8f9cSdm 	}
36863ab8f9cSdm }
36963ab8f9cSdm 
3705325fcc5Sderaadt static void
print_hosts(FILE * pfile,char * fname,char * domain)37133dd5606Sderaadt print_hosts(FILE *pfile, char *fname, char *domain)
37263ab8f9cSdm {
373fb92953eSderaadt 	char  line[1024], *p, *u;
374cd85bc22Sderaadt 	int   line_no = 0, len;
37563ab8f9cSdm 
37663ab8f9cSdm 	while (read_line(pfile, line, sizeof(line))) {
37763ab8f9cSdm 		line_no++;
37863ab8f9cSdm 		len = strlen(line);
37963ab8f9cSdm 
38063ab8f9cSdm 		if (len > 0) {
38163ab8f9cSdm 			if (line[0] == '#')
38263ab8f9cSdm 				continue;
38363ab8f9cSdm 		}
38463ab8f9cSdm 
38563ab8f9cSdm 		/*
38663ab8f9cSdm 		 * Check if we have the whole line
38763ab8f9cSdm 		 */
38863ab8f9cSdm 		if (line[len-1] != '\n') {
3895bb3a5e4Sderaadt 			fprintf(stderr, "line %d in \"%s\" is too long\n",
39063ab8f9cSdm 			    line_no, fname);
39163ab8f9cSdm 		} else {
39263ab8f9cSdm 			line[len-1] = '\0';
39363ab8f9cSdm 		}
39463ab8f9cSdm 
39563ab8f9cSdm 		p = (char *)&line;
39663ab8f9cSdm 
397ff01f5f6Sderaadt 		while (!isspace((unsigned char)*p))
3985bb3a5e4Sderaadt 			p++;			/* find first "space" */
399ff01f5f6Sderaadt 		while (isspace((unsigned char)*p))
4005bb3a5e4Sderaadt 			*p++ = '\0';		/* replace space with <NUL> */
40163ab8f9cSdm 
40263ab8f9cSdm 		u = p;
40363ab8f9cSdm 		while (p != NULL) {
40463ab8f9cSdm 			if (*p == '\0') {
40563ab8f9cSdm 				p = NULL;
40663ab8f9cSdm 			} else {
407ff01f5f6Sderaadt 				if (!isspace((unsigned char)*p)) {
40863ab8f9cSdm 					p++;
40963ab8f9cSdm 				} else {
41063ab8f9cSdm 					*p = '\0';
41163ab8f9cSdm 					p = NULL;
41263ab8f9cSdm 				}
41363ab8f9cSdm 			}
41463ab8f9cSdm 		}
41563ab8f9cSdm 
4165bb3a5e4Sderaadt 		printf("unix.%s@%s 0:%s\n", u, domain, u);
41763ab8f9cSdm 	}
41863ab8f9cSdm }
41963ab8f9cSdm 
4205325fcc5Sderaadt static void
print_netid(FILE * mfile,char * fname)42133dd5606Sderaadt print_netid(FILE *mfile, char *fname)
42263ab8f9cSdm {
423cd85bc22Sderaadt 	char  line[1024], *p, *k, *u;
424cd85bc22Sderaadt 	int   line_no = 0, len;
42563ab8f9cSdm 
42663ab8f9cSdm 	while (read_line(mfile, line, sizeof(line))) {
42763ab8f9cSdm 		line_no++;
42863ab8f9cSdm 		len = strlen(line);
42963ab8f9cSdm 
43063ab8f9cSdm 		if (len > 0) {
43163ab8f9cSdm 			if (line[0] == '#')
43263ab8f9cSdm 				continue;
43363ab8f9cSdm 		}
43463ab8f9cSdm 
43563ab8f9cSdm 		/*
43663ab8f9cSdm 		 * Check if we have the whole line
43763ab8f9cSdm 		 */
43863ab8f9cSdm 		if (line[len-1] != '\n') {
4395bb3a5e4Sderaadt 			fprintf(stderr, "line %d in \"%s\" is too long\n",
44063ab8f9cSdm 			    line_no, fname);
44163ab8f9cSdm 		} else {
44263ab8f9cSdm 			line[len-1] = '\0';
44363ab8f9cSdm 		}
44463ab8f9cSdm 
44563ab8f9cSdm 		p = (char *)&line;
44663ab8f9cSdm 
44763ab8f9cSdm 		k = p;				/* save start of key  */
448ff01f5f6Sderaadt 		while (!isspace((unsigned char)*p))
4495bb3a5e4Sderaadt 			p++;			/* find first "space" */
450ff01f5f6Sderaadt 		while (isspace((unsigned char)*p))
4515bb3a5e4Sderaadt 			*p++ = '\0';		/* replace space with <NUL> */
45263ab8f9cSdm 
45363ab8f9cSdm 		u = p;
45463ab8f9cSdm 		while (p != NULL) {
45563ab8f9cSdm 			if (*p == '\0') {
45663ab8f9cSdm 				p = NULL;
45763ab8f9cSdm 			} else {
458ff01f5f6Sderaadt 				if (!isspace((unsigned char)*p)) {
45963ab8f9cSdm 					p++;
46063ab8f9cSdm 				} else {
46163ab8f9cSdm 					*p = '\0';
46263ab8f9cSdm 					p = NULL;
46363ab8f9cSdm 				}
46463ab8f9cSdm 			}
46563ab8f9cSdm 		}
46663ab8f9cSdm 
46763ab8f9cSdm 		printf("%s %s\n", k, u);
46863ab8f9cSdm 	}
46963ab8f9cSdm }
47063ab8f9cSdm 
4715325fcc5Sderaadt static void
usage(void)4728ceb539dSderaadt usage(void)
4738ceb539dSderaadt {
474cd06f2a3Ssobrado 	fprintf(stderr, "usage: mknetid [-q] [-d domain] [-g groupfile] "
475cd06f2a3Ssobrado 	    "[-h hostfile] [-m netidfile]\n"
476a6d6c217Sschwarze 	    "               [-P master.passwdfile] [-p passwdfile]\n");
4778ceb539dSderaadt 	exit(1);
4788ceb539dSderaadt }
4798ceb539dSderaadt 
48063ab8f9cSdm int
main(int argc,char * argv[])48133dd5606Sderaadt main(int argc, char *argv[])
48263ab8f9cSdm {
48363ab8f9cSdm 	FILE   *pfile, *gfile, *hfile, *mfile;
4848ceb539dSderaadt 	int	qflag = 0, ch;
4858ceb539dSderaadt 	char   *domain = NULL;
48663ab8f9cSdm 
487a6d6c217Sschwarze 	while ((ch = getopt(argc, argv, "d:g:h:m:p:P:q")) != -1)
48863ab8f9cSdm 		switch (ch) {
48963ab8f9cSdm 		case 'd':
49063ab8f9cSdm 			domain = optarg;
49163ab8f9cSdm 			break;
49263ab8f9cSdm 		case 'g':
49363ab8f9cSdm 			GroupFile = optarg;
49463ab8f9cSdm 			break;
49563ab8f9cSdm 		case 'h':
49663ab8f9cSdm 			HostFile = optarg;
49763ab8f9cSdm 			break;
49863ab8f9cSdm 		case 'm':
49963ab8f9cSdm 			NetidFile = optarg;
50063ab8f9cSdm 			break;
50163ab8f9cSdm 		case 'p':
50263ab8f9cSdm 			PasswdFile = optarg;
50363ab8f9cSdm 			break;
504a6d6c217Sschwarze 		case 'P':
505a6d6c217Sschwarze 			MasterPasswdFile = optarg;
506a6d6c217Sschwarze 			break;
50763ab8f9cSdm 		case 'q':
508*28056f30Sderaadt 			qflag = 1;
50963ab8f9cSdm 			break;
51063ab8f9cSdm 		default:
5118ceb539dSderaadt 			usage();
51263ab8f9cSdm 			break;
51363ab8f9cSdm 		}
51463ab8f9cSdm 
5158ceb539dSderaadt 	if (argc > optind)
5168ceb539dSderaadt 		usage();
51763ab8f9cSdm 
5188ceb539dSderaadt 	if (domain == NULL)
51963ab8f9cSdm 		yp_get_default_domain(&domain);
52063ab8f9cSdm 
52163ab8f9cSdm 	pfile = fopen(PasswdFile, "r");
522a6d6c217Sschwarze 	if (pfile == NULL)
523a6d6c217Sschwarze 		pfile = fopen(MasterPasswdFile, "r");
524a6d6c217Sschwarze 	if (pfile == NULL)
5258aac6f43Sguenther 		err(1, "%s", MasterPasswdFile);
52663ab8f9cSdm 
52763ab8f9cSdm 	gfile = fopen(GroupFile, "r");
528a6d6c217Sschwarze 	if (gfile == NULL)
5298aac6f43Sguenther 		err(1, "%s", GroupFile);
53063ab8f9cSdm 
53163ab8f9cSdm 	hfile = fopen(HostFile, "r");
532a6d6c217Sschwarze 	if (hfile == NULL)
5338aac6f43Sguenther 		err(1, "%s", HostFile);
53463ab8f9cSdm 
53563ab8f9cSdm 	mfile = fopen(NetidFile, "r");
53663ab8f9cSdm 
53763ab8f9cSdm 	read_passwd(pfile, PasswdFile);
53863ab8f9cSdm 	read_group(gfile, GroupFile);
53963ab8f9cSdm 
54063ab8f9cSdm 	print_passwd_group(qflag, domain);
54163ab8f9cSdm 	print_hosts(hfile, HostFile, domain);
5425bb3a5e4Sderaadt 
5435bb3a5e4Sderaadt 	if (mfile != NULL)
54463ab8f9cSdm 		print_netid(mfile, NetidFile);
545dbc5aed9Sderaadt 
546a6d6c217Sschwarze 	return 0;
54763ab8f9cSdm }
548