]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
privsep: Only fetch PRIVSEP_USER at init
authorRoy Marples <roy@marples.name>
Tue, 21 Jan 2020 20:17:27 +0000 (20:17 +0000)
committerRoy Marples <roy@marples.name>
Tue, 21 Jan 2020 20:17:27 +0000 (20:17 +0000)
And not each time it's needed - we don't want a sudden change in
the details to affect a running dhcpcd.

src/dhcpcd.h
src/privsep-root.c
src/privsep.c

index 1c727660095af1c403b19427a4a18d28527b7a05..6242956a0f7a9cab49699c2f8ab0a60f829058e2 100644 (file)
@@ -121,6 +121,8 @@ TAILQ_HEAD(if_head, interface);
                        CMSG_SPACE(sizeof(int)))
 #endif
 
+struct passwd;
+
 struct dhcpcd_ctx {
        char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1];
        int fork_fd;    /* FD for the fork init signal pipe */
@@ -182,7 +184,7 @@ struct dhcpcd_ctx {
        char *randomstate; /* original state */
 
 #ifdef PRIVSEP
-       char *ps_user;          /* Username to drop privs to */
+       struct passwd *ps_user; /* struct passwd for privsep user */
        pid_t ps_root_pid;
        int ps_root_fd;         /* Privileged Actioneer commands */
        int ps_data_fd;         /* Data from root spawned processes */
index 076d71dec1aebbe50d0724d608561f52467f9b3b..2dc57ae79c4db822c9499d718873b142ec3f4590 100644 (file)
@@ -203,7 +203,7 @@ ps_root_run_script(struct dhcpcd_ctx *ctx, const void *data, size_t len)
 }
 
 static ssize_t
-ps_root_docopy(const char *dir, const char *file)
+ps_root_docopy(struct dhcpcd_ctx *ctx, const char *file)
 {
 
        char path[PATH_MAX], buf[BUFSIZ], *slash;
@@ -216,7 +216,8 @@ ps_root_docopy(const char *dir, const char *file)
        struct timeval ts[2];
 #endif
 
-       if (snprintf(path, sizeof(path), "%s/%s", dir, file) == -1)
+       if (snprintf(path, sizeof(path), "%s/%s",
+           ctx->ps_user->pw_dir, file) == -1)
                return -1;
        if (stat(file, &from_sb) == -1)
                return -1;
@@ -275,22 +276,7 @@ ps_root_docopy(const char *dir, const char *file)
 }
 
 static ssize_t
-ps_root_docopy1(const char *file)
-{
-       struct passwd *pw;
-
-       errno = 0;
-       if ((pw = getpwnam(PRIVSEP_USER)) == NULL) {
-               if (errno == 0)
-                       errno = ENOENT;
-               return -1;
-       }
-
-       return ps_root_docopy(pw->pw_dir, file);
-}
-
-static ssize_t
-ps_root_dofileop(void *data, size_t len, uint8_t op)
+ps_root_dofileop(struct dhcpcd_ctx *ctx, void *data, size_t len, uint8_t op)
 {
        char *path = data;
        size_t plen;
@@ -309,7 +295,7 @@ ps_root_dofileop(void *data, size_t len, uint8_t op)
 
        switch(op) {
        case PS_COPY:
-               return ps_root_docopy1(path);
+               return ps_root_docopy(ctx, path);
        case PS_UNLINK:
                return (ssize_t)unlink(path);
        default:
@@ -389,7 +375,7 @@ ps_root_recvmsgcb(void *arg, struct ps_msghdr *psm, struct msghdr *msg)
                break;
        case PS_COPY:   /* FALLTHROUGH */
        case PS_UNLINK:
-               err = ps_root_dofileop(data, len, psm->ps_cmd);
+               err = ps_root_dofileop(ctx, data, len, psm->ps_cmd);
                break;
        default:
                err = ps_root_os(psm, msg);
index ffd32de0a7ee8cffd4f9c94cc810f7336526e00c..af9e27bf80385f00f84849fff5491ffe3532c49e 100644 (file)
@@ -93,11 +93,11 @@ ps_mkdir(char *path)
 int
 ps_init(struct dhcpcd_ctx *ctx)
 {
-       struct passwd *pw;
        char path[PATH_MAX];
+       struct passwd *pw = ctx->ps_user;
 
        errno = 0;
-       if ((pw = getpwnam(PRIVSEP_USER)) == NULL) {
+       if ((ctx->ps_user = pw = getpwnam(PRIVSEP_USER)) == NULL) {
                ctx->options &= ~DHCPCD_PRIVSEP;
                if (errno == 0) {
                        logerrx("no such user %s", PRIVSEP_USER);
@@ -122,15 +122,7 @@ ps_init(struct dhcpcd_ctx *ctx)
 int
 ps_dropprivs(struct dhcpcd_ctx *ctx)
 {
-       struct passwd *pw;
-
-       if ((pw = getpwnam(PRIVSEP_USER)) == NULL) {
-               if (errno == 0)
-                       logerrx("no such user %s", PRIVSEP_USER);
-               else
-                       logerr("getpwnam");
-               return -1;
-       }
+       struct passwd *pw = ctx->ps_user;
 
        if (!(ctx->options & DHCPCD_FORKED))
                logdebugx("chrooting to `%s'", pw->pw_dir);