]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/sm-notify.c
bb6c2efa3f4e949191f4831757f7cf5e649eafa6
[nfs-utils.git] / utils / statd / sm-notify.c
1 /*
2  * Send NSM notify calls to all hosts listed in /var/lib/sm
3  *
4  * Copyright (C) 2004-2006 Olaf Kirch <okir@suse.de>
5  */
6
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <sys/stat.h>
10 #include <sys/poll.h>
11 #include <sys/param.h>
12 #include <sys/syslog.h>
13 #include <arpa/inet.h>
14 #include <dirent.h>
15 #include <time.h>
16 #include <stdio.h>
17 #include <getopt.h>
18 #include <stdlib.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <stdarg.h>
23 #include <netdb.h>
24 #include <errno.h>
25 #include <grp.h>
26
27 #ifndef BASEDIR
28 # ifdef NFS_STATEDIR
29 #  define BASEDIR               NFS_STATEDIR
30 # else
31 #  define BASEDIR               "/var/lib/nfs"
32 # endif
33 #endif
34
35 #define DEFAULT_SM_STATE_PATH   BASEDIR "/state"
36 #define DEFAULT_SM_DIR_PATH     BASEDIR "/sm"
37 #define DEFAULT_SM_BAK_PATH     DEFAULT_SM_DIR_PATH ".bak"
38
39 char *_SM_BASE_PATH = BASEDIR;
40 char *_SM_STATE_PATH = DEFAULT_SM_STATE_PATH;
41 char *_SM_DIR_PATH = DEFAULT_SM_DIR_PATH;
42 char *_SM_BAK_PATH = DEFAULT_SM_BAK_PATH;
43
44 #define NSM_PROG        100024
45 #define NSM_PROGRAM     100024
46 #define NSM_VERSION     1
47 #define NSM_TIMEOUT     2
48 #define NSM_NOTIFY      6
49 #define NSM_MAX_TIMEOUT 120     /* don't make this too big */
50 #define MAXMSGSIZE      256
51
52 typedef struct sockaddr_storage nsm_address;
53
54 struct nsm_host {
55         struct nsm_host *       next;
56         char *                  name;
57         char *                  path;
58         nsm_address             addr;
59         struct addrinfo         *ai;
60         time_t                  last_used;
61         time_t                  send_next;
62         unsigned int            timeout;
63         unsigned int            retries;
64         unsigned int            xid;
65 };
66
67 static char             nsm_hostname[256];
68 static uint32_t         nsm_state;
69 static int              opt_debug = 0;
70 static int              opt_quiet = 0;
71 static int              opt_update_state = 1;
72 static unsigned int     opt_max_retry = 15 * 60;
73 static char *           opt_srcaddr = 0;
74 static uint16_t         opt_srcport = 0;
75 static int              log_syslog = 0;
76
77 static unsigned int     nsm_get_state(int);
78 static void             notify(void);
79 static void             notify_host(int, struct nsm_host *);
80 static void             recv_reply(int);
81 static void             backup_hosts(const char *, const char *);
82 static void             get_hosts(const char *);
83 static void             insert_host(struct nsm_host *);
84 struct nsm_host *       find_host(uint32_t);
85 static int              addr_get_port(nsm_address *);
86 static void             addr_set_port(nsm_address *, int);
87 static struct addrinfo  *host_lookup(int, const char *);
88 void                    nsm_log(int fac, const char *fmt, ...);
89 static int              record_pid();
90 static void             drop_privs(void);
91 static void set_kernel_nsm_state(int state);
92
93 static struct nsm_host *        hosts = NULL;
94
95 int
96 main(int argc, char **argv)
97 {
98         int     c;
99         int     force = 0;
100
101         while ((c = getopt(argc, argv, "dm:np:v:qP:f")) != -1) {
102                 switch (c) {
103                 case 'f':
104                         force = 1;
105                         break;
106                 case 'd':
107                         opt_debug++;
108                         break;
109                 case 'm':
110                         opt_max_retry = atoi(optarg) * 60;
111                         break;
112                 case 'n':
113                         opt_update_state = 0;
114                         break;
115                 case 'p':
116                         opt_srcport = atoi(optarg);
117                         break;
118                 case 'v':
119                         opt_srcaddr = optarg;
120                         break;
121                 case 'q':
122                         opt_quiet = 1;
123                         break;
124                 case 'P':
125                         _SM_BASE_PATH = strdup(optarg);
126                         _SM_STATE_PATH = malloc(strlen(optarg)+1+sizeof("state"));
127                         _SM_DIR_PATH = malloc(strlen(optarg)+1+sizeof("sm"));
128                         _SM_BAK_PATH = malloc(strlen(optarg)+1+sizeof("sm.bak"));
129                         if (_SM_BASE_PATH == NULL ||
130                             _SM_STATE_PATH == NULL ||
131                             _SM_DIR_PATH == NULL ||
132                             _SM_BAK_PATH == NULL) {
133                                 nsm_log(LOG_WARNING, "unable to allocate memory");
134                                 exit(1);
135                         }
136                         strcat(strcpy(_SM_STATE_PATH, _SM_BASE_PATH), "/state");
137                         strcat(strcpy(_SM_DIR_PATH, _SM_BASE_PATH), "/sm");
138                         strcat(strcpy(_SM_BAK_PATH, _SM_BASE_PATH), "/sm.bak");
139                         break;
140
141                 default:
142                         goto usage;
143                 }
144         }
145
146         if (optind < argc) {
147 usage:          fprintf(stderr,
148                         "Usage: sm-notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
149                         "            [-P /path/to/state/directory] [-v my_host_name]\n");
150                 return 1;
151         }
152
153         if (strcmp(_SM_BASE_PATH, BASEDIR) == 0) {
154                 if (record_pid() == 0 && force == 0 && opt_update_state == 1)
155                         /* already run, don't try again */
156                         exit(0);
157         }
158
159         if (opt_srcaddr) {
160                 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
161         } else
162         if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
163                 perror("gethostname");
164                 return 1;
165         }
166
167         backup_hosts(_SM_DIR_PATH, _SM_BAK_PATH);
168         get_hosts(_SM_BAK_PATH);
169
170         /* Get and update the NSM state. This will call sync() */
171         nsm_state = nsm_get_state(opt_update_state);
172         set_kernel_nsm_state(nsm_state);
173
174         if (!opt_debug) {
175                 if (!opt_quiet)
176                         printf("Backgrounding to notify hosts...\n");
177
178                 openlog("sm-notify", LOG_PID, LOG_DAEMON);
179                 log_syslog = 1;
180
181                 if (daemon(0, 0) < 0) {
182                         nsm_log(LOG_WARNING, "unable to background: %s",
183                                         strerror(errno));
184                         return 1;
185                 }
186
187                 close(0);
188                 close(1);
189                 close(2);
190         }
191
192         notify();
193
194         if (hosts) {
195                 struct nsm_host *hp;
196
197                 while ((hp = hosts) != 0) {
198                         hosts = hp->next;
199                         nsm_log(LOG_NOTICE,
200                                 "Unable to notify %s, giving up",
201                                 hp->name);
202                 }
203                 return 1;
204         }
205
206         return 0;
207 }
208
209 /*
210  * Notify hosts
211  */
212 void
213 notify(void)
214 {
215         nsm_address local_addr;
216         time_t  failtime = 0;
217         int     sock = -1;
218
219         sock = socket(AF_INET, SOCK_DGRAM, 0);
220         if (sock < 0) {
221                 perror("socket");
222                 exit(1);
223         }
224         fcntl(sock, F_SETFL, O_NONBLOCK);
225
226         memset(&local_addr, 0, sizeof(local_addr));
227         local_addr.ss_family = AF_INET; /* Default to IPv4 */
228
229         /* Bind source IP if provided on command line */
230         if (opt_srcaddr) {
231                 struct addrinfo *ai = host_lookup(AF_INET, opt_srcaddr);
232                 if (!ai) {
233                         nsm_log(LOG_WARNING,
234                                 "Not a valid hostname or address: \"%s\"\n",
235                                 opt_srcaddr);
236                         exit(1);
237                 }
238                 memcpy(&local_addr, ai->ai_addr, ai->ai_addrlen);
239                 /* We know it's IPv4 at this point */
240         }
241
242         /* Use source port if provided on the command line,
243          * otherwise use bindresvport */
244         if (opt_srcport) {
245                 addr_set_port(&local_addr, opt_srcport);
246                 if (bind(sock, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0) {
247                         perror("bind");
248                         exit(1);
249                 }
250         } else {
251                 (void) bindresvport(sock, (struct sockaddr_in *) &local_addr);
252         }
253
254         if (opt_max_retry)
255                 failtime = time(NULL) + opt_max_retry;
256
257         drop_privs();
258
259         while (hosts) {
260                 struct pollfd   pfd;
261                 time_t          now = time(NULL);
262                 unsigned int    sent = 0;
263                 struct nsm_host *hp;
264                 long            wait;
265
266                 if (failtime && now >= failtime)
267                         break;
268
269                 while ((wait = hosts->send_next - now) <= 0) {
270                         /* Never send more than 10 packets at once */
271                         if (sent++ >= 10)
272                                 break;
273
274                         /* Remove queue head */
275                         hp = hosts;
276                         hosts = hp->next;
277
278                         notify_host(sock, hp);
279
280                         /* Set the timeout for this call, using an
281                            exponential timeout strategy */
282                         wait = hp->timeout;
283                         if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
284                                 hp->timeout = NSM_MAX_TIMEOUT;
285                         hp->send_next = now + wait;
286                         hp->retries++;
287
288                         insert_host(hp);
289                 }
290
291                 nsm_log(LOG_DEBUG, "Host %s due in %ld seconds",
292                                 hosts->name, wait);
293
294                 pfd.fd = sock;
295                 pfd.events = POLLIN;
296
297                 wait *= 1000;
298                 if (wait < 100)
299                         wait = 100;
300                 if (poll(&pfd, 1, wait) != 1)
301                         continue;
302
303                 recv_reply(sock);
304         }
305 }
306
307 /*
308  * Send notification to a single host
309  */
310 void
311 notify_host(int sock, struct nsm_host *host)
312 {
313         static unsigned int     xid = 0;
314         nsm_address             dest;
315         uint32_t                msgbuf[MAXMSGSIZE], *p;
316         unsigned int            len;
317
318         if (!xid)
319                 xid = getpid() + time(NULL);
320         if (!host->xid)
321                 host->xid = xid++;
322
323         memset(msgbuf, 0, sizeof(msgbuf));
324         p = msgbuf;
325         *p++ = htonl(host->xid);
326         *p++ = 0;
327         *p++ = htonl(2);
328
329         /* If we retransmitted 4 times, reset the port to force
330          * a new portmap lookup (in case statd was restarted).
331          * We also rotate through multiple IP addresses at this
332          * point.
333          */
334         if (host->retries >= 4) {
335                 struct addrinfo *hold = host->ai;
336                 struct addrinfo **next = &host->ai;
337                 *next = hold->ai_next;
338                 while ( *next )
339                         next = & (*next)->ai_next;
340                 *next = hold;
341                 hold->ai_next = NULL;
342                 memcpy(&host->addr, hold->ai_addr, hold->ai_addrlen);
343                 addr_set_port(&host->addr, 0);
344                 host->retries = 0;
345         }
346
347         dest = host->addr;
348         if (addr_get_port(&dest) == 0) {
349                 /* Build a PMAP packet */
350                 nsm_log(LOG_DEBUG, "Sending portmap query to %s", host->name);
351
352                 addr_set_port(&dest, 111);
353                 *p++ = htonl(100000);
354                 *p++ = htonl(2);
355                 *p++ = htonl(3);
356
357                 /* Auth and verf */
358                 *p++ = 0; *p++ = 0;
359                 *p++ = 0; *p++ = 0;
360
361                 *p++ = htonl(NSM_PROGRAM);
362                 *p++ = htonl(NSM_VERSION);
363                 *p++ = htonl(IPPROTO_UDP);
364                 *p++ = 0;
365         } else {
366                 /* Build an SM_NOTIFY packet */
367                 nsm_log(LOG_DEBUG, "Sending SM_NOTIFY to %s", host->name);
368
369                 *p++ = htonl(NSM_PROGRAM);
370                 *p++ = htonl(NSM_VERSION);
371                 *p++ = htonl(NSM_NOTIFY);
372
373                 /* Auth and verf */
374                 *p++ = 0; *p++ = 0;
375                 *p++ = 0; *p++ = 0;
376
377                 /* state change */
378                 len = strlen(nsm_hostname);
379                 *p++ = htonl(len);
380                 memcpy(p, nsm_hostname, len);
381                 p += (len + 3) >> 2;
382                 *p++ = htonl(nsm_state);
383         }
384         len = (p - msgbuf) << 2;
385
386         sendto(sock, msgbuf, len, 0, (struct sockaddr *) &dest, sizeof(dest));
387 }
388
389 /*
390  * Receive reply from remote host
391  */
392 void
393 recv_reply(int sock)
394 {
395         struct nsm_host *hp;
396         uint32_t        msgbuf[MAXMSGSIZE], *p, *end;
397         uint32_t        xid;
398         int             res;
399
400         res = recv(sock, msgbuf, sizeof(msgbuf), 0);
401         if (res < 0)
402                 return;
403
404         nsm_log(LOG_DEBUG, "Received packet...");
405
406         p = msgbuf;
407         end = p + (res >> 2);
408
409         xid = ntohl(*p++);
410         if (*p++ != htonl(1)    /* must be REPLY */
411          || *p++ != htonl(0)    /* must be ACCEPTED */
412          || *p++ != htonl(0)    /* must be NULL verifier */
413          || *p++ != htonl(0)
414          || *p++ != htonl(0))   /* must be SUCCESS */
415                 return;
416
417         /* Before we look at the data, find the host struct for
418            this reply */
419         if ((hp = find_host(xid)) == NULL)
420                 return;
421
422         if (addr_get_port(&hp->addr) == 0) {
423                 /* This was a portmap request */
424                 unsigned int    port;
425
426                 port = ntohl(*p++);
427                 if (p > end)
428                         goto fail;
429
430                 hp->send_next = time(NULL);
431                 if (port == 0) {
432                         /* No binding for statd. Delay the next
433                          * portmap query for max timeout */
434                         nsm_log(LOG_DEBUG, "No statd on %s", hp->name);
435                         hp->timeout = NSM_MAX_TIMEOUT;
436                         hp->send_next += NSM_MAX_TIMEOUT;
437                 } else {
438                         addr_set_port(&hp->addr, port);
439                         if (hp->timeout >= NSM_MAX_TIMEOUT / 4)
440                                 hp->timeout = NSM_MAX_TIMEOUT / 4;
441                 }
442                 hp->xid = 0;
443         } else {
444                 /* Successful NOTIFY call. Server returns void,
445                  * so nothing we need to do here (except
446                  * check that we didn't read past the end of the
447                  * packet)
448                  */
449                 if (p <= end) {
450                         nsm_log(LOG_DEBUG, "Host %s notified successfully", hp->name);
451                         unlink(hp->path);
452                         free(hp->name);
453                         free(hp->path);
454                         free(hp);
455                         freeaddrinfo(hp->ai);
456                         return;
457                 }
458         }
459
460 fail:   /* Re-insert the host */
461         insert_host(hp);
462 }
463
464 /*
465  * Back up all hosts from the sm directory to sm.bak
466  */
467 static void
468 backup_hosts(const char *dirname, const char *bakname)
469 {
470         struct dirent   *de;
471         DIR             *dir;
472
473         if (!(dir = opendir(dirname))) {
474                 perror(dirname);
475                 return;
476         }
477
478         while ((de = readdir(dir)) != NULL) {
479                 char    src[1024], dst[1024];
480
481                 if (de->d_name[0] == '.')
482                         continue;
483
484                 snprintf(src, sizeof(src), "%s/%s", dirname, de->d_name);
485                 snprintf(dst, sizeof(dst), "%s/%s", bakname, de->d_name);
486                 if (rename(src, dst) < 0) {
487                         nsm_log(LOG_WARNING,
488                                 "Failed to rename %s -> %s: %m",
489                                 src, dst);
490                 }
491         }
492         closedir(dir);
493 }
494
495 /*
496  * Get all entries from sm.bak and convert them to host names
497  */
498 static void
499 get_hosts(const char *dirname)
500 {
501         struct nsm_host *host;
502         struct dirent   *de;
503         DIR             *dir;
504
505         if (!(dir = opendir(dirname))) {
506                 perror(dirname);
507                 return;
508         }
509
510         host = NULL;
511         while ((de = readdir(dir)) != NULL) {
512                 struct stat     stb;
513                 char            path[1024];
514
515                 if (de->d_name[0] == '.')
516                         continue;
517                 if (host == NULL)
518                         host = calloc(1, sizeof(*host));
519
520                 snprintf(path, sizeof(path), "%s/%s", dirname, de->d_name);
521                 if (stat(path, &stb) < 0)
522                         continue;
523
524                 host->ai = host_lookup(AF_UNSPEC, de->d_name);
525                 if (! host->ai) {
526                         nsm_log(LOG_WARNING,
527                                 "%s doesn't seem to be a valid address, skipped",
528                                 de->d_name);
529                         unlink(path);
530                         continue;
531                 }
532
533                 host->last_used = stb.st_mtime;
534                 host->timeout = NSM_TIMEOUT;
535                 host->path = strdup(path);
536                 host->name = strdup(de->d_name);
537                 host->retries = 100; /* force address retry */
538
539                 insert_host(host);
540                 host = NULL;
541         }
542         closedir(dir);
543
544         if (host)
545                 free(host);
546 }
547
548 /*
549  * Insert host into sorted list
550  */
551 void
552 insert_host(struct nsm_host *host)
553 {
554         struct nsm_host **where, *p;
555
556         where = &hosts;
557         while ((p = *where) != 0) {
558                 /* Sort in ascending order of timeout */
559                 if (host->send_next < p->send_next)
560                         break;
561                 /* If we have the same timeout, put the
562                  * most recently used host first.
563                  * This makes sure that "recent" hosts
564                  * get notified first.
565                  */
566                 if (host->send_next == p->send_next
567                  && host->last_used > p->last_used)
568                         break;
569                 where = &p->next;
570         }
571
572         host->next = *where;
573         *where = host;
574 }
575
576 /*
577  * Find host given the XID
578  */
579 struct nsm_host *
580 find_host(uint32_t xid)
581 {
582         struct nsm_host **where, *p;
583
584         where = &hosts;
585         while ((p = *where) != 0) {
586                 if (p->xid == xid) {
587                         *where = p->next;
588                         return p;
589                 }
590                 where = &p->next;
591         }
592         return NULL;
593 }
594
595
596 /*
597  * Retrieve the current NSM state
598  */
599 unsigned int
600 nsm_get_state(int update)
601 {
602         char            newfile[PATH_MAX];
603         int             fd, state;
604
605         if ((fd = open(_SM_STATE_PATH, O_RDONLY)) < 0) {
606                 if (!opt_quiet) {
607                         nsm_log(LOG_WARNING, "%s: %m", _SM_STATE_PATH);
608                         nsm_log(LOG_WARNING, "Creating %s, set initial state 1",
609                                 _SM_STATE_PATH);
610                 }
611                 state = 1;
612                 update = 1;
613         } else {
614                 if (read(fd, &state, sizeof(state)) != sizeof(state)) {
615                         nsm_log(LOG_WARNING,
616                                 "%s: bad file size, setting state = 1",
617                                 _SM_STATE_PATH);
618                         state = 1;
619                         update = 1;
620                 } else {
621                         if (!(state & 1))
622                                 state += 1;
623                 }
624                 close(fd);
625         }
626
627         if (update) {
628                 state += 2;
629                 snprintf(newfile, sizeof(newfile),
630                                 "%s.new", _SM_STATE_PATH);
631                 if ((fd = open(newfile, O_CREAT|O_WRONLY, 0644)) < 0) {
632                         nsm_log(LOG_WARNING, "Cannot create %s: %m", newfile);
633                         exit(1);
634                 }
635                 if (write(fd, &state, sizeof(state)) != sizeof(state)) {
636                         nsm_log(LOG_WARNING,
637                                 "Failed to write state to %s", newfile);
638                         exit(1);
639                 }
640                 close(fd);
641                 if (rename(newfile, _SM_STATE_PATH) < 0) {
642                         nsm_log(LOG_WARNING,
643                                 "Cannot create %s: %m", _SM_STATE_PATH);
644                         exit(1);
645                 }
646                 sync();
647         }
648
649         return state;
650 }
651
652 /*
653  * Address handling utilities
654  */
655
656 int
657 addr_get_port(nsm_address *addr)
658 {
659         switch (((struct sockaddr *) addr)->sa_family) {
660         case AF_INET:
661                 return ntohs(((struct sockaddr_in *) addr)->sin_port);
662         case AF_INET6:
663                 return ntohs(((struct sockaddr_in6 *) addr)->sin6_port);
664         }
665         return 0;
666 }
667
668 static void
669 addr_set_port(nsm_address *addr, int port)
670 {
671         switch (((struct sockaddr *) addr)->sa_family) {
672         case AF_INET:
673                 ((struct sockaddr_in *) addr)->sin_port = htons(port);
674                 break;
675         case AF_INET6:
676                 ((struct sockaddr_in6 *) addr)->sin6_port = htons(port);
677         }
678 }
679
680 static struct addrinfo *
681 host_lookup(int af, const char *name)
682 {
683         struct addrinfo hints, *ai;
684
685         memset(&hints, 0, sizeof(hints));
686         hints.ai_family = af;
687         hints.ai_protocol = IPPROTO_UDP;
688
689         if (getaddrinfo(name, NULL, &hints, &ai) != 0)
690                 return NULL;
691
692         return ai;
693 }
694
695 /*
696  * Log a message
697  */
698 void
699 nsm_log(int fac, const char *fmt, ...)
700 {
701         va_list ap;
702
703         if (fac == LOG_DEBUG && !opt_debug)
704                 return;
705
706         va_start(ap, fmt);
707         if (log_syslog)
708                 vsyslog(fac, fmt, ap);
709         else {
710                 vfprintf(stderr, fmt, ap);
711                 fputs("\n", stderr);
712         }
713         va_end(ap);
714 }
715
716 /*
717  * Record pid in /var/run/sm-notify.pid
718  * This file should remain until a reboot, even if the
719  * program exits.
720  * If file already exists, fail.
721  */
722 static int record_pid()
723 {
724         char pid[20];
725         int fd;
726
727         snprintf(pid, 20, "%d\n", getpid());
728         fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
729         if (fd < 0)
730                 return 0;
731         write(fd, pid, strlen(pid));
732         close(fd);
733         return 1;
734 }
735
736 /* Drop privileges to match owner of state-directory
737  * (in case a reply triggers some unknown bug).
738  */
739 static void drop_privs(void)
740 {
741         struct stat st;
742
743         if (stat(_SM_DIR_PATH, &st) == -1 &&
744             stat(_SM_BASE_PATH, &st) == -1) {
745                 st.st_uid = 0;
746                 st.st_gid = 0;
747         }
748
749         if (st.st_uid == 0) {
750                 nsm_log(LOG_WARNING,
751                         "sm-notify running as root. chown %s to choose different user\n",
752                     _SM_DIR_PATH);
753                 return;
754         }
755
756         setgroups(0, NULL);
757         if (setgid(st.st_gid) == -1
758             || setuid(st.st_uid) == -1) {
759                 nsm_log(LOG_ERR, "Fail to drop privileges");
760                 exit(1);
761         }
762 }
763
764 static void set_kernel_nsm_state(int state)
765 {
766         int fd;
767
768         fd = open("/proc/sys/fs/nfs/nsm_local_state",O_WRONLY);
769         if (fd >= 0) {
770                 char buf[20];
771                 snprintf(buf, sizeof(buf), "%d", state);
772                 write(fd, buf, strlen(buf));
773                 close(fd);
774         }
775 }