Fix Charset comment in fi_FI, fi_FI@euro
[glibc.git] / nis / nss_nis / nis-service.c
bloba63e8e9ca99e7c2379ea15bb07a0a3ef1aa8fe93
1 /* Copyright (C) 1996-2013 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"
29 #include <libnsl.h>
32 /* Get the declaration of the parser function. */
33 #define ENTNAME servent
34 #define EXTERN_PARSER
35 #include <nss/nss_files/files-parse.c>
37 __libc_lock_define_initialized (static, lock)
39 static intern_t intern;
41 struct search_t
43 const char *name;
44 const char *proto;
45 int port;
46 enum nss_status status;
47 struct servent *serv;
48 char *buffer;
49 size_t buflen;
50 int *errnop;
53 static int
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 (__builtin_expect (instatus != YP_TRUE, 0))
60 return 1;
62 if (inkey && inkeylen > 0 && inval && invallen > 0)
64 if (__builtin_expect ((size_t) (invallen + 1) > req->buflen, 0))
66 *req->errnop = ERANGE;
67 req->status = NSS_STATUS_TRYAGAIN;
68 return 1;
71 char *p = strncpy (req->buffer, inval, invallen);
72 req->buffer[invallen] = '\0';
73 while (isspace (*p))
74 ++p;
76 int parse_res = _nss_files_parse_servent (p, req->serv,
77 (void *) req->buffer,
78 req->buflen, req->errnop);
79 if (parse_res == -1)
81 req->status = NSS_STATUS_TRYAGAIN;
82 return 1;
85 if (!parse_res)
86 return 0;
88 if (req->proto != NULL && strcmp (req->serv->s_proto, req->proto) != 0)
89 return 0;
91 if (req->port != -1 && req->serv->s_port != req->port)
92 return 0;
94 if (req->name != NULL && strcmp (req->serv->s_name, req->name) != 0)
96 char **cp;
97 for (cp = req->serv->s_aliases; *cp; cp++)
98 if (strcmp (req->name, *cp) == 0)
99 break;
101 if (*cp == NULL)
102 return 0;
105 req->status = NSS_STATUS_SUCCESS;
106 return 1;
109 return 0;
112 static void
113 internal_nis_endservent (void)
115 struct response_t *curr = intern.next;
117 while (curr != NULL)
119 struct response_t *last = curr;
120 curr = curr->next;
121 free (last);
124 intern.next = intern.start = NULL;
127 enum nss_status
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)
142 char *domainname;
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;
160 intern.offset = 0;
162 return status;
165 enum nss_status
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);
176 return status;
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;
184 int parse_res;
185 char *p;
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 (__builtin_expect (intern.offset >= bucket->size, 0))
201 if (bucket->next == NULL)
202 return NSS_STATUS_NOTFOUND;
204 /* We look at all the content in the current bucket. Go on
205 to the next. */
206 bucket = intern.next = bucket->next;
207 intern.offset = 0;
210 for (p = &bucket->mem[intern.offset]; isspace (*p); ++p)
211 ++intern.offset;
213 size_t len = strlen (p) + 1;
214 if (__builtin_expect (len > buflen, 0))
216 *errnop = ERANGE;
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 (__builtin_expect (parse_res == -1, 0))
232 return NSS_STATUS_TRYAGAIN;
234 intern.offset += len;
236 while (!parse_res);
238 return NSS_STATUS_SUCCESS;
241 enum nss_status
242 _nss_nis_getservent_r (struct servent *serv, char *buffer, size_t buflen,
243 int *errnop)
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);
253 return status;
256 enum nss_status
257 _nss_nis_getservbyname_r (const char *name, const char *protocol,
258 struct servent *serv, char *buffer, size_t buflen,
259 int *errnop)
261 if (name == NULL)
263 *errnop = EINVAL;
264 return NSS_STATUS_UNAVAIL;
267 char *domain;
268 if (__builtin_expect (yp_get_default_domain (&domain), 0))
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 char key[keylen + 1];
276 /* key is: "name/proto" */
277 char *cp = stpcpy (key, name);
278 if (protocol != NULL)
280 *cp++ = '/';
281 strcpy (cp, protocol);
284 char *result;
285 int int_len;
286 int status = yp_match (domain, "services.byservicename", key,
287 keylen, &result, &int_len);
288 size_t len = int_len;
290 /* If we found the key, it's ok and parse the result. If not,
291 fall through and parse the complete table. */
292 if (__builtin_expect (status == YPERR_SUCCESS, 1))
294 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
296 free (result);
297 *errnop = ERANGE;
298 return NSS_STATUS_TRYAGAIN;
301 char *p = strncpy (buffer, result, len);
302 buffer[len] = '\0';
303 while (isspace (*p))
304 ++p;
305 free (result);
307 int parse_res = _nss_files_parse_servent (p, serv, (void *) buffer,
308 buflen, errnop);
309 if (__builtin_expect (parse_res < 0, 0))
311 if (parse_res == -1)
312 return NSS_STATUS_TRYAGAIN;
313 else
314 return NSS_STATUS_NOTFOUND;
316 else
317 return NSS_STATUS_SUCCESS;
320 /* Check if it is safe to rely on services.byservicename. */
321 if (_nsl_default_nss () & NSS_FLAG_SERVICES_AUTHORITATIVE)
322 return yperr2nss (status);
324 struct ypall_callback ypcb;
325 struct search_t req;
327 ypcb.foreach = dosearch;
328 ypcb.data = (char *) &req;
329 req.name = name;
330 req.proto = protocol;
331 req.port = -1;
332 req.serv = serv;
333 req.buffer = buffer;
334 req.buflen = buflen;
335 req.errnop = errnop;
336 req.status = NSS_STATUS_NOTFOUND;
337 status = yp_all (domain, "services.byname", &ypcb);
339 if (__builtin_expect (status != YPERR_SUCCESS, 0))
340 return yperr2nss (status);
342 return req.status;
345 enum nss_status
346 _nss_nis_getservbyport_r (int port, const char *protocol,
347 struct servent *serv, char *buffer,
348 size_t buflen, int *errnop)
350 char *domain;
351 if (__builtin_expect (yp_get_default_domain (&domain), 0))
352 return NSS_STATUS_UNAVAIL;
354 /* If the protocol is given, we only need one query.
355 Otherwise try first port/tcp, then port/udp and then fallback
356 to sequential scanning of services.byname. */
357 const char *proto = protocol != NULL ? protocol : "tcp";
360 /* key is: "port/proto" */
361 char key[sizeof (int) * 3 + strlen (proto) + 2];
362 size_t keylen = snprintf (key, sizeof (key), "%d/%s", ntohs (port),
363 proto);
365 char *result;
366 int int_len;
367 int status = yp_match (domain, "services.byname", key, keylen, &result,
368 &int_len);
369 size_t len = int_len;
371 /* If we found the key, it's ok and parse the result. If not,
372 fall through and parse the complete table. */
373 if (__builtin_expect (status == YPERR_SUCCESS, 1))
375 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
377 free (result);
378 *errnop = ERANGE;
379 return NSS_STATUS_TRYAGAIN;
382 char *p = strncpy (buffer, result, len);
383 buffer[len] = '\0';
384 while (isspace (*p))
385 ++p;
386 free (result);
387 int parse_res = _nss_files_parse_servent (p, serv, (void *) buffer,
388 buflen, errnop);
389 if (__builtin_expect (parse_res < 0, 0))
391 if (parse_res == -1)
392 return NSS_STATUS_TRYAGAIN;
393 else
394 return NSS_STATUS_NOTFOUND;
397 return NSS_STATUS_SUCCESS;
400 while (protocol == NULL && (proto[0] == 't' ? (proto = "udp") : NULL));
402 if (port == -1)
403 return NSS_STATUS_NOTFOUND;
405 struct ypall_callback ypcb;
406 struct search_t req;
408 ypcb.foreach = dosearch;
409 ypcb.data = (char *) &req;
410 req.name = NULL;
411 req.proto = protocol;
412 req.port = port;
413 req.serv = serv;
414 req.buffer = buffer;
415 req.buflen = buflen;
416 req.errnop = errnop;
417 req.status = NSS_STATUS_NOTFOUND;
418 int status = yp_all (domain, "services.byname", &ypcb);
420 if (__builtin_expect (status != YPERR_SUCCESS, 0))
421 return yperr2nss (status);
423 return req.status;