]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/nfsdcltrack/nfsdcld.c
473d069da062666086074130aaa546a624b6ea0d
[nfs-utils.git] / utils / nfsdcltrack / nfsdcld.c
1 /*
2  * nfsdcld.c -- NFSv4 client name tracking daemon
3  *
4  * Copyright (C) 2011  Red Hat, Jeff Layton <jlayton@redhat.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif /* HAVE_CONFIG_H */
25
26 #include <errno.h>
27 #include <event.h>
28 #include <stdbool.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <libgen.h>
36 #include <sys/inotify.h>
37 #ifdef HAVE_SYS_CAPABILITY_H
38 #include <sys/prctl.h>
39 #include <sys/capability.h>
40 #endif
41
42 #include "xlog.h"
43 #include "nfslib.h"
44 #include "cld.h"
45 #include "sqlite.h"
46
47 #ifndef PIPEFS_DIR
48 #define PIPEFS_DIR NFS_STATEDIR "/rpc_pipefs"
49 #endif
50
51 #define DEFAULT_CLD_PATH        PIPEFS_DIR "/nfsd/cld"
52
53 #ifndef CLD_DEFAULT_STORAGEDIR
54 #define CLD_DEFAULT_STORAGEDIR NFS_STATEDIR "/nfsdcld"
55 #endif
56
57 #define UPCALL_VERSION          1
58
59 /* private data structures */
60 struct cld_client {
61         int                     cl_fd;
62         struct event            cl_event;
63         struct cld_msg  cl_msg;
64 };
65
66 /* global variables */
67 static char *pipepath = DEFAULT_CLD_PATH;
68 static int              inotify_fd = -1;
69 static struct event     pipedir_event;
70
71 static struct option longopts[] =
72 {
73         { "help", 0, NULL, 'h' },
74         { "foreground", 0, NULL, 'F' },
75         { "debug", 0, NULL, 'd' },
76         { "pipe", 1, NULL, 'p' },
77         { "storagedir", 1, NULL, 's' },
78         { NULL, 0, 0, 0 },
79 };
80
81 /* forward declarations */
82 static void cldcb(int UNUSED(fd), short which, void *data);
83
84 static void
85 usage(char *progname)
86 {
87         printf("%s [ -hFd ] [ -p pipe ] [ -s dir ]\n", progname);
88 }
89
90 static int
91 cld_set_caps(void)
92 {
93         int ret = 0;
94 #ifdef HAVE_SYS_CAPABILITY_H
95         unsigned long i;
96         cap_t caps;
97
98         if (getuid() != 0) {
99                 xlog(L_ERROR, "Not running as root. Daemon won't be able to "
100                               "open the pipe after dropping capabilities!");
101                 return -EINVAL;
102         }
103
104         /* prune the bounding set to nothing */
105         for (i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0 ; ++i) {
106                 ret = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
107                 if (ret) {
108                         xlog(L_ERROR, "Unable to prune capability %lu from "
109                                       "bounding set: %m", i);
110                         return -errno;
111                 }
112         }
113
114         /* get a blank capset */
115         caps = cap_init();
116         if (caps == NULL) {
117                 xlog(L_ERROR, "Unable to get blank capability set: %m");
118                 return -errno;
119         }
120
121         /* reset the process capabilities */
122         if (cap_set_proc(caps) != 0) {
123                 xlog(L_ERROR, "Unable to set process capabilities: %m");
124                 ret = -errno;
125         }
126         cap_free(caps);
127 #endif
128         return ret;
129 }
130
131 #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX)
132
133 static int
134 cld_pipe_open(struct cld_client *clnt)
135 {
136         int fd;
137
138         xlog(D_GENERAL, "%s: opening upcall pipe %s", __func__, pipepath);
139         fd = open(pipepath, O_RDWR, 0);
140         if (fd < 0) {
141                 xlog(D_GENERAL, "%s: open of %s failed: %m", __func__, pipepath);
142                 return -errno;
143         }
144
145         if (clnt->cl_event.ev_flags & EVLIST_INIT)
146                 event_del(&clnt->cl_event);
147         if (clnt->cl_fd >= 0)
148                 close(clnt->cl_fd);
149
150         clnt->cl_fd = fd;
151         event_set(&clnt->cl_event, clnt->cl_fd, EV_READ, cldcb, clnt);
152         /* event_add is done by the caller */
153         return 0;
154 }
155
156 static void
157 cld_inotify_cb(int UNUSED(fd), short which, void *data)
158 {
159         int ret;
160         size_t elen;
161         ssize_t rret;
162         char evbuf[INOTIFY_EVENT_MAX];
163         char *dirc = NULL, *pname;
164         struct inotify_event *event = (struct inotify_event *)evbuf;
165         struct cld_client *clnt = data;
166
167         if (which != EV_READ)
168                 return;
169
170         xlog(D_GENERAL, "%s: called for EV_READ", __func__);
171
172         dirc = strndup(pipepath, PATH_MAX);
173         if (!dirc) {
174                 xlog(L_ERROR, "%s: unable to allocate memory", __func__);
175                 goto out;
176         }
177
178         rret = read(inotify_fd, evbuf, INOTIFY_EVENT_MAX);
179         if (rret < 0) {
180                 xlog(L_ERROR, "%s: read from inotify fd failed: %m", __func__);
181                 goto out;
182         }
183
184         /* check to see if we have a filename in the evbuf */
185         if (!event->len) {
186                 xlog(D_GENERAL, "%s: no filename in inotify event", __func__);
187                 goto out;
188         }
189
190         pname = basename(dirc);
191         elen = strnlen(event->name, event->len);
192
193         /* does the filename match our pipe? */
194         if (strlen(pname) != elen || memcmp(pname, event->name, elen)) {
195                 xlog(D_GENERAL, "%s: wrong filename (%s)", __func__,
196                                 event->name);
197                 goto out;
198         }
199
200         ret = cld_pipe_open(clnt);
201         switch (ret) {
202         case 0:
203                 /* readd the event for the cl_event pipe */
204                 event_add(&clnt->cl_event, NULL);
205                 break;
206         case -ENOENT:
207                 /* pipe must have disappeared, wait for it to come back */
208                 goto out;
209         default:
210                 /* anything else is fatal */
211                 xlog(L_FATAL, "%s: unable to open new pipe (%d). Aborting.",
212                         ret, __func__);
213                 exit(ret);
214         }
215
216 out:
217         event_add(&pipedir_event, NULL);
218         free(dirc);
219 }
220
221 static int
222 cld_inotify_setup(void)
223 {
224         int ret;
225         char *dirc, *dname;
226
227         dirc = strndup(pipepath, PATH_MAX);
228         if (!dirc) {
229                 xlog_err("%s: unable to allocate memory", __func__);
230                 ret = -ENOMEM;
231                 goto out_free;
232         }
233
234         dname = dirname(dirc);
235
236         inotify_fd = inotify_init();
237         if (inotify_fd < 0) {
238                 xlog_err("%s: inotify_init failed: %m", __func__);
239                 ret = -errno;
240                 goto out_free;
241         }
242
243         ret = inotify_add_watch(inotify_fd, dname, IN_CREATE);
244         if (ret < 0) {
245                 xlog_err("%s: inotify_add_watch failed: %m", __func__);
246                 ret = -errno;
247                 goto out_err;
248         }
249
250 out_free:
251         free(dirc);
252         return 0;
253 out_err:
254         close(inotify_fd);
255         goto out_free;
256 }
257
258 /*
259  * Set an inotify watch on the directory that should contain the pipe, and then
260  * try to open it. If it fails with anything but -ENOENT, return the error
261  * immediately.
262  *
263  * If it succeeds, then set up the pipe event handler. At that point, set up
264  * the inotify event handler and go ahead and return success.
265  */
266 static int
267 cld_pipe_init(struct cld_client *clnt)
268 {
269         int ret;
270
271         xlog(D_GENERAL, "%s: init pipe handlers", __func__);
272
273         ret = cld_inotify_setup();
274         if (ret != 0)
275                 goto out;
276
277         clnt->cl_fd = -1;
278         ret = cld_pipe_open(clnt);
279         switch (ret) {
280         case 0:
281                 /* add the event and we're good to go */
282                 event_add(&clnt->cl_event, NULL);
283                 break;
284         case -ENOENT:
285                 /* ignore this error -- cld_inotify_cb will handle it */
286                 ret = 0;
287                 break;
288         default:
289                 /* anything else is fatal */
290                 close(inotify_fd);
291                 goto out;
292         }
293
294         /* set event for inotify read */
295         event_set(&pipedir_event, inotify_fd, EV_READ, cld_inotify_cb, clnt);
296         event_add(&pipedir_event, NULL);
297 out:
298         return ret;
299 }
300
301 static void
302 cld_not_implemented(struct cld_client *clnt)
303 {
304         int ret;
305         ssize_t bsize, wsize;
306         struct cld_msg *cmsg = &clnt->cl_msg;
307
308         xlog(D_GENERAL, "%s: downcalling with not implemented error", __func__);
309
310         /* set up reply */
311         cmsg->cm_status = -EOPNOTSUPP;
312
313         bsize = sizeof(*cmsg);
314
315         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
316         if (wsize != bsize)
317                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
318                          __func__, wsize);
319
320         /* reopen pipe, just to be sure */
321         ret = cld_pipe_open(clnt);
322         if (ret) {
323                 xlog(L_FATAL, "%s: unable to reopen pipe: %d", __func__, ret);
324                 exit(ret);
325         }
326 }
327
328 static void
329 cld_create(struct cld_client *clnt)
330 {
331         int ret;
332         ssize_t bsize, wsize;
333         struct cld_msg *cmsg = &clnt->cl_msg;
334
335         xlog(D_GENERAL, "%s: create client record.", __func__);
336
337         ret = sqlite_insert_client(cmsg->cm_u.cm_name.cn_id,
338                                    cmsg->cm_u.cm_name.cn_len);
339
340         cmsg->cm_status = ret ? -EREMOTEIO : ret;
341
342         bsize = sizeof(*cmsg);
343
344         xlog(D_GENERAL, "Doing downcall with status %d", cmsg->cm_status);
345         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
346         if (wsize != bsize) {
347                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
348                          __func__, wsize);
349                 ret = cld_pipe_open(clnt);
350                 if (ret) {
351                         xlog(L_FATAL, "%s: unable to reopen pipe: %d",
352                                         __func__, ret);
353                         exit(ret);
354                 }
355         }
356 }
357
358 static void
359 cld_remove(struct cld_client *clnt)
360 {
361         int ret;
362         ssize_t bsize, wsize;
363         struct cld_msg *cmsg = &clnt->cl_msg;
364
365         xlog(D_GENERAL, "%s: remove client record.", __func__);
366
367         ret = sqlite_remove_client(cmsg->cm_u.cm_name.cn_id,
368                                    cmsg->cm_u.cm_name.cn_len);
369
370         cmsg->cm_status = ret ? -EREMOTEIO : ret;
371
372         bsize = sizeof(*cmsg);
373
374         xlog(D_GENERAL, "%s: downcall with status %d", __func__,
375                         cmsg->cm_status);
376         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
377         if (wsize != bsize) {
378                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
379                          __func__, wsize);
380                 ret = cld_pipe_open(clnt);
381                 if (ret) {
382                         xlog(L_FATAL, "%s: unable to reopen pipe: %d",
383                                         __func__, ret);
384                         exit(ret);
385                 }
386         }
387 }
388
389 static void
390 cld_check(struct cld_client *clnt)
391 {
392         int ret;
393         ssize_t bsize, wsize;
394         struct cld_msg *cmsg = &clnt->cl_msg;
395
396         xlog(D_GENERAL, "%s: check client record", __func__);
397
398         ret = sqlite_check_client(cmsg->cm_u.cm_name.cn_id,
399                                   cmsg->cm_u.cm_name.cn_len);
400
401         /* set up reply */
402         cmsg->cm_status = ret ? -EACCES : ret;
403
404         bsize = sizeof(*cmsg);
405
406         xlog(D_GENERAL, "%s: downcall with status %d", __func__,
407                         cmsg->cm_status);
408         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
409         if (wsize != bsize) {
410                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
411                          __func__, wsize);
412                 ret = cld_pipe_open(clnt);
413                 if (ret) {
414                         xlog(L_FATAL, "%s: unable to reopen pipe: %d",
415                                         __func__, ret);
416                         exit(ret);
417                 }
418         }
419 }
420
421 static void
422 cld_gracedone(struct cld_client *clnt)
423 {
424         int ret;
425         ssize_t bsize, wsize;
426         struct cld_msg *cmsg = &clnt->cl_msg;
427
428         xlog(D_GENERAL, "%s: grace done. cm_gracetime=%ld", __func__,
429                         cmsg->cm_u.cm_gracetime);
430
431         ret = sqlite_remove_unreclaimed(cmsg->cm_u.cm_gracetime);
432
433         /* set up reply: downcall with 0 status */
434         cmsg->cm_status = ret ? -EREMOTEIO : ret;
435
436         bsize = sizeof(*cmsg);
437
438         xlog(D_GENERAL, "Doing downcall with status %d", cmsg->cm_status);
439         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
440         if (wsize != bsize) {
441                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
442                          __func__, wsize);
443                 ret = cld_pipe_open(clnt);
444                 if (ret) {
445                         xlog(L_FATAL, "%s: unable to reopen pipe: %d",
446                                         __func__, ret);
447                         exit(ret);
448                 }
449         }
450 }
451
452 static void
453 cldcb(int UNUSED(fd), short which, void *data)
454 {
455         ssize_t len;
456         struct cld_client *clnt = data;
457         struct cld_msg *cmsg = &clnt->cl_msg;
458
459         if (which != EV_READ)
460                 goto out;
461
462         len = atomicio(read, clnt->cl_fd, cmsg, sizeof(*cmsg));
463         if (len <= 0) {
464                 xlog(L_ERROR, "%s: pipe read failed: %m", __func__);
465                 cld_pipe_open(clnt);
466                 goto out;
467         }
468
469         if (cmsg->cm_vers != UPCALL_VERSION) {
470                 xlog(L_ERROR, "%s: unsupported upcall version: %hu",
471                                 cmsg->cm_vers);
472                 cld_pipe_open(clnt);
473                 goto out;
474         }
475
476         switch(cmsg->cm_cmd) {
477         case Cld_Create:
478                 cld_create(clnt);
479                 break;
480         case Cld_Remove:
481                 cld_remove(clnt);
482                 break;
483         case Cld_Check:
484                 cld_check(clnt);
485                 break;
486         case Cld_GraceDone:
487                 cld_gracedone(clnt);
488                 break;
489         default:
490                 xlog(L_WARNING, "%s: command %u is not yet implemented",
491                                 __func__, cmsg->cm_cmd);
492                 cld_not_implemented(clnt);
493         }
494 out:
495         event_add(&clnt->cl_event, NULL);
496 }
497
498 int
499 main(int argc, char **argv)
500 {
501         char arg;
502         int rc = 0;
503         bool foreground = false;
504         char *progname;
505         char *storagedir = CLD_DEFAULT_STORAGEDIR;
506         struct cld_client clnt;
507
508         memset(&clnt, 0, sizeof(clnt));
509
510         progname = strdup(basename(argv[0]));
511         if (!progname) {
512                 fprintf(stderr, "%s: unable to allocate memory.\n", argv[0]);
513                 return 1;
514         }
515
516         event_init();
517         xlog_syslog(0);
518         xlog_stderr(1);
519
520         /* process command-line options */
521         while ((arg = getopt_long(argc, argv, "hdFp:s:", longopts,
522                                   NULL)) != EOF) {
523                 switch (arg) {
524                 case 'd':
525                         xlog_config(D_ALL, 1);
526                         break;
527                 case 'F':
528                         foreground = true;
529                         break;
530                 case 'p':
531                         pipepath = optarg;
532                         break;
533                 case 's':
534                         storagedir = optarg;
535                         break;
536                 default:
537                         usage(progname);
538                         return 0;
539                 }
540         }
541
542
543         xlog_open(progname);
544         if (!foreground) {
545                 xlog_syslog(1);
546                 xlog_stderr(0);
547                 rc = daemon(0, 0);
548                 if (rc) {
549                         xlog(L_ERROR, "Unable to daemonize: %m");
550                         goto out;
551                 }
552         }
553
554         /* drop all capabilities */
555         rc = cld_set_caps();
556         if (rc)
557                 goto out;
558
559         /*
560          * now see if the storagedir is writable by root w/o CAP_DAC_OVERRIDE.
561          * If it isn't then give the user a warning but proceed as if
562          * everything is OK. If the DB has already been created, then
563          * everything might still work. If it doesn't exist at all, then
564          * assume that the maindb init will be able to create it. Fail on
565          * anything else.
566          */
567         if (access(storagedir, W_OK) == -1) {
568                 switch (errno) {
569                 case EACCES:
570                         xlog(L_WARNING, "Storage directory %s is not writable. "
571                                         "Should be owned by root and writable "
572                                         "by owner!", storagedir);
573                         break;
574                 case ENOENT:
575                         /* ignore and assume that we can create dir as root */
576                         break;
577                 default:
578                         xlog(L_ERROR, "Unexpected error when checking access "
579                                       "on %s: %m", storagedir);
580                         rc = -errno;
581                         goto out;
582                 }
583         }
584
585         /* set up storage db */
586         rc = sqlite_maindb_init(storagedir);
587         if (rc) {
588                 xlog(L_ERROR, "Failed to open main database: %d", rc);
589                 goto out;
590         }
591
592         /* set up event handler */
593         rc = cld_pipe_init(&clnt);
594         if (rc)
595                 goto out;
596
597         xlog(D_GENERAL, "%s: Starting event dispatch handler.", __func__);
598         rc = event_dispatch();
599         if (rc < 0)
600                 xlog(L_ERROR, "%s: event_dispatch failed: %m", __func__);
601
602         close(clnt.cl_fd);
603         close(inotify_fd);
604 out:
605         free(progname);
606         return rc;
607 }