1 /* $OpenBSD: yp_bind.c,v 1.17 2009/06/05 17:19:00 schwarze 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/param.h> 29 #include <sys/types.h> 30 #include <sys/socket.h> 31 #include <sys/uio.h> 32 #include <errno.h> 33 #include <fcntl.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <unistd.h> 38 #include <rpc/rpc.h> 39 #include <rpc/xdr.h> 40 #include <rpcsvc/yp.h> 41 #include <rpcsvc/ypclnt.h> 42 #include "ypinternal.h" 43 44 struct dom_binding *_ypbindlist; 45 char _yp_domain[MAXHOSTNAMELEN]; 46 int _yplib_timeout = 10; 47 48 int 49 _yp_dobind(const char *dom, struct dom_binding **ypdb) 50 { 51 static pid_t pid = -1; 52 char path[MAXPATHLEN]; 53 struct dom_binding *ysd, *ysd2; 54 struct ypbind_resp ypbr; 55 struct timeval tv; 56 struct sockaddr_in clnt_sin; 57 struct ypbind_binding *bn; 58 int clnt_sock, fd; 59 pid_t gpid; 60 CLIENT *client; 61 int new = 0, r; 62 int count = 0; 63 u_short port; 64 65 /* 66 * test if YP is running or not 67 */ 68 if ((fd = open(YPBINDLOCK, O_RDONLY)) == -1) 69 return YPERR_YPBIND; 70 if (!(flock(fd, LOCK_EX | LOCK_NB) == -1 && errno == EWOULDBLOCK)) { 71 (void)close(fd); 72 return YPERR_YPBIND; 73 } 74 (void)close(fd); 75 76 gpid = getpid(); 77 if (!(pid == -1 || pid == gpid)) { 78 ysd = _ypbindlist; 79 while (ysd) { 80 if (ysd->dom_client) 81 clnt_destroy(ysd->dom_client); 82 ysd2 = ysd->dom_pnext; 83 free(ysd); 84 ysd = ysd2; 85 } 86 _ypbindlist = NULL; 87 } 88 pid = gpid; 89 90 if (ypdb != NULL) 91 *ypdb = NULL; 92 93 if (dom == NULL || strlen(dom) == 0) 94 return YPERR_BADARGS; 95 96 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext) 97 if (strcmp(dom, ysd->dom_domain) == 0) 98 break; 99 if (ysd == NULL) { 100 if ((ysd = malloc(sizeof *ysd)) == NULL) 101 return YPERR_RESRC; 102 (void)memset(ysd, 0, sizeof *ysd); 103 ysd->dom_socket = -1; 104 ysd->dom_vers = 0; 105 new = 1; 106 } 107 again: 108 if (ysd->dom_vers == 0) { 109 r = snprintf(path, sizeof(path), "%s/%s.%d", 110 BINDINGDIR, dom, 2); 111 if (r < 0 || r >= sizeof(path)) { 112 if (new) 113 free(ysd); 114 return YPERR_BADARGS; 115 } 116 if ((fd = open(path, O_RDONLY)) == -1) { 117 /* 118 * no binding file, YP is dead, or not yet fully 119 * alive. 120 */ 121 goto trynet; 122 } 123 if (flock(fd, LOCK_EX | LOCK_NB) == -1 && 124 errno == EWOULDBLOCK) { 125 struct iovec iov[2]; 126 u_short ypb_port; 127 128 /* 129 * we fetch the ypbind port number, but do 130 * nothing with it. 131 */ 132 iov[0].iov_base = (caddr_t) &ypb_port; 133 iov[0].iov_len = sizeof ypb_port; 134 iov[1].iov_base = (caddr_t) &ypbr; 135 iov[1].iov_len = sizeof ypbr; 136 137 r = readv(fd, iov, 2); 138 if (r != iov[0].iov_len + iov[1].iov_len) { 139 (void)close(fd); 140 ysd->dom_vers = -1; 141 goto again; 142 } 143 (void)close(fd); 144 goto gotdata; 145 } else { 146 /* no lock on binding file, YP is dead. */ 147 (void)close(fd); 148 if (new) 149 free(ysd); 150 return YPERR_YPBIND; 151 } 152 } 153 trynet: 154 if (ysd->dom_vers == -1 || ysd->dom_vers == 0) { 155 (void)memset(&clnt_sin, 0, sizeof clnt_sin); 156 clnt_sin.sin_len = sizeof(struct sockaddr_in); 157 clnt_sin.sin_family = AF_INET; 158 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 159 160 clnt_sock = RPC_ANYSOCK; 161 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, 162 &clnt_sock, 0, 0); 163 if (client == NULL) { 164 clnt_pcreateerror("clnttcp_create"); 165 if (new) 166 free(ysd); 167 switch (rpc_createerr.cf_error.re_errno) { 168 case ECONNREFUSED: 169 return YPERR_YPBIND; 170 case ENOMEM: 171 return YPERR_RESRC; 172 default: 173 return YPERR_YPERR; 174 } 175 } 176 if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED || 177 ntohs(clnt_sin.sin_port) == 20) { 178 /* 179 * YP was not running, but someone has registered 180 * ypbind with portmap -- this simply means YP is 181 * not running. 182 */ 183 clnt_destroy(client); 184 if (new) 185 free(ysd); 186 return YPERR_YPBIND; 187 } 188 tv.tv_sec = _yplib_timeout; 189 tv.tv_usec = 0; 190 r = clnt_call(client, YPBINDPROC_DOMAIN, xdr_domainname, 191 &dom, xdr_ypbind_resp, &ypbr, tv); 192 if (r != RPC_SUCCESS) { 193 if (new == 0 || count) 194 fprintf(stderr, 195 "YP server for domain %s not responding, still trying\n", 196 dom); 197 count++; 198 clnt_destroy(client); 199 ysd->dom_vers = -1; 200 goto again; 201 } 202 clnt_destroy(client); 203 gotdata: 204 bn = &ypbr.ypbind_resp_u.ypbind_bindinfo; 205 memcpy(&port, &bn->ypbind_binding_port, sizeof port); 206 if (ntohs(port) >= IPPORT_RESERVED || 207 ntohs(port) == 20) { 208 /* 209 * This is bullshit -- the ypbind wants me to 210 * communicate to an insecure ypserv. We are 211 * within rights to syslog this as an attack, 212 * but for now we'll simply ignore it; real YP 213 * is obviously not running. 214 */ 215 if (new) 216 free(ysd); 217 return YPERR_YPBIND; 218 } 219 (void)memset(&ysd->dom_server_addr, 0, 220 sizeof ysd->dom_server_addr); 221 ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in); 222 ysd->dom_server_addr.sin_family = AF_INET; 223 memcpy(&ysd->dom_server_addr.sin_port, 224 &bn->ypbind_binding_port, 225 sizeof(ysd->dom_server_addr.sin_port)); 226 memcpy(&ysd->dom_server_addr.sin_addr.s_addr, 227 &bn->ypbind_binding_addr, 228 sizeof(ysd->dom_server_addr.sin_addr.s_addr)); 229 ysd->dom_server_port = ysd->dom_server_addr.sin_port; 230 ysd->dom_vers = YPVERS; 231 strlcpy(ysd->dom_domain, dom, sizeof ysd->dom_domain); 232 } 233 tv.tv_sec = _yplib_timeout / 2; 234 tv.tv_usec = 0; 235 if (ysd->dom_client) 236 clnt_destroy(ysd->dom_client); 237 ysd->dom_socket = RPC_ANYSOCK; 238 ysd->dom_client = clntudp_create(&ysd->dom_server_addr, 239 YPPROG, YPVERS, tv, &ysd->dom_socket); 240 if (ysd->dom_client == NULL) { 241 clnt_pcreateerror("clntudp_create"); 242 ysd->dom_vers = -1; 243 goto again; 244 } 245 if (fcntl(ysd->dom_socket, F_SETFD, 1) == -1) 246 perror("fcntl: F_SETFD"); 247 248 if (new) { 249 ysd->dom_pnext = _ypbindlist; 250 _ypbindlist = ysd; 251 } 252 if (ypdb != NULL) 253 *ypdb = ysd; 254 return 0; 255 } 256 257 void 258 _yp_unbind(struct dom_binding *ypb) 259 { 260 clnt_destroy(ypb->dom_client); 261 ypb->dom_client = NULL; 262 ypb->dom_socket = -1; 263 } 264 265 int 266 yp_bind(const char *dom) 267 { 268 return _yp_dobind(dom, NULL); 269 } 270 271 void 272 yp_unbind(const char *dom) 273 { 274 struct dom_binding *ypb, *ypbp; 275 276 ypbp = NULL; 277 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) { 278 if (strcmp(dom, ypb->dom_domain) == 0) { 279 clnt_destroy(ypb->dom_client); 280 if (ypbp) 281 ypbp->dom_pnext = ypb->dom_pnext; 282 else 283 _ypbindlist = ypb->dom_pnext; 284 free(ypb); 285 return; 286 } 287 ypbp = ypb; 288 } 289 } 290