Update.
[glibc.git] / inet / getnetgrent_r.c
bloba0fb6f42a1ea4cc4bdff62e04218e18eef2b45a2
1 /* Copyright (C) 1996, 1997, 1998, 1999, 2002 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 /* This handle for the NSS data base is shared between all
32 set/get/endXXXent functions. */
33 static service_user *nip;
35 /* The whole information for the set/get/endnetgrent functions are
36 kept in this structure. */
37 static struct __netgrent dataset;
39 /* The lookup function for the first entry of this service. */
40 extern int __nss_netgroup_lookup (service_user **nip, const char *name,
41 void **fctp) internal_function;
44 /* Set up NIP to run through the services. If ALL is zero, use NIP's
45 current location if it's not nil. Return nonzero if there are no
46 services (left). */
47 static enum nss_status
48 setup (void **fctp, const char *func_name, int all)
50 /* Remember the first service_entry, it's always the same. */
51 static service_user *startp;
52 int no_more;
54 if (startp == NULL)
56 no_more = __nss_netgroup_lookup (&nip, func_name, fctp);
57 startp = no_more ? (service_user *) -1 : nip;
59 else if (startp == (service_user *) -1)
60 /* No services at all. */
61 return 1;
62 else
64 if (all || !nip)
65 /* Reset to the beginning of the service list. */
66 nip = startp;
67 /* Look up the first function. */
68 no_more = __nss_lookup (&nip, func_name, fctp);
70 return no_more;
73 /* Free used memory. */
74 static void
75 free_memory (struct __netgrent *data)
77 while (data->known_groups != NULL)
79 struct name_list *tmp = data->known_groups;
80 data->known_groups = data->known_groups->next;
81 free ((void *) tmp->name);
82 free (tmp);
85 while (data->needed_groups != NULL)
87 struct name_list *tmp = data->needed_groups;
88 data->needed_groups = data->needed_groups->next;
89 free ((void *) tmp->name);
90 free (tmp);
94 static int
95 internal_function
96 __internal_setnetgrent_reuse (const char *group, struct __netgrent *datap,
97 int *errnop)
99 enum nss_status (*fct) (const char *, struct __netgrent *);
100 enum nss_status status = NSS_STATUS_UNAVAIL;
101 struct name_list *new_elem;
102 int no_more;
104 /* Cycle through all the services and run their setnetgrent functions. */
105 no_more = setup ((void **) &fct, "setnetgrent", 1);
106 while (! no_more)
108 /* Ignore status, we force check in `__nss_next'. */
109 status = (*fct) (group, datap);
111 no_more = __nss_next (&nip, "setnetgrent", (void **) &fct, status, 0);
114 /* Add the current group to the list of known groups. */
115 new_elem = (struct name_list *) malloc (sizeof (struct name_list));
116 if (new_elem == NULL || (new_elem->name = __strdup (group)) == NULL)
118 if (new_elem != NULL)
119 free (new_elem);
120 *errnop = errno;
121 status = NSS_STATUS_TRYAGAIN;
123 else
125 new_elem->next = datap->known_groups;
126 datap->known_groups = new_elem;
129 return status == NSS_STATUS_SUCCESS;
132 static int
133 internal_setnetgrent (const char *group, struct __netgrent *datap)
135 /* Free list of all netgroup names from last run. */
136 free_memory (datap);
138 return __internal_setnetgrent_reuse (group, datap, &errno);
140 strong_alias (internal_setnetgrent, __internal_setnetgrent)
143 setnetgrent (const char *group)
145 int result;
147 __libc_lock_lock (lock);
149 result = internal_setnetgrent (group, &dataset);
151 __libc_lock_unlock (lock);
153 return result;
157 static void
158 internal_endnetgrent (struct __netgrent *datap)
160 service_user *old_nip;
161 enum nss_status (*fct) (struct __netgrent *);
162 int no_more;
164 /* Remember which was the last used service. */
165 old_nip = nip;
167 /* Cycle through all the services and run their endnetgrent functions. */
168 no_more = setup ((void **) &fct, "endnetgrent", 1);
169 while (! no_more)
171 /* Ignore status, we force check in `__nss_next'. */
172 (void) (*fct) (datap);
174 no_more = (nip == old_nip
175 || __nss_next (&nip, "endnetgrent", (void **) &fct, 0, 1));
178 /* Now free list of all netgroup names from last run. */
179 free_memory (datap);
181 strong_alias (internal_endnetgrent, __internal_endnetgrent)
184 void
185 endnetgrent (void)
187 __libc_lock_lock (lock);
189 internal_endnetgrent (&dataset);
191 __libc_lock_unlock (lock);
195 static int
196 internal_getnetgrent_r (char **hostp, char **userp, char **domainp,
197 struct __netgrent *datap,
198 char *buffer, size_t buflen, int *errnop)
200 enum nss_status (*fct) (struct __netgrent *, char *, size_t, int *);
201 int no_more;
203 /* Initialize status to return if no more functions are found. */
204 enum nss_status status = NSS_STATUS_NOTFOUND;
206 /* Run through available functions, starting with the same function last
207 run. We will repeat each function as long as it succeeds, and then go
208 on to the next service action. */
209 no_more = setup ((void **) &fct, "getnetgrent_r", 0);
210 while (! no_more)
212 status = (*fct) (datap, buffer, buflen, &errno);
214 if (status == NSS_STATUS_RETURN)
216 /* This was the last one for this group. Look at next group
217 if available. */
218 int found = 0;
219 while (datap->needed_groups != NULL && ! found)
221 struct name_list *tmp = datap->needed_groups;
222 datap->needed_groups = datap->needed_groups->next;
223 tmp->next = datap->known_groups;
224 datap->known_groups = tmp;
226 found = __internal_setnetgrent_reuse (datap->known_groups->name,
227 datap, errnop);
230 if (found)
231 continue;
233 else if (status == NSS_STATUS_SUCCESS && datap->type == group_val)
235 /* The last entry was a name of another netgroup. */
236 struct name_list *namep;
238 /* Ignore if we've seen the name before. */
239 for (namep = datap->known_groups; namep != NULL;
240 namep = namep->next)
241 if (strcmp (datap->val.group, namep->name) == 0)
242 break;
243 if (namep != NULL)
244 /* Really ignore. */
245 continue;
247 namep = (struct name_list *) malloc (sizeof (struct name_list));
248 if (namep == NULL
249 || (namep->name = __strdup (datap->val.group)) == NULL)
251 /* We are out of memory. */
252 if (namep != NULL)
253 free (namep);
254 status = NSS_STATUS_RETURN;
256 else
258 namep->next = datap->needed_groups;
259 datap->needed_groups = namep;
260 /* And get the next entry. */
261 continue;
265 no_more = __nss_next (&nip, "getnetgrent_r", (void **) &fct, status, 0);
268 if (status == NSS_STATUS_SUCCESS)
270 *hostp = (char *) datap->val.triple.host;
271 *userp = (char *) datap->val.triple.user;
272 *domainp = (char *) datap->val.triple.domain;
275 return status == NSS_STATUS_SUCCESS ? 1 : 0;
277 strong_alias (internal_getnetgrent_r, __internal_getnetgrent_r)
279 /* The real entry point. */
281 __getnetgrent_r (char **hostp, char **userp, char **domainp,
282 char *buffer, size_t buflen)
284 enum nss_status status;
286 __libc_lock_lock (lock);
288 status = internal_getnetgrent_r (hostp, userp, domainp, &dataset,
289 buffer, buflen, &errno);
291 __libc_lock_unlock (lock);
293 return status;
295 weak_alias (__getnetgrent_r, getnetgrent_r)
297 /* Test whether given (host,user,domain) triple is in NETGROUP. */
299 innetgr (const char *netgroup, const char *host, const char *user,
300 const char *domain)
302 int (*setfct) (const char *, struct __netgrent *);
303 void (*endfct) (struct __netgrent *);
304 int (*getfct) (struct __netgrent *, char *, size_t, int *);
305 struct name_list *known = NULL;
306 struct name_list *needed = NULL;
307 int result = 0;
308 int no_more;
309 const char *current_group = netgroup;
310 int real_entry = 0;
312 /* Walk through the services until we found an answer or we shall
313 not work further. We can do some optimization here. Since all
314 services must provide the `setnetgrent' function we can do all
315 the work during one walk through the service list. */
316 while (1)
318 no_more = setup ((void **) &setfct, "setnetgrent", 1);
319 while (! no_more)
321 enum nss_status status;
322 struct __netgrent entry;
324 /* Clear the space for the netgroup data. */
325 __bzero (&entry, sizeof (entry));
327 /* Open netgroup. */
328 status = (*setfct) (current_group, &entry);
329 if (status == NSS_STATUS_SUCCESS
330 && __nss_lookup (&nip, "getnetgrent_r", (void **) &getfct) == 0)
332 char buffer[1024];
334 while ((*getfct) (&entry, buffer, sizeof buffer, &errno)
335 == NSS_STATUS_SUCCESS)
337 if (entry.type == group_val)
339 /* Make sure we haven't seen the name before. */
340 struct name_list *namep;
342 for (namep = known; namep != NULL; namep = namep->next)
343 if (strcmp (entry.val.group, namep->name) == 0)
344 break;
345 if (namep == NULL
346 && strcmp (netgroup, entry.val.group) != 0)
348 namep =
349 (struct name_list *) malloc (sizeof (*namep));
350 if (namep == NULL
351 || ((namep->name = __strdup (entry.val.group))
352 == NULL))
354 /* Out of memory, simply return. */
355 if (namep != NULL)
356 free (namep);
357 result = -1;
358 break;
361 namep->next = needed;
362 needed = namep;
365 else
367 real_entry = 1;
369 if ((entry.val.triple.host == NULL || host == NULL
370 || __strcasecmp (entry.val.triple.host, host) == 0)
371 && (entry.val.triple.user == NULL || user == NULL
372 || strcmp (entry.val.triple.user, user) == 0)
373 && (entry.val.triple.domain == NULL || domain == NULL
374 || __strcasecmp (entry.val.triple.domain,
375 domain) == 0))
377 result = 1;
378 break;
383 if (result != 0)
384 break;
386 /* If we found one service which does know the given
387 netgroup we don't try further. */
388 status = NSS_STATUS_RETURN;
391 /* Free all resources of the service. */
392 if (__nss_lookup (&nip, "endnetgrent", (void **) &endfct) == 0)
393 (*endfct) (&entry);
395 /* Look for the next service. */
396 no_more = __nss_next (&nip, "setnetgrent",
397 (void **) &setfct, status, 0);
400 if (result == 0 && needed != NULL)
402 struct name_list *tmp = needed;
403 needed = tmp->next;
404 tmp->next = known;
405 known = tmp;
406 current_group = known->name;
407 continue;
410 /* No way out. */
411 break;
414 /* Free the memory. */
415 while (known != NULL)
417 struct name_list *tmp = known;
418 known = known->next;
419 free ((void *) tmp->name);
420 free (tmp);
422 while (needed != NULL)
424 struct name_list *tmp = needed;
425 needed = needed->next;
426 free ((void *) tmp->name);
427 free (tmp);
430 return result == 1;
432 libc_hidden_def (innetgr)