xref: /freebsd/lib/libc/yp/xdryp.c (revision d0b2dbfa)
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 #include <rpc/rpc.h>
34 #include <rpcsvc/yp.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 extern int (*ypresp_allfn)(u_long, char *, int, char *, int, void *);
39 extern void *ypresp_data;
40 
41 /*
42  * I'm leaving the xdr_datum() function in purely for backwards
43  * compatibility. yplib.c doesn't actually use it, but it's listed
44  * in yp_prot.h as being available, so it's probably a good idea to
45  * leave it in case somebody goes looking for it.
46  */
47 typedef struct {
48 	char *dptr;
49 	int  dsize;
50 } datum;
51 
52 bool_t
53 xdr_datum(XDR *xdrs, datum *objp)
54 {
55 	if (!xdr_bytes(xdrs, (char **)&objp->dptr, (u_int *)&objp->dsize, YPMAXRECORD)) {
56 		return (FALSE);
57 	}
58 	return (TRUE);
59 }
60 
61 bool_t
62 xdr_ypresp_all_seq(XDR *xdrs, u_long *objp)
63 {
64 	struct ypresp_all out;
65 	u_long status;
66 	char *key, *val;
67 	int r;
68 
69 	bzero(&out, sizeof out);
70 	while (1) {
71 		if (!xdr_ypresp_all(xdrs, &out)) {
72 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
73 			*objp = YP_YPERR;
74 			return (FALSE);
75 		}
76 		if (out.more == 0) {
77 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
78 			*objp = YP_NOMORE;
79 			return (TRUE);
80 		}
81 		status = out.ypresp_all_u.val.stat;
82 		switch (status) {
83 		case YP_TRUE:
84 			key = (char *)malloc(out.ypresp_all_u.val.key.keydat_len + 1);
85 			if (key == NULL) {
86 				xdr_free((xdrproc_t)xdr_ypresp_all, &out);
87 				*objp = YP_YPERR;
88 				return (FALSE);
89 			}
90 			bcopy(out.ypresp_all_u.val.key.keydat_val, key,
91 				out.ypresp_all_u.val.key.keydat_len);
92 			key[out.ypresp_all_u.val.key.keydat_len] = '\0';
93 			val = (char *)malloc(out.ypresp_all_u.val.val.valdat_len + 1);
94 			if (val == NULL) {
95 				free(key);
96 				xdr_free((xdrproc_t)xdr_ypresp_all, &out);
97 				*objp = YP_YPERR;
98 				return (FALSE);
99 			}
100 			bcopy(out.ypresp_all_u.val.val.valdat_val, val,
101 				out.ypresp_all_u.val.val.valdat_len);
102 			val[out.ypresp_all_u.val.val.valdat_len] = '\0';
103 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
104 
105 			r = (*ypresp_allfn)(status,
106 				key, out.ypresp_all_u.val.key.keydat_len,
107 				val, out.ypresp_all_u.val.val.valdat_len,
108 				ypresp_data);
109 			*objp = status;
110 			free(key);
111 			free(val);
112 			if (r)
113 				return (TRUE);
114 			break;
115 		case YP_NOMORE:
116 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
117 			*objp = YP_NOMORE;
118 			return (TRUE);
119 		default:
120 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
121 			*objp = status;
122 			return (TRUE);
123 		}
124 	}
125 }
126