Fix typo.
[glibc/history.git] / nis / nss_nis / nis-network.c
blob802c03229ecba31d1d6aa1978f635305b35f2d88
1 /* Copyright (C) 1996, 1997, 1998 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 <string.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <bits/libc-lock.h>
28 #include <rpcsvc/yp.h>
29 #include <rpcsvc/ypclnt.h>
31 #include "nss-nis.h"
33 /* Get the declaration of the parser function. */
34 #define ENTNAME netent
35 #define EXTERN_PARSER
36 #include <nss/nss_files/files-parse.c>
38 __libc_lock_define_initialized (static, lock)
40 static bool_t new_start = 1;
41 static char *oldkey = NULL;
42 static int oldkeylen = 0;
44 enum nss_status
45 _nss_nis_setnetent (void)
47 __libc_lock_lock (lock);
49 new_start = 1;
50 if (oldkey != NULL)
52 free (oldkey);
53 oldkey = NULL;
54 oldkeylen = 0;
57 __libc_lock_unlock (lock);
59 return NSS_STATUS_SUCCESS;
62 enum nss_status
63 _nss_nis_endnetent (void)
65 __libc_lock_lock (lock);
67 new_start = 1;
68 if (oldkey != NULL)
70 free (oldkey);
71 oldkey = NULL;
72 oldkeylen = 0;
75 __libc_lock_unlock (lock);
77 return NSS_STATUS_SUCCESS;
80 static enum nss_status
81 internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
82 int *errnop, int *herrnop)
84 struct parser_data *data = (void *) buffer;
85 char *domain, *result, *outkey;
86 int len, keylen, parse_res;
88 if (yp_get_default_domain (&domain))
89 return NSS_STATUS_UNAVAIL;
91 /* Get the next entry until we found a correct one. */
94 enum nss_status retval;
95 char *p;
97 if (new_start)
98 retval = yperr2nss (yp_first (domain, "networks.byname",
99 &outkey, &keylen, &result, &len));
100 else
101 retval = yperr2nss ( yp_next (domain, "networks.byname",
102 oldkey, oldkeylen,
103 &outkey, &keylen, &result, &len));
105 if (retval != NSS_STATUS_SUCCESS)
107 if (retval == NSS_STATUS_TRYAGAIN)
109 *herrnop = NETDB_INTERNAL;
110 *errnop = errno;
112 return retval;
115 if ((size_t) (len + 1) > buflen)
117 free (result);
118 *errnop = ERANGE;
119 *herrnop = NETDB_INTERNAL;
120 return NSS_STATUS_TRYAGAIN;
123 p = strncpy (buffer, result, len);
124 buffer[len] = '\0';
125 while (isspace (*p))
126 ++p;
127 free (result);
129 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
130 if (parse_res == -1)
132 free (outkey);
133 *herrnop = NETDB_INTERNAL;
134 *errnop = ERANGE;
135 return NSS_STATUS_TRYAGAIN;
138 free (oldkey);
139 oldkey = outkey;
140 oldkeylen = keylen;
141 new_start = 0;
143 while (!parse_res);
145 return NSS_STATUS_SUCCESS;
148 enum nss_status
149 _nss_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
150 int *errnop, int *herrnop)
152 enum nss_status status;
154 __libc_lock_lock (lock);
156 status = internal_nis_getnetent_r (net, buffer, buflen, errnop, herrnop);
158 __libc_lock_unlock (lock);
160 return status;
163 enum nss_status
164 _nss_nis_getnetbyname_r (const char *name, struct netent *net, char *buffer,
165 size_t buflen, int *errnop, int *herrnop)
167 enum nss_status retval;
168 struct parser_data *data = (void *) buffer;
169 char *domain, *result, *p;
170 int len, parse_res;
172 if (name == NULL)
174 *errnop = EINVAL;
175 *herrnop = NETDB_INTERNAL;
176 return NSS_STATUS_UNAVAIL;
179 if (yp_get_default_domain (&domain))
180 return NSS_STATUS_UNAVAIL;
182 if (buflen < sizeof *data + 1)
184 *herrnop = NETDB_INTERNAL;
185 *errnop = ERANGE;
186 return NSS_STATUS_TRYAGAIN;
188 else
190 /* Convert name to lowercase. */
191 size_t namlen = strlen (name);
192 char name2[namlen + 1];
193 int i;
195 for (i = 0; i < namlen; ++i)
196 name2[i] = tolower (name[i]);
197 name2[i] = '\0';
199 retval = yperr2nss (yp_match (domain, "networks.byname", name2,
200 namlen, &result, &len));
204 if (retval != NSS_STATUS_SUCCESS)
206 if (retval == NSS_STATUS_TRYAGAIN)
208 *errnop = errno;
209 *herrnop = NETDB_INTERNAL;
211 return retval;
214 if ((size_t) (len + 1) > buflen)
216 free (result);
217 *errnop = ERANGE;
218 *herrnop = NETDB_INTERNAL;
219 return NSS_STATUS_TRYAGAIN;
222 p = strncpy (buffer, result, len);
223 buffer[len] = '\0';
224 while (isspace (*p))
225 ++p;
226 free (result);
228 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
230 if (parse_res < 1)
232 *herrnop = NETDB_INTERNAL;
233 if (parse_res == -1)
234 return NSS_STATUS_TRYAGAIN;
235 else
236 return NSS_STATUS_NOTFOUND;
238 else
239 return NSS_STATUS_SUCCESS;
242 enum nss_status
243 _nss_nis_getnetbyaddr_r (unsigned long addr, int type, struct netent *net,
244 char *buffer, size_t buflen, int *errnop,
245 int *herrnop)
247 struct parser_data *data = (void *) buffer;
248 char *domain;
249 char *result;
250 int len;
251 char buf[256];
252 int blen;
253 struct in_addr in;
254 char *p;
256 if (yp_get_default_domain (&domain))
257 return NSS_STATUS_UNAVAIL;
259 in = inet_makeaddr (addr, 0);
260 strcpy (buf, inet_ntoa (in));
261 blen = strlen (buf);
263 while (1)
265 enum nss_status retval;
266 int parse_res;
268 retval = yperr2nss (yp_match (domain, "networks.byaddr", buf,
269 strlen (buf), &result, &len));
271 if (retval != NSS_STATUS_SUCCESS)
273 if (retval == NSS_STATUS_NOTFOUND)
275 if (buf[blen - 2] == '.' && buf[blen - 1] == '0')
277 /* Try again, but with trailing dot(s)
278 removed (one by one) */
279 buf[blen - 2] = '\0';
280 blen -= 2;
281 continue;
283 else
284 return NSS_STATUS_NOTFOUND;
286 else
288 if (retval == NSS_STATUS_TRYAGAIN)
289 *errnop = errno;
290 return retval;
294 if ((size_t) (len + 1) > buflen)
296 free (result);
297 *errnop = ERANGE;
298 *herrnop = NETDB_INTERNAL;
299 return NSS_STATUS_TRYAGAIN;
302 p = strncpy (buffer, result, len);
303 buffer[len] = '\0';
304 while (isspace (*p))
305 ++p;
306 free (result);
308 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
310 if (parse_res < 1)
312 *herrnop = NETDB_INTERNAL;
313 if (parse_res == -1)
314 return NSS_STATUS_TRYAGAIN;
315 else
316 return NSS_STATUS_NOTFOUND;
318 else
319 return NSS_STATUS_SUCCESS;