getprotoent.c revision 158115
1199989Srdivacky/* 2199989Srdivacky * Copyright (c) 1983, 1993 3199989Srdivacky * The Regents of the University of California. All rights reserved. 4199989Srdivacky * 5199989Srdivacky * Redistribution and use in source and binary forms, with or without 6199989Srdivacky * modification, are permitted provided that the following conditions 7199989Srdivacky * are met: 8199989Srdivacky * 1. Redistributions of source code must retain the above copyright 9199989Srdivacky * notice, this list of conditions and the following disclaimer. 10199989Srdivacky * 2. Redistributions in binary form must reproduce the above copyright 11201360Srdivacky * notice, this list of conditions and the following disclaimer in the 12201360Srdivacky * documentation and/or other materials provided with the distribution. 13226633Sdim * 3. All advertising materials mentioning features or use of this software 14226633Sdim * must display the following acknowledgement: 15239462Sdim * This product includes software developed by the University of 16206124Srdivacky * California, Berkeley and its contributors. 17199989Srdivacky * 4. Neither the name of the University nor the names of its contributors 18201360Srdivacky * may be used to endorse or promote products derived from this software 19199989Srdivacky * without specific prior written permission. 20201360Srdivacky * 21201360Srdivacky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22201360Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23201360Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24201360Srdivacky * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25201360Srdivacky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26201360Srdivacky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27201360Srdivacky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28201360Srdivacky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29201360Srdivacky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30201360Srdivacky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31201360Srdivacky * SUCH DAMAGE. 32201360Srdivacky */ 33201360Srdivacky 34201360Srdivacky#if defined(LIBC_SCCS) && !defined(lint) 35201360Srdivackystatic char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 6/4/93"; 36201360Srdivacky#endif /* LIBC_SCCS and not lint */ 37201360Srdivacky#include <sys/cdefs.h> 38201360Srdivacky__FBSDID("$FreeBSD: head/lib/libc/net/getprotoent.c 158115 2006-04-28 12:03:38Z ume $"); 39201360Srdivacky 40201360Srdivacky#include <sys/param.h> 41201360Srdivacky#include <sys/types.h> 42221345Sdim#include <sys/socket.h> 43221345Sdim#include <errno.h> 44221345Sdim#include <limits.h> 45221345Sdim#include <netdb.h> 46221345Sdim#include <nsswitch.h> 47221345Sdim#include <stdio.h> 48201360Srdivacky#include <stdlib.h> 49201360Srdivacky#include <string.h> 50201360Srdivacky#include "namespace.h" 51201360Srdivacky#include "reentrant.h" 52201360Srdivacky#include "un-namespace.h" 53201360Srdivacky#include "netdb_private.h" 54201360Srdivacky#ifdef NS_CACHING 55201360Srdivacky#include "nscache.h" 56201360Srdivacky#endif 57201360Srdivacky#include "nss_tls.h" 58201360Srdivacky 59201360Srdivackystatic const ns_src defaultsrc[] = { 60201360Srdivacky { NSSRC_FILES, NS_SUCCESS }, 61201360Srdivacky { NULL, 0 } 62201360Srdivacky}; 63201360Srdivacky 64201360SrdivackyNETDB_THREAD_ALLOC(protoent_data) 65201360SrdivackyNETDB_THREAD_ALLOC(protodata) 66201360Srdivacky 67201360Srdivackystatic void 68201360Srdivackyprotoent_data_clear(struct protoent_data *ped) 69201360Srdivacky{ 70201360Srdivacky if (ped->fp) { 71201360Srdivacky fclose(ped->fp); 72201360Srdivacky ped->fp = NULL; 73201360Srdivacky } 74201360Srdivacky} 75201360Srdivacky 76201360Srdivackystatic void 77201360Srdivackyprotoent_data_free(void *ptr) 78201360Srdivacky{ 79201360Srdivacky struct protoent_data *ped = ptr; 80201360Srdivacky 81201360Srdivacky protoent_data_clear(ped); 82201360Srdivacky free(ped); 83201360Srdivacky} 84201360Srdivacky 85201360Srdivackystatic void 86201360Srdivackyprotodata_free(void *ptr) 87201360Srdivacky{ 88201360Srdivacky free(ptr); 89201360Srdivacky} 90201360Srdivacky 91201360Srdivacky#ifdef NS_CACHING 92201360Srdivackyint 93201360Srdivacky__proto_id_func(char *buffer, size_t *buffer_size, va_list ap, 94201360Srdivacky void *cache_mdata) 95201360Srdivacky{ 96239462Sdim char *name; 97239462Sdim int proto; 98239462Sdim 99239462Sdim size_t desired_size, size; 100239462Sdim enum nss_lookup_type lookup_type; 101239462Sdim int res = NS_UNAVAIL; 102239462Sdim 103239462Sdim lookup_type = (enum nss_lookup_type)cache_mdata; 104239462Sdim switch (lookup_type) { 105239462Sdim case nss_lt_name: 106239462Sdim name = va_arg(ap, char *); 107218893Sdim 108239462Sdim size = strlen(name); 109199989Srdivacky desired_size = sizeof(enum nss_lookup_type) + size + 1; 110201360Srdivacky if (desired_size > *buffer_size) { 111201360Srdivacky res = NS_RETURN; 112201360Srdivacky goto fin; 113239462Sdim } 114205407Srdivacky 115205407Srdivacky memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type)); 116239462Sdim memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1); 117201360Srdivacky 118201360Srdivacky res = NS_SUCCESS; 119201360Srdivacky break; 120201360Srdivacky case nss_lt_id: 121243830Sdim proto = va_arg(ap, int); 122243830Sdim 123201360Srdivacky desired_size = sizeof(enum nss_lookup_type) + sizeof(int); 124201360Srdivacky if (desired_size > *buffer_size) { 125201360Srdivacky res = NS_RETURN; 126201360Srdivacky goto fin; 127201360Srdivacky } 128226633Sdim 129226633Sdim memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type)); 130249423Sdim memcpy(buffer + sizeof(enum nss_lookup_type), &proto, 131249423Sdim sizeof(int)); 132249423Sdim 133239462Sdim res = NS_SUCCESS; 134239462Sdim break; 135239462Sdim default: 136239462Sdim /* should be unreachable */ 137206124Srdivacky return (NS_UNAVAIL); 138226633Sdim } 139206124Srdivacky 140239462Sdimfin: 141239462Sdim *buffer_size = desired_size; 142239462Sdim return (res); 143239462Sdim} 144199989Srdivacky 145239462Sdim 146239462Sdimint 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 233int 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 296NSS_MP_CACHE_HANDLING(protocols); 297#endif /* NS_CACHING */ 298 299int 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 345void 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 355void 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 365int 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); 374again: 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 415static int 416files_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 448static int 449files_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 462static int 463files_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 474int 475getprotoent_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 503void 504setprotoent(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 524void 525endprotoent(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 544struct protoent * 545getprotoent(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