Implement -p for su(1)

This commit is contained in:
sin 2013-10-18 14:26:14 +01:00
parent 7e59506b4b
commit acb9ae197e
1 changed files with 12 additions and 6 deletions

8
su.c
View File

@ -18,10 +18,11 @@ static void dologin(struct passwd *);
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-l] [username]\n", argv0); eprintf("usage: %s [-lp] [username]\n", argv0);
} }
static int lflag = 0; static int lflag = 0;
static int pflag = 0;
int int
main(int argc, char **argv) main(int argc, char **argv)
@ -37,6 +38,9 @@ main(int argc, char **argv)
case 'l': case 'l':
lflag = 1; lflag = 1;
break; break;
case 'p':
pflag = 1;
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -100,12 +104,14 @@ main(int argc, char **argv)
dologin(pw); dologin(pw);
} else { } else {
newargv = (char *const[]){pw->pw_shell, NULL}; newargv = (char *const[]){pw->pw_shell, NULL};
if (!pflag) {
setenv("HOME", pw->pw_dir, 1); setenv("HOME", pw->pw_dir, 1);
setenv("SHELL", pw->pw_dir, 1); setenv("SHELL", pw->pw_dir, 1);
if (strcmp(pw->pw_name, "root") != 0) { if (strcmp(pw->pw_name, "root") != 0) {
setenv("USER", pw->pw_name, 1); setenv("USER", pw->pw_name, 1);
setenv("LOGNAME", pw->pw_name, 1); setenv("LOGNAME", pw->pw_name, 1);
} }
}
execve(pw->pw_shell, newargv, environ); execve(pw->pw_shell, newargv, environ);
} }
return (errno == ENOENT) ? 127 : 126; return (errno == ENOENT) ? 127 : 126;