Update.
[glibc.git] / nis / nss_nis / nis-rpc.c
bloba56ad037fe1afd494e292ea8ee9547af1af25786
1 /* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 intern->next = intern->start;
69 else
71 intern->next->next = malloc (sizeof (struct response_t));
72 intern->next = intern->next->next;
74 intern->next->next = NULL;
75 intern->next->val = malloc (invallen + 1);
76 strncpy (intern->next->val, inval, invallen);
77 intern->next->val[invallen] = '\0';
80 return 0;
83 static enum nss_status
84 internal_nis_setrpcent (intern_t *intern)
86 char *domainname;
87 struct ypall_callback ypcb;
88 enum nss_status status;
90 if (yp_get_default_domain (&domainname))
91 return NSS_STATUS_UNAVAIL;
93 while (intern->start != NULL)
95 if (intern->start->val != NULL)
96 free (intern->start->val);
97 intern->next = intern->start;
98 intern->start = intern->start->next;
99 free (intern->next);
101 intern->start = NULL;
103 ypcb.foreach = saveit;
104 ypcb.data = (char *)intern;
105 status = yperr2nss (yp_all(domainname, "rpc.bynumber", &ypcb));
106 intern->next = intern->start;
108 return status;
111 enum nss_status
112 _nss_nis_setrpcent (void)
114 enum nss_status status;
116 __libc_lock_lock (lock);
118 status = internal_nis_setrpcent (&intern);
120 __libc_lock_unlock (lock);
122 return status;
125 static enum nss_status
126 internal_nis_endrpcent (intern_t *intern)
128 while (intern->start != NULL)
130 if (intern->start->val != NULL)
131 free (intern->start->val);
132 intern->next = intern->start;
133 intern->start = intern->start->next;
134 free (intern->next);
136 intern->start = NULL;
138 return NSS_STATUS_SUCCESS;
141 enum nss_status
142 _nss_nis_endrpcent (void)
144 enum nss_status status;
146 __libc_lock_lock (lock);
148 status = internal_nis_endrpcent (&intern);
150 __libc_lock_unlock (lock);
152 return status;
155 static enum nss_status
156 internal_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
157 int *errnop, intern_t *data)
159 struct parser_data *pdata = (void *) buffer;
160 int parse_res;
161 char *p;
163 if (data->start == NULL)
164 internal_nis_setrpcent (data);
166 /* Get the next entry until we found a correct one. */
169 if (data->next == NULL)
171 *errnop = ENOENT;
172 return NSS_STATUS_NOTFOUND;
174 p = strncpy (buffer, data->next->val, buflen);
175 while (isspace (*p))
176 ++p;
178 parse_res = _nss_files_parse_rpcent (p, rpc, pdata, buflen, errnop);
179 if (parse_res == -1)
180 return NSS_STATUS_TRYAGAIN;
181 data->next = data->next->next;
183 while (!parse_res);
185 return NSS_STATUS_SUCCESS;
188 enum nss_status
189 _nss_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
190 int *errnop)
192 enum nss_status status;
194 __libc_lock_lock (lock);
196 status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop, &intern);
198 __libc_lock_unlock (lock);
200 return status;
203 enum nss_status
204 _nss_nis_getrpcbyname_r (const char *name, struct rpcent *rpc,
205 char *buffer, size_t buflen, int *errnop)
207 intern_t data = {NULL, NULL};
208 enum nss_status status;
209 int found;
211 if (name == NULL)
213 *errnop = EINVAL;
214 return NSS_STATUS_UNAVAIL;
217 status = internal_nis_setrpcent (&data);
218 if (status != NSS_STATUS_SUCCESS)
219 return status;
221 found = 0;
222 while (!found &&
223 ((status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop,
224 &data)) == NSS_STATUS_SUCCESS))
226 if (strcmp (rpc->r_name, name) == 0)
227 found = 1;
228 else
230 int i = 0;
232 while (rpc->r_aliases[i] != NULL)
234 if (strcmp (rpc->r_aliases[i], name) == 0)
236 found = 1;
237 break;
239 else
240 ++i;
245 internal_nis_endrpcent (&data);
247 if (!found && status == NSS_STATUS_SUCCESS)
249 *errnop = ENOENT;
250 return NSS_STATUS_NOTFOUND;
252 else
253 return status;
256 enum nss_status
257 _nss_nis_getrpcbynumber_r (int number, struct rpcent *rpc,
258 char *buffer, size_t buflen, int *errnop)
260 struct parser_data *data = (void *) buffer;
261 enum nss_status retval;
262 char *domain, *result, *p;
263 int len, nlen, parse_res;
264 char buf[32];
266 if (yp_get_default_domain (&domain))
267 return NSS_STATUS_UNAVAIL;
269 nlen = sprintf (buf, "%d", number);
271 retval = yperr2nss (yp_match (domain, "rpc.bynumber", buf,
272 nlen, &result, &len));
274 if (retval != NSS_STATUS_SUCCESS)
276 if (retval == NSS_STATUS_NOTFOUND)
277 *errnop = ENOENT;
278 else if (retval == NSS_STATUS_TRYAGAIN)
279 *errnop = errno;
280 return retval;
283 if ((size_t) (len + 1) > buflen)
285 free (result);
286 *errnop = ERANGE;
287 return NSS_STATUS_TRYAGAIN;
290 p = strncpy (buffer, result, len);
291 buffer[len] = '\0';
292 while (isspace (*p))
293 ++p;
294 free (result);
296 parse_res = _nss_files_parse_rpcent (p, rpc, data, buflen, errnop);
298 if (parse_res < 1)
300 if (parse_res == -1)
301 return NSS_STATUS_TRYAGAIN;
302 else
304 *errnop = ENOENT;
305 return NSS_STATUS_NOTFOUND;
308 else
309 return NSS_STATUS_SUCCESS;