607bc2c1ca10dd1e32e6df7ae77b40b1da38df22
[platform/upstream/glibc.git] / nis / nss_nisplus / nisplus-netgrp.c
1 /* Copyright (C) 1997, 2003, 2004 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
4
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.
9
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.
14
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
18    02111-1307 USA.  */
19
20 #include <nss.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <netdb.h>
24 #include <string.h>
25 #include <netgroup.h>
26 #include <rpcsvc/nis.h>
27
28 #include "nss-nisplus.h"
29
30 #define NISENTRYVAL(idx,col,res) \
31         ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
32
33 #define NISENTRYLEN(idx,col,res) \
34         ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
35
36 enum nss_status
37 _nss_nisplus_getnetgrent_r (struct __netgrent *result, char *buffer,
38                             size_t buflen, int *errnop)
39 {
40   enum nss_status status;
41
42   /* Some sanity checks.  */
43   if (result->data == NULL || result->data_size == 0)
44     return NSS_STATUS_NOTFOUND;
45
46   if (result->position == result->data_size)
47     return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
48
49   unsigned int entrylen
50     = NISENTRYLEN (result->position, 1, (nis_result *) result->data);
51   if (entrylen > 0)
52     {
53       /* We have a list of other netgroups.  */
54
55       result->type = group_val;
56       if (entrylen >= buflen)
57         {
58           *errnop = ERANGE;
59           return NSS_STATUS_TRYAGAIN;
60         }
61       strncpy (buffer, NISENTRYVAL (result->position, 1,
62                                     (nis_result *) result->data),
63                entrylen);
64       buffer[entrylen] = '\0';
65       result->val.group = buffer;
66       ++result->position;
67       result->first = 0;
68
69       return NSS_STATUS_SUCCESS;
70     }
71
72   /* Before we can copy the entry to the private buffer we have to make
73      sure it is big enough.  */
74   unsigned int hostlen
75     = NISENTRYLEN (result->position, 2, (nis_result *) result->data);
76   unsigned int userlen
77     = NISENTRYLEN (result->position, 3, (nis_result *) result->data);
78   unsigned int domainlen
79     = NISENTRYLEN (result->position, 4, (nis_result *) result->data);
80   if (hostlen + userlen + domainlen + 6 > buflen)
81     {
82       *errnop = ERANGE;
83       status = NSS_STATUS_TRYAGAIN;
84     }
85   else
86     {
87       char *cp = buffer;
88
89       result->type = triple_val;
90
91       if (hostlen == 0 ||
92           NISENTRYVAL (result->position, 2,
93                        (nis_result *) result->data)[0] == '\0')
94         result->val.triple.host = NULL;
95       else
96         {
97           result->val.triple.host = cp;
98           cp = __stpncpy (cp, NISENTRYVAL (result->position, 2,
99                                            (nis_result *) result->data),
100                           hostlen);
101           *cp++ = '\0';
102         }
103
104       if (userlen == 0 ||
105           NISENTRYVAL (result->position, 3,
106                        (nis_result *) result->data)[0] == '\0')
107         result->val.triple.user = NULL;
108       else
109         {
110           result->val.triple.user = cp;
111           cp = __stpncpy (cp, NISENTRYVAL (result->position, 3,
112                                            (nis_result *) result->data),
113                           userlen);
114           *cp++ = '\0';
115         }
116
117       if (domainlen == 0 ||
118           NISENTRYVAL (result->position, 4,
119                        (nis_result *) result->data)[0] == '\0')
120         result->val.triple.domain = NULL;
121       else
122         {
123           result->val.triple.domain = cp;
124           cp = __stpncpy (cp, NISENTRYVAL (result->position, 4,
125                                            (nis_result *) result->data),
126                           domainlen);
127           *cp = '\0';
128         }
129
130       status = NSS_STATUS_SUCCESS;
131
132       /* Remember where we stopped reading.  */
133       ++result->position;
134
135       result->first = 0;
136     }
137
138   return status;
139 }
140
141 static void
142 internal_endnetgrent (struct __netgrent *netgrp)
143 {
144   if (netgrp->data != NULL)
145     {
146       nis_freeresult ((nis_result *) netgrp->data);
147       netgrp->data = NULL;
148       netgrp->data_size = 0;
149       netgrp->position = 0;
150     }
151 }
152
153 enum nss_status
154 _nss_nisplus_setnetgrent (const char *group, struct __netgrent *netgrp)
155 {
156   enum nss_status status;
157   char buf[strlen (group) + 30];
158
159   if (group == NULL || group[0] == '\0')
160     return NSS_STATUS_UNAVAIL;
161
162   status = NSS_STATUS_SUCCESS;
163
164   internal_endnetgrent (netgrp);
165
166   sprintf (buf, "[name=%s],netgroup.org_dir", group);
167
168   netgrp->data = (char *) nis_list (buf, EXPAND_NAME, NULL, NULL);
169
170   if (netgrp->data == NULL)
171     {
172       __set_errno (ENOMEM);
173       status = NSS_STATUS_TRYAGAIN;
174     }
175   else if (niserr2nss (((nis_result *) netgrp->data)->status)
176            != NSS_STATUS_SUCCESS)
177     {
178       status = niserr2nss (((nis_result *) netgrp->data)->status);
179
180       internal_endnetgrent (netgrp);
181     }
182   else
183     {
184       netgrp->data_size = ((nis_result *) netgrp->data)->objects.objects_len;
185       netgrp->position = 0;
186       netgrp->first = 1;
187     }
188
189   return status;
190 }
191
192 enum nss_status
193 _nss_nisplus_endnetgrent (struct __netgrent *netgrp)
194 {
195   internal_endnetgrent (netgrp);
196
197   return NSS_STATUS_SUCCESS;
198 }