Libinfo-503.50.4.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;
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
63 char *NETGROUP = "netgroup";
64 /*
65 * recursive function to find the members of netgroup "group". "list" is
66 * the path followed through the netgroups so far, to check for cycles.
67 */
68 static void
69 doit(group,list)
70 char *group;
71 struct list *list;
72 {
73 register char *p, *q;
74 register struct list *ls;
75 struct list this_group;
76 char *val;
77 struct grouplist *gpls;
78
79 /*
80 * check for non-existing groups
81 */
82 if ((val = match(group)) == NULL)
83 return;
84
85 /*
86 * check for cycles
87 */
88 for (ls = list; ls != NULL; ls = ls->nxt)
89 if (strcmp(ls->name, group) == 0) {
90 (void) fprintf(stderr,
91 "Cycle detected in /etc/netgroup: %s.\n", group);
92 return;
93 }
94
95 ls = &this_group;
96 ls->name = group;
97 ls->nxt = list;
98 list = ls;
99
100 p = val;
101 while (p != NULL) {
102 while (*p == ' ' || *p == '\t')
103 p++;
104 if (*p == 0 || *p =='#')
105 break;
106 if (*p == '(') {
107 gpls = (struct grouplist *)
108 malloc(sizeof(struct grouplist));
109 if (gpls == NULL) return;
110 p++;
111 if (!(p = fill(p,&gpls->gl_machine,',')))
112 goto syntax_error;
113 if (!(p = fill(p,&gpls->gl_name,',')))
114 goto syntax_error;
115 if (!(p = fill(p,&gpls->gl_domain,')')))
116 goto syntax_error;
117 gpls->gl_nxt = grouplist;
118 grouplist = gpls;
119 } else {
120 q = strpbrk(p, " \t\n#");
121 if (q == NULL || *q == '#')
122 break;
123 *q = 0;
124 doit(p,list);
125 *q = ' ';
126 }
127 p = strpbrk(p, " \t");
128 }
129 return;
130
131 syntax_error:
132 free(gpls);
133 (void) fprintf(stderr,"syntax error in /etc/netgroup\n");
134 (void) fprintf(stderr,"--- %s\n",val);
135 return;
136 }
137
138 /*
139 * Fill a buffer "target" selectively from buffer "start".
140 * "termchar" terminates the information in start, and preceding
141 * or trailing white space is ignored. The location just after the
142 * terminating character is returned.
143 */
144 static char *
145 fill(start,target,termchar)
146 char *start, **target, termchar;
147 {
148 register char *p, *q;
149 char *r;
150 unsigned size;
151
152 for (p = start; *p == ' ' || *p == '\t'; p++)
153 ;
154 r = index(p, termchar);
155 if (r == NULL)
156 return (NULL);
157 if (p == r)
158 *target = NULL;
159 else {
160 for (q = r-1; *q == ' ' || *q == '\t'; q--)
161 ;
162 size = q - p + 1;
163 *target = malloc(size+1);
164 if (*target == NULL) return NULL;
165 (void) strncpy(*target,p,(int) size);
166 (*target)[size] = 0;
167 }
168 return (r+1);
169 }
170
171 static char *
172 match(group)
173 char *group;
174 {
175 char *val;
176 int vallen;
177
178 if (domain == NULL)
179 (void) yp_get_default_domain(&domain );
180 if (yp_match(domain, NETGROUP, group, strlen(group), &val, &vallen))
181 return (NULL);
182 return (val);
183 }