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#define FOR_su
37#include "toys.h"
38
39GLOBALS(
40 char *s;
41 char *c;
42)
43
44void su_main()
45{
46 char *name, *passhash = 0, **argu, **argv;
47 struct passwd *up;
48 struct spwd *shp;
49
50 if (*toys.optargs && !strcmp("-", *toys.optargs)) {
51 toys.optflags |= FLAG_l;
52 toys.optargs++;
53 }
54
55 if (*toys.optargs) name = *(toys.optargs++);
56 else name = "root";
57
58 loggit(LOG_NOTICE, "%s->%s", getusername(geteuid()), name);
59
60 if (!(shp = getspnam(name))) perror_exit("no '%s'", name);
61 if (getuid()) {
62 if (*shp->sp_pwdp != '$') goto deny;
63 if (read_password(toybuf, sizeof(toybuf), "Password: ")) goto deny;
64 passhash = crypt(toybuf, shp->sp_pwdp);
65 memset(toybuf, 0, sizeof(toybuf));
66 if (!passhash || strcmp(passhash, shp->sp_pwdp)) goto deny;
67 }
68 closelog();
69
70 xsetuser(up = xgetpwnam(name));
71
72 if (FLAG(m)||FLAG(p)) {
73 unsetenv("IFS");
74 setenv("PATH", _PATH_DEFPATH, 1);
75 } else reset_env(up, FLAG(l));
76
77 argv = argu = xmalloc(sizeof(char *)*(toys.optc + 4));
78 *(argv++) = TT.s ? TT.s : up->pw_shell;
79 loggit(LOG_NOTICE, "run %s", *argu);
80
81 if (FLAG(l)) *(argv++) = "-l";
82 if (FLAG(c)) {
83 *(argv++) = "-c";
84 *(argv++) = TT.c;
85 }
86 while ((*(argv++) = *(toys.optargs++)));
87 xexec(argu);
88
89deny:
90 syslog(LOG_NOTICE, "No.");
91 puts("No.");
92 toys.exitval = 1;
93}
94