(EM_*): Synch with official list.
[glibc.git] / nis / nss_nis / nis-rpc.c
blobbe7a2d09dfa882d5a46eaaa2bf08b43dc7bfc7f3
1 /* Copyright (C) 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <nss.h>
21 #include <netdb.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
29 #include "nss-nis.h"
31 /* Get the declaration of the parser function. */
32 #define ENTNAME rpcent
33 #define EXTERN_PARSER
34 #include <nss/nss_files/files-parse.c>
36 __libc_lock_define_initialized (static, lock)
38 struct response_t
40 char *val;
41 struct response_t *next;
44 struct intern_t
46 struct response_t *start;
47 struct response_t *next;
49 typedef struct intern_t intern_t;
51 static intern_t intern = {NULL, NULL};
53 static int
54 saveit (int instatus, char *inkey, int inkeylen, char *inval,
55 int invallen, char *indata)
57 intern_t *intern = (intern_t *)indata;
59 if (instatus != YP_TRUE)
60 return instatus;
62 if (inkey && inkeylen > 0 && inval && invallen > 0)
64 if (intern->start == NULL)
66 intern->start = malloc (sizeof (struct response_t));
67 if (intern->start == NULL)
68 return YP_FALSE;
69 intern->next = intern->start;
71 else
73 intern->next->next = malloc (sizeof (struct response_t));
74 if (intern->next->next == NULL)
75 return YP_FALSE;
76 intern->next = intern->next->next;
78 intern->next->next = NULL;
79 intern->next->val = malloc (invallen + 1);
80 if (intern->next->val == NULL)
81 return YP_FALSE;
82 strncpy (intern->next->val, inval, invallen);
83 intern->next->val[invallen] = '\0';
86 return 0;
89 static enum nss_status
90 internal_nis_setrpcent (intern_t *intern)
92 char *domainname;
93 struct ypall_callback ypcb;
94 enum nss_status status;
96 if (yp_get_default_domain (&domainname))
97 return NSS_STATUS_UNAVAIL;
99 while (intern->start != NULL)
101 if (intern->start->val != NULL)
102 free (intern->start->val);
103 intern->next = intern->start;
104 intern->start = intern->start->next;
105 free (intern->next);
107 intern->start = NULL;
109 ypcb.foreach = saveit;
110 ypcb.data = (char *)intern;
111 status = yperr2nss (yp_all(domainname, "rpc.bynumber", &ypcb));
112 intern->next = intern->start;
114 return status;
117 enum nss_status
118 _nss_nis_setrpcent (int stayopen)
120 enum nss_status status;
122 __libc_lock_lock (lock);
124 status = internal_nis_setrpcent (&intern);
126 __libc_lock_unlock (lock);
128 return status;
131 static enum nss_status
132 internal_nis_endrpcent (intern_t *intern)
134 while (intern->start != NULL)
136 if (intern->start->val != NULL)
137 free (intern->start->val);
138 intern->next = intern->start;
139 intern->start = intern->start->next;
140 free (intern->next);
142 intern->start = NULL;
144 return NSS_STATUS_SUCCESS;
147 enum nss_status
148 _nss_nis_endrpcent (void)
150 enum nss_status status;
152 __libc_lock_lock (lock);
154 status = internal_nis_endrpcent (&intern);
156 __libc_lock_unlock (lock);
158 return status;
161 static enum nss_status
162 internal_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
163 int *errnop, intern_t *data)
165 struct parser_data *pdata = (void *) buffer;
166 int parse_res;
167 char *p;
169 if (data->start == NULL)
170 internal_nis_setrpcent (data);
172 /* Get the next entry until we found a correct one. */
175 if (data->next == NULL)
177 *errnop = ENOENT;
178 return NSS_STATUS_NOTFOUND;
180 p = strncpy (buffer, data->next->val, buflen);
181 while (isspace (*p))
182 ++p;
184 parse_res = _nss_files_parse_rpcent (p, rpc, pdata, buflen, errnop);
185 if (parse_res == -1)
186 return NSS_STATUS_TRYAGAIN;
187 data->next = data->next->next;
189 while (!parse_res);
191 return NSS_STATUS_SUCCESS;
194 enum nss_status
195 _nss_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
196 int *errnop)
198 enum nss_status status;
200 __libc_lock_lock (lock);
202 status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop, &intern);
204 __libc_lock_unlock (lock);
206 return status;
209 enum nss_status
210 _nss_nis_getrpcbyname_r (const char *name, struct rpcent *rpc,
211 char *buffer, size_t buflen, int *errnop)
213 intern_t data = {NULL, NULL};
214 enum nss_status status;
215 int found;
217 if (name == NULL)
219 *errnop = EINVAL;
220 return NSS_STATUS_UNAVAIL;
223 status = internal_nis_setrpcent (&data);
224 if (status != NSS_STATUS_SUCCESS)
225 return status;
227 found = 0;
228 while (!found &&
229 ((status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop,
230 &data)) == NSS_STATUS_SUCCESS))
232 if (strcmp (rpc->r_name, name) == 0)
233 found = 1;
234 else
236 int i = 0;
238 while (rpc->r_aliases[i] != NULL)
240 if (strcmp (rpc->r_aliases[i], name) == 0)
242 found = 1;
243 break;
245 else
246 ++i;
251 internal_nis_endrpcent (&data);
253 if (!found && status == NSS_STATUS_SUCCESS)
255 *errnop = ENOENT;
256 return NSS_STATUS_NOTFOUND;
258 else
259 return status;
262 enum nss_status
263 _nss_nis_getrpcbynumber_r (int number, struct rpcent *rpc,
264 char *buffer, size_t buflen, int *errnop)
266 struct parser_data *data = (void *) buffer;
267 enum nss_status retval;
268 char *domain, *result, *p;
269 int len, nlen, parse_res;
270 char buf[32];
272 if (yp_get_default_domain (&domain))
273 return NSS_STATUS_UNAVAIL;
275 nlen = sprintf (buf, "%d", number);
277 retval = yperr2nss (yp_match (domain, "rpc.bynumber", buf,
278 nlen, &result, &len));
280 if (retval != NSS_STATUS_SUCCESS)
282 if (retval == NSS_STATUS_NOTFOUND)
283 *errnop = ENOENT;
284 else if (retval == NSS_STATUS_TRYAGAIN)
285 *errnop = errno;
286 return retval;
289 if ((size_t) (len + 1) > buflen)
291 free (result);
292 *errnop = ERANGE;
293 return NSS_STATUS_TRYAGAIN;
296 p = strncpy (buffer, result, len);
297 buffer[len] = '\0';
298 while (isspace (*p))
299 ++p;
300 free (result);
302 parse_res = _nss_files_parse_rpcent (p, rpc, data, buflen, errnop);
304 if (parse_res < 1)
306 if (parse_res == -1)
307 return NSS_STATUS_TRYAGAIN;
308 else
310 *errnop = ENOENT;
311 return NSS_STATUS_NOTFOUND;
314 else
315 return NSS_STATUS_SUCCESS;