yplib_host.c revision 1.1 1 1.1 thorpej /* $NetBSD: yplib_host.c,v 1.1 1996/08/09 10:14:51 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) theos.com>
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
16 1.1 thorpej * must display the following acknowledgement:
17 1.1 thorpej * This product includes software developed by Theo de Raadt.
18 1.1 thorpej * 4. The name of the author may not be used to endorse or promote products
19 1.1 thorpej * derived from this software without specific prior written permission.
20 1.1 thorpej *
21 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 1.1 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.1 thorpej * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 1.1 thorpej * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 thorpej * SUCH DAMAGE.
32 1.1 thorpej */
33 1.1 thorpej
34 1.1 thorpej #include <sys/param.h>
35 1.1 thorpej #include <sys/types.h>
36 1.1 thorpej #include <sys/socket.h>
37 1.1 thorpej #include <sys/file.h>
38 1.1 thorpej #include <sys/uio.h>
39 1.1 thorpej
40 1.1 thorpej #include <netinet/in.h>
41 1.1 thorpej #include <arpa/inet.h>
42 1.1 thorpej
43 1.1 thorpej #include <errno.h>
44 1.1 thorpej #include <stdio.h>
45 1.1 thorpej #include <stdlib.h>
46 1.1 thorpej #include <string.h>
47 1.1 thorpej #include <netdb.h>
48 1.1 thorpej #include <unistd.h>
49 1.1 thorpej
50 1.1 thorpej #include <rpc/rpc.h>
51 1.1 thorpej #include <rpc/xdr.h>
52 1.1 thorpej #include <rpcsvc/yp_prot.h>
53 1.1 thorpej #include <rpcsvc/ypclnt.h>
54 1.1 thorpej
55 1.1 thorpej struct timeval _yplib_host_timeout = { 10, 0 };
56 1.1 thorpej
57 1.1 thorpej CLIENT *
58 1.1 thorpej yp_bind_host(server, program, version, port, usetcp)
59 1.1 thorpej char *server;
60 1.1 thorpej u_int program, version;
61 1.1 thorpej u_short port;
62 1.1 thorpej int usetcp;
63 1.1 thorpej {
64 1.1 thorpej struct sockaddr_in rsrv_sin;
65 1.1 thorpej int rsrv_sock;
66 1.1 thorpej struct hostent *h;
67 1.1 thorpej static CLIENT *client;
68 1.1 thorpej
69 1.1 thorpej memset(&rsrv_sin, 0, sizeof rsrv_sin);
70 1.1 thorpej rsrv_sin.sin_len = sizeof rsrv_sin;
71 1.1 thorpej rsrv_sin.sin_family = AF_INET;
72 1.1 thorpej rsrv_sock = RPC_ANYSOCK;
73 1.1 thorpej if (port != 0) {
74 1.1 thorpej rsrv_sin.sin_port = htons(port);
75 1.1 thorpej }
76 1.1 thorpej
77 1.1 thorpej if (isdigit(*server)) {
78 1.1 thorpej if (inet_aton(server,&rsrv_sin.sin_addr) == 0) {
79 1.1 thorpej errx(1, "invalid IP address `%s'", server);
80 1.1 thorpej }
81 1.1 thorpej } else {
82 1.1 thorpej h = gethostbyname(server);
83 1.1 thorpej if(h == NULL) {
84 1.1 thorpej errx(1, "unknown host `%s'", server);
85 1.1 thorpej }
86 1.1 thorpej memcpy(&rsrv_sin.sin_addr.s_addr, h->h_addr_list[0],
87 1.1 thorpej h->h_length);
88 1.1 thorpej }
89 1.1 thorpej
90 1.1 thorpej if (usetcp)
91 1.1 thorpej client = clnttcp_create(&rsrv_sin, program, version,
92 1.1 thorpej &rsrv_sock, 0, 0);
93 1.1 thorpej else
94 1.1 thorpej client = clntudp_create(&rsrv_sin, program, version,
95 1.1 thorpej _yplib_host_timeout, &rsrv_sock);
96 1.1 thorpej
97 1.1 thorpej if (client == NULL)
98 1.1 thorpej errx(1, "%s: no contact with host `%s'",
99 1.1 thorpej usetcp ? "clnttcp_create" : "clntudp_create", server);
100 1.1 thorpej
101 1.1 thorpej return(client);
102 1.1 thorpej }
103 1.1 thorpej
104 1.1 thorpej CLIENT *
105 1.1 thorpej yp_bind_local(program, version)
106 1.1 thorpej u_int program, version;
107 1.1 thorpej {
108 1.1 thorpej struct sockaddr_in rsrv_sin;
109 1.1 thorpej int rsrv_sock;
110 1.1 thorpej static CLIENT *client;
111 1.1 thorpej
112 1.1 thorpej memset(&rsrv_sin, 0, sizeof rsrv_sin);
113 1.1 thorpej rsrv_sin.sin_len = sizeof rsrv_sin;
114 1.1 thorpej rsrv_sin.sin_family = AF_INET;
115 1.1 thorpej rsrv_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
116 1.1 thorpej rsrv_sock = RPC_ANYSOCK;
117 1.1 thorpej
118 1.1 thorpej client = clntudp_create(&rsrv_sin, program, version,
119 1.1 thorpej _yplib_host_timeout, &rsrv_sock);
120 1.1 thorpej if (client == NULL)
121 1.1 thorpej errx(1, "clntudp_create: no contact with localhost");
122 1.1 thorpej
123 1.1 thorpej return(client);
124 1.1 thorpej }
125 1.1 thorpej
126 1.1 thorpej int
127 1.1 thorpej yp_match_host(client, indomain, inmap, inkey, inkeylen, outval, outvallen)
128 1.1 thorpej CLIENT *client;
129 1.1 thorpej char *indomain;
130 1.1 thorpej char *inmap;
131 1.1 thorpej const char *inkey;
132 1.1 thorpej int inkeylen;
133 1.1 thorpej char **outval;
134 1.1 thorpej int *outvallen;
135 1.1 thorpej {
136 1.1 thorpej struct ypresp_val yprv;
137 1.1 thorpej struct ypreq_key yprk;
138 1.1 thorpej int r;
139 1.1 thorpej
140 1.1 thorpej *outval = NULL;
141 1.1 thorpej *outvallen = 0;
142 1.1 thorpej
143 1.1 thorpej yprk.domain = indomain;
144 1.1 thorpej yprk.map = inmap;
145 1.1 thorpej yprk.keydat.dptr = (char *)inkey;
146 1.1 thorpej yprk.keydat.dsize = inkeylen;
147 1.1 thorpej
148 1.1 thorpej memset(&yprv, 0, sizeof yprv);
149 1.1 thorpej
150 1.1 thorpej r = clnt_call(client, YPPROC_MATCH, xdr_ypreq_key, &yprk,
151 1.1 thorpej xdr_ypresp_val, &yprv, _yplib_host_timeout);
152 1.1 thorpej if(r != RPC_SUCCESS)
153 1.1 thorpej clnt_perror(client, "yp_match_host: clnt_call");
154 1.1 thorpej
155 1.1 thorpej if(!(r=ypprot_err(yprv.status)) ) {
156 1.1 thorpej *outvallen = yprv.valdat.dsize;
157 1.1 thorpej *outval = (char *)malloc(*outvallen+1);
158 1.1 thorpej memcpy(*outval, yprv.valdat.dptr, *outvallen);
159 1.1 thorpej (*outval)[*outvallen] = '\0';
160 1.1 thorpej }
161 1.1 thorpej xdr_free(xdr_ypresp_val, (char *)&yprv);
162 1.1 thorpej return r;
163 1.1 thorpej }
164 1.1 thorpej
165 1.1 thorpej int
166 1.1 thorpej yp_first_host(client, indomain, inmap, outkey, outkeylen, outval, outvallen)
167 1.1 thorpej CLIENT *client;
168 1.1 thorpej char *indomain;
169 1.1 thorpej char *inmap;
170 1.1 thorpej char **outkey;
171 1.1 thorpej int *outkeylen;
172 1.1 thorpej char **outval;
173 1.1 thorpej int *outvallen;
174 1.1 thorpej {
175 1.1 thorpej struct ypresp_key_val yprkv;
176 1.1 thorpej struct ypreq_nokey yprnk;
177 1.1 thorpej int r;
178 1.1 thorpej
179 1.1 thorpej *outkey = *outval = NULL;
180 1.1 thorpej *outkeylen = *outvallen = 0;
181 1.1 thorpej
182 1.1 thorpej yprnk.domain = indomain;
183 1.1 thorpej yprnk.map = inmap;
184 1.1 thorpej memset(&yprkv, 0, sizeof yprkv);
185 1.1 thorpej
186 1.1 thorpej r = clnt_call(client, YPPROC_FIRST, xdr_ypreq_nokey, &yprnk,
187 1.1 thorpej xdr_ypresp_key_val, &yprkv, _yplib_host_timeout);
188 1.1 thorpej if (r != RPC_SUCCESS)
189 1.1 thorpej clnt_perror(client, "yp_first_host: clnt_call");
190 1.1 thorpej
191 1.1 thorpej if(!(r=ypprot_err(yprkv.status)) ) {
192 1.1 thorpej *outkeylen = yprkv.keydat.dsize;
193 1.1 thorpej *outkey = (char *)malloc(*outkeylen+1);
194 1.1 thorpej memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
195 1.1 thorpej (*outkey)[*outkeylen] = '\0';
196 1.1 thorpej *outvallen = yprkv.valdat.dsize;
197 1.1 thorpej *outval = (char *)malloc(*outvallen+1);
198 1.1 thorpej memcpy(*outval, yprkv.valdat.dptr, *outvallen);
199 1.1 thorpej (*outval)[*outvallen] = '\0';
200 1.1 thorpej }
201 1.1 thorpej xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
202 1.1 thorpej return r;
203 1.1 thorpej }
204 1.1 thorpej
205 1.1 thorpej int
206 1.1 thorpej yp_next_host(client, indomain, inmap, inkey, inkeylen, outkey,
207 1.1 thorpej outkeylen, outval, outvallen)
208 1.1 thorpej CLIENT *client;
209 1.1 thorpej char *indomain;
210 1.1 thorpej char *inmap;
211 1.1 thorpej char *inkey;
212 1.1 thorpej int inkeylen;
213 1.1 thorpej char **outkey;
214 1.1 thorpej int *outkeylen;
215 1.1 thorpej char **outval;
216 1.1 thorpej int *outvallen;
217 1.1 thorpej {
218 1.1 thorpej struct ypresp_key_val yprkv;
219 1.1 thorpej struct ypreq_key yprk;
220 1.1 thorpej int r;
221 1.1 thorpej
222 1.1 thorpej *outkey = *outval = NULL;
223 1.1 thorpej *outkeylen = *outvallen = 0;
224 1.1 thorpej
225 1.1 thorpej yprk.domain = indomain;
226 1.1 thorpej yprk.map = inmap;
227 1.1 thorpej yprk.keydat.dptr = inkey;
228 1.1 thorpej yprk.keydat.dsize = inkeylen;
229 1.1 thorpej memset(&yprkv, 0, sizeof yprkv);
230 1.1 thorpej
231 1.1 thorpej r = clnt_call(client, YPPROC_NEXT, xdr_ypreq_key, &yprk,
232 1.1 thorpej xdr_ypresp_key_val, &yprkv, _yplib_host_timeout);
233 1.1 thorpej if (r != RPC_SUCCESS)
234 1.1 thorpej clnt_perror(client, "yp_next_host: clnt_call");
235 1.1 thorpej
236 1.1 thorpej if(!(r=ypprot_err(yprkv.status)) ) {
237 1.1 thorpej *outkeylen = yprkv.keydat.dsize;
238 1.1 thorpej *outkey = (char *)malloc(*outkeylen+1);
239 1.1 thorpej memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
240 1.1 thorpej (*outkey)[*outkeylen] = '\0';
241 1.1 thorpej *outvallen = yprkv.valdat.dsize;
242 1.1 thorpej *outval = (char *)malloc(*outvallen+1);
243 1.1 thorpej memcpy(*outval, yprkv.valdat.dptr, *outvallen);
244 1.1 thorpej (*outval)[*outvallen] = '\0';
245 1.1 thorpej }
246 1.1 thorpej xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
247 1.1 thorpej return r;
248 1.1 thorpej }
249 1.1 thorpej
250 1.1 thorpej int
251 1.1 thorpej yp_all_host(client, indomain, inmap, incallback)
252 1.1 thorpej CLIENT *client;
253 1.1 thorpej char *indomain;
254 1.1 thorpej char *inmap;
255 1.1 thorpej struct ypall_callback *incallback;
256 1.1 thorpej {
257 1.1 thorpej struct ypreq_nokey yprnk;
258 1.1 thorpej int status;
259 1.1 thorpej
260 1.1 thorpej yprnk.domain = indomain;
261 1.1 thorpej yprnk.map = inmap;
262 1.1 thorpej
263 1.1 thorpej status = clnt_call(client, YPPROC_ALL, xdr_ypreq_nokey, &yprnk,
264 1.1 thorpej xdr_ypall, (char *)incallback, _yplib_host_timeout);
265 1.1 thorpej
266 1.1 thorpej if (status != RPC_SUCCESS)
267 1.1 thorpej return YPERR_RPC;
268 1.1 thorpej
269 1.1 thorpej return 0;
270 1.1 thorpej }
271 1.1 thorpej
272 1.1 thorpej int
273 1.1 thorpej yp_order_host(client, indomain, inmap, outorder)
274 1.1 thorpej CLIENT *client;
275 1.1 thorpej char *indomain;
276 1.1 thorpej char *inmap;
277 1.1 thorpej int *outorder;
278 1.1 thorpej {
279 1.1 thorpej struct ypresp_order ypro;
280 1.1 thorpej struct ypreq_nokey yprnk;
281 1.1 thorpej int r;
282 1.1 thorpej
283 1.1 thorpej yprnk.domain = indomain;
284 1.1 thorpej yprnk.map = inmap;
285 1.1 thorpej
286 1.1 thorpej memset(&ypro, 0, sizeof ypro);
287 1.1 thorpej
288 1.1 thorpej r = clnt_call(client, YPPROC_ORDER, xdr_ypreq_nokey, &yprnk,
289 1.1 thorpej xdr_ypresp_order, &ypro, _yplib_host_timeout);
290 1.1 thorpej if(r != RPC_SUCCESS)
291 1.1 thorpej clnt_perror(client, "yp_order_host: clnt_call");
292 1.1 thorpej
293 1.1 thorpej *outorder = ypro.ordernum;
294 1.1 thorpej xdr_free(xdr_ypresp_order, (char *)&ypro);
295 1.1 thorpej return ypprot_err(ypro.status);
296 1.1 thorpej }
297 1.1 thorpej
298 1.1 thorpej int
299 1.1 thorpej yp_master_host(client, indomain, inmap, outname)
300 1.1 thorpej CLIENT *client;
301 1.1 thorpej char *indomain;
302 1.1 thorpej char *inmap;
303 1.1 thorpej char **outname;
304 1.1 thorpej {
305 1.1 thorpej struct ypresp_master yprm;
306 1.1 thorpej struct ypreq_nokey yprnk;
307 1.1 thorpej int r;
308 1.1 thorpej
309 1.1 thorpej yprnk.domain = indomain;
310 1.1 thorpej yprnk.map = inmap;
311 1.1 thorpej
312 1.1 thorpej memset(&yprm, 0, sizeof yprm);
313 1.1 thorpej
314 1.1 thorpej r = clnt_call(client, YPPROC_MASTER, xdr_ypreq_nokey, &yprnk,
315 1.1 thorpej xdr_ypresp_master, &yprm, _yplib_host_timeout);
316 1.1 thorpej if (r != RPC_SUCCESS)
317 1.1 thorpej clnt_perror(client, "yp_master: clnt_call");
318 1.1 thorpej
319 1.1 thorpej if(!(r=ypprot_err(yprm.status)) ) {
320 1.1 thorpej *outname = (char *)strdup(yprm.master);
321 1.1 thorpej }
322 1.1 thorpej xdr_free(xdr_ypresp_master, (char *)&yprm);
323 1.1 thorpej return r;
324 1.1 thorpej }
325 1.1 thorpej
326 1.1 thorpej int
327 1.1 thorpej yp_maplist_host(client, indomain, outmaplist)
328 1.1 thorpej CLIENT *client;
329 1.1 thorpej char *indomain;
330 1.1 thorpej struct ypmaplist **outmaplist;
331 1.1 thorpej {
332 1.1 thorpej struct ypresp_maplist ypml;
333 1.1 thorpej int r;
334 1.1 thorpej
335 1.1 thorpej memset(&ypml, 0, sizeof ypml);
336 1.1 thorpej
337 1.1 thorpej r = clnt_call(client, YPPROC_MAPLIST, xdr_ypdomain_wrap_string,
338 1.1 thorpej indomain, xdr_ypresp_maplist, &ypml, _yplib_host_timeout);
339 1.1 thorpej if (r != RPC_SUCCESS)
340 1.1 thorpej clnt_perror(client, "yp_maplist: clnt_call");
341 1.1 thorpej
342 1.1 thorpej *outmaplist = ypml.list;
343 1.1 thorpej /* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
344 1.1 thorpej return ypprot_err(ypml.status);
345 1.1 thorpej }
346