Compare commits

..

No commits in common. "fe9d88e7f9961969654933aa2aa714ee87f2643e" and "72c217d74a745e2916b7010e04ab7408c267c773" have entirely different histories.

99
us.c
View File

@ -36,14 +36,11 @@ static int perm_set (struct passwd *, struct group *);
static int authenticate (const char *); static int authenticate (const char *);
static struct passwd* user_to_passwd (const char *); static struct passwd* user_to_passwd (const char *);
static struct group* group_to_grp (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 // FIXME: misc_conv is a separate library, should stick to plain PAM or make
// 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
@ -51,9 +48,9 @@ int main (int argc, char *argv[])
char *t_usr = "root", *t_grp = NULL; char *t_usr = "root", *t_grp = NULL;
struct passwd *t_pw; struct passwd *t_pw;
struct group *t_gr; struct group *t_gr;
int opt, err; int opt;
int shellflag = 0, envflag = 0; int shellflag = 0;
while ((opt = getopt(argc, argv, "A:u:g:C:se")) != -1) { while ((opt = getopt(argc, argv, "A:u:g:C:s")) != -1) {
switch (opt) { switch (opt) {
case 'A': case 'A':
printf("-A is not yet implemented\n"); printf("-A is not yet implemented\n");
@ -72,9 +69,6 @@ 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);
@ -140,53 +134,10 @@ int main (int argc, char *argv[])
c_argv[c_argc] = NULL; c_argv[c_argc] = NULL;
/* Authenticate */ /* Authenticate */
// FIXME: move this up
if (authenticate(uname) != PAM_SUCCESS) if (authenticate(uname) != PAM_SUCCESS)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
struct env_elem { // TODO: clean up env
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; 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);
}
}
// 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 */
@ -196,6 +147,7 @@ int main (int argc, char *argv[])
} }
/* Execute the command */ /* Execute the command */
int err;
err = execvp(c_argv[0], c_argv); err = execvp(c_argv[0], c_argv);
if (err == -1) if (err == -1)
fprintf(stderr, "execl: %s\n", strerror(errno)); fprintf(stderr, "execl: %s\n", strerror(errno));
@ -219,7 +171,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 [-se] [-u user] [-g group] command [args]\n"); printf("usage: us [-s] [-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)
@ -353,42 +305,3 @@ static struct group* group_to_grp (const char *group)
} }
return gr; 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;
}
*/