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