summaryrefslogtreecommitdiff
path: root/usr/src/lib/libbc/libc/gen/common/getgraent.c
blob: 006a30355af027409ff83891ed86d2f434745cad (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 1990 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <grp.h>
#include <grpadj.h>
#include <rpcsvc/ypclnt.h>
#include <string.h>
#include <malloc.h>

extern void rewind();
extern long strtol();
extern int fclose();

void	setgraent(void);
void	endgraent(void);

static struct gradata {
	char	*domain;
	FILE	*grfa;
	char	*yp;
	int	yplen;
	char	*oldyp;
	int	oldyplen;
	struct list {
		char *name;
		struct list *nxt;
	} *minuslist;			/* list of - items */
	struct	group_adjunct interpgra;
	char	interpline[BUFSIZ+1];
	struct	group_adjunct *sv;
} *gradata, *_gradata(void);

static char *GROUPADJ = "/etc/security/group.adjunct";

static struct group_adjunct	*interpret(char *, int);
static struct group_adjunct	*interpretwithsave(char *, int,
    struct group_adjunct *);
static struct group_adjunct	*save(struct group_adjunct *);
static struct group_adjunct	*getnamefromyellow(char *,
    struct group_adjunct *);
static int	onminuslist(struct group_adjunct *);
static int	matchname(char [], struct group_adjunct **, char *);
static void	freeminuslist(void);
static void	getnextfromyellow(void);
static void	getfirstfromyellow(void);
static void	addtominuslist(char *);


static struct gradata *
_gradata(void)
{
	struct gradata *g = gradata;

	if (g == 0) {
		g = (struct gradata *)calloc(1, sizeof (struct gradata));
		gradata = g;
	}
	return (g);
}

struct group_adjunct *
getgranam(char *name)
{
	struct gradata *g = _gradata();
	struct group_adjunct *gra;
	char line[BUFSIZ+1];

	setgraent();
	if (g == 0)
		return (0);
	if (!g->grfa)
		return (NULL);
	while (fgets(line, BUFSIZ, g->grfa) != NULL) {
		if ((gra = interpret(line, strlen(line))) == NULL)
			continue;
		if (matchname(line, &gra, name)) {
			endgraent();
			return (gra);
		}
	}
	endgraent();
	return (NULL);
}

void
setgraent(void)
{
	struct gradata *g = _gradata();

	if (g == NULL)
		return;
	if (g->domain == NULL)
		(void) yp_get_default_domain(&g->domain);
	if (!g->grfa)
		g->grfa = fopen(GROUPADJ, "r");
	else
		rewind(g->grfa);
	if (g->yp)
		free(g->yp);
	g->yp = NULL;
	freeminuslist();
}

void
endgraent(void)
{
	struct gradata *g = _gradata();

	if (g == 0)
		return;
	if (g->grfa) {
		(void) fclose(g->grfa);
		g->grfa = NULL;
	}
	if (g->yp)
		free(g->yp);
	g->yp = NULL;
	freeminuslist();
}

struct group_adjunct *
fgetgraent(FILE *f)
{
	char line1[BUFSIZ+1];

	if(fgets(line1, BUFSIZ, f) == NULL)
		return (NULL);
	return (interpret(line1, strlen(line1)));
}

static char *
grskip(char *p, int c)
{
	while(*p && *p != c && *p != '\n') ++p;
	if (*p == '\n')
		*p = '\0';
	else if (*p != '\0')
		*p++ = '\0';
	return (p);
}

struct group_adjunct *
getgraent(void)
{
	struct gradata *g = _gradata();
	char line1[BUFSIZ+1];
	static struct group_adjunct *savegra;
	struct group_adjunct *gra;

	if (g == 0)
		return (0);
	if (g->domain == NULL) {
		(void) yp_get_default_domain(&g->domain);
	}
	if(!g->grfa && !(g->grfa = fopen(GROUPADJ, "r")))
		return (NULL);
  again:
	if (g->yp) {
		gra = interpretwithsave(g->yp, g->yplen, savegra);
		free(g->yp);
		if (gra == NULL)
			return (NULL);
		getnextfromyellow();
		if (onminuslist(gra))
			goto again;
		else
			return (gra);
	}
	else if (fgets(line1, BUFSIZ, g->grfa) == NULL)
		return (NULL);
	if ((gra = interpret(line1, strlen(line1))) == NULL)
		return (NULL);
	switch(line1[0]) {
		case '+':
			if (strcmp(gra->gra_name, "+") == 0) {
				getfirstfromyellow();
				savegra = save(gra);
				goto again;
			}
			/* 
			 * else look up this entry in NIS
			 */
			savegra = save(gra);
			gra = getnamefromyellow(gra->gra_name+1, savegra);
			if (gra == NULL)
				goto again;
			else if (onminuslist(gra))
				goto again;
			else
				return (gra);
			break;
		case '-':
			addtominuslist(gra->gra_name+1);
			goto again;
			break;
		default:
			if (onminuslist(gra))
				goto again;
			return (gra);
			break;
	}
	/* NOTREACHED */
}

static struct group_adjunct *
interpret(char *val, int len)
{
	struct gradata *g = _gradata();
	char *p;

	if (g == 0)
		return (0);
	strncpy(g->interpline, val, len);
	p = g->interpline;
	g->interpline[len] = '\n';
	g->interpline[len+1] = 0;
	g->interpgra.gra_name = p;
	p = grskip(p,':');
        if (strcmp(g->interpgra.gra_name, "+") == 0) {
                /* we are going to the NIS - fix the
                 * rest of the struct as much as is needed
                 */
                g->interpgra.gra_passwd = "";
		return (&g->interpgra);
        }
	g->interpgra.gra_passwd = p;
        while(*p && *p != '\n') p++;
        *p = '\0';
	return (&g->interpgra);
}

static void
freeminuslist(void)
{
	struct gradata *g = _gradata();
	struct list *ls;
	
	if (g == 0)
		return;
	for (ls = g->minuslist; ls != NULL; ls = ls->nxt) {
		free(ls->name);
		free(ls);
	}
	g->minuslist = NULL;
}

static struct group_adjunct *
interpretwithsave(char *val, int len, struct group_adjunct *savegra)
{
	struct gradata *g = _gradata();
	struct group_adjunct *gra;
	
	if (g == 0)
		return (0);
	if ((gra = interpret(val, len)) == NULL)
		return (NULL);
	if (savegra->gra_passwd && *savegra->gra_passwd)
		gra->gra_passwd =  savegra->gra_passwd;
	return (gra);
}

static int
onminuslist(struct group_adjunct *gra)
{
	struct gradata *g = _gradata();
	struct list *ls;
	char *nm;
	
	if (g == 0)
		return (0);
	nm = gra->gra_name;
	for (ls = g->minuslist; ls != NULL; ls = ls->nxt)
		if (strcmp(ls->name, nm) == 0)
			return (1);
	return (0);
}

static void
getnextfromyellow(void)
{
	struct gradata *g = _gradata();
	int reason;
	char *key = NULL;
	int keylen;
	
	if (g == 0)
		return;
	if (reason = yp_next(g->domain, "group.adjunct.byname",
	    g->oldyp, g->oldyplen, &key, &keylen,
	    &g->yp, &g->yplen)) {
#ifdef DEBUG
fprintf(stderr, "reason yp_next failed is %d\n", reason);
#endif
		g->yp = NULL;
	}
	if (g->oldyp)
		free(g->oldyp);
	g->oldyp = key;
	g->oldyplen = keylen;
}

static void
getfirstfromyellow(void)
{
	struct gradata *g = _gradata();
	int reason;
	char *key = NULL;
	int keylen;
	
	if (g == 0)
		return;
	if (reason =  yp_first(g->domain, "group.adjunct.byname",
	    &key, &keylen, &g->yp, &g->yplen)) {
#ifdef DEBUG
fprintf(stderr, "reason yp_first failed is %d\n", reason);
#endif
		g->yp = NULL;
	}
	if (g->oldyp)
		free(g->oldyp);
	g->oldyp = key;
	g->oldyplen = keylen;
}

static struct group_adjunct *
getnamefromyellow(char *name, struct group_adjunct *savegra)
{
	struct gradata *g = _gradata();
	struct group_adjunct *gra;
	int reason;
	char *val;
	int vallen;
	
	if (g == 0)
		return (NULL);
	if (reason = yp_match(g->domain, "group.adjunct.byname",
	    name, strlen(name), &val, &vallen)) {
#ifdef DEBUG
fprintf(stderr, "reason yp_next failed is %d\n", reason);
#endif
		return (NULL);
	}
	else {
		gra = interpret(val, vallen);
		free(val);
		if (gra == NULL)
			return (NULL);
		if (savegra->gra_passwd && *savegra->gra_passwd)
			gra->gra_passwd =  savegra->gra_passwd;
		return (gra);
	}
}

static void
addtominuslist(char *name)
{
	struct gradata *g = _gradata();
	struct list *ls;
	char *buf;
	
	if (g == 0)
		return;
	ls = (struct list *)malloc(sizeof(struct list));
	buf = (char *)malloc(strlen(name) + 1);
	(void) strcpy(buf, name);
	ls->name = buf;
	ls->nxt = g->minuslist;
	g->minuslist = ls;
}

/* 
 * save away psswd field, which is the only
 * one which can be specified in a local + entry to override the
 * value in the NIS
 */
static struct group_adjunct *
save(struct group_adjunct *gra)
{
	struct gradata *g = _gradata();
	
	if (g == 0)
		return (0);
	/* 
	 * free up stuff from last time around
	 */
	if (g->sv) {
		free(g->sv->gra_passwd);
		free(g->sv);
	}
	g->sv = (struct group_adjunct *)calloc(1, sizeof(struct group_adjunct));
	g->sv->gra_passwd = (char *)malloc(strlen(gra->gra_passwd) + 1);
	(void) strcpy(g->sv->gra_passwd, gra->gra_passwd);
	return (g->sv);
}

static int
matchname(char line1[], struct group_adjunct **grap, char *name)
{
	struct group_adjunct *savegra;
	struct group_adjunct *gra = *grap;

	switch (line1[0]) {
		case '+':
			if (strcmp(gra->gra_name, "+") == 0) {
				savegra = save(gra);
				gra = getnamefromyellow(name, savegra);
				if (gra) {
					*grap = gra;
					return (1);
				}
				else
					return (0);
			}
			if (strcmp(gra->gra_name+1, name) == 0) {
				savegra = save(gra);
				gra = getnamefromyellow(gra->gra_name+1, savegra);
				if (gra) {
					*grap = gra;
					return (1);
				}
				else
					return (0);
			}
			break;
		case '-':
			if (strcmp(gra->gra_name+1, name) == 0) {
				*grap = NULL;
				return (1);
			}
			break;
		default:
			if (strcmp(gra->gra_name, name) == 0)
				return (1);
	}
	return (0);
}