xref: /openbsd-src/lib/libc/yp/yp_all.c (revision 2d62dde8310c0f11b45f14adaba36cf1950be6bf)
1*2d62dde8Sderaadt /*	$OpenBSD: yp_all.c,v 1.16 2022/08/02 16:59:29 deraadt Exp $ */
218128546Sderaadt /*
318128546Sderaadt  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
418128546Sderaadt  * All rights reserved.
518128546Sderaadt  *
618128546Sderaadt  * Redistribution and use in source and binary forms, with or without
718128546Sderaadt  * modification, are permitted provided that the following conditions
818128546Sderaadt  * are met:
918128546Sderaadt  * 1. Redistributions of source code must retain the above copyright
1018128546Sderaadt  *    notice, this list of conditions and the following disclaimer.
1118128546Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
1218128546Sderaadt  *    notice, this list of conditions and the following disclaimer in the
1318128546Sderaadt  *    documentation and/or other materials provided with the distribution.
1418128546Sderaadt  *
1518128546Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1618128546Sderaadt  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1718128546Sderaadt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1818128546Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
1918128546Sderaadt  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2018128546Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2118128546Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2218128546Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2318128546Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2418128546Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2518128546Sderaadt  * SUCH DAMAGE.
2618128546Sderaadt  */
2718128546Sderaadt 
2818128546Sderaadt #include <sys/types.h>
29125e6122Sderaadt #include <sys/socket.h>
3018128546Sderaadt #include <stdio.h>
3118128546Sderaadt #include <stdlib.h>
3218128546Sderaadt #include <string.h>
33aea60beeSderaadt #include <limits.h>
34125e6122Sderaadt #include <unistd.h>
3518128546Sderaadt #include <rpc/rpc.h>
3618128546Sderaadt #include <rpc/xdr.h>
3718128546Sderaadt #include <rpcsvc/yp.h>
3818128546Sderaadt #include <rpcsvc/ypclnt.h>
3918128546Sderaadt #include "ypinternal.h"
4018128546Sderaadt 
417db392d8Sderaadt static int (*ypresp_allfn)(u_long, char *, int, char *, int, void *);
427db392d8Sderaadt static void *ypresp_data;
437db392d8Sderaadt 
4481420c00Sderaadt static bool_t
_xdr_ypresp_all_seq(XDR * xdrs,u_long * objp)4581420c00Sderaadt _xdr_ypresp_all_seq(XDR *xdrs, u_long *objp)
4618128546Sderaadt {
4718128546Sderaadt 	struct ypresp_all out;
4818128546Sderaadt 	u_long status;
4918128546Sderaadt 	char *key, *val;
5018128546Sderaadt 	int size;
51117ce104Sschwarze 	int done = 0;  /* set to 1 when the user does not want more data */
52117ce104Sschwarze 	bool_t rc = TRUE;  /* FALSE at the end of loop signals failure */
5318128546Sderaadt 
5418128546Sderaadt 	memset(&out, 0, sizeof out);
55117ce104Sschwarze 	while (rc && !done) {
56117ce104Sschwarze 		rc = FALSE;
5718128546Sderaadt 		if (!xdr_ypresp_all(xdrs, &out)) {
58198c6cf5Stholo 			*objp = (u_long)YP_YPERR;
59117ce104Sschwarze 			goto fail;
6018128546Sderaadt 		}
61117ce104Sschwarze 		if (out.more == 0)
62117ce104Sschwarze 			goto fail;
6318128546Sderaadt 		status = out.ypresp_all_u.val.stat;
64117ce104Sschwarze 		if (status == YP_TRUE) {
6518128546Sderaadt 			size = out.ypresp_all_u.val.key.keydat_len;
66117ce104Sschwarze 			if ((key = malloc(size + 1)) == NULL) {
67117ce104Sschwarze 				*objp = (u_long)YP_YPERR;
68117ce104Sschwarze 				goto fail;
69117ce104Sschwarze 			}
70117ce104Sschwarze 			(void)memcpy(key, out.ypresp_all_u.val.key.keydat_val,
7118128546Sderaadt 			    size);
7218128546Sderaadt 			key[size] = '\0';
73117ce104Sschwarze 
7418128546Sderaadt 			size = out.ypresp_all_u.val.val.valdat_len;
75117ce104Sschwarze 			if ((val = malloc(size + 1)) == NULL) {
76117ce104Sschwarze 				free(key);
77117ce104Sschwarze 				*objp = (u_long)YP_YPERR;
78117ce104Sschwarze 				goto fail;
79117ce104Sschwarze 			}
80117ce104Sschwarze 			(void)memcpy(val, out.ypresp_all_u.val.val.valdat_val,
8118128546Sderaadt 			    size);
8218128546Sderaadt 			val[size] = '\0';
8318128546Sderaadt 
84117ce104Sschwarze 			done = (*ypresp_allfn)(status, key,
85a69cfd83Sderaadt 			    out.ypresp_all_u.val.key.keydat_len, val,
86a69cfd83Sderaadt 			    out.ypresp_all_u.val.val.valdat_len, ypresp_data);
8718128546Sderaadt 			free(key);
8818128546Sderaadt 			free(val);
89117ce104Sschwarze 		} else
90117ce104Sschwarze 			done = 1;
91117ce104Sschwarze 		if (status != YP_NOMORE)
9218128546Sderaadt 			*objp = status;
93117ce104Sschwarze 		rc = TRUE;
94117ce104Sschwarze fail:
95117ce104Sschwarze 		xdr_free(xdr_ypresp_all, (char *)&out);
9618128546Sderaadt 	}
97117ce104Sschwarze 	return rc;
9818128546Sderaadt }
9918128546Sderaadt 
10018128546Sderaadt int
yp_all(const char * dom,const char * inmap,struct ypall_callback * incallback)101125e6122Sderaadt yp_all(const char *dom, const char *inmap, struct ypall_callback *incallback)
10218128546Sderaadt {
10318128546Sderaadt 	struct ypreq_nokey yprnk;
104*2d62dde8Sderaadt 	struct dom_binding ypbinding;
10518128546Sderaadt 	struct timeval  tv;
106125e6122Sderaadt 	int connected = 1;
10718128546Sderaadt 	u_long		status;
108125e6122Sderaadt 	int		r = 0, s;
10918128546Sderaadt 
110125e6122Sderaadt 	if (dom == NULL || strlen(dom) == 0)
111125e6122Sderaadt 		return YPERR_BADARGS;
112125e6122Sderaadt 
113125e6122Sderaadt 	if (strlen(dom) > YPMAXDOMAIN || inmap == NULL ||
114a69cfd83Sderaadt 	    *inmap == '\0' || strlen(inmap) > YPMAXMAP || incallback == NULL)
115a69cfd83Sderaadt 		return YPERR_BADARGS;
116a69cfd83Sderaadt 
117125e6122Sderaadt again:
118125e6122Sderaadt 	s = ypconnect(SOCK_STREAM);
119125e6122Sderaadt 	if (s == -1)
120125e6122Sderaadt 		return YPERR_DOMAIN;	/* YP not running */
121*2d62dde8Sderaadt 	ypbinding.dom_socket = s;
122*2d62dde8Sderaadt 	ypbinding.dom_server_addr.sin_port = -1; /* don't consult portmap */
123125e6122Sderaadt 
124*2d62dde8Sderaadt 	ypbinding.dom_client = clnttcp_create(&ypbinding.dom_server_addr,
125*2d62dde8Sderaadt 	    YPPROG, YPVERS, &ypbinding.dom_socket, 0, 0);
126*2d62dde8Sderaadt 	if (ypbinding.dom_client == NULL) {
127*2d62dde8Sderaadt 		close(ypbinding.dom_socket);
128*2d62dde8Sderaadt 		ypbinding.dom_socket = -1;
1297150dbc1Sderaadt 		clnt_pcreateerror("clnttcp_create");
130125e6122Sderaadt 		goto again;
131125e6122Sderaadt 	}
132*2d62dde8Sderaadt 	clnt_control(ypbinding.dom_client, CLSET_CONNECTED, &connected);
13318128546Sderaadt 
13418128546Sderaadt 	tv.tv_sec = _yplib_timeout;
13518128546Sderaadt 	tv.tv_usec = 0;
136125e6122Sderaadt 	yprnk.domain = (char *)dom;
13718128546Sderaadt 	yprnk.map = (char *)inmap;
13818128546Sderaadt 	ypresp_allfn = incallback->foreach;
13918128546Sderaadt 	ypresp_data = (void *) incallback->data;
140*2d62dde8Sderaadt 	(void) clnt_call(ypbinding.dom_client, YPPROC_ALL,
14181420c00Sderaadt 	    xdr_ypreq_nokey, &yprnk, _xdr_ypresp_all_seq, &status, tv);
142*2d62dde8Sderaadt 	close(ypbinding.dom_socket);
143*2d62dde8Sderaadt 	clnt_destroy(ypbinding.dom_client);
144125e6122Sderaadt 
14518128546Sderaadt 	if (status != YP_FALSE)
14618128546Sderaadt 		r = ypprot_err(status);
14718128546Sderaadt 	return r;
14818128546Sderaadt }
149