1 /* Copyright (C) 1996-2014 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/>. */
20 #include <bits/libc-lock.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 /* The lookup function for the first entry of this service. */
40 extern int __nss_netgroup_lookup (service_user
**nipp
, const char *name
,
41 void **fctp
) internal_function
;
43 /* Set up NIP to run through the services. Return nonzero if there are no
46 setup (void **fctp
, service_user
**nipp
)
48 /* Remember the first service_entry, it's always the same. */
49 static bool startp_initialized
;
50 static service_user
*startp
;
53 if (!startp_initialized
)
55 /* Executing this more than once at the same time must yield the
56 same result every time. So we need no locking. */
57 no_more
= __nss_netgroup_lookup (nipp
, "setnetgrent", fctp
);
58 startp
= no_more
? (service_user
*) -1 : *nipp
;
62 atomic_write_barrier ();
63 startp_initialized
= true;
67 service_user
*nip
= startp
;
71 if (nip
== (service_user
*) -1)
72 /* No services at all. */
75 /* Reset to the beginning of the service list. */
77 /* Look up the first function. */
78 no_more
= __nss_lookup (nipp
, "setnetgrent", NULL
, fctp
);
83 /* Free used memory. */
85 free_memory (struct __netgrent
*data
)
87 while (data
->known_groups
!= NULL
)
89 struct name_list
*tmp
= data
->known_groups
;
90 data
->known_groups
= data
->known_groups
->next
;
94 while (data
->needed_groups
!= NULL
)
96 struct name_list
*tmp
= data
->needed_groups
;
97 data
->needed_groups
= data
->needed_groups
->next
;
103 endnetgrent_hook (struct __netgrent
*datap
)
105 enum nss_status (*endfct
) (struct __netgrent
*);
107 if (datap
->nip
== NULL
|| datap
->nip
== (service_user
*) -1l)
110 endfct
= __nss_lookup_function (datap
->nip
, "endnetgrent");
112 (void) (*endfct
) (datap
);
118 __internal_setnetgrent_reuse (const char *group
, struct __netgrent
*datap
,
123 enum nss_status (*f
) (const char *, struct __netgrent
*);
126 enum nss_status status
= NSS_STATUS_UNAVAIL
;
127 struct name_list
*new_elem
;
129 /* Free data from previous service. */
130 endnetgrent_hook (datap
);
132 /* Cycle through all the services and run their setnetgrent functions. */
133 int no_more
= setup (&fct
.ptr
, &datap
->nip
);
136 assert (datap
->data
== NULL
);
138 /* Ignore status, we force check in `__nss_next2'. */
139 status
= DL_CALL_FCT (*fct
.f
, (group
, datap
));
141 service_user
*old_nip
= datap
->nip
;
142 no_more
= __nss_next2 (&datap
->nip
, "setnetgrent", NULL
, &fct
.ptr
,
145 if (status
== NSS_STATUS_SUCCESS
&& ! no_more
)
147 enum nss_status (*endfct
) (struct __netgrent
*);
149 endfct
= __nss_lookup_function (old_nip
, "endnetgrent");
151 (void) DL_CALL_FCT (*endfct
, (datap
));
155 /* Add the current group to the list of known groups. */
156 size_t group_len
= strlen (group
) + 1;
157 new_elem
= (struct name_list
*) malloc (sizeof (struct name_list
)
159 if (new_elem
== NULL
)
162 status
= NSS_STATUS_TRYAGAIN
;
166 new_elem
->next
= datap
->known_groups
;
167 memcpy (new_elem
->name
, group
, group_len
);
168 datap
->known_groups
= new_elem
;
171 return status
== NSS_STATUS_SUCCESS
;
176 __internal_setnetgrent (const char *group
, struct __netgrent
*datap
)
178 /* Free list of all netgroup names from last run. */
181 return __internal_setnetgrent_reuse (group
, datap
, &errno
);
183 libc_hidden_def (__internal_setnetgrent
)
186 nscd_setnetgrent (const char *group
)
189 if (__nss_not_use_nscd_netgroup
> 0
190 && ++__nss_not_use_nscd_netgroup
> NSS_NSCD_RETRY
)
191 __nss_not_use_nscd_netgroup
= 0;
193 if (!__nss_not_use_nscd_netgroup
194 && !__nss_database_custom
[NSS_DBSIDX_netgroup
])
195 return __nscd_setnetgrent (group
, &dataset
);
201 setnetgrent (const char *group
)
205 __libc_lock_lock (lock
);
207 result
= nscd_setnetgrent (group
);
209 result
= __internal_setnetgrent (group
, &dataset
);
211 __libc_lock_unlock (lock
);
218 __internal_endnetgrent (struct __netgrent
*datap
)
220 endnetgrent_hook (datap
);
221 /* Now free list of all netgroup names from last run. */
224 libc_hidden_def (__internal_endnetgrent
)
230 __libc_lock_lock (lock
);
232 __internal_endnetgrent (&dataset
);
234 __libc_lock_unlock (lock
);
239 get_nonempty_val (const char *in
)
246 static enum nss_status
247 nscd_getnetgrent (struct __netgrent
*datap
, char *buffer
, size_t buflen
,
250 if (datap
->cursor
>= datap
->data
+ datap
->data_size
)
251 return NSS_STATUS_UNAVAIL
;
253 datap
->type
= triple_val
;
254 datap
->val
.triple
.host
= get_nonempty_val (datap
->cursor
);
255 datap
->cursor
= (char *) __rawmemchr (datap
->cursor
, '\0') + 1;
256 datap
->val
.triple
.user
= get_nonempty_val (datap
->cursor
);
257 datap
->cursor
= (char *) __rawmemchr (datap
->cursor
, '\0') + 1;
258 datap
->val
.triple
.domain
= get_nonempty_val (datap
->cursor
);
259 datap
->cursor
= (char *) __rawmemchr (datap
->cursor
, '\0') + 1;
261 return NSS_STATUS_SUCCESS
;
267 __internal_getnetgrent_r (char **hostp
, char **userp
, char **domainp
,
268 struct __netgrent
*datap
,
269 char *buffer
, size_t buflen
, int *errnop
)
271 enum nss_status (*fct
) (struct __netgrent
*, char *, size_t, int *);
273 /* Initialize status to return if no more functions are found. */
274 enum nss_status status
= NSS_STATUS_NOTFOUND
;
276 /* Run through available functions, starting with the same function last
277 run. We will repeat each function as long as it succeeds, and then go
278 on to the next service action. */
279 int no_more
= datap
->nip
== NULL
;
283 /* This bogus function pointer is a special marker left by
284 __nscd_setnetgrent to tell us to use the data it left
285 before considering any modules. */
286 if (datap
->nip
== (service_user
*) -1l)
287 fct
= nscd_getnetgrent
;
291 fct
= __nss_lookup_function (datap
->nip
, "getnetgrent_r");
292 no_more
= fct
== NULL
;
298 status
= DL_CALL_FCT (*fct
, (datap
, buffer
, buflen
, &errno
));
300 if (status
== NSS_STATUS_RETURN
301 /* The service returned a NOTFOUND, but there are more groups that we
302 need to resolve before we give up. */
303 || (status
== NSS_STATUS_NOTFOUND
&& datap
->needed_groups
!= NULL
))
305 /* This was the last one for this group. Look at next group
308 while (datap
->needed_groups
!= NULL
&& ! found
)
310 struct name_list
*tmp
= datap
->needed_groups
;
311 datap
->needed_groups
= datap
->needed_groups
->next
;
312 tmp
->next
= datap
->known_groups
;
313 datap
->known_groups
= tmp
;
315 found
= __internal_setnetgrent_reuse (datap
->known_groups
->name
,
319 if (found
&& datap
->nip
!= NULL
)
321 fct
= __nss_lookup_function (datap
->nip
, "getnetgrent_r");
326 else if (status
== NSS_STATUS_SUCCESS
&& datap
->type
== group_val
)
328 /* The last entry was a name of another netgroup. */
329 struct name_list
*namep
;
331 /* Ignore if we've seen the name before. */
332 for (namep
= datap
->known_groups
; namep
!= NULL
;
334 if (strcmp (datap
->val
.group
, namep
->name
) == 0)
337 for (namep
= datap
->needed_groups
; namep
!= NULL
;
339 if (strcmp (datap
->val
.group
, namep
->name
) == 0)
345 size_t group_len
= strlen (datap
->val
.group
) + 1;
346 namep
= (struct name_list
*) malloc (sizeof (struct name_list
)
349 /* We are out of memory. */
350 status
= NSS_STATUS_RETURN
;
353 namep
->next
= datap
->needed_groups
;
354 memcpy (namep
->name
, datap
->val
.group
, group_len
);
355 datap
->needed_groups
= namep
;
356 /* And get the next entry. */
364 if (status
== NSS_STATUS_SUCCESS
)
366 *hostp
= (char *) datap
->val
.triple
.host
;
367 *userp
= (char *) datap
->val
.triple
.user
;
368 *domainp
= (char *) datap
->val
.triple
.domain
;
371 return status
== NSS_STATUS_SUCCESS
? 1 : 0;
373 libc_hidden_def (__internal_getnetgrent_r
)
375 /* The real entry point. */
377 __getnetgrent_r (char **hostp
, char **userp
, char **domainp
,
378 char *buffer
, size_t buflen
)
380 enum nss_status status
;
382 __libc_lock_lock (lock
);
384 status
= __internal_getnetgrent_r (hostp
, userp
, domainp
, &dataset
,
385 buffer
, buflen
, &errno
);
387 __libc_lock_unlock (lock
);
391 weak_alias (__getnetgrent_r
, getnetgrent_r
)
393 /* Test whether given (host,user,domain) triple is in NETGROUP. */
395 innetgr (const char *netgroup
, const char *host
, const char *user
,
399 if (__nss_not_use_nscd_netgroup
> 0
400 && ++__nss_not_use_nscd_netgroup
> NSS_NSCD_RETRY
)
401 __nss_not_use_nscd_netgroup
= 0;
403 if (!__nss_not_use_nscd_netgroup
404 && !__nss_database_custom
[NSS_DBSIDX_netgroup
])
406 int result
= __nscd_innetgr (netgroup
, host
, user
, domain
);
414 enum nss_status (*f
) (const char *, struct __netgrent
*);
417 void (*endfct
) (struct __netgrent
*);
418 int (*getfct
) (struct __netgrent
*, char *, size_t, int *);
419 struct __netgrent entry
;
421 const char *current_group
= netgroup
;
423 memset (&entry
, '\0', sizeof (entry
));
425 /* Walk through the services until we found an answer or we shall
426 not work further. We can do some optimization here. Since all
427 services must provide the `setnetgrent' function we can do all
428 the work during one walk through the service list. */
431 int no_more
= setup (&setfct
.ptr
, &entry
.nip
);
434 assert (entry
.data
== NULL
);
437 enum nss_status status
= DL_CALL_FCT (*setfct
.f
,
438 (current_group
, &entry
));
440 if (status
== NSS_STATUS_SUCCESS
441 && (getfct
= __nss_lookup_function (entry
.nip
, "getnetgrent_r"))
446 while (DL_CALL_FCT (*getfct
,
447 (&entry
, buffer
, sizeof buffer
, &errno
))
448 == NSS_STATUS_SUCCESS
)
450 if (entry
.type
== group_val
)
452 /* Make sure we haven't seen the name before. */
453 struct name_list
*namep
;
455 for (namep
= entry
.known_groups
; namep
!= NULL
;
457 if (strcmp (entry
.val
.group
, namep
->name
) == 0)
460 for (namep
= entry
.needed_groups
; namep
!= NULL
;
462 if (strcmp (entry
.val
.group
, namep
->name
) == 0)
465 && strcmp (netgroup
, entry
.val
.group
) != 0)
467 size_t group_len
= strlen (entry
.val
.group
) + 1;
469 (struct name_list
*) malloc (sizeof (*namep
)
473 /* Out of memory, simply return. */
478 namep
->next
= entry
.needed_groups
;
479 memcpy (namep
->name
, entry
.val
.group
, group_len
);
480 entry
.needed_groups
= namep
;
485 if ((entry
.val
.triple
.host
== NULL
|| host
== NULL
486 || __strcasecmp (entry
.val
.triple
.host
, host
) == 0)
487 && (entry
.val
.triple
.user
== NULL
|| user
== NULL
488 || strcmp (entry
.val
.triple
.user
, user
) == 0)
489 && (entry
.val
.triple
.domain
== NULL
|| domain
== NULL
490 || __strcasecmp (entry
.val
.triple
.domain
,
499 /* If we found one service which does know the given
500 netgroup we don't try further. */
501 status
= NSS_STATUS_RETURN
;
504 /* Free all resources of the service. */
505 endfct
= __nss_lookup_function (entry
.nip
, "endnetgrent");
507 DL_CALL_FCT (*endfct
, (&entry
));
512 /* Look for the next service. */
513 no_more
= __nss_next2 (&entry
.nip
, "setnetgrent", NULL
,
514 &setfct
.ptr
, status
, 0);
517 if (result
== 0 && entry
.needed_groups
!= NULL
)
519 struct name_list
*tmp
= entry
.needed_groups
;
520 entry
.needed_groups
= tmp
->next
;
521 tmp
->next
= entry
.known_groups
;
522 entry
.known_groups
= tmp
;
523 current_group
= tmp
->name
;
531 /* Free the memory. */
532 free_memory (&entry
);
536 libc_hidden_def (innetgr
)