xref: /freebsd/lib/libc/yp/xdryp.c (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
1 /*
2  * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char *rcsid = "$FreeBSD$";
32 #endif
33 
34 #include <rpc/rpc.h>
35 #include <rpcsvc/yp.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 extern int (*ypresp_allfn)();
40 extern void *ypresp_data;
41 
42 /*
43  * I'm leaving the xdr_datum() function in purely for backwards
44  * compatibility. yplib.c doesn't actually use it, but it's listed
45  * in yp_prot.h as being available, so it's probably a good idea to
46  * leave it in in case somebody goes looking for it.
47  */
48 typedef struct {
49 	char *dptr;
50 	int  dsize;
51 } datum;
52 
53 bool_t
54 xdr_datum(XDR *xdrs, datum *objp)
55 {
56 	if (!xdr_bytes(xdrs, (char **)&objp->dptr, (u_int *)&objp->dsize, YPMAXRECORD)) {
57 		return (FALSE);
58 	}
59 	return (TRUE);
60 }
61 
62 bool_t
63 xdr_ypresp_all_seq(XDR *xdrs, u_long *objp)
64 {
65 	struct ypresp_all out;
66 	u_long status;
67 	char *key, *val;
68 	int r;
69 
70 	bzero(&out, sizeof out);
71 	while (1) {
72 		if (!xdr_ypresp_all(xdrs, &out)) {
73 			xdr_free(xdr_ypresp_all, (char *)&out);
74 			*objp = YP_YPERR;
75 			return (FALSE);
76 		}
77 		if (out.more == 0) {
78 			xdr_free(xdr_ypresp_all, (char *)&out);
79 			*objp = YP_NOMORE;
80 			return (TRUE);
81 		}
82 		status = out.ypresp_all_u.val.stat;
83 		switch (status) {
84 		case YP_TRUE:
85 			key = (char *)malloc(out.ypresp_all_u.val.key.keydat_len + 1);
86 			bcopy(out.ypresp_all_u.val.key.keydat_val, key,
87 				out.ypresp_all_u.val.key.keydat_len);
88 			key[out.ypresp_all_u.val.key.keydat_len] = '\0';
89 			val = (char *)malloc(out.ypresp_all_u.val.val.valdat_len + 1);
90 			bcopy(out.ypresp_all_u.val.val.valdat_val, val,
91 				out.ypresp_all_u.val.val.valdat_len);
92 			val[out.ypresp_all_u.val.val.valdat_len] = '\0';
93 			xdr_free(xdr_ypresp_all, (char *)&out);
94 
95 			r = (*ypresp_allfn)(status,
96 				key, out.ypresp_all_u.val.key.keydat_len,
97 				val, out.ypresp_all_u.val.val.valdat_len,
98 				ypresp_data);
99 			*objp = status;
100 			free(key);
101 			free(val);
102 			if (r)
103 				return (TRUE);
104 			break;
105 		case YP_NOMORE:
106 			xdr_free(xdr_ypresp_all, (char *)&out);
107 			*objp = YP_NOMORE;
108 			return (TRUE);
109 		default:
110 			xdr_free(xdr_ypresp_all, (char *)&out);
111 			*objp = status;
112 			return (TRUE);
113 		}
114 	}
115 }
116