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
noproxy
Alessandro Mauri 4 years ago
parent 5412a3785e
commit fe9d88e7f9
  1. 75
      us.c

75
us.c

@ -42,6 +42,8 @@ static struct group* group_to_grp (const char *);
// our own pam module // our own pam module
static struct pam_conv conv = {misc_conv, NULL}; static struct pam_conv conv = {misc_conv, NULL};
extern char **environ;
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
// TODO: Add arguments // TODO: Add arguments
@ -50,8 +52,8 @@ int main (int argc, char *argv[])
struct passwd *t_pw; struct passwd *t_pw;
struct group *t_gr; struct group *t_gr;
int opt, err; int opt, err;
int shellflag = 0; int shellflag = 0, envflag = 0;
while ((opt = getopt(argc, argv, "A:u:g:C:s")) != -1) { while ((opt = getopt(argc, argv, "A:u:g:C:se")) != -1) {
switch (opt) { switch (opt) {
case 'A': case 'A':
printf("-A is not yet implemented\n"); printf("-A is not yet implemented\n");
@ -70,6 +72,9 @@ int main (int argc, char *argv[])
case 's': case 's':
shellflag = 1; shellflag = 1;
break; break;
case 'e':
envflag = 1;
break;
case '?': case '?':
usage(); usage();
exit(EINVAL); exit(EINVAL);
@ -139,33 +144,49 @@ int main (int argc, char *argv[])
if (authenticate(uname) != PAM_SUCCESS) if (authenticate(uname) != PAM_SUCCESS)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
// TODO: clean up env struct env_elem {
/* copy and filter env */ char *name;
/* char *value;
char **c_env; };
extern char **environ;
int size = 0; struct env_elem env_keep[] = {
for (int i = 0; environ[i]; i++, size++); {"PATH", NULL},
c_env = malloc(sizeof(char *) * (size + 1)); {"TERM", NULL},
if (!c_env) { {"EDITOR", NULL},
fprintf(stderr, "malloc: %s\n", strerror(errno)); {"VISUAL", NULL},
exit(errno); {"DISPLAY", NULL},
} {"XAUTHORITY", NULL},
for (int i = 0; environ[i]; i++) { {NULL, NULL}
c_env[i] = strdup(environ[i]); };
if (!c_env[i]) {
fprintf(stderr, "strdup: %s\n", strerror(errno)); struct env_elem env_mod[] = {
exit(errno); {"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; env_mod[i].name; i++) {
// TODO: check err value
err = setenv(env_mod[i].name, env_mod[i].value, 1);
} }
c_env[size] = NULL;
*/ if (envflag) {
for (int i = 0; env_keep[i].name; i++) {
// TODO: check err value // TODO: check err value
// TODO: add all this to list and loop over it if (env_keep[i].value)
err = setenv("USER", t_pw->pw_name, 1); err = setenv(env_keep[i].name, env_keep[i].value, 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; errno = 0;
/* Set permissions */ /* Set permissions */
@ -198,7 +219,7 @@ static inline void usage (void)
// -c [file]: manually select config file // -c [file]: manually select config file
// something about environment // something about environment
// something about non interactiveness // 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) static int perm_set (struct passwd *pw, struct group *gr)

Loading…
Cancel
Save