Update copyright notices with scripts/update-copyrights
[glibc.git] / nis / nss_nis / nis-proto.c
blobeff2eddd6cb5e27ffbd739805e819d2dbd34735e
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 protoent
32 #define EXTERN_PARSER
33 #include <nss/nss_files/files-parse.c>
35 __libc_lock_define_initialized (static, lock)
37 struct response
39 struct response *next;
40 char val[0];
43 static struct response *start;
44 static struct response *next;
46 static int
47 saveit (int instatus, char *inkey, int inkeylen, char *inval,
48 int invallen, char *indata)
50 if (instatus != YP_TRUE)
51 return 1;
53 if (inkey && inkeylen > 0 && inval && invallen > 0)
55 struct response *newp = malloc (sizeof (struct response) + invallen + 1);
56 if (newp == NULL)
57 return 1; /* We have no error code for out of memory */
59 if (start == NULL)
60 start = newp;
61 else
62 next->next = newp;
63 next = newp;
65 newp->next = NULL;
66 *((char *) mempcpy (newp->val, inval, invallen)) = '\0';
69 return 0;
72 static void
73 internal_nis_endprotoent (void)
75 while (start != NULL)
77 next = start;
78 start = start->next;
79 free (next);
83 static enum nss_status
84 internal_nis_setprotoent (void)
86 char *domainname;
87 struct ypall_callback ypcb;
88 enum nss_status status;
90 yp_get_default_domain (&domainname);
92 internal_nis_endprotoent ();
94 ypcb.foreach = saveit;
95 ypcb.data = NULL;
96 status = yperr2nss (yp_all (domainname, "protocols.bynumber", &ypcb));
97 next = start;
99 return status;
102 enum nss_status
103 _nss_nis_setprotoent (int stayopen)
105 enum nss_status status;
107 __libc_lock_lock (lock);
109 status = internal_nis_setprotoent ();
111 __libc_lock_unlock (lock);
113 return status;
116 enum nss_status
117 _nss_nis_endprotoent (void)
119 __libc_lock_lock (lock);
121 internal_nis_endprotoent ();
122 next = NULL;
124 __libc_lock_unlock (lock);
126 return NSS_STATUS_SUCCESS;
129 static enum nss_status
130 internal_nis_getprotoent_r (struct protoent *proto,
131 char *buffer, size_t buflen, int *errnop)
133 struct parser_data *data = (void *) buffer;
134 int parse_res;
136 if (start == NULL)
137 internal_nis_setprotoent ();
139 /* Get the next entry until we found a correct one. */
142 char *p;
144 if (next == NULL)
145 return NSS_STATUS_NOTFOUND;
147 p = strncpy (buffer, next->val, buflen);
149 while (isspace (*p))
150 ++p;
152 parse_res = _nss_files_parse_protoent (p, proto, data, buflen, errnop);
153 if (parse_res == -1)
154 return NSS_STATUS_TRYAGAIN;
155 next = next->next;
157 while (!parse_res);
159 return NSS_STATUS_SUCCESS;
162 enum nss_status
163 _nss_nis_getprotoent_r (struct protoent *proto, char *buffer, size_t buflen,
164 int *errnop)
166 enum nss_status status;
168 __libc_lock_lock (lock);
170 status = internal_nis_getprotoent_r (proto, buffer, buflen, errnop);
172 __libc_lock_unlock (lock);
174 return status;
177 enum nss_status
178 _nss_nis_getprotobyname_r (const char *name, struct protoent *proto,
179 char *buffer, size_t buflen, int *errnop)
181 if (name == NULL)
183 *errnop = EINVAL;
184 return NSS_STATUS_UNAVAIL;
187 char *domain;
188 if (__builtin_expect (yp_get_default_domain (&domain), 0))
189 return NSS_STATUS_UNAVAIL;
191 char *result;
192 int len;
193 int yperr = yp_match (domain, "protocols.byname", name, strlen (name),
194 &result, &len);
196 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
198 enum nss_status retval = yperr2nss (yperr);
200 if (retval == NSS_STATUS_TRYAGAIN)
201 *errnop = errno;
202 return retval;
205 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
207 free (result);
208 *errnop = ERANGE;
209 return NSS_STATUS_TRYAGAIN;
212 char *p = strncpy (buffer, result, len);
213 buffer[len] = '\0';
214 while (isspace (*p))
215 ++p;
216 free (result);
218 int parse_res = _nss_files_parse_protoent (p, proto, (void *) buffer, buflen,
219 errnop);
220 if (__builtin_expect (parse_res < 1, 0))
222 if (parse_res == -1)
223 return NSS_STATUS_TRYAGAIN;
224 else
225 return NSS_STATUS_NOTFOUND;
227 return NSS_STATUS_SUCCESS;
230 enum nss_status
231 _nss_nis_getprotobynumber_r (int number, struct protoent *proto,
232 char *buffer, size_t buflen, int *errnop)
234 char *domain;
235 if (__builtin_expect (yp_get_default_domain (&domain), 0))
236 return NSS_STATUS_UNAVAIL;
238 char buf[32];
239 int nlen = snprintf (buf, sizeof (buf), "%d", number);
241 char *result;
242 int len;
243 int yperr = yp_match (domain, "protocols.bynumber", buf, nlen, &result,
244 &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_protoent (p, proto, (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 return NSS_STATUS_SUCCESS;