]>
Commit | Line | Data |
---|---|---|
0ecb606c JJ |
1 | /* Copyright (C) 1996, 1997, 1998, 1999, 2002, 2004, 2005 |
2 | Free Software Foundation, Inc. | |
afd4eb37 | 3 | This file is part of the GNU C Library. |
a68b0d31 | 4 | |
afd4eb37 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
a68b0d31 | 9 | |
afd4eb37 UD |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
a68b0d31 | 14 | |
41bdb6e2 AJ |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
a68b0d31 | 19 | |
0ecb606c | 20 | #include <assert.h> |
5107cf1d | 21 | #include <bits/libc-lock.h> |
6591c335 | 22 | #include <errno.h> |
a68b0d31 | 23 | #include <netdb.h> |
afd4eb37 | 24 | #include <stdlib.h> |
2303f5fd | 25 | #include <string.h> |
a68b0d31 UD |
26 | #include "netgroup.h" |
27 | #include "nsswitch.h" | |
28 | ||
29 | ||
30 | /* Protect above variable against multiple uses at the same time. */ | |
31 | __libc_lock_define_initialized (static, lock) | |
32 | ||
2303f5fd UD |
33 | /* The whole information for the set/get/endnetgrent functions are |
34 | kept in this structure. */ | |
35 | static struct __netgrent dataset; | |
11336c16 | 36 | |
a68b0d31 | 37 | /* The lookup function for the first entry of this service. */ |
0ecb606c | 38 | extern int __nss_netgroup_lookup (service_user **nipp, const char *name, |
f7ddf3d3 | 39 | void **fctp) internal_function; |
a68b0d31 | 40 | |
0ecb606c | 41 | /* Set up NIP to run through the services. Return nonzero if there are no |
a68b0d31 | 42 | services (left). */ |
0ecb606c JJ |
43 | static int |
44 | setup (void **fctp, service_user **nipp) | |
a68b0d31 | 45 | { |
2303f5fd | 46 | /* Remember the first service_entry, it's always the same. */ |
390500b1 | 47 | static service_user *startp; |
a68b0d31 | 48 | int no_more; |
2303f5fd | 49 | |
a68b0d31 UD |
50 | if (startp == NULL) |
51 | { | |
8500b0ca UD |
52 | /* Executing this more than once at the same time must yield the |
53 | same result every time. So we need no locking. */ | |
0ecb606c | 54 | no_more = __nss_netgroup_lookup (nipp, "setnetgrent", fctp); |
8500b0ca | 55 | startp = no_more ? (service_user *) -1 : *nipp; |
a68b0d31 UD |
56 | } |
57 | else if (startp == (service_user *) -1) | |
58 | /* No services at all. */ | |
59 | return 1; | |
60 | else | |
61 | { | |
0ecb606c JJ |
62 | /* Reset to the beginning of the service list. */ |
63 | *nipp = startp; | |
a68b0d31 | 64 | /* Look up the first function. */ |
0ecb606c | 65 | no_more = __nss_lookup (nipp, "setnetgrent", fctp); |
a68b0d31 UD |
66 | } |
67 | return no_more; | |
68 | } | |
69 | \f | |
11336c16 UD |
70 | /* Free used memory. */ |
71 | static void | |
2303f5fd | 72 | free_memory (struct __netgrent *data) |
11336c16 | 73 | { |
2303f5fd | 74 | while (data->known_groups != NULL) |
11336c16 | 75 | { |
2303f5fd UD |
76 | struct name_list *tmp = data->known_groups; |
77 | data->known_groups = data->known_groups->next; | |
11336c16 UD |
78 | free (tmp); |
79 | } | |
80 | ||
2303f5fd | 81 | while (data->needed_groups != NULL) |
11336c16 | 82 | { |
2303f5fd UD |
83 | struct name_list *tmp = data->needed_groups; |
84 | data->needed_groups = data->needed_groups->next; | |
11336c16 UD |
85 | free (tmp); |
86 | } | |
87 | } | |
88 | \f | |
0ecb606c JJ |
89 | static void |
90 | endnetgrent_hook (struct __netgrent *datap) | |
91 | { | |
92 | enum nss_status (*endfct) (struct __netgrent *); | |
93 | ||
94 | if (datap->nip == NULL) | |
95 | return; | |
96 | ||
97 | endfct = __nss_lookup_function (datap->nip, "endnetgrent"); | |
98 | if (endfct != NULL) | |
99 | (void) (*endfct) (datap); | |
100 | datap->nip = NULL; | |
101 | } | |
102 | ||
11336c16 | 103 | static int |
dfd2257a | 104 | internal_function |
d71b808a UD |
105 | __internal_setnetgrent_reuse (const char *group, struct __netgrent *datap, |
106 | int *errnop) | |
a68b0d31 | 107 | { |
fb776f3e AJ |
108 | union |
109 | { | |
110 | enum nss_status (*f) (const char *, struct __netgrent *); | |
111 | void *ptr; | |
112 | } fct; | |
a68b0d31 | 113 | enum nss_status status = NSS_STATUS_UNAVAIL; |
11336c16 | 114 | struct name_list *new_elem; |
a68b0d31 | 115 | |
0ecb606c JJ |
116 | /* Free data from previous service. */ |
117 | endnetgrent_hook (datap); | |
118 | ||
a68b0d31 | 119 | /* Cycle through all the services and run their setnetgrent functions. */ |
0ecb606c | 120 | int no_more = setup (&fct.ptr, &datap->nip); |
a68b0d31 UD |
121 | while (! no_more) |
122 | { | |
0ecb606c JJ |
123 | assert (datap->data == NULL); |
124 | ||
11336c16 | 125 | /* Ignore status, we force check in `__nss_next'. */ |
fb776f3e | 126 | status = (*fct.f) (group, datap); |
a68b0d31 | 127 | |
0ecb606c | 128 | service_user *old_nip = datap->nip; |
8500b0ca | 129 | no_more = __nss_next (&datap->nip, "setnetgrent", &fct.ptr, status, 0); |
0ecb606c JJ |
130 | |
131 | if (status == NSS_STATUS_SUCCESS && ! no_more) | |
132 | { | |
133 | enum nss_status (*endfct) (struct __netgrent *); | |
134 | ||
135 | endfct = __nss_lookup_function (old_nip, "endnetgrent"); | |
136 | if (endfct != NULL) | |
137 | (void) (*endfct) (datap); | |
138 | } | |
a68b0d31 UD |
139 | } |
140 | ||
11336c16 | 141 | /* Add the current group to the list of known groups. */ |
2486480e UD |
142 | size_t group_len = strlen (group) + 1; |
143 | new_elem = (struct name_list *) malloc (sizeof (struct name_list) | |
144 | + group_len); | |
145 | if (new_elem == NULL) | |
11336c16 | 146 | { |
d71b808a UD |
147 | *errnop = errno; |
148 | status = NSS_STATUS_TRYAGAIN; | |
11336c16 UD |
149 | } |
150 | else | |
151 | { | |
2303f5fd | 152 | new_elem->next = datap->known_groups; |
2486480e | 153 | memcpy (new_elem->name, group, group_len); |
2303f5fd | 154 | datap->known_groups = new_elem; |
11336c16 | 155 | } |
a68b0d31 UD |
156 | |
157 | return status == NSS_STATUS_SUCCESS; | |
158 | } | |
159 | ||
ee600e3f UD |
160 | int internal_setnetgrent (const char *group, struct __netgrent *datap); |
161 | libc_hidden_proto (internal_setnetgrent) | |
162 | ||
163 | int | |
e78f615d | 164 | internal_setnetgrent (const char *group, struct __netgrent *datap) |
2303f5fd UD |
165 | { |
166 | /* Free list of all netgroup names from last run. */ | |
167 | free_memory (datap); | |
168 | ||
7ba4fcfc | 169 | return __internal_setnetgrent_reuse (group, datap, &errno); |
2303f5fd | 170 | } |
ee600e3f | 171 | libc_hidden_def (internal_setnetgrent) |
e78f615d | 172 | strong_alias (internal_setnetgrent, __internal_setnetgrent) |
2303f5fd | 173 | |
11336c16 UD |
174 | int |
175 | setnetgrent (const char *group) | |
176 | { | |
177 | int result; | |
178 | ||
179 | __libc_lock_lock (lock); | |
180 | ||
e78f615d | 181 | result = internal_setnetgrent (group, &dataset); |
11336c16 UD |
182 | |
183 | __libc_lock_unlock (lock); | |
184 | ||
185 | return result; | |
186 | } | |
187 | ||
ee600e3f UD |
188 | void internal_endnetgrent (struct __netgrent *datap); |
189 | libc_hidden_proto (internal_endnetgrent) | |
190 | ||
191 | void | |
e78f615d | 192 | internal_endnetgrent (struct __netgrent *datap) |
a68b0d31 | 193 | { |
0ecb606c | 194 | endnetgrent_hook (datap); |
11336c16 | 195 | /* Now free list of all netgroup names from last run. */ |
2303f5fd UD |
196 | free_memory (datap); |
197 | } | |
ee600e3f | 198 | libc_hidden_def (internal_endnetgrent) |
e78f615d | 199 | strong_alias (internal_endnetgrent, __internal_endnetgrent) |
2303f5fd UD |
200 | |
201 | ||
202 | void | |
203 | endnetgrent (void) | |
204 | { | |
205 | __libc_lock_lock (lock); | |
206 | ||
2a54ad77 | 207 | internal_endnetgrent (&dataset); |
11336c16 | 208 | |
a68b0d31 UD |
209 | __libc_lock_unlock (lock); |
210 | } | |
211 | ||
212 | ||
ee600e3f UD |
213 | int internal_getnetgrent_r (char **hostp, char **userp, char **domainp, |
214 | struct __netgrent *datap, | |
215 | char *buffer, size_t buflen, int *errnop); | |
216 | libc_hidden_proto (internal_getnetgrent_r) | |
217 | ||
218 | int | |
e78f615d | 219 | internal_getnetgrent_r (char **hostp, char **userp, char **domainp, |
01c901a5 | 220 | struct __netgrent *datap, |
d71b808a | 221 | char *buffer, size_t buflen, int *errnop) |
a68b0d31 | 222 | { |
0ecb606c | 223 | enum nss_status (*fct) (struct __netgrent *, char *, size_t, int *); |
a68b0d31 UD |
224 | |
225 | /* Initialize status to return if no more functions are found. */ | |
226 | enum nss_status status = NSS_STATUS_NOTFOUND; | |
227 | ||
a68b0d31 UD |
228 | /* Run through available functions, starting with the same function last |
229 | run. We will repeat each function as long as it succeeds, and then go | |
230 | on to the next service action. */ | |
0ecb606c JJ |
231 | int no_more = (datap->nip == NULL |
232 | || (fct = __nss_lookup_function (datap->nip, "getnetgrent_r")) | |
233 | == NULL); | |
a68b0d31 UD |
234 | while (! no_more) |
235 | { | |
0ecb606c | 236 | status = (*fct) (datap, buffer, buflen, &errno); |
a68b0d31 | 237 | |
11336c16 UD |
238 | if (status == NSS_STATUS_RETURN) |
239 | { | |
240 | /* This was the last one for this group. Look at next group | |
241 | if available. */ | |
242 | int found = 0; | |
2303f5fd | 243 | while (datap->needed_groups != NULL && ! found) |
11336c16 | 244 | { |
2303f5fd UD |
245 | struct name_list *tmp = datap->needed_groups; |
246 | datap->needed_groups = datap->needed_groups->next; | |
247 | tmp->next = datap->known_groups; | |
248 | datap->known_groups = tmp; | |
11336c16 | 249 | |
2303f5fd | 250 | found = __internal_setnetgrent_reuse (datap->known_groups->name, |
d71b808a | 251 | datap, errnop); |
11336c16 UD |
252 | } |
253 | ||
0ecb606c JJ |
254 | if (found && datap->nip != NULL) |
255 | { | |
256 | fct = __nss_lookup_function (datap->nip, "getnetgrent_r"); | |
257 | if (fct != NULL) | |
258 | continue; | |
259 | } | |
11336c16 | 260 | } |
2303f5fd | 261 | else if (status == NSS_STATUS_SUCCESS && datap->type == group_val) |
11336c16 UD |
262 | { |
263 | /* The last entry was a name of another netgroup. */ | |
264 | struct name_list *namep; | |
265 | ||
266 | /* Ignore if we've seen the name before. */ | |
2303f5fd UD |
267 | for (namep = datap->known_groups; namep != NULL; |
268 | namep = namep->next) | |
269 | if (strcmp (datap->val.group, namep->name) == 0) | |
11336c16 UD |
270 | break; |
271 | if (namep != NULL) | |
272 | /* Really ignore. */ | |
273 | continue; | |
274 | ||
2486480e UD |
275 | size_t group_len = strlen (datap->val.group) + 1; |
276 | namep = (struct name_list *) malloc (sizeof (struct name_list) | |
277 | + group_len); | |
278 | if (namep == NULL) | |
279 | /* We are out of memory. */ | |
280 | status = NSS_STATUS_RETURN; | |
11336c16 UD |
281 | else |
282 | { | |
2303f5fd | 283 | namep->next = datap->needed_groups; |
2486480e | 284 | memcpy (namep->name, datap->val.group, group_len); |
2303f5fd | 285 | datap->needed_groups = namep; |
11336c16 UD |
286 | /* And get the next entry. */ |
287 | continue; | |
288 | } | |
289 | } | |
290 | ||
0ecb606c | 291 | break; |
a68b0d31 UD |
292 | } |
293 | ||
294 | if (status == NSS_STATUS_SUCCESS) | |
295 | { | |
afd4eb37 UD |
296 | *hostp = (char *) datap->val.triple.host; |
297 | *userp = (char *) datap->val.triple.user; | |
298 | *domainp = (char *) datap->val.triple.domain; | |
a68b0d31 UD |
299 | } |
300 | ||
2303f5fd UD |
301 | return status == NSS_STATUS_SUCCESS ? 1 : 0; |
302 | } | |
ee600e3f | 303 | libc_hidden_def (internal_getnetgrent_r) |
e78f615d | 304 | strong_alias (internal_getnetgrent_r, __internal_getnetgrent_r) |
2303f5fd UD |
305 | |
306 | /* The real entry point. */ | |
307 | int | |
308 | __getnetgrent_r (char **hostp, char **userp, char **domainp, | |
309 | char *buffer, size_t buflen) | |
310 | { | |
311 | enum nss_status status; | |
312 | ||
313 | __libc_lock_lock (lock); | |
314 | ||
2a54ad77 RM |
315 | status = internal_getnetgrent_r (hostp, userp, domainp, &dataset, |
316 | buffer, buflen, &errno); | |
2303f5fd | 317 | |
a68b0d31 UD |
318 | __libc_lock_unlock (lock); |
319 | ||
2303f5fd | 320 | return status; |
a68b0d31 UD |
321 | } |
322 | weak_alias (__getnetgrent_r, getnetgrent_r) | |
323 | \f | |
324 | /* Test whether given (host,user,domain) triple is in NETGROUP. */ | |
325 | int | |
326 | innetgr (const char *netgroup, const char *host, const char *user, | |
327 | const char *domain) | |
328 | { | |
fb776f3e AJ |
329 | union |
330 | { | |
331 | int (*f) (const char *, struct __netgrent *); | |
332 | void *ptr; | |
333 | } setfct; | |
0ecb606c JJ |
334 | void (*endfct) (struct __netgrent *); |
335 | int (*getfct) (struct __netgrent *, char *, size_t, int *); | |
8500b0ca | 336 | struct __netgrent entry; |
a68b0d31 | 337 | int result = 0; |
11336c16 UD |
338 | const char *current_group = netgroup; |
339 | int real_entry = 0; | |
a68b0d31 | 340 | |
8500b0ca | 341 | memset (&entry, '\0', sizeof (entry)); |
a5753206 | 342 | |
a68b0d31 UD |
343 | /* Walk through the services until we found an answer or we shall |
344 | not work further. We can do some optimization here. Since all | |
345 | services must provide the `setnetgrent' function we can do all | |
346 | the work during one walk through the service list. */ | |
11336c16 | 347 | while (1) |
a68b0d31 | 348 | { |
0ecb606c | 349 | int no_more = setup (&setfct.ptr, &entry.nip); |
11336c16 | 350 | while (! no_more) |
a68b0d31 | 351 | { |
0ecb606c JJ |
352 | assert (entry.data == NULL); |
353 | ||
11336c16 | 354 | /* Open netgroup. */ |
8500b0ca UD |
355 | enum nss_status status = (*setfct.f) (current_group, &entry); |
356 | ||
11336c16 | 357 | if (status == NSS_STATUS_SUCCESS |
0ecb606c JJ |
358 | && (getfct = __nss_lookup_function (entry.nip, "getnetgrent_r")) |
359 | != NULL) | |
a68b0d31 | 360 | { |
11336c16 | 361 | char buffer[1024]; |
11336c16 | 362 | |
0ecb606c | 363 | while ((*getfct) (&entry, buffer, sizeof buffer, &errno) |
11336c16 | 364 | == NSS_STATUS_SUCCESS) |
a68b0d31 | 365 | { |
11336c16 UD |
366 | if (entry.type == group_val) |
367 | { | |
368 | /* Make sure we haven't seen the name before. */ | |
369 | struct name_list *namep; | |
370 | ||
8500b0ca UD |
371 | for (namep = entry.known_groups; namep != NULL; |
372 | namep = namep->next) | |
11336c16 UD |
373 | if (strcmp (entry.val.group, namep->name) == 0) |
374 | break; | |
375 | if (namep == NULL | |
376 | && strcmp (netgroup, entry.val.group) != 0) | |
377 | { | |
2486480e | 378 | size_t group_len = strlen (entry.val.group) + 1; |
11336c16 | 379 | namep = |
2486480e UD |
380 | (struct name_list *) malloc (sizeof (*namep) |
381 | + group_len); | |
382 | if (namep == NULL) | |
11336c16 UD |
383 | { |
384 | /* Out of memory, simply return. */ | |
11336c16 UD |
385 | result = -1; |
386 | break; | |
387 | } | |
388 | ||
8500b0ca | 389 | namep->next = entry.needed_groups; |
2486480e | 390 | memcpy (namep->name, entry.val.group, group_len); |
8500b0ca | 391 | entry.needed_groups = namep; |
11336c16 UD |
392 | } |
393 | } | |
394 | else | |
395 | { | |
396 | real_entry = 1; | |
397 | ||
398 | if ((entry.val.triple.host == NULL || host == NULL | |
b77e6cd6 | 399 | || __strcasecmp (entry.val.triple.host, host) == 0) |
11336c16 UD |
400 | && (entry.val.triple.user == NULL || user == NULL |
401 | || strcmp (entry.val.triple.user, user) == 0) | |
402 | && (entry.val.triple.domain == NULL || domain == NULL | |
b77e6cd6 UD |
403 | || __strcasecmp (entry.val.triple.domain, |
404 | domain) == 0)) | |
11336c16 UD |
405 | { |
406 | result = 1; | |
407 | break; | |
408 | } | |
409 | } | |
a68b0d31 | 410 | } |
11336c16 | 411 | |
11336c16 UD |
412 | /* If we found one service which does know the given |
413 | netgroup we don't try further. */ | |
414 | status = NSS_STATUS_RETURN; | |
a68b0d31 UD |
415 | } |
416 | ||
11336c16 | 417 | /* Free all resources of the service. */ |
0ecb606c JJ |
418 | endfct = __nss_lookup_function (entry.nip, "endnetgrent"); |
419 | if (endfct != NULL) | |
420 | (*endfct) (&entry); | |
421 | ||
422 | if (result != 0) | |
423 | break; | |
a68b0d31 | 424 | |
11336c16 | 425 | /* Look for the next service. */ |
8500b0ca | 426 | no_more = __nss_next (&entry.nip, "setnetgrent", |
fb776f3e | 427 | &setfct.ptr, status, 0); |
a68b0d31 UD |
428 | } |
429 | ||
8500b0ca | 430 | if (result == 0 && entry.needed_groups != NULL) |
11336c16 | 431 | { |
8500b0ca UD |
432 | struct name_list *tmp = entry.needed_groups; |
433 | entry.needed_groups = tmp->next; | |
434 | tmp->next = entry.known_groups; | |
435 | entry.known_groups = tmp; | |
436 | current_group = entry.known_groups->name; | |
11336c16 UD |
437 | continue; |
438 | } | |
a68b0d31 | 439 | |
11336c16 UD |
440 | /* No way out. */ |
441 | break; | |
a68b0d31 UD |
442 | } |
443 | ||
11336c16 | 444 | /* Free the memory. */ |
8500b0ca | 445 | free_memory (&entry); |
11336c16 | 446 | |
0ecb606c | 447 | return result == 1; |
a68b0d31 | 448 | } |
a585ba22 | 449 | libc_hidden_def (innetgr) |