]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/statd/sm-notify.c
sm-notify - use state directory provided via ./configure
[nfs-utils.git] / utils / statd / sm-notify.c
index aa2c7d199b59c67b48839050c17f2eafd90aefc8..b7a5dd50b0e880c4a4b1d391c748e57c647bb5e3 100644 (file)
 #include <stdarg.h>
 #include <netdb.h>
 #include <errno.h>
+#include <grp.h>
 
 #ifndef BASEDIR
-#define BASEDIR                "/var/lib/nfs"
+# ifdef NFS_STATEDIR
+#  define BASEDIR              NFS_STATEDIR
+# else
+#  define BASEDIR              "/var/lib/nfs"
+# endif
 #endif
 
 #define DEFAULT_SM_STATE_PATH  BASEDIR "/state"
@@ -80,6 +85,8 @@ static int            addr_get_port(nsm_address *);
 static void            addr_set_port(nsm_address *, int);
 static int             host_lookup(int, const char *, nsm_address *);
 void                   nsm_log(int fac, const char *fmt, ...);
+static int             record_pid();
+static void            drop_privs(void);
 
 static struct nsm_host *       hosts = NULL;
 
@@ -87,9 +94,13 @@ int
 main(int argc, char **argv)
 {
        int     c;
+       int     force = 0;
 
-       while ((c = getopt(argc, argv, "dm:np:v:qP:")) != -1) {
+       while ((c = getopt(argc, argv, "dm:np:v:qP:f")) != -1) {
                switch (c) {
+               case 'f':
+                       force = 1;
+                       break;
                case 'd':
                        opt_debug++;
                        break;
@@ -131,10 +142,18 @@ main(int argc, char **argv)
        }
 
        if (optind < argc) {
-usage:         fprintf(stderr, "sm-notify [-d]\n");
+usage:         fprintf(stderr,
+                       "Usage: sm-notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
+                       "            [-P /path/to/state/directory] [-N my_host_name\n");
                return 1;
        }
 
+       if (strcmp(_SM_BASE_PATH, BASEDIR) == 0) {
+               if (record_pid() == 0 && force == 0 && opt_update_state == 0)
+                       /* already run, don't try again */
+                       exit(0);
+       }
+
        if (opt_srcaddr) {
                strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
        } else
@@ -230,6 +249,8 @@ notify(void)
        if (opt_max_retry)
                failtime = time(NULL) + opt_max_retry;
 
+       drop_privs();
+
        while (hosts) {
                struct pollfd   pfd;
                time_t          now = time(NULL);
@@ -678,3 +699,51 @@ nsm_log(int fac, const char *fmt, ...)
        }
        va_end(ap);
 }
+
+/*
+ * Record pid in /var/run/sm-notify.pid
+ * This file should remain until a reboot, even if the
+ * program exits.
+ * If file already exists, fail.
+ */
+static int record_pid()
+{
+       char pid[20];
+       int fd;
+
+       snprintf(pid, 20, "%d\n", getpid());
+       fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
+       if (!fd)
+               return 0;
+       write(fd, pid, strlen(pid));
+       close(fd);
+       return 1;
+}
+
+/* Drop privileges to match owner of state-directory
+ * (in case a reply triggers some unknown bug).
+ */
+static void drop_privs(void)
+{
+       struct stat st;
+
+       if (stat(_SM_DIR_PATH, &st) == -1 &&
+           stat(_SM_BASE_PATH, &st) == -1) {
+               st.st_uid = 0;
+               st.st_gid = 0;
+       }
+
+       if (st.st_uid == 0) {
+               nsm_log(LOG_WARNING,
+                       "sm-notify running as root. chown %s to choose different user\n",
+                   _SM_DIR_PATH);
+               return;
+       }
+
+       setgroups(0, NULL);
+       if (setgid(st.st_gid) == -1
+           || setuid(st.st_uid) == -1) {
+               nsm_log(LOG_ERR, "Fail to drop privileges");
+               exit(1);
+       }
+}