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