xref: /titanic_44/usr/src/cmd/ypcmd/revnetgroup/getgroup.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate /*
24*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1996, by Sun Microsystems, Inc.
25*7c478bd9Sstevel@tonic-gate  * All rights reserved.
26*7c478bd9Sstevel@tonic-gate  */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SMI4.1 1.5 */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <ctype.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include "table.h"
34*7c478bd9Sstevel@tonic-gate #include "util.h"
35*7c478bd9Sstevel@tonic-gate #include "getgroup.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #define	MAXGROUPLEN 1024
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * Stolen mostly, from getnetgrent.c
41*7c478bd9Sstevel@tonic-gate  *
42*7c478bd9Sstevel@tonic-gate  * my_getgroup() performs the same function as _getgroup(), but operates
43*7c478bd9Sstevel@tonic-gate  * on /etc/netgroup directly, rather than doing yp lookups.
44*7c478bd9Sstevel@tonic-gate  *
45*7c478bd9Sstevel@tonic-gate  * /etc/netgroup must first loaded into a hash table so the matching
46*7c478bd9Sstevel@tonic-gate  * function can look up lines quickly.
47*7c478bd9Sstevel@tonic-gate  */
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /* To check for cycles in netgroups */
51*7c478bd9Sstevel@tonic-gate struct list {
52*7c478bd9Sstevel@tonic-gate 	char *name;
53*7c478bd9Sstevel@tonic-gate 	struct list *nxt;
54*7c478bd9Sstevel@tonic-gate };
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate extern stringtable ngtable; /* stored info from /etc/netgroup */
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate static struct grouplist *grouplist; /* stores a list of users in a group */
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate static char *any();
62*7c478bd9Sstevel@tonic-gate static char *match();
63*7c478bd9Sstevel@tonic-gate static char *fill();
64*7c478bd9Sstevel@tonic-gate static void freegrouplist();
65*7c478bd9Sstevel@tonic-gate static void doit();
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate static void
freegrouplist()70*7c478bd9Sstevel@tonic-gate freegrouplist()
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	struct grouplist *gl;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	for (gl = grouplist; gl != NULL; gl = gl->gl_nxt) {
75*7c478bd9Sstevel@tonic-gate 		FREE(gl->gl_name);
76*7c478bd9Sstevel@tonic-gate 		FREE(gl->gl_domain);
77*7c478bd9Sstevel@tonic-gate 		FREE(gl->gl_machine);
78*7c478bd9Sstevel@tonic-gate 		FREE(gl);
79*7c478bd9Sstevel@tonic-gate 	}
80*7c478bd9Sstevel@tonic-gate 	grouplist = NULL;
81*7c478bd9Sstevel@tonic-gate }
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate struct grouplist *
my_getgroup(group)87*7c478bd9Sstevel@tonic-gate my_getgroup(group)
88*7c478bd9Sstevel@tonic-gate 	char *group;
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate 	freegrouplist();
91*7c478bd9Sstevel@tonic-gate 	doit(group, (struct list *) NULL);
92*7c478bd9Sstevel@tonic-gate 	return (grouplist);
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * recursive function to find the members of netgroup "group". "list" is
101*7c478bd9Sstevel@tonic-gate  * the path followed through the netgroups so far, to check for cycles.
102*7c478bd9Sstevel@tonic-gate  */
103*7c478bd9Sstevel@tonic-gate static void
doit(group,list)104*7c478bd9Sstevel@tonic-gate doit(group, list)
105*7c478bd9Sstevel@tonic-gate 	char *group;
106*7c478bd9Sstevel@tonic-gate 	struct list *list;
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	register char *p, *q;
109*7c478bd9Sstevel@tonic-gate 	register struct list *ls;
110*7c478bd9Sstevel@tonic-gate 	struct list tmplist;
111*7c478bd9Sstevel@tonic-gate 	char *val;
112*7c478bd9Sstevel@tonic-gate 	struct grouplist *gpls;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	/*
116*7c478bd9Sstevel@tonic-gate 	 * check for non-existing groups
117*7c478bd9Sstevel@tonic-gate 	 */
118*7c478bd9Sstevel@tonic-gate 	if ((val = match(group)) == NULL) {
119*7c478bd9Sstevel@tonic-gate 		return;
120*7c478bd9Sstevel@tonic-gate 	}
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	/*
124*7c478bd9Sstevel@tonic-gate 	 * check for cycles
125*7c478bd9Sstevel@tonic-gate 	 */
126*7c478bd9Sstevel@tonic-gate 	for (ls = list; ls != NULL; ls = ls->nxt) {
127*7c478bd9Sstevel@tonic-gate 		if (strcmp(ls->name, group) == 0) {
128*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
129*7c478bd9Sstevel@tonic-gate 				"Cycle detected in /etc/netgroup: %s.\n",
130*7c478bd9Sstevel@tonic-gate 				group);
131*7c478bd9Sstevel@tonic-gate 			return;
132*7c478bd9Sstevel@tonic-gate 		}
133*7c478bd9Sstevel@tonic-gate 	}
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	ls = &tmplist;
137*7c478bd9Sstevel@tonic-gate 	ls->name = group;
138*7c478bd9Sstevel@tonic-gate 	ls->nxt = list;
139*7c478bd9Sstevel@tonic-gate 	list = ls;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	p = val;
142*7c478bd9Sstevel@tonic-gate 	while (p != NULL) {
143*7c478bd9Sstevel@tonic-gate 		while (*p == ' ' || *p == '\t')
144*7c478bd9Sstevel@tonic-gate 			p++;
145*7c478bd9Sstevel@tonic-gate 		if (*p == EOS || *p == '#')
146*7c478bd9Sstevel@tonic-gate 			break;
147*7c478bd9Sstevel@tonic-gate 		if (*p == '(') {
148*7c478bd9Sstevel@tonic-gate 			gpls = MALLOC(struct grouplist);
149*7c478bd9Sstevel@tonic-gate 			p++;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 			if (!(p = fill(p, &gpls->gl_machine, ',')))  {
152*7c478bd9Sstevel@tonic-gate 				goto syntax_error;
153*7c478bd9Sstevel@tonic-gate 			}
154*7c478bd9Sstevel@tonic-gate 			if (!(p = fill(p, &gpls->gl_name, ','))) {
155*7c478bd9Sstevel@tonic-gate 				goto syntax_error;
156*7c478bd9Sstevel@tonic-gate 			}
157*7c478bd9Sstevel@tonic-gate 			if (!(p = fill(p, &gpls->gl_domain, ')'))) {
158*7c478bd9Sstevel@tonic-gate 				goto syntax_error;
159*7c478bd9Sstevel@tonic-gate 			}
160*7c478bd9Sstevel@tonic-gate 			gpls->gl_nxt = grouplist;
161*7c478bd9Sstevel@tonic-gate 			grouplist = gpls;
162*7c478bd9Sstevel@tonic-gate 		} else {
163*7c478bd9Sstevel@tonic-gate 			q = any(p, " \t\n#");
164*7c478bd9Sstevel@tonic-gate 			if (q && *q == '#')
165*7c478bd9Sstevel@tonic-gate 				break;
166*7c478bd9Sstevel@tonic-gate 			*q = EOS;
167*7c478bd9Sstevel@tonic-gate 			doit(p, list);
168*7c478bd9Sstevel@tonic-gate 			*q = ' ';
169*7c478bd9Sstevel@tonic-gate 		}
170*7c478bd9Sstevel@tonic-gate 		p = any(p, " \t");
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 	return;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate syntax_error:
175*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "syntax error in /etc/netgroup\n");
176*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "--- %s %s\n", group, val);
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate /*
183*7c478bd9Sstevel@tonic-gate  * Fill a buffer "target" selectively from buffer "start".
184*7c478bd9Sstevel@tonic-gate  * "termchar" terminates the information in start, and preceding
185*7c478bd9Sstevel@tonic-gate  * or trailing white space is ignored.  If the buffer "start" is
186*7c478bd9Sstevel@tonic-gate  * empty, "target" is filled with "*". The location just after the
187*7c478bd9Sstevel@tonic-gate  * terminating character is returned.
188*7c478bd9Sstevel@tonic-gate  */
189*7c478bd9Sstevel@tonic-gate static char *
fill(start,target,termchar)190*7c478bd9Sstevel@tonic-gate fill(start, target, termchar)
191*7c478bd9Sstevel@tonic-gate 	char *start;
192*7c478bd9Sstevel@tonic-gate 	char **target;
193*7c478bd9Sstevel@tonic-gate 	char termchar;
194*7c478bd9Sstevel@tonic-gate {
195*7c478bd9Sstevel@tonic-gate 	register char *p;
196*7c478bd9Sstevel@tonic-gate 	register char *q;
197*7c478bd9Sstevel@tonic-gate 	register char *r;
198*7c478bd9Sstevel@tonic-gate 	int size;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	for (p = start; *p == ' ' || *p == '\t'; p++)
201*7c478bd9Sstevel@tonic-gate 		;
202*7c478bd9Sstevel@tonic-gate 	r = strchr(p, termchar);
203*7c478bd9Sstevel@tonic-gate 	if (r == (char *)NULL) {
204*7c478bd9Sstevel@tonic-gate 		return ((char *)NULL);
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 	if (p == r) {
207*7c478bd9Sstevel@tonic-gate 		*target = NULL;
208*7c478bd9Sstevel@tonic-gate 	} else {
209*7c478bd9Sstevel@tonic-gate 		for (q = r-1; *q == ' ' || *q == '\t'; q--)
210*7c478bd9Sstevel@tonic-gate 			;
211*7c478bd9Sstevel@tonic-gate 		size = q-p+1;
212*7c478bd9Sstevel@tonic-gate 		STRNCPY(*target, p, size);
213*7c478bd9Sstevel@tonic-gate 	}
214*7c478bd9Sstevel@tonic-gate 	return (r+1);
215*7c478bd9Sstevel@tonic-gate }
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate /*
219*7c478bd9Sstevel@tonic-gate  * scans cp, looking for a match with any character
220*7c478bd9Sstevel@tonic-gate  * in match.  Returns pointer to place in cp that matched
221*7c478bd9Sstevel@tonic-gate  * (or NULL if no match)
222*7c478bd9Sstevel@tonic-gate  */
223*7c478bd9Sstevel@tonic-gate static char *
any(cp,match)224*7c478bd9Sstevel@tonic-gate any(cp, match)
225*7c478bd9Sstevel@tonic-gate 	register char *cp;
226*7c478bd9Sstevel@tonic-gate 	char *match;
227*7c478bd9Sstevel@tonic-gate {
228*7c478bd9Sstevel@tonic-gate 	register char *mp, c;
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	while (c = *cp) {
231*7c478bd9Sstevel@tonic-gate 		for (mp = match; *mp; mp++)
232*7c478bd9Sstevel@tonic-gate 			if (*mp == c)
233*7c478bd9Sstevel@tonic-gate 				return (cp);
234*7c478bd9Sstevel@tonic-gate 		cp++;
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate 	return (NULL);
237*7c478bd9Sstevel@tonic-gate }
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate /*
242*7c478bd9Sstevel@tonic-gate  * The equivalent of yp_match. Returns the match, or NULL if there is none.
243*7c478bd9Sstevel@tonic-gate  */
244*7c478bd9Sstevel@tonic-gate static char *
match(group)245*7c478bd9Sstevel@tonic-gate match(group)
246*7c478bd9Sstevel@tonic-gate 	char *group;
247*7c478bd9Sstevel@tonic-gate {
248*7c478bd9Sstevel@tonic-gate 	return (lookup(ngtable, group));
249*7c478bd9Sstevel@tonic-gate }
250