]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nss_nis/nis-rpc.c
* nis/nss-default.c (vars): Add SETENT_BATCH_READ.
[thirdparty/glibc.git] / nis / nss_nis / nis-rpc.c
CommitLineData
ab9a9ff8
UD
1/* Copyright (C) 1996-1998,2000,2002,2003,2004,2006
2 Free Software Foundation, Inc.
6259ec0d 3 This file is part of the GNU C Library.
3b965a7d 4 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
6259ec0d
UD
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
6259ec0d
UD
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
41bdb6e2 14 Lesser General Public License for more details.
6259ec0d 15
41bdb6e2
AJ
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. */
6259ec0d
UD
20
21#include <nss.h>
22#include <netdb.h>
23#include <ctype.h>
24#include <errno.h>
25#include <string.h>
5107cf1d 26#include <bits/libc-lock.h>
6259ec0d
UD
27#include <rpcsvc/yp.h>
28#include <rpcsvc/ypclnt.h>
29
30#include "nss-nis.h"
31
7e3be507
UD
32/* Get the declaration of the parser function. */
33#define ENTNAME rpcent
34#define EXTERN_PARSER
cf29ffbe 35#include <nss/nss_files/files-parse.c>
7e3be507 36
6259ec0d
UD
37__libc_lock_define_initialized (static, lock)
38
a334319f
UD
39struct response_t
40{
41 struct response_t *next;
42 char val[0];
43};
b569ae38 44
a334319f
UD
45struct intern_t
46{
47 struct response_t *start;
48 struct response_t *next;
49};
50typedef struct intern_t intern_t;
6259ec0d 51
a334319f
UD
52static intern_t intern = {NULL, NULL};
53
54static int
55saveit (int instatus, char *inkey, int inkeylen, char *inval,
56 int invallen, char *indata)
b677d674 57{
ab9a9ff8 58 intern_t *intern = (intern_t *) indata;
a334319f
UD
59
60 if (instatus != YP_TRUE)
61 return 1;
0ecb606c 62
a334319f 63 if (inkey && inkeylen > 0 && inval && invallen > 0)
b677d674 64 {
a334319f
UD
65 struct response_t *newp = malloc (sizeof (struct response_t)
66 + invallen + 1);
67 if (newp == NULL)
68 return 1; /* We have no error code for out of memory */
69
70 if (intern->start == NULL)
71 intern->start = newp;
72 else
73 intern->next->next = newp;
74 intern->next = newp;
75
76 newp->next = NULL;
77 *((char *) mempcpy (newp->val, inval, invallen)) = '\0';
b677d674 78 }
0ecb606c 79
a334319f
UD
80 return 0;
81}
82
83static void
84internal_nis_endrpcent (intern_t *intern)
85{
86 while (intern->start != NULL)
87 {
88 intern->next = intern->start;
89 intern->start = intern->start->next;
90 free (intern->next);
91 }
b677d674
UD
92}
93
6259ec0d 94static enum nss_status
f166d865 95internal_nis_setrpcent (intern_t *intern)
6259ec0d 96{
f166d865
UD
97 char *domainname;
98 struct ypall_callback ypcb;
0d8733c4 99 enum nss_status status;
cf29ffbe 100
f166d865
UD
101 if (yp_get_default_domain (&domainname))
102 return NSS_STATUS_UNAVAIL;
cf29ffbe 103
b677d674 104 internal_nis_endrpcent (intern);
f166d865 105
a334319f
UD
106 ypcb.foreach = saveit;
107 ypcb.data = (char *)intern;
108 status = yperr2nss (yp_all(domainname, "rpc.bynumber", &ypcb));
f166d865
UD
109 intern->next = intern->start;
110
0d8733c4 111 return status;
6259ec0d
UD
112}
113
114enum nss_status
23bab906 115_nss_nis_setrpcent (int stayopen)
6259ec0d
UD
116{
117 enum nss_status status;
118
119 __libc_lock_lock (lock);
120
121 status = internal_nis_setrpcent (&intern);
122
123 __libc_lock_unlock (lock);
124
125 return status;
126}
127
6259ec0d
UD
128enum nss_status
129_nss_nis_endrpcent (void)
130{
6259ec0d
UD
131 __libc_lock_lock (lock);
132
b677d674 133 internal_nis_endrpcent (&intern);
6259ec0d
UD
134
135 __libc_lock_unlock (lock);
136
b677d674 137 return NSS_STATUS_SUCCESS;
6259ec0d
UD
138}
139
140static enum nss_status
141internal_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
a334319f 142 int *errnop, intern_t *data)
6259ec0d 143{
7e3be507 144 struct parser_data *pdata = (void *) buffer;
f166d865 145 int parse_res;
6259ec0d 146 char *p;
cf29ffbe 147
a334319f
UD
148 if (data->start == NULL)
149 internal_nis_setrpcent (data);
cf29ffbe 150
6259ec0d
UD
151 /* Get the next entry until we found a correct one. */
152 do
153 {
a334319f
UD
154 if (data->next == NULL)
155 return NSS_STATUS_NOTFOUND;
0ecb606c 156
a334319f
UD
157 p = strncpy (buffer, data->next->val, buflen);
158 while (isspace (*p))
159 ++p;
cf29ffbe 160
d71b808a 161 parse_res = _nss_files_parse_rpcent (p, rpc, pdata, buflen, errnop);
ab9a9ff8 162 if (__builtin_expect (parse_res == -1, 0))
6259ec0d 163 return NSS_STATUS_TRYAGAIN;
a334319f 164 data->next = data->next->next;
6259ec0d
UD
165 }
166 while (!parse_res);
cf29ffbe 167
6259ec0d
UD
168 return NSS_STATUS_SUCCESS;
169}
170
171enum nss_status
d71b808a
UD
172_nss_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
173 int *errnop)
6259ec0d
UD
174{
175 enum nss_status status;
176
177 __libc_lock_lock (lock);
178
d71b808a 179 status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop, &intern);
6259ec0d
UD
180
181 __libc_lock_unlock (lock);
182
183 return status;
184}
185
186enum nss_status
187_nss_nis_getrpcbyname_r (const char *name, struct rpcent *rpc,
d71b808a 188 char *buffer, size_t buflen, int *errnop)
6259ec0d 189{
6259ec0d
UD
190 if (name == NULL)
191 {
ac9f45cf 192 *errnop = EINVAL;
6259ec0d
UD
193 return NSS_STATUS_UNAVAIL;
194 }
195
ab9a9ff8
UD
196 intern_t data = { NULL, NULL };
197 enum nss_status status = internal_nis_setrpcent (&data);
198 if (__builtin_expect (status != NSS_STATUS_SUCCESS, 0))
6259ec0d
UD
199 return status;
200
ab9a9ff8 201 int found = 0;
6259ec0d 202 while (!found &&
d71b808a
UD
203 ((status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop,
204 &data)) == NSS_STATUS_SUCCESS))
6259ec0d
UD
205 {
206 if (strcmp (rpc->r_name, name) == 0)
207 found = 1;
208 else
209 {
210 int i = 0;
211
212 while (rpc->r_aliases[i] != NULL)
213 {
214 if (strcmp (rpc->r_aliases[i], name) == 0)
215 {
216 found = 1;
217 break;
218 }
219 else
220 ++i;
221 }
222 }
223 }
224
225 internal_nis_endrpcent (&data);
226
ab9a9ff8 227 if (__builtin_expect (!found && status == NSS_STATUS_SUCCESS, 0))
34816665 228 return NSS_STATUS_NOTFOUND;
ab9a9ff8
UD
229
230 return status;
6259ec0d
UD
231}
232
233enum nss_status
234_nss_nis_getrpcbynumber_r (int number, struct rpcent *rpc,
d71b808a 235 char *buffer, size_t buflen, int *errnop)
6259ec0d 236{
ab9a9ff8
UD
237 char *domain;
238 if (__builtin_expect (yp_get_default_domain (&domain), 0))
6259ec0d
UD
239 return NSS_STATUS_UNAVAIL;
240
ab9a9ff8
UD
241 char buf[32];
242 int nlen = snprintf (buf, sizeof (buf), "%d", number);
6259ec0d 243
ab9a9ff8
UD
244 char *result;
245 int len;
246 int yperr = yp_match (domain, "rpc.bynumber", buf, nlen, &result, &len);
6259ec0d 247
ab9a9ff8 248 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
6259ec0d 249 {
ab9a9ff8
UD
250 enum nss_status retval = yperr2nss (yperr);
251
34816665 252 if (retval == NSS_STATUS_TRYAGAIN)
d71b808a 253 *errnop = errno;
6259ec0d
UD
254 return retval;
255 }
256
ab9a9ff8 257 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
6259ec0d
UD
258 {
259 free (result);
d71b808a 260 *errnop = ERANGE;
6259ec0d
UD
261 return NSS_STATUS_TRYAGAIN;
262 }
263
ab9a9ff8 264 char *p = strncpy (buffer, result, len);
6259ec0d
UD
265 buffer[len] = '\0';
266 while (isspace (*p))
267 ++p;
268 free (result);
269
ab9a9ff8
UD
270 int parse_res = _nss_files_parse_rpcent (p, rpc, (void *) buffer, buflen,
271 errnop);
272 if (__builtin_expect (parse_res < 1, 0))
6259ec0d 273 {
60c96635 274 if (parse_res == -1)
6259ec0d
UD
275 return NSS_STATUS_TRYAGAIN;
276 else
34816665 277 return NSS_STATUS_NOTFOUND;
6259ec0d
UD
278 }
279 else
280 return NSS_STATUS_SUCCESS;
281}