1 /* $NetBSD: getrpcent.c,v 1.17 2000/01/22 22:19:17 mycroft Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *sccsid = "@(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Copyright (c) 1984 by Sun Microsystems, Inc.
41 #include <sys/param.h>
42 #include <sys/socket.h>
43 #include <arpa/inet.h>
47 #include <netinet/in.h>
54 #include <rpcsvc/yp_prot.h>
55 #include <rpcsvc/ypclnt.h>
58 #include "namespace.h"
59 #include "reentrant.h"
60 #include "un-namespace.h"
61 #include "libc_private.h"
67 #define RPCDB "/etc/rpc"
69 /* nsswitch declarations */
74 RPCENT_STORAGE_INITIAL = 1 << 10, /* 1 KByte */
75 RPCENT_STORAGE_MAX = 1 << 20, /* 1 MByte */
78 static const ns_src defaultsrc[] = {
79 { NSSRC_FILES, NS_SUCCESS },
81 { NSSRC_NIS, NS_SUCCESS },
86 /* files backend declarations */
92 static int files_rpcent(void *, void *, va_list);
93 static int files_setrpcent(void *, void *, va_list);
95 static void files_endstate(void *);
96 NSS_TLS_HANDLING(files);
98 /* nis backend declarations */
101 char domain[MAXHOSTNAMELEN];
108 static int nis_rpcent(void *, void *, va_list);
109 static int nis_setrpcent(void *, void *, va_list);
111 static void nis_endstate(void *);
112 NSS_TLS_HANDLING(nis);
115 /* get** wrappers for get**_r functions declarations */
116 struct rpcent_state {
121 static void rpcent_endstate(void *);
122 NSS_TLS_HANDLING(rpcent);
129 static int wrap_getrpcbyname_r(union key, struct rpcent *, char *,
130 size_t, struct rpcent **);
131 static int wrap_getrpcbynumber_r(union key, struct rpcent *, char *,
132 size_t, struct rpcent **);
133 static int wrap_getrpcent_r(union key, struct rpcent *, char *,
134 size_t, struct rpcent **);
135 static struct rpcent *getrpc(int (*fn)(union key, struct rpcent *, char *,
136 size_t, struct rpcent **), union key);
139 static int rpc_id_func(char *, size_t *, va_list, void *);
140 static int rpc_marshal_func(char *, size_t *, void *, va_list, void *);
141 static int rpc_unmarshal_func(char *, size_t, void *, va_list, void *);
145 rpcent_unpack(char *p, struct rpcent *rpc, char **r_aliases,
146 size_t aliases_size, int *errnop)
154 cp = strpbrk(p, "#\n");
158 cp = strpbrk(p, " \t");
162 /* THIS STUFF IS INTERNET SPECIFIC */
164 while (*cp == ' ' || *cp == '\t')
166 rpc->r_number = atoi(cp);
167 q = rpc->r_aliases = r_aliases;
168 cp = strpbrk(cp, " \t");
172 if (*cp == ' ' || *cp == '\t') {
176 if (q < &(r_aliases[aliases_size - 1]))
183 cp = strpbrk(cp, " \t");
191 /* files backend implementation */
193 files_endstate(void *p)
200 f = ((struct files_state *)p)->fp;
208 files_rpcent(void *retval, void *mdata, va_list ap)
223 struct files_state *st;
226 enum nss_lookup_type how;
228 how = (enum nss_lookup_type)mdata;
232 name = va_arg(ap, char *);
235 number = va_arg(ap, int);
240 return (NS_NOTFOUND);
243 rpc = va_arg(ap, struct rpcent *);
244 buffer = va_arg(ap, char *);
245 bufsize = va_arg(ap, size_t);
246 errnop = va_arg(ap, int *);
248 *errnop = files_getstate(&st);
252 if (st->fp == NULL && (st->fp = fopen(RPCDB, "r")) == NULL) {
257 if (how == nss_lt_all)
261 stayopen = st->stayopen;
265 if ((line = fgetln(st->fp, &linesize)) == NULL) {
271 if (bufsize <= linesize + _ALIGNBYTES + sizeof(char *)) {
277 aliases = (char **)_ALIGN(&buffer[linesize+1]);
278 aliases_size = (buffer + bufsize -
279 (char *)aliases)/sizeof(char *);
280 if (aliases_size < 1) {
286 memcpy(buffer, line, linesize);
287 buffer[linesize] = '\0';
289 rv = rpcent_unpack(buffer, rpc, aliases, aliases_size, errnop);
304 if (strcmp(rpc->r_name, name) == 0)
306 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
307 if (strcmp(*rp, name) == 0)
316 rv = (rpc->r_number == number) ? NS_SUCCESS :
324 } while (!(rv & NS_TERMINATE));
326 if (!stayopen && st->fp!=NULL) {
331 if ((rv == NS_SUCCESS) && (retval != NULL))
332 *((struct rpcent **)retval) = rpc;
338 files_setrpcent(void *retval, void *mdata, va_list ap)
340 struct files_state *st;
344 rv = files_getstate(&st);
348 switch ((enum constants)mdata)
353 st->fp = fopen(RPCDB, "r");
359 if (st->fp != NULL) {
372 /* nis backend implementation */
375 nis_endstate(void *p)
380 free(((struct nis_state *)p)->current);
385 nis_rpcent(void *retval, void *mdata, va_list ap)
401 char buf[YPMAXRECORD + 2];
403 struct nis_state *st;
405 enum nss_lookup_type how;
408 how = (enum nss_lookup_type)mdata;
412 name = va_arg(ap, char *);
415 number = va_arg(ap, int);
420 return (NS_NOTFOUND);
423 rpc = va_arg(ap, struct rpcent *);
424 buffer = va_arg(ap, char *);
425 bufsize = va_arg(ap, size_t);
426 errnop = va_arg(ap, int *);
428 *errnop = nis_getstate(&st);
432 if (st->domain[0] == '\0') {
433 if (getdomainname(st->domain, sizeof(st->domain)) != 0) {
444 if (!st->no_name_map)
446 snprintf(buf, sizeof buf, "%s", name);
447 rv = yp_match(st->domain, "rpc.byname", buf,
448 strlen(buf), &resultbuf, &resultbuflen);
474 snprintf(buf, sizeof buf, "%d", number);
475 if (yp_match(st->domain, "rpc.bynumber", buf,
476 strlen(buf), &resultbuf, &resultbuflen)) {
483 rv = yp_first(st->domain, "rpc.bynumber",
485 &st->currentlen, &resultbuf,
493 lastkey = st->current;
494 rv = yp_next(st->domain, "rpc.bynumber",
496 st->currentlen, &st->current,
498 &resultbuf, &resultbuflen);
509 /* we need a room for additional \n symbol */
510 if (bufsize <= resultbuflen + 1 + _ALIGNBYTES +
518 aliases=(char **)_ALIGN(&buffer[resultbuflen+2]);
519 aliases_size = (buffer + bufsize - (char *)aliases) /
521 if (aliases_size < 1) {
529 * rpcent_unpack expects lines terminated with \n -- make it happy
531 memcpy(buffer, resultbuf, resultbuflen);
532 buffer[resultbuflen] = '\n';
533 buffer[resultbuflen+1] = '\0';
536 if (rpcent_unpack(buffer, rpc, aliases, aliases_size,
543 if ((how == nss_lt_all) && (no_name_active != 0)) {
544 if (strcmp(rpc->r_name, name) == 0)
546 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
547 if (strcmp(*rp, name) == 0)
558 } while (!(rv & NS_TERMINATE) && (how == nss_lt_all));
561 if ((rv == NS_SUCCESS) && (retval != NULL))
562 *((struct rpcent **)retval) = rpc;
568 nis_setrpcent(void *retval, void *mdata, va_list ap)
570 struct nis_state *st;
573 rv = nis_getstate(&st);
577 switch ((enum constants)mdata)
595 rpc_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata)
600 size_t desired_size, size;
601 enum nss_lookup_type lookup_type;
602 int res = NS_UNAVAIL;
604 lookup_type = (enum nss_lookup_type)cache_mdata;
605 switch (lookup_type) {
607 name = va_arg(ap, char *);
610 desired_size = sizeof(enum nss_lookup_type) + size + 1;
611 if (desired_size > *buffer_size) {
616 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
617 memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
622 rpc = va_arg(ap, int);
624 desired_size = sizeof(enum nss_lookup_type) + sizeof(int);
625 if (desired_size > *buffer_size) {
630 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
631 memcpy(buffer + sizeof(enum nss_lookup_type), &rpc,
637 /* should be unreachable */
642 *buffer_size = desired_size;
647 rpc_marshal_func(char *buffer, size_t *buffer_size, void *retval, va_list ap,
654 size_t orig_buf_size;
656 struct rpcent new_rpc;
657 size_t desired_size, size, aliases_size;
661 switch ((enum nss_lookup_type)cache_mdata) {
663 name = va_arg(ap, char *);
666 num = va_arg(ap, int);
671 /* should be unreachable */
675 rpc = va_arg(ap, struct rpcent *);
676 orig_buf = va_arg(ap, char *);
677 orig_buf_size = va_arg(ap, size_t);
679 desired_size = _ALIGNBYTES + sizeof(struct rpcent) + sizeof(char *);
680 if (rpc->r_name != NULL)
681 desired_size += strlen(rpc->r_name) + 1;
683 if (rpc->r_aliases != NULL) {
685 for (alias = rpc->r_aliases; *alias; ++alias) {
686 desired_size += strlen(*alias) + 1;
690 desired_size += _ALIGNBYTES + (aliases_size + 1) *
694 if (*buffer_size < desired_size) {
695 /* this assignment is here for future use */
696 *buffer_size = desired_size;
702 *buffer_size = desired_size;
703 memset(buffer, 0, desired_size);
704 p = buffer + sizeof(struct rpcent) + sizeof(char *);
705 memcpy(buffer + sizeof(struct rpcent), &p, sizeof(char *));
706 p = (char *)_ALIGN(p);
708 if (new_rpc.r_name != NULL) {
709 size = strlen(new_rpc.r_name);
710 memcpy(p, new_rpc.r_name, size);
715 if (new_rpc.r_aliases != NULL) {
716 p = (char *)_ALIGN(p);
717 memcpy(p, new_rpc.r_aliases, sizeof(char *) * aliases_size);
718 new_rpc.r_aliases = (char **)p;
719 p += sizeof(char *) * (aliases_size + 1);
721 for (alias = new_rpc.r_aliases; *alias; ++alias) {
722 size = strlen(*alias);
723 memcpy(p, *alias, size);
729 memcpy(buffer, &new_rpc, sizeof(struct rpcent));
734 rpc_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
741 size_t orig_buf_size;
747 switch ((enum nss_lookup_type)cache_mdata) {
749 name = va_arg(ap, char *);
752 num = va_arg(ap, int);
757 /* should be unreachable */
761 rpc = va_arg(ap, struct rpcent *);
762 orig_buf = va_arg(ap, char *);
763 orig_buf_size = va_arg(ap, size_t);
764 ret_errno = va_arg(ap, int *);
767 buffer_size - sizeof(struct rpcent) - sizeof(char *)) {
772 memcpy(rpc, buffer, sizeof(struct rpcent));
773 memcpy(&p, buffer + sizeof(struct rpcent), sizeof(char *));
775 orig_buf = (char *)_ALIGN(orig_buf);
776 memcpy(orig_buf, buffer + sizeof(struct rpcent) + sizeof(char *) +
777 _ALIGN(p) - (size_t)p,
778 buffer_size - sizeof(struct rpcent) - sizeof(char *) -
779 _ALIGN(p) + (size_t)p);
780 p = (char *)_ALIGN(p);
782 NS_APPLY_OFFSET(rpc->r_name, orig_buf, p, char *);
783 if (rpc->r_aliases != NULL) {
784 NS_APPLY_OFFSET(rpc->r_aliases, orig_buf, p, char **);
786 for (alias = rpc->r_aliases ; *alias; ++alias)
787 NS_APPLY_OFFSET(*alias, orig_buf, p, char *);
791 *((struct rpcent **)retval) = rpc;
796 NSS_MP_CACHE_HANDLING(rpc);
797 #endif /* NS_CACHING */
800 /* get**_r functions implementation */
802 getrpcbyname_r(const char *name, struct rpcent *rpc, char *buffer,
803 size_t bufsize, struct rpcent **result)
806 static const nss_cache_info cache_info =
807 NS_COMMON_CACHE_INFO_INITIALIZER(
808 rpc, (void *)nss_lt_name,
809 rpc_id_func, rpc_marshal_func, rpc_unmarshal_func);
811 static const ns_dtab dtab[] = {
812 { NSSRC_FILES, files_rpcent, (void *)nss_lt_name },
814 { NSSRC_NIS, nis_rpcent, (void *)nss_lt_name },
817 NS_CACHE_CB(&cache_info)
825 rv = nsdispatch(result, dtab, NSDB_RPC, "getrpcbyname_r", defaultsrc,
826 name, rpc, buffer, bufsize, &ret_errno);
828 if (rv == NS_SUCCESS)
835 getrpcbynumber_r(int number, struct rpcent *rpc, char *buffer,
836 size_t bufsize, struct rpcent **result)
839 static const nss_cache_info cache_info =
840 NS_COMMON_CACHE_INFO_INITIALIZER(
841 rpc, (void *)nss_lt_id,
842 rpc_id_func, rpc_marshal_func, rpc_unmarshal_func);
844 static const ns_dtab dtab[] = {
845 { NSSRC_FILES, files_rpcent, (void *)nss_lt_id },
847 { NSSRC_NIS, nis_rpcent, (void *)nss_lt_id },
850 NS_CACHE_CB(&cache_info)
858 rv = nsdispatch(result, dtab, NSDB_RPC, "getrpcbynumber_r", defaultsrc,
859 number, rpc, buffer, bufsize, &ret_errno);
861 if (rv == NS_SUCCESS)
868 getrpcent_r(struct rpcent *rpc, char *buffer, size_t bufsize,
869 struct rpcent **result)
872 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
873 rpc, (void *)nss_lt_all,
874 rpc_marshal_func, rpc_unmarshal_func);
876 static const ns_dtab dtab[] = {
877 { NSSRC_FILES, files_rpcent, (void *)nss_lt_all },
879 { NSSRC_NIS, nis_rpcent, (void *)nss_lt_all },
882 NS_CACHE_CB(&cache_info)
890 rv = nsdispatch(result, dtab, NSDB_RPC, "getrpcent_r", defaultsrc,
891 rpc, buffer, bufsize, &ret_errno);
893 if (rv == NS_SUCCESS)
899 /* get** wrappers for get**_r functions implementation */
901 rpcent_endstate(void *p)
906 free(((struct rpcent_state *)p)->buffer);
911 wrap_getrpcbyname_r(union key key, struct rpcent *rpc, char *buffer,
912 size_t bufsize, struct rpcent **res)
914 return (getrpcbyname_r(key.name, rpc, buffer, bufsize, res));
918 wrap_getrpcbynumber_r(union key key, struct rpcent *rpc, char *buffer,
919 size_t bufsize, struct rpcent **res)
921 return (getrpcbynumber_r(key.number, rpc, buffer, bufsize, res));
925 wrap_getrpcent_r(union key key __unused, struct rpcent *rpc, char *buffer,
926 size_t bufsize, struct rpcent **res)
928 return (getrpcent_r(rpc, buffer, bufsize, res));
931 static struct rpcent *
932 getrpc(int (*fn)(union key, struct rpcent *, char *, size_t, struct rpcent **),
937 struct rpcent_state * st;
939 rv=rpcent_getstate(&st);
945 if (st->buffer == NULL) {
946 st->buffer = malloc(RPCENT_STORAGE_INITIAL);
947 if (st->buffer == NULL)
949 st->bufsize = RPCENT_STORAGE_INITIAL;
952 rv = fn(key, &st->rpc, st->buffer, st->bufsize, &res);
953 if (res == NULL && rv == ERANGE) {
955 if ((st->bufsize << 1) > RPCENT_STORAGE_MAX) {
961 st->buffer = malloc(st->bufsize);
962 if (st->buffer == NULL)
965 } while (res == NULL && rv == ERANGE);
973 getrpcbyname(const char *name)
979 return (getrpc(wrap_getrpcbyname_r, key));
983 getrpcbynumber(int number)
989 return (getrpc(wrap_getrpcbynumber_r, key));
997 key.number = 0; /* not used */
999 return (getrpc(wrap_getrpcent_r, key));
1003 setrpcent(int stayopen)
1006 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
1007 rpc, (void *)nss_lt_all,
1011 static const ns_dtab dtab[] = {
1012 { NSSRC_FILES, files_setrpcent, (void *)SETRPCENT },
1014 { NSSRC_NIS, nis_setrpcent, (void *)SETRPCENT },
1017 NS_CACHE_CB(&cache_info)
1019 { NULL, NULL, NULL }
1022 (void)nsdispatch(NULL, dtab, NSDB_RPC, "setrpcent", defaultsrc,
1030 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
1031 rpc, (void *)nss_lt_all,
1035 static const ns_dtab dtab[] = {
1036 { NSSRC_FILES, files_setrpcent, (void *)ENDRPCENT },
1038 { NSSRC_NIS, nis_setrpcent, (void *)ENDRPCENT },
1041 NS_CACHE_CB(&cache_info)
1043 { NULL, NULL, NULL }
1046 (void)nsdispatch(NULL, dtab, NSDB_RPC, "endrpcent", defaultsrc);