xref: /freebsd/lib/libc/yp/xdryp.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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. The name of the author may not be used to endorse or promote
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * 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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <rpc/rpc.h>
36 #include <rpcsvc/yp.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 extern int (*ypresp_allfn)(u_long, char *, int, char *, int, void *);
41 extern void *ypresp_data;
42 
43 /*
44  * I'm leaving the xdr_datum() function in purely for backwards
45  * compatibility. yplib.c doesn't actually use it, but it's listed
46  * in yp_prot.h as being available, so it's probably a good idea to
47  * leave it in case somebody goes looking for it.
48  */
49 typedef struct {
50 	char *dptr;
51 	int  dsize;
52 } datum;
53 
54 bool_t
55 xdr_datum(XDR *xdrs, datum *objp)
56 {
57 	if (!xdr_bytes(xdrs, (char **)&objp->dptr, (u_int *)&objp->dsize, YPMAXRECORD)) {
58 		return (FALSE);
59 	}
60 	return (TRUE);
61 }
62 
63 bool_t
64 xdr_ypresp_all_seq(XDR *xdrs, u_long *objp)
65 {
66 	struct ypresp_all out;
67 	u_long status;
68 	char *key, *val;
69 	int r;
70 
71 	bzero(&out, sizeof out);
72 	while (1) {
73 		if (!xdr_ypresp_all(xdrs, &out)) {
74 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
75 			*objp = YP_YPERR;
76 			return (FALSE);
77 		}
78 		if (out.more == 0) {
79 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
80 			*objp = YP_NOMORE;
81 			return (TRUE);
82 		}
83 		status = out.ypresp_all_u.val.stat;
84 		switch (status) {
85 		case YP_TRUE:
86 			key = (char *)malloc(out.ypresp_all_u.val.key.keydat_len + 1);
87 			if (key == NULL) {
88 				xdr_free((xdrproc_t)xdr_ypresp_all, &out);
89 				*objp = YP_YPERR;
90 				return (FALSE);
91 			}
92 			bcopy(out.ypresp_all_u.val.key.keydat_val, key,
93 				out.ypresp_all_u.val.key.keydat_len);
94 			key[out.ypresp_all_u.val.key.keydat_len] = '\0';
95 			val = (char *)malloc(out.ypresp_all_u.val.val.valdat_len + 1);
96 			if (val == NULL) {
97 				free(key);
98 				xdr_free((xdrproc_t)xdr_ypresp_all, &out);
99 				*objp = YP_YPERR;
100 				return (FALSE);
101 			}
102 			bcopy(out.ypresp_all_u.val.val.valdat_val, val,
103 				out.ypresp_all_u.val.val.valdat_len);
104 			val[out.ypresp_all_u.val.val.valdat_len] = '\0';
105 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
106 
107 			r = (*ypresp_allfn)(status,
108 				key, out.ypresp_all_u.val.key.keydat_len,
109 				val, out.ypresp_all_u.val.val.valdat_len,
110 				ypresp_data);
111 			*objp = status;
112 			free(key);
113 			free(val);
114 			if (r)
115 				return (TRUE);
116 			break;
117 		case YP_NOMORE:
118 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
119 			*objp = YP_NOMORE;
120 			return (TRUE);
121 		default:
122 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
123 			*objp = status;
124 			return (TRUE);
125 		}
126 	}
127 }
128