[BZ #4069]
[glibc.git] / inet / getnetgrent_r.c
blob97b2b809f0ff9e003a51cdf036d01f2e9927025f
1 /* Copyright (C) 1996, 1997, 1998, 1999, 2002, 2004, 2005
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
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. */
20 #include <assert.h>
21 #include <bits/libc-lock.h>
22 #include <errno.h>
23 #include <netdb.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "netgroup.h"
27 #include "nsswitch.h"
30 /* Protect above variable against multiple uses at the same time. */
31 __libc_lock_define_initialized (static, lock)
33 /* The whole information for the set/get/endnetgrent functions are
34 kept in this structure. */
35 static struct __netgrent dataset;
37 /* The lookup function for the first entry of this service. */
38 extern int __nss_netgroup_lookup (service_user **nipp, const char *name,
39 void **fctp) internal_function;
41 /* Set up NIP to run through the services. Return nonzero if there are no
42 services (left). */
43 static int
44 setup (void **fctp, 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, "setnetgrent", 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 /* Reset to the beginning of the service list. */
63 *nipp = startp;
64 /* Look up the first function. */
65 no_more = __nss_lookup (nipp, "setnetgrent", fctp);
67 return no_more;
70 /* Free used memory. */
71 static void
72 free_memory (struct __netgrent *data)
74 while (data->known_groups != NULL)
76 struct name_list *tmp = data->known_groups;
77 data->known_groups = data->known_groups->next;
78 free (tmp);
81 while (data->needed_groups != NULL)
83 struct name_list *tmp = data->needed_groups;
84 data->needed_groups = data->needed_groups->next;
85 free (tmp);
89 static void
90 endnetgrent_hook (struct __netgrent *datap)
92 enum nss_status (*endfct) (struct __netgrent *);
94 if (datap->nip == NULL)
95 return;
97 endfct = __nss_lookup_function (datap->nip, "endnetgrent");
98 if (endfct != NULL)
99 (void) (*endfct) (datap);
100 datap->nip = NULL;
103 static int
104 internal_function
105 __internal_setnetgrent_reuse (const char *group, struct __netgrent *datap,
106 int *errnop)
108 union
110 enum nss_status (*f) (const char *, struct __netgrent *);
111 void *ptr;
112 } fct;
113 enum nss_status status = NSS_STATUS_UNAVAIL;
114 struct name_list *new_elem;
116 /* Free data from previous service. */
117 endnetgrent_hook (datap);
119 /* Cycle through all the services and run their setnetgrent functions. */
120 int no_more = setup (&fct.ptr, &datap->nip);
121 while (! no_more)
123 assert (datap->data == NULL);
125 /* Ignore status, we force check in `__nss_next'. */
126 status = (*fct.f) (group, datap);
128 service_user *old_nip = datap->nip;
129 no_more = __nss_next (&datap->nip, "setnetgrent", &fct.ptr, status, 0);
131 if (status == NSS_STATUS_SUCCESS && ! no_more)
133 enum nss_status (*endfct) (struct __netgrent *);
135 endfct = __nss_lookup_function (old_nip, "endnetgrent");
136 if (endfct != NULL)
137 (void) (*endfct) (datap);
141 /* Add the current group to the list of known groups. */
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)
147 *errnop = errno;
148 status = NSS_STATUS_TRYAGAIN;
150 else
152 new_elem->next = datap->known_groups;
153 memcpy (new_elem->name, group, group_len);
154 datap->known_groups = new_elem;
157 return status == NSS_STATUS_SUCCESS;
160 int internal_setnetgrent (const char *group, struct __netgrent *datap);
161 libc_hidden_proto (internal_setnetgrent)
164 internal_setnetgrent (const char *group, struct __netgrent *datap)
166 /* Free list of all netgroup names from last run. */
167 free_memory (datap);
169 return __internal_setnetgrent_reuse (group, datap, &errno);
171 libc_hidden_def (internal_setnetgrent)
172 strong_alias (internal_setnetgrent, __internal_setnetgrent)
175 setnetgrent (const char *group)
177 int result;
179 __libc_lock_lock (lock);
181 result = internal_setnetgrent (group, &dataset);
183 __libc_lock_unlock (lock);
185 return result;
188 void internal_endnetgrent (struct __netgrent *datap);
189 libc_hidden_proto (internal_endnetgrent)
191 void
192 internal_endnetgrent (struct __netgrent *datap)
194 endnetgrent_hook (datap);
195 /* Now free list of all netgroup names from last run. */
196 free_memory (datap);
198 libc_hidden_def (internal_endnetgrent)
199 strong_alias (internal_endnetgrent, __internal_endnetgrent)
202 void
203 endnetgrent (void)
205 __libc_lock_lock (lock);
207 internal_endnetgrent (&dataset);
209 __libc_lock_unlock (lock);
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)
219 internal_getnetgrent_r (char **hostp, char **userp, char **domainp,
220 struct __netgrent *datap,
221 char *buffer, size_t buflen, int *errnop)
223 enum nss_status (*fct) (struct __netgrent *, char *, size_t, int *);
225 /* Initialize status to return if no more functions are found. */
226 enum nss_status status = NSS_STATUS_NOTFOUND;
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. */
231 int no_more = (datap->nip == NULL
232 || (fct = __nss_lookup_function (datap->nip, "getnetgrent_r"))
233 == NULL);
234 while (! no_more)
236 status = (*fct) (datap, buffer, buflen, &errno);
238 if (status == NSS_STATUS_RETURN)
240 /* This was the last one for this group. Look at next group
241 if available. */
242 int found = 0;
243 while (datap->needed_groups != NULL && ! found)
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;
250 found = __internal_setnetgrent_reuse (datap->known_groups->name,
251 datap, errnop);
254 if (found && datap->nip != NULL)
256 fct = __nss_lookup_function (datap->nip, "getnetgrent_r");
257 if (fct != NULL)
258 continue;
261 else if (status == NSS_STATUS_SUCCESS && datap->type == group_val)
263 /* The last entry was a name of another netgroup. */
264 struct name_list *namep;
266 /* Ignore if we've seen the name before. */
267 for (namep = datap->known_groups; namep != NULL;
268 namep = namep->next)
269 if (strcmp (datap->val.group, namep->name) == 0)
270 break;
271 if (namep != NULL)
272 /* Really ignore. */
273 continue;
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;
281 else
283 namep->next = datap->needed_groups;
284 memcpy (namep->name, datap->val.group, group_len);
285 datap->needed_groups = namep;
286 /* And get the next entry. */
287 continue;
291 break;
294 if (status == NSS_STATUS_SUCCESS)
296 *hostp = (char *) datap->val.triple.host;
297 *userp = (char *) datap->val.triple.user;
298 *domainp = (char *) datap->val.triple.domain;
301 return status == NSS_STATUS_SUCCESS ? 1 : 0;
303 libc_hidden_def (internal_getnetgrent_r)
304 strong_alias (internal_getnetgrent_r, __internal_getnetgrent_r)
306 /* The real entry point. */
308 __getnetgrent_r (char **hostp, char **userp, char **domainp,
309 char *buffer, size_t buflen)
311 enum nss_status status;
313 __libc_lock_lock (lock);
315 status = internal_getnetgrent_r (hostp, userp, domainp, &dataset,
316 buffer, buflen, &errno);
318 __libc_lock_unlock (lock);
320 return status;
322 weak_alias (__getnetgrent_r, getnetgrent_r)
324 /* Test whether given (host,user,domain) triple is in NETGROUP. */
326 innetgr (const char *netgroup, const char *host, const char *user,
327 const char *domain)
329 union
331 int (*f) (const char *, struct __netgrent *);
332 void *ptr;
333 } setfct;
334 void (*endfct) (struct __netgrent *);
335 int (*getfct) (struct __netgrent *, char *, size_t, int *);
336 struct __netgrent entry;
337 int result = 0;
338 const char *current_group = netgroup;
339 int real_entry = 0;
341 memset (&entry, '\0', sizeof (entry));
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. */
347 while (1)
349 int no_more = setup (&setfct.ptr, &entry.nip);
350 while (! no_more)
352 assert (entry.data == NULL);
354 /* Open netgroup. */
355 enum nss_status status = (*setfct.f) (current_group, &entry);
357 if (status == NSS_STATUS_SUCCESS
358 && (getfct = __nss_lookup_function (entry.nip, "getnetgrent_r"))
359 != NULL)
361 char buffer[1024];
363 while ((*getfct) (&entry, buffer, sizeof buffer, &errno)
364 == NSS_STATUS_SUCCESS)
366 if (entry.type == group_val)
368 /* Make sure we haven't seen the name before. */
369 struct name_list *namep;
371 for (namep = entry.known_groups; namep != NULL;
372 namep = namep->next)
373 if (strcmp (entry.val.group, namep->name) == 0)
374 break;
375 if (namep == NULL
376 && strcmp (netgroup, entry.val.group) != 0)
378 size_t group_len = strlen (entry.val.group) + 1;
379 namep =
380 (struct name_list *) malloc (sizeof (*namep)
381 + group_len);
382 if (namep == NULL)
384 /* Out of memory, simply return. */
385 result = -1;
386 break;
389 namep->next = entry.needed_groups;
390 memcpy (namep->name, entry.val.group, group_len);
391 entry.needed_groups = namep;
394 else
396 real_entry = 1;
398 if ((entry.val.triple.host == NULL || host == NULL
399 || __strcasecmp (entry.val.triple.host, host) == 0)
400 && (entry.val.triple.user == NULL || user == NULL
401 || strcmp (entry.val.triple.user, user) == 0)
402 && (entry.val.triple.domain == NULL || domain == NULL
403 || __strcasecmp (entry.val.triple.domain,
404 domain) == 0))
406 result = 1;
407 break;
412 /* If we found one service which does know the given
413 netgroup we don't try further. */
414 status = NSS_STATUS_RETURN;
417 /* Free all resources of the service. */
418 endfct = __nss_lookup_function (entry.nip, "endnetgrent");
419 if (endfct != NULL)
420 (*endfct) (&entry);
422 if (result != 0)
423 break;
425 /* Look for the next service. */
426 no_more = __nss_next (&entry.nip, "setnetgrent",
427 &setfct.ptr, status, 0);
430 if (result == 0 && entry.needed_groups != NULL)
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;
437 continue;
440 /* No way out. */
441 break;
444 /* Free the memory. */
445 free_memory (&entry);
447 return result == 1;
449 libc_hidden_def (innetgr)