1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1997 Berkeley Software Design, Inc. 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 * 3. Berkeley Software Design Inc's name may not be used to endorse or
15 * promote products derived from this software without specific prior
16 * written permission.
17 *
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
28 * SUCH DAMAGE.
29 *
30 * from BSDI kern.c,v 1.2 1998/11/25 22:38:27 don Exp
31 */
32
33 #include <sys/param.h>
34 #include <sys/mount.h>
35 #include <sys/queue.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41
42 #include <assert.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <paths.h>
47 #include <pwd.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <syslog.h>
52 #include <unistd.h>
53 #include <netdb.h>
54
55 #include "nlm_prot.h"
56 #include <nfs/nfsproto.h>
57 #include <nfs/nfs_lock.h>
58
59 #include "lockd.h"
60 #include "lockd_lock.h"
61 #include <nfsclient/nfs.h>
62
63 #define DAEMON_USERNAME "daemon"
64
65 /* Lock request owner. */
66 typedef struct __owner {
67 pid_t pid; /* Process ID. */
68 time_t tod; /* Time-of-day. */
69 } OWNER;
70 static OWNER owner;
71
72 static char hostname[MAXHOSTNAMELEN + 1]; /* Hostname. */
73 static int devfd;
74
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 *);
82
83 static int
nfslockdans(int vers,struct lockd_ans * ansp)84 nfslockdans(int vers, struct lockd_ans *ansp)
85 {
86
87 ansp->la_vers = vers;
88 return (write(devfd, ansp, sizeof *ansp) <= 0);
89 }
90
91 /*
92 * will break because fifo needs to be repopened when EOF'd
93 */
94 #define lockd_seteuid(uid) seteuid(uid)
95
96 #define d_calls (debug_level > 1)
97 #define d_args (debug_level > 2)
98
99 static const char *
from_addr(struct sockaddr * saddr)100 from_addr(struct sockaddr *saddr)
101 {
102 static char inet_buf[INET6_ADDRSTRLEN];
103
104 if (getnameinfo(saddr, saddr->sa_len, inet_buf, sizeof(inet_buf),
105 NULL, 0, NI_NUMERICHOST) == 0)
106 return inet_buf;
107 return "???";
108 }
109
110 void
client_cleanup(void)111 client_cleanup(void)
112 {
113 (void)lockd_seteuid(0);
114 exit(-1);
115 }
116
117 /*
118 * client_request --
119 * Loop around messages from the kernel, forwarding them off to
120 * NLM servers.
121 */
122 pid_t
client_request(void)123 client_request(void)
124 {
125 LOCKD_MSG msg;
126 int nr, ret;
127 pid_t child;
128 uid_t daemon_uid;
129 struct passwd *pw;
130
131 /* Open the dev . */
132 devfd = open(_PATH_DEV _PATH_NFSLCKDEV, O_RDWR | O_NONBLOCK);
133 if (devfd < 0) {
134 syslog(LOG_ERR, "open: %s: %m", _PATH_NFSLCKDEV);
135 goto err;
136 }
137
138 signal(SIGPIPE, SIG_IGN);
139
140 /*
141 * Create a separate process, the client code is really a separate
142 * daemon that shares a lot of code.
143 */
144 switch (child = fork()) {
145 case -1:
146 err(1, "fork");
147 case 0:
148 setproctitle("client");
149 break;
150 default:
151 setproctitle("server");
152 return (child);
153 }
154
155 signal(SIGHUP, (sig_t)client_cleanup);
156 signal(SIGTERM, (sig_t)client_cleanup);
157
158 /* Setup. */
159 (void)time(&owner.tod);
160 owner.pid = getpid();
161 (void)gethostname(hostname, sizeof(hostname) - 1);
162
163 pw = getpwnam(DAEMON_USERNAME);
164 if (pw == NULL) {
165 syslog(LOG_ERR, "getpwnam: %s: %m", DAEMON_USERNAME);
166 goto err;
167 }
168 daemon_uid = pw->pw_uid;
169 /* drop our root privileges */
170 (void)lockd_seteuid(daemon_uid);
171
172 for (;;) {
173 /* Read the fixed length message. */
174 if ((nr = read(devfd, &msg, sizeof(msg))) == sizeof(msg)) {
175 if (d_args)
176 show(&msg);
177
178 if (msg.lm_version != LOCKD_MSG_VERSION) {
179 syslog(LOG_ERR,
180 "unknown msg type: %d", msg.lm_version);
181 }
182 /*
183 * Send it to the NLM server and don't grant the lock
184 * if we fail for any reason.
185 */
186 switch (msg.lm_fl.l_type) {
187 case F_RDLCK:
188 case F_WRLCK:
189 if (msg.lm_getlk)
190 ret = test_request(&msg);
191 else
192 ret = lock_request(&msg);
193 break;
194 case F_UNLCK:
195 ret = unlock_request(&msg);
196 break;
197 default:
198 ret = 1;
199 syslog(LOG_ERR,
200 "unknown lock type: %d", msg.lm_fl.l_type);
201 break;
202 }
203 if (ret) {
204 struct lockd_ans ans;
205
206 ans.la_msg_ident = msg.lm_msg_ident;
207 ans.la_errno = EHOSTUNREACH;
208
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);
213 }
214 }
215 } else if (nr == -1) {
216 if (errno != EAGAIN) {
217 syslog(LOG_ERR, "read: %s: %m", _PATH_NFSLCKDEV);
218 goto err;
219 }
220 } else if (nr != 0) {
221 syslog(LOG_ERR,
222 "%s: discard %d bytes", _PATH_NFSLCKDEV, nr);
223 }
224 }
225
226 /* Reached only on error. */
227 err:
228 (void)lockd_seteuid(0);
229 _exit (1);
230 }
231
232 void
set_auth(CLIENT * cl,struct xucred * xucred)233 set_auth(CLIENT *cl, struct xucred *xucred)
234 {
235 int ngroups;
236 gid_t *groups;
237
238 /*
239 * Exclude the first element if it is actually the egid, but account for
240 * the possibility that we could eventually exclude the egid from the
241 * exported group list some day.
242 */
243 ngroups = xucred->cr_ngroups;
244 groups = &xucred->cr_groups[0];
245 if (groups == &xucred->cr_gid) {
246 assert(ngroups > 0);
247 ngroups--;
248 groups++;
249 }
250 if (ngroups > NGRPS)
251 ngroups = NGRPS;
252 if (cl->cl_auth != NULL)
253 cl->cl_auth->ah_ops->ah_destroy(cl->cl_auth);
254 cl->cl_auth = authunix_create(hostname,
255 xucred->cr_uid,
256 xucred->cr_gid,
257 ngroups,
258 groups);
259 }
260
261
262 /*
263 * test_request --
264 * Convert a lock LOCKD_MSG into an NLM request, and send it off.
265 */
266 int
test_request(LOCKD_MSG * msg)267 test_request(LOCKD_MSG *msg)
268 {
269 CLIENT *cli;
270 struct timeval timeout = {0, 0}; /* No timeout, no response. */
271 char dummy;
272
273 if (d_calls)
274 syslog(LOG_DEBUG, "test request: %s: %s to %s",
275 msg->lm_nfsv3 ? "V4" : "V1/3",
276 msg->lm_fl.l_type == F_WRLCK ? "write" : "read",
277 from_addr((struct sockaddr *)&msg->lm_addr));
278
279 if (msg->lm_nfsv3) {
280 struct nlm4_testargs arg4;
281
282 arg4.cookie.n_bytes = (char *)&msg->lm_msg_ident;
283 arg4.cookie.n_len = sizeof(msg->lm_msg_ident);
284 arg4.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0;
285 arg4.alock.caller_name = hostname;
286 arg4.alock.fh.n_bytes = (char *)&msg->lm_fh;
287 arg4.alock.fh.n_len = msg->lm_fh_len;
288 arg4.alock.oh.n_bytes = (char *)&owner;
289 arg4.alock.oh.n_len = sizeof(owner);
290 arg4.alock.svid = msg->lm_msg_ident.pid;
291 arg4.alock.l_offset = msg->lm_fl.l_start;
292 arg4.alock.l_len = msg->lm_fl.l_len;
293
294 if ((cli = get_client(
295 (struct sockaddr *)&msg->lm_addr,
296 NLM_VERS4)) == NULL)
297 return (1);
298
299 set_auth(cli, &msg->lm_cred);
300 (void)clnt_call(cli, NLM_TEST_MSG,
301 (xdrproc_t)xdr_nlm4_testargs, &arg4,
302 (xdrproc_t)xdr_void, &dummy, timeout);
303 } else {
304 struct nlm_testargs arg;
305
306 arg.cookie.n_bytes = (char *)&msg->lm_msg_ident;
307 arg.cookie.n_len = sizeof(msg->lm_msg_ident);
308 arg.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0;
309 arg.alock.caller_name = hostname;
310 arg.alock.fh.n_bytes = (char *)&msg->lm_fh;
311 arg.alock.fh.n_len = msg->lm_fh_len;
312 arg.alock.oh.n_bytes = (char *)&owner;
313 arg.alock.oh.n_len = sizeof(owner);
314 arg.alock.svid = msg->lm_msg_ident.pid;
315 arg.alock.l_offset = msg->lm_fl.l_start;
316 arg.alock.l_len = msg->lm_fl.l_len;
317
318 if ((cli = get_client(
319 (struct sockaddr *)&msg->lm_addr,
320 NLM_VERS)) == NULL)
321 return (1);
322
323 set_auth(cli, &msg->lm_cred);
324 (void)clnt_call(cli, NLM_TEST_MSG,
325 (xdrproc_t)xdr_nlm_testargs, &arg,
326 (xdrproc_t)xdr_void, &dummy, timeout);
327 }
328 return (0);
329 }
330
331 /*
332 * lock_request --
333 * Convert a lock LOCKD_MSG into an NLM request, and send it off.
334 */
335 int
lock_request(LOCKD_MSG * msg)336 lock_request(LOCKD_MSG *msg)
337 {
338 CLIENT *cli;
339 struct nlm4_lockargs arg4;
340 struct nlm_lockargs arg;
341 struct timeval timeout = {0, 0}; /* No timeout, no response. */
342 char dummy;
343
344 if (d_calls)
345 syslog(LOG_DEBUG, "lock request: %s: %s to %s",
346 msg->lm_nfsv3 ? "V4" : "V1/3",
347 msg->lm_fl.l_type == F_WRLCK ? "write" : "read",
348 from_addr((struct sockaddr *)&msg->lm_addr));
349
350 if (msg->lm_nfsv3) {
351 arg4.cookie.n_bytes = (char *)&msg->lm_msg_ident;
352 arg4.cookie.n_len = sizeof(msg->lm_msg_ident);
353 arg4.block = msg->lm_wait ? 1 : 0;
354 arg4.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0;
355 arg4.alock.caller_name = hostname;
356 arg4.alock.fh.n_bytes = (char *)&msg->lm_fh;
357 arg4.alock.fh.n_len = msg->lm_fh_len;
358 arg4.alock.oh.n_bytes = (char *)&owner;
359 arg4.alock.oh.n_len = sizeof(owner);
360 arg4.alock.svid = msg->lm_msg_ident.pid;
361 arg4.alock.l_offset = msg->lm_fl.l_start;
362 arg4.alock.l_len = msg->lm_fl.l_len;
363 arg4.reclaim = 0;
364 arg4.state = nsm_state;
365
366 if ((cli = get_client(
367 (struct sockaddr *)&msg->lm_addr,
368 NLM_VERS4)) == NULL)
369 return (1);
370
371 set_auth(cli, &msg->lm_cred);
372 (void)clnt_call(cli, NLM_LOCK_MSG,
373 (xdrproc_t)xdr_nlm4_lockargs, &arg4,
374 (xdrproc_t)xdr_void, &dummy, timeout);
375 } else {
376 arg.cookie.n_bytes = (char *)&msg->lm_msg_ident;
377 arg.cookie.n_len = sizeof(msg->lm_msg_ident);
378 arg.block = msg->lm_wait ? 1 : 0;
379 arg.exclusive = msg->lm_fl.l_type == F_WRLCK ? 1 : 0;
380 arg.alock.caller_name = hostname;
381 arg.alock.fh.n_bytes = (char *)&msg->lm_fh;
382 arg.alock.fh.n_len = msg->lm_fh_len;
383 arg.alock.oh.n_bytes = (char *)&owner;
384 arg.alock.oh.n_len = sizeof(owner);
385 arg.alock.svid = msg->lm_msg_ident.pid;
386 arg.alock.l_offset = msg->lm_fl.l_start;
387 arg.alock.l_len = msg->lm_fl.l_len;
388 arg.reclaim = 0;
389 arg.state = nsm_state;
390
391 if ((cli = get_client(
392 (struct sockaddr *)&msg->lm_addr,
393 NLM_VERS)) == NULL)
394 return (1);
395
396 set_auth(cli, &msg->lm_cred);
397 (void)clnt_call(cli, NLM_LOCK_MSG,
398 (xdrproc_t)xdr_nlm_lockargs, &arg,
399 (xdrproc_t)xdr_void, &dummy, timeout);
400 }
401 return (0);
402 }
403
404 /*
405 * unlock_request --
406 * Convert an unlock LOCKD_MSG into an NLM request, and send it off.
407 */
408 int
unlock_request(LOCKD_MSG * msg)409 unlock_request(LOCKD_MSG *msg)
410 {
411 CLIENT *cli;
412 struct nlm4_unlockargs arg4;
413 struct nlm_unlockargs arg;
414 struct timeval timeout = {0, 0}; /* No timeout, no response. */
415 char dummy;
416
417 if (d_calls)
418 syslog(LOG_DEBUG, "unlock request: %s: to %s",
419 msg->lm_nfsv3 ? "V4" : "V1/3",
420 from_addr((struct sockaddr *)&msg->lm_addr));
421
422 if (msg->lm_nfsv3) {
423 arg4.cookie.n_bytes = (char *)&msg->lm_msg_ident;
424 arg4.cookie.n_len = sizeof(msg->lm_msg_ident);
425 arg4.alock.caller_name = hostname;
426 arg4.alock.fh.n_bytes = (char *)&msg->lm_fh;
427 arg4.alock.fh.n_len = msg->lm_fh_len;
428 arg4.alock.oh.n_bytes = (char *)&owner;
429 arg4.alock.oh.n_len = sizeof(owner);
430 arg4.alock.svid = msg->lm_msg_ident.pid;
431 arg4.alock.l_offset = msg->lm_fl.l_start;
432 arg4.alock.l_len = msg->lm_fl.l_len;
433
434 if ((cli = get_client(
435 (struct sockaddr *)&msg->lm_addr,
436 NLM_VERS4)) == NULL)
437 return (1);
438
439 set_auth(cli, &msg->lm_cred);
440 (void)clnt_call(cli, NLM_UNLOCK_MSG,
441 (xdrproc_t)xdr_nlm4_unlockargs, &arg4,
442 (xdrproc_t)xdr_void, &dummy, timeout);
443 } else {
444 arg.cookie.n_bytes = (char *)&msg->lm_msg_ident;
445 arg.cookie.n_len = sizeof(msg->lm_msg_ident);
446 arg.alock.caller_name = hostname;
447 arg.alock.fh.n_bytes = (char *)&msg->lm_fh;
448 arg.alock.fh.n_len = msg->lm_fh_len;
449 arg.alock.oh.n_bytes = (char *)&owner;
450 arg.alock.oh.n_len = sizeof(owner);
451 arg.alock.svid = msg->lm_msg_ident.pid;
452 arg.alock.l_offset = msg->lm_fl.l_start;
453 arg.alock.l_len = msg->lm_fl.l_len;
454
455 if ((cli = get_client(
456 (struct sockaddr *)&msg->lm_addr,
457 NLM_VERS)) == NULL)
458 return (1);
459
460 set_auth(cli, &msg->lm_cred);
461 (void)clnt_call(cli, NLM_UNLOCK_MSG,
462 (xdrproc_t)xdr_nlm_unlockargs, &arg,
463 (xdrproc_t)xdr_void, &dummy, timeout);
464 }
465
466 return (0);
467 }
468
469 int
lock_answer(int pid,netobj * netcookie,int result,int * pid_p,int version)470 lock_answer(int pid, netobj *netcookie, int result, int *pid_p, int version)
471 {
472 struct lockd_ans ans;
473
474 if (netcookie->n_len != sizeof(ans.la_msg_ident)) {
475 if (pid == -1) { /* we're screwed */
476 syslog(LOG_ERR, "inedible nlm cookie");
477 return -1;
478 }
479 ans.la_msg_ident.pid = pid;
480 ans.la_msg_ident.msg_seq = -1;
481 } else {
482 memcpy(&ans.la_msg_ident, netcookie->n_bytes,
483 sizeof(ans.la_msg_ident));
484 }
485
486 if (d_calls)
487 syslog(LOG_DEBUG, "lock answer: pid %lu: %s %d",
488 (unsigned long)ans.la_msg_ident.pid,
489 version == NLM_VERS4 ? "nlmv4" : "nlmv3",
490 result);
491
492 ans.la_set_getlk_pid = 0;
493 if (version == NLM_VERS4)
494 switch (result) {
495 case nlm4_granted:
496 ans.la_errno = 0;
497 break;
498 default:
499 ans.la_errno = EACCES;
500 break;
501 case nlm4_denied:
502 if (pid_p == NULL)
503 ans.la_errno = EAGAIN;
504 else {
505 /* this is an answer to a nlm_test msg */
506 ans.la_set_getlk_pid = 1;
507 ans.la_getlk_pid = *pid_p;
508 ans.la_errno = 0;
509 }
510 break;
511 case nlm4_denied_nolocks:
512 ans.la_errno = EAGAIN;
513 break;
514 case nlm4_blocked:
515 return -1;
516 /* NOTREACHED */
517 case nlm4_denied_grace_period:
518 ans.la_errno = EAGAIN;
519 break;
520 case nlm4_deadlck:
521 ans.la_errno = EDEADLK;
522 break;
523 case nlm4_rofs:
524 ans.la_errno = EROFS;
525 break;
526 case nlm4_stale_fh:
527 ans.la_errno = ESTALE;
528 break;
529 case nlm4_fbig:
530 ans.la_errno = EFBIG;
531 break;
532 case nlm4_failed:
533 ans.la_errno = EACCES;
534 break;
535 }
536 else
537 switch (result) {
538 case nlm_granted:
539 ans.la_errno = 0;
540 break;
541 default:
542 ans.la_errno = EACCES;
543 break;
544 case nlm_denied:
545 if (pid_p == NULL)
546 ans.la_errno = EAGAIN;
547 else {
548 /* this is an answer to a nlm_test msg */
549 ans.la_set_getlk_pid = 1;
550 ans.la_getlk_pid = *pid_p;
551 ans.la_errno = 0;
552 }
553 break;
554 case nlm_denied_nolocks:
555 ans.la_errno = EAGAIN;
556 break;
557 case nlm_blocked:
558 return -1;
559 /* NOTREACHED */
560 case nlm_denied_grace_period:
561 ans.la_errno = EAGAIN;
562 break;
563 case nlm_deadlck:
564 ans.la_errno = EDEADLK;
565 break;
566 }
567
568 if (nfslockdans(LOCKD_ANS_VERSION, &ans)) {
569 syslog(((errno == EPIPE || errno == ESRCH) ?
570 LOG_INFO : LOG_ERR),
571 "process %lu: %m", (u_long)ans.la_msg_ident.pid);
572 return -1;
573 }
574 return 0;
575 }
576
577 /*
578 * show --
579 * Display the contents of a kernel LOCKD_MSG structure.
580 */
581 void
show(LOCKD_MSG * mp)582 show(LOCKD_MSG *mp)
583 {
584 static char hex[] = "0123456789abcdef";
585 size_t len;
586 u_int8_t *p, *t, buf[NFS_SMALLFH*3+1];
587
588 syslog(LOG_DEBUG, "process ID: %lu\n", (long)mp->lm_msg_ident.pid);
589
590 for (t = buf, p = (u_int8_t *)mp->lm_fh,
591 len = mp->lm_fh_len;
592 len > 0; ++p, --len) {
593 *t++ = '\\';
594 *t++ = hex[(*p & 0xf0) >> 4];
595 *t++ = hex[*p & 0x0f];
596 }
597 *t = '\0';
598
599 syslog(LOG_DEBUG, "fh_len %d, fh %s\n", (int)mp->lm_fh_len, buf);
600
601 /* Show flock structure. */
602 syslog(LOG_DEBUG, "start %llu; len %llu; pid %lu; type %d; whence %d\n",
603 (unsigned long long)mp->lm_fl.l_start,
604 (unsigned long long)mp->lm_fl.l_len, (u_long)mp->lm_fl.l_pid,
605 mp->lm_fl.l_type, mp->lm_fl.l_whence);
606
607 /* Show wait flag. */
608 syslog(LOG_DEBUG, "wait was %s\n", mp->lm_wait ? "set" : "not set");
609 }
610