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