]> git.saurik.com Git - apple/libinfo.git/blob - nis.subproj/getnetgrent.c
Libinfo-477.40.5.tar.gz
[apple/libinfo.git] / nis.subproj / getnetgrent.c
1 /*
2 * Copyright (c) 1999-2015 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 #if !defined(lint) && defined(SCCSIDS)
25 static char sccsid[] = "@(#)getnetgrent.c 1.2 90/07/20 4.1NFSSRC; from 1.22 88/02/08 Copyr 1985 Sun Micro";
26 #endif
27
28 /*
29 * Copyright (c) 1985 by Sun Microsystems, Inc.
30 */
31
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <rpcsvc/ypclnt.h>
37
38 #define MAXGROUPLEN 1024
39
40 /*
41 * access members of a netgroup
42 */
43
44 static struct grouplist { /* also used by pwlib */
45 char *gl_machine;
46 char *gl_name;
47 char *gl_domain;
48 struct grouplist *gl_nxt;
49 } *grouplist, *grlist;
50
51
52 struct list { /* list of names to check for loops */
53 char *name;
54 struct list *nxt;
55 };
56
57 static void doit();
58 static char *fill();
59 static char *match();
60
61 static char *domain;
62 static char *oldgrp;
63
64 char *NETGROUP = "netgroup";
65
66 void _old_endnetgrent(void);
67 void _old_setnetgrent(char *);
68
69 void _old_setnetgrent(grp)
70 char *grp;
71 {
72
73 if (oldgrp == NULL)
74 oldgrp = (char *)calloc(1,256);
75 if (oldgrp == NULL) return;
76 if (strcmp(oldgrp, grp) == 0)
77 grlist = grouplist;
78 else {
79 if (grouplist != NULL)
80 _old_endnetgrent();
81 doit(grp, (struct list *) NULL);
82 grlist = grouplist;
83 (void) strcpy(oldgrp, grp);
84 }
85 }
86
87 void _old_endnetgrent()
88 {
89 register struct grouplist *gl;
90
91 for (gl = grouplist; gl != NULL; gl = gl->gl_nxt) {
92 if (gl->gl_name)
93 free(gl->gl_name);
94 if (gl->gl_domain)
95 free(gl->gl_domain);
96 if (gl->gl_machine)
97 free(gl->gl_machine);
98 free((char *) gl);
99 }
100 grouplist = NULL;
101 grlist = NULL;
102 if (oldgrp) {
103 free(oldgrp);
104 oldgrp = 0;
105 }
106 }
107
108 int _old_getnetgrent(machinep, namep, domainp)
109 char **machinep, **namep, **domainp;
110 {
111
112 if (grlist == 0)
113 return (0);
114 *machinep = grlist->gl_machine;
115 *namep = grlist->gl_name;
116 *domainp = grlist->gl_domain;
117 grlist = grlist->gl_nxt;
118 return (1);
119 }
120
121 /*
122 * recursive function to find the members of netgroup "group". "list" is
123 * the path followed through the netgroups so far, to check for cycles.
124 */
125 static void
126 doit(group,list)
127 char *group;
128 struct list *list;
129 {
130 register char *p, *q;
131 register struct list *ls;
132 struct list this_group;
133 char *val;
134 struct grouplist *gpls;
135
136 /*
137 * check for non-existing groups
138 */
139 if ((val = match(group)) == NULL)
140 return;
141
142 /*
143 * check for cycles
144 */
145 for (ls = list; ls != NULL; ls = ls->nxt)
146 if (strcmp(ls->name, group) == 0) {
147 (void) fprintf(stderr,
148 "Cycle detected in /etc/netgroup: %s.\n", group);
149 return;
150 }
151
152 ls = &this_group;
153 ls->name = group;
154 ls->nxt = list;
155 list = ls;
156
157 p = val;
158 while (p != NULL) {
159 while (*p == ' ' || *p == '\t')
160 p++;
161 if (*p == 0 || *p =='#')
162 break;
163 if (*p == '(') {
164 gpls = (struct grouplist *)
165 malloc(sizeof(struct grouplist));
166 if (gpls == NULL) return;
167 p++;
168 if (!(p = fill(p,&gpls->gl_machine,',')))
169 goto syntax_error;
170 if (!(p = fill(p,&gpls->gl_name,',')))
171 goto syntax_error;
172 if (!(p = fill(p,&gpls->gl_domain,')')))
173 goto syntax_error;
174 gpls->gl_nxt = grouplist;
175 grouplist = gpls;
176 } else {
177 q = strpbrk(p, " \t\n#");
178 if (q && *q == '#')
179 break;
180 *q = 0;
181 doit(p,list);
182 *q = ' ';
183 }
184 p = strpbrk(p, " \t");
185 }
186 return;
187
188 syntax_error:
189 (void) fprintf(stderr,"syntax error in /etc/netgroup\n");
190 (void) fprintf(stderr,"--- %s\n",val);
191 return;
192 }
193
194 /*
195 * Fill a buffer "target" selectively from buffer "start".
196 * "termchar" terminates the information in start, and preceding
197 * or trailing white space is ignored. The location just after the
198 * terminating character is returned.
199 */
200 static char *
201 fill(start,target,termchar)
202 char *start, **target, termchar;
203 {
204 register char *p, *q;
205 char *r;
206 unsigned size;
207
208 for (p = start; *p == ' ' || *p == '\t'; p++)
209 ;
210 r = index(p, termchar);
211 if (r == NULL)
212 return (NULL);
213 if (p == r)
214 *target = NULL;
215 else {
216 for (q = r-1; *q == ' ' || *q == '\t'; q--)
217 ;
218 size = q - p + 1;
219 *target = malloc(size+1);
220 if (*target == NULL) return NULL;
221 (void) strncpy(*target,p,(int) size);
222 (*target)[size] = 0;
223 }
224 return (r+1);
225 }
226
227 static char *
228 match(group)
229 char *group;
230 {
231 char *val;
232 int vallen;
233
234 if (domain == NULL)
235 (void) yp_get_default_domain(&domain );
236 if (yp_match(domain, NETGROUP, group, strlen(group), &val, &vallen))
237 return (NULL);
238 return (val);
239 }