Mercurial > hg > toybox
annotate toys/lsb/su.c @ 1564:685a0da6ca59 draft
probe for getspnam(), forkpty(), utmpx, replace sethostname()
Android is missing all of these; we need to probe for some so we have
a config symbol to depend on.
sethostname() is easily replaced.
We got termios.h via pty.h; now it's not included in configure-step tools,
so we need termios.h to generate globals.
author | Isaac Dunham <ibid.ag@gmail.com> |
---|---|
date | Wed, 19 Nov 2014 16:38:46 -0600 |
parents | 407357afa07f |
children |
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 |
1564
685a0da6ca59
probe for getspnam(), forkpty(), utmpx, replace sethostname()
Isaac Dunham <ibid.ag@gmail.com>
parents:
1248
diff
changeset
|
13 depends on TOYBOX_SHADOW |
1004 | 14 help |
1008 | 15 usage: su [-lmp] [-c CMD] [-s SHELL] [USER [ARGS...]] |
1004 | 16 |
1152 | 17 Switch to user (or root) and run shell (with optional command line). |
1004 | 18 |
1152 | 19 -s shell to use |
20 -c command to pass to shell with -c | |
21 -l login shell | |
22 -(m|p) preserve environment | |
1004 | 23 */ |
24 | |
25 #define FOR_su | |
26 #include "toys.h" | |
27 | |
28 GLOBALS( | |
1008 | 29 char *s; |
30 char *c; | |
1004 | 31 ) |
32 | |
1152 | 33 static char *snapshot_env(char *name) |
34 { | |
35 char *s = getenv(name); | |
36 | |
1183
0752b2d58909
Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents:
1156
diff
changeset
|
37 if (s) return xmprintf("%s=%s", name, s); |
1152 | 38 |
39 return 0; | |
1004 | 40 } |
41 | |
1152 | 42 void su_main() |
43 { | |
44 char *name, *passhash = 0, **argu, **argv; | |
1004 | 45 struct passwd *up; |
46 struct spwd *shp; | |
47 | |
1152 | 48 if (*toys.optargs && !strcmp("-", *toys.optargs)) { |
1004 | 49 toys.optflags |= FLAG_l; |
1152 | 50 toys.optargs++; |
1004 | 51 } |
52 | |
1152 | 53 if (*toys.optargs) name = *(toys.optargs++); |
54 else name = "root"; | |
1004 | 55 |
1152 | 56 if (!(shp = getspnam(name))) perror_exit("no '%s'", name); |
57 if (*shp->sp_pwdp != '$') goto deny; | |
58 if (read_password(toybuf, sizeof(toybuf), "Password: ")) goto deny; | |
59 passhash = crypt(toybuf, shp->sp_pwdp); | |
60 memset(toybuf, 0, sizeof(toybuf)); | |
61 if (!passhash || strcmp(passhash, shp->sp_pwdp)) goto deny; | |
1004 | 62 |
1152 | 63 up = xgetpwnam(name); |
1156
faf7117c4489
Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents:
1153
diff
changeset
|
64 xsetuser(up); |
1004 | 65 |
1152 | 66 argv = argu = xmalloc(sizeof(char *)*(toys.optc + 4)); |
67 *(argv++) = TT.s ? TT.s : up->pw_shell; | |
1004 | 68 |
1152 | 69 if (toys.optflags & FLAG_l) { |
70 int i; | |
71 char *stuff[] = {snapshot_env("TERM"), snapshot_env("DISPLAY"), | |
72 snapshot_env("COLORTERM"), snapshot_env("XAUTHORITY")}; | |
1004 | 73 |
1152 | 74 clearenv(); |
1248
407357afa07f
Bugfix: if $TERM and friends aren't set, putenv() got passed a NULL.
Rob Landley <rob@landley.net>
parents:
1183
diff
changeset
|
75 for (i=0; i < ARRAY_LEN(stuff); i++) if (stuff[i]) putenv(stuff[i]); |
1152 | 76 *(argv++) = "-l"; |
77 xchdir(up->pw_dir); | |
78 } else unsetenv("IFS"); | |
79 setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1); | |
80 if (!(toys.optflags & (FLAG_m|FLAG_p))) { | |
81 setenv("HOME", up->pw_dir, 1); | |
82 setenv("SHELL", up->pw_shell, 1); | |
83 setenv("USER", up->pw_name, 1); | |
84 setenv("LOGNAME", up->pw_name, 1); | |
85 } else unsetenv("IFS"); | |
86 | |
1004 | 87 if (toys.optflags & FLAG_c) { |
1152 | 88 *(argv++) = "-c"; |
89 *(argv++) = TT.c; | |
1004 | 90 } |
1152 | 91 while ((*(argv++) = *(toys.optargs++))); |
92 xexec(argu); | |
93 perror_exit("can't exec %s", *argu); | |
94 | |
95 deny: | |
96 puts("No."); | |
97 toys.exitval = 1; | |
1004 | 98 } |