Fix tst-rfc3484* build failures from USE_NSCD move to config.h.
[glibc.git] / nis / nss_nis / nis-netgrp.c
blob3546a0ee6b2fc6893dfca5cf2221dfd22cdb1b03
1 /* Copyright (C) 1996,1997,1999,2000,2002,2003,2004,2005,2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <assert.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <malloc.h>
24 #include <netdb.h>
25 #include <nss.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <netgroup.h>
30 #include <rpcsvc/yp.h>
31 #include <rpcsvc/ypclnt.h>
33 #include "nss-nis.h"
35 extern enum nss_status
36 _nss_netgroup_parseline (char **cursor, struct __netgrent *netgrp,
37 char *buffer, size_t buflen, int *errnop);
40 static void
41 internal_nis_endnetgrent (struct __netgrent *netgrp)
43 free (netgrp->data);
44 netgrp->data = NULL;
45 netgrp->data_size = 0;
46 netgrp->cursor = NULL;
50 enum nss_status
51 _nss_nis_setnetgrent (const char *group, struct __netgrent *netgrp)
53 int len;
54 enum nss_status status;
56 status = NSS_STATUS_SUCCESS;
58 if (__builtin_expect (group == NULL || group[0] == '\0', 0))
59 return NSS_STATUS_UNAVAIL;
61 char *domain;
62 if (__builtin_expect (yp_get_default_domain (&domain), 0))
63 return NSS_STATUS_UNAVAIL;
65 status = yperr2nss (yp_match (domain, "netgroup", group, strlen (group),
66 &netgrp->data, &len));
67 if (__builtin_expect (status == NSS_STATUS_SUCCESS, 1))
69 /* Our implementation of yp_match already allocates a buffer
70 which is one byte larger than the value in LEN specifies
71 and the last byte is filled with NUL. So we can simply
72 use that buffer. */
73 assert (len >= 0);
74 assert (malloc_usable_size (netgrp->data) >= len + 1);
75 assert (netgrp->data[len] == '\0');
77 netgrp->data_size = len;
78 netgrp->cursor = netgrp->data;
81 return status;
85 enum nss_status
86 _nss_nis_endnetgrent (struct __netgrent *netgrp)
88 internal_nis_endnetgrent (netgrp);
90 return NSS_STATUS_SUCCESS;
94 enum nss_status
95 _nss_nis_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
96 int *errnop)
98 return _nss_netgroup_parseline (&result->cursor, result, buffer, buflen,
99 errnop);