2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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 * 3. Berkeley Software Design Inc's name may not be used to endorse or
15 * promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * from BSDI kern.c,v 1.2 1998/11/25 22:38:27 don Exp
33 #include <sys/param.h>
34 #include <sys/mount.h>
35 #include <sys/queue.h>
36 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
55 #include <nfs/nfsproto.h>
56 #include <nfs/nfs_lock.h>
59 #include "lockd_lock.h"
60 #include <nfsclient/nfs.h>
62 #define DAEMON_USERNAME "daemon"
64 /* Lock request owner. */
65 typedef struct __owner {
66 pid_t pid; /* Process ID. */
67 time_t tod; /* Time-of-day. */
71 static char hostname[MAXHOSTNAMELEN + 1]; /* Hostname. */
74 static void client_cleanup(void);
75 static const char *from_addr(struct sockaddr *);
76 int lock_request(LOCKD_MSG *);
77 static void set_auth(CLIENT *cl, struct xucred *ucred);
78 void show(LOCKD_MSG *);
79 int test_request(LOCKD_MSG *);
80 int unlock_request(LOCKD_MSG *);
83 nfslockdans(int vers, struct lockd_ans *ansp)
87 return (write(devfd, ansp, sizeof *ansp) <= 0);
91 * will break because fifo needs to be repopened when EOF'd
93 #define lockd_seteuid(uid) seteuid(uid)
95 #define d_calls (debug_level > 1)
96 #define d_args (debug_level > 2)
99 from_addr(struct sockaddr *saddr)
101 static char inet_buf[INET6_ADDRSTRLEN];
103 if (getnameinfo(saddr, saddr->sa_len, inet_buf, sizeof(inet_buf),
104 NULL, 0, NI_NUMERICHOST) == 0)
112 (void)lockd_seteuid(0);
118 * Loop around messages from the kernel, forwarding them off to
131 devfd = open(_PATH_DEV _PATH_NFSLCKDEV, O_RDWR | O_NONBLOCK);
133 syslog(LOG_ERR, "open: %s: %m", _PATH_NFSLCKDEV);
137 signal(SIGPIPE, SIG_IGN);
140 * Create a separate process, the client code is really a separate
141 * daemon that shares a lot of code.
143 switch (child = fork()) {
147 setproctitle("client");
150 setproctitle("server");
154 signal(SIGHUP, (sig_t)client_cleanup);
155 signal(SIGTERM, (sig_t)client_cleanup);
158 (void)time(&owner.tod);
159 owner.pid = getpid();
160 (void)gethostname(hostname, sizeof(hostname) - 1);
162 pw = getpwnam(DAEMON_USERNAME);
164 syslog(LOG_ERR, "getpwnam: %s: %m", DAEMON_USERNAME);
167 daemon_uid = pw->pw_uid;
168 /* drop our root privileges */
169 (void)lockd_seteuid(daemon_uid);
172 /* Read the fixed length message. */
173 if ((nr = read(devfd, &msg, sizeof(msg))) == sizeof(msg)) {
177 if (msg.lm_version != LOCKD_MSG_VERSION) {
179 "unknown msg type: %d", msg.lm_version);
182 * Send it to the NLM server and don't grant the lock
183 * if we fail for any reason.
185 switch (msg.lm_fl.l_type) {
189 ret = test_request(&msg);
191 ret = lock_request(&msg);
194 ret = unlock_request(&msg);
199 "unknown lock type: %d", msg.lm_fl.l_type);
203 struct lockd_ans ans;
205 ans.la_msg_ident = msg.lm_msg_ident;
206 ans.la_errno = EHOSTUNREACH;
208 if (nfslockdans(LOCKD_ANS_VERSION, &ans)) {
209 syslog((errno == EPIPE ? LOG_INFO :
210 LOG_ERR), "process %lu: %m",
211 (u_long)msg.lm_msg_ident.pid);
214 } else if (nr == -1) {
215 if (errno != EAGAIN) {
216 syslog(LOG_ERR, "read: %s: %m", _PATH_NFSLCKDEV);
219 } else if (nr != 0) {
221 "%s: discard %d bytes", _PATH_NFSLCKDEV, nr);
225 /* Reached only on error. */
227 (void)lockd_seteuid(0);
232 set_auth(CLIENT *cl, struct xucred *xucred)
236 ngroups = xucred->cr_ngroups - 1;
239 if (cl->cl_auth != NULL)
240 cl->cl_auth->ah_ops->ah_destroy(cl->cl_auth);
241 cl->cl_auth = authunix_create(hostname,
243 xucred->cr_groups[0],
245 &xucred->cr_groups[1]);
251 * Convert a lock LOCKD_MSG into an NLM request, and send it off.
254 test_request(LOCKD_MSG *msg)
257 struct timeval timeout = {0, 0}; /* No timeout, no response. */
261 syslog(LOG_DEBUG, "test request: %s: %s to %s",
262 msg->lm_nfsv3 ? "V4" : "V1/3",
263 msg->lm_fl.l_type == F_WRLCK ? "write" : "read",
264 from_addr((struct sockaddr *)&msg->lm_addr));
267 struct nlm4_testargs arg4;
269 arg4.cookie.n_bytes = (char *)&msg->lm_msg_ident;
270 arg4.cookie.n_len = sizeof(msg->lm_msg_ident);
271 arg4.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0;
272 arg4.alock.caller_name = hostname;
273 arg4.alock.fh.n_bytes = (char *)&msg->lm_fh;
274 arg4.alock.fh.n_len = msg->lm_fh_len;
275 arg4.alock.oh.n_bytes = (char *)&owner;
276 arg4.alock.oh.n_len = sizeof(owner);
277 arg4.alock.svid = msg->lm_msg_ident.pid;
278 arg4.alock.l_offset = msg->lm_fl.l_start;
279 arg4.alock.l_len = msg->lm_fl.l_len;
281 if ((cli = get_client(
282 (struct sockaddr *)&msg->lm_addr,
286 set_auth(cli, &msg->lm_cred);
287 (void)clnt_call(cli, NLM_TEST_MSG,
288 (xdrproc_t)xdr_nlm4_testargs, &arg4,
289 (xdrproc_t)xdr_void, &dummy, timeout);
291 struct nlm_testargs arg;
293 arg.cookie.n_bytes = (char *)&msg->lm_msg_ident;
294 arg.cookie.n_len = sizeof(msg->lm_msg_ident);
295 arg.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0;
296 arg.alock.caller_name = hostname;
297 arg.alock.fh.n_bytes = (char *)&msg->lm_fh;
298 arg.alock.fh.n_len = msg->lm_fh_len;
299 arg.alock.oh.n_bytes = (char *)&owner;
300 arg.alock.oh.n_len = sizeof(owner);
301 arg.alock.svid = msg->lm_msg_ident.pid;
302 arg.alock.l_offset = msg->lm_fl.l_start;
303 arg.alock.l_len = msg->lm_fl.l_len;
305 if ((cli = get_client(
306 (struct sockaddr *)&msg->lm_addr,
310 set_auth(cli, &msg->lm_cred);
311 (void)clnt_call(cli, NLM_TEST_MSG,
312 (xdrproc_t)xdr_nlm_testargs, &arg,
313 (xdrproc_t)xdr_void, &dummy, timeout);
320 * Convert a lock LOCKD_MSG into an NLM request, and send it off.
323 lock_request(LOCKD_MSG *msg)
326 struct nlm4_lockargs arg4;
327 struct nlm_lockargs arg;
328 struct timeval timeout = {0, 0}; /* No timeout, no response. */
332 syslog(LOG_DEBUG, "lock request: %s: %s to %s",
333 msg->lm_nfsv3 ? "V4" : "V1/3",
334 msg->lm_fl.l_type == F_WRLCK ? "write" : "read",
335 from_addr((struct sockaddr *)&msg->lm_addr));
338 arg4.cookie.n_bytes = (char *)&msg->lm_msg_ident;
339 arg4.cookie.n_len = sizeof(msg->lm_msg_ident);
340 arg4.block = msg->lm_wait ? 1 : 0;
341 arg4.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0;
342 arg4.alock.caller_name = hostname;
343 arg4.alock.fh.n_bytes = (char *)&msg->lm_fh;
344 arg4.alock.fh.n_len = msg->lm_fh_len;
345 arg4.alock.oh.n_bytes = (char *)&owner;
346 arg4.alock.oh.n_len = sizeof(owner);
347 arg4.alock.svid = msg->lm_msg_ident.pid;
348 arg4.alock.l_offset = msg->lm_fl.l_start;
349 arg4.alock.l_len = msg->lm_fl.l_len;
351 arg4.state = nsm_state;
353 if ((cli = get_client(
354 (struct sockaddr *)&msg->lm_addr,
358 set_auth(cli, &msg->lm_cred);
359 (void)clnt_call(cli, NLM_LOCK_MSG,
360 (xdrproc_t)xdr_nlm4_lockargs, &arg4,
361 (xdrproc_t)xdr_void, &dummy, timeout);
363 arg.cookie.n_bytes = (char *)&msg->lm_msg_ident;
364 arg.cookie.n_len = sizeof(msg->lm_msg_ident);
365 arg.block = msg->lm_wait ? 1 : 0;
366 arg.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0;
367 arg.alock.caller_name = hostname;
368 arg.alock.fh.n_bytes = (char *)&msg->lm_fh;
369 arg.alock.fh.n_len = msg->lm_fh_len;
370 arg.alock.oh.n_bytes = (char *)&owner;
371 arg.alock.oh.n_len = sizeof(owner);
372 arg.alock.svid = msg->lm_msg_ident.pid;
373 arg.alock.l_offset = msg->lm_fl.l_start;
374 arg.alock.l_len = msg->lm_fl.l_len;
376 arg.state = nsm_state;
378 if ((cli = get_client(
379 (struct sockaddr *)&msg->lm_addr,
383 set_auth(cli, &msg->lm_cred);
384 (void)clnt_call(cli, NLM_LOCK_MSG,
385 (xdrproc_t)xdr_nlm_lockargs, &arg,
386 (xdrproc_t)xdr_void, &dummy, timeout);
393 * Convert an unlock LOCKD_MSG into an NLM request, and send it off.
396 unlock_request(LOCKD_MSG *msg)
399 struct nlm4_unlockargs arg4;
400 struct nlm_unlockargs arg;
401 struct timeval timeout = {0, 0}; /* No timeout, no response. */
405 syslog(LOG_DEBUG, "unlock request: %s: to %s",
406 msg->lm_nfsv3 ? "V4" : "V1/3",
407 from_addr((struct sockaddr *)&msg->lm_addr));
410 arg4.cookie.n_bytes = (char *)&msg->lm_msg_ident;
411 arg4.cookie.n_len = sizeof(msg->lm_msg_ident);
412 arg4.alock.caller_name = hostname;
413 arg4.alock.fh.n_bytes = (char *)&msg->lm_fh;
414 arg4.alock.fh.n_len = msg->lm_fh_len;
415 arg4.alock.oh.n_bytes = (char *)&owner;
416 arg4.alock.oh.n_len = sizeof(owner);
417 arg4.alock.svid = msg->lm_msg_ident.pid;
418 arg4.alock.l_offset = msg->lm_fl.l_start;
419 arg4.alock.l_len = msg->lm_fl.l_len;
421 if ((cli = get_client(
422 (struct sockaddr *)&msg->lm_addr,
426 set_auth(cli, &msg->lm_cred);
427 (void)clnt_call(cli, NLM_UNLOCK_MSG,
428 (xdrproc_t)xdr_nlm4_unlockargs, &arg4,
429 (xdrproc_t)xdr_void, &dummy, timeout);
431 arg.cookie.n_bytes = (char *)&msg->lm_msg_ident;
432 arg.cookie.n_len = sizeof(msg->lm_msg_ident);
433 arg.alock.caller_name = hostname;
434 arg.alock.fh.n_bytes = (char *)&msg->lm_fh;
435 arg.alock.fh.n_len = msg->lm_fh_len;
436 arg.alock.oh.n_bytes = (char *)&owner;
437 arg.alock.oh.n_len = sizeof(owner);
438 arg.alock.svid = msg->lm_msg_ident.pid;
439 arg.alock.l_offset = msg->lm_fl.l_start;
440 arg.alock.l_len = msg->lm_fl.l_len;
442 if ((cli = get_client(
443 (struct sockaddr *)&msg->lm_addr,
447 set_auth(cli, &msg->lm_cred);
448 (void)clnt_call(cli, NLM_UNLOCK_MSG,
449 (xdrproc_t)xdr_nlm_unlockargs, &arg,
450 (xdrproc_t)xdr_void, &dummy, timeout);
457 lock_answer(int pid, netobj *netcookie, int result, int *pid_p, int version)
459 struct lockd_ans ans;
461 if (netcookie->n_len != sizeof(ans.la_msg_ident)) {
462 if (pid == -1) { /* we're screwed */
463 syslog(LOG_ERR, "inedible nlm cookie");
466 ans.la_msg_ident.pid = pid;
467 ans.la_msg_ident.msg_seq = -1;
469 memcpy(&ans.la_msg_ident, netcookie->n_bytes,
470 sizeof(ans.la_msg_ident));
474 syslog(LOG_DEBUG, "lock answer: pid %lu: %s %d",
475 (unsigned long)ans.la_msg_ident.pid,
476 version == NLM_VERS4 ? "nlmv4" : "nlmv3",
479 ans.la_set_getlk_pid = 0;
480 if (version == NLM_VERS4)
486 ans.la_errno = EACCES;
490 ans.la_errno = EAGAIN;
492 /* this is an answer to a nlm_test msg */
493 ans.la_set_getlk_pid = 1;
494 ans.la_getlk_pid = *pid_p;
498 case nlm4_denied_nolocks:
499 ans.la_errno = EAGAIN;
504 case nlm4_denied_grace_period:
505 ans.la_errno = EAGAIN;
508 ans.la_errno = EDEADLK;
511 ans.la_errno = EROFS;
514 ans.la_errno = ESTALE;
517 ans.la_errno = EFBIG;
520 ans.la_errno = EACCES;
529 ans.la_errno = EACCES;
533 ans.la_errno = EAGAIN;
535 /* this is an answer to a nlm_test msg */
536 ans.la_set_getlk_pid = 1;
537 ans.la_getlk_pid = *pid_p;
541 case nlm_denied_nolocks:
542 ans.la_errno = EAGAIN;
547 case nlm_denied_grace_period:
548 ans.la_errno = EAGAIN;
551 ans.la_errno = EDEADLK;
555 if (nfslockdans(LOCKD_ANS_VERSION, &ans)) {
556 syslog(((errno == EPIPE || errno == ESRCH) ?
558 "process %lu: %m", (u_long)ans.la_msg_ident.pid);
566 * Display the contents of a kernel LOCKD_MSG structure.
571 static char hex[] = "0123456789abcdef";
573 u_int8_t *p, *t, buf[NFS_SMALLFH*3+1];
575 syslog(LOG_DEBUG, "process ID: %lu\n", (long)mp->lm_msg_ident.pid);
577 for (t = buf, p = (u_int8_t *)mp->lm_fh,
579 len > 0; ++p, --len) {
581 *t++ = hex[(*p & 0xf0) >> 4];
582 *t++ = hex[*p & 0x0f];
586 syslog(LOG_DEBUG, "fh_len %d, fh %s\n", (int)mp->lm_fh_len, buf);
588 /* Show flock structure. */
589 syslog(LOG_DEBUG, "start %llu; len %llu; pid %lu; type %d; whence %d\n",
590 (unsigned long long)mp->lm_fl.l_start,
591 (unsigned long long)mp->lm_fl.l_len, (u_long)mp->lm_fl.l_pid,
592 mp->lm_fl.l_type, mp->lm_fl.l_whence);
594 /* Show wait flag. */
595 syslog(LOG_DEBUG, "wait was %s\n", mp->lm_wait ? "set" : "not set");