Updated to fedora-glibc-20071011T1636
[glibc.git] / nis / nss_nis / nis-proto.c
blob1480a928b5d4647c244c8c974ed545abac2b9629
1 /* Copyright (C) 1996-1998, 2000-2004, 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 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 protoent
33 #define EXTERN_PARSER
34 #include <nss/nss_files/files-parse.c>
36 __libc_lock_define_initialized (static, lock)
38 struct response
40 struct response *next;
41 char val[0];
44 static struct response *start;
45 static struct response *next;
47 static int
48 saveit (int instatus, char *inkey, int inkeylen, char *inval,
49 int invallen, char *indata)
51 if (instatus != YP_TRUE)
52 return 1;
54 if (inkey && inkeylen > 0 && inval && invallen > 0)
56 struct response *newp = malloc (sizeof (struct response) + invallen + 1);
57 if (newp == NULL)
58 return 1; /* We have no error code for out of memory */
60 if (start == NULL)
61 start = newp;
62 else
63 next->next = newp;
64 next = newp;
66 newp->next = NULL;
67 *((char *) mempcpy (newp->val, inval, invallen)) = '\0';
70 return 0;
73 static void
74 internal_nis_endprotoent (void)
76 while (start != NULL)
78 next = start;
79 start = start->next;
80 free (next);
84 static enum nss_status
85 internal_nis_setprotoent (void)
87 char *domainname;
88 struct ypall_callback ypcb;
89 enum nss_status status;
91 yp_get_default_domain (&domainname);
93 internal_nis_endprotoent ();
95 ypcb.foreach = saveit;
96 ypcb.data = NULL;
97 status = yperr2nss (yp_all (domainname, "protocols.bynumber", &ypcb));
98 next = start;
100 return status;
103 enum nss_status
104 _nss_nis_setprotoent (int stayopen)
106 enum nss_status status;
108 __libc_lock_lock (lock);
110 status = internal_nis_setprotoent ();
112 __libc_lock_unlock (lock);
114 return status;
117 enum nss_status
118 _nss_nis_endprotoent (void)
120 __libc_lock_lock (lock);
122 internal_nis_endprotoent ();
123 next = NULL;
125 __libc_lock_unlock (lock);
127 return NSS_STATUS_SUCCESS;
130 static enum nss_status
131 internal_nis_getprotoent_r (struct protoent *proto,
132 char *buffer, size_t buflen, int *errnop)
134 struct parser_data *data = (void *) buffer;
135 int parse_res;
137 if (start == NULL)
138 internal_nis_setprotoent ();
140 /* Get the next entry until we found a correct one. */
143 char *p;
145 if (next == NULL)
146 return NSS_STATUS_NOTFOUND;
148 p = strncpy (buffer, next->val, buflen);
150 while (isspace (*p))
151 ++p;
153 parse_res = _nss_files_parse_protoent (p, proto, data, buflen, errnop);
154 if (parse_res == -1)
155 return NSS_STATUS_TRYAGAIN;
156 next = next->next;
158 while (!parse_res);
160 return NSS_STATUS_SUCCESS;
163 enum nss_status
164 _nss_nis_getprotoent_r (struct protoent *proto, char *buffer, size_t buflen,
165 int *errnop)
167 enum nss_status status;
169 __libc_lock_lock (lock);
171 status = internal_nis_getprotoent_r (proto, buffer, buflen, errnop);
173 __libc_lock_unlock (lock);
175 return status;
178 enum nss_status
179 _nss_nis_getprotobyname_r (const char *name, struct protoent *proto,
180 char *buffer, size_t buflen, int *errnop)
182 if (name == NULL)
184 *errnop = EINVAL;
185 return NSS_STATUS_UNAVAIL;
188 char *domain;
189 if (__builtin_expect (yp_get_default_domain (&domain), 0))
190 return NSS_STATUS_UNAVAIL;
192 char *result;
193 int len;
194 int yperr = yp_match (domain, "protocols.byname", name, strlen (name),
195 &result, &len);
197 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
199 enum nss_status retval = yperr2nss (yperr);
201 if (retval == NSS_STATUS_TRYAGAIN)
202 *errnop = errno;
203 return retval;
206 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
208 free (result);
209 *errnop = ERANGE;
210 return NSS_STATUS_TRYAGAIN;
213 char *p = strncpy (buffer, result, len);
214 buffer[len] = '\0';
215 while (isspace (*p))
216 ++p;
217 free (result);
219 int parse_res = _nss_files_parse_protoent (p, proto, (void *) buffer, buflen,
220 errnop);
221 if (__builtin_expect (parse_res < 1, 0))
223 if (parse_res == -1)
224 return NSS_STATUS_TRYAGAIN;
225 else
226 return NSS_STATUS_NOTFOUND;
228 return NSS_STATUS_SUCCESS;
231 enum nss_status
232 _nss_nis_getprotobynumber_r (int number, struct protoent *proto,
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, "protocols.bynumber", buf, nlen, &result,
245 &len);
247 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
249 enum nss_status retval = yperr2nss (yperr);
251 if (retval == NSS_STATUS_TRYAGAIN)
252 *errnop = errno;
253 return retval;
256 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
258 free (result);
259 *errnop = ERANGE;
260 return NSS_STATUS_TRYAGAIN;
263 char *p = strncpy (buffer, result, len);
264 buffer[len] = '\0';
265 while (isspace (*p))
266 ++p;
267 free (result);
269 int parse_res = _nss_files_parse_protoent (p, proto, (void *) buffer, buflen,
270 errnop);
271 if (__builtin_expect (parse_res < 1, 0))
273 if (parse_res == -1)
274 return NSS_STATUS_TRYAGAIN;
275 else
276 return NSS_STATUS_NOTFOUND;
278 return NSS_STATUS_SUCCESS;