From 57514130fce31fb8b1cfcd119beff375bfac149e Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Thu, 28 Jul 2016 23:36:08 +0200 Subject: [PATCH] Starting service as @user chdir() to their $HOME Signed-off-by: Joachim Nilsson --- helpers.c | 6 ++++-- helpers.h | 2 +- service.c | 7 ++++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/helpers.c b/helpers.c index cb94ba25..037bfd1f 100644 --- a/helpers.c +++ b/helpers.c @@ -189,9 +189,10 @@ int print_result(int fail) return fail; } -int getuser(char *username) +int getuser(char *username, char **home) { #ifdef ENABLE_STATIC + home = NULL; /* XXX: Fixme */ return fgetint("/etc/passwd", "x:\n", username); #else struct passwd *usr; @@ -199,7 +200,8 @@ int getuser(char *username) if (!username || (usr = getpwnam(username)) == NULL) return -1; - return usr->pw_uid; + *home = usr->pw_dir; + return usr->pw_uid; #endif } diff --git a/helpers.h b/helpers.h index 0eae47c9..f21d7af3 100644 --- a/helpers.h +++ b/helpers.h @@ -76,7 +76,7 @@ void print_desc (char *action, char *desc); int print_result (int fail); int start_process (char *cmd, char *args[], int console); void do_sleep (unsigned int sec); -int getuser (char *username); +int getuser (char *username, char **home); int getgroup (char *group); void set_hostname (char **hostname); diff --git a/service.c b/service.c index 7cdbf24c..6cad71b5 100755 --- a/service.c +++ b/service.c @@ -151,10 +151,11 @@ static int service_start(svc_t *svc) pid = fork(); if (pid == 0) { int status; + char *home = NULL; #ifdef ENABLE_STATIC int uid = 0; /* XXX: Fix better warning that dropprivs is disabled. */ #else - int uid = getuser(svc->username); + int uid = getuser(svc->username, &home); #endif char *args[MAX_NUM_SVC_ARGS]; @@ -165,6 +166,10 @@ static int service_start(svc_t *svc) /* Set default path for regular users */ if (uid > 0) setenv("PATH", _PATH_DEFPATH, 1); + if (home) { + setenv("HOME", home, 1); + chdir(home); + } } /* Serve copy of args to process in case it modifies them. */