1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#define FOR_su
38#include "toys.h"
39
40GLOBALS(
41 char *s;
42 char *c;
43)
44
45void su_main()
46{
47 char *name, *passhash = 0, **argu, **argv;
48 struct passwd *up;
49 struct spwd *shp;
50
51 if (*toys.optargs && !strcmp("-", *toys.optargs)) {
52 toys.optflags |= FLAG_l;
53 toys.optargs++;
54 }
55
56 if (*toys.optargs) name = *(toys.optargs++);
57 else name = "root";
58
59 loggit(LOG_NOTICE, "%s->%s", getusername(getuid()), name);
60
61 if (!(shp = getspnam(name))) perror_exit("no '%s'", name);
62 if (getuid()) {
63 if (*shp->sp_pwdp != '$') goto deny;
64 if (read_password(toybuf, sizeof(toybuf), "Password: ")) goto deny;
65 passhash = crypt(toybuf, shp->sp_pwdp);
66 memset(toybuf, 0, sizeof(toybuf));
67 if (!passhash || strcmp(passhash, shp->sp_pwdp)) goto deny;
68 }
69 closelog();
70
71 xsetuser(up = xgetpwnam(name));
72
73 if (FLAG(m)||FLAG(p)) {
74 unsetenv("IFS");
75 setenv("PATH", _PATH_DEFPATH, 1);
76 } else reset_env(up, FLAG(l));
77
78 argv = argu = xmalloc(sizeof(char *)*(toys.optc + 4));
79 *(argv++) = TT.s ? TT.s : up->pw_shell;
80 loggit(LOG_NOTICE, "run %s", *argu);
81
82 if (FLAG(l)) *(argv++) = "-l";
83 if (FLAG(c)) {
84 *(argv++) = "-c";
85 *(argv++) = TT.c;
86 }
87 while ((*(argv++) = *(toys.optargs++)));
88 xexec(argu);
89
90deny:
91 syslog(LOG_NOTICE, "No.");
92 puts("No.");
93 toys.exitval = 1;
94}
95