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
22 #include <bits/libc-lock.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
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
;
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
;
61 atomic_write_barrier ();
62 startp_initialized
= true;
66 service_user
*nip
= startp
;
68 if (nip
== (service_user
*) -1)
69 /* No services at all. */
72 /* Reset to the beginning of the service list. */
74 /* Look up the first function. */
75 no_more
= __nss_lookup (nipp
, "setnetgrent", NULL
, fctp
);
80 /* Free used memory. */
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
;
91 while (data
->needed_groups
!= NULL
)
93 struct name_list
*tmp
= data
->needed_groups
;
94 data
->needed_groups
= data
->needed_groups
->next
;
100 endnetgrent_hook (struct __netgrent
*datap
)
102 enum nss_status (*endfct
) (struct __netgrent
*);
104 if (datap
->nip
== NULL
)
107 endfct
= __nss_lookup_function (datap
->nip
, "endnetgrent");
109 (void) (*endfct
) (datap
);
115 __internal_setnetgrent_reuse (const char *group
, struct __netgrent
*datap
,
120 enum nss_status (*f
) (const char *, struct __netgrent
*);
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
);
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
,
142 if (status
== NSS_STATUS_SUCCESS
&& ! no_more
)
144 enum nss_status (*endfct
) (struct __netgrent
*);
146 endfct
= __nss_lookup_function (old_nip
, "endnetgrent");
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
)
156 if (new_elem
== NULL
)
159 status
= NSS_STATUS_TRYAGAIN
;
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. */
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
)
190 __libc_lock_lock (lock
);
192 result
= internal_setnetgrent (group
, &dataset
);
194 __libc_lock_unlock (lock
);
199 void internal_endnetgrent (struct __netgrent
*datap
);
200 libc_hidden_proto (internal_endnetgrent
)
203 internal_endnetgrent (struct __netgrent
*datap
)
205 endnetgrent_hook (datap
);
206 /* Now free list of all netgroup names from last run. */
209 libc_hidden_def (internal_endnetgrent
)
210 strong_alias (internal_endnetgrent
, __internal_endnetgrent
)
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"))
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
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
,
265 if (found
&& datap
->nip
!= NULL
)
267 fct
= __nss_lookup_function (datap
->nip
, "getnetgrent_r");
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
;
280 if (strcmp (datap
->val
.group
, namep
->name
) == 0)
286 size_t group_len
= strlen (datap
->val
.group
) + 1;
287 namep
= (struct name_list
*) malloc (sizeof (struct name_list
)
290 /* We are out of memory. */
291 status
= NSS_STATUS_RETURN
;
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. */
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
);
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
,
342 int (*f
) (const char *, struct __netgrent
*);
345 void (*endfct
) (struct __netgrent
*);
346 int (*getfct
) (struct __netgrent
*, char *, size_t, int *);
347 struct __netgrent entry
;
349 const char *current_group
= netgroup
;
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. */
360 int no_more
= setup (&setfct
.ptr
, &entry
.nip
);
363 assert (entry
.data
== NULL
);
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"))
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
;
384 if (strcmp (entry
.val
.group
, namep
->name
) == 0)
387 && strcmp (netgroup
, entry
.val
.group
) != 0)
389 size_t group_len
= strlen (entry
.val
.group
) + 1;
391 (struct name_list
*) malloc (sizeof (*namep
)
395 /* Out of memory, simply return. */
400 namep
->next
= entry
.needed_groups
;
401 memcpy (namep
->name
, entry
.val
.group
, group_len
);
402 entry
.needed_groups
= namep
;
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
,
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");
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
;
455 /* Free the memory. */
456 free_memory (&entry
);
460 libc_hidden_def (innetgr
)