1 /* $NetBSD: getrpcent.c,v 1.17 2000/01/22 22:19:17 mycroft Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid = "@(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * Copyright (c) 1984 by Sun Microsystems, Inc.
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <arpa/inet.h>
49 #include <netinet/in.h>
56 #include <rpcsvc/yp_prot.h>
57 #include <rpcsvc/ypclnt.h>
60 #include "namespace.h"
61 #include "reentrant.h"
62 #include "un-namespace.h"
63 #include "libc_private.h"
69 #define RPCDB "/etc/rpc"
71 /* nsswitch declarations */
76 RPCENT_STORAGE_INITIAL = 1 << 10, /* 1 KByte */
77 RPCENT_STORAGE_MAX = 1 << 20, /* 1 MByte */
80 static const ns_src defaultsrc[] = {
81 { NSSRC_FILES, NS_SUCCESS },
83 { NSSRC_NIS, NS_SUCCESS },
88 /* files backend declarations */
94 static int files_rpcent(void *, void *, va_list);
95 static int files_setrpcent(void *, void *, va_list);
97 static void files_endstate(void *);
98 NSS_TLS_HANDLING(files);
100 /* nis backend declarations */
103 char domain[MAXHOSTNAMELEN];
110 static int nis_rpcent(void *, void *, va_list);
111 static int nis_setrpcent(void *, void *, va_list);
113 static void nis_endstate(void *);
114 NSS_TLS_HANDLING(nis);
117 /* get** wrappers for get**_r functions declarations */
118 struct rpcent_state {
123 static void rpcent_endstate(void *);
124 NSS_TLS_HANDLING(rpcent);
131 static int wrap_getrpcbyname_r(union key, struct rpcent *, char *,
132 size_t, struct rpcent **);
133 static int wrap_getrpcbynumber_r(union key, struct rpcent *, char *,
134 size_t, struct rpcent **);
135 static int wrap_getrpcent_r(union key, struct rpcent *, char *,
136 size_t, struct rpcent **);
137 static struct rpcent *getrpc(int (*fn)(union key, struct rpcent *, char *,
138 size_t, struct rpcent **), union key);
141 static int rpc_id_func(char *, size_t *, va_list, void *);
142 static int rpc_marshal_func(char *, size_t *, void *, va_list, void *);
143 static int rpc_unmarshal_func(char *, size_t, void *, va_list, void *);
147 rpcent_unpack(char *p, struct rpcent *rpc, char **r_aliases,
148 size_t aliases_size, int *errnop)
156 cp = strpbrk(p, "#\n");
160 cp = strpbrk(p, " \t");
164 /* THIS STUFF IS INTERNET SPECIFIC */
166 while (*cp == ' ' || *cp == '\t')
168 rpc->r_number = atoi(cp);
169 q = rpc->r_aliases = r_aliases;
170 cp = strpbrk(cp, " \t");
174 if (*cp == ' ' || *cp == '\t') {
178 if (q < &(r_aliases[aliases_size - 1]))
185 cp = strpbrk(cp, " \t");
193 /* files backend implementation */
195 files_endstate(void *p)
202 f = ((struct files_state *)p)->fp;
210 files_rpcent(void *retval, void *mdata, va_list ap)
225 struct files_state *st;
228 enum nss_lookup_type how;
230 how = (enum nss_lookup_type)mdata;
234 name = va_arg(ap, char *);
237 number = va_arg(ap, int);
242 return (NS_NOTFOUND);
245 rpc = va_arg(ap, struct rpcent *);
246 buffer = va_arg(ap, char *);
247 bufsize = va_arg(ap, size_t);
248 errnop = va_arg(ap, int *);
250 *errnop = files_getstate(&st);
254 if (st->fp == NULL && (st->fp = fopen(RPCDB, "r")) == NULL) {
259 if (how == nss_lt_all)
263 stayopen = st->stayopen;
267 if ((line = fgetln(st->fp, &linesize)) == NULL) {
273 if (bufsize <= linesize + _ALIGNBYTES + sizeof(char *)) {
279 aliases = (char **)_ALIGN(&buffer[linesize+1]);
280 aliases_size = (buffer + bufsize -
281 (char *)aliases)/sizeof(char *);
282 if (aliases_size < 1) {
288 memcpy(buffer, line, linesize);
289 buffer[linesize] = '\0';
291 rv = rpcent_unpack(buffer, rpc, aliases, aliases_size, errnop);
306 if (strcmp(rpc->r_name, name) == 0)
308 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
309 if (strcmp(*rp, name) == 0)
318 rv = (rpc->r_number == number) ? NS_SUCCESS :
326 } while (!(rv & NS_TERMINATE));
328 if (!stayopen && st->fp!=NULL) {
333 if ((rv == NS_SUCCESS) && (retval != NULL))
334 *((struct rpcent **)retval) = rpc;
340 files_setrpcent(void *retval, void *mdata, va_list ap)
342 struct files_state *st;
346 rv = files_getstate(&st);
350 switch ((enum constants)mdata)
355 st->fp = fopen(RPCDB, "r");
361 if (st->fp != NULL) {
374 /* nis backend implementation */
377 nis_endstate(void *p)
382 free(((struct nis_state *)p)->current);
387 nis_rpcent(void *retval, void *mdata, va_list ap)
403 char buf[YPMAXRECORD + 2];
405 struct nis_state *st;
407 enum nss_lookup_type how;
410 how = (enum nss_lookup_type)mdata;
414 name = va_arg(ap, char *);
417 number = va_arg(ap, int);
422 return (NS_NOTFOUND);
425 rpc = va_arg(ap, struct rpcent *);
426 buffer = va_arg(ap, char *);
427 bufsize = va_arg(ap, size_t);
428 errnop = va_arg(ap, int *);
430 *errnop = nis_getstate(&st);
434 if (st->domain[0] == '\0') {
435 if (getdomainname(st->domain, sizeof(st->domain)) != 0) {
446 if (!st->no_name_map)
448 snprintf(buf, sizeof buf, "%s", name);
449 rv = yp_match(st->domain, "rpc.byname", buf,
450 strlen(buf), &resultbuf, &resultbuflen);
476 snprintf(buf, sizeof buf, "%d", number);
477 if (yp_match(st->domain, "rpc.bynumber", buf,
478 strlen(buf), &resultbuf, &resultbuflen)) {
485 rv = yp_first(st->domain, "rpc.bynumber",
487 &st->currentlen, &resultbuf,
495 lastkey = st->current;
496 rv = yp_next(st->domain, "rpc.bynumber",
498 st->currentlen, &st->current,
500 &resultbuf, &resultbuflen);
511 /* we need a room for additional \n symbol */
512 if (bufsize <= resultbuflen + 1 + _ALIGNBYTES +
520 aliases=(char **)_ALIGN(&buffer[resultbuflen+2]);
521 aliases_size = (buffer + bufsize - (char *)aliases) /
523 if (aliases_size < 1) {
531 * rpcent_unpack expects lines terminated with \n -- make it happy
533 memcpy(buffer, resultbuf, resultbuflen);
534 buffer[resultbuflen] = '\n';
535 buffer[resultbuflen+1] = '\0';
538 if (rpcent_unpack(buffer, rpc, aliases, aliases_size,
545 if ((how == nss_lt_all) && (no_name_active != 0)) {
546 if (strcmp(rpc->r_name, name) == 0)
548 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
549 if (strcmp(*rp, name) == 0)
560 } while (!(rv & NS_TERMINATE) && (how == nss_lt_all));
563 if ((rv == NS_SUCCESS) && (retval != NULL))
564 *((struct rpcent **)retval) = rpc;
570 nis_setrpcent(void *retval, void *mdata, va_list ap)
572 struct nis_state *st;
575 rv = nis_getstate(&st);
579 switch ((enum constants)mdata)
597 rpc_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata)
602 size_t desired_size, size;
603 enum nss_lookup_type lookup_type;
604 int res = NS_UNAVAIL;
606 lookup_type = (enum nss_lookup_type)cache_mdata;
607 switch (lookup_type) {
609 name = va_arg(ap, char *);
612 desired_size = sizeof(enum nss_lookup_type) + size + 1;
613 if (desired_size > *buffer_size) {
618 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
619 memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
624 rpc = va_arg(ap, int);
626 desired_size = sizeof(enum nss_lookup_type) + sizeof(int);
627 if (desired_size > *buffer_size) {
632 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
633 memcpy(buffer + sizeof(enum nss_lookup_type), &rpc,
639 /* should be unreachable */
644 *buffer_size = desired_size;
649 rpc_marshal_func(char *buffer, size_t *buffer_size, void *retval, va_list ap,
656 size_t orig_buf_size;
658 struct rpcent new_rpc;
659 size_t desired_size, size, aliases_size;
663 switch ((enum nss_lookup_type)cache_mdata) {
665 name = va_arg(ap, char *);
668 num = va_arg(ap, int);
673 /* should be unreachable */
677 rpc = va_arg(ap, struct rpcent *);
678 orig_buf = va_arg(ap, char *);
679 orig_buf_size = va_arg(ap, size_t);
681 desired_size = _ALIGNBYTES + sizeof(struct rpcent) + sizeof(char *);
682 if (rpc->r_name != NULL)
683 desired_size += strlen(rpc->r_name) + 1;
685 if (rpc->r_aliases != NULL) {
687 for (alias = rpc->r_aliases; *alias; ++alias) {
688 desired_size += strlen(*alias) + 1;
692 desired_size += _ALIGNBYTES + (aliases_size + 1) *
696 if (*buffer_size < desired_size) {
697 /* this assignment is here for future use */
698 *buffer_size = desired_size;
704 *buffer_size = desired_size;
705 memset(buffer, 0, desired_size);
706 p = buffer + sizeof(struct rpcent) + sizeof(char *);
707 memcpy(buffer + sizeof(struct rpcent), &p, sizeof(char *));
708 p = (char *)_ALIGN(p);
710 if (new_rpc.r_name != NULL) {
711 size = strlen(new_rpc.r_name);
712 memcpy(p, new_rpc.r_name, size);
717 if (new_rpc.r_aliases != NULL) {
718 p = (char *)_ALIGN(p);
719 memcpy(p, new_rpc.r_aliases, sizeof(char *) * aliases_size);
720 new_rpc.r_aliases = (char **)p;
721 p += sizeof(char *) * (aliases_size + 1);
723 for (alias = new_rpc.r_aliases; *alias; ++alias) {
724 size = strlen(*alias);
725 memcpy(p, *alias, size);
731 memcpy(buffer, &new_rpc, sizeof(struct rpcent));
736 rpc_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
743 size_t orig_buf_size;
749 switch ((enum nss_lookup_type)cache_mdata) {
751 name = va_arg(ap, char *);
754 num = va_arg(ap, int);
759 /* should be unreachable */
763 rpc = va_arg(ap, struct rpcent *);
764 orig_buf = va_arg(ap, char *);
765 orig_buf_size = va_arg(ap, size_t);
766 ret_errno = va_arg(ap, int *);
769 buffer_size - sizeof(struct rpcent) - sizeof(char *)) {
774 memcpy(rpc, buffer, sizeof(struct rpcent));
775 memcpy(&p, buffer + sizeof(struct rpcent), sizeof(char *));
777 orig_buf = (char *)_ALIGN(orig_buf);
778 memcpy(orig_buf, buffer + sizeof(struct rpcent) + sizeof(char *) +
779 _ALIGN(p) - (size_t)p,
780 buffer_size - sizeof(struct rpcent) - sizeof(char *) -
781 _ALIGN(p) + (size_t)p);
782 p = (char *)_ALIGN(p);
784 NS_APPLY_OFFSET(rpc->r_name, orig_buf, p, char *);
785 if (rpc->r_aliases != NULL) {
786 NS_APPLY_OFFSET(rpc->r_aliases, orig_buf, p, char **);
788 for (alias = rpc->r_aliases ; *alias; ++alias)
789 NS_APPLY_OFFSET(*alias, orig_buf, p, char *);
793 *((struct rpcent **)retval) = rpc;
798 NSS_MP_CACHE_HANDLING(rpc);
799 #endif /* NS_CACHING */
802 /* get**_r functions implementation */
804 getrpcbyname_r(const char *name, struct rpcent *rpc, char *buffer,
805 size_t bufsize, struct rpcent **result)
808 static const nss_cache_info cache_info =
809 NS_COMMON_CACHE_INFO_INITIALIZER(
810 rpc, (void *)nss_lt_name,
811 rpc_id_func, rpc_marshal_func, rpc_unmarshal_func);
813 static const ns_dtab dtab[] = {
814 { NSSRC_FILES, files_rpcent, (void *)nss_lt_name },
816 { NSSRC_NIS, nis_rpcent, (void *)nss_lt_name },
819 NS_CACHE_CB(&cache_info)
827 rv = nsdispatch(result, dtab, NSDB_RPC, "getrpcbyname_r", defaultsrc,
828 name, rpc, buffer, bufsize, &ret_errno);
830 if (rv == NS_SUCCESS)
837 getrpcbynumber_r(int number, struct rpcent *rpc, char *buffer,
838 size_t bufsize, struct rpcent **result)
841 static const nss_cache_info cache_info =
842 NS_COMMON_CACHE_INFO_INITIALIZER(
843 rpc, (void *)nss_lt_id,
844 rpc_id_func, rpc_marshal_func, rpc_unmarshal_func);
846 static const ns_dtab dtab[] = {
847 { NSSRC_FILES, files_rpcent, (void *)nss_lt_id },
849 { NSSRC_NIS, nis_rpcent, (void *)nss_lt_id },
852 NS_CACHE_CB(&cache_info)
860 rv = nsdispatch(result, dtab, NSDB_RPC, "getrpcbynumber_r", defaultsrc,
861 number, rpc, buffer, bufsize, &ret_errno);
863 if (rv == NS_SUCCESS)
870 getrpcent_r(struct rpcent *rpc, char *buffer, size_t bufsize,
871 struct rpcent **result)
874 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
875 rpc, (void *)nss_lt_all,
876 rpc_marshal_func, rpc_unmarshal_func);
878 static const ns_dtab dtab[] = {
879 { NSSRC_FILES, files_rpcent, (void *)nss_lt_all },
881 { NSSRC_NIS, nis_rpcent, (void *)nss_lt_all },
884 NS_CACHE_CB(&cache_info)
892 rv = nsdispatch(result, dtab, NSDB_RPC, "getrpcent_r", defaultsrc,
893 rpc, buffer, bufsize, &ret_errno);
895 if (rv == NS_SUCCESS)
901 /* get** wrappers for get**_r functions implementation */
903 rpcent_endstate(void *p)
908 free(((struct rpcent_state *)p)->buffer);
913 wrap_getrpcbyname_r(union key key, struct rpcent *rpc, char *buffer,
914 size_t bufsize, struct rpcent **res)
916 return (getrpcbyname_r(key.name, rpc, buffer, bufsize, res));
920 wrap_getrpcbynumber_r(union key key, struct rpcent *rpc, char *buffer,
921 size_t bufsize, struct rpcent **res)
923 return (getrpcbynumber_r(key.number, rpc, buffer, bufsize, res));
927 wrap_getrpcent_r(union key key __unused, struct rpcent *rpc, char *buffer,
928 size_t bufsize, struct rpcent **res)
930 return (getrpcent_r(rpc, buffer, bufsize, res));
933 static struct rpcent *
934 getrpc(int (*fn)(union key, struct rpcent *, char *, size_t, struct rpcent **),
939 struct rpcent_state * st;
941 rv=rpcent_getstate(&st);
947 if (st->buffer == NULL) {
948 st->buffer = malloc(RPCENT_STORAGE_INITIAL);
949 if (st->buffer == NULL)
951 st->bufsize = RPCENT_STORAGE_INITIAL;
954 rv = fn(key, &st->rpc, st->buffer, st->bufsize, &res);
955 if (res == NULL && rv == ERANGE) {
957 if ((st->bufsize << 1) > RPCENT_STORAGE_MAX) {
963 st->buffer = malloc(st->bufsize);
964 if (st->buffer == NULL)
967 } while (res == NULL && rv == ERANGE);
975 getrpcbyname(const char *name)
981 return (getrpc(wrap_getrpcbyname_r, key));
985 getrpcbynumber(int number)
991 return (getrpc(wrap_getrpcbynumber_r, key));
999 key.number = 0; /* not used */
1001 return (getrpc(wrap_getrpcent_r, key));
1005 setrpcent(int stayopen)
1008 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
1009 rpc, (void *)nss_lt_all,
1013 static const ns_dtab dtab[] = {
1014 { NSSRC_FILES, files_setrpcent, (void *)SETRPCENT },
1016 { NSSRC_NIS, nis_setrpcent, (void *)SETRPCENT },
1019 NS_CACHE_CB(&cache_info)
1021 { NULL, NULL, NULL }
1024 (void)nsdispatch(NULL, dtab, NSDB_RPC, "setrpcent", defaultsrc,
1032 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
1033 rpc, (void *)nss_lt_all,
1037 static const ns_dtab dtab[] = {
1038 { NSSRC_FILES, files_setrpcent, (void *)ENDRPCENT },
1040 { NSSRC_NIS, nis_setrpcent, (void *)ENDRPCENT },
1043 NS_CACHE_CB(&cache_info)
1045 { NULL, NULL, NULL }
1048 (void)nsdispatch(NULL, dtab, NSDB_RPC, "endrpcent", defaultsrc);