Remove "[Add new features here]" for 2.27
[glibc.git] / inet / getnetgrent_r.c
blob89fefeb612be4d12d669aea8a42d607e7e3c6f3d
1 /* Copyright (C) 1996-2017 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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <assert.h>
19 #include <atomic.h>
20 #include <libc-lock.h>
21 #include <errno.h>
22 #include <netdb.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "netgroup.h"
27 #include "nsswitch.h"
28 #include <sysdep.h>
29 #include <nscd/nscd_proto.h>
32 /* Protect above variable against multiple uses at the same time. */
33 __libc_lock_define_initialized (static, lock)
35 /* The whole information for the set/get/endnetgrent functions are
36 kept in this structure. */
37 static struct __netgrent dataset;
39 /* Set up NIP to run through the services. Return nonzero if there are no
40 services (left). */
41 static int
42 setup (void **fctp, service_user **nipp)
44 /* Remember the first service_entry, it's always the same. */
45 static bool startp_initialized;
46 static service_user *startp;
47 int no_more;
49 if (!startp_initialized)
51 /* Executing this more than once at the same time must yield the
52 same result every time. So we need no locking. */
53 no_more = __nss_netgroup_lookup2 (nipp, "setnetgrent", NULL, fctp);
54 startp = no_more ? (service_user *) -1 : *nipp;
55 #ifdef PTR_MANGLE
56 PTR_MANGLE (startp);
57 #endif
58 atomic_write_barrier ();
59 startp_initialized = true;
61 else
63 service_user *nip = startp;
64 #ifdef PTR_DEMANGLE
65 PTR_DEMANGLE (nip);
66 #endif
67 if (nip == (service_user *) -1)
68 /* No services at all. */
69 return 1;
71 /* Reset to the beginning of the service list. */
72 *nipp = nip;
73 /* Look up the first function. */
74 no_more = __nss_lookup (nipp, "setnetgrent", NULL, fctp);
76 return no_more;
79 /* Free used memory. */
80 static void
81 free_memory (struct __netgrent *data)
83 while (data->known_groups != NULL)
85 struct name_list *tmp = data->known_groups;
86 data->known_groups = data->known_groups->next;
87 free (tmp);
90 while (data->needed_groups != NULL)
92 struct name_list *tmp = data->needed_groups;
93 data->needed_groups = data->needed_groups->next;
94 free (tmp);
98 static void
99 endnetgrent_hook (struct __netgrent *datap)
101 enum nss_status (*endfct) (struct __netgrent *);
103 if (datap->nip == NULL || datap->nip == (service_user *) -1l)
104 return;
106 endfct = __nss_lookup_function (datap->nip, "endnetgrent");
107 if (endfct != NULL)
108 (void) (*endfct) (datap);
109 datap->nip = NULL;
112 static int
113 internal_function
114 __internal_setnetgrent_reuse (const char *group, struct __netgrent *datap,
115 int *errnop)
117 union
119 enum nss_status (*f) (const char *, struct __netgrent *);
120 void *ptr;
121 } fct;
122 enum nss_status status = NSS_STATUS_UNAVAIL;
123 struct name_list *new_elem;
125 /* Free data from previous service. */
126 endnetgrent_hook (datap);
128 /* Cycle through all the services and run their setnetgrent functions. */
129 int no_more = setup (&fct.ptr, &datap->nip);
130 while (! no_more)
132 assert (datap->data == NULL);
134 /* Ignore status, we force check in `__nss_next2'. */
135 status = DL_CALL_FCT (*fct.f, (group, datap));
137 service_user *old_nip = datap->nip;
138 no_more = __nss_next2 (&datap->nip, "setnetgrent", NULL, &fct.ptr,
139 status, 0);
141 if (status == NSS_STATUS_SUCCESS && ! no_more)
143 enum nss_status (*endfct) (struct __netgrent *);
145 endfct = __nss_lookup_function (old_nip, "endnetgrent");
146 if (endfct != NULL)
147 (void) DL_CALL_FCT (*endfct, (datap));
151 /* Add the current group to the list of known groups. */
152 size_t group_len = strlen (group) + 1;
153 new_elem = (struct name_list *) malloc (sizeof (struct name_list)
154 + group_len);
155 if (new_elem == NULL)
157 *errnop = errno;
158 status = NSS_STATUS_TRYAGAIN;
160 else
162 new_elem->next = datap->known_groups;
163 memcpy (new_elem->name, group, group_len);
164 datap->known_groups = new_elem;
167 return status == NSS_STATUS_SUCCESS;
171 __internal_setnetgrent (const char *group, struct __netgrent *datap)
173 /* Free list of all netgroup names from last run. */
174 free_memory (datap);
176 return __internal_setnetgrent_reuse (group, datap, &errno);
178 libc_hidden_def (__internal_setnetgrent)
180 static int
181 nscd_setnetgrent (const char *group)
183 #ifdef USE_NSCD
184 if (__nss_not_use_nscd_netgroup > 0
185 && ++__nss_not_use_nscd_netgroup > NSS_NSCD_RETRY)
186 __nss_not_use_nscd_netgroup = 0;
188 if (!__nss_not_use_nscd_netgroup
189 && !__nss_database_custom[NSS_DBSIDX_netgroup])
190 return __nscd_setnetgrent (group, &dataset);
191 #endif
192 return -1;
196 setnetgrent (const char *group)
198 int result;
200 __libc_lock_lock (lock);
202 result = nscd_setnetgrent (group);
203 if (result < 0)
204 result = __internal_setnetgrent (group, &dataset);
206 __libc_lock_unlock (lock);
208 return result;
211 void
212 __internal_endnetgrent (struct __netgrent *datap)
214 endnetgrent_hook (datap);
215 /* Now free list of all netgroup names from last run. */
216 free_memory (datap);
218 libc_hidden_def (__internal_endnetgrent)
221 void
222 endnetgrent (void)
224 __libc_lock_lock (lock);
226 __internal_endnetgrent (&dataset);
228 __libc_lock_unlock (lock);
231 #ifdef USE_NSCD
232 static const char *
233 get_nonempty_val (const char *in)
235 if (*in == '\0')
236 return NULL;
237 return in;
240 static enum nss_status
241 nscd_getnetgrent (struct __netgrent *datap, char *buffer, size_t buflen,
242 int *errnop)
244 if (datap->cursor >= datap->data + datap->data_size)
245 return NSS_STATUS_UNAVAIL;
247 datap->type = triple_val;
248 datap->val.triple.host = get_nonempty_val (datap->cursor);
249 datap->cursor = (char *) __rawmemchr (datap->cursor, '\0') + 1;
250 datap->val.triple.user = get_nonempty_val (datap->cursor);
251 datap->cursor = (char *) __rawmemchr (datap->cursor, '\0') + 1;
252 datap->val.triple.domain = get_nonempty_val (datap->cursor);
253 datap->cursor = (char *) __rawmemchr (datap->cursor, '\0') + 1;
255 return NSS_STATUS_SUCCESS;
257 #endif
260 __internal_getnetgrent_r (char **hostp, char **userp, char **domainp,
261 struct __netgrent *datap,
262 char *buffer, size_t buflen, int *errnop)
264 enum nss_status (*fct) (struct __netgrent *, char *, size_t, int *);
266 /* Initialize status to return if no more functions are found. */
267 enum nss_status status = NSS_STATUS_NOTFOUND;
269 /* Run through available functions, starting with the same function last
270 run. We will repeat each function as long as it succeeds, and then go
271 on to the next service action. */
272 int no_more = datap->nip == NULL;
273 if (! no_more)
275 #ifdef USE_NSCD
276 /* This bogus function pointer is a special marker left by
277 __nscd_setnetgrent to tell us to use the data it left
278 before considering any modules. */
279 if (datap->nip == (service_user *) -1l)
280 fct = nscd_getnetgrent;
281 else
282 #endif
284 fct = __nss_lookup_function (datap->nip, "getnetgrent_r");
285 no_more = fct == NULL;
288 while (! no_more)
290 status = DL_CALL_FCT (*fct, (datap, buffer, buflen, &errno));
292 if (status == NSS_STATUS_RETURN
293 /* The service returned a NOTFOUND, but there are more groups that
294 we need to resolve before we give up. */
295 || (status == NSS_STATUS_NOTFOUND && datap->needed_groups != NULL))
297 /* This was the last one for this group. Look at next group
298 if available. */
299 int found = 0;
300 while (datap->needed_groups != NULL && ! found)
302 struct name_list *tmp = datap->needed_groups;
303 datap->needed_groups = datap->needed_groups->next;
304 tmp->next = datap->known_groups;
305 datap->known_groups = tmp;
307 found = __internal_setnetgrent_reuse (datap->known_groups->name,
308 datap, errnop);
311 if (found && datap->nip != NULL)
313 fct = __nss_lookup_function (datap->nip, "getnetgrent_r");
314 if (fct != NULL)
315 continue;
318 else if (status == NSS_STATUS_SUCCESS && datap->type == group_val)
320 /* The last entry was a name of another netgroup. */
321 struct name_list *namep;
323 /* Ignore if we've seen the name before. */
324 for (namep = datap->known_groups; namep != NULL;
325 namep = namep->next)
326 if (strcmp (datap->val.group, namep->name) == 0)
327 break;
328 if (namep == NULL)
329 for (namep = datap->needed_groups; namep != NULL;
330 namep = namep->next)
331 if (strcmp (datap->val.group, namep->name) == 0)
332 break;
333 if (namep != NULL)
334 /* Really ignore. */
335 continue;
337 size_t group_len = strlen (datap->val.group) + 1;
338 namep = (struct name_list *) malloc (sizeof (struct name_list)
339 + group_len);
340 if (namep == NULL)
341 /* We are out of memory. */
342 status = NSS_STATUS_RETURN;
343 else
345 namep->next = datap->needed_groups;
346 memcpy (namep->name, datap->val.group, group_len);
347 datap->needed_groups = namep;
348 /* And get the next entry. */
349 continue;
352 break;
356 if (status == NSS_STATUS_SUCCESS)
358 *hostp = (char *) datap->val.triple.host;
359 *userp = (char *) datap->val.triple.user;
360 *domainp = (char *) datap->val.triple.domain;
363 return status == NSS_STATUS_SUCCESS ? 1 : 0;
365 libc_hidden_def (__internal_getnetgrent_r)
367 /* The real entry point. */
369 __getnetgrent_r (char **hostp, char **userp, char **domainp,
370 char *buffer, size_t buflen)
372 enum nss_status status;
374 __libc_lock_lock (lock);
376 status = __internal_getnetgrent_r (hostp, userp, domainp, &dataset,
377 buffer, buflen, &errno);
379 __libc_lock_unlock (lock);
381 return status;
383 weak_alias (__getnetgrent_r, getnetgrent_r)
385 /* Test whether given (host,user,domain) triple is in NETGROUP. */
387 innetgr (const char *netgroup, const char *host, const char *user,
388 const char *domain)
390 #ifdef USE_NSCD
391 if (__nss_not_use_nscd_netgroup > 0
392 && ++__nss_not_use_nscd_netgroup > NSS_NSCD_RETRY)
393 __nss_not_use_nscd_netgroup = 0;
395 if (!__nss_not_use_nscd_netgroup
396 && !__nss_database_custom[NSS_DBSIDX_netgroup])
398 int result = __nscd_innetgr (netgroup, host, user, domain);
399 if (result >= 0)
400 return result;
402 #endif
404 union
406 enum nss_status (*f) (const char *, struct __netgrent *);
407 void *ptr;
408 } setfct;
409 void (*endfct) (struct __netgrent *);
410 int (*getfct) (struct __netgrent *, char *, size_t, int *);
411 struct __netgrent entry;
412 int result = 0;
413 const char *current_group = netgroup;
415 memset (&entry, '\0', sizeof (entry));
417 /* Walk through the services until we found an answer or we shall
418 not work further. We can do some optimization here. Since all
419 services must provide the `setnetgrent' function we can do all
420 the work during one walk through the service list. */
421 while (1)
423 int no_more = setup (&setfct.ptr, &entry.nip);
424 while (! no_more)
426 assert (entry.data == NULL);
428 /* Open netgroup. */
429 enum nss_status status = DL_CALL_FCT (*setfct.f,
430 (current_group, &entry));
432 if (status == NSS_STATUS_SUCCESS
433 && (getfct = __nss_lookup_function (entry.nip, "getnetgrent_r"))
434 != NULL)
436 char buffer[1024];
438 while (DL_CALL_FCT (*getfct,
439 (&entry, buffer, sizeof buffer, &errno))
440 == NSS_STATUS_SUCCESS)
442 if (entry.type == group_val)
444 /* Make sure we haven't seen the name before. */
445 struct name_list *namep;
447 for (namep = entry.known_groups; namep != NULL;
448 namep = namep->next)
449 if (strcmp (entry.val.group, namep->name) == 0)
450 break;
451 if (namep == NULL)
452 for (namep = entry.needed_groups; namep != NULL;
453 namep = namep->next)
454 if (strcmp (entry.val.group, namep->name) == 0)
455 break;
456 if (namep == NULL
457 && strcmp (netgroup, entry.val.group) != 0)
459 size_t group_len = strlen (entry.val.group) + 1;
460 namep =
461 (struct name_list *) malloc (sizeof (*namep)
462 + group_len);
463 if (namep == NULL)
465 /* Out of memory, simply return. */
466 result = -1;
467 break;
470 namep->next = entry.needed_groups;
471 memcpy (namep->name, entry.val.group, group_len);
472 entry.needed_groups = namep;
475 else
477 if ((entry.val.triple.host == NULL || host == NULL
478 || __strcasecmp (entry.val.triple.host, host) == 0)
479 && (entry.val.triple.user == NULL || user == NULL
480 || strcmp (entry.val.triple.user, user) == 0)
481 && (entry.val.triple.domain == NULL || domain == NULL
482 || __strcasecmp (entry.val.triple.domain,
483 domain) == 0))
485 result = 1;
486 break;
491 /* If we found one service which does know the given
492 netgroup we don't try further. */
493 status = NSS_STATUS_RETURN;
496 /* Free all resources of the service. */
497 endfct = __nss_lookup_function (entry.nip, "endnetgrent");
498 if (endfct != NULL)
499 DL_CALL_FCT (*endfct, (&entry));
501 if (result != 0)
502 break;
504 /* Look for the next service. */
505 no_more = __nss_next2 (&entry.nip, "setnetgrent", NULL,
506 &setfct.ptr, status, 0);
509 if (result == 0 && entry.needed_groups != NULL)
511 struct name_list *tmp = entry.needed_groups;
512 entry.needed_groups = tmp->next;
513 tmp->next = entry.known_groups;
514 entry.known_groups = tmp;
515 current_group = tmp->name;
516 continue;
519 /* No way out. */
520 break;
523 /* Free the memory. */
524 free_memory (&entry);
526 return result == 1;
528 libc_hidden_def (innetgr)