2 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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
28 * from BSDI kern.c,v 1.2 1998/11/25 22:38:27 don Exp
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/mount.h>
36 #include <sys/queue.h>
37 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
56 #include <nfs/nfsproto.h>
57 #include <nfs/nfs_lock.h>
60 #include "lockd_lock.h"
61 #include <nfsclient/nfs.h>
63 #define DAEMON_USERNAME "daemon"
65 /* Lock request owner. */
66 typedef struct __owner {
67 pid_t pid; /* Process ID. */
68 time_t tod; /* Time-of-day. */
72 static char hostname[MAXHOSTNAMELEN + 1]; /* Hostname. */
75 static void client_cleanup(void);
76 static const char *from_addr(struct sockaddr *);
77 int lock_request(LOCKD_MSG *);
78 static void set_auth(CLIENT *cl, struct xucred *ucred);
79 void show(LOCKD_MSG *);
80 int test_request(LOCKD_MSG *);
81 int unlock_request(LOCKD_MSG *);
84 nfslockdans(int vers, struct lockd_ans *ansp)
88 return (write(devfd, ansp, sizeof *ansp) <= 0);
92 * will break because fifo needs to be repopened when EOF'd
94 #define lockd_seteuid(uid) seteuid(uid)
96 #define d_calls (debug_level > 1)
97 #define d_args (debug_level > 2)
100 from_addr(struct sockaddr *saddr)
102 static char inet_buf[INET6_ADDRSTRLEN];
104 if (getnameinfo(saddr, saddr->sa_len, inet_buf, sizeof(inet_buf),
105 NULL, 0, NI_NUMERICHOST) == 0)
113 (void)lockd_seteuid(0);
119 * Loop around messages from the kernel, forwarding them off to
132 devfd = open(_PATH_DEV _PATH_NFSLCKDEV, O_RDWR | O_NONBLOCK);
134 syslog(LOG_ERR, "open: %s: %m", _PATH_NFSLCKDEV);
138 signal(SIGPIPE, SIG_IGN);
141 * Create a separate process, the client code is really a separate
142 * daemon that shares a lot of code.
144 switch (child = fork()) {
148 setproctitle("client");
151 setproctitle("server");
155 signal(SIGHUP, (sig_t)client_cleanup);
156 signal(SIGTERM, (sig_t)client_cleanup);
159 (void)time(&owner.tod);
160 owner.pid = getpid();
161 (void)gethostname(hostname, sizeof(hostname) - 1);
163 pw = getpwnam(DAEMON_USERNAME);
165 syslog(LOG_ERR, "getpwnam: %s: %m", DAEMON_USERNAME);
168 daemon_uid = pw->pw_uid;
169 /* drop our root privileges */
170 (void)lockd_seteuid(daemon_uid);
173 /* Read the fixed length message. */
174 if ((nr = read(devfd, &msg, sizeof(msg))) == sizeof(msg)) {
178 if (msg.lm_version != LOCKD_MSG_VERSION) {
180 "unknown msg type: %d", msg.lm_version);
183 * Send it to the NLM server and don't grant the lock
184 * if we fail for any reason.
186 switch (msg.lm_fl.l_type) {
190 ret = test_request(&msg);
192 ret = lock_request(&msg);
195 ret = unlock_request(&msg);
200 "unknown lock type: %d", msg.lm_fl.l_type);
204 struct lockd_ans ans;
206 ans.la_msg_ident = msg.lm_msg_ident;
207 ans.la_errno = EHOSTUNREACH;
209 if (nfslockdans(LOCKD_ANS_VERSION, &ans)) {
210 syslog((errno == EPIPE ? LOG_INFO :
211 LOG_ERR), "process %lu: %m",
212 (u_long)msg.lm_msg_ident.pid);
215 } else if (nr == -1) {
216 if (errno != EAGAIN) {
217 syslog(LOG_ERR, "read: %s: %m", _PATH_NFSLCKDEV);
220 } else if (nr != 0) {
222 "%s: discard %d bytes", _PATH_NFSLCKDEV, nr);
226 /* Reached only on error. */
228 (void)lockd_seteuid(0);
233 set_auth(CLIENT *cl, struct xucred *xucred)
237 ngroups = xucred->cr_ngroups - 1;
240 if (cl->cl_auth != NULL)
241 cl->cl_auth->ah_ops->ah_destroy(cl->cl_auth);
242 cl->cl_auth = authunix_create(hostname,
244 xucred->cr_groups[0],
246 &xucred->cr_groups[1]);
252 * Convert a lock LOCKD_MSG into an NLM request, and send it off.
255 test_request(LOCKD_MSG *msg)
258 struct timeval timeout = {0, 0}; /* No timeout, no response. */
262 syslog(LOG_DEBUG, "test request: %s: %s to %s",
263 msg->lm_nfsv3 ? "V4" : "V1/3",
264 msg->lm_fl.l_type == F_WRLCK ? "write" : "read",
265 from_addr((struct sockaddr *)&msg->lm_addr));
268 struct nlm4_testargs arg4;
270 arg4.cookie.n_bytes = (char *)&msg->lm_msg_ident;
271 arg4.cookie.n_len = sizeof(msg->lm_msg_ident);
272 arg4.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0;
273 arg4.alock.caller_name = hostname;
274 arg4.alock.fh.n_bytes = (char *)&msg->lm_fh;
275 arg4.alock.fh.n_len = msg->lm_fh_len;
276 arg4.alock.oh.n_bytes = (char *)&owner;
277 arg4.alock.oh.n_len = sizeof(owner);
278 arg4.alock.svid = msg->lm_msg_ident.pid;
279 arg4.alock.l_offset = msg->lm_fl.l_start;
280 arg4.alock.l_len = msg->lm_fl.l_len;
282 if ((cli = get_client(
283 (struct sockaddr *)&msg->lm_addr,
287 set_auth(cli, &msg->lm_cred);
288 (void)clnt_call(cli, NLM_TEST_MSG,
289 (xdrproc_t)xdr_nlm4_testargs, &arg4,
290 (xdrproc_t)xdr_void, &dummy, timeout);
292 struct nlm_testargs arg;
294 arg.cookie.n_bytes = (char *)&msg->lm_msg_ident;
295 arg.cookie.n_len = sizeof(msg->lm_msg_ident);
296 arg.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0;
297 arg.alock.caller_name = hostname;
298 arg.alock.fh.n_bytes = (char *)&msg->lm_fh;
299 arg.alock.fh.n_len = msg->lm_fh_len;
300 arg.alock.oh.n_bytes = (char *)&owner;
301 arg.alock.oh.n_len = sizeof(owner);
302 arg.alock.svid = msg->lm_msg_ident.pid;
303 arg.alock.l_offset = msg->lm_fl.l_start;
304 arg.alock.l_len = msg->lm_fl.l_len;
306 if ((cli = get_client(
307 (struct sockaddr *)&msg->lm_addr,
311 set_auth(cli, &msg->lm_cred);
312 (void)clnt_call(cli, NLM_TEST_MSG,
313 (xdrproc_t)xdr_nlm_testargs, &arg,
314 (xdrproc_t)xdr_void, &dummy, timeout);
321 * Convert a lock LOCKD_MSG into an NLM request, and send it off.
324 lock_request(LOCKD_MSG *msg)
327 struct nlm4_lockargs arg4;
328 struct nlm_lockargs arg;
329 struct timeval timeout = {0, 0}; /* No timeout, no response. */
333 syslog(LOG_DEBUG, "lock request: %s: %s to %s",
334 msg->lm_nfsv3 ? "V4" : "V1/3",
335 msg->lm_fl.l_type == F_WRLCK ? "write" : "read",
336 from_addr((struct sockaddr *)&msg->lm_addr));
339 arg4.cookie.n_bytes = (char *)&msg->lm_msg_ident;
340 arg4.cookie.n_len = sizeof(msg->lm_msg_ident);
341 arg4.block = msg->lm_wait ? 1 : 0;
342 arg4.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0;
343 arg4.alock.caller_name = hostname;
344 arg4.alock.fh.n_bytes = (char *)&msg->lm_fh;
345 arg4.alock.fh.n_len = msg->lm_fh_len;
346 arg4.alock.oh.n_bytes = (char *)&owner;
347 arg4.alock.oh.n_len = sizeof(owner);
348 arg4.alock.svid = msg->lm_msg_ident.pid;
349 arg4.alock.l_offset = msg->lm_fl.l_start;
350 arg4.alock.l_len = msg->lm_fl.l_len;
352 arg4.state = nsm_state;
354 if ((cli = get_client(
355 (struct sockaddr *)&msg->lm_addr,
359 set_auth(cli, &msg->lm_cred);
360 (void)clnt_call(cli, NLM_LOCK_MSG,
361 (xdrproc_t)xdr_nlm4_lockargs, &arg4,
362 (xdrproc_t)xdr_void, &dummy, timeout);
364 arg.cookie.n_bytes = (char *)&msg->lm_msg_ident;
365 arg.cookie.n_len = sizeof(msg->lm_msg_ident);
366 arg.block = msg->lm_wait ? 1 : 0;
367 arg.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0;
368 arg.alock.caller_name = hostname;
369 arg.alock.fh.n_bytes = (char *)&msg->lm_fh;
370 arg.alock.fh.n_len = msg->lm_fh_len;
371 arg.alock.oh.n_bytes = (char *)&owner;
372 arg.alock.oh.n_len = sizeof(owner);
373 arg.alock.svid = msg->lm_msg_ident.pid;
374 arg.alock.l_offset = msg->lm_fl.l_start;
375 arg.alock.l_len = msg->lm_fl.l_len;
377 arg.state = nsm_state;
379 if ((cli = get_client(
380 (struct sockaddr *)&msg->lm_addr,
384 set_auth(cli, &msg->lm_cred);
385 (void)clnt_call(cli, NLM_LOCK_MSG,
386 (xdrproc_t)xdr_nlm_lockargs, &arg,
387 (xdrproc_t)xdr_void, &dummy, timeout);
394 * Convert an unlock LOCKD_MSG into an NLM request, and send it off.
397 unlock_request(LOCKD_MSG *msg)
400 struct nlm4_unlockargs arg4;
401 struct nlm_unlockargs arg;
402 struct timeval timeout = {0, 0}; /* No timeout, no response. */
406 syslog(LOG_DEBUG, "unlock request: %s: to %s",
407 msg->lm_nfsv3 ? "V4" : "V1/3",
408 from_addr((struct sockaddr *)&msg->lm_addr));
411 arg4.cookie.n_bytes = (char *)&msg->lm_msg_ident;
412 arg4.cookie.n_len = sizeof(msg->lm_msg_ident);
413 arg4.alock.caller_name = hostname;
414 arg4.alock.fh.n_bytes = (char *)&msg->lm_fh;
415 arg4.alock.fh.n_len = msg->lm_fh_len;
416 arg4.alock.oh.n_bytes = (char *)&owner;
417 arg4.alock.oh.n_len = sizeof(owner);
418 arg4.alock.svid = msg->lm_msg_ident.pid;
419 arg4.alock.l_offset = msg->lm_fl.l_start;
420 arg4.alock.l_len = msg->lm_fl.l_len;
422 if ((cli = get_client(
423 (struct sockaddr *)&msg->lm_addr,
427 set_auth(cli, &msg->lm_cred);
428 (void)clnt_call(cli, NLM_UNLOCK_MSG,
429 (xdrproc_t)xdr_nlm4_unlockargs, &arg4,
430 (xdrproc_t)xdr_void, &dummy, timeout);
432 arg.cookie.n_bytes = (char *)&msg->lm_msg_ident;
433 arg.cookie.n_len = sizeof(msg->lm_msg_ident);
434 arg.alock.caller_name = hostname;
435 arg.alock.fh.n_bytes = (char *)&msg->lm_fh;
436 arg.alock.fh.n_len = msg->lm_fh_len;
437 arg.alock.oh.n_bytes = (char *)&owner;
438 arg.alock.oh.n_len = sizeof(owner);
439 arg.alock.svid = msg->lm_msg_ident.pid;
440 arg.alock.l_offset = msg->lm_fl.l_start;
441 arg.alock.l_len = msg->lm_fl.l_len;
443 if ((cli = get_client(
444 (struct sockaddr *)&msg->lm_addr,
448 set_auth(cli, &msg->lm_cred);
449 (void)clnt_call(cli, NLM_UNLOCK_MSG,
450 (xdrproc_t)xdr_nlm_unlockargs, &arg,
451 (xdrproc_t)xdr_void, &dummy, timeout);
458 lock_answer(int pid, netobj *netcookie, int result, int *pid_p, int version)
460 struct lockd_ans ans;
462 if (netcookie->n_len != sizeof(ans.la_msg_ident)) {
463 if (pid == -1) { /* we're screwed */
464 syslog(LOG_ERR, "inedible nlm cookie");
467 ans.la_msg_ident.pid = pid;
468 ans.la_msg_ident.msg_seq = -1;
470 memcpy(&ans.la_msg_ident, netcookie->n_bytes,
471 sizeof(ans.la_msg_ident));
475 syslog(LOG_DEBUG, "lock answer: pid %lu: %s %d",
476 (unsigned long)ans.la_msg_ident.pid,
477 version == NLM_VERS4 ? "nlmv4" : "nlmv3",
480 ans.la_set_getlk_pid = 0;
481 if (version == NLM_VERS4)
487 ans.la_errno = EACCES;
491 ans.la_errno = EAGAIN;
493 /* this is an answer to a nlm_test msg */
494 ans.la_set_getlk_pid = 1;
495 ans.la_getlk_pid = *pid_p;
499 case nlm4_denied_nolocks:
500 ans.la_errno = EAGAIN;
505 case nlm4_denied_grace_period:
506 ans.la_errno = EAGAIN;
509 ans.la_errno = EDEADLK;
512 ans.la_errno = EROFS;
515 ans.la_errno = ESTALE;
518 ans.la_errno = EFBIG;
521 ans.la_errno = EACCES;
530 ans.la_errno = EACCES;
534 ans.la_errno = EAGAIN;
536 /* this is an answer to a nlm_test msg */
537 ans.la_set_getlk_pid = 1;
538 ans.la_getlk_pid = *pid_p;
542 case nlm_denied_nolocks:
543 ans.la_errno = EAGAIN;
548 case nlm_denied_grace_period:
549 ans.la_errno = EAGAIN;
552 ans.la_errno = EDEADLK;
556 if (nfslockdans(LOCKD_ANS_VERSION, &ans)) {
557 syslog(((errno == EPIPE || errno == ESRCH) ?
559 "process %lu: %m", (u_long)ans.la_msg_ident.pid);
567 * Display the contents of a kernel LOCKD_MSG structure.
572 static char hex[] = "0123456789abcdef";
576 u_int8_t *p, *t, buf[NFS_SMALLFH*3+1];
578 syslog(LOG_DEBUG, "process ID: %lu\n", (long)mp->lm_msg_ident.pid);
580 fsidp = (fsid_t *)&mp->lm_fh;
581 fidp = (struct fid *)((u_int8_t *)&mp->lm_fh + sizeof(fsid_t));
583 for (t = buf, p = (u_int8_t *)mp->lm_fh,
585 len > 0; ++p, --len) {
587 *t++ = hex[(*p & 0xf0) >> 4];
588 *t++ = hex[*p & 0x0f];
592 syslog(LOG_DEBUG, "fh_len %d, fh %s\n", (int)mp->lm_fh_len, buf);
594 /* Show flock structure. */
595 syslog(LOG_DEBUG, "start %llu; len %llu; pid %lu; type %d; whence %d\n",
596 (unsigned long long)mp->lm_fl.l_start,
597 (unsigned long long)mp->lm_fl.l_len, (u_long)mp->lm_fl.l_pid,
598 mp->lm_fl.l_type, mp->lm_fl.l_whence);
600 /* Show wait flag. */
601 syslog(LOG_DEBUG, "wait was %s\n", mp->lm_wait ? "set" : "not set");