* locale/programs/locarchive.c (create_archive): Add MAP_PRIVATE
[glibc.git] / inet / getnetgrent_r.c
blob38701857cb1f84d949fcaea905e906bef625a670
1 /* Copyright (C) 1996, 1997, 1998, 1999, 2002, 2004, 2005, 2007
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 <atomic.h>
22 #include <bits/libc-lock.h>
23 #include <errno.h>
24 #include <netdb.h>
25 #include <stdbool.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "netgroup.h"
29 #include "nsswitch.h"
30 #include <sysdep.h>
33 /* Protect above variable against multiple uses at the same time. */
34 __libc_lock_define_initialized (static, lock)
36 /* The whole information for the set/get/endnetgrent functions are
37 kept in this structure. */
38 static struct __netgrent dataset;
40 /* The lookup function for the first entry of this service. */
41 extern int __nss_netgroup_lookup (service_user **nipp, const char *name,
42 void **fctp) internal_function;
44 /* Set up NIP to run through the services. Return nonzero if there are no
45 services (left). */
46 static int
47 setup (void **fctp, service_user **nipp)
49 /* Remember the first service_entry, it's always the same. */
50 static bool startp_initialized;
51 static service_user *startp;
52 int no_more;
54 if (!startp_initialized)
56 /* Executing this more than once at the same time must yield the
57 same result every time. So we need no locking. */
58 no_more = __nss_netgroup_lookup (nipp, "setnetgrent", fctp);
59 startp = no_more ? (service_user *) -1 : *nipp;
60 PTR_MANGLE (startp);
61 atomic_write_barrier ();
62 startp_initialized = true;
64 else
66 service_user *nip = startp;
67 PTR_DEMANGLE (nip);
68 if (nip == (service_user *) -1)
69 /* No services at all. */
70 return 1;
72 /* Reset to the beginning of the service list. */
73 *nipp = nip;
74 /* Look up the first function. */
75 no_more = __nss_lookup (nipp, "setnetgrent", NULL, fctp);
77 return no_more;
80 /* Free used memory. */
81 static void
82 free_memory (struct __netgrent *data)
84 while (data->known_groups != NULL)
86 struct name_list *tmp = data->known_groups;
87 data->known_groups = data->known_groups->next;
88 free (tmp);
91 while (data->needed_groups != NULL)
93 struct name_list *tmp = data->needed_groups;
94 data->needed_groups = data->needed_groups->next;
95 free (tmp);
99 static void
100 endnetgrent_hook (struct __netgrent *datap)
102 enum nss_status (*endfct) (struct __netgrent *);
104 if (datap->nip == NULL)
105 return;
107 endfct = __nss_lookup_function (datap->nip, "endnetgrent");
108 if (endfct != NULL)
109 (void) (*endfct) (datap);
110 datap->nip = NULL;
113 static int
114 internal_function
115 __internal_setnetgrent_reuse (const char *group, struct __netgrent *datap,
116 int *errnop)
118 union
120 enum nss_status (*f) (const char *, struct __netgrent *);
121 void *ptr;
122 } fct;
123 enum nss_status status = NSS_STATUS_UNAVAIL;
124 struct name_list *new_elem;
126 /* Free data from previous service. */
127 endnetgrent_hook (datap);
129 /* Cycle through all the services and run their setnetgrent functions. */
130 int no_more = setup (&fct.ptr, &datap->nip);
131 while (! no_more)
133 assert (datap->data == NULL);
135 /* Ignore status, we force check in `__nss_next2'. */
136 status = (*fct.f) (group, datap);
138 service_user *old_nip = datap->nip;
139 no_more = __nss_next2 (&datap->nip, "setnetgrent", NULL, &fct.ptr,
140 status, 0);
142 if (status == NSS_STATUS_SUCCESS && ! no_more)
144 enum nss_status (*endfct) (struct __netgrent *);
146 endfct = __nss_lookup_function (old_nip, "endnetgrent");
147 if (endfct != NULL)
148 (void) (*endfct) (datap);
152 /* Add the current group to the list of known groups. */
153 size_t group_len = strlen (group) + 1;
154 new_elem = (struct name_list *) malloc (sizeof (struct name_list)
155 + group_len);
156 if (new_elem == NULL)
158 *errnop = errno;
159 status = NSS_STATUS_TRYAGAIN;
161 else
163 new_elem->next = datap->known_groups;
164 memcpy (new_elem->name, group, group_len);
165 datap->known_groups = new_elem;
168 return status == NSS_STATUS_SUCCESS;
171 int internal_setnetgrent (const char *group, struct __netgrent *datap);
172 libc_hidden_proto (internal_setnetgrent)
175 internal_setnetgrent (const char *group, struct __netgrent *datap)
177 /* Free list of all netgroup names from last run. */
178 free_memory (datap);
180 return __internal_setnetgrent_reuse (group, datap, &errno);
182 libc_hidden_def (internal_setnetgrent)
183 strong_alias (internal_setnetgrent, __internal_setnetgrent)
186 setnetgrent (const char *group)
188 int result;
190 __libc_lock_lock (lock);
192 result = internal_setnetgrent (group, &dataset);
194 __libc_lock_unlock (lock);
196 return result;
199 void internal_endnetgrent (struct __netgrent *datap);
200 libc_hidden_proto (internal_endnetgrent)
202 void
203 internal_endnetgrent (struct __netgrent *datap)
205 endnetgrent_hook (datap);
206 /* Now free list of all netgroup names from last run. */
207 free_memory (datap);
209 libc_hidden_def (internal_endnetgrent)
210 strong_alias (internal_endnetgrent, __internal_endnetgrent)
213 void
214 endnetgrent (void)
216 __libc_lock_lock (lock);
218 internal_endnetgrent (&dataset);
220 __libc_lock_unlock (lock);
224 int internal_getnetgrent_r (char **hostp, char **userp, char **domainp,
225 struct __netgrent *datap,
226 char *buffer, size_t buflen, int *errnop);
227 libc_hidden_proto (internal_getnetgrent_r)
230 internal_getnetgrent_r (char **hostp, char **userp, char **domainp,
231 struct __netgrent *datap,
232 char *buffer, size_t buflen, int *errnop)
234 enum nss_status (*fct) (struct __netgrent *, char *, size_t, int *);
236 /* Initialize status to return if no more functions are found. */
237 enum nss_status status = NSS_STATUS_NOTFOUND;
239 /* Run through available functions, starting with the same function last
240 run. We will repeat each function as long as it succeeds, and then go
241 on to the next service action. */
242 int no_more = (datap->nip == NULL
243 || (fct = __nss_lookup_function (datap->nip, "getnetgrent_r"))
244 == NULL);
245 while (! no_more)
247 status = (*fct) (datap, buffer, buflen, &errno);
249 if (status == NSS_STATUS_RETURN)
251 /* This was the last one for this group. Look at next group
252 if available. */
253 int found = 0;
254 while (datap->needed_groups != NULL && ! found)
256 struct name_list *tmp = datap->needed_groups;
257 datap->needed_groups = datap->needed_groups->next;
258 tmp->next = datap->known_groups;
259 datap->known_groups = tmp;
261 found = __internal_setnetgrent_reuse (datap->known_groups->name,
262 datap, errnop);
265 if (found && datap->nip != NULL)
267 fct = __nss_lookup_function (datap->nip, "getnetgrent_r");
268 if (fct != NULL)
269 continue;
272 else if (status == NSS_STATUS_SUCCESS && datap->type == group_val)
274 /* The last entry was a name of another netgroup. */
275 struct name_list *namep;
277 /* Ignore if we've seen the name before. */
278 for (namep = datap->known_groups; namep != NULL;
279 namep = namep->next)
280 if (strcmp (datap->val.group, namep->name) == 0)
281 break;
282 if (namep != NULL)
283 /* Really ignore. */
284 continue;
286 size_t group_len = strlen (datap->val.group) + 1;
287 namep = (struct name_list *) malloc (sizeof (struct name_list)
288 + group_len);
289 if (namep == NULL)
290 /* We are out of memory. */
291 status = NSS_STATUS_RETURN;
292 else
294 namep->next = datap->needed_groups;
295 memcpy (namep->name, datap->val.group, group_len);
296 datap->needed_groups = namep;
297 /* And get the next entry. */
298 continue;
302 break;
305 if (status == NSS_STATUS_SUCCESS)
307 *hostp = (char *) datap->val.triple.host;
308 *userp = (char *) datap->val.triple.user;
309 *domainp = (char *) datap->val.triple.domain;
312 return status == NSS_STATUS_SUCCESS ? 1 : 0;
314 libc_hidden_def (internal_getnetgrent_r)
315 strong_alias (internal_getnetgrent_r, __internal_getnetgrent_r)
317 /* The real entry point. */
319 __getnetgrent_r (char **hostp, char **userp, char **domainp,
320 char *buffer, size_t buflen)
322 enum nss_status status;
324 __libc_lock_lock (lock);
326 status = internal_getnetgrent_r (hostp, userp, domainp, &dataset,
327 buffer, buflen, &errno);
329 __libc_lock_unlock (lock);
331 return status;
333 weak_alias (__getnetgrent_r, getnetgrent_r)
335 /* Test whether given (host,user,domain) triple is in NETGROUP. */
337 innetgr (const char *netgroup, const char *host, const char *user,
338 const char *domain)
340 union
342 int (*f) (const char *, struct __netgrent *);
343 void *ptr;
344 } setfct;
345 void (*endfct) (struct __netgrent *);
346 int (*getfct) (struct __netgrent *, char *, size_t, int *);
347 struct __netgrent entry;
348 int result = 0;
349 const char *current_group = netgroup;
350 int real_entry = 0;
352 memset (&entry, '\0', sizeof (entry));
354 /* Walk through the services until we found an answer or we shall
355 not work further. We can do some optimization here. Since all
356 services must provide the `setnetgrent' function we can do all
357 the work during one walk through the service list. */
358 while (1)
360 int no_more = setup (&setfct.ptr, &entry.nip);
361 while (! no_more)
363 assert (entry.data == NULL);
365 /* Open netgroup. */
366 enum nss_status status = (*setfct.f) (current_group, &entry);
368 if (status == NSS_STATUS_SUCCESS
369 && (getfct = __nss_lookup_function (entry.nip, "getnetgrent_r"))
370 != NULL)
372 char buffer[1024];
374 while ((*getfct) (&entry, buffer, sizeof buffer, &errno)
375 == NSS_STATUS_SUCCESS)
377 if (entry.type == group_val)
379 /* Make sure we haven't seen the name before. */
380 struct name_list *namep;
382 for (namep = entry.known_groups; namep != NULL;
383 namep = namep->next)
384 if (strcmp (entry.val.group, namep->name) == 0)
385 break;
386 if (namep == NULL
387 && strcmp (netgroup, entry.val.group) != 0)
389 size_t group_len = strlen (entry.val.group) + 1;
390 namep =
391 (struct name_list *) malloc (sizeof (*namep)
392 + group_len);
393 if (namep == NULL)
395 /* Out of memory, simply return. */
396 result = -1;
397 break;
400 namep->next = entry.needed_groups;
401 memcpy (namep->name, entry.val.group, group_len);
402 entry.needed_groups = namep;
405 else
407 real_entry = 1;
409 if ((entry.val.triple.host == NULL || host == NULL
410 || __strcasecmp (entry.val.triple.host, host) == 0)
411 && (entry.val.triple.user == NULL || user == NULL
412 || strcmp (entry.val.triple.user, user) == 0)
413 && (entry.val.triple.domain == NULL || domain == NULL
414 || __strcasecmp (entry.val.triple.domain,
415 domain) == 0))
417 result = 1;
418 break;
423 /* If we found one service which does know the given
424 netgroup we don't try further. */
425 status = NSS_STATUS_RETURN;
428 /* Free all resources of the service. */
429 endfct = __nss_lookup_function (entry.nip, "endnetgrent");
430 if (endfct != NULL)
431 (*endfct) (&entry);
433 if (result != 0)
434 break;
436 /* Look for the next service. */
437 no_more = __nss_next2 (&entry.nip, "setnetgrent", NULL,
438 &setfct.ptr, status, 0);
441 if (result == 0 && entry.needed_groups != NULL)
443 struct name_list *tmp = entry.needed_groups;
444 entry.needed_groups = tmp->next;
445 tmp->next = entry.known_groups;
446 entry.known_groups = tmp;
447 current_group = entry.known_groups->name;
448 continue;
451 /* No way out. */
452 break;
455 /* Free the memory. */
456 free_memory (&entry);
458 return result == 1;
460 libc_hidden_def (innetgr)