2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 6/4/93";
34 #endif /* LIBC_SCCS and not lint */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/socket.h>
47 #include "namespace.h"
48 #include "reentrant.h"
49 #include "un-namespace.h"
50 #include "netdb_private.h"
56 static const ns_src defaultsrc[] = {
57 { NSSRC_FILES, NS_SUCCESS },
61 NETDB_THREAD_ALLOC(protoent_data)
62 NETDB_THREAD_ALLOC(protodata)
65 protoent_data_clear(struct protoent_data *ped)
74 protoent_data_free(void *ptr)
76 struct protoent_data *ped = ptr;
78 protoent_data_clear(ped);
83 protodata_free(void *ptr)
90 __proto_id_func(char *buffer, size_t *buffer_size, va_list ap,
96 size_t desired_size, size;
97 enum nss_lookup_type lookup_type;
100 lookup_type = (enum nss_lookup_type)(uintptr_t)cache_mdata;
101 switch (lookup_type) {
103 name = va_arg(ap, char *);
106 desired_size = sizeof(enum nss_lookup_type) + size + 1;
107 if (desired_size > *buffer_size) {
112 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
113 memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
118 proto = va_arg(ap, int);
120 desired_size = sizeof(enum nss_lookup_type) + sizeof(int);
121 if (desired_size > *buffer_size) {
126 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
127 memcpy(buffer + sizeof(enum nss_lookup_type), &proto,
133 /* should be unreachable */
138 *buffer_size = desired_size;
144 __proto_marshal_func(char *buffer, size_t *buffer_size, void *retval,
145 va_list ap, void *cache_mdata)
149 struct protoent *proto;
151 size_t orig_buf_size;
153 struct protoent new_proto;
154 size_t desired_size, size, aliases_size;
158 switch ((enum nss_lookup_type)(uintptr_t)cache_mdata) {
160 name = va_arg(ap, char *);
163 num = va_arg(ap, int);
168 /* should be unreachable */
172 proto = va_arg(ap, struct protoent *);
173 orig_buf = va_arg(ap, char *);
174 orig_buf_size = va_arg(ap, size_t);
176 desired_size = _ALIGNBYTES + sizeof(struct protoent) + sizeof(char *);
177 if (proto->p_name != NULL)
178 desired_size += strlen(proto->p_name) + 1;
180 if (proto->p_aliases != NULL) {
182 for (alias = proto->p_aliases; *alias; ++alias) {
183 desired_size += strlen(*alias) + 1;
187 desired_size += _ALIGNBYTES + (aliases_size + 1) *
191 if (*buffer_size < desired_size) {
192 /* this assignment is here for future use */
193 *buffer_size = desired_size;
197 memcpy(&new_proto, proto, sizeof(struct protoent));
199 *buffer_size = desired_size;
200 memset(buffer, 0, desired_size);
201 p = buffer + sizeof(struct protoent) + sizeof(char *);
202 memcpy(buffer + sizeof(struct protoent), &p, sizeof(char *));
203 p = (char *)_ALIGN(p);
205 if (new_proto.p_name != NULL) {
206 size = strlen(new_proto.p_name);
207 memcpy(p, new_proto.p_name, size);
208 new_proto.p_name = p;
212 if (new_proto.p_aliases != NULL) {
213 p = (char *)_ALIGN(p);
214 memcpy(p, new_proto.p_aliases, sizeof(char *) * aliases_size);
215 new_proto.p_aliases = (char **)p;
216 p += sizeof(char *) * (aliases_size + 1);
218 for (alias = new_proto.p_aliases; *alias; ++alias) {
219 size = strlen(*alias);
220 memcpy(p, *alias, size);
226 memcpy(buffer, &new_proto, sizeof(struct protoent));
231 __proto_unmarshal_func(char *buffer, size_t buffer_size, void *retval,
232 va_list ap, void *cache_mdata)
236 struct protoent *proto;
238 size_t orig_buf_size;
244 switch ((enum nss_lookup_type)(uintptr_t)cache_mdata) {
246 name = va_arg(ap, char *);
249 num = va_arg(ap, int);
254 /* should be unreachable */
258 proto = va_arg(ap, struct protoent *);
259 orig_buf = va_arg(ap, char *);
260 orig_buf_size = va_arg(ap, size_t);
261 ret_errno = va_arg(ap, int *);
264 buffer_size - sizeof(struct protoent) - sizeof(char *)) {
269 memcpy(proto, buffer, sizeof(struct protoent));
270 memcpy(&p, buffer + sizeof(struct protoent), sizeof(char *));
272 orig_buf = (char *)_ALIGN(orig_buf);
273 memcpy(orig_buf, buffer + sizeof(struct protoent) + sizeof(char *) +
274 _ALIGN(p) - (size_t)p,
275 buffer_size - sizeof(struct protoent) - sizeof(char *) -
276 _ALIGN(p) + (size_t)p);
277 p = (char *)_ALIGN(p);
279 NS_APPLY_OFFSET(proto->p_name, orig_buf, p, char *);
280 if (proto->p_aliases != NULL) {
281 NS_APPLY_OFFSET(proto->p_aliases, orig_buf, p, char **);
283 for (alias = proto->p_aliases; *alias; ++alias)
284 NS_APPLY_OFFSET(*alias, orig_buf, p, char *);
288 *((struct protoent **)retval) = proto;
293 NSS_MP_CACHE_HANDLING(protocols);
294 #endif /* NS_CACHING */
297 __copy_protoent(struct protoent *pe, struct protoent *pptr, char *buf,
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; pe->p_aliases[i]; i++, numptr++) {
308 len += strlen(pe->p_aliases[i]) + 1;
310 len += strlen(pe->p_name) + 1;
311 len += numptr * sizeof(char*);
313 if (len > (int)buflen) {
318 /* copy protocol value*/
319 pptr->p_proto = pe->p_proto;
321 cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
323 /* copy official name */
324 n = strlen(pe->p_name) + 1;
325 strcpy(cp, pe->p_name);
330 pptr->p_aliases = (char **)ALIGN(buf);
331 for (i = 0 ; pe->p_aliases[i]; i++) {
332 n = strlen(pe->p_aliases[i]) + 1;
333 strcpy(cp, pe->p_aliases[i]);
334 pptr->p_aliases[i] = cp;
337 pptr->p_aliases[i] = NULL;
343 __setprotoent_p(int f, struct protoent_data *ped)
346 ped->fp = fopen(_PATH_PROTOCOLS, "re");
353 __endprotoent_p(struct protoent_data *ped)
363 __getprotoent_p(struct protoent *pe, struct protoent_data *ped)
366 char *cp, **q, *endp;
369 if (ped->fp == NULL && (ped->fp = fopen(_PATH_PROTOCOLS, "re")) == NULL)
372 if ((p = fgets(ped->line, sizeof ped->line, ped->fp)) == NULL)
376 cp = strpbrk(p, "#\n");
380 cp = strpbrk(p, " \t");
384 while (*cp == ' ' || *cp == '\t')
386 p = strpbrk(cp, " \t");
389 l = strtol(cp, &endp, 10);
390 if (endp == cp || *endp != '\0' || l < 0 || l > USHRT_MAX)
393 q = pe->p_aliases = ped->aliases;
397 if (*cp == ' ' || *cp == '\t') {
401 if (q < &ped->aliases[_MAXALIASES - 1])
403 cp = strpbrk(cp, " \t");
413 files_getprotoent_r(void *retval, void *mdata, va_list ap)
416 struct protoent_data *ped;
418 struct protoent *pptr;
423 pptr = va_arg(ap, struct protoent *);
424 buffer = va_arg(ap, char *);
425 buflen = va_arg(ap, size_t);
426 errnop = va_arg(ap, int *);
428 if ((ped = __protoent_data_init()) == NULL) {
430 return (NS_NOTFOUND);
433 if (__getprotoent_p(&pe, ped) != 0) {
435 return (NS_NOTFOUND);
438 if (__copy_protoent(&pe, pptr, buffer, buflen) != 0) {
443 *((struct protoent **)retval) = pptr;
448 files_setprotoent(void *retval, void *mdata, va_list ap)
450 struct protoent_data *ped;
454 if ((ped = __protoent_data_init()) == NULL)
457 __setprotoent_p(f, ped);
462 files_endprotoent(void *retval, void *mdata, va_list ap)
464 struct protoent_data *ped;
466 if ((ped = __protoent_data_init()) == NULL)
469 __endprotoent_p(ped);
474 getprotoent_r(struct protoent *pptr, char *buffer, size_t buflen,
475 struct protoent **result)
478 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
479 protocols, (void *)nss_lt_all,
480 __proto_marshal_func, __proto_unmarshal_func);
482 static const ns_dtab dtab[] = {
483 { NSSRC_FILES, files_getprotoent_r, (void *)nss_lt_all },
485 NS_CACHE_CB(&cache_info)
493 rv = nsdispatch(result, dtab, NSDB_PROTOCOLS, "getprotoent_r",
494 defaultsrc, pptr, buffer, buflen, &ret_errno);
496 if (rv != NS_SUCCESS) {
504 setprotoent(int stayopen)
507 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
508 protocols, (void *)nss_lt_all,
512 static const ns_dtab dtab[] = {
513 { NSSRC_FILES, files_setprotoent, NULL },
515 NS_CACHE_CB(&cache_info)
520 (void)nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "setprotoent", defaultsrc,
528 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
529 protocols, (void *)nss_lt_all,
533 static const ns_dtab dtab[] = {
534 { NSSRC_FILES, files_endprotoent, NULL },
536 NS_CACHE_CB(&cache_info)
541 (void)nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "endprotoent", defaultsrc);
547 struct protodata *pd;
548 struct protoent *rval;
550 if ((pd = __protodata_init()) == NULL)
552 if (getprotoent_r(&pd->proto, pd->data, sizeof(pd->data), &rval) != 0)