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