Update copyright notices with scripts/update-copyrights
[glibc.git] / nis / nss_nis / nis-rpc.c
bloba2fff630c42082f370e8187473c623fcdd2af11a
1 /* Copyright (C) 1996-2014 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 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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <nss.h>
20 #include <netdb.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/yp.h>
26 #include <rpcsvc/ypclnt.h>
28 #include "nss-nis.h"
30 /* Get the declaration of the parser function. */
31 #define ENTNAME rpcent
32 #define EXTERN_PARSER
33 #include <nss/nss_files/files-parse.c>
35 __libc_lock_define_initialized (static, lock)
37 static intern_t intern;
40 static void
41 internal_nis_endrpcent (intern_t *intern)
43 struct response_t *curr = intern->next;
45 while (curr != NULL)
47 struct response_t *last = curr;
48 curr = curr->next;
49 free (last);
52 intern->next = intern->start = NULL;
55 static enum nss_status
56 internal_nis_setrpcent (intern_t *intern)
58 char *domainname;
59 struct ypall_callback ypcb;
60 enum nss_status status;
62 if (yp_get_default_domain (&domainname))
63 return NSS_STATUS_UNAVAIL;
65 internal_nis_endrpcent (intern);
67 ypcb.foreach = _nis_saveit;
68 ypcb.data = (char *) intern;
69 status = yperr2nss (yp_all (domainname, "rpc.bynumber", &ypcb));
71 /* Mark the last buffer as full. */
72 if (intern->next != NULL)
73 intern->next->size = intern->offset;
75 intern->next = intern->start;
76 intern->offset = 0;
78 return status;
81 enum nss_status
82 _nss_nis_setrpcent (int stayopen)
84 enum nss_status status;
86 __libc_lock_lock (lock);
88 status = internal_nis_setrpcent (&intern);
90 __libc_lock_unlock (lock);
92 return status;
95 enum nss_status
96 _nss_nis_endrpcent (void)
98 __libc_lock_lock (lock);
100 internal_nis_endrpcent (&intern);
102 __libc_lock_unlock (lock);
104 return NSS_STATUS_SUCCESS;
107 static enum nss_status
108 internal_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
109 int *errnop, intern_t *intern)
111 struct parser_data *pdata = (void *) buffer;
112 int parse_res;
113 char *p;
115 if (intern->start == NULL)
116 internal_nis_setrpcent (intern);
118 if (intern->next == NULL)
119 /* Not one entry in the map. */
120 return NSS_STATUS_NOTFOUND;
122 /* Get the next entry until we found a correct one. */
125 struct response_t *bucket = intern->next;
127 if (__builtin_expect (intern->offset >= bucket->size, 0))
129 if (bucket->next == NULL)
130 return NSS_STATUS_NOTFOUND;
132 /* We look at all the content in the current bucket. Go on
133 to the next. */
134 bucket = intern->next = bucket->next;
135 intern->offset = 0;
138 for (p = &bucket->mem[intern->offset]; isspace (*p); ++p)
139 ++intern->offset;
141 size_t len = strlen (p) + 1;
142 if (__builtin_expect (len > buflen, 0))
144 *errnop = ERANGE;
145 return NSS_STATUS_TRYAGAIN;
148 /* We unfortunately have to copy the data in the user-provided
149 buffer because that buffer might be around for a very long
150 time and the servent structure must remain valid. If we would
151 rely on the BUCKET memory the next 'setservent' or 'endservent'
152 call would destroy it.
154 The important thing is that it is a single NUL-terminated
155 string. This is what the parsing routine expects. */
156 p = memcpy (buffer, &bucket->mem[intern->offset], len);
158 parse_res = _nss_files_parse_rpcent (p, rpc, pdata, buflen, errnop);
159 if (__builtin_expect (parse_res == -1, 0))
160 return NSS_STATUS_TRYAGAIN;
162 intern->offset += len;
164 while (!parse_res);
166 return NSS_STATUS_SUCCESS;
169 enum nss_status
170 _nss_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
171 int *errnop)
173 enum nss_status status;
175 __libc_lock_lock (lock);
177 status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop, &intern);
179 __libc_lock_unlock (lock);
181 return status;
184 enum nss_status
185 _nss_nis_getrpcbyname_r (const char *name, struct rpcent *rpc,
186 char *buffer, size_t buflen, int *errnop)
188 if (name == NULL)
190 *errnop = EINVAL;
191 return NSS_STATUS_UNAVAIL;
194 intern_t data = { NULL, NULL, 0 };
195 enum nss_status status = internal_nis_setrpcent (&data);
196 if (__builtin_expect (status != NSS_STATUS_SUCCESS, 0))
197 return status;
199 int found = 0;
200 while (!found &&
201 ((status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop,
202 &data)) == NSS_STATUS_SUCCESS))
204 if (strcmp (rpc->r_name, name) == 0)
205 found = 1;
206 else
208 int i = 0;
210 while (rpc->r_aliases[i] != NULL)
212 if (strcmp (rpc->r_aliases[i], name) == 0)
214 found = 1;
215 break;
217 else
218 ++i;
223 internal_nis_endrpcent (&data);
225 if (__builtin_expect (!found && status == NSS_STATUS_SUCCESS, 0))
226 return NSS_STATUS_NOTFOUND;
228 return status;
231 enum nss_status
232 _nss_nis_getrpcbynumber_r (int number, struct rpcent *rpc,
233 char *buffer, size_t buflen, int *errnop)
235 char *domain;
236 if (__builtin_expect (yp_get_default_domain (&domain), 0))
237 return NSS_STATUS_UNAVAIL;
239 char buf[32];
240 int nlen = snprintf (buf, sizeof (buf), "%d", number);
242 char *result;
243 int len;
244 int yperr = yp_match (domain, "rpc.bynumber", buf, nlen, &result, &len);
246 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
248 enum nss_status retval = yperr2nss (yperr);
250 if (retval == NSS_STATUS_TRYAGAIN)
251 *errnop = errno;
252 return retval;
255 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
257 free (result);
258 *errnop = ERANGE;
259 return NSS_STATUS_TRYAGAIN;
262 char *p = strncpy (buffer, result, len);
263 buffer[len] = '\0';
264 while (isspace (*p))
265 ++p;
266 free (result);
268 int parse_res = _nss_files_parse_rpcent (p, rpc, (void *) buffer, buflen,
269 errnop);
270 if (__builtin_expect (parse_res < 1, 0))
272 if (parse_res == -1)
273 return NSS_STATUS_TRYAGAIN;
274 else
275 return NSS_STATUS_NOTFOUND;
277 else
278 return NSS_STATUS_SUCCESS;