1 /* $OpenBSD: yp_bind.c,v 1.31 2022/07/22 05:55:05 jsg Exp $ */ 2 /* 3 * Copyright (c) 1992, 1993, 1996 Theo de Raadt <deraadt@theos.com> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 #include <sys/uio.h> 31 #include <errno.h> 32 #include <fcntl.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 #include <limits.h> 38 #include <paths.h> 39 40 #include <rpc/rpc.h> 41 #include <rpc/xdr.h> 42 #include <rpcsvc/yp.h> 43 #include <rpcsvc/ypclnt.h> 44 #include "ypinternal.h" 45 46 static struct dom_binding *ypbinding; 47 char _yp_domain[HOST_NAME_MAX+1]; 48 int _yplib_timeout = 10; 49 50 int 51 _yp_dobind(const char *dom, struct dom_binding **ypdb) 52 { 53 struct timeval tv; 54 int connected = 1; 55 int s; 56 57 if (dom == NULL || strlen(dom) == 0) 58 return YPERR_BADARGS; 59 60 again: 61 if (ypbinding && ypbinding->dom_client) 62 _yp_unbind(ypbinding); 63 64 s = ypconnect(SOCK_DGRAM); 65 if (s == -1) 66 return YPERR_YPBIND; /* YP not running */ 67 68 free(ypbinding); 69 ypbinding = calloc(1, sizeof *ypbinding); 70 if (ypbinding == NULL) { 71 close(s); 72 return YPERR_RESRC; 73 } 74 ypbinding->dom_socket = s; 75 ypbinding->dom_server_addr.sin_port = -1; /* don't consult portmap */ 76 77 tv.tv_sec = _yplib_timeout / 2; 78 tv.tv_usec = 0; 79 ypbinding->dom_client = clntudp_create(&ypbinding->dom_server_addr, 80 YPPROG, YPVERS, tv, &ypbinding->dom_socket); 81 if (ypbinding->dom_client == NULL) { 82 close(ypbinding->dom_socket); 83 free(ypbinding); 84 ypbinding = NULL; 85 clnt_pcreateerror("clntudp_create"); 86 goto again; 87 } 88 clnt_control(ypbinding->dom_client, CLSET_CONNECTED, &connected); 89 if (ypdb) 90 *ypdb = ypbinding; 91 return 0; 92 } 93 94 void 95 _yp_unbind(struct dom_binding *ypb) 96 { 97 close(ypb->dom_socket); 98 ypb->dom_socket = -1; 99 clnt_destroy(ypb->dom_client); 100 ypb->dom_client = NULL; 101 } 102 103 int 104 yp_bind(const char *dom) 105 { 106 return _yp_dobind(dom, NULL); 107 } 108 DEF_WEAK(yp_bind); 109 110 void 111 yp_unbind(const char *dom) 112 { 113 _yp_unbind(ypbinding); 114 } 115