From fe9d88e7f9961969654933aa2aa714ee87f2643e Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Sun, 4 Apr 2021 19:34:10 +0200 Subject: [PATCH] env flag implemented -e flag: it copies the elements of env that we want to keep, clears the environment and sets it to only the saved and default elements. also removed the code for copying the environment and allocating a new one --- us.c | 75 ++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/us.c b/us.c index 69db7a7..f1647a4 100644 --- a/us.c +++ b/us.c @@ -42,6 +42,8 @@ static struct group* group_to_grp (const char *); // our own pam module static struct pam_conv conv = {misc_conv, NULL}; +extern char **environ; + int main (int argc, char *argv[]) { // TODO: Add arguments @@ -50,8 +52,8 @@ int main (int argc, char *argv[]) struct passwd *t_pw; struct group *t_gr; int opt, err; - int shellflag = 0; - while ((opt = getopt(argc, argv, "A:u:g:C:s")) != -1) { + int shellflag = 0, envflag = 0; + while ((opt = getopt(argc, argv, "A:u:g:C:se")) != -1) { switch (opt) { case 'A': printf("-A is not yet implemented\n"); @@ -70,6 +72,9 @@ int main (int argc, char *argv[]) case 's': shellflag = 1; break; + case 'e': + envflag = 1; + break; case '?': usage(); exit(EINVAL); @@ -139,33 +144,49 @@ int main (int argc, char *argv[]) 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); + struct env_elem { + char *name; + char *value; + }; + + struct env_elem env_keep[] = { + {"PATH", NULL}, + {"TERM", NULL}, + {"EDITOR", NULL}, + {"VISUAL", NULL}, + {"DISPLAY", NULL}, + {"XAUTHORITY", NULL}, + {NULL, NULL} + }; + + struct env_elem env_mod[] = { + {"USER", t_pw->pw_name}, + {"LOGNAME", t_pw->pw_name}, + {"SHELL", t_pw->pw_shell}, + {"HOME", t_pw->pw_dir}, + {NULL, NULL} + }; + + if (envflag) { /* clear env */ + for (int i = 0; env_keep[i].name; i++) + env_keep[i].value = strdup(getenv(env_keep[i].name)); + environ = NULL; // in place of clearenv } - 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); + + for (int i = 0; env_mod[i].name; i++) { + // TODO: check err value + err = setenv(env_mod[i].name, env_mod[i].value, 1); + } + + if (envflag) { + for (int i = 0; env_keep[i].name; i++) { + // TODO: check err value + if (env_keep[i].value) + err = setenv(env_keep[i].name, env_keep[i].value, 1); } } - 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); + // do not override, we might be under more levels of 'us' + err = setenv("US_USER", my_pw->pw_name, 0); errno = 0; /* Set permissions */ @@ -198,7 +219,7 @@ static inline void usage (void) // -c [file]: manually select config file // something about environment // something about non interactiveness - printf("usage: us [-s] [-u user] [-g group] command [args]\n"); + printf("usage: us [-se] [-u user] [-g group] command [args]\n"); } static int perm_set (struct passwd *pw, struct group *gr)