xref: /dragonfly/lib/libc/net/getnetnamadr.c (revision cf93a39fef9ce0912092ad4da6e6d8c2ac8e8126)
1 /*-
2  * Copyright (c) 1994, Garrett Wollman
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: src/lib/libc/net/getnetnamadr.c,v 1.23 2006/04/28 12:03:35 ume Exp $
26  */
27 
28 #include "namespace.h"
29 #include "reentrant.h"
30 #include <sys/param.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <netdb.h>
35 #include <stdio.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdarg.h>
41 #include <nsswitch.h>
42 #include "un-namespace.h"
43 #include "netdb_private.h"
44 #ifdef NS_CACHING
45 #include "nscache.h"
46 #endif
47 
48 /* Network lookup order if nsswitch.conf is broken or nonexistant */
49 static const ns_src default_src[] = {
50           { NSSRC_FILES, NS_SUCCESS },
51           { NSSRC_DNS, NS_SUCCESS },
52           { 0 }
53 };
54 
55 NETDB_THREAD_ALLOC(netent_data)
NETDB_THREAD_ALLOC(netdata)56 NETDB_THREAD_ALLOC(netdata)
57 
58 #ifdef NS_CACHING
59 static int
60 net_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata)
61 {
62           char *name;
63           uint32_t net;
64           int type;
65 
66           size_t desired_size, size;
67           enum nss_lookup_type lookup_type;
68           int res = NS_UNAVAIL;
69 
70           lookup_type = (enum nss_lookup_type)cache_mdata;
71           switch (lookup_type) {
72           case nss_lt_name:
73                     name = va_arg(ap, char *);
74 
75                     size = strlen(name);
76                     desired_size = sizeof(enum nss_lookup_type) + size + 1;
77                     if (desired_size > *buffer_size) {
78                               res = NS_RETURN;
79                               goto fin;
80                     }
81 
82                     memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
83                     memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
84 
85                     res = NS_SUCCESS;
86                     break;
87           case nss_lt_id:
88                     net = va_arg(ap, uint32_t);
89                     type = va_arg(ap, int);
90 
91                     desired_size = sizeof(enum nss_lookup_type) +
92                         sizeof(uint32_t) + sizeof(int);
93                     if (desired_size > *buffer_size) {
94                               res = NS_RETURN;
95                               goto fin;
96                     }
97 
98                     memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
99                     memcpy(buffer + sizeof(enum nss_lookup_type), &net,
100                         sizeof(uint32_t));
101                     memcpy(buffer + sizeof(enum nss_lookup_type) + sizeof(uint32_t),
102                         &type, sizeof(int));
103 
104                     res = NS_SUCCESS;
105                     break;
106           default:
107                     /* should be unreachable */
108                     return (NS_UNAVAIL);
109           }
110 
111 fin:
112           *buffer_size = desired_size;
113           return (res);
114 }
115 
116 
117 static int
net_marshal_func(char * buffer,size_t * buffer_size,void * retval __unused,va_list ap,void * cache_mdata)118 net_marshal_func(char *buffer, size_t *buffer_size, void *retval __unused,
119                      va_list ap, void *cache_mdata)
120 {
121           char *name __unused;
122           uint32_t net __unused;
123           int type __unused;
124           struct netent *ne;
125           char *orig_buf __unused;
126           size_t orig_buf_size __unused;
127 
128           struct netent new_ne;
129           size_t desired_size, size, aliases_size;
130           char *p;
131           char **alias;
132 
133           switch ((enum nss_lookup_type)cache_mdata) {
134           case nss_lt_name:
135                     name = va_arg(ap, char *);
136                     break;
137           case nss_lt_id:
138                     net = va_arg(ap, uint32_t);
139                     type = va_arg(ap, int);
140           break;
141           case nss_lt_all:
142                     break;
143           default:
144                     /* should be unreachable */
145                     return (NS_UNAVAIL);
146           }
147 
148           ne = va_arg(ap, struct netent *);
149           orig_buf = va_arg(ap, char *);
150           orig_buf_size = va_arg(ap, size_t);
151 
152           desired_size = _ALIGNBYTES + sizeof(struct netent) + sizeof(char *);
153           if (ne->n_name != NULL)
154                     desired_size += strlen(ne->n_name) + 1;
155 
156           if (ne->n_aliases != NULL) {
157                     aliases_size = 0;
158                     for (alias = ne->n_aliases; *alias; ++alias) {
159                               desired_size += strlen(*alias) + 1;
160                               ++aliases_size;
161                     }
162 
163                     desired_size += _ALIGNBYTES +
164                         (aliases_size + 1) * sizeof(char *);
165           }
166 
167           if (*buffer_size < desired_size) {
168                     /* this assignment is here for future use */
169                     *buffer_size = desired_size;
170                     return (NS_RETURN);
171           }
172 
173           memcpy(&new_ne, ne, sizeof(struct netent));
174 
175           *buffer_size = desired_size;
176           memset(buffer, 0, desired_size);
177           p = buffer + sizeof(struct netent) + sizeof(char *);
178           memcpy(buffer + sizeof(struct netent), &p, sizeof(char *));
179           p = (char *)_ALIGN(p);
180 
181           if (new_ne.n_name != NULL) {
182                     size = strlen(new_ne.n_name);
183                     memcpy(p, new_ne.n_name, size);
184                     new_ne.n_name = p;
185                     p += size + 1;
186           }
187 
188           if (new_ne.n_aliases != NULL) {
189                     p = (char *)_ALIGN(p);
190                     memcpy(p, new_ne.n_aliases, sizeof(char *) * aliases_size);
191                     new_ne.n_aliases = (char **)p;
192                     p += sizeof(char *) * (aliases_size + 1);
193 
194                     for (alias = new_ne.n_aliases; *alias; ++alias) {
195                               size = strlen(*alias);
196                               memcpy(p, *alias, size);
197                               *alias = p;
198                               p += size + 1;
199                     }
200           }
201 
202           memcpy(buffer, &new_ne, sizeof(struct netent));
203           return (NS_SUCCESS);
204 }
205 
206 static int
net_unmarshal_func(char * buffer,size_t buffer_size,void * retval,va_list ap,void * cache_mdata)207 net_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
208                        void *cache_mdata)
209 {
210           char *name __unused;
211           uint32_t net __unused;
212           int type __unused;
213           struct netent *ne;
214           char *orig_buf;
215           size_t orig_buf_size;
216           int *ret_errno;
217 
218           char *p;
219           char **alias;
220 
221           switch ((enum nss_lookup_type)cache_mdata) {
222           case nss_lt_name:
223                     name = va_arg(ap, char *);
224                     break;
225           case nss_lt_id:
226                     net = va_arg(ap, uint32_t);
227                     type = va_arg(ap, int);
228                     break;
229           case nss_lt_all:
230                     break;
231           default:
232                     /* should be unreachable */
233                     return (NS_UNAVAIL);
234           }
235 
236           ne = va_arg(ap, struct netent *);
237           orig_buf = va_arg(ap, char *);
238           orig_buf_size = va_arg(ap, size_t);
239           ret_errno = va_arg(ap, int *);
240 
241           if (orig_buf_size <
242               buffer_size - sizeof(struct netent) - sizeof(char *)) {
243                     *ret_errno = ERANGE;
244                     return (NS_RETURN);
245           }
246 
247           memcpy(ne, buffer, sizeof(struct netent));
248           memcpy(&p, buffer + sizeof(struct netent), sizeof(char *));
249 
250           orig_buf = (char *)_ALIGN(orig_buf);
251           memcpy(orig_buf, buffer + sizeof(struct netent) + sizeof(char *) +
252               _ALIGN(p) - (size_t)p,
253               buffer_size - sizeof(struct netent) - sizeof(char *) -
254               _ALIGN(p) + (size_t)p);
255           p = (char *)_ALIGN(p);
256 
257           NS_APPLY_OFFSET(ne->n_name, orig_buf, p, char *);
258           if (ne->n_aliases != NULL) {
259                     NS_APPLY_OFFSET(ne->n_aliases, orig_buf, p, char **);
260 
261                     for (alias = ne->n_aliases; *alias; ++alias)
262                               NS_APPLY_OFFSET(*alias, orig_buf, p, char *);
263           }
264 
265           if (retval != NULL)
266                     *((struct netent **)retval) = ne;
267 
268           return (NS_SUCCESS);
269 }
270 #endif /* NS_CACHING */
271 
272 static void
netent_data_free(void * ptr)273 netent_data_free(void *ptr)
274 {
275           struct netent_data *ned = ptr;
276 
277           if (ned == NULL)
278                     return;
279           ned->stayopen = 0;
280           _endnethtent(ned);
281           free(ned);
282 }
283 
284 static void
netdata_free(void * ptr)285 netdata_free(void *ptr)
286 {
287           free(ptr);
288 }
289 
290 int
__copy_netent(struct netent * ne,struct netent * nptr,char * buf,size_t buflen)291 __copy_netent(struct netent *ne, struct netent *nptr, char *buf, size_t buflen)
292 {
293           char *cp;
294           int i, n;
295           int numptr, len;
296 
297           /* Find out the amount of space required to store the answer. */
298           numptr = 1; /* NULL ptr */
299           len = (char *)ALIGN(buf) - buf;
300           for (i = 0; ne->n_aliases[i]; i++, numptr++) {
301                     len += strlen(ne->n_aliases[i]) + 1;
302           }
303           len += strlen(ne->n_name) + 1;
304           len += numptr * sizeof(char*);
305 
306           if (len > (int)buflen) {
307                     errno = ERANGE;
308                     return (-1);
309           }
310 
311           /* copy net value and type */
312           nptr->n_addrtype = ne->n_addrtype;
313           nptr->n_net = ne->n_net;
314 
315           cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
316 
317           /* copy official name */
318           n = strlen(ne->n_name) + 1;
319           strcpy(cp, ne->n_name);
320           nptr->n_name = cp;
321           cp += n;
322 
323           /* copy aliases */
324           nptr->n_aliases = (char **)ALIGN(buf);
325           for (i = 0 ; ne->n_aliases[i]; i++) {
326                     n = strlen(ne->n_aliases[i]) + 1;
327                     strcpy(cp, ne->n_aliases[i]);
328                     nptr->n_aliases[i] = cp;
329                     cp += n;
330           }
331           nptr->n_aliases[i] = NULL;
332 
333           return (0);
334 }
335 
336 int
getnetbyname_r(const char * name,struct netent * ne,char * buffer,size_t buflen,struct netent ** result,int * h_errnop)337 getnetbyname_r(const char *name, struct netent *ne, char *buffer,
338                  size_t buflen, struct netent **result, int *h_errnop)
339 {
340 #ifdef NS_CACHING
341           static const nss_cache_info cache_info =
342                     NS_COMMON_CACHE_INFO_INITIALIZER(
343                     networks, (void *)nss_lt_name,
344                     net_id_func, net_marshal_func, net_unmarshal_func);
345 #endif
346           static const ns_dtab dtab[] = {
347                     NS_FILES_CB(_ht_getnetbyname, NULL)
348                     { NSSRC_DNS, _dns_getnetbyname, NULL },
349                     NS_NIS_CB(_nis_getnetbyname, NULL) /* force -DHESIOD */
350 #ifdef NS_CACHING
351                     NS_CACHE_CB(&cache_info)
352 #endif
353                     { 0 }
354           };
355           int rval, ret_errno;
356 
357           rval = _nsdispatch((void *)result, dtab, NSDB_NETWORKS,
358               "getnetbyname_r", default_src, name, ne, buffer, buflen,
359               &ret_errno, h_errnop);
360 
361           return ((rval == NS_SUCCESS) ? 0 : -1);
362 }
363 
364 int
getnetbyaddr_r(uint32_t addr,int af,struct netent * ne,char * buffer,size_t buflen,struct netent ** result,int * h_errnop)365 getnetbyaddr_r(uint32_t addr, int af, struct netent *ne, char *buffer,
366                  size_t buflen, struct netent **result, int *h_errnop)
367 {
368 #ifdef NS_CACHING
369           static const nss_cache_info cache_info =
370                     NS_COMMON_CACHE_INFO_INITIALIZER(
371                     networks, (void *)nss_lt_id,
372                     net_id_func, net_marshal_func, net_unmarshal_func);
373 #endif
374           static const ns_dtab dtab[] = {
375                     NS_FILES_CB(_ht_getnetbyaddr, NULL)
376                     { NSSRC_DNS, _dns_getnetbyaddr, NULL },
377                     NS_NIS_CB(_nis_getnetbyaddr, NULL) /* force -DHESIOD */
378 #ifdef NS_CACHING
379                     NS_CACHE_CB(&cache_info)
380 #endif
381                     { 0 }
382           };
383           int rval, ret_errno;
384 
385           rval = _nsdispatch((void *)result, dtab, NSDB_NETWORKS,
386               "getnetbyaddr_r", default_src, addr, af, ne, buffer, buflen,
387               &ret_errno, h_errnop);
388 
389           return ((rval == NS_SUCCESS) ? 0 : -1);
390 }
391 
392 struct netent *
getnetbyname(const char * name)393 getnetbyname(const char *name)
394 {
395           struct netdata *nd;
396           struct netent *rval;
397           int ret_h_errno;
398 
399           if ((nd = __netdata_init()) == NULL)
400                     return (NULL);
401           if (getnetbyname_r(name, &nd->net, nd->data, sizeof(nd->data), &rval,
402               &ret_h_errno) != 0)
403                     return (NULL);
404           return (rval);
405 }
406 
407 struct netent *
getnetbyaddr(uint32_t addr,int af)408 getnetbyaddr(uint32_t addr, int af)
409 {
410           struct netdata *nd;
411           struct netent *rval;
412           int ret_h_errno;
413 
414           if ((nd = __netdata_init()) == NULL)
415                     return (NULL);
416           if (getnetbyaddr_r(addr, af, &nd->net, nd->data, sizeof(nd->data),
417               &rval, &ret_h_errno) != 0)
418                     return (NULL);
419           return (rval);
420 }
421 
422 void
setnetent(int stayopen)423 setnetent(int stayopen)
424 {
425           struct netent_data *ned;
426 
427           if ((ned = __netent_data_init()) == NULL)
428                     return;
429           _setnethtent(stayopen, ned);
430           _setnetdnsent(stayopen);
431 }
432 
433 void
endnetent(void)434 endnetent(void)
435 {
436           struct netent_data *ned;
437 
438           if ((ned = __netent_data_init()) == NULL)
439                     return;
440           _endnethtent(ned);
441           _endnetdnsent();
442 }
443