xref: /openbsd-src/lib/libc/yp/yp_all.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*
2  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Theo de Raadt.
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written 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 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *rcsid = "$OpenBSD: yp_all.c,v 1.5 1996/12/14 06:49:46 tholo Exp $";
34 #endif /* LIBC_SCCS and not lint */
35 
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/file.h>
40 #include <sys/uio.h>
41 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <rpc/rpc.h>
47 #include <rpc/xdr.h>
48 #include <rpcsvc/yp.h>
49 #include <rpcsvc/ypclnt.h>
50 #include "ypinternal.h"
51 
52 bool_t
53 xdr_ypresp_all_seq(xdrs, objp)
54 XDR *xdrs;
55 u_long *objp;
56 {
57 	struct ypresp_all out;
58 	u_long status;
59 	char *key, *val;
60 	int size;
61 	int r;
62 
63 	memset(&out, 0, sizeof out);
64 	while(1) {
65 		if( !xdr_ypresp_all(xdrs, &out)) {
66 			xdr_free(xdr_ypresp_all, (char *)&out);
67 			*objp = (u_long)YP_YPERR;
68 			return FALSE;
69 		}
70 		if(out.more == 0) {
71 			xdr_free(xdr_ypresp_all, (char *)&out);
72 			return FALSE;
73 		}
74 		status = out.ypresp_all_u.val.stat;
75 		switch(status) {
76 		case YP_TRUE:
77 			size = out.ypresp_all_u.val.key.keydat_len;
78 			if ((key = malloc(size + 1)) != NULL) {
79 				(void)memcpy(key,
80 				    out.ypresp_all_u.val.key.keydat_val,
81 				    size);
82 				key[size] = '\0';
83 			}
84 			size = out.ypresp_all_u.val.val.valdat_len;
85 			if ((val = malloc(size + 1)) != NULL) {
86 				(void)memcpy(val,
87 				    out.ypresp_all_u.val.val.valdat_val,
88 				    size);
89 				val[size] = '\0';
90 			}
91 			else {
92 				free(key);
93 				key = NULL;
94 			}
95 			xdr_free(xdr_ypresp_all, (char *)&out);
96 
97 			if (key == NULL || val == NULL)
98 				return FALSE;
99 
100 			r = (*ypresp_allfn)(status, key,
101 			    out.ypresp_all_u.val.key.keydat_len, val,
102 			    out.ypresp_all_u.val.val.valdat_len, ypresp_data);
103 			*objp = status;
104 			free(key);
105 			free(val);
106 			if(r)
107 				return TRUE;
108 			break;
109 		case YP_NOMORE:
110 			xdr_free(xdr_ypresp_all, (char *)&out);
111 			return TRUE;
112 		default:
113 			xdr_free(xdr_ypresp_all, (char *)&out);
114 			*objp = status;
115 			return TRUE;
116 		}
117 	}
118 }
119 
120 int
121 yp_all(indomain, inmap, incallback)
122 	const char     *indomain;
123 	const char     *inmap;
124 	struct ypall_callback *incallback;
125 {
126 	struct ypreq_nokey yprnk;
127 	struct dom_binding *ysd;
128 	struct timeval  tv;
129 	struct sockaddr_in clnt_sin;
130 	CLIENT         *clnt;
131 	u_long		status;
132 	int             clnt_sock;
133 	int		r = 0;
134 
135 	if (indomain == NULL || *indomain == '\0' ||
136 	    strlen(indomain) > YPMAXDOMAIN || inmap == NULL ||
137 	    *inmap == '\0' || strlen(inmap) > YPMAXMAP || incallback == NULL)
138 		return YPERR_BADARGS;
139 
140 	if (_yp_dobind(indomain, &ysd) != 0)
141 		return YPERR_DOMAIN;
142 
143 	tv.tv_sec = _yplib_timeout;
144 	tv.tv_usec = 0;
145 	clnt_sock = RPC_ANYSOCK;
146 	clnt_sin = ysd->dom_server_addr;
147 	clnt_sin.sin_port = 0;
148 	clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
149 	if (clnt == NULL) {
150 		printf("clnttcp_create failed\n");
151 		r = YPERR_PMAP;
152 		goto out;
153 	}
154 	yprnk.domain = (char *)indomain;
155 	yprnk.map = (char *)inmap;
156 	ypresp_allfn = incallback->foreach;
157 	ypresp_data = (void *) incallback->data;
158 
159 	(void) clnt_call(clnt, YPPROC_ALL,
160 	    xdr_ypreq_nokey, &yprnk, xdr_ypresp_all_seq, &status, tv);
161 	clnt_destroy(clnt);
162 	if(status != YP_FALSE)
163 		r = ypprot_err(status);
164 out:
165 	_yp_unbind(ysd);
166 	return r;
167 }
168