7bd6527f2d600619e2e71457e8a275d7b0e9fe4e
[platform/upstream/glibc.git] / nis / nss_nis / nis-netgrp.c
1 /* Copyright (C) 1996,1997,1999,2000,2002,2003,2004,2005
2    Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
5
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.
10
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.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <assert.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <malloc.h>
25 #include <netdb.h>
26 #include <nss.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <netgroup.h>
31 #include <rpcsvc/yp.h>
32 #include <rpcsvc/ypclnt.h>
33
34 #include "nss-nis.h"
35
36 extern enum nss_status
37 _nss_netgroup_parseline (char **cursor, struct __netgrent *netgrp,
38                          char *buffer, size_t buflen, int *errnop);
39
40
41 static void
42 internal_nis_endnetgrent (struct __netgrent *netgrp)
43 {
44   if (netgrp->data != NULL)
45     {
46       free (netgrp->data);
47       netgrp->data = NULL;
48       netgrp->data_size = 0;
49       netgrp->cursor = NULL;
50     }
51 }
52
53 enum nss_status
54 _nss_nis_setnetgrent (const char *group, struct __netgrent *netgrp)
55 {
56   char *domain;
57   int len;
58   enum nss_status status;
59
60   status = NSS_STATUS_SUCCESS;
61
62   if (group == NULL || group[0] == '\0')
63     return NSS_STATUS_UNAVAIL;
64
65   if (yp_get_default_domain (&domain))
66     return NSS_STATUS_UNAVAIL;
67
68   internal_nis_endnetgrent (netgrp);
69
70   status = yperr2nss (yp_match (domain, "netgroup", group, strlen (group),
71                                 &netgrp->data, &len));
72   if (status == NSS_STATUS_SUCCESS)
73     {
74       /* Our implementation of yp_match already allocates a buffer
75          which is one byte larger than the value in LEN specifies
76          and the last byte is filled with NUL.  So we can simply
77          use that buffer.  */
78       assert (len >= 0);
79       assert (malloc_usable_size (netgrp->data) >= len + 1);
80       assert (netgrp->data[len] == '\0');
81
82       netgrp->data_size = len;
83       netgrp->cursor = netgrp->data;
84     }
85
86   return status;
87 }
88
89
90 enum nss_status
91 _nss_nis_endnetgrent (struct __netgrent *netgrp)
92 {
93   internal_nis_endnetgrent (netgrp);
94
95   return NSS_STATUS_SUCCESS;
96 }
97
98 enum nss_status
99 _nss_nis_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
100                         int *errnop)
101 {
102   if (result->cursor == NULL)
103     return NSS_STATUS_NOTFOUND;
104
105   return _nss_netgroup_parseline (&result->cursor, result, buffer, buflen,
106                                   errnop);
107 }