2.3.90-4
[glibc.git] / inet / getnetgrent_r.c
blob640210ab2e03b4a2387a72d3c88f1111c5648295
1 /* Copyright (C) 1996,1997,1998,1999,2002,2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <bits/libc-lock.h>
20 #include <errno.h>
21 #include <netdb.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "netgroup.h"
25 #include "nsswitch.h"
28 /* Protect above variable against multiple uses at the same time. */
29 __libc_lock_define_initialized (static, lock)
31 /* The whole information for the set/get/endnetgrent functions are
32 kept in this structure. */
33 static struct __netgrent dataset;
35 /* The lookup function for the first entry of this service. */
36 extern int __nss_netgroup_lookup (service_user **nip, const char *name,
37 void **fctp) internal_function;
40 /* Set up NIP to run through the services. If ALL is zero, use NIP's
41 current location if it's not nil. Return nonzero if there are no
42 services (left). */
43 static enum nss_status
44 setup (void **fctp, const char *func_name, int all, service_user **nipp)
46 /* Remember the first service_entry, it's always the same. */
47 static service_user *startp;
48 int no_more;
50 if (startp == NULL)
52 /* Executing this more than once at the same time must yield the
53 same result every time. So we need no locking. */
54 no_more = __nss_netgroup_lookup (nipp, func_name, fctp);
55 startp = no_more ? (service_user *) -1 : *nipp;
57 else if (startp == (service_user *) -1)
58 /* No services at all. */
59 return 1;
60 else
62 if (all || *nipp == NULL)
63 /* Reset to the beginning of the service list. */
64 *nipp = startp;
65 /* Look up the first function. */
66 no_more = __nss_lookup (nipp, func_name, fctp);
68 return no_more;
71 /* Free used memory. */
72 static void
73 free_memory (struct __netgrent *data)
75 while (data->known_groups != NULL)
77 struct name_list *tmp = data->known_groups;
78 data->known_groups = data->known_groups->next;
79 free (tmp);
82 while (data->needed_groups != NULL)
84 struct name_list *tmp = data->needed_groups;
85 data->needed_groups = data->needed_groups->next;
86 free (tmp);
90 static int
91 internal_function
92 __internal_setnetgrent_reuse (const char *group, struct __netgrent *datap,
93 int *errnop)
95 union
97 enum nss_status (*f) (const char *, struct __netgrent *);
98 void *ptr;
99 } fct;
100 enum nss_status status = NSS_STATUS_UNAVAIL;
101 struct name_list *new_elem;
103 /* Cycle through all the services and run their setnetgrent functions. */
104 int no_more = setup (&fct.ptr, "setnetgrent", 1, &datap->nip);
105 while (! no_more)
107 /* Ignore status, we force check in `__nss_next'. */
108 status = (*fct.f) (group, datap);
110 no_more = __nss_next (&datap->nip, "setnetgrent", &fct.ptr, status, 0);
113 /* Add the current group to the list of known groups. */
114 size_t group_len = strlen (group) + 1;
115 new_elem = (struct name_list *) malloc (sizeof (struct name_list)
116 + group_len);
117 if (new_elem == NULL)
119 *errnop = errno;
120 status = NSS_STATUS_TRYAGAIN;
122 else
124 new_elem->next = datap->known_groups;
125 memcpy (new_elem->name, group, group_len);
126 datap->known_groups = new_elem;
129 return status == NSS_STATUS_SUCCESS;
132 int internal_setnetgrent (const char *group, struct __netgrent *datap);
133 libc_hidden_proto (internal_setnetgrent)
136 internal_setnetgrent (const char *group, struct __netgrent *datap)
138 /* Free list of all netgroup names from last run. */
139 free_memory (datap);
141 return __internal_setnetgrent_reuse (group, datap, &errno);
143 libc_hidden_def (internal_setnetgrent)
144 strong_alias (internal_setnetgrent, __internal_setnetgrent)
147 setnetgrent (const char *group)
149 int result;
151 __libc_lock_lock (lock);
153 result = internal_setnetgrent (group, &dataset);
155 __libc_lock_unlock (lock);
157 return result;
161 void internal_endnetgrent (struct __netgrent *datap);
162 libc_hidden_proto (internal_endnetgrent)
164 void
165 internal_endnetgrent (struct __netgrent *datap)
167 service_user *old_nip;
168 union
170 enum nss_status (*f) (struct __netgrent *);
171 void *ptr;
172 } fct;
174 /* Remember which was the last used service. */
175 old_nip = datap->nip;
177 /* Cycle through all the services and run their endnetgrent functions. */
178 int no_more = setup (&fct.ptr, "endnetgrent", 1, &datap->nip);
179 while (! no_more)
181 /* Ignore status, we force check in `__nss_next'. */
182 (void) (*fct.f) (datap);
184 no_more = (datap->nip == old_nip
185 || __nss_next (&datap->nip, "endnetgrent", &fct.ptr, 0, 1));
188 /* Now free list of all netgroup names from last run. */
189 free_memory (datap);
191 libc_hidden_def (internal_endnetgrent)
192 strong_alias (internal_endnetgrent, __internal_endnetgrent)
195 void
196 endnetgrent (void)
198 __libc_lock_lock (lock);
200 internal_endnetgrent (&dataset);
202 __libc_lock_unlock (lock);
206 int internal_getnetgrent_r (char **hostp, char **userp, char **domainp,
207 struct __netgrent *datap,
208 char *buffer, size_t buflen, int *errnop);
209 libc_hidden_proto (internal_getnetgrent_r)
212 internal_getnetgrent_r (char **hostp, char **userp, char **domainp,
213 struct __netgrent *datap,
214 char *buffer, size_t buflen, int *errnop)
216 union
218 enum nss_status (*f) (struct __netgrent *, char *, size_t, int *);
219 void *ptr;
220 } fct;
222 /* Initialize status to return if no more functions are found. */
223 enum nss_status status = NSS_STATUS_NOTFOUND;
225 /* Run through available functions, starting with the same function last
226 run. We will repeat each function as long as it succeeds, and then go
227 on to the next service action. */
228 int no_more = setup (&fct.ptr, "getnetgrent_r", 0, &datap->nip);
229 while (! no_more)
231 status = (*fct.f) (datap, buffer, buflen, &errno);
233 if (status == NSS_STATUS_RETURN)
235 /* This was the last one for this group. Look at next group
236 if available. */
237 int found = 0;
238 while (datap->needed_groups != NULL && ! found)
240 struct name_list *tmp = datap->needed_groups;
241 datap->needed_groups = datap->needed_groups->next;
242 tmp->next = datap->known_groups;
243 datap->known_groups = tmp;
245 found = __internal_setnetgrent_reuse (datap->known_groups->name,
246 datap, errnop);
249 if (found)
250 continue;
252 else if (status == NSS_STATUS_SUCCESS && datap->type == group_val)
254 /* The last entry was a name of another netgroup. */
255 struct name_list *namep;
257 /* Ignore if we've seen the name before. */
258 for (namep = datap->known_groups; namep != NULL;
259 namep = namep->next)
260 if (strcmp (datap->val.group, namep->name) == 0)
261 break;
262 if (namep != NULL)
263 /* Really ignore. */
264 continue;
266 size_t group_len = strlen (datap->val.group) + 1;
267 namep = (struct name_list *) malloc (sizeof (struct name_list)
268 + group_len);
269 if (namep == NULL)
270 /* We are out of memory. */
271 status = NSS_STATUS_RETURN;
272 else
274 namep->next = datap->needed_groups;
275 memcpy (namep->name, datap->val.group, group_len);
276 datap->needed_groups = namep;
277 /* And get the next entry. */
278 continue;
282 no_more = __nss_next (&datap->nip, "getnetgrent_r", &fct.ptr, status, 0);
285 if (status == NSS_STATUS_SUCCESS)
287 *hostp = (char *) datap->val.triple.host;
288 *userp = (char *) datap->val.triple.user;
289 *domainp = (char *) datap->val.triple.domain;
292 return status == NSS_STATUS_SUCCESS ? 1 : 0;
294 libc_hidden_def (internal_getnetgrent_r)
295 strong_alias (internal_getnetgrent_r, __internal_getnetgrent_r)
297 /* The real entry point. */
299 __getnetgrent_r (char **hostp, char **userp, char **domainp,
300 char *buffer, size_t buflen)
302 enum nss_status status;
304 __libc_lock_lock (lock);
306 status = internal_getnetgrent_r (hostp, userp, domainp, &dataset,
307 buffer, buflen, &errno);
309 __libc_lock_unlock (lock);
311 return status;
313 weak_alias (__getnetgrent_r, getnetgrent_r)
315 /* Test whether given (host,user,domain) triple is in NETGROUP. */
317 innetgr (const char *netgroup, const char *host, const char *user,
318 const char *domain)
320 union
322 int (*f) (const char *, struct __netgrent *);
323 void *ptr;
324 } setfct;
325 union
327 void (*f) (struct __netgrent *);
328 void *ptr;
329 } endfct;
330 union
332 int (*f) (struct __netgrent *, char *, size_t, int *);
333 void *ptr;
334 } getfct;
335 struct __netgrent entry;
336 int result = 0;
337 const char *current_group = netgroup;
338 int real_entry = 0;
340 memset (&entry, '\0', sizeof (entry));
342 /* Walk through the services until we found an answer or we shall
343 not work further. We can do some optimization here. Since all
344 services must provide the `setnetgrent' function we can do all
345 the work during one walk through the service list. */
346 while (1)
348 int no_more = setup (&setfct.ptr, "setnetgrent", 1, &entry.nip);
349 while (! no_more)
351 /* Open netgroup. */
352 enum nss_status status = (*setfct.f) (current_group, &entry);
354 if (status == NSS_STATUS_SUCCESS
355 && __nss_lookup (&entry.nip, "getnetgrent_r", &getfct.ptr) == 0)
357 char buffer[1024];
359 while ((*getfct.f) (&entry, buffer, sizeof buffer, &errno)
360 == NSS_STATUS_SUCCESS)
362 if (entry.type == group_val)
364 /* Make sure we haven't seen the name before. */
365 struct name_list *namep;
367 for (namep = entry.known_groups; namep != NULL;
368 namep = namep->next)
369 if (strcmp (entry.val.group, namep->name) == 0)
370 break;
371 if (namep == NULL
372 && strcmp (netgroup, entry.val.group) != 0)
374 size_t group_len = strlen (entry.val.group) + 1;
375 namep =
376 (struct name_list *) malloc (sizeof (*namep)
377 + group_len);
378 if (namep == NULL)
380 /* Out of memory, simply return. */
381 result = -1;
382 break;
385 namep->next = entry.needed_groups;
386 memcpy (namep->name, entry.val.group, group_len);
387 entry.needed_groups = namep;
390 else
392 real_entry = 1;
394 if ((entry.val.triple.host == NULL || host == NULL
395 || __strcasecmp (entry.val.triple.host, host) == 0)
396 && (entry.val.triple.user == NULL || user == NULL
397 || strcmp (entry.val.triple.user, user) == 0)
398 && (entry.val.triple.domain == NULL || domain == NULL
399 || __strcasecmp (entry.val.triple.domain,
400 domain) == 0))
402 result = 1;
403 break;
408 if (result != 0)
409 break;
411 /* If we found one service which does know the given
412 netgroup we don't try further. */
413 status = NSS_STATUS_RETURN;
416 /* Free all resources of the service. */
417 if (__nss_lookup (&entry.nip, "endnetgrent", &endfct.ptr) == 0)
418 (*endfct.f) (&entry);
420 /* Look for the next service. */
421 no_more = __nss_next (&entry.nip, "setnetgrent",
422 &setfct.ptr, status, 0);
425 if (result == 0 && entry.needed_groups != NULL)
427 struct name_list *tmp = entry.needed_groups;
428 entry.needed_groups = tmp->next;
429 tmp->next = entry.known_groups;
430 entry.known_groups = tmp;
431 current_group = entry.known_groups->name;
432 continue;
435 /* No way out. */
436 break;
439 /* Free the memory. */
440 free_memory (&entry);
442 return result;
444 libc_hidden_def (innetgr)