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 extern int _ht_getnetbyname(void *, void *, va_list); 49 extern int _dns_getnetbyname(void *, void *, va_list); 50 extern int _nis_getnetbyname(void *, void *, va_list); 51 extern int _ht_getnetbyaddr(void *, void *, va_list); 52 extern int _dns_getnetbyaddr(void *, void *, va_list); 53 extern int _nis_getnetbyaddr(void *, void *, va_list); 54 55 /* Network lookup order if nsswitch.conf is broken or nonexistant */ 56 static const ns_src default_src[] = { 57 { NSSRC_FILES, NS_SUCCESS }, 58 { NSSRC_DNS, NS_SUCCESS }, 59 { 0 } 60 }; 61 62 NETDB_THREAD_ALLOC(netent_data) 63 NETDB_THREAD_ALLOC(netdata) 64 65 #ifdef NS_CACHING 66 static int 67 net_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata) 68 { 69 char *name; 70 uint32_t net; 71 int type; 72 73 size_t desired_size, size; 74 enum nss_lookup_type lookup_type; 75 int res = NS_UNAVAIL; 76 77 lookup_type = (enum nss_lookup_type)cache_mdata; 78 switch (lookup_type) { 79 case nss_lt_name: 80 name = va_arg(ap, char *); 81 82 size = strlen(name); 83 desired_size = sizeof(enum nss_lookup_type) + size + 1; 84 if (desired_size > *buffer_size) { 85 res = NS_RETURN; 86 goto fin; 87 } 88 89 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type)); 90 memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1); 91 92 res = NS_SUCCESS; 93 break; 94 case nss_lt_id: 95 net = va_arg(ap, uint32_t); 96 type = va_arg(ap, int); 97 98 desired_size = sizeof(enum nss_lookup_type) + 99 sizeof(uint32_t) + sizeof(int); 100 if (desired_size > *buffer_size) { 101 res = NS_RETURN; 102 goto fin; 103 } 104 105 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type)); 106 memcpy(buffer + sizeof(enum nss_lookup_type), &net, 107 sizeof(uint32_t)); 108 memcpy(buffer + sizeof(enum nss_lookup_type) + sizeof(uint32_t), 109 &type, sizeof(int)); 110 111 res = NS_SUCCESS; 112 break; 113 default: 114 /* should be unreachable */ 115 return (NS_UNAVAIL); 116 } 117 118 fin: 119 *buffer_size = desired_size; 120 return (res); 121 } 122 123 124 static int 125 net_marshal_func(char *buffer, size_t *buffer_size, void *retval __unused, 126 va_list ap, void *cache_mdata) 127 { 128 char *name __unused; 129 uint32_t net __unused; 130 int type __unused; 131 struct netent *ne; 132 char *orig_buf __unused; 133 size_t orig_buf_size __unused; 134 135 struct netent new_ne; 136 size_t desired_size, size, aliases_size; 137 char *p; 138 char **alias; 139 140 switch ((enum nss_lookup_type)cache_mdata) { 141 case nss_lt_name: 142 name = va_arg(ap, char *); 143 break; 144 case nss_lt_id: 145 net = va_arg(ap, uint32_t); 146 type = va_arg(ap, int); 147 break; 148 case nss_lt_all: 149 break; 150 default: 151 /* should be unreachable */ 152 return (NS_UNAVAIL); 153 } 154 155 ne = va_arg(ap, struct netent *); 156 orig_buf = va_arg(ap, char *); 157 orig_buf_size = va_arg(ap, size_t); 158 159 desired_size = _ALIGNBYTES + sizeof(struct netent) + sizeof(char *); 160 if (ne->n_name != NULL) 161 desired_size += strlen(ne->n_name) + 1; 162 163 if (ne->n_aliases != NULL) { 164 aliases_size = 0; 165 for (alias = ne->n_aliases; *alias; ++alias) { 166 desired_size += strlen(*alias) + 1; 167 ++aliases_size; 168 } 169 170 desired_size += _ALIGNBYTES + 171 (aliases_size + 1) * sizeof(char *); 172 } 173 174 if (*buffer_size < desired_size) { 175 /* this assignment is here for future use */ 176 *buffer_size = desired_size; 177 return (NS_RETURN); 178 } 179 180 memcpy(&new_ne, ne, sizeof(struct netent)); 181 182 *buffer_size = desired_size; 183 memset(buffer, 0, desired_size); 184 p = buffer + sizeof(struct netent) + sizeof(char *); 185 memcpy(buffer + sizeof(struct netent), &p, sizeof(char *)); 186 p = (char *)_ALIGN(p); 187 188 if (new_ne.n_name != NULL) { 189 size = strlen(new_ne.n_name); 190 memcpy(p, new_ne.n_name, size); 191 new_ne.n_name = p; 192 p += size + 1; 193 } 194 195 if (new_ne.n_aliases != NULL) { 196 p = (char *)_ALIGN(p); 197 memcpy(p, new_ne.n_aliases, sizeof(char *) * aliases_size); 198 new_ne.n_aliases = (char **)p; 199 p += sizeof(char *) * (aliases_size + 1); 200 201 for (alias = new_ne.n_aliases; *alias; ++alias) { 202 size = strlen(*alias); 203 memcpy(p, *alias, size); 204 *alias = p; 205 p += size + 1; 206 } 207 } 208 209 memcpy(buffer, &new_ne, sizeof(struct netent)); 210 return (NS_SUCCESS); 211 } 212 213 static int 214 net_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap, 215 void *cache_mdata) 216 { 217 char *name __unused; 218 uint32_t net __unused; 219 int type __unused; 220 struct netent *ne; 221 char *orig_buf; 222 size_t orig_buf_size; 223 int *ret_errno; 224 225 char *p; 226 char **alias; 227 228 switch ((enum nss_lookup_type)cache_mdata) { 229 case nss_lt_name: 230 name = va_arg(ap, char *); 231 break; 232 case nss_lt_id: 233 net = va_arg(ap, uint32_t); 234 type = va_arg(ap, int); 235 break; 236 case nss_lt_all: 237 break; 238 default: 239 /* should be unreachable */ 240 return (NS_UNAVAIL); 241 } 242 243 ne = va_arg(ap, struct netent *); 244 orig_buf = va_arg(ap, char *); 245 orig_buf_size = va_arg(ap, size_t); 246 ret_errno = va_arg(ap, int *); 247 248 if (orig_buf_size < 249 buffer_size - sizeof(struct netent) - sizeof(char *)) { 250 *ret_errno = ERANGE; 251 return (NS_RETURN); 252 } 253 254 memcpy(ne, buffer, sizeof(struct netent)); 255 memcpy(&p, buffer + sizeof(struct netent), sizeof(char *)); 256 257 orig_buf = (char *)_ALIGN(orig_buf); 258 memcpy(orig_buf, buffer + sizeof(struct netent) + sizeof(char *) + 259 _ALIGN(p) - (size_t)p, 260 buffer_size - sizeof(struct netent) - sizeof(char *) - 261 _ALIGN(p) + (size_t)p); 262 p = (char *)_ALIGN(p); 263 264 NS_APPLY_OFFSET(ne->n_name, orig_buf, p, char *); 265 if (ne->n_aliases != NULL) { 266 NS_APPLY_OFFSET(ne->n_aliases, orig_buf, p, char **); 267 268 for (alias = ne->n_aliases; *alias; ++alias) 269 NS_APPLY_OFFSET(*alias, orig_buf, p, char *); 270 } 271 272 if (retval != NULL) 273 *((struct netent **)retval) = ne; 274 275 return (NS_SUCCESS); 276 } 277 #endif /* NS_CACHING */ 278 279 static void 280 netent_data_free(void *ptr) 281 { 282 struct netent_data *ned = ptr; 283 284 if (ned == NULL) 285 return; 286 ned->stayopen = 0; 287 _endnethtent(ned); 288 free(ned); 289 } 290 291 static void 292 netdata_free(void *ptr) 293 { 294 free(ptr); 295 } 296 297 int 298 __copy_netent(struct netent *ne, struct netent *nptr, char *buf, size_t buflen) 299 { 300 char *cp; 301 int i, n; 302 int numptr, len; 303 304 /* Find out the amount of space required to store the answer. */ 305 numptr = 1; /* NULL ptr */ 306 len = (char *)ALIGN(buf) - buf; 307 for (i = 0; ne->n_aliases[i]; i++, numptr++) { 308 len += strlen(ne->n_aliases[i]) + 1; 309 } 310 len += strlen(ne->n_name) + 1; 311 len += numptr * sizeof(char*); 312 313 if (len > (int)buflen) { 314 errno = ERANGE; 315 return (-1); 316 } 317 318 /* copy net value and type */ 319 nptr->n_addrtype = ne->n_addrtype; 320 nptr->n_net = ne->n_net; 321 322 cp = (char *)ALIGN(buf) + numptr * sizeof(char *); 323 324 /* copy official name */ 325 n = strlen(ne->n_name) + 1; 326 strcpy(cp, ne->n_name); 327 nptr->n_name = cp; 328 cp += n; 329 330 /* copy aliases */ 331 nptr->n_aliases = (char **)ALIGN(buf); 332 for (i = 0 ; ne->n_aliases[i]; i++) { 333 n = strlen(ne->n_aliases[i]) + 1; 334 strcpy(cp, ne->n_aliases[i]); 335 nptr->n_aliases[i] = cp; 336 cp += n; 337 } 338 nptr->n_aliases[i] = NULL; 339 340 return (0); 341 } 342 343 int 344 getnetbyname_r(const char *name, struct netent *ne, char *buffer, 345 size_t buflen, struct netent **result, int *h_errorp) 346 { 347 #ifdef NS_CACHING 348 static const nss_cache_info cache_info = 349 NS_COMMON_CACHE_INFO_INITIALIZER( 350 networks, (void *)nss_lt_name, 351 net_id_func, net_marshal_func, net_unmarshal_func); 352 #endif 353 static const ns_dtab dtab[] = { 354 NS_FILES_CB(_ht_getnetbyname, NULL) 355 { NSSRC_DNS, _dns_getnetbyname, NULL }, 356 NS_NIS_CB(_nis_getnetbyname, NULL) /* force -DHESIOD */ 357 #ifdef NS_CACHING 358 NS_CACHE_CB(&cache_info) 359 #endif 360 { 0 } 361 }; 362 int rval, ret_errno; 363 364 rval = _nsdispatch((void *)result, dtab, NSDB_NETWORKS, 365 "getnetbyname_r", default_src, name, ne, buffer, buflen, 366 &ret_errno, h_errorp); 367 368 return ((rval == NS_SUCCESS) ? 0 : -1); 369 } 370 371 int 372 getnetbyaddr_r(uint32_t addr, int af, struct netent *ne, char *buffer, 373 size_t buflen, struct netent **result, int *h_errorp) 374 { 375 #ifdef NS_CACHING 376 static const nss_cache_info cache_info = 377 NS_COMMON_CACHE_INFO_INITIALIZER( 378 networks, (void *)nss_lt_id, 379 net_id_func, net_marshal_func, net_unmarshal_func); 380 #endif 381 static const ns_dtab dtab[] = { 382 NS_FILES_CB(_ht_getnetbyaddr, NULL) 383 { NSSRC_DNS, _dns_getnetbyaddr, NULL }, 384 NS_NIS_CB(_nis_getnetbyaddr, NULL) /* force -DHESIOD */ 385 #ifdef NS_CACHING 386 NS_CACHE_CB(&cache_info) 387 #endif 388 { 0 } 389 }; 390 int rval, ret_errno; 391 392 rval = _nsdispatch((void *)result, dtab, NSDB_NETWORKS, 393 "getnetbyaddr_r", default_src, addr, af, ne, buffer, buflen, 394 &ret_errno, h_errorp); 395 396 return ((rval == NS_SUCCESS) ? 0 : -1); 397 } 398 399 struct netent * 400 getnetbyname(const char *name) 401 { 402 struct netdata *nd; 403 struct netent *rval; 404 int ret_h_errno; 405 406 if ((nd = __netdata_init()) == NULL) 407 return (NULL); 408 if (getnetbyname_r(name, &nd->net, nd->data, sizeof(nd->data), &rval, 409 &ret_h_errno) != 0) 410 return (NULL); 411 return (rval); 412 } 413 414 struct netent * 415 getnetbyaddr(uint32_t addr, int af) 416 { 417 struct netdata *nd; 418 struct netent *rval; 419 int ret_h_errno; 420 421 if ((nd = __netdata_init()) == NULL) 422 return (NULL); 423 if (getnetbyaddr_r(addr, af, &nd->net, nd->data, sizeof(nd->data), 424 &rval, &ret_h_errno) != 0) 425 return (NULL); 426 return (rval); 427 } 428 429 void 430 setnetent(int stayopen) 431 { 432 struct netent_data *ned; 433 434 if ((ned = __netent_data_init()) == NULL) 435 return; 436 _setnethtent(stayopen, ned); 437 _setnetdnsent(stayopen); 438 } 439 440 void 441 endnetent(void) 442 { 443 struct netent_data *ned; 444 445 if ((ned = __netent_data_init()) == NULL) 446 return; 447 _endnethtent(ned); 448 _endnetdnsent(); 449 } 450