From 5412a3785eb9293c98f28431ba00ca262bb3479f Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Sun, 4 Apr 2021 12:42:03 +0200 Subject: [PATCH] change env us now changes the environment variables listed in TODO before executing the command, also added but commented out a version where the env gets copied and the execution happens trough the musl implementation of execvpe(3) --- us.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/us.c b/us.c index 09778b2..69db7a7 100644 --- a/us.c +++ b/us.c @@ -36,6 +36,7 @@ static int perm_set (struct passwd *, struct group *); static int authenticate (const char *); static struct passwd* user_to_passwd (const char *); static struct group* group_to_grp (const char *); +//static int execvpe(const char *, char *const *, char *const *); // FIXME: misc_conv is a separate library, should stick to plain PAM or make // our own pam module @@ -48,7 +49,7 @@ int main (int argc, char *argv[]) char *t_usr = "root", *t_grp = NULL; struct passwd *t_pw; struct group *t_gr; - int opt; + int opt, err; int shellflag = 0; while ((opt = getopt(argc, argv, "A:u:g:C:s")) != -1) { switch (opt) { @@ -134,10 +135,37 @@ int main (int argc, char *argv[]) c_argv[c_argc] = NULL; /* Authenticate */ + // FIXME: move this up if (authenticate(uname) != PAM_SUCCESS) exit(EXIT_FAILURE); // TODO: clean up env + /* copy and filter env */ +/* + char **c_env; + extern char **environ; + int size = 0; + for (int i = 0; environ[i]; i++, size++); + c_env = malloc(sizeof(char *) * (size + 1)); + if (!c_env) { + fprintf(stderr, "malloc: %s\n", strerror(errno)); + exit(errno); + } + for (int i = 0; environ[i]; i++) { + c_env[i] = strdup(environ[i]); + if (!c_env[i]) { + fprintf(stderr, "strdup: %s\n", strerror(errno)); + exit(errno); + } + } + c_env[size] = NULL; +*/ + // TODO: check err value + // TODO: add all this to list and loop over it + err = setenv("USER", t_pw->pw_name, 1); + err = setenv("LOGNAME", t_pw->pw_name, 1); + err = setenv("SHELL", t_pw->pw_shell, 1); + err = setenv("HOME", t_pw->pw_dir, 1); errno = 0; /* Set permissions */ @@ -147,7 +175,6 @@ int main (int argc, char *argv[]) } /* Execute the command */ - int err; err = execvp(c_argv[0], c_argv); if (err == -1) fprintf(stderr, "execl: %s\n", strerror(errno)); @@ -305,3 +332,42 @@ static struct group* group_to_grp (const char *group) } return gr; } + +/* +static int execvpe(const char *file, char *const argv[], char *const envp[]) +{ + const char *p, *z, *path = getenv("PATH"); + size_t l, k; + + errno = ENOENT; + if (!*file) return -1; + + if (strchr(file, '/')) + return execve(file, argv, envp); + + if (!path) path = "/usr/local/bin:/bin:/usr/bin"; + k = strnlen(file, NAME_MAX+1); + if (k > NAME_MAX) { + errno = ENAMETOOLONG; + return -1; + } + l = strnlen(path, PATH_MAX-1)+1; + + for(p=path; ; p=z) { + char b[l+k+1]; + z = strchr(p, ':'); + if (!z) z = p+strlen(p); + if ((size_t)(z-p) >= l) { + if (!*z++) break; + continue; + } + memcpy(b, p, z-p); + b[z-p] = '/'; + memcpy(b+(z-p)+(z>p), file, k+1); + execve(b, argv, envp); + if (errno != ENOENT) return -1; + if (!*z++) break; + } + return -1; +} +*/