iAdd __nscd_getai prototype.
[glibc.git] / nis / nss_nis / nis-network.c
blobed8439c814e302a111cdec9bb9bc362efdf641ed
1 /* Copyright (C) 1996-2000, 2001, 2002, 2003 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;
76 char *domain, *result, *outkey;
77 int len, keylen, parse_res;
79 if (yp_get_default_domain (&domain))
80 return NSS_STATUS_UNAVAIL;
82 /* Get the next entry until we found a correct one. */
85 enum nss_status retval;
86 char *p;
88 if (new_start)
89 retval = yperr2nss (yp_first (domain, "networks.byname",
90 &outkey, &keylen, &result, &len));
91 else
92 retval = yperr2nss ( yp_next (domain, "networks.byname",
93 oldkey, oldkeylen,
94 &outkey, &keylen, &result, &len));
96 if (retval != NSS_STATUS_SUCCESS)
98 if (retval == NSS_STATUS_TRYAGAIN)
100 *herrnop = NETDB_INTERNAL;
101 *errnop = errno;
103 return retval;
106 if ((size_t) (len + 1) > buflen)
108 free (result);
109 *errnop = ERANGE;
110 *herrnop = NETDB_INTERNAL;
111 return NSS_STATUS_TRYAGAIN;
114 p = strncpy (buffer, result, len);
115 buffer[len] = '\0';
116 while (isspace (*p))
117 ++p;
118 free (result);
120 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
121 if (parse_res == -1)
123 free (outkey);
124 *herrnop = NETDB_INTERNAL;
125 *errnop = ERANGE;
126 return NSS_STATUS_TRYAGAIN;
129 free (oldkey);
130 oldkey = outkey;
131 oldkeylen = keylen;
132 new_start = 0;
134 while (!parse_res);
136 return NSS_STATUS_SUCCESS;
139 enum nss_status
140 _nss_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
141 int *errnop, int *herrnop)
143 enum nss_status status;
145 __libc_lock_lock (lock);
147 status = internal_nis_getnetent_r (net, buffer, buflen, errnop, herrnop);
149 __libc_lock_unlock (lock);
151 return status;
154 enum nss_status
155 _nss_nis_getnetbyname_r (const char *name, struct netent *net, char *buffer,
156 size_t buflen, int *errnop, int *herrnop)
158 enum nss_status retval;
159 struct parser_data *data = (void *) buffer;
160 char *domain, *result, *p;
161 int len, parse_res;
163 if (name == NULL)
165 *errnop = EINVAL;
166 *herrnop = NETDB_INTERNAL;
167 return NSS_STATUS_UNAVAIL;
170 if (yp_get_default_domain (&domain))
171 return NSS_STATUS_UNAVAIL;
173 if (buflen < sizeof *data + 1)
175 *herrnop = NETDB_INTERNAL;
176 *errnop = ERANGE;
177 return NSS_STATUS_TRYAGAIN;
179 else
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 retval = yperr2nss (yp_match (domain, "networks.byname", name2,
191 namlen, &result, &len));
195 if (retval != NSS_STATUS_SUCCESS)
197 if (retval == NSS_STATUS_TRYAGAIN)
199 *errnop = errno;
200 *herrnop = NETDB_INTERNAL;
202 return retval;
205 if ((size_t) (len + 1) > buflen)
207 free (result);
208 *errnop = ERANGE;
209 *herrnop = NETDB_INTERNAL;
210 return NSS_STATUS_TRYAGAIN;
213 p = strncpy (buffer, result, len);
214 buffer[len] = '\0';
215 while (isspace (*p))
216 ++p;
217 free (result);
219 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
221 if (parse_res < 1)
223 *herrnop = NETDB_INTERNAL;
224 if (parse_res == -1)
225 return NSS_STATUS_TRYAGAIN;
226 else
227 return NSS_STATUS_NOTFOUND;
229 else
230 return NSS_STATUS_SUCCESS;
233 enum nss_status
234 _nss_nis_getnetbyaddr_r (uint32_t addr, int type, struct netent *net,
235 char *buffer, size_t buflen, int *errnop,
236 int *herrnop)
238 struct parser_data *data = (void *) buffer;
239 char *domain;
240 char *result;
241 int len;
242 char buf[256];
243 int blen;
244 struct in_addr in;
245 char *p;
247 if (yp_get_default_domain (&domain))
248 return NSS_STATUS_UNAVAIL;
250 in = inet_makeaddr (addr, 0);
251 strcpy (buf, inet_ntoa (in));
252 blen = strlen (buf);
254 while (1)
256 enum nss_status retval;
257 int parse_res;
259 retval = yperr2nss (yp_match (domain, "networks.byaddr", buf,
260 strlen (buf), &result, &len));
262 if (retval != NSS_STATUS_SUCCESS)
264 if (retval == NSS_STATUS_NOTFOUND)
266 if (buf[blen - 2] == '.' && buf[blen - 1] == '0')
268 /* Try again, but with trailing dot(s)
269 removed (one by one) */
270 buf[blen - 2] = '\0';
271 blen -= 2;
272 continue;
274 else
275 return NSS_STATUS_NOTFOUND;
277 else
279 if (retval == NSS_STATUS_TRYAGAIN)
280 *errnop = errno;
281 return retval;
285 if ((size_t) (len + 1) > buflen)
287 free (result);
288 *errnop = ERANGE;
289 *herrnop = NETDB_INTERNAL;
290 return NSS_STATUS_TRYAGAIN;
293 p = strncpy (buffer, result, len);
294 buffer[len] = '\0';
295 while (isspace (*p))
296 ++p;
297 free (result);
299 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
301 if (parse_res < 1)
303 *herrnop = NETDB_INTERNAL;
304 if (parse_res == -1)
305 return NSS_STATUS_TRYAGAIN;
306 else
307 return NSS_STATUS_NOTFOUND;
309 else
310 return NSS_STATUS_SUCCESS;