1 /* $NetBSD: yplib.c,v 1.45 2012/03/20 16:30:26 matt Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca> 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #if defined(LIBC_SCCS) && !defined(lint) 31 __RCSID("$NetBSD: yplib.c,v 1.45 2012/03/20 16:30:26 matt Exp $"); 32 #endif 33 34 #include "namespace.h" 35 #include "reentrant.h" 36 37 #include <sys/param.h> 38 #include <sys/socket.h> 39 #include <sys/file.h> 40 #include <sys/uio.h> 41 42 #include <arpa/nameser.h> 43 44 #include <assert.h> 45 #include <errno.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 #include <rpc/rpc.h> 52 #include <rpc/xdr.h> 53 #include <rpcsvc/yp_prot.h> 54 #include <rpcsvc/ypclnt.h> 55 #include "local.h" 56 57 #define BINDINGDIR "/var/yp/binding" 58 #define YPBINDLOCK "/var/run/ypbind.lock" 59 60 struct dom_binding *_ypbindlist; 61 char _yp_domain[MAXHOSTNAMELEN]; 62 63 #define YPLIB_TIMEOUT 10 64 #define YPLIB_RPC_RETRIES 4 65 66 struct timeval _yplib_timeout = { YPLIB_TIMEOUT, 0 }; 67 struct timeval _yplib_rpc_timeout = { YPLIB_TIMEOUT / YPLIB_RPC_RETRIES, 68 1000000 * (YPLIB_TIMEOUT % YPLIB_RPC_RETRIES) / YPLIB_RPC_RETRIES }; 69 int _yplib_nerrs = 5; 70 int _yplib_bindtries = 0; 71 72 #ifdef __weak_alias 73 __weak_alias(yp_bind, _yp_bind) 74 __weak_alias(yp_unbind, _yp_unbind) 75 __weak_alias(yp_get_default_domain, _yp_get_default_domain) 76 __weak_alias(yp_setbindtries, _yp_setbindtries) 77 #endif 78 79 #ifdef _REENTRANT 80 static mutex_t _ypmutex = MUTEX_INITIALIZER; 81 #define YPLOCK() mutex_lock(&_ypmutex) 82 #define YPUNLOCK() mutex_unlock(&_ypmutex) 83 #else 84 #define YPLOCK() 85 #define YPUNLOCK() 86 #endif 87 88 int 89 yp_setbindtries(int ntries) 90 { 91 int old_val = _yplib_bindtries; 92 93 if (ntries >= 0) 94 _yplib_bindtries = ntries; 95 return old_val; 96 } 97 98 int 99 _yp_dobind(const char *dom, struct dom_binding **ypdb) 100 { 101 static int pid = -1; 102 char path[MAXPATHLEN]; 103 struct dom_binding *ysd, *ysd2; 104 struct ypbind_resp ypbr; 105 struct sockaddr_in clnt_sin; 106 int clnt_sock, fd, gpid; 107 CLIENT *client; 108 int new = 0; 109 int nerrs = 0; 110 ssize_t r; 111 112 if (dom == NULL || *dom == '\0') 113 return YPERR_BADARGS; 114 115 /* 116 * test if YP is running or not 117 */ 118 if ((fd = open(YPBINDLOCK, O_RDONLY)) == -1) 119 return YPERR_YPBIND; 120 if (!(flock(fd, LOCK_EX | LOCK_NB) == -1 && errno == EWOULDBLOCK)) { 121 (void)close(fd); 122 return YPERR_YPBIND; 123 } 124 (void)close(fd); 125 126 gpid = getpid(); 127 if (!(pid == -1 || pid == gpid)) { 128 ysd = _ypbindlist; 129 while (ysd) { 130 if (ysd->dom_client) 131 clnt_destroy(ysd->dom_client); 132 ysd2 = ysd->dom_pnext; 133 free(ysd); 134 ysd = ysd2; 135 } 136 _ypbindlist = NULL; 137 } 138 pid = gpid; 139 140 if (ypdb != NULL) 141 *ypdb = NULL; 142 143 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext) 144 if (strcmp(dom, ysd->dom_domain) == 0) 145 break; 146 if (ysd == NULL) { 147 if ((ysd = malloc(sizeof *ysd)) == NULL) 148 return YPERR_YPERR; 149 (void)memset(ysd, 0, sizeof *ysd); 150 ysd->dom_socket = -1; 151 ysd->dom_vers = 0; 152 new = 1; 153 } 154 again: 155 if (ysd->dom_vers == 0) { 156 (void) snprintf(path, sizeof(path), "%s/%s.%d", 157 BINDINGDIR, dom, 2); 158 if ((fd = open(path, O_RDONLY)) == -1) { 159 /* 160 * no binding file, YP is dead, or not yet fully 161 * alive. 162 */ 163 goto trynet; 164 } 165 if (flock(fd, LOCK_EX | LOCK_NB) == -1 && 166 errno == EWOULDBLOCK) { 167 struct iovec iov[2]; 168 struct ypbind_resp ybr; 169 u_short ypb_port; 170 struct ypbind_binding *bn; 171 172 iov[0].iov_base = &ypb_port; 173 iov[0].iov_len = sizeof ypb_port; 174 iov[1].iov_base = &ybr; 175 iov[1].iov_len = sizeof ybr; 176 177 r = readv(fd, iov, 2); 178 if (r != (ssize_t)(iov[0].iov_len + iov[1].iov_len)) { 179 (void)close(fd); 180 ysd->dom_vers = -1; 181 goto again; 182 } 183 (void)memset(&ysd->dom_server_addr, 0, 184 sizeof ysd->dom_server_addr); 185 ysd->dom_server_addr.sin_len = 186 sizeof(struct sockaddr_in); 187 ysd->dom_server_addr.sin_family = AF_INET; 188 bn = &ybr.ypbind_respbody.ypbind_bindinfo; 189 ysd->dom_server_addr.sin_port = 190 bn->ypbind_binding_port; 191 192 ysd->dom_server_addr.sin_addr = 193 bn->ypbind_binding_addr; 194 195 ysd->dom_server_port = ysd->dom_server_addr.sin_port; 196 (void)close(fd); 197 goto gotit; 198 } else { 199 /* no lock on binding file, YP is dead. */ 200 (void)close(fd); 201 if (new) 202 free(ysd); 203 return YPERR_YPBIND; 204 } 205 } 206 trynet: 207 if (ysd->dom_vers == -1 || ysd->dom_vers == 0) { 208 struct ypbind_binding *bn; 209 (void)memset(&clnt_sin, 0, sizeof clnt_sin); 210 clnt_sin.sin_len = sizeof(struct sockaddr_in); 211 clnt_sin.sin_family = AF_INET; 212 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 213 214 clnt_sock = RPC_ANYSOCK; 215 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, 216 &clnt_sock, 0, 0); 217 if (client == NULL) { 218 clnt_pcreateerror("clnttcp_create"); 219 if (new) 220 free(ysd); 221 return YPERR_YPBIND; 222 } 223 r = clnt_call(client, (rpcproc_t)YPBINDPROC_DOMAIN, 224 (xdrproc_t)xdr_ypdomain_wrap_string, &dom, 225 (xdrproc_t)xdr_ypbind_resp, &ypbr, _yplib_timeout); 226 if (r != RPC_SUCCESS) { 227 if (_yplib_bindtries <= 0 && new == 0 && 228 ++nerrs == _yplib_nerrs) { 229 nerrs = 0; 230 fprintf(stderr, 231 "YP server for domain %s not responding, still trying\n", 232 dom); 233 } 234 else if (_yplib_bindtries > 0 && 235 ++nerrs == _yplib_bindtries) { 236 free(ysd); 237 return YPERR_YPBIND; 238 } 239 clnt_destroy(client); 240 ysd->dom_vers = -1; 241 goto again; 242 } 243 clnt_destroy(client); 244 245 (void)memset(&ysd->dom_server_addr, 0, 246 sizeof ysd->dom_server_addr); 247 ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in); 248 ysd->dom_server_addr.sin_family = AF_INET; 249 bn = &ypbr.ypbind_respbody.ypbind_bindinfo; 250 ysd->dom_server_addr.sin_port = 251 bn->ypbind_binding_port; 252 ysd->dom_server_addr.sin_addr.s_addr = 253 bn->ypbind_binding_addr.s_addr; 254 ysd->dom_server_port = 255 bn->ypbind_binding_port; 256 gotit: 257 ysd->dom_vers = YPVERS; 258 (void)strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain)); 259 } 260 if (ysd->dom_client) 261 clnt_destroy(ysd->dom_client); 262 ysd->dom_socket = RPC_ANYSOCK; 263 ysd->dom_client = clntudp_create(&ysd->dom_server_addr, 264 YPPROG, YPVERS, _yplib_rpc_timeout, &ysd->dom_socket); 265 if (ysd->dom_client == NULL) { 266 clnt_pcreateerror("clntudp_create"); 267 ysd->dom_vers = -1; 268 goto again; 269 } 270 if (fcntl(ysd->dom_socket, F_SETFD, FD_CLOEXEC) == -1) 271 perror("fcntl: F_SETFD"); 272 273 if (new) { 274 ysd->dom_pnext = _ypbindlist; 275 _ypbindlist = ysd; 276 } 277 if (ypdb != NULL) 278 *ypdb = ysd; 279 return 0; 280 } 281 282 void 283 __yp_unbind(struct dom_binding *ypb) 284 { 285 286 _DIAGASSERT(ypb != NULL); 287 288 clnt_destroy(ypb->dom_client); 289 ypb->dom_client = NULL; 290 ypb->dom_socket = -1; 291 } 292 293 int 294 yp_bind(const char *dom) 295 { 296 if (_yp_invalid_domain(dom)) 297 return YPERR_BADARGS; 298 299 return _yp_dobind(dom, NULL); 300 } 301 302 void 303 yp_unbind(const char *dom) 304 { 305 struct dom_binding *ypb, *ypbp; 306 307 if (_yp_invalid_domain(dom)) 308 return; 309 310 ypbp = NULL; 311 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) { 312 if (strcmp(dom, ypb->dom_domain) == 0) { 313 clnt_destroy(ypb->dom_client); 314 if (ypbp) 315 ypbp->dom_pnext = ypb->dom_pnext; 316 else 317 _ypbindlist = ypb->dom_pnext; 318 free(ypb); 319 return; 320 } 321 ypbp = ypb; 322 } 323 return; 324 } 325 326 int 327 yp_get_default_domain(char **domp) 328 { 329 if (domp == NULL) 330 return YPERR_BADARGS; 331 *domp = NULL; 332 if (_yp_domain[0] == '\0') 333 if (getdomainname(_yp_domain, sizeof _yp_domain)) 334 return YPERR_NODOM; 335 *domp = _yp_domain; 336 return 0; 337 } 338 339 int 340 _yp_check(char **dom) 341 { 342 char *unused; 343 int good; 344 345 YPLOCK(); 346 347 if (_yp_domain[0] == '\0') 348 if (yp_get_default_domain(&unused)) { 349 good = 0; 350 goto done; 351 } 352 if (dom) 353 *dom = _yp_domain; 354 355 good = yp_bind(_yp_domain) == 0; 356 done: 357 YPUNLOCK(); 358 return good; 359 } 360 361 /* 362 * _yp_invalid_domain: check if given domainname isn't legal. 363 * returns non-zero if invalid 364 */ 365 int 366 _yp_invalid_domain(const char *dom) 367 { 368 if (dom == NULL || *dom == '\0') 369 return 1; 370 371 if (strlen(dom) > YPMAXDOMAIN) 372 return 1; 373 374 if (strchr(dom, '/') != NULL) 375 return 1; 376 377 return 0; 378 } 379