yplib_host.c revision 1.2 1 1.2 thorpej /* $NetBSD: yplib_host.c,v 1.2 1997/07/18 21:57:03 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.2 thorpej #include <ctype.h>
44 1.2 thorpej #include <err.h>
45 1.1 thorpej #include <errno.h>
46 1.1 thorpej #include <stdio.h>
47 1.1 thorpej #include <stdlib.h>
48 1.1 thorpej #include <string.h>
49 1.1 thorpej #include <netdb.h>
50 1.1 thorpej #include <unistd.h>
51 1.1 thorpej
52 1.1 thorpej #include <rpc/rpc.h>
53 1.1 thorpej #include <rpc/xdr.h>
54 1.1 thorpej #include <rpcsvc/yp_prot.h>
55 1.1 thorpej #include <rpcsvc/ypclnt.h>
56 1.2 thorpej
57 1.2 thorpej #include "yplib_host.h"
58 1.1 thorpej
59 1.1 thorpej struct timeval _yplib_host_timeout = { 10, 0 };
60 1.1 thorpej
61 1.1 thorpej CLIENT *
62 1.1 thorpej yp_bind_host(server, program, version, port, usetcp)
63 1.1 thorpej char *server;
64 1.1 thorpej u_int program, version;
65 1.1 thorpej u_short port;
66 1.1 thorpej int usetcp;
67 1.1 thorpej {
68 1.1 thorpej struct sockaddr_in rsrv_sin;
69 1.1 thorpej int rsrv_sock;
70 1.1 thorpej struct hostent *h;
71 1.1 thorpej static CLIENT *client;
72 1.1 thorpej
73 1.1 thorpej memset(&rsrv_sin, 0, sizeof rsrv_sin);
74 1.1 thorpej rsrv_sin.sin_len = sizeof rsrv_sin;
75 1.1 thorpej rsrv_sin.sin_family = AF_INET;
76 1.1 thorpej rsrv_sock = RPC_ANYSOCK;
77 1.1 thorpej if (port != 0) {
78 1.1 thorpej rsrv_sin.sin_port = htons(port);
79 1.1 thorpej }
80 1.1 thorpej
81 1.1 thorpej if (isdigit(*server)) {
82 1.1 thorpej if (inet_aton(server,&rsrv_sin.sin_addr) == 0) {
83 1.1 thorpej errx(1, "invalid IP address `%s'", server);
84 1.1 thorpej }
85 1.1 thorpej } else {
86 1.1 thorpej h = gethostbyname(server);
87 1.1 thorpej if(h == NULL) {
88 1.1 thorpej errx(1, "unknown host `%s'", server);
89 1.1 thorpej }
90 1.1 thorpej memcpy(&rsrv_sin.sin_addr.s_addr, h->h_addr_list[0],
91 1.1 thorpej h->h_length);
92 1.1 thorpej }
93 1.1 thorpej
94 1.1 thorpej if (usetcp)
95 1.1 thorpej client = clnttcp_create(&rsrv_sin, program, version,
96 1.1 thorpej &rsrv_sock, 0, 0);
97 1.1 thorpej else
98 1.1 thorpej client = clntudp_create(&rsrv_sin, program, version,
99 1.1 thorpej _yplib_host_timeout, &rsrv_sock);
100 1.1 thorpej
101 1.1 thorpej if (client == NULL)
102 1.1 thorpej errx(1, "%s: no contact with host `%s'",
103 1.1 thorpej usetcp ? "clnttcp_create" : "clntudp_create", server);
104 1.1 thorpej
105 1.1 thorpej return(client);
106 1.1 thorpej }
107 1.1 thorpej
108 1.1 thorpej CLIENT *
109 1.1 thorpej yp_bind_local(program, version)
110 1.1 thorpej u_int program, version;
111 1.1 thorpej {
112 1.1 thorpej struct sockaddr_in rsrv_sin;
113 1.1 thorpej int rsrv_sock;
114 1.1 thorpej static CLIENT *client;
115 1.1 thorpej
116 1.1 thorpej memset(&rsrv_sin, 0, sizeof rsrv_sin);
117 1.1 thorpej rsrv_sin.sin_len = sizeof rsrv_sin;
118 1.1 thorpej rsrv_sin.sin_family = AF_INET;
119 1.1 thorpej rsrv_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
120 1.1 thorpej rsrv_sock = RPC_ANYSOCK;
121 1.1 thorpej
122 1.1 thorpej client = clntudp_create(&rsrv_sin, program, version,
123 1.1 thorpej _yplib_host_timeout, &rsrv_sock);
124 1.1 thorpej if (client == NULL)
125 1.1 thorpej errx(1, "clntudp_create: no contact with localhost");
126 1.1 thorpej
127 1.1 thorpej return(client);
128 1.1 thorpej }
129 1.1 thorpej
130 1.1 thorpej int
131 1.1 thorpej yp_match_host(client, indomain, inmap, inkey, inkeylen, outval, outvallen)
132 1.1 thorpej CLIENT *client;
133 1.1 thorpej char *indomain;
134 1.1 thorpej char *inmap;
135 1.1 thorpej const char *inkey;
136 1.1 thorpej int inkeylen;
137 1.1 thorpej char **outval;
138 1.1 thorpej int *outvallen;
139 1.1 thorpej {
140 1.1 thorpej struct ypresp_val yprv;
141 1.1 thorpej struct ypreq_key yprk;
142 1.1 thorpej int r;
143 1.1 thorpej
144 1.1 thorpej *outval = NULL;
145 1.1 thorpej *outvallen = 0;
146 1.1 thorpej
147 1.1 thorpej yprk.domain = indomain;
148 1.1 thorpej yprk.map = inmap;
149 1.1 thorpej yprk.keydat.dptr = (char *)inkey;
150 1.1 thorpej yprk.keydat.dsize = inkeylen;
151 1.1 thorpej
152 1.1 thorpej memset(&yprv, 0, sizeof yprv);
153 1.1 thorpej
154 1.1 thorpej r = clnt_call(client, YPPROC_MATCH, xdr_ypreq_key, &yprk,
155 1.1 thorpej xdr_ypresp_val, &yprv, _yplib_host_timeout);
156 1.1 thorpej if(r != RPC_SUCCESS)
157 1.1 thorpej clnt_perror(client, "yp_match_host: clnt_call");
158 1.1 thorpej
159 1.1 thorpej if(!(r=ypprot_err(yprv.status)) ) {
160 1.1 thorpej *outvallen = yprv.valdat.dsize;
161 1.1 thorpej *outval = (char *)malloc(*outvallen+1);
162 1.1 thorpej memcpy(*outval, yprv.valdat.dptr, *outvallen);
163 1.1 thorpej (*outval)[*outvallen] = '\0';
164 1.1 thorpej }
165 1.1 thorpej xdr_free(xdr_ypresp_val, (char *)&yprv);
166 1.1 thorpej return r;
167 1.1 thorpej }
168 1.1 thorpej
169 1.1 thorpej int
170 1.1 thorpej yp_first_host(client, indomain, inmap, outkey, outkeylen, outval, outvallen)
171 1.1 thorpej CLIENT *client;
172 1.1 thorpej char *indomain;
173 1.1 thorpej char *inmap;
174 1.1 thorpej char **outkey;
175 1.1 thorpej int *outkeylen;
176 1.1 thorpej char **outval;
177 1.1 thorpej int *outvallen;
178 1.1 thorpej {
179 1.1 thorpej struct ypresp_key_val yprkv;
180 1.1 thorpej struct ypreq_nokey yprnk;
181 1.1 thorpej int r;
182 1.1 thorpej
183 1.1 thorpej *outkey = *outval = NULL;
184 1.1 thorpej *outkeylen = *outvallen = 0;
185 1.1 thorpej
186 1.1 thorpej yprnk.domain = indomain;
187 1.1 thorpej yprnk.map = inmap;
188 1.1 thorpej memset(&yprkv, 0, sizeof yprkv);
189 1.1 thorpej
190 1.1 thorpej r = clnt_call(client, YPPROC_FIRST, xdr_ypreq_nokey, &yprnk,
191 1.1 thorpej xdr_ypresp_key_val, &yprkv, _yplib_host_timeout);
192 1.1 thorpej if (r != RPC_SUCCESS)
193 1.1 thorpej clnt_perror(client, "yp_first_host: clnt_call");
194 1.1 thorpej
195 1.1 thorpej if(!(r=ypprot_err(yprkv.status)) ) {
196 1.1 thorpej *outkeylen = yprkv.keydat.dsize;
197 1.1 thorpej *outkey = (char *)malloc(*outkeylen+1);
198 1.1 thorpej memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
199 1.1 thorpej (*outkey)[*outkeylen] = '\0';
200 1.1 thorpej *outvallen = yprkv.valdat.dsize;
201 1.1 thorpej *outval = (char *)malloc(*outvallen+1);
202 1.1 thorpej memcpy(*outval, yprkv.valdat.dptr, *outvallen);
203 1.1 thorpej (*outval)[*outvallen] = '\0';
204 1.1 thorpej }
205 1.1 thorpej xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
206 1.1 thorpej return r;
207 1.1 thorpej }
208 1.1 thorpej
209 1.1 thorpej int
210 1.1 thorpej yp_next_host(client, indomain, inmap, inkey, inkeylen, outkey,
211 1.1 thorpej outkeylen, outval, outvallen)
212 1.1 thorpej CLIENT *client;
213 1.1 thorpej char *indomain;
214 1.1 thorpej char *inmap;
215 1.1 thorpej char *inkey;
216 1.1 thorpej int inkeylen;
217 1.1 thorpej char **outkey;
218 1.1 thorpej int *outkeylen;
219 1.1 thorpej char **outval;
220 1.1 thorpej int *outvallen;
221 1.1 thorpej {
222 1.1 thorpej struct ypresp_key_val yprkv;
223 1.1 thorpej struct ypreq_key yprk;
224 1.1 thorpej int r;
225 1.1 thorpej
226 1.1 thorpej *outkey = *outval = NULL;
227 1.1 thorpej *outkeylen = *outvallen = 0;
228 1.1 thorpej
229 1.1 thorpej yprk.domain = indomain;
230 1.1 thorpej yprk.map = inmap;
231 1.1 thorpej yprk.keydat.dptr = inkey;
232 1.1 thorpej yprk.keydat.dsize = inkeylen;
233 1.1 thorpej memset(&yprkv, 0, sizeof yprkv);
234 1.1 thorpej
235 1.1 thorpej r = clnt_call(client, YPPROC_NEXT, xdr_ypreq_key, &yprk,
236 1.1 thorpej xdr_ypresp_key_val, &yprkv, _yplib_host_timeout);
237 1.1 thorpej if (r != RPC_SUCCESS)
238 1.1 thorpej clnt_perror(client, "yp_next_host: clnt_call");
239 1.1 thorpej
240 1.1 thorpej if(!(r=ypprot_err(yprkv.status)) ) {
241 1.1 thorpej *outkeylen = yprkv.keydat.dsize;
242 1.1 thorpej *outkey = (char *)malloc(*outkeylen+1);
243 1.1 thorpej memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
244 1.1 thorpej (*outkey)[*outkeylen] = '\0';
245 1.1 thorpej *outvallen = yprkv.valdat.dsize;
246 1.1 thorpej *outval = (char *)malloc(*outvallen+1);
247 1.1 thorpej memcpy(*outval, yprkv.valdat.dptr, *outvallen);
248 1.1 thorpej (*outval)[*outvallen] = '\0';
249 1.1 thorpej }
250 1.1 thorpej xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
251 1.1 thorpej return r;
252 1.1 thorpej }
253 1.1 thorpej
254 1.1 thorpej int
255 1.1 thorpej yp_all_host(client, indomain, inmap, incallback)
256 1.1 thorpej CLIENT *client;
257 1.1 thorpej char *indomain;
258 1.1 thorpej char *inmap;
259 1.1 thorpej struct ypall_callback *incallback;
260 1.1 thorpej {
261 1.1 thorpej struct ypreq_nokey yprnk;
262 1.1 thorpej int status;
263 1.1 thorpej
264 1.1 thorpej yprnk.domain = indomain;
265 1.1 thorpej yprnk.map = inmap;
266 1.1 thorpej
267 1.1 thorpej status = clnt_call(client, YPPROC_ALL, xdr_ypreq_nokey, &yprnk,
268 1.1 thorpej xdr_ypall, (char *)incallback, _yplib_host_timeout);
269 1.1 thorpej
270 1.1 thorpej if (status != RPC_SUCCESS)
271 1.1 thorpej return YPERR_RPC;
272 1.1 thorpej
273 1.1 thorpej return 0;
274 1.1 thorpej }
275 1.1 thorpej
276 1.1 thorpej int
277 1.1 thorpej yp_order_host(client, indomain, inmap, outorder)
278 1.1 thorpej CLIENT *client;
279 1.1 thorpej char *indomain;
280 1.1 thorpej char *inmap;
281 1.1 thorpej int *outorder;
282 1.1 thorpej {
283 1.1 thorpej struct ypresp_order ypro;
284 1.1 thorpej struct ypreq_nokey yprnk;
285 1.1 thorpej int r;
286 1.1 thorpej
287 1.1 thorpej yprnk.domain = indomain;
288 1.1 thorpej yprnk.map = inmap;
289 1.1 thorpej
290 1.1 thorpej memset(&ypro, 0, sizeof ypro);
291 1.1 thorpej
292 1.1 thorpej r = clnt_call(client, YPPROC_ORDER, xdr_ypreq_nokey, &yprnk,
293 1.1 thorpej xdr_ypresp_order, &ypro, _yplib_host_timeout);
294 1.1 thorpej if(r != RPC_SUCCESS)
295 1.1 thorpej clnt_perror(client, "yp_order_host: clnt_call");
296 1.1 thorpej
297 1.1 thorpej *outorder = ypro.ordernum;
298 1.1 thorpej xdr_free(xdr_ypresp_order, (char *)&ypro);
299 1.1 thorpej return ypprot_err(ypro.status);
300 1.1 thorpej }
301 1.1 thorpej
302 1.1 thorpej int
303 1.1 thorpej yp_master_host(client, indomain, inmap, outname)
304 1.1 thorpej CLIENT *client;
305 1.1 thorpej char *indomain;
306 1.1 thorpej char *inmap;
307 1.1 thorpej char **outname;
308 1.1 thorpej {
309 1.1 thorpej struct ypresp_master yprm;
310 1.1 thorpej struct ypreq_nokey yprnk;
311 1.1 thorpej int r;
312 1.1 thorpej
313 1.1 thorpej yprnk.domain = indomain;
314 1.1 thorpej yprnk.map = inmap;
315 1.1 thorpej
316 1.1 thorpej memset(&yprm, 0, sizeof yprm);
317 1.1 thorpej
318 1.1 thorpej r = clnt_call(client, YPPROC_MASTER, xdr_ypreq_nokey, &yprnk,
319 1.1 thorpej xdr_ypresp_master, &yprm, _yplib_host_timeout);
320 1.1 thorpej if (r != RPC_SUCCESS)
321 1.1 thorpej clnt_perror(client, "yp_master: clnt_call");
322 1.1 thorpej
323 1.1 thorpej if(!(r=ypprot_err(yprm.status)) ) {
324 1.1 thorpej *outname = (char *)strdup(yprm.master);
325 1.1 thorpej }
326 1.1 thorpej xdr_free(xdr_ypresp_master, (char *)&yprm);
327 1.1 thorpej return r;
328 1.1 thorpej }
329 1.1 thorpej
330 1.1 thorpej int
331 1.1 thorpej yp_maplist_host(client, indomain, outmaplist)
332 1.1 thorpej CLIENT *client;
333 1.1 thorpej char *indomain;
334 1.1 thorpej struct ypmaplist **outmaplist;
335 1.1 thorpej {
336 1.1 thorpej struct ypresp_maplist ypml;
337 1.1 thorpej int r;
338 1.1 thorpej
339 1.1 thorpej memset(&ypml, 0, sizeof ypml);
340 1.1 thorpej
341 1.1 thorpej r = clnt_call(client, YPPROC_MAPLIST, xdr_ypdomain_wrap_string,
342 1.1 thorpej indomain, xdr_ypresp_maplist, &ypml, _yplib_host_timeout);
343 1.1 thorpej if (r != RPC_SUCCESS)
344 1.1 thorpej clnt_perror(client, "yp_maplist: clnt_call");
345 1.1 thorpej
346 1.1 thorpej *outmaplist = ypml.list;
347 1.1 thorpej /* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
348 1.1 thorpej return ypprot_err(ypml.status);
349 1.1 thorpej }
350