1 /* Copyright (C) 1996-2020 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 <https://www.gnu.org/licenses/>. */
24 #include <libc-lock.h>
25 #include <rpcsvc/yp.h>
26 #include <rpcsvc/ypclnt.h>
32 /* Get the declaration of the parser function. */
33 #define ENTNAME servent
35 #include <nss/nss_files/files-parse.c>
37 __libc_lock_define_initialized (static, lock)
39 static intern_t intern;
46 enum nss_status status;
54 dosearch (int instatus, char *inkey, int inkeylen, char *inval,
55 int invallen, char *indata)
57 struct search_t *req = (struct search_t *) indata;
59 if (__glibc_unlikely (instatus != YP_TRUE))
62 if (inkey && inkeylen > 0 && inval && invallen > 0)
64 if (__glibc_unlikely ((size_t) (invallen + 1) > req->buflen))
66 *req->errnop = ERANGE;
67 req->status = NSS_STATUS_TRYAGAIN;
71 char *p = strncpy (req->buffer, inval, invallen);
72 req->buffer[invallen] = '\0';
76 int parse_res = _nss_files_parse_servent (p, req->serv,
78 req->buflen, req->errnop);
81 req->status = NSS_STATUS_TRYAGAIN;
88 if (req->proto != NULL && strcmp (req->serv->s_proto, req->proto) != 0)
91 if (req->port != -1 && req->serv->s_port != req->port)
94 if (req->name != NULL && strcmp (req->serv->s_name, req->name) != 0)
97 for (cp = req->serv->s_aliases; *cp; cp++)
98 if (strcmp (req->name, *cp) == 0)
105 req->status = NSS_STATUS_SUCCESS;
113 internal_nis_endservent (void)
115 struct response_t *curr = intern.next;
119 struct response_t *last = curr;
124 intern.next = intern.start = NULL;
128 _nss_nis_endservent (void)
130 __libc_lock_lock (lock);
132 internal_nis_endservent ();
134 __libc_lock_unlock (lock);
136 return NSS_STATUS_SUCCESS;
139 static enum nss_status
140 internal_nis_setservent (void)
143 struct ypall_callback ypcb;
144 enum nss_status status;
146 if (yp_get_default_domain (&domainname))
147 return NSS_STATUS_UNAVAIL;
149 internal_nis_endservent ();
151 ypcb.foreach = _nis_saveit;
152 ypcb.data = (char *) &intern;
153 status = yperr2nss (yp_all (domainname, "services.byname", &ypcb));
155 /* Mark the last buffer as full. */
156 if (intern.next != NULL)
157 intern.next->size = intern.offset;
159 intern.next = intern.start;
166 _nss_nis_setservent (int stayopen)
168 enum nss_status status;
170 __libc_lock_lock (lock);
172 status = internal_nis_setservent ();
174 __libc_lock_unlock (lock);
179 static enum nss_status
180 internal_nis_getservent_r (struct servent *serv, char *buffer,
181 size_t buflen, int *errnop)
183 struct parser_data *pdata = (void *) buffer;
187 if (intern.start == NULL)
188 internal_nis_setservent ();
190 if (intern.next == NULL)
191 /* Not one entry in the map. */
192 return NSS_STATUS_NOTFOUND;
194 /* Get the next entry until we found a correct one. */
197 struct response_t *bucket = intern.next;
199 if (__glibc_unlikely (intern.offset >= bucket->size))
201 if (bucket->next == NULL)
202 return NSS_STATUS_NOTFOUND;
204 /* We look at all the content in the current bucket. Go on
206 bucket = intern.next = bucket->next;
210 for (p = &bucket->mem[intern.offset]; isspace (*p); ++p)
213 size_t len = strlen (p) + 1;
214 if (__glibc_unlikely (len > buflen))
217 return NSS_STATUS_TRYAGAIN;
220 /* We unfortunately have to copy the data in the user-provided
221 buffer because that buffer might be around for a very long
222 time and the servent structure must remain valid. If we would
223 rely on the BUCKET memory the next 'setservent' or 'endservent'
224 call would destroy it.
226 The important thing is that it is a single NUL-terminated
227 string. This is what the parsing routine expects. */
228 p = memcpy (buffer, &bucket->mem[intern.offset], len);
230 parse_res = _nss_files_parse_servent (p, serv, pdata, buflen, errnop);
231 if (__glibc_unlikely (parse_res == -1))
232 return NSS_STATUS_TRYAGAIN;
234 intern.offset += len;
238 return NSS_STATUS_SUCCESS;
242 _nss_nis_getservent_r (struct servent *serv, char *buffer, size_t buflen,
245 enum nss_status status;
247 __libc_lock_lock (lock);
249 status = internal_nis_getservent_r (serv, buffer, buflen, errnop);
251 __libc_lock_unlock (lock);
257 _nss_nis_getservbyname_r (const char *name, const char *protocol,
258 struct servent *serv, char *buffer, size_t buflen,
264 return NSS_STATUS_UNAVAIL;
268 if (__glibc_unlikely (yp_get_default_domain (&domain)))
269 return NSS_STATUS_UNAVAIL;
271 /* If the protocol is given, we could try if our NIS server knows
272 about services.byservicename map. If yes, we only need one query. */
273 size_t keylen = strlen (name) + (protocol ? 1 + strlen (protocol) : 0);
274 /* Limit key length to the maximum size of an RPC packet. */
275 if (keylen > UDPMSGSIZE)
278 return NSS_STATUS_UNAVAIL;
281 char key[keylen + 1];
283 /* key is: "name/proto" */
284 char *cp = stpcpy (key, name);
285 if (protocol != NULL)
288 strcpy (cp, protocol);
293 int status = yp_match (domain, "services.byservicename", key,
294 keylen, &result, &int_len);
295 size_t len = int_len;
297 /* If we found the key, it's ok and parse the result. If not,
298 fall through and parse the complete table. */
299 if (__glibc_likely (status == YPERR_SUCCESS))
301 if (__glibc_unlikely ((size_t) (len + 1) > buflen))
305 return NSS_STATUS_TRYAGAIN;
308 char *p = strncpy (buffer, result, len);
314 int parse_res = _nss_files_parse_servent (p, serv, (void *) buffer,
316 if (__glibc_unlikely (parse_res < 0))
319 return NSS_STATUS_TRYAGAIN;
321 return NSS_STATUS_NOTFOUND;
324 return NSS_STATUS_SUCCESS;
327 /* Check if it is safe to rely on services.byservicename. */
328 if (_nsl_default_nss () & NSS_FLAG_SERVICES_AUTHORITATIVE)
329 return yperr2nss (status);
331 struct ypall_callback ypcb;
334 ypcb.foreach = dosearch;
335 ypcb.data = (char *) &req;
337 req.proto = protocol;
343 req.status = NSS_STATUS_NOTFOUND;
344 status = yp_all (domain, "services.byname", &ypcb);
346 if (__glibc_unlikely (status != YPERR_SUCCESS))
347 return yperr2nss (status);
353 _nss_nis_getservbyport_r (int port, const char *protocol,
354 struct servent *serv, char *buffer,
355 size_t buflen, int *errnop)
358 if (__glibc_unlikely (yp_get_default_domain (&domain)))
359 return NSS_STATUS_UNAVAIL;
361 /* If the protocol is given, we only need one query.
362 Otherwise try first port/tcp, then port/udp and then fallback
363 to sequential scanning of services.byname. */
364 const char *proto = protocol != NULL ? protocol : "tcp";
365 /* Limit protocol name length to the maximum size of an RPC packet. */
366 if (strlen (proto) > UDPMSGSIZE)
369 return NSS_STATUS_UNAVAIL;
374 /* key is: "port/proto" */
375 char key[sizeof (int) * 3 + strlen (proto) + 2];
376 size_t keylen = snprintf (key, sizeof (key), "%d/%s", ntohs (port),
381 int status = yp_match (domain, "services.byname", key, keylen, &result,
383 size_t len = int_len;
385 /* If we found the key, it's ok and parse the result. If not,
386 fall through and parse the complete table. */
387 if (__glibc_likely (status == YPERR_SUCCESS))
389 if (__glibc_unlikely ((size_t) (len + 1) > buflen))
393 return NSS_STATUS_TRYAGAIN;
396 char *p = strncpy (buffer, result, len);
401 int parse_res = _nss_files_parse_servent (p, serv, (void *) buffer,
403 if (__glibc_unlikely (parse_res < 0))
406 return NSS_STATUS_TRYAGAIN;
408 return NSS_STATUS_NOTFOUND;
411 return NSS_STATUS_SUCCESS;
414 while (protocol == NULL && (proto[0] == 't' ? (proto = "udp") : NULL));
417 return NSS_STATUS_NOTFOUND;
419 struct ypall_callback ypcb;
422 ypcb.foreach = dosearch;
423 ypcb.data = (char *) &req;
425 req.proto = protocol;
431 req.status = NSS_STATUS_NOTFOUND;
432 int status = yp_all (domain, "services.byname", &ypcb);
434 if (__glibc_unlikely (status != YPERR_SUCCESS))
435 return yperr2nss (status);