]>
Commit | Line | Data |
---|---|---|
b7080c8e A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
2b484d24 A |
6 | * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.0 (the 'License'). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
b7080c8e A |
13 | * |
14 | * The Original Code and all software distributed under the License are | |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2b484d24 A |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License." | |
b7080c8e A |
21 | * |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | /* $OpenBSD: yplib_host.c,v 1.7 1997/06/23 01:11:12 deraadt Exp $ */ | |
25 | ||
26 | /* | |
27 | * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com> | |
28 | * All rights reserved. | |
29 | * | |
30 | * Redistribution and use in source and binary forms, with or without | |
31 | * modification, are permitted provided that the following conditions | |
32 | * are met: | |
33 | * 1. Redistributions of source code must retain the above copyright | |
34 | * notice, this list of conditions and the following disclaimer. | |
35 | * 2. Redistributions in binary form must reproduce the above copyright | |
36 | * notice, this list of conditions and the following disclaimer in the | |
37 | * documentation and/or other materials provided with the distribution. | |
38 | * 3. All advertising materials mentioning features or use of this software | |
39 | * must display the following acknowledgement: | |
40 | * This product includes software developed by Theo de Raadt. | |
41 | * 4. The name of the author may not be used to endorse or promote products | |
42 | * derived from this software without specific prior written permission. | |
43 | * | |
44 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |
45 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
46 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
47 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
48 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
54 | * SUCH DAMAGE. | |
55 | */ | |
56 | ||
57 | #ifndef LINT | |
58 | static char *rcsid = "$OpenBSD: yplib_host.c,v 1.7 1997/06/23 01:11:12 deraadt Exp $"; | |
59 | #endif | |
60 | ||
61 | #include <sys/param.h> | |
62 | #include <sys/types.h> | |
63 | #include <sys/socket.h> | |
64 | #include <sys/file.h> | |
65 | #include <sys/uio.h> | |
66 | #include <errno.h> | |
67 | #include <stdio.h> | |
68 | #include <stdlib.h> | |
69 | #include <string.h> | |
70 | #include <netinet/in.h> | |
71 | #include <arpa/inet.h> | |
72 | #include <sys/socket.h> | |
73 | #include <netdb.h> | |
74 | #include <unistd.h> | |
75 | #include <rpc/rpc.h> | |
76 | #include <rpc/xdr.h> | |
77 | #include <rpcsvc/yp.h> | |
78 | #include <rpcsvc/ypclnt.h> | |
79 | ||
80 | extern bool_t xdr_domainname(), xdr_ypbind_resp(); | |
81 | extern bool_t xdr_ypreq_key(), xdr_ypresp_val(); | |
82 | extern bool_t xdr_ypreq_nokey(), xdr_ypresp_key_val(); | |
83 | extern bool_t xdr_ypresp_all(), xdr_ypresp_all_seq(); | |
84 | extern bool_t xdr_ypresp_master(); | |
85 | ||
86 | extern int (*ypresp_allfn)(); | |
87 | extern void *ypresp_data; | |
88 | ||
89 | int _yplib_host_timeout = 10; | |
90 | ||
91 | CLIENT * | |
92 | yp_bind_host(server,program,version,port,usetcp) | |
93 | char *server; | |
94 | u_long program,version; | |
95 | u_short port; | |
96 | int usetcp; | |
97 | { | |
98 | struct sockaddr_in rsrv_sin; | |
99 | int rsrv_sock; | |
100 | struct hostent *h; | |
101 | struct timeval tv; | |
102 | static CLIENT *client; | |
103 | ||
104 | memset(&rsrv_sin, 0, sizeof rsrv_sin); | |
105 | rsrv_sin.sin_len = sizeof rsrv_sin; | |
106 | rsrv_sin.sin_family = AF_INET; | |
107 | rsrv_sock = RPC_ANYSOCK; | |
108 | if (port != 0) { | |
109 | rsrv_sin.sin_port = htons(port); | |
110 | } | |
111 | ||
112 | if ((*server >= '0') && (*server <= '9')) { | |
113 | if(inet_aton(server,&rsrv_sin.sin_addr) == 0) { | |
114 | fprintf(stderr, "inet_aton: invalid address %s.\n", | |
115 | server); | |
116 | exit(1); | |
117 | } | |
118 | } else { | |
119 | h = gethostbyname(server); | |
120 | if(h == NULL) { | |
121 | fprintf(stderr, "gethostbyname: unknown host %s.\n", | |
122 | server); | |
123 | exit(1); | |
124 | } | |
125 | rsrv_sin.sin_addr.s_addr = *(u_int32_t *)h->h_addr; | |
126 | } | |
127 | ||
128 | tv.tv_sec = 10; | |
129 | tv.tv_usec = 0; | |
130 | ||
131 | if (usetcp) { | |
132 | client = clnttcp_create(&rsrv_sin, program, version, | |
133 | &rsrv_sock, 0, 0); | |
134 | } else { | |
135 | client = clntudp_create(&rsrv_sin, program, version, tv, | |
136 | &rsrv_sock); | |
137 | } | |
138 | ||
139 | if (client == NULL) { | |
140 | fprintf(stderr, "clntudp_create: no contact with host %s.\n", | |
141 | server); | |
142 | exit(1); | |
143 | } | |
144 | ||
145 | return(client); | |
146 | ||
147 | } | |
148 | ||
149 | CLIENT * | |
150 | yp_bind_local(program,version) | |
151 | u_long program,version; | |
152 | { | |
153 | struct sockaddr_in rsrv_sin; | |
154 | int rsrv_sock; | |
155 | struct timeval tv; | |
156 | static CLIENT *client; | |
157 | ||
158 | memset(&rsrv_sin, 0, sizeof rsrv_sin); | |
159 | rsrv_sin.sin_len = sizeof rsrv_sin; | |
160 | rsrv_sin.sin_family = AF_INET; | |
161 | rsrv_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
162 | rsrv_sock = RPC_ANYSOCK; | |
163 | ||
164 | tv.tv_sec = 10; | |
165 | tv.tv_usec = 0; | |
166 | ||
167 | client = clntudp_create(&rsrv_sin, program, version, tv, &rsrv_sock); | |
168 | if (client == NULL) { | |
169 | fprintf(stderr,"clntudp_create: no contact with localhost.\n"); | |
170 | exit(1); | |
171 | } | |
172 | ||
173 | return(client); | |
174 | ||
175 | } | |
176 | ||
177 | int | |
178 | yp_match_host(client, indomain, inmap, inkey, inkeylen, outval, outvallen) | |
179 | CLIENT *client; | |
180 | char *indomain; | |
181 | char *inmap; | |
182 | const char *inkey; | |
183 | int inkeylen; | |
184 | char **outval; | |
185 | int *outvallen; | |
186 | { | |
187 | struct ypresp_val yprv; | |
188 | struct timeval tv; | |
189 | struct ypreq_key yprk; | |
190 | int r; | |
191 | ||
192 | *outval = NULL; | |
193 | *outvallen = 0; | |
194 | ||
195 | tv.tv_sec = _yplib_host_timeout; | |
196 | tv.tv_usec = 0; | |
197 | ||
198 | yprk.domain = indomain; | |
199 | yprk.map = inmap; | |
200 | yprk.key.keydat_val = (char *)inkey; | |
201 | yprk.key.keydat_len = inkeylen; | |
202 | ||
203 | memset(&yprv, 0, sizeof yprv); | |
204 | ||
205 | r = clnt_call(client, YPPROC_MATCH, | |
206 | xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv); | |
207 | if(r != RPC_SUCCESS) { | |
208 | clnt_perror(client, "yp_match_host: clnt_call"); | |
209 | } | |
210 | if( !(r=ypprot_err(yprv.stat)) ) { | |
211 | *outvallen = yprv.val.valdat_len; | |
212 | *outval = (char *)malloc(*outvallen+1); | |
213 | memcpy(*outval, yprv.val.valdat_val, *outvallen); | |
214 | (*outval)[*outvallen] = '\0'; | |
215 | } | |
216 | xdr_free(xdr_ypresp_val, (char *)&yprv); | |
217 | return r; | |
218 | } | |
219 | ||
220 | int | |
221 | yp_first_host(client, indomain, inmap, outkey, outkeylen, outval, outvallen) | |
222 | CLIENT *client; | |
223 | char *indomain; | |
224 | char *inmap; | |
225 | char **outkey; | |
226 | int *outkeylen; | |
227 | char **outval; | |
228 | int *outvallen; | |
229 | { | |
230 | struct ypresp_key_val yprkv; | |
231 | struct ypreq_nokey yprnk; | |
232 | struct timeval tv; | |
233 | int r; | |
234 | ||
235 | *outkey = *outval = NULL; | |
236 | *outkeylen = *outvallen = 0; | |
237 | ||
238 | tv.tv_sec = _yplib_host_timeout; | |
239 | tv.tv_usec = 0; | |
240 | ||
241 | yprnk.domain = indomain; | |
242 | yprnk.map = inmap; | |
243 | memset(&yprkv, 0, sizeof yprkv); | |
244 | ||
245 | r = clnt_call(client, YPPROC_FIRST, | |
246 | xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv); | |
247 | if(r != RPC_SUCCESS) { | |
248 | clnt_perror(client, "yp_first_host: clnt_call"); | |
249 | } | |
250 | if( !(r=ypprot_err(yprkv.stat)) ) { | |
251 | *outkeylen = yprkv.key.keydat_len; | |
252 | *outkey = (char *)malloc(*outkeylen+1); | |
253 | memcpy(*outkey, yprkv.key.keydat_val, *outkeylen); | |
254 | (*outkey)[*outkeylen] = '\0'; | |
255 | *outvallen = yprkv.val.valdat_len; | |
256 | *outval = (char *)malloc(*outvallen+1); | |
257 | memcpy(*outval, yprkv.val.valdat_val, *outvallen); | |
258 | (*outval)[*outvallen] = '\0'; | |
259 | } | |
260 | xdr_free(xdr_ypresp_key_val, (char *)&yprkv); | |
261 | return r; | |
262 | } | |
263 | ||
264 | int | |
265 | yp_next_host(client, indomain, inmap, inkey, inkeylen, outkey, outkeylen, outval, outvallen) | |
266 | CLIENT *client; | |
267 | char *indomain; | |
268 | char *inmap; | |
269 | char *inkey; | |
270 | int inkeylen; | |
271 | char **outkey; | |
272 | int *outkeylen; | |
273 | char **outval; | |
274 | int *outvallen; | |
275 | { | |
276 | struct ypresp_key_val yprkv; | |
277 | struct ypreq_key yprk; | |
278 | struct timeval tv; | |
279 | int r; | |
280 | ||
281 | *outkey = *outval = NULL; | |
282 | *outkeylen = *outvallen = 0; | |
283 | ||
284 | tv.tv_sec = _yplib_host_timeout; | |
285 | tv.tv_usec = 0; | |
286 | ||
287 | yprk.domain = indomain; | |
288 | yprk.map = inmap; | |
289 | yprk.key.keydat_val = inkey; | |
290 | yprk.key.keydat_len = inkeylen; | |
291 | memset(&yprkv, 0, sizeof yprkv); | |
292 | ||
293 | r = clnt_call(client, YPPROC_NEXT, | |
294 | xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv); | |
295 | if(r != RPC_SUCCESS) { | |
296 | clnt_perror(client, "yp_next_host: clnt_call"); | |
297 | } | |
298 | if( !(r=ypprot_err(yprkv.stat)) ) { | |
299 | *outkeylen = yprkv.key.keydat_len; | |
300 | *outkey = (char *)malloc(*outkeylen+1); | |
301 | memcpy(*outkey, yprkv.key.keydat_val, *outkeylen); | |
302 | (*outkey)[*outkeylen] = '\0'; | |
303 | *outvallen = yprkv.val.valdat_len; | |
304 | *outval = (char *)malloc(*outvallen+1); | |
305 | memcpy(*outval, yprkv.val.valdat_val, *outvallen); | |
306 | (*outval)[*outvallen] = '\0'; | |
307 | } | |
308 | xdr_free(xdr_ypresp_key_val, (char *)&yprkv); | |
309 | return r; | |
310 | } | |
311 | ||
312 | int | |
313 | yp_all_host(client, indomain, inmap, incallback) | |
314 | CLIENT *client; | |
315 | char *indomain; | |
316 | char *inmap; | |
317 | struct ypall_callback *incallback; | |
318 | { | |
319 | struct ypreq_nokey yprnk; | |
320 | struct timeval tv; | |
321 | u_long status; | |
322 | ||
323 | tv.tv_sec = _yplib_host_timeout; | |
324 | tv.tv_usec = 0; | |
325 | ||
326 | yprnk.domain = indomain; | |
327 | yprnk.map = inmap; | |
328 | ypresp_allfn = incallback->foreach; | |
329 | ypresp_data = (void *)incallback->data; | |
330 | ||
331 | (void) clnt_call(client, YPPROC_ALL, | |
332 | xdr_ypreq_nokey, &yprnk, xdr_ypresp_all_seq, &status, tv); | |
333 | xdr_free(xdr_ypresp_all_seq, (char *)&status); /* not really needed... */ | |
334 | ||
335 | if(status != YP_FALSE) | |
336 | return ypprot_err(status); | |
337 | return 0; | |
338 | } | |
339 | ||
340 | int | |
341 | yp_order_host(client, indomain, inmap, outorder) | |
342 | CLIENT *client; | |
343 | char *indomain; | |
344 | char *inmap; | |
345 | u_int32_t *outorder; | |
346 | { | |
347 | struct ypresp_order ypro; | |
348 | struct ypreq_nokey yprnk; | |
349 | struct timeval tv; | |
350 | int r; | |
351 | ||
352 | tv.tv_sec = _yplib_host_timeout; | |
353 | tv.tv_usec = 0; | |
354 | ||
355 | yprnk.domain = indomain; | |
356 | yprnk.map = inmap; | |
357 | ||
358 | memset(&ypro, 0, sizeof ypro); | |
359 | ||
360 | r = clnt_call(client, YPPROC_ORDER, | |
361 | xdr_ypreq_nokey, &yprnk, xdr_ypresp_order, &ypro, tv); | |
362 | if(r != RPC_SUCCESS) { | |
363 | clnt_perror(client, "yp_order_host: clnt_call"); | |
364 | } | |
365 | ||
366 | *outorder = ypro.ordernum; | |
367 | xdr_free(xdr_ypresp_order, (char *)&ypro); | |
368 | return ypprot_err(ypro.stat); | |
369 | } | |
370 | ||
371 | int | |
372 | yp_master_host(client, indomain, inmap, outname) | |
373 | CLIENT *client; | |
374 | char *indomain; | |
375 | char *inmap; | |
376 | char **outname; | |
377 | { | |
378 | struct ypresp_master yprm; | |
379 | struct ypreq_nokey yprnk; | |
380 | struct timeval tv; | |
381 | int r; | |
382 | ||
383 | tv.tv_sec = _yplib_host_timeout; | |
384 | tv.tv_usec = 0; | |
385 | ||
386 | yprnk.domain = indomain; | |
387 | yprnk.map = inmap; | |
388 | ||
389 | memset(&yprm, 0, sizeof yprm); | |
390 | ||
391 | r = clnt_call(client, YPPROC_MASTER, | |
392 | xdr_ypreq_nokey, &yprnk, xdr_ypresp_master, &yprm, tv); | |
393 | if(r != RPC_SUCCESS) { | |
394 | clnt_perror(client, "yp_master: clnt_call"); | |
395 | } | |
396 | if( !(r=ypprot_err(yprm.stat)) ) { | |
397 | *outname = (char *)strdup(yprm.peer); | |
398 | } | |
399 | xdr_free(xdr_ypresp_master, (char *)&yprm); | |
400 | return r; | |
401 | } | |
402 | ||
403 | int | |
404 | yp_maplist_host(client, indomain, outmaplist) | |
405 | CLIENT *client; | |
406 | char *indomain; | |
407 | struct ypmaplist **outmaplist; | |
408 | { | |
409 | struct ypresp_maplist ypml; | |
410 | struct timeval tv; | |
411 | int r; | |
412 | ||
413 | tv.tv_sec = _yplib_host_timeout; | |
414 | tv.tv_usec = 0; | |
415 | ||
416 | memset(&ypml, 0, sizeof ypml); | |
417 | ||
418 | r = clnt_call(client, YPPROC_MAPLIST, | |
419 | xdr_domainname, &indomain, xdr_ypresp_maplist, &ypml, tv); | |
420 | if (r != RPC_SUCCESS) { | |
421 | clnt_perror(client, "yp_maplist: clnt_call"); | |
422 | } | |
423 | *outmaplist = ypml.maps; | |
424 | /* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/ | |
425 | return ypprot_err(ypml.stat); | |
426 | } | |
427 |