xref: /openbsd/lib/libc/yp/yp_all.c (revision 404b540a)
1 /*	$OpenBSD: yp_all.c,v 1.9 2005/08/05 13:02:16 espie Exp $ */
2 /*
3  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/types.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <rpc/rpc.h>
34 #include <rpc/xdr.h>
35 #include <rpcsvc/yp.h>
36 #include <rpcsvc/ypclnt.h>
37 #include "ypinternal.h"
38 
39 bool_t
40 xdr_ypresp_all_seq(XDR *xdrs, u_long *objp)
41 {
42 	struct ypresp_all out;
43 	u_long status;
44 	char *key, *val;
45 	int size;
46 	int r;
47 
48 	memset(&out, 0, sizeof out);
49 	while (1) {
50 		if (!xdr_ypresp_all(xdrs, &out)) {
51 			xdr_free(xdr_ypresp_all, (char *)&out);
52 			*objp = (u_long)YP_YPERR;
53 			return FALSE;
54 		}
55 		if (out.more == 0) {
56 			xdr_free(xdr_ypresp_all, (char *)&out);
57 			return FALSE;
58 		}
59 		status = out.ypresp_all_u.val.stat;
60 		switch (status) {
61 		case YP_TRUE:
62 			size = out.ypresp_all_u.val.key.keydat_len;
63 			if ((key = malloc(size + 1)) != NULL) {
64 				(void)memcpy(key,
65 				    out.ypresp_all_u.val.key.keydat_val,
66 				    size);
67 				key[size] = '\0';
68 			}
69 			size = out.ypresp_all_u.val.val.valdat_len;
70 			if ((val = malloc(size + 1)) != NULL) {
71 				(void)memcpy(val,
72 				    out.ypresp_all_u.val.val.valdat_val,
73 				    size);
74 				val[size] = '\0';
75 			} else {
76 				free(key);
77 				key = NULL;
78 			}
79 			xdr_free(xdr_ypresp_all, (char *)&out);
80 
81 			if (key == NULL || val == NULL)
82 				return FALSE;
83 
84 			r = (*ypresp_allfn)(status, key,
85 			    out.ypresp_all_u.val.key.keydat_len, val,
86 			    out.ypresp_all_u.val.val.valdat_len, ypresp_data);
87 			*objp = status;
88 			free(key);
89 			free(val);
90 			if (r)
91 				return TRUE;
92 			break;
93 		case YP_NOMORE:
94 			xdr_free(xdr_ypresp_all, (char *)&out);
95 			return TRUE;
96 		default:
97 			xdr_free(xdr_ypresp_all, (char *)&out);
98 			*objp = status;
99 			return TRUE;
100 		}
101 	}
102 }
103 
104 int
105 yp_all(const char *indomain, const char *inmap, struct ypall_callback *incallback)
106 {
107 	struct ypreq_nokey yprnk;
108 	struct dom_binding *ysd;
109 	struct timeval  tv;
110 	struct sockaddr_in clnt_sin;
111 	CLIENT         *clnt;
112 	u_long		status;
113 	int             clnt_sock;
114 	int		r = 0;
115 
116 	if (indomain == NULL || *indomain == '\0' ||
117 	    strlen(indomain) > YPMAXDOMAIN || inmap == NULL ||
118 	    *inmap == '\0' || strlen(inmap) > YPMAXMAP || incallback == NULL)
119 		return YPERR_BADARGS;
120 
121 	if (_yp_dobind(indomain, &ysd) != 0)
122 		return YPERR_DOMAIN;
123 
124 	tv.tv_sec = _yplib_timeout;
125 	tv.tv_usec = 0;
126 	clnt_sock = RPC_ANYSOCK;
127 	clnt_sin = ysd->dom_server_addr;
128 	clnt_sin.sin_port = 0;
129 	clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
130 	if (clnt == NULL) {
131 		printf("clnttcp_create failed\n");
132 		r = YPERR_PMAP;
133 		goto out;
134 	}
135 	yprnk.domain = (char *)indomain;
136 	yprnk.map = (char *)inmap;
137 	ypresp_allfn = incallback->foreach;
138 	ypresp_data = (void *) incallback->data;
139 
140 	(void) clnt_call(clnt, YPPROC_ALL,
141 	    xdr_ypreq_nokey, &yprnk, xdr_ypresp_all_seq, &status, tv);
142 	clnt_destroy(clnt);
143 	if (status != YP_FALSE)
144 		r = ypprot_err(status);
145 out:
146 	_yp_unbind(ysd);
147 	return r;
148 }
149