2.9
[glibc/nacl-glibc.git] / nis / nss_nis / nis-network.c
blob9b02302e0b521d0ec84264ce166255d14e9ba7ea
1 /* Copyright (C) 1996-2003, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 /* The following is an ugly trick to avoid a prototype declaration for
22 _nss_nis_endgrent. */
23 #define _nss_nis_endnetent _nss_nis_endnetent_XXX
24 #include <netdb.h>
25 #undef _nss_nis_endnetent
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <bits/libc-lock.h>
33 #include <rpcsvc/yp.h>
34 #include <rpcsvc/ypclnt.h>
36 #include "nss-nis.h"
38 /* Get the declaration of the parser function. */
39 #define ENTNAME netent
40 #define EXTERN_PARSER
41 #include <nss/nss_files/files-parse.c>
43 __libc_lock_define_initialized (static, lock)
45 static bool_t new_start = 1;
46 static char *oldkey;
47 static int oldkeylen;
49 enum nss_status
50 _nss_nis_setnetent (int stayopen)
52 __libc_lock_lock (lock);
54 new_start = 1;
55 if (oldkey != NULL)
57 free (oldkey);
58 oldkey = NULL;
59 oldkeylen = 0;
62 __libc_lock_unlock (lock);
64 return NSS_STATUS_SUCCESS;
66 /* Make _nss_nis_endnetent an alias of _nss_nis_setnetent. We do this
67 even though the prototypes don't match. The argument of setnetent
68 is not used so this makes no difference. */
69 strong_alias (_nss_nis_setnetent, _nss_nis_endnetent)
71 static enum nss_status
72 internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
73 int *errnop, int *herrnop)
75 struct parser_data *data = (void *) buffer;
77 char *domain;
78 if (__builtin_expect (yp_get_default_domain (&domain), 0))
79 return NSS_STATUS_UNAVAIL;
81 /* Get the next entry until we found a correct one. */
82 int parse_res;
85 char *result;
86 char *outkey;
87 int len;
88 int keylen;
89 int yperr;
91 if (new_start)
92 yperr = yp_first (domain, "networks.byname", &outkey, &keylen, &result,
93 &len);
94 else
95 yperr = yp_next (domain, "networks.byname", oldkey, oldkeylen, &outkey,
96 &keylen, &result, &len);
98 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
100 enum nss_status retval = yperr2nss (yperr);
102 if (retval == NSS_STATUS_TRYAGAIN)
104 *herrnop = NETDB_INTERNAL;
105 *errnop = errno;
107 return retval;
110 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
112 free (result);
113 *errnop = ERANGE;
114 *herrnop = NETDB_INTERNAL;
115 return NSS_STATUS_TRYAGAIN;
118 char *p = strncpy (buffer, result, len);
119 buffer[len] = '\0';
120 while (isspace (*p))
121 ++p;
122 free (result);
124 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
125 if (__builtin_expect (parse_res == -1, 0))
127 free (outkey);
128 *herrnop = NETDB_INTERNAL;
129 *errnop = ERANGE;
130 return NSS_STATUS_TRYAGAIN;
133 free (oldkey);
134 oldkey = outkey;
135 oldkeylen = keylen;
136 new_start = 0;
138 while (!parse_res);
140 return NSS_STATUS_SUCCESS;
143 enum nss_status
144 _nss_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
145 int *errnop, int *herrnop)
147 enum nss_status status;
149 __libc_lock_lock (lock);
151 status = internal_nis_getnetent_r (net, buffer, buflen, errnop, herrnop);
153 __libc_lock_unlock (lock);
155 return status;
158 enum nss_status
159 _nss_nis_getnetbyname_r (const char *name, struct netent *net, char *buffer,
160 size_t buflen, int *errnop, int *herrnop)
162 if (name == NULL)
164 *errnop = EINVAL;
165 *herrnop = NETDB_INTERNAL;
166 return NSS_STATUS_UNAVAIL;
169 char *domain;
170 if (__builtin_expect (yp_get_default_domain (&domain), 0))
171 return NSS_STATUS_UNAVAIL;
173 struct parser_data *data = (void *) buffer;
174 if (buflen < sizeof *data + 1)
176 *herrnop = NETDB_INTERNAL;
177 *errnop = ERANGE;
178 return NSS_STATUS_TRYAGAIN;
181 /* Convert name to lowercase. */
182 size_t namlen = strlen (name);
183 char name2[namlen + 1];
184 size_t i;
186 for (i = 0; i < namlen; ++i)
187 name2[i] = _tolower (name[i]);
188 name2[i] = '\0';
190 char *result;
191 int len;
192 int yperr = yp_match (domain, "networks.byname", name2, namlen, &result,
193 &len);
195 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
197 enum nss_status retval = yperr2nss (yperr);
199 if (retval == NSS_STATUS_TRYAGAIN)
201 *errnop = errno;
202 *herrnop = NETDB_INTERNAL;
204 return retval;
207 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
209 free (result);
210 *errnop = ERANGE;
211 *herrnop = NETDB_INTERNAL;
212 return NSS_STATUS_TRYAGAIN;
215 char *p = strncpy (buffer, result, len);
216 buffer[len] = '\0';
217 while (isspace (*p))
218 ++p;
219 free (result);
221 int parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
223 if (__builtin_expect (parse_res < 1, 0))
225 *herrnop = NETDB_INTERNAL;
226 if (parse_res == -1)
227 return NSS_STATUS_TRYAGAIN;
228 else
229 return NSS_STATUS_NOTFOUND;
231 else
232 return NSS_STATUS_SUCCESS;
235 enum nss_status
236 _nss_nis_getnetbyaddr_r (uint32_t addr, int type, struct netent *net,
237 char *buffer, size_t buflen, int *errnop,
238 int *herrnop)
240 char *domain;
241 if (__builtin_expect (yp_get_default_domain (&domain), 0))
242 return NSS_STATUS_UNAVAIL;
244 struct in_addr in = inet_makeaddr (addr, 0);
245 char *buf = inet_ntoa (in);
246 size_t blen = strlen (buf);
248 while (1)
250 char *result;
251 int len;
253 int yperr = yp_match (domain, "networks.byaddr", buf, blen, &result,
254 &len);
256 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
258 enum nss_status retval = yperr2nss (yperr);
260 if (retval == NSS_STATUS_NOTFOUND)
262 if (buf[blen - 2] == '.' && buf[blen - 1] == '0')
264 /* Try again, but with trailing dot(s)
265 removed (one by one) */
266 buf[blen - 2] = '\0';
267 blen -= 2;
268 continue;
270 else
271 return NSS_STATUS_NOTFOUND;
273 else
275 if (retval == NSS_STATUS_TRYAGAIN)
276 *errnop = errno;
277 return retval;
281 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
283 free (result);
284 *errnop = ERANGE;
285 *herrnop = NETDB_INTERNAL;
286 return NSS_STATUS_TRYAGAIN;
289 char *p = strncpy (buffer, result, len);
290 buffer[len] = '\0';
291 while (isspace (*p))
292 ++p;
293 free (result);
295 int parse_res = _nss_files_parse_netent (p, net, (void *) buffer,
296 buflen, errnop);
298 if (__builtin_expect (parse_res < 1, 0))
300 *herrnop = NETDB_INTERNAL;
301 if (parse_res == -1)
302 return NSS_STATUS_TRYAGAIN;
303 else
304 return NSS_STATUS_NOTFOUND;
306 else
307 return NSS_STATUS_SUCCESS;