1 /* Copyright (C) 1996,1997,1998,1999,2002,2004 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 #include <bits/libc-lock.h>
28 /* Protect above variable against multiple uses at the same time. */
29 __libc_lock_define_initialized (static, lock
)
31 /* This handle for the NSS data base is shared between all
32 set/get/endXXXent functions. */
33 static service_user
*nip
;
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
**nip
, const char *name
,
41 void **fctp
) internal_function
;
44 /* Set up NIP to run through the services. If ALL is zero, use NIP's
45 current location if it's not nil. Return nonzero if there are no
47 static enum nss_status
48 setup (void **fctp
, const char *func_name
, int all
)
50 /* Remember the first service_entry, it's always the same. */
51 static service_user
*startp
;
56 no_more
= __nss_netgroup_lookup (&nip
, func_name
, fctp
);
57 startp
= no_more
? (service_user
*) -1 : nip
;
59 else if (startp
== (service_user
*) -1)
60 /* No services at all. */
65 /* Reset to the beginning of the service list. */
67 /* Look up the first function. */
68 no_more
= __nss_lookup (&nip
, func_name
, fctp
);
73 /* Free used memory. */
75 free_memory (struct __netgrent
*data
)
77 while (data
->known_groups
!= NULL
)
79 struct name_list
*tmp
= data
->known_groups
;
80 data
->known_groups
= data
->known_groups
->next
;
81 free ((void *) tmp
->name
);
85 while (data
->needed_groups
!= NULL
)
87 struct name_list
*tmp
= data
->needed_groups
;
88 data
->needed_groups
= data
->needed_groups
->next
;
89 free ((void *) tmp
->name
);
96 __internal_setnetgrent_reuse (const char *group
, struct __netgrent
*datap
,
101 enum nss_status (*f
) (const char *, struct __netgrent
*);
104 enum nss_status status
= NSS_STATUS_UNAVAIL
;
105 struct name_list
*new_elem
;
108 /* Cycle through all the services and run their setnetgrent functions. */
109 no_more
= setup (&fct
.ptr
, "setnetgrent", 1);
112 /* Ignore status, we force check in `__nss_next'. */
113 status
= (*fct
.f
) (group
, datap
);
115 no_more
= __nss_next (&nip
, "setnetgrent", &fct
.ptr
, status
, 0);
118 /* Add the current group to the list of known groups. */
119 new_elem
= (struct name_list
*) malloc (sizeof (struct name_list
));
120 if (new_elem
== NULL
|| (new_elem
->name
= __strdup (group
)) == NULL
)
122 if (new_elem
!= NULL
)
125 status
= NSS_STATUS_TRYAGAIN
;
129 new_elem
->next
= datap
->known_groups
;
130 datap
->known_groups
= new_elem
;
133 return status
== NSS_STATUS_SUCCESS
;
136 int internal_setnetgrent (const char *group
, struct __netgrent
*datap
);
137 libc_hidden_proto (internal_setnetgrent
)
140 internal_setnetgrent (const char *group
, struct __netgrent
*datap
)
142 /* Free list of all netgroup names from last run. */
145 return __internal_setnetgrent_reuse (group
, datap
, &errno
);
147 libc_hidden_def (internal_setnetgrent
)
148 strong_alias (internal_setnetgrent
, __internal_setnetgrent
)
151 setnetgrent (const char *group
)
155 __libc_lock_lock (lock
);
157 result
= internal_setnetgrent (group
, &dataset
);
159 __libc_lock_unlock (lock
);
165 void internal_endnetgrent (struct __netgrent
*datap
);
166 libc_hidden_proto (internal_endnetgrent
)
169 internal_endnetgrent (struct __netgrent
*datap
)
171 service_user
*old_nip
;
174 enum nss_status (*f
) (struct __netgrent
*);
179 /* Remember which was the last used service. */
182 /* Cycle through all the services and run their endnetgrent functions. */
183 no_more
= setup (&fct
.ptr
, "endnetgrent", 1);
186 /* Ignore status, we force check in `__nss_next'. */
187 (void) (*fct
.f
) (datap
);
189 no_more
= (nip
== old_nip
190 || __nss_next (&nip
, "endnetgrent", &fct
.ptr
, 0, 1));
193 /* Now free list of all netgroup names from last run. */
196 libc_hidden_def (internal_endnetgrent
)
197 strong_alias (internal_endnetgrent
, __internal_endnetgrent
)
203 __libc_lock_lock (lock
);
205 internal_endnetgrent (&dataset
);
207 __libc_lock_unlock (lock
);
211 int internal_getnetgrent_r (char **hostp
, char **userp
, char **domainp
,
212 struct __netgrent
*datap
,
213 char *buffer
, size_t buflen
, int *errnop
);
214 libc_hidden_proto (internal_getnetgrent_r
)
217 internal_getnetgrent_r (char **hostp
, char **userp
, char **domainp
,
218 struct __netgrent
*datap
,
219 char *buffer
, size_t buflen
, int *errnop
)
223 enum nss_status (*f
) (struct __netgrent
*, char *, size_t, int *);
228 /* Initialize status to return if no more functions are found. */
229 enum nss_status status
= NSS_STATUS_NOTFOUND
;
231 /* Run through available functions, starting with the same function last
232 run. We will repeat each function as long as it succeeds, and then go
233 on to the next service action. */
234 no_more
= setup (&fct
.ptr
, "getnetgrent_r", 0);
237 status
= (*fct
.f
) (datap
, buffer
, buflen
, &errno
);
239 if (status
== NSS_STATUS_RETURN
)
241 /* This was the last one for this group. Look at next group
244 while (datap
->needed_groups
!= NULL
&& ! found
)
246 struct name_list
*tmp
= datap
->needed_groups
;
247 datap
->needed_groups
= datap
->needed_groups
->next
;
248 tmp
->next
= datap
->known_groups
;
249 datap
->known_groups
= tmp
;
251 found
= __internal_setnetgrent_reuse (datap
->known_groups
->name
,
258 else if (status
== NSS_STATUS_SUCCESS
&& datap
->type
== group_val
)
260 /* The last entry was a name of another netgroup. */
261 struct name_list
*namep
;
263 /* Ignore if we've seen the name before. */
264 for (namep
= datap
->known_groups
; namep
!= NULL
;
266 if (strcmp (datap
->val
.group
, namep
->name
) == 0)
272 namep
= (struct name_list
*) malloc (sizeof (struct name_list
));
274 || (namep
->name
= __strdup (datap
->val
.group
)) == NULL
)
276 /* We are out of memory. */
279 status
= NSS_STATUS_RETURN
;
283 namep
->next
= datap
->needed_groups
;
284 datap
->needed_groups
= namep
;
285 /* And get the next entry. */
290 no_more
= __nss_next (&nip
, "getnetgrent_r", &fct
.ptr
, status
, 0);
293 if (status
== NSS_STATUS_SUCCESS
)
295 *hostp
= (char *) datap
->val
.triple
.host
;
296 *userp
= (char *) datap
->val
.triple
.user
;
297 *domainp
= (char *) datap
->val
.triple
.domain
;
300 return status
== NSS_STATUS_SUCCESS
? 1 : 0;
302 libc_hidden_def (internal_getnetgrent_r
)
303 strong_alias (internal_getnetgrent_r
, __internal_getnetgrent_r
)
305 /* The real entry point. */
307 __getnetgrent_r (char **hostp
, char **userp
, char **domainp
,
308 char *buffer
, size_t buflen
)
310 enum nss_status status
;
312 __libc_lock_lock (lock
);
314 status
= internal_getnetgrent_r (hostp
, userp
, domainp
, &dataset
,
315 buffer
, buflen
, &errno
);
317 __libc_lock_unlock (lock
);
321 weak_alias (__getnetgrent_r
, getnetgrent_r
)
323 /* Test whether given (host,user,domain) triple is in NETGROUP. */
325 innetgr (const char *netgroup
, const char *host
, const char *user
,
330 int (*f
) (const char *, struct __netgrent
*);
335 void (*f
) (struct __netgrent
*);
340 int (*f
) (struct __netgrent
*, char *, size_t, int *);
343 struct name_list
*known
= NULL
;
344 struct name_list
*needed
= NULL
;
347 const char *current_group
= netgroup
;
350 /* Walk through the services until we found an answer or we shall
351 not work further. We can do some optimization here. Since all
352 services must provide the `setnetgrent' function we can do all
353 the work during one walk through the service list. */
356 no_more
= setup (&setfct
.ptr
, "setnetgrent", 1);
359 enum nss_status status
;
360 struct __netgrent entry
;
362 /* Clear the space for the netgroup data. */
363 __bzero (&entry
, sizeof (entry
));
366 status
= (*setfct
.f
) (current_group
, &entry
);
367 if (status
== NSS_STATUS_SUCCESS
368 && __nss_lookup (&nip
, "getnetgrent_r", &getfct
.ptr
) == 0)
372 while ((*getfct
.f
) (&entry
, buffer
, sizeof buffer
, &errno
)
373 == NSS_STATUS_SUCCESS
)
375 if (entry
.type
== group_val
)
377 /* Make sure we haven't seen the name before. */
378 struct name_list
*namep
;
380 for (namep
= known
; namep
!= NULL
; namep
= namep
->next
)
381 if (strcmp (entry
.val
.group
, namep
->name
) == 0)
384 && strcmp (netgroup
, entry
.val
.group
) != 0)
387 (struct name_list
*) malloc (sizeof (*namep
));
389 || ((namep
->name
= __strdup (entry
.val
.group
))
392 /* Out of memory, simply return. */
399 namep
->next
= needed
;
407 if ((entry
.val
.triple
.host
== NULL
|| host
== NULL
408 || __strcasecmp (entry
.val
.triple
.host
, host
) == 0)
409 && (entry
.val
.triple
.user
== NULL
|| user
== NULL
410 || strcmp (entry
.val
.triple
.user
, user
) == 0)
411 && (entry
.val
.triple
.domain
== NULL
|| domain
== NULL
412 || __strcasecmp (entry
.val
.triple
.domain
,
424 /* If we found one service which does know the given
425 netgroup we don't try further. */
426 status
= NSS_STATUS_RETURN
;
429 /* Free all resources of the service. */
430 if (__nss_lookup (&nip
, "endnetgrent", &endfct
.ptr
) == 0)
431 (*endfct
.f
) (&entry
);
433 /* Look for the next service. */
434 no_more
= __nss_next (&nip
, "setnetgrent",
435 &setfct
.ptr
, status
, 0);
438 if (result
== 0 && needed
!= NULL
)
440 struct name_list
*tmp
= needed
;
444 current_group
= known
->name
;
452 /* Free the memory. */
453 while (known
!= NULL
)
455 struct name_list
*tmp
= known
;
457 free ((void *) tmp
->name
);
460 while (needed
!= NULL
)
462 struct name_list
*tmp
= needed
;
463 needed
= needed
->next
;
464 free ((void *) tmp
->name
);
470 libc_hidden_def (innetgr
)