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