8d87c69d8e1757088dfeffa8cff3bef18e003613
[platform/upstream/glibc.git] / nis / nss_nis / nis-network.c
1 /* Copyright (C) 1996-2020 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 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.
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    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18
19 #include <nss.h>
20 #include <netdb.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <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;
42 static int oldkeylen;
43
44 enum nss_status
45 _nss_nis_setnetent (int stayopen)
46 {
47   return _nss_nis_endnetent ();
48 }
49
50 enum nss_status
51 _nss_nis_endnetent (void)
52 {
53 __libc_lock_lock (lock);
54
55   new_start = 1;
56   if (oldkey != NULL)
57     {
58       free (oldkey);
59       oldkey = NULL;
60       oldkeylen = 0;
61     }
62
63   __libc_lock_unlock (lock);
64
65   return NSS_STATUS_SUCCESS;
66 }
67 libnss_nis_hidden_def (_nss_nis_endnetent)
68
69 static enum nss_status
70 internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
71                           int *errnop, int *herrnop)
72 {
73   struct parser_data *data = (void *) buffer;
74
75   char *domain;
76   if (__glibc_unlikely (yp_get_default_domain (&domain)))
77     return NSS_STATUS_UNAVAIL;
78
79   /* Get the next entry until we found a correct one. */
80   int parse_res;
81   do
82     {
83       char *result;
84       char *outkey;
85       int len;
86       int keylen;
87       int yperr;
88
89       if (new_start)
90         yperr = yp_first (domain, "networks.byname", &outkey, &keylen, &result,
91                           &len);
92       else
93         yperr = yp_next (domain, "networks.byname", oldkey, oldkeylen, &outkey,
94                          &keylen, &result, &len);
95
96       if (__glibc_unlikely (yperr != YPERR_SUCCESS))
97         {
98           enum nss_status retval = yperr2nss (yperr);
99
100           if (retval == NSS_STATUS_TRYAGAIN)
101             {
102               *herrnop = NETDB_INTERNAL;
103               *errnop = errno;
104             }
105           return retval;
106         }
107
108       if (__glibc_unlikely ((size_t) (len + 1) > buflen))
109         {
110           free (result);
111           *errnop = ERANGE;
112           *herrnop = NETDB_INTERNAL;
113           return NSS_STATUS_TRYAGAIN;
114         }
115
116       char *p = strncpy (buffer, result, len);
117       buffer[len] = '\0';
118       while (isspace (*p))
119         ++p;
120       free (result);
121
122       parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
123       if (__glibc_unlikely (parse_res == -1))
124         {
125           free (outkey);
126           *herrnop = NETDB_INTERNAL;
127           *errnop = ERANGE;
128           return NSS_STATUS_TRYAGAIN;
129         }
130
131       free (oldkey);
132       oldkey = outkey;
133       oldkeylen = keylen;
134       new_start = 0;
135     }
136   while (!parse_res);
137
138   return NSS_STATUS_SUCCESS;
139 }
140
141 enum nss_status
142 _nss_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
143                       int *errnop, int *herrnop)
144 {
145   enum nss_status status;
146
147   __libc_lock_lock (lock);
148
149   status = internal_nis_getnetent_r (net, buffer, buflen, errnop, herrnop);
150
151   __libc_lock_unlock (lock);
152
153   return status;
154 }
155
156 enum nss_status
157 _nss_nis_getnetbyname_r (const char *name, struct netent *net, char *buffer,
158                          size_t buflen, int *errnop, int *herrnop)
159 {
160   if (name == NULL)
161     {
162       *errnop = EINVAL;
163       *herrnop = NETDB_INTERNAL;
164       return NSS_STATUS_UNAVAIL;
165     }
166
167   char *domain;
168   if (__glibc_unlikely (yp_get_default_domain (&domain)))
169     return NSS_STATUS_UNAVAIL;
170
171   struct parser_data *data = (void *) buffer;
172   if (buflen < sizeof *data + 1)
173     {
174       *herrnop = NETDB_INTERNAL;
175       *errnop = ERANGE;
176       return NSS_STATUS_TRYAGAIN;
177     }
178
179   /* Convert name to lowercase.  */
180   size_t namlen = strlen (name);
181   /* Limit name length to the maximum size of an RPC packet.  */
182   if (namlen > UDPMSGSIZE)
183     {
184       *errnop = ERANGE;
185       return NSS_STATUS_UNAVAIL;
186     }
187
188   char name2[namlen + 1];
189   size_t i;
190
191   for (i = 0; i < namlen; ++i)
192     name2[i] = _tolower (name[i]);
193   name2[i] = '\0';
194
195   char *result;
196   int len;
197   int yperr = yp_match (domain, "networks.byname", name2, namlen, &result,
198                         &len);
199
200   if (__glibc_unlikely (yperr != YPERR_SUCCESS))
201     {
202       enum nss_status retval = yperr2nss (yperr);
203
204       if (retval == NSS_STATUS_TRYAGAIN)
205         {
206           *errnop = errno;
207           *herrnop = NETDB_INTERNAL;
208         }
209       return retval;
210     }
211
212   if (__glibc_unlikely ((size_t) (len + 1) > buflen))
213     {
214       free (result);
215       *errnop = ERANGE;
216       *herrnop = NETDB_INTERNAL;
217       return NSS_STATUS_TRYAGAIN;
218     }
219
220   char *p = strncpy (buffer, result, len);
221   buffer[len] = '\0';
222   while (isspace (*p))
223     ++p;
224   free (result);
225
226   int parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
227
228   if (__glibc_unlikely (parse_res < 1))
229     {
230       *herrnop = NETDB_INTERNAL;
231       if (parse_res == -1)
232         return NSS_STATUS_TRYAGAIN;
233       else
234         return NSS_STATUS_NOTFOUND;
235     }
236   else
237     return NSS_STATUS_SUCCESS;
238 }
239
240 enum nss_status
241 _nss_nis_getnetbyaddr_r (uint32_t addr, int type, struct netent *net,
242                          char *buffer, size_t buflen, int *errnop,
243                          int *herrnop)
244 {
245   char *domain;
246   if (__glibc_unlikely (yp_get_default_domain (&domain)))
247     return NSS_STATUS_UNAVAIL;
248
249   struct in_addr in = { .s_addr = htonl (addr) };
250   char *buf = inet_ntoa (in);
251   size_t blen = strlen (buf);
252
253   while (1)
254     {
255       char *result;
256       int len;
257
258       int yperr = yp_match (domain, "networks.byaddr", buf, blen, &result,
259                             &len);
260
261       if (__glibc_unlikely (yperr != YPERR_SUCCESS))
262           {
263             enum nss_status retval = yperr2nss (yperr);
264
265             if (retval == NSS_STATUS_NOTFOUND)
266               {
267                 if (buf[blen - 2] == '.' && buf[blen - 1] == '0')
268                   {
269                     /* Try again, but with trailing dot(s)
270                        removed (one by one) */
271                     buf[blen - 2] = '\0';
272                     blen -= 2;
273                     continue;
274                   }
275                 else
276                   return NSS_STATUS_NOTFOUND;
277               }
278             else
279               {
280                 if (retval == NSS_STATUS_TRYAGAIN)
281                   *errnop = errno;
282                 return retval;
283               }
284           }
285
286       if (__glibc_unlikely ((size_t) (len + 1) > buflen))
287         {
288           free (result);
289           *errnop = ERANGE;
290           *herrnop = NETDB_INTERNAL;
291           return NSS_STATUS_TRYAGAIN;
292         }
293
294         char *p = strncpy (buffer, result, len);
295         buffer[len] = '\0';
296         while (isspace (*p))
297           ++p;
298         free (result);
299
300         int parse_res = _nss_files_parse_netent (p, net, (void *) buffer,
301                                                  buflen, errnop);
302
303         if (__glibc_unlikely (parse_res < 1))
304           {
305             *herrnop = NETDB_INTERNAL;
306             if (parse_res == -1)
307               return NSS_STATUS_TRYAGAIN;
308             else
309               return NSS_STATUS_NOTFOUND;
310           }
311         else
312           return NSS_STATUS_SUCCESS;
313     }
314 }