Mercurial > hg > toybox
annotate toys/lsb/su.c @ 1153:c4ac6a90963d draft
Promote su from pending to lsb.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 22 Dec 2013 15:48:44 -0600 |
parents | toys/pending/su.c@b7ca3e926250 |
children | faf7117c4489 |
rev | line source |
---|---|
1004 | 1 /* su.c - switch user |
2 * | |
3 * Copyright 2013 CE Strake <strake888@gmail.com> | |
4 * | |
5 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/su.html | |
1152 | 6 * TODO: log su attempts |
1004 | 7 |
1152 | 8 USE_SU(NEWTOY(su, "lmpc:s:", TOYFLAG_BIN|TOYFLAG_ROOTONLY)) |
1004 | 9 |
10 config SU | |
11 bool "su" | |
1153
c4ac6a90963d
Promote su from pending to lsb.
Rob Landley <rob@landley.net>
parents:
1152
diff
changeset
|
12 default y |
1004 | 13 help |
1008 | 14 usage: su [-lmp] [-c CMD] [-s SHELL] [USER [ARGS...]] |
1004 | 15 |
1152 | 16 Switch to user (or root) and run shell (with optional command line). |
1004 | 17 |
1152 | 18 -s shell to use |
19 -c command to pass to shell with -c | |
20 -l login shell | |
21 -(m|p) preserve environment | |
1004 | 22 */ |
23 | |
24 #define FOR_su | |
25 #include "toys.h" | |
26 | |
27 GLOBALS( | |
1008 | 28 char *s; |
29 char *c; | |
1004 | 30 ) |
31 | |
1152 | 32 static char *snapshot_env(char *name) |
33 { | |
34 char *s = getenv(name); | |
35 | |
36 if (s) return xmsprintf("%s=%s", name, s); | |
37 | |
38 return 0; | |
1004 | 39 } |
40 | |
1152 | 41 void su_main() |
42 { | |
43 char *name, *passhash = 0, **argu, **argv; | |
1004 | 44 struct passwd *up; |
45 struct spwd *shp; | |
46 | |
1152 | 47 if (*toys.optargs && !strcmp("-", *toys.optargs)) { |
1004 | 48 toys.optflags |= FLAG_l; |
1152 | 49 toys.optargs++; |
1004 | 50 } |
51 | |
1152 | 52 if (*toys.optargs) name = *(toys.optargs++); |
53 else name = "root"; | |
1004 | 54 |
1152 | 55 if (!(shp = getspnam(name))) perror_exit("no '%s'", name); |
56 if (*shp->sp_pwdp != '$') goto deny; | |
57 if (read_password(toybuf, sizeof(toybuf), "Password: ")) goto deny; | |
58 passhash = crypt(toybuf, shp->sp_pwdp); | |
59 memset(toybuf, 0, sizeof(toybuf)); | |
60 if (!passhash || strcmp(passhash, shp->sp_pwdp)) goto deny; | |
1004 | 61 |
1152 | 62 up = xgetpwnam(name); |
63 xsetuid(up->pw_uid); | |
1004 | 64 |
1152 | 65 argv = argu = xmalloc(sizeof(char *)*(toys.optc + 4)); |
66 *(argv++) = TT.s ? TT.s : up->pw_shell; | |
1004 | 67 |
1152 | 68 if (toys.optflags & FLAG_l) { |
69 int i; | |
70 char *stuff[] = {snapshot_env("TERM"), snapshot_env("DISPLAY"), | |
71 snapshot_env("COLORTERM"), snapshot_env("XAUTHORITY")}; | |
1004 | 72 |
1152 | 73 clearenv(); |
74 for (i=0; i < sizeof(stuff)/sizeof(char *); i++) putenv(stuff[i]); | |
75 *(argv++) = "-l"; | |
76 xchdir(up->pw_dir); | |
77 } else unsetenv("IFS"); | |
78 setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1); | |
79 if (!(toys.optflags & (FLAG_m|FLAG_p))) { | |
80 setenv("HOME", up->pw_dir, 1); | |
81 setenv("SHELL", up->pw_shell, 1); | |
82 setenv("USER", up->pw_name, 1); | |
83 setenv("LOGNAME", up->pw_name, 1); | |
84 } else unsetenv("IFS"); | |
85 | |
1004 | 86 if (toys.optflags & FLAG_c) { |
1152 | 87 *(argv++) = "-c"; |
88 *(argv++) = TT.c; | |
1004 | 89 } |
1152 | 90 while ((*(argv++) = *(toys.optargs++))); |
91 xexec(argu); | |
92 perror_exit("can't exec %s", *argu); | |
93 | |
94 deny: | |
95 puts("No."); | |
96 toys.exitval = 1; | |
1004 | 97 } |