1 /* Copyright (C) 1996, 1997, 1998 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
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
,
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
= NULL
;
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
,
99 enum nss_status (*fct
) (const char *, struct __netgrent
*);
100 enum nss_status status
= NSS_STATUS_UNAVAIL
;
101 struct name_list
*new_elem
;
104 /* Cycle through all the services and run their setnetgrent functions. */
105 no_more
= setup ((void **) &fct
, "setnetgrent", 1);
108 /* Ignore status, we force check in `__nss_next'. */
109 status
= (*fct
) (group
, datap
);
111 no_more
= __nss_next (&nip
, "setnetgrent", (void **) &fct
, status
, 0);
114 /* Add the current group to the list of known groups. */
115 new_elem
= (struct name_list
*) malloc (sizeof (struct name_list
));
116 if (new_elem
== NULL
|| (new_elem
->name
= __strdup (group
)) == NULL
)
118 if (new_elem
!= NULL
)
121 status
= NSS_STATUS_TRYAGAIN
;
125 new_elem
->next
= datap
->known_groups
;
126 datap
->known_groups
= new_elem
;
129 return status
== NSS_STATUS_SUCCESS
;
133 __internal_setnetgrent (const char *group
, struct __netgrent
*datap
)
135 /* Free list of all netgroup names from last run. */
138 return __internal_setnetgrent_reuse (group
, datap
, &errno
);
142 setnetgrent (const char *group
)
146 __libc_lock_lock (lock
);
148 result
= __internal_setnetgrent (group
, &dataset
);
150 __libc_lock_unlock (lock
);
157 __internal_endnetgrent (struct __netgrent
*datap
)
159 service_user
*old_nip
;
160 enum nss_status (*fct
) (struct __netgrent
*);
163 /* Remember which was the last used service. */
166 /* Cycle through all the services and run their endnetgrent functions. */
167 no_more
= setup ((void **) &fct
, "endnetgrent", 1);
170 /* Ignore status, we force check in `__nss_next'. */
171 (void) (*fct
) (datap
);
173 no_more
= (nip
== old_nip
174 || __nss_next (&nip
, "endnetgrent", (void **) &fct
, 0, 1));
177 /* Now free list of all netgroup names from last run. */
185 __libc_lock_lock (lock
);
187 __internal_endnetgrent (&dataset
);
189 __libc_lock_unlock (lock
);
194 __internal_getnetgrent_r (char **hostp
, char **userp
, char **domainp
,
195 struct __netgrent
*datap
,
196 char *buffer
, size_t buflen
, int *errnop
)
198 enum nss_status (*fct
) (struct __netgrent
*, char *, size_t);
201 /* Initialize status to return if no more functions are found. */
202 enum nss_status status
= NSS_STATUS_NOTFOUND
;
204 /* Run through available functions, starting with the same function last
205 run. We will repeat each function as long as it succeeds, and then go
206 on to the next service action. */
207 no_more
= setup ((void **) &fct
, "getnetgrent_r", 0);
210 status
= (*fct
) (datap
, buffer
, buflen
);
212 if (status
== NSS_STATUS_RETURN
)
214 /* This was the last one for this group. Look at next group
217 while (datap
->needed_groups
!= NULL
&& ! found
)
219 struct name_list
*tmp
= datap
->needed_groups
;
220 datap
->needed_groups
= datap
->needed_groups
->next
;
221 tmp
->next
= datap
->known_groups
;
222 datap
->known_groups
= tmp
;
224 found
= __internal_setnetgrent_reuse (datap
->known_groups
->name
,
231 else if (status
== NSS_STATUS_SUCCESS
&& datap
->type
== group_val
)
233 /* The last entry was a name of another netgroup. */
234 struct name_list
*namep
;
236 /* Ignore if we've seen the name before. */
237 for (namep
= datap
->known_groups
; namep
!= NULL
;
239 if (strcmp (datap
->val
.group
, namep
->name
) == 0)
245 namep
= (struct name_list
*) malloc (sizeof (struct name_list
));
247 || (namep
->name
= __strdup (datap
->val
.group
)) == NULL
)
249 /* We are out of memory. */
252 status
= NSS_STATUS_RETURN
;
256 namep
->next
= datap
->needed_groups
;
257 datap
->needed_groups
= namep
;
258 /* And get the next entry. */
263 no_more
= __nss_next (&nip
, "getnetgrent_r", (void **) &fct
, status
, 0);
266 if (status
== NSS_STATUS_SUCCESS
)
268 *hostp
= (char *) datap
->val
.triple
.host
;
269 *userp
= (char *) datap
->val
.triple
.user
;
270 *domainp
= (char *) datap
->val
.triple
.domain
;
273 return status
== NSS_STATUS_SUCCESS
? 1 : 0;
276 /* The real entry point. */
278 __getnetgrent_r (char **hostp
, char **userp
, char **domainp
,
279 char *buffer
, size_t buflen
)
281 enum nss_status status
;
283 __libc_lock_lock (lock
);
285 status
= __internal_getnetgrent_r (hostp
, userp
, domainp
, &dataset
,
286 buffer
, buflen
, &errno
);
288 __libc_lock_unlock (lock
);
292 weak_alias (__getnetgrent_r
, getnetgrent_r
)
294 /* Test whether given (host,user,domain) triple is in NETGROUP. */
296 innetgr (const char *netgroup
, const char *host
, const char *user
,
299 int (*setfct
) (const char *, struct __netgrent
*);
300 void (*endfct
) (struct __netgrent
*);
301 int (*getfct
) (struct __netgrent
*, char *, size_t);
302 struct name_list
*known
= NULL
;
303 struct name_list
*needed
= NULL
;
306 const char *current_group
= netgroup
;
309 /* Walk through the services until we found an answer or we shall
310 not work further. We can do some optimization here. Since all
311 services must provide the `setnetgrent' function we can do all
312 the work during one walk through the service list. */
315 no_more
= setup ((void **) &setfct
, "setnetgrent", 1);
318 enum nss_status status
;
319 struct __netgrent entry
;
321 /* Clear the space for the netgroup data. */
322 __bzero (&entry
, sizeof (entry
));
325 status
= (*setfct
) (current_group
, &entry
);
326 if (status
== NSS_STATUS_SUCCESS
327 && __nss_lookup (&nip
, "getnetgrent_r", (void **) &getfct
) == 0)
331 while ((*getfct
) (&entry
, buffer
, sizeof buffer
)
332 == NSS_STATUS_SUCCESS
)
334 if (entry
.type
== group_val
)
336 /* Make sure we haven't seen the name before. */
337 struct name_list
*namep
;
339 for (namep
= known
; namep
!= NULL
; namep
= namep
->next
)
340 if (strcmp (entry
.val
.group
, namep
->name
) == 0)
343 && strcmp (netgroup
, entry
.val
.group
) != 0)
346 (struct name_list
*) malloc (sizeof (*namep
));
348 || ((namep
->name
= __strdup (entry
.val
.group
))
351 /* Out of memory, simply return. */
358 namep
->next
= needed
;
366 if ((entry
.val
.triple
.host
== NULL
|| host
== NULL
367 || __strcasecmp (entry
.val
.triple
.host
, host
) == 0)
368 && (entry
.val
.triple
.user
== NULL
|| user
== NULL
369 || strcmp (entry
.val
.triple
.user
, user
) == 0)
370 && (entry
.val
.triple
.domain
== NULL
|| domain
== NULL
371 || __strcasecmp (entry
.val
.triple
.domain
,
383 /* If we found one service which does know the given
384 netgroup we don't try further. */
385 status
= NSS_STATUS_RETURN
;
388 /* Free all resources of the service. */
389 if (__nss_lookup (&nip
, "endnetgrent", (void **) &endfct
) == 0)
392 /* Look for the next service. */
393 no_more
= __nss_next (&nip
, "setnetgrent",
394 (void **) &setfct
, status
, 0);
397 if (result
== 0 && needed
!= NULL
)
399 struct name_list
*tmp
= needed
;
403 current_group
= known
->name
;
411 /* Free the memory. */
412 while (known
!= NULL
)
414 struct name_list
*tmp
= known
;
416 free ((void *) tmp
->name
);
419 while (needed
!= NULL
)
421 struct name_list
*tmp
= needed
;
422 needed
= needed
->next
;
423 free ((void *) tmp
->name
);