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