Update.
[platform/upstream/linaro-glibc.git] / nis / nss_nis / nis-network.c
1 /* Copyright (C) 1996, 1997 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.
4
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.
9
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.
14
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.  */
19
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>
30
31 #include "nss-nis.h"
32
33 /* Get the declaration of the parser function.  */
34 #define ENTNAME netent
35 #define EXTERN_PARSER
36 #include <nss/nss_files/files-parse.c>
37
38 __libc_lock_define_initialized (static, lock)
39
40 static bool_t new_start = 1;
41 static char *oldkey = NULL;
42 static int oldkeylen = 0;
43
44 enum nss_status
45 _nss_nis_setnetent (void)
46 {
47   __libc_lock_lock (lock);
48
49   new_start = 1;
50   if (oldkey != NULL)
51     {
52       free (oldkey);
53       oldkey = NULL;
54       oldkeylen = 0;
55     }
56
57   __libc_lock_unlock (lock);
58
59   return NSS_STATUS_SUCCESS;
60 }
61
62 enum nss_status
63 _nss_nis_endnetent (void)
64 {
65   __libc_lock_lock (lock);
66
67   new_start = 1;
68   if (oldkey != NULL)
69     {
70       free (oldkey);
71       oldkey = NULL;
72       oldkeylen = 0;
73     }
74
75   __libc_lock_unlock (lock);
76
77   return NSS_STATUS_SUCCESS;
78 }
79
80 static enum nss_status
81 internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
82                           int *errnop, int *herrnop)
83 {
84   struct parser_data *data = (void *) buffer;
85   char *domain, *result, *outkey;
86   int len, keylen, parse_res;
87
88   if (yp_get_default_domain (&domain))
89     return NSS_STATUS_UNAVAIL;
90
91   /* Get the next entry until we found a correct one. */
92   do
93     {
94       enum nss_status retval;
95       char *p;
96
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));
104
105       if (retval != NSS_STATUS_SUCCESS)
106         {
107           if (retval == NSS_STATUS_TRYAGAIN)
108             {
109               *herrnop = NETDB_INTERNAL;
110               *errnop = errno;
111             }
112           return retval;
113         }
114
115       if ((size_t) (len + 1) > buflen)
116         {
117           free (result);
118           __set_errno (ERANGE);
119           *herrnop = NETDB_INTERNAL;
120           return NSS_STATUS_TRYAGAIN;
121         }
122
123       p = strncpy (buffer, result, len);
124       buffer[len] = '\0';
125       while (isspace (*p))
126         ++p;
127       free (result);
128
129       parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
130       if (parse_res == -1)
131         {
132           free (outkey);
133           *herrnop = NETDB_INTERNAL;
134           *errnop = ERANGE;
135           return NSS_STATUS_TRYAGAIN;
136         }
137
138       free (oldkey);
139       oldkey = outkey;
140       oldkeylen = keylen;
141       new_start = 0;
142     }
143   while (!parse_res);
144
145   return NSS_STATUS_SUCCESS;
146 }
147
148 enum nss_status
149 _nss_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
150                       int *errnop, int *herrnop)
151 {
152   enum nss_status status;
153
154   __libc_lock_lock (lock);
155
156   status = internal_nis_getnetent_r (net, buffer, buflen, errnop, herrnop);
157
158   __libc_lock_unlock (lock);
159
160   return status;
161 }
162
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)
166 {
167   enum nss_status retval;
168   struct parser_data *data = (void *) buffer;
169   char *domain, *result, *p;
170   int len, parse_res;
171
172   if (name == NULL)
173     {
174       __set_errno (EINVAL);
175       *herrnop = NETDB_INTERNAL;
176       return NSS_STATUS_UNAVAIL;
177     }
178
179   if (yp_get_default_domain (&domain))
180     return NSS_STATUS_UNAVAIL;
181
182   retval = yperr2nss (yp_match (domain, "networks.byname", name,
183                                 strlen (name), &result, &len));
184
185   if (retval != NSS_STATUS_SUCCESS)
186     {
187       if (retval == NSS_STATUS_TRYAGAIN)
188         {
189           *errnop = errno;
190           *herrnop = NETDB_INTERNAL;
191         }
192       return retval;
193     }
194
195   if ((size_t) (len + 1) > buflen)
196     {
197       free (result);
198       *errnop = ERANGE;
199       *herrnop = NETDB_INTERNAL;
200       return NSS_STATUS_TRYAGAIN;
201     }
202
203   p = strncpy (buffer, result, len);
204   buffer[len] = '\0';
205   while (isspace (*p))
206     ++p;
207   free (result);
208
209   parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
210
211   if (parse_res < 1)
212     {
213       *herrnop = NETDB_INTERNAL;
214       if (parse_res == -1)
215         return NSS_STATUS_TRYAGAIN;
216       else
217         return NSS_STATUS_NOTFOUND;
218     }
219   else
220     return NSS_STATUS_SUCCESS;
221 }
222
223 enum nss_status
224 _nss_nis_getnetbyaddr_r (unsigned long addr, int type, struct netent *net,
225                          char *buffer, size_t buflen, int *errnop,
226                          int *herrnop)
227 {
228   struct parser_data *data = (void *) buffer;
229   char *domain;
230   char *result;
231   int len;
232   char buf[256];
233   int blen;
234   struct in_addr in;
235   char *p;
236
237   if (yp_get_default_domain (&domain))
238     return NSS_STATUS_UNAVAIL;
239
240   in = inet_makeaddr (addr, 0);
241   strcpy (buf, inet_ntoa (in));
242   blen = strlen (buf);
243
244   while (1)
245     {
246       enum nss_status retval;
247       int parse_res;
248
249       retval = yperr2nss (yp_match (domain, "networks.byaddr", buf,
250                                     strlen (buf), &result, &len));
251
252         if (retval != NSS_STATUS_SUCCESS)
253           {
254             if (retval == NSS_STATUS_NOTFOUND)
255               {
256                 if (buf[blen - 2] == '.' && buf[blen - 1] == '0')
257                   {
258                     /* Try again, but with trailing dot(s)
259                        removed (one by one) */
260                     buf[blen - 2] = '\0';
261                     blen -= 2;
262                     continue;
263                   }
264                 else
265                   return NSS_STATUS_NOTFOUND;
266               }
267             else
268               {
269                 if (retval == NSS_STATUS_TRYAGAIN)
270                   *errnop = errno;
271                 return retval;
272               }
273           }
274
275       if ((size_t) (len + 1) > buflen)
276         {
277           free (result);
278           *errnop = ERANGE;
279           *herrnop = NETDB_INTERNAL;
280           return NSS_STATUS_TRYAGAIN;
281         }
282
283         p = strncpy (buffer, result, len);
284         buffer[len] = '\0';
285         while (isspace (*p))
286           ++p;
287         free (result);
288
289         parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
290
291         if (parse_res < 1)
292           {
293             *herrnop = NETDB_INTERNAL;
294             if (parse_res == -1)
295               return NSS_STATUS_TRYAGAIN;
296             else
297               return NSS_STATUS_NOTFOUND;
298           }
299         else
300           return NSS_STATUS_SUCCESS;
301     }
302 }