1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #if defined(LIBC_SCCS) && !defined(lint) 35 static char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 6/4/93"; 36 #endif /* LIBC_SCCS and not lint */ 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include <sys/param.h> 41 #include <sys/types.h> 42 #include <sys/socket.h> 43 #include <errno.h> 44 #include <limits.h> 45 #include <netdb.h> 46 #include <nsswitch.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include "namespace.h" 51 #include "reentrant.h" 52 #include "un-namespace.h" 53 #include "netdb_private.h" 54 #ifdef NS_CACHING 55 #include "nscache.h" 56 #endif 57 #include "nss_tls.h" 58 59 static const ns_src defaultsrc[] = { 60 { NSSRC_FILES, NS_SUCCESS }, 61 { NULL, 0 } 62 }; 63 64 NETDB_THREAD_ALLOC(protoent_data) 65 NETDB_THREAD_ALLOC(protodata) 66 67 static void 68 protoent_data_clear(struct protoent_data *ped) 69 { 70 if (ped->fp) { 71 fclose(ped->fp); 72 ped->fp = NULL; 73 } 74 } 75 76 static void 77 protoent_data_free(void *ptr) 78 { 79 struct protoent_data *ped = ptr; 80 81 protoent_data_clear(ped); 82 free(ped); 83 } 84 85 static void 86 protodata_free(void *ptr) 87 { 88 free(ptr); 89 } 90 91 #ifdef NS_CACHING 92 int 93 __proto_id_func(char *buffer, size_t *buffer_size, va_list ap, 94 void *cache_mdata) 95 { 96 char *name; 97 int proto; 98 99 size_t desired_size, size; 100 enum nss_lookup_type lookup_type; 101 int res = NS_UNAVAIL; 102 103 lookup_type = (enum nss_lookup_type)cache_mdata; 104 switch (lookup_type) { 105 case nss_lt_name: 106 name = va_arg(ap, char *); 107 108 size = strlen(name); 109 desired_size = sizeof(enum nss_lookup_type) + size + 1; 110 if (desired_size > *buffer_size) { 111 res = NS_RETURN; 112 goto fin; 113 } 114 115 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type)); 116 memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1); 117 118 res = NS_SUCCESS; 119 break; 120 case nss_lt_id: 121 proto = va_arg(ap, int); 122 123 desired_size = sizeof(enum nss_lookup_type) + sizeof(int); 124 if (desired_size > *buffer_size) { 125 res = NS_RETURN; 126 goto fin; 127 } 128 129 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type)); 130 memcpy(buffer + sizeof(enum nss_lookup_type), &proto, 131 sizeof(int)); 132 133 res = NS_SUCCESS; 134 break; 135 default: 136 /* should be unreachable */ 137 return (NS_UNAVAIL); 138 } 139 140 fin: 141 *buffer_size = desired_size; 142 return (res); 143 } 144 145 146 int 147 __proto_marshal_func(char *buffer, size_t *buffer_size, void *retval, 148 va_list ap, void *cache_mdata) 149 { 150 char *name; 151 int num; 152 struct protoent *proto; 153 char *orig_buf; 154 size_t orig_buf_size; 155 156 struct protoent new_proto; 157 size_t desired_size, size, aliases_size; 158 char *p; 159 char **alias; 160 161 switch ((enum nss_lookup_type)cache_mdata) { 162 case nss_lt_name: 163 name = va_arg(ap, char *); 164 break; 165 case nss_lt_id: 166 num = va_arg(ap, int); 167 break; 168 case nss_lt_all: 169 break; 170 default: 171 /* should be unreachable */ 172 return (NS_UNAVAIL); 173 } 174 175 proto = va_arg(ap, struct protoent *); 176 orig_buf = va_arg(ap, char *); 177 orig_buf_size = va_arg(ap, size_t); 178 179 desired_size = _ALIGNBYTES + sizeof(struct protoent) + sizeof(char *); 180 if (proto->p_name != NULL) 181 desired_size += strlen(proto->p_name) + 1; 182 183 if (proto->p_aliases != NULL) { 184 aliases_size = 0; 185 for (alias = proto->p_aliases; *alias; ++alias) { 186 desired_size += strlen(*alias) + 1; 187 ++aliases_size; 188 } 189 190 desired_size += _ALIGNBYTES + (aliases_size + 1) * 191 sizeof(char *); 192 } 193 194 if (*buffer_size < desired_size) { 195 /* this assignment is here for future use */ 196 *buffer_size = desired_size; 197 return (NS_RETURN); 198 } 199 200 memcpy(&new_proto, proto, sizeof(struct protoent)); 201 202 *buffer_size = desired_size; 203 memset(buffer, 0, desired_size); 204 p = buffer + sizeof(struct protoent) + sizeof(char *); 205 memcpy(buffer + sizeof(struct protoent), &p, sizeof(char *)); 206 p = (char *)_ALIGN(p); 207 208 if (new_proto.p_name != NULL) { 209 size = strlen(new_proto.p_name); 210 memcpy(p, new_proto.p_name, size); 211 new_proto.p_name = p; 212 p += size + 1; 213 } 214 215 if (new_proto.p_aliases != NULL) { 216 p = (char *)_ALIGN(p); 217 memcpy(p, new_proto.p_aliases, sizeof(char *) * aliases_size); 218 new_proto.p_aliases = (char **)p; 219 p += sizeof(char *) * (aliases_size + 1); 220 221 for (alias = new_proto.p_aliases; *alias; ++alias) { 222 size = strlen(*alias); 223 memcpy(p, *alias, size); 224 *alias = p; 225 p += size + 1; 226 } 227 } 228 229 memcpy(buffer, &new_proto, sizeof(struct protoent)); 230 return (NS_SUCCESS); 231 } 232 233 int 234 __proto_unmarshal_func(char *buffer, size_t buffer_size, void *retval, 235 va_list ap, void *cache_mdata) 236 { 237 char *name; 238 int num; 239 struct protoent *proto; 240 char *orig_buf; 241 size_t orig_buf_size; 242 int *ret_errno; 243 244 char *p; 245 char **alias; 246 247 switch ((enum nss_lookup_type)cache_mdata) { 248 case nss_lt_name: 249 name = va_arg(ap, char *); 250 break; 251 case nss_lt_id: 252 num = va_arg(ap, int); 253 break; 254 case nss_lt_all: 255 break; 256 default: 257 /* should be unreachable */ 258 return (NS_UNAVAIL); 259 } 260 261 proto = va_arg(ap, struct protoent *); 262 orig_buf = va_arg(ap, char *); 263 orig_buf_size = va_arg(ap, size_t); 264 ret_errno = va_arg(ap, int *); 265 266 if (orig_buf_size < 267 buffer_size - sizeof(struct protoent) - sizeof(char *)) { 268 *ret_errno = ERANGE; 269 return (NS_RETURN); 270 } 271 272 memcpy(proto, buffer, sizeof(struct protoent)); 273 memcpy(&p, buffer + sizeof(struct protoent), sizeof(char *)); 274 275 orig_buf = (char *)_ALIGN(orig_buf); 276 memcpy(orig_buf, buffer + sizeof(struct protoent) + sizeof(char *) + 277 _ALIGN(p) - (size_t)p, 278 buffer_size - sizeof(struct protoent) - sizeof(char *) - 279 _ALIGN(p) + (size_t)p); 280 p = (char *)_ALIGN(p); 281 282 NS_APPLY_OFFSET(proto->p_name, orig_buf, p, char *); 283 if (proto->p_aliases != NULL) { 284 NS_APPLY_OFFSET(proto->p_aliases, orig_buf, p, char **); 285 286 for (alias = proto->p_aliases; *alias; ++alias) 287 NS_APPLY_OFFSET(*alias, orig_buf, p, char *); 288 } 289 290 if (retval != NULL) 291 *((struct protoent **)retval) = proto; 292 293 return (NS_SUCCESS); 294 } 295 296 NSS_MP_CACHE_HANDLING(protocols); 297 #endif /* NS_CACHING */ 298 299 int 300 __copy_protoent(struct protoent *pe, struct protoent *pptr, char *buf, 301 size_t buflen) 302 { 303 char *cp; 304 int i, n; 305 int numptr, len; 306 307 /* Find out the amount of space required to store the answer. */ 308 numptr = 1; /* NULL ptr */ 309 len = (char *)ALIGN(buf) - buf; 310 for (i = 0; pe->p_aliases[i]; i++, numptr++) { 311 len += strlen(pe->p_aliases[i]) + 1; 312 } 313 len += strlen(pe->p_name) + 1; 314 len += numptr * sizeof(char*); 315 316 if (len > (int)buflen) { 317 errno = ERANGE; 318 return (-1); 319 } 320 321 /* copy protocol value*/ 322 pptr->p_proto = pe->p_proto; 323 324 cp = (char *)ALIGN(buf) + numptr * sizeof(char *); 325 326 /* copy official name */ 327 n = strlen(pe->p_name) + 1; 328 strcpy(cp, pe->p_name); 329 pptr->p_name = cp; 330 cp += n; 331 332 /* copy aliases */ 333 pptr->p_aliases = (char **)ALIGN(buf); 334 for (i = 0 ; pe->p_aliases[i]; i++) { 335 n = strlen(pe->p_aliases[i]) + 1; 336 strcpy(cp, pe->p_aliases[i]); 337 pptr->p_aliases[i] = cp; 338 cp += n; 339 } 340 pptr->p_aliases[i] = NULL; 341 342 return (0); 343 } 344 345 void 346 __setprotoent_p(int f, struct protoent_data *ped) 347 { 348 if (ped->fp == NULL) 349 ped->fp = fopen(_PATH_PROTOCOLS, "r"); 350 else 351 rewind(ped->fp); 352 ped->stayopen |= f; 353 } 354 355 void 356 __endprotoent_p(struct protoent_data *ped) 357 { 358 if (ped->fp) { 359 fclose(ped->fp); 360 ped->fp = NULL; 361 } 362 ped->stayopen = 0; 363 } 364 365 int 366 __getprotoent_p(struct protoent *pe, struct protoent_data *ped) 367 { 368 char *p; 369 char *cp, **q, *endp; 370 long l; 371 372 if (ped->fp == NULL && (ped->fp = fopen(_PATH_PROTOCOLS, "r")) == NULL) 373 return (-1); 374 again: 375 if ((p = fgets(ped->line, sizeof ped->line, ped->fp)) == NULL) 376 return (-1); 377 if (*p == '#') 378 goto again; 379 cp = strpbrk(p, "#\n"); 380 if (cp != NULL) 381 *cp = '\0'; 382 pe->p_name = p; 383 cp = strpbrk(p, " \t"); 384 if (cp == NULL) 385 goto again; 386 *cp++ = '\0'; 387 while (*cp == ' ' || *cp == '\t') 388 cp++; 389 p = strpbrk(cp, " \t"); 390 if (p != NULL) 391 *p++ = '\0'; 392 l = strtol(cp, &endp, 10); 393 if (endp == cp || *endp != '\0' || l < 0 || l > USHRT_MAX) 394 goto again; 395 pe->p_proto = l; 396 q = pe->p_aliases = ped->aliases; 397 if (p != NULL) { 398 cp = p; 399 while (cp && *cp) { 400 if (*cp == ' ' || *cp == '\t') { 401 cp++; 402 continue; 403 } 404 if (q < &ped->aliases[_MAXALIASES - 1]) 405 *q++ = cp; 406 cp = strpbrk(cp, " \t"); 407 if (cp != NULL) 408 *cp++ = '\0'; 409 } 410 } 411 *q = NULL; 412 return (0); 413 } 414 415 static int 416 files_getprotoent_r(void *retval, void *mdata, va_list ap) 417 { 418 struct protoent pe; 419 struct protoent_data *ped; 420 421 struct protoent *pptr; 422 char *buffer; 423 size_t buflen; 424 int *errnop; 425 426 pptr = va_arg(ap, struct protoent *); 427 buffer = va_arg(ap, char *); 428 buflen = va_arg(ap, size_t); 429 errnop = va_arg(ap, int *); 430 431 if ((ped = __protoent_data_init()) == NULL) 432 return (-1); 433 434 if (__getprotoent_p(&pe, ped) != 0) { 435 *errnop = errno; 436 return (NS_NOTFOUND); 437 } 438 439 if (__copy_protoent(&pe, pptr, buffer, buflen) != 0) { 440 *errnop = errno; 441 return (NS_NOTFOUND); 442 } 443 444 *((struct protoent **)retval) = pptr; 445 return (NS_SUCCESS); 446 } 447 448 static int 449 files_setprotoent(void *retval, void *mdata, va_list ap) 450 { 451 struct protoent_data *ped; 452 int f; 453 454 f = va_arg(ap, int); 455 if ((ped = __protoent_data_init()) == NULL) 456 return (NS_UNAVAIL); 457 458 __setprotoent_p(f, ped); 459 return (NS_UNAVAIL); 460 } 461 462 static int 463 files_endprotoent(void *retval, void *mdata, va_list ap) 464 { 465 struct protoent_data *ped; 466 467 if ((ped = __protoent_data_init()) == NULL) 468 return (NS_UNAVAIL); 469 470 __endprotoent_p(ped); 471 return (NS_UNAVAIL); 472 } 473 474 int 475 getprotoent_r(struct protoent *pptr, char *buffer, size_t buflen, 476 struct protoent **result) 477 { 478 #ifdef NS_CACHING 479 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER( 480 protocols, (void *)nss_lt_all, 481 __proto_marshal_func, __proto_unmarshal_func); 482 #endif 483 static const ns_dtab dtab[] = { 484 { NSSRC_FILES, files_getprotoent_r, (void *)nss_lt_all }, 485 #ifdef NS_CACHING 486 NS_CACHE_CB(&cache_info) 487 #endif 488 { NULL, NULL, NULL } 489 }; 490 int rv, ret_errno; 491 492 ret_errno = 0; 493 *result = NULL; 494 rv = nsdispatch(result, dtab, NSDB_PROTOCOLS, "getprotoent_r", 495 defaultsrc, pptr, buffer, buflen, &ret_errno); 496 497 if (rv == NS_SUCCESS) 498 return (0); 499 else 500 return (ret_errno); 501 } 502 503 void 504 setprotoent(int stayopen) 505 { 506 #ifdef NS_CACHING 507 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER( 508 protocols, (void *)nss_lt_all, 509 NULL, NULL); 510 #endif 511 512 static const ns_dtab dtab[] = { 513 { NSSRC_FILES, files_setprotoent, NULL }, 514 #ifdef NS_CACHING 515 NS_CACHE_CB(&cache_info) 516 #endif 517 { NULL, NULL, NULL } 518 }; 519 520 (void)nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "setprotoent", defaultsrc, 521 stayopen); 522 } 523 524 void 525 endprotoent(void) 526 { 527 #ifdef NS_CACHING 528 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER( 529 protocols, (void *)nss_lt_all, 530 NULL, NULL); 531 #endif 532 533 static const ns_dtab dtab[] = { 534 { NSSRC_FILES, files_endprotoent, NULL }, 535 #ifdef NS_CACHING 536 NS_CACHE_CB(&cache_info) 537 #endif 538 { NULL, NULL, NULL } 539 }; 540 541 (void)nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "endprotoent", defaultsrc); 542 } 543 544 struct protoent * 545 getprotoent(void) 546 { 547 struct protodata *pd; 548 struct protoent *rval; 549 550 if ((pd = __protodata_init()) == NULL) 551 return (NULL); 552 if (getprotoent_r(&pd->proto, pd->data, sizeof(pd->data), &rval) != 0) 553 return (NULL); 554 return (rval); 555 } 556