1331722Seadler/*
21919Swollman * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
31919Swollman * All rights reserved.
41919Swollman *
51919Swollman * Redistribution and use in source and binary forms, with or without
61919Swollman * modification, are permitted provided that the following conditions
71919Swollman * are met:
81919Swollman * 1. Redistributions of source code must retain the above copyright
91919Swollman *    notice, this list of conditions and the following disclaimer.
101919Swollman * 2. Redistributions in binary form must reproduce the above copyright
111919Swollman *    notice, this list of conditions and the following disclaimer in the
121919Swollman *    documentation and/or other materials provided with the distribution.
131919Swollman * 3. The name of the author may not be used to endorse or promote
141919Swollman *    products derived from this software without specific prior written
151919Swollman *    permission.
161919Swollman *
171919Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
181919Swollman * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
191919Swollman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201919Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
211919Swollman * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221919Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231919Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241919Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251919Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261919Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271919Swollman * SUCH DAMAGE.
281919Swollman */
291919Swollman
3092986Sobrien#include <sys/cdefs.h>
3192986Sobrien__FBSDID("$FreeBSD$");
321919Swollman
331919Swollman#include <rpc/rpc.h>
3412816Swpaul#include <rpcsvc/yp.h>
3516095Sjraynard#include <stdlib.h>
3616095Sjraynard#include <string.h>
371919Swollman
381919Swollmanextern int (*ypresp_allfn)();
391919Swollmanextern void *ypresp_data;
401919Swollman
4112816Swpaul/*
4212816Swpaul * I'm leaving the xdr_datum() function in purely for backwards
4312816Swpaul * compatibility. yplib.c doesn't actually use it, but it's listed
4412816Swpaul * in yp_prot.h as being available, so it's probably a good idea to
45219126Sbrucec * leave it in case somebody goes looking for it.
4612816Swpaul */
4712816Swpaultypedef struct {
4812816Swpaul	char *dptr;
4912816Swpaul	int  dsize;
5012816Swpaul} datum;
511919Swollman
521919Swollmanbool_t
5390298Sdesxdr_datum(XDR *xdrs, datum *objp)
541919Swollman{
551919Swollman	if (!xdr_bytes(xdrs, (char **)&objp->dptr, (u_int *)&objp->dsize, YPMAXRECORD)) {
561919Swollman		return (FALSE);
571919Swollman	}
581919Swollman	return (TRUE);
591919Swollman}
601919Swollman
611919Swollmanbool_t
6290298Sdesxdr_ypresp_all_seq(XDR *xdrs, u_long *objp)
631919Swollman{
641919Swollman	struct ypresp_all out;
651919Swollman	u_long status;
661919Swollman	char *key, *val;
671919Swollman	int r;
681919Swollman
691919Swollman	bzero(&out, sizeof out);
7090297Sdes	while (1) {
7190297Sdes		if (!xdr_ypresp_all(xdrs, &out)) {
7295658Sdes			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
731919Swollman			*objp = YP_YPERR;
7490297Sdes			return (FALSE);
751919Swollman		}
7690297Sdes		if (out.more == 0) {
7795658Sdes			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
7813894Swpaul			*objp = YP_NOMORE;
7990297Sdes			return (TRUE);
801919Swollman		}
8112816Swpaul		status = out.ypresp_all_u.val.stat;
8290297Sdes		switch (status) {
831919Swollman		case YP_TRUE:
8412816Swpaul			key = (char *)malloc(out.ypresp_all_u.val.key.keydat_len + 1);
85228826Sghelmer			if (key == NULL) {
86228826Sghelmer				xdr_free((xdrproc_t)xdr_ypresp_all, &out);
87228826Sghelmer				*objp = YP_YPERR;
88228826Sghelmer				return (FALSE);
89228826Sghelmer			}
9012816Swpaul			bcopy(out.ypresp_all_u.val.key.keydat_val, key,
9112816Swpaul				out.ypresp_all_u.val.key.keydat_len);
9212816Swpaul			key[out.ypresp_all_u.val.key.keydat_len] = '\0';
9312816Swpaul			val = (char *)malloc(out.ypresp_all_u.val.val.valdat_len + 1);
94228826Sghelmer			if (val == NULL) {
95228826Sghelmer				free(key);
96228826Sghelmer				xdr_free((xdrproc_t)xdr_ypresp_all, &out);
97228826Sghelmer				*objp = YP_YPERR;
98228826Sghelmer				return (FALSE);
99228826Sghelmer			}
10012816Swpaul			bcopy(out.ypresp_all_u.val.val.valdat_val, val,
10112816Swpaul				out.ypresp_all_u.val.val.valdat_len);
10212816Swpaul			val[out.ypresp_all_u.val.val.valdat_len] = '\0';
10395658Sdes			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
1041919Swollman
1051919Swollman			r = (*ypresp_allfn)(status,
10612816Swpaul				key, out.ypresp_all_u.val.key.keydat_len,
10712816Swpaul				val, out.ypresp_all_u.val.val.valdat_len,
1081919Swollman				ypresp_data);
1091919Swollman			*objp = status;
1101919Swollman			free(key);
1111919Swollman			free(val);
11290297Sdes			if (r)
11390297Sdes				return (TRUE);
1141919Swollman			break;
1151919Swollman		case YP_NOMORE:
11695658Sdes			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
11713894Swpaul			*objp = YP_NOMORE;
11890297Sdes			return (TRUE);
1191919Swollman		default:
12095658Sdes			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
1211919Swollman			*objp = status;
12290297Sdes			return (TRUE);
1231919Swollman		}
1241919Swollman	}
1251919Swollman}
126