X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=utils%2Fstatd%2Fsm-notify.c;h=cdadcd7509f223fbe9b3fff5cf7d79e95eedd65a;hb=7b01c4762ae242d2114cc6a59994f678b7d253fc;hp=0090137ae3dfa99b284b33587be5b50fe17754b7;hpb=08ed16cab8a460ea56df19b7e27fd663cc96316d;p=nfs-utils.git diff --git a/utils/statd/sm-notify.c b/utils/statd/sm-notify.c index 0090137..cdadcd7 100644 --- a/utils/statd/sm-notify.c +++ b/utils/statd/sm-notify.c @@ -22,9 +22,14 @@ #include #include #include +#include #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" @@ -81,6 +86,7 @@ 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; @@ -138,7 +144,7 @@ main(int argc, char **argv) if (optind < argc) { usage: fprintf(stderr, "Usage: sm-notify [-dfq] [-m max-retry-minutes] [-p srcport]\n" - " [-P /path/to/state/directory] [-N my_host_name\n"); + " [-P /path/to/state/directory] [-v my_host_name]\n"); return 1; } @@ -243,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); @@ -711,3 +719,31 @@ static int record_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); + } +}