kern.c revision 75631
1/*- 2 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Berkeley Software Design Inc's name may not be used to endorse or 13 * promote products derived from this software without specific prior 14 * written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 20 * FOR ANY 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 * from BSDI kern.c,v 1.2 1998/11/25 22:38:27 don Exp 29 * $FreeBSD: head/usr.sbin/rpc.lockd/kern.c 75631 2001-04-17 20:45:23Z alfred $ 30 */ 31 32#include <sys/param.h> 33#include <sys/mount.h> 34#include <sys/queue.h> 35#include <sys/socket.h> 36#include <sys/stat.h> 37 38#include <err.h> 39#include <errno.h> 40#include <fcntl.h> 41#include <stdio.h> 42#include <stdlib.h> 43#include <string.h> 44#include <syslog.h> 45#include <unistd.h> 46 47#include "nlm_prot.h" 48#include "nfs/nfs_lock.h" 49 50#include "lockd.h" 51#include "lockd_lock.h" 52#include <nfs/rpcv2.h> 53#include <nfs/nfsproto.h> 54#include <nfs/nfs.h> 55 56#define nfslockdans(_v, _ansp) \ 57 ((_ansp)->la_vers = _v, \ 58 nfssvc(NFSSVC_LOCKDANS, _ansp)) 59 60/* Lock request owner. */ 61typedef struct __owner { 62 pid_t pid; /* Process ID. */ 63 time_t tod; /* Time-of-day. */ 64} OWNER; 65static OWNER owner; 66 67static char hostname[MAXHOSTNAMELEN + 1]; /* Hostname. */ 68 69int lock_request(LOCKD_MSG *); 70int test_request(LOCKD_MSG *); 71void show(LOCKD_MSG *); 72int unlock_request(LOCKD_MSG *); 73 74/* 75 * will break because fifo needs to be repopened when EOF'd 76 */ 77#ifdef SETUID_DAEMON 78#define lockd_seteuid(uid) seteuid(uid) 79#else 80#define lockd_seteuid(uid) (1) 81#endif 82 83#define d_calls 0 84#define d_args 0 85 86void 87client_cleanup(sig, code) 88 int sig; 89 int code; 90{ 91 (void)lockd_seteuid(0); 92 (void)unlink(_PATH_LCKFIFO); 93 exit(-1); 94} 95 96/* 97 * client_request -- 98 * Loop around messages from the kernel, forwarding them off to 99 * NLM servers. 100 */ 101pid_t 102client_request(void) 103{ 104 LOCKD_MSG msg; 105 fd_set rdset; 106 int fd, nr, ret; 107 pid_t child; 108 109 /* Recreate the NLM fifo. */ 110 (void)unlink(_PATH_LCKFIFO); 111 (void)umask(S_IXGRP|S_IXOTH); 112 if (mkfifo(_PATH_LCKFIFO, S_IWUSR | S_IRUSR)) { 113 syslog(LOG_ERR, "mkfifo: %s: %m", _PATH_LCKFIFO); 114 exit (1); 115 } 116#define linebug syslog(LOG_ERR, "lockd: %d", __LINE__); 117linebug 118 119 /* 120 * Create a separate process, the client code is really a separate 121 * daemon that shares a lot of code. 122 */ 123 switch (child = fork()) { 124 case -1: 125 err(1, "fork"); 126 case 0: 127 break; 128 default: 129 return (child); 130 } 131 132linebug 133 signal(SIGHUP, client_cleanup); 134 signal(SIGTERM, client_cleanup); 135 136 /* Setup. */ 137 (void)time(&owner.tod); 138 owner.pid = getpid(); 139 (void)gethostname(hostname, sizeof(hostname) - 1); 140 141linebug 142reopen: 143 /* Open the fifo for reading. */ 144 if ((fd = open(_PATH_LCKFIFO, O_RDONLY /* | O_NONBLOCK */)) < 0) 145 syslog(LOG_ERR, "open: %s: %m", _PATH_LCKFIFO); 146 147 /* drop our root priviledges */ 148 (void)lockd_seteuid(daemon_uid); 149 150 /* Set up the select. */ 151 FD_ZERO(&rdset); 152 153 for (;;) { 154linebug 155 /* Wait for contact... fifo's return EAGAIN when read with 156 * no data 157 */ 158 FD_SET(fd, &rdset); 159 (void)select(fd + 1, &rdset, NULL, NULL, NULL); 160 161linebug 162 /* Read the fixed length message. */ 163 if ((nr = read(fd, &msg, sizeof(msg))) == sizeof(msg)) { 164 if (d_args) 165 show(&msg); 166 167 if (msg.lm_version != LOCKD_MSG_VERSION) { 168 syslog(LOG_ERR, 169 "unknown msg type: %d", msg.lm_version); 170 } 171 /* 172 * Send it to the NLM server and don't grant the lock 173 * if we fail for any reason. 174 */ 175 switch (msg.lm_fl.l_type) { 176 case F_RDLCK: 177 case F_WRLCK: 178 if (msg.lm_getlk) 179 ret = test_request(&msg); 180 else 181 ret = lock_request(&msg); 182 break; 183 case F_UNLCK: 184 ret = unlock_request(&msg); 185 break; 186 default: 187 ret = 1; 188 syslog(LOG_ERR, 189 "unknown lock type: %d", msg.lm_fl.l_type); 190 break; 191 } 192 if (ret) { 193 struct lockd_ans ans; 194 195 ans.la_msg_ident = msg.lm_msg_ident; 196 ans.la_errno = EHOSTUNREACH; 197 198 if (nfslockdans(LOCKD_ANS_VERSION, &ans)) { 199 syslog((errno == EPIPE ? LOG_INFO : 200 LOG_ERR), "process %lu: %m", 201 (u_long)msg.lm_msg_ident.pid); 202 } 203 } 204 } else if (nr == -1) { 205linebug 206 if (errno != EAGAIN) { 207 syslog(LOG_ERR, "read: %s: %m", _PATH_LCKFIFO); 208 goto err; 209 } 210 } else if (nr != 0) { 211 syslog(LOG_ERR, 212 "%s: discard %d bytes", _PATH_LCKFIFO, nr); 213 } if (nr == 0) { 214 close (fd); 215 goto reopen; 216 } 217 } 218 219 /* Reached only on error. */ 220err: 221 (void)lockd_seteuid(0); 222 (void)unlink(_PATH_LCKFIFO); 223 _exit (1); 224} 225 226void 227set_auth( 228 CLIENT *cl, 229 struct ucred *ucred 230) 231{ 232 if (cl->cl_auth != NULL) 233 cl->cl_auth->ah_ops->ah_destroy(cl->cl_auth); 234 cl->cl_auth = authunix_create(hostname, 235 ucred->cr_uid, 236 ucred->cr_groups[0], 237 ucred->cr_ngroups-1, 238 &ucred->cr_groups[1]); 239} 240 241 242/* 243 * test_request -- 244 * Convert a lock LOCKD_MSG into an NLM request, and send it off. 245 */ 246int 247test_request(LOCKD_MSG *msg) 248{ 249 CLIENT *cli; 250 struct timeval timeout = {0, 0}; /* No timeout, no response. */ 251 char dummy; 252 253 if (d_calls) 254 syslog(LOG_DEBUG, "test request: %s: %s to %s", 255 msg->lm_nfsv3 ? "V4" : "V1/3", 256 msg->lm_fl.l_type == F_WRLCK ? "write" : "read", 257 from_addr((struct sockaddr_in *)&msg->lm_addr)); 258 259 if (msg->lm_nfsv3) { 260 struct nlm4_testargs arg4; 261 262 arg4.cookie.n_bytes = (char *)&msg->lm_msg_ident; 263 arg4.cookie.n_len = sizeof(msg->lm_msg_ident); 264 arg4.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0; 265 arg4.alock.caller_name = hostname; 266 arg4.alock.fh.n_bytes = (char *)&msg->lm_fh; 267 arg4.alock.fh.n_len = msg->lm_fh_len; 268 arg4.alock.oh.n_bytes = (char *)&owner; 269 arg4.alock.oh.n_len = sizeof(owner); 270 arg4.alock.svid = msg->lm_msg_ident.pid; 271 arg4.alock.l_offset = msg->lm_fl.l_start; 272 arg4.alock.l_len = msg->lm_fl.l_len; 273 274 if ((cli = get_client( 275 (struct sockaddr *)&msg->lm_addr, 276 NLM_VERS4)) == NULL) 277 return (1); 278 279 set_auth(cli, &msg->lm_cred); 280 (void)clnt_call(cli, NLM_TEST_MSG, 281 xdr_nlm4_testargs, &arg4, xdr_void, &dummy, timeout); 282 } else { 283 struct nlm_testargs arg; 284 285 arg.cookie.n_bytes = (char *)&msg->lm_msg_ident; 286 arg.cookie.n_len = sizeof(msg->lm_msg_ident); 287 arg.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0; 288 arg.alock.caller_name = hostname; 289 arg.alock.fh.n_bytes = (char *)&msg->lm_fh; 290 arg.alock.fh.n_len = msg->lm_fh_len; 291 arg.alock.oh.n_bytes = (char *)&owner; 292 arg.alock.oh.n_len = sizeof(owner); 293 arg.alock.svid = msg->lm_msg_ident.pid; 294 arg.alock.l_offset = msg->lm_fl.l_start; 295 arg.alock.l_len = msg->lm_fl.l_len; 296 297 if ((cli = get_client( 298 (struct sockaddr *)&msg->lm_addr, 299 NLM_VERS)) == NULL) 300 return (1); 301 302 set_auth(cli, &msg->lm_cred); 303 (void)clnt_call(cli, NLM_TEST_MSG, 304 xdr_nlm_testargs, &arg, xdr_void, &dummy, timeout); 305 } 306 return (0); 307} 308 309/* 310 * lock_request -- 311 * Convert a lock LOCKD_MSG into an NLM request, and send it off. 312 */ 313int 314lock_request(LOCKD_MSG *msg) 315{ 316 CLIENT *cli; 317 struct nlm4_lockargs arg4; 318 struct nlm_lockargs arg; 319 struct timeval timeout = {0, 0}; /* No timeout, no response. */ 320 char dummy; 321 322 if (d_calls) 323 syslog(LOG_DEBUG, "lock request: %s: %s to %s", 324 msg->lm_nfsv3 ? "V4" : "V1/3", 325 msg->lm_fl.l_type == F_WRLCK ? "write" : "read", 326 from_addr((struct sockaddr_in *)&msg->lm_addr)); 327 328 if (msg->lm_nfsv3) { 329 arg4.cookie.n_bytes = (char *)&msg->lm_msg_ident; 330 arg4.cookie.n_len = sizeof(msg->lm_msg_ident); 331 arg4.block = msg->lm_wait ? 1 : 0; 332 arg4.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0; 333 arg4.alock.caller_name = hostname; 334 arg4.alock.fh.n_bytes = (char *)&msg->lm_fh; 335 arg4.alock.fh.n_len = msg->lm_fh_len; 336 arg4.alock.oh.n_bytes = (char *)&owner; 337 arg4.alock.oh.n_len = sizeof(owner); 338 arg4.alock.svid = msg->lm_msg_ident.pid; 339 arg4.alock.l_offset = msg->lm_fl.l_start; 340 arg4.alock.l_len = msg->lm_fl.l_len; 341 arg4.reclaim = 0; 342 arg4.state = nsm_state; 343 344 if ((cli = get_client( 345 (struct sockaddr *)&msg->lm_addr, 346 NLM_VERS4)) == NULL) 347 return (1); 348 349 set_auth(cli, &msg->lm_cred); 350 (void)clnt_call(cli, NLM_LOCK_MSG, 351 xdr_nlm4_lockargs, &arg4, xdr_void, &dummy, timeout); 352 } else { 353 arg.cookie.n_bytes = (char *)&msg->lm_msg_ident; 354 arg.cookie.n_len = sizeof(msg->lm_msg_ident); 355 arg.block = msg->lm_wait ? 1 : 0; 356 arg.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0; 357 arg.alock.caller_name = hostname; 358 arg.alock.fh.n_bytes = (char *)&msg->lm_fh; 359 arg.alock.fh.n_len = msg->lm_fh_len; 360 arg.alock.oh.n_bytes = (char *)&owner; 361 arg.alock.oh.n_len = sizeof(owner); 362 arg.alock.svid = msg->lm_msg_ident.pid; 363 arg.alock.l_offset = msg->lm_fl.l_start; 364 arg.alock.l_len = msg->lm_fl.l_len; 365 arg.reclaim = 0; 366 arg.state = nsm_state; 367 368 if ((cli = get_client( 369 (struct sockaddr *)&msg->lm_addr, 370 NLM_VERS)) == NULL) 371 return (1); 372 373 set_auth(cli, &msg->lm_cred); 374 (void)clnt_call(cli, NLM_LOCK_MSG, 375 xdr_nlm_lockargs, &arg, xdr_void, &dummy, timeout); 376 } 377 return (0); 378} 379 380/* 381 * unlock_request -- 382 * Convert an unlock LOCKD_MSG into an NLM request, and send it off. 383 */ 384int 385unlock_request(LOCKD_MSG *msg) 386{ 387 CLIENT *cli; 388 struct nlm4_unlockargs arg4; 389 struct nlm_unlockargs arg; 390 struct timeval timeout = {0, 0}; /* No timeout, no response. */ 391 char dummy; 392 393 if (d_calls) 394 syslog(LOG_DEBUG, "unlock request: %s: to %s", 395 msg->lm_nfsv3 ? "V4" : "V1/3", 396 from_addr((struct sockaddr_in *)&msg->lm_addr)); 397 398 if (msg->lm_nfsv3) { 399 arg4.cookie.n_bytes = (char *)&msg->lm_msg_ident; 400 arg4.cookie.n_len = sizeof(msg->lm_msg_ident); 401 arg4.alock.caller_name = hostname; 402 arg4.alock.fh.n_bytes = (char *)&msg->lm_fh; 403 arg4.alock.fh.n_len = msg->lm_fh_len; 404 arg4.alock.oh.n_bytes = (char *)&owner; 405 arg4.alock.oh.n_len = sizeof(owner); 406 arg4.alock.svid = msg->lm_msg_ident.pid; 407 arg4.alock.l_offset = msg->lm_fl.l_start; 408 arg4.alock.l_len = msg->lm_fl.l_len; 409 410 if ((cli = get_client( 411 (struct sockaddr *)&msg->lm_addr, 412 NLM_VERS4)) == NULL) 413 return (1); 414 415 set_auth(cli, &msg->lm_cred); 416 (void)clnt_call(cli, NLM_UNLOCK_MSG, 417 xdr_nlm4_unlockargs, &arg4, xdr_void, &dummy, timeout); 418 } else { 419 arg.cookie.n_bytes = (char *)&msg->lm_msg_ident; 420 arg.cookie.n_len = sizeof(msg->lm_msg_ident); 421 arg.alock.caller_name = hostname; 422 arg.alock.fh.n_bytes = (char *)&msg->lm_fh; 423 arg.alock.fh.n_len = msg->lm_fh_len; 424 arg.alock.oh.n_bytes = (char *)&owner; 425 arg.alock.oh.n_len = sizeof(owner); 426 arg.alock.svid = msg->lm_msg_ident.pid; 427 arg.alock.l_offset = msg->lm_fl.l_start; 428 arg.alock.l_len = msg->lm_fl.l_len; 429 430 if ((cli = get_client( 431 (struct sockaddr *)&msg->lm_addr, 432 NLM_VERS)) == NULL) 433 return (1); 434 435 set_auth(cli, &msg->lm_cred); 436 (void)clnt_call(cli, NLM_UNLOCK_MSG, 437 xdr_nlm_unlockargs, &arg, xdr_void, &dummy, timeout); 438 } 439 440 return (0); 441} 442 443int 444lock_answer(int pid, netobj *netcookie, int result, int *pid_p, int version) 445{ 446 struct lockd_ans ans; 447 448 if (netcookie->n_len != sizeof(ans.la_msg_ident)) { 449 if (pid == -1) { /* we're screwed */ 450 syslog(LOG_ERR, "inedible nlm cookie"); 451 return -1; 452 } 453 ans.la_msg_ident.pid = pid; 454 ans.la_msg_ident.msg_seq = -1; 455 } else { 456 memcpy(&ans.la_msg_ident, netcookie->n_bytes, 457 sizeof(ans.la_msg_ident)); 458 } 459 460 if (d_calls) 461 syslog(LOG_DEBUG, "lock answer: pid %lu: %s", 462 ans.la_msg_ident.pid, version == NLM_VERS4 ? 463 show_4state(result) : show_state(result)); 464 465 ans.la_set_getlk_pid = 0; 466 if (version == NLM_VERS4) 467 switch (result) { 468 case nlm4_granted: 469 ans.la_errno = 0; 470 break; 471 default: 472 ans.la_errno = EACCES; 473 break; 474 case nlm4_denied: 475 if (pid_p == NULL) 476 ans.la_errno = EACCES; 477 else { 478 /* this is an answer to a nlm_test msg */ 479 ans.la_set_getlk_pid = 1; 480 ans.la_getlk_pid = *pid_p; 481 ans.la_errno = 0; 482 } 483 break; 484 case nlm4_denied_nolocks: 485 ans.la_errno = EAGAIN; 486 break; 487 case nlm4_blocked: 488 return -1; 489 /* NOTREACHED */ 490 case nlm4_denied_grace_period: 491 ans.la_errno = EAGAIN; 492 break; 493 case nlm4_deadlck: 494 ans.la_errno = EDEADLK; 495 break; 496 case nlm4_rofs: 497 ans.la_errno = EROFS; 498 break; 499 case nlm4_stale_fh: 500 ans.la_errno = ESTALE; 501 break; 502 case nlm4_fbig: 503 ans.la_errno = EFBIG; 504 break; 505 case nlm4_failed: 506 ans.la_errno = EACCES; 507 break; 508 } 509 else 510 switch (result) { 511 case nlm_granted: 512 ans.la_errno = 0; 513 break; 514 default: 515 ans.la_errno = EACCES; 516 break; 517 case nlm_denied: 518 if (pid_p == NULL) 519 ans.la_errno = EACCES; 520 else { 521 /* this is an answer to a nlm_test msg */ 522 ans.la_set_getlk_pid = 1; 523 ans.la_getlk_pid = *pid_p; 524 ans.la_errno = 0; 525 } 526 break; 527 case nlm_denied_nolocks: 528 ans.la_errno = EAGAIN; 529 break; 530 case nlm_blocked: 531 return -1; 532 /* NOTREACHED */ 533 case nlm_denied_grace_period: 534 ans.la_errno = EAGAIN; 535 break; 536 case nlm_deadlck: 537 ans.la_errno = EDEADLK; 538 break; 539 } 540 541 if (nfslockdans(LOCKD_ANS_VERSION, &ans)) { 542 syslog(((errno == EPIPE || errno == ESRCH) ? 543 LOG_INFO : LOG_ERR), 544 "process %lu: %m", (u_long)ans.la_msg_ident.pid); 545 return -1; 546 } 547 return 0; 548} 549 550/* 551 * show -- 552 * Display the contents of a kernel LOCKD_MSG structure. 553 */ 554void 555show(LOCKD_MSG *mp) 556{ 557 static char hex[] = "0123456789abcdef"; 558 struct fid *fidp; 559 fsid_t *fsidp; 560 size_t len; 561 u_int8_t *p, *t, buf[NFS_SMALLFH*3+1]; 562 563 printf("process ID: %lu\n", (long)mp->lm_msg_ident.pid); 564 565 fsidp = (fsid_t *)&mp->lm_fh; 566 fidp = (struct fid *)((u_int8_t *)&mp->lm_fh + sizeof(fsid_t)); 567 568 for (t = buf, p = (u_int8_t *)mp->lm_fh, 569 len = mp->lm_fh_len; 570 len > 0; ++p, --len) { 571 *t++ = '\\'; 572 *t++ = hex[(*p & 0xf0) >> 4]; 573 *t++ = hex[*p & 0x0f]; 574 } 575 *t = '\0'; 576 577 printf("fh_len %d, fh %s\n", mp->lm_fh_len, buf); 578 579 /* Show flock structure. */ 580 printf("start %qu; len %qu; pid %lu; type %d; whence %d\n", 581 mp->lm_fl.l_start, mp->lm_fl.l_len, (u_long)mp->lm_fl.l_pid, 582 mp->lm_fl.l_type, mp->lm_fl.l_whence); 583 584 /* Show wait flag. */ 585 printf("wait was %s\n", mp->lm_wait ? "set" : "not set"); 586} 587