summaryrefslogtreecommitdiff
path: root/usr/src/cmd/ypcmd/mknetid/mknetid.c
blob: 1cc0d85057215c27eebf38d4c3dd97606d14bd41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright (c) 2016 by Delphix. All rights reserved.
 */

/*
 * Network name to unix credential database generator.
 * Uses /etc/passwd, /etc/group, /etc/hosts and /etc/netid to
 * create the database.
 *
 * If some user appears in passwd, they get an entry like:
 *	sun.<uid>@<domainname>	<uid>:<gid1>,<gid2>,...
 * If some host appears in hosts, it gets an entry like:
 *	sun.<hostname>@<domainname>	0:<hostname>
 *
 * The file /etc/netid is used to add other domains (possibly non-unix)
 * to the database.
 */
#include <stdio.h>
#include <pwd.h>
#include <limits.h>
#include <sys/param.h>
#include <rpc/rpc.h>
#include <rpc/key_prot.h>


#define	MAXNAMELEN	256
#define	MAXLINELEN	1024
#define	MAXDOMAINLEN	32

#define	GRPTABSIZE	256		/* size of group table */
#define	PRNTABSIZE	4096		/* size of printed item table */

#define	NUMGIDS	(NGROUPS_MAX + 1)	/* group-access-list + gid */

extern char **getaline();
extern char *malloc();
extern char *strcpy();

/*
 * The group list
 * Store username and groups to which they belong
 */
struct group_list {
	char *user;
	int group_len;
	int groups[NUMGIDS];
	struct group_list *next;
};

/*
 * General purpose list of strings
 */
struct string_list {
	char *str;
	struct string_list *next;
};

static FILE *openfile();
static char *scanargs();
static int atoi();

static char *cmdname;	/* name of this program */
static int quietmode;	/* quiet mode: don't print error messages */
static char *curfile;	/* name of file we are parsing */
static int curline;		/* current line parsed in this file */

static struct group_list *groups[GRPTABSIZE];	/* group table */
static struct string_list *printed[PRNTABSIZE];	/* printed item table */
static char domain[MAXDOMAINLEN];	/* name of our domain */

static char PASSWD[] = "/etc/passwd";	/* default passwd database */
static char IDMAP[] = "/etc/idmap";	/* default net-id map database */
static char GROUP[] = "/etc/group";	/* default group database */
static char HOSTS[] = "/etc/hosts";	/* default hosts database */

static char *pwdfile = PASSWD;	/* password file to parse */
static char *grpfile = GROUP;	/* group file */
static char *hostfile = HOSTS;	/* hosts file */
static char *mapfile = IDMAP;	/* network id file */

/*
 * Various separaters
 */
static char WHITE[] = "\t ";
static char COLON[] = ":";
static char COMMA[] = ",";

void domapfile(char *, FILE *);
void dogrpfile(char *, FILE *);
void dopwdfile(char *, FILE *);
void dohostfile(char *, FILE *);
static int Atoi(char *);
void check_getname(char **, char *, char *, char *, char *);
void multdef(char *);
static int wasprinted(char *);
void storegid(int, char *);
void printgroups(char *, int);
int parseargs(int, char *[]);
void put_s(char *);
void put_d(int);


int
main(argc, argv)
	int argc;
	char *argv[];
{
	FILE *pf, *mf, *gf, *hf;

	cmdname = argv[0];
	if (!parseargs(argc, argv)) {
		(void) fprintf(stderr,
			"usage: %s [-q] [-pghm filename]\n", cmdname);
		exit(1);
	}
	(void) getdomainname(domain, sizeof (domain));

	pf = openfile(pwdfile);
	gf = openfile(grpfile);
	hf = openfile(hostfile);
	mf = fopen(mapfile, "r");


	if (mf != NULL) {
		domapfile(mapfile, mf);
	}
	dogrpfile(grpfile, gf);
	dopwdfile(pwdfile, pf);
	dohostfile(hostfile, hf);

	return (0);
	/* NOTREACHED */
}

/*
 * Parse the network id mapping file
 */
void
domapfile(mapfile, mf)
	char *mapfile;
	FILE *mf;
{
	char **lp;
	char line[MAXLINELEN];
	char name[MAXNAMELEN];
	int uid, gid;

	curfile = mapfile;
	curline = 0;
	while (lp = getaline(line, sizeof (line), mf, &curline, "#")) {
		check_getname(lp, name, WHITE, WHITE, "#");
		if (wasprinted(name)) {
			multdef(name);
			continue;
		}
		put_s(name);
		(void) putchar(' ');
		check_getname(lp, name, WHITE, COLON, "#");
		uid = Atoi(name);
		put_d(uid);
		(void) putchar(':');
		if (uid == 0) {
			check_getname(lp, name, WHITE, "\t\n ", "#");
			put_s(name);
			(void) putchar(' ');
		} else {
			check_getname(lp, name, WHITE, ",\n", "#");
			gid = Atoi(name);
			put_d(gid);
			while (getname(name, sizeof (name), WHITE, ",\n", lp,
					"#") >= 0) {
				gid = Atoi(name);
				(void) putchar(',');
				put_d(gid);
			}
		}
		(void) putchar('\n');
	}
}


/*
 * Parse the groups file
 */
void
dogrpfile(grpfile, gf)
	char *grpfile;
	FILE *gf;
{
	char **lp;
	char line[MAXLINELEN];
	char name[MAXNAMELEN];
	int gid;

	curfile = grpfile;
	curline = 0;
	while (lp = getaline(line, sizeof (line), gf, &curline, "")) {
		check_getname(lp, name, WHITE, COLON, "");
		if (name[0] == '+') {
			continue;
		}
		check_getname(lp, name, WHITE, COLON, ""); /* ignore passwd */
		check_getname(lp, name, WHITE, COLON, "");
		gid = Atoi(name);
		while (getname(name, sizeof (name), WHITE, COMMA, lp,
				"") >= 0) {
			storegid(gid, name);
		}
	}
}


/*
 * Parse the password file
 */
void
dopwdfile(pwdfile, pf)
	char *pwdfile;
	FILE *pf;
{
	char **lp;
	char line[MAXLINELEN];
	char name[MAXNAMELEN];
	char user[MAXNAMELEN];
	int uid, gid;

	curfile = pwdfile;
	curline = 0;

	while (lp = getaline(line, sizeof (line), pf, &curline, "")) {
		check_getname(lp, user, WHITE, COLON, ""); 	/* username */
		if (user[0] == '-' || user[0] == '+') {
			continue;	/* NIS entry */
		}
		check_getname(lp, name, WHITE, COLON, ""); /* ignore passwd */
		check_getname(lp, name, WHITE, COLON, ""); /* but get uid */
		uid = Atoi(name);
		user2netname(name, uid, domain);
		if (wasprinted(name)) {
			multdef(name);
			continue;
		}
		put_s(name);
		(void) putchar(' ');
		check_getname(lp, name, WHITE, COLON, "");
		gid = Atoi(name);
		put_d(uid);
		(void) putchar(':');
		printgroups(user, gid);
	}
}


/*
 * Parse the hosts file
 */
void
dohostfile(hostfile, hf)
	char *hostfile;
	FILE *hf;
{
	char **lp;
	char line[MAXLINELEN];
	char name[MAXNAMELEN];
	char netname[MAXNETNAMELEN];

	curfile = hostfile;
	curline = 0;
	while (lp = getaline(line, sizeof (line), hf, &curline, "#")) {
		check_getname(lp, name, WHITE, WHITE, "#");
		if (getname(name, MAXNAMELEN, WHITE, WHITE, lp, "#") != 1) {
			continue;
		}
		host2netname(netname, name, domain);
		if (wasprinted(netname)) {
			multdef(netname);
			continue;
		}
		(void) printf("%s 0:%.*s\n", netname, sizeof (name), name);
	}
}

/*
 * Open a file, exit on failure
 */
static FILE *
openfile(fname)
	char *fname;
{
	FILE *f;

	f = fopen(fname, "r");
	if (f == NULL) {
		(void) fprintf(stderr, "%s: can't open %s\n", cmdname, fname);
		exit(1);
	}
	return (f);
}

/*
 * Print syntax error message, then exit
 */
void
syntaxerror()
{
	(void) fprintf(stderr, "%s: syntax error in file \"%s\", line %d\n",
	    cmdname, curfile, curline);
	exit(1);
}


/*
 * an atoi() that prints a message and exits upong failure
 */
static int
Atoi(str)
	char *str;
{
	int res;

	if (!sscanf(str, "%d", &res)) {
		syntaxerror();
	}
	return (res);
}


/*
 * Attempt to get a token from a file, print a message and exit upon failure
 */
void
check_getname(lp, name, skip, term, com)
	char **lp;
	char *name;
	char *skip;
	char *term;
	char *com;
{
	if (getname(name, MAXNAMELEN, skip, term, lp, com) != 1) {
		syntaxerror();
	}
}

/*
 * Something was defined more than once
 */
void
multdef(name)
	char *name;
{
	if (!quietmode) {
		(void) fprintf(stderr,
			"%s: %s multiply defined, other definitions ignored\n",
			cmdname, name);
	}
}

static int
hash(str, size)
	unsigned char *str;
	int size;
{
	unsigned val;
	int flip;

	val = 0;
	flip = 0;
	while (*str) {
		if (flip) {
			val ^= (*str++ << 6);
		} else {
			val ^= *str++;
		}
		flip = !flip;
	}
	return (val % size);
}


/*
 * Check if an item has been printed
 * If not, store the item into the printed item table
 */
static int
wasprinted(name)
	char *name;
{
	struct string_list *s;
	int val;

	val = hash((unsigned char *) name, PRNTABSIZE);
	for (s = printed[val]; s != NULL && strcmp(s->str, name); s = s->next)
		;
	if (s != NULL) {
		return (1);
	}
	s = (struct string_list *)malloc(sizeof (struct string_list));
	s->str = malloc((unsigned)strlen(name) + 1);
	(void) strcpy(s->str, name);
	s->next = printed[val];
	printed[val] = s;
	return (0);
}

/*
 * Add gid to the list of a user's groups
 */
void
storegid(gid, user)
	int gid;
	char *user;
{
	struct group_list *g;
	int i;
	int val;

	val = hash((unsigned char *) user, GRPTABSIZE);
	for (g = groups[val]; g != NULL && strcmp(g->user, user); g = g->next)
		;
	if (g == NULL) {
		g = (struct group_list *)malloc(sizeof (struct group_list));
		g->user = malloc((unsigned)strlen(user) + 1);
		(void) strcpy(g->user, user);
		g->group_len = 1;
		g->groups[0] = gid;
		g->next = groups[val];
		groups[val] = g;
	} else {
		for (i = 0; i < g->group_len; i++) {
			if (g->groups[i] == gid) {
				return;
			}
		}
		if (g->group_len >= NUMGIDS) {
			(void) fprintf(stderr, "%s: %s's groups exceed %d\n",
				cmdname, user, NGROUPS_MAX);
			return;
		}
		g->groups[g->group_len++] = gid;
	}
}

/*
 * print out a user's groups
 */
void
printgroups(user, gid)
	char *user;
	int gid;
{
	struct group_list *g;
	int i;
	int val;

	val = hash((unsigned char *) user, GRPTABSIZE);
	for (g = groups[val]; g != NULL && strcmp(g->user, user); g = g->next)
		;
	put_d(gid);
	if (g != NULL) {
		for (i = 0; i < g->group_len; i++) {
			if (gid != g->groups[i]) {
				(void) putchar(',');
				put_d(g->groups[i]);
			}
		}
	}
	(void) putchar('\n');
}


/*
 * Parse command line arguments
 */
int
parseargs(argc, argv)
	int argc;
	char *argv[];
{
	int i;
	int j;
	static struct {
		char letter;
		char *standard;
		char **filename;
	} whattodo[] = {
		{ 'p', PASSWD, &pwdfile },
		{ 'g', GROUP, &grpfile },
		{ 'm', IDMAP, &mapfile },
		{ 'h', HOSTS, &hostfile }
	};

#define	TABSIZE  sizeof (whattodo)/sizeof (whattodo[0])

	for (i = 1; i < argc; i++) {
		if (argv[i][0] == '-') {
			if (argv[i][2] != 0) {
				return (0);
			}
			if (argv[i][1] == 'q') {
				quietmode = 1;
				continue;
			}
			for (j = 0; j < TABSIZE; j++) {
				if (whattodo[j].letter == argv[i][1]) {
					if (*whattodo[j].filename !=
							whattodo[j].standard) {
						return (0);
					}
					if (++i == argc) {
						return (0);
					}
					*whattodo[j].filename = argv[i];
					break;
				}
			}
			if (j == TABSIZE) {
				return (0);
			}
		}
	}
	return (1);
}

/*
 * Print a string, quickly
 */
void
put_s(s)
	char *s;
{
	(void) fwrite(s, strlen(s), 1, stdout);
}

/*
 * Print an integer, quickly
 */
void
put_d(d)
	int d;
{
	char buf[20];
	char *p;

	if (d == 0) {
		(void) putchar('0');
		return;
	}
	if (d < 0) {
		(void) putchar('-');
		d = -d;
	}
	p = buf + sizeof (buf);
	*--p = 0;
	while (d > 0) {
		*--p = (d % 10) + '0';
		d /= 10;
	}
	put_s(p);
}