(EM_*): Synch with official list.
[glibc.git] / nis / nss_nis / nis-network.c
blobca02f019025538d4c0aed468243702fd37bd90a1
1 /* Copyright (C) 1996, 1997, 1998, 1999, 2000 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <nss.h>
21 #include <netdb.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <bits/libc-lock.h>
29 #include <rpcsvc/yp.h>
30 #include <rpcsvc/ypclnt.h>
32 #include "nss-nis.h"
34 /* Get the declaration of the parser function. */
35 #define ENTNAME netent
36 #define EXTERN_PARSER
37 #include <nss/nss_files/files-parse.c>
39 __libc_lock_define_initialized (static, lock)
41 static bool_t new_start = 1;
42 static char *oldkey = NULL;
43 static int oldkeylen = 0;
45 enum nss_status
46 _nss_nis_setnetent (int stayopen)
48 __libc_lock_lock (lock);
50 new_start = 1;
51 if (oldkey != NULL)
53 free (oldkey);
54 oldkey = NULL;
55 oldkeylen = 0;
58 __libc_lock_unlock (lock);
60 return NSS_STATUS_SUCCESS;
63 enum nss_status
64 _nss_nis_endnetent (void)
66 __libc_lock_lock (lock);
68 new_start = 1;
69 if (oldkey != NULL)
71 free (oldkey);
72 oldkey = NULL;
73 oldkeylen = 0;
76 __libc_lock_unlock (lock);
78 return NSS_STATUS_SUCCESS;
81 static enum nss_status
82 internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
83 int *errnop, int *herrnop)
85 struct parser_data *data = (void *) buffer;
86 char *domain, *result, *outkey;
87 int len, keylen, parse_res;
89 if (yp_get_default_domain (&domain))
90 return NSS_STATUS_UNAVAIL;
92 /* Get the next entry until we found a correct one. */
95 enum nss_status retval;
96 char *p;
98 if (new_start)
99 retval = yperr2nss (yp_first (domain, "networks.byname",
100 &outkey, &keylen, &result, &len));
101 else
102 retval = yperr2nss ( yp_next (domain, "networks.byname",
103 oldkey, oldkeylen,
104 &outkey, &keylen, &result, &len));
106 if (retval != NSS_STATUS_SUCCESS)
108 if (retval == NSS_STATUS_NOTFOUND)
109 *errnop = ENOENT;
110 else if (retval == NSS_STATUS_TRYAGAIN)
112 *herrnop = NETDB_INTERNAL;
113 *errnop = errno;
115 return retval;
118 if ((size_t) (len + 1) > buflen)
120 free (result);
121 *errnop = ERANGE;
122 *herrnop = NETDB_INTERNAL;
123 return NSS_STATUS_TRYAGAIN;
126 p = strncpy (buffer, result, len);
127 buffer[len] = '\0';
128 while (isspace (*p))
129 ++p;
130 free (result);
132 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
133 if (parse_res == -1)
135 free (outkey);
136 *herrnop = NETDB_INTERNAL;
137 *errnop = ERANGE;
138 return NSS_STATUS_TRYAGAIN;
141 free (oldkey);
142 oldkey = outkey;
143 oldkeylen = keylen;
144 new_start = 0;
146 while (!parse_res);
148 return NSS_STATUS_SUCCESS;
151 enum nss_status
152 _nss_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
153 int *errnop, int *herrnop)
155 enum nss_status status;
157 __libc_lock_lock (lock);
159 status = internal_nis_getnetent_r (net, buffer, buflen, errnop, herrnop);
161 __libc_lock_unlock (lock);
163 return status;
166 enum nss_status
167 _nss_nis_getnetbyname_r (const char *name, struct netent *net, char *buffer,
168 size_t buflen, int *errnop, int *herrnop)
170 enum nss_status retval;
171 struct parser_data *data = (void *) buffer;
172 char *domain, *result, *p;
173 int len, parse_res;
175 if (name == NULL)
177 *errnop = EINVAL;
178 *herrnop = NETDB_INTERNAL;
179 return NSS_STATUS_UNAVAIL;
182 if (yp_get_default_domain (&domain))
183 return NSS_STATUS_UNAVAIL;
185 if (buflen < sizeof *data + 1)
187 *herrnop = NETDB_INTERNAL;
188 *errnop = ERANGE;
189 return NSS_STATUS_TRYAGAIN;
191 else
193 /* Convert name to lowercase. */
194 size_t namlen = strlen (name);
195 char name2[namlen + 1];
196 int i;
198 for (i = 0; i < namlen; ++i)
199 name2[i] = _tolower (name[i]);
200 name2[i] = '\0';
202 retval = yperr2nss (yp_match (domain, "networks.byname", name2,
203 namlen, &result, &len));
207 if (retval != NSS_STATUS_SUCCESS)
209 if (retval == NSS_STATUS_NOTFOUND)
210 *errnop = ENOENT;
211 else if (retval == NSS_STATUS_TRYAGAIN)
213 *errnop = errno;
214 *herrnop = NETDB_INTERNAL;
216 return retval;
219 if ((size_t) (len + 1) > buflen)
221 free (result);
222 *errnop = ERANGE;
223 *herrnop = NETDB_INTERNAL;
224 return NSS_STATUS_TRYAGAIN;
227 p = strncpy (buffer, result, len);
228 buffer[len] = '\0';
229 while (isspace (*p))
230 ++p;
231 free (result);
233 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
235 if (parse_res < 1)
237 *herrnop = NETDB_INTERNAL;
238 if (parse_res == -1)
239 return NSS_STATUS_TRYAGAIN;
240 else
242 *errnop = ENOENT;
243 return NSS_STATUS_NOTFOUND;
246 else
247 return NSS_STATUS_SUCCESS;
250 enum nss_status
251 _nss_nis_getnetbyaddr_r (uint32_t addr, int type, struct netent *net,
252 char *buffer, size_t buflen, int *errnop,
253 int *herrnop)
255 struct parser_data *data = (void *) buffer;
256 char *domain;
257 char *result;
258 int len;
259 char buf[256];
260 int blen;
261 struct in_addr in;
262 char *p;
264 if (yp_get_default_domain (&domain))
265 return NSS_STATUS_UNAVAIL;
267 in = inet_makeaddr (addr, 0);
268 strcpy (buf, inet_ntoa (in));
269 blen = strlen (buf);
271 while (1)
273 enum nss_status retval;
274 int parse_res;
276 retval = yperr2nss (yp_match (domain, "networks.byaddr", buf,
277 strlen (buf), &result, &len));
279 if (retval != NSS_STATUS_SUCCESS)
281 if (retval == NSS_STATUS_NOTFOUND)
283 if (buf[blen - 2] == '.' && buf[blen - 1] == '0')
285 /* Try again, but with trailing dot(s)
286 removed (one by one) */
287 buf[blen - 2] = '\0';
288 blen -= 2;
289 continue;
291 else
293 *errnop = ENOENT;
294 return NSS_STATUS_NOTFOUND;
297 else
299 if (retval == NSS_STATUS_TRYAGAIN)
300 *errnop = errno;
301 return retval;
305 if ((size_t) (len + 1) > buflen)
307 free (result);
308 *errnop = ERANGE;
309 *herrnop = NETDB_INTERNAL;
310 return NSS_STATUS_TRYAGAIN;
313 p = strncpy (buffer, result, len);
314 buffer[len] = '\0';
315 while (isspace (*p))
316 ++p;
317 free (result);
319 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
321 if (parse_res < 1)
323 *herrnop = NETDB_INTERNAL;
324 if (parse_res == -1)
325 return NSS_STATUS_TRYAGAIN;
326 else
328 *errnop = ENOENT;
329 return NSS_STATUS_NOTFOUND;
332 else
333 return NSS_STATUS_SUCCESS;