1 /* (C) 2013 rofl0r, based on design ideas of Rich Felker.
3 Permission is hereby granted, free of charge, to any person obtaining
4 a copy of this software and associated documentation files (the
5 "Software"), to deal in the Software without restriction, including
6 without limitation the rights to use, copy, modify, merge, publish,
7 distribute, sublicense, and/or sell copies of the Software, and to
8 permit persons to whom the Software is furnished to do so, subject to
9 the following conditions:
11 The above copyright notice and this permission notice shall be
12 included in all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 #if 1 || defined(IN_KDEVELOP_PARSER)
42 #warning "shadow support disabled, did you forget to pass -DHAVE_SHADOW ?"
45 #if !defined(_Noreturn) && __STDC_VERSION__+0 < 201112L
47 #define _Noreturn __attribute__((noreturn))
53 static const char usage_text
[] =
54 "Usage: su [OPTION] name\n"
55 "available options:\n"
56 "- : start login shell\n"
57 "-c command : run command and return\n"
58 "-s shell : use shell provided as argument (root only)\n"
59 "if name is omitted, root is assumed.\n";
61 static _Noreturn
void usage(void) {
62 dprintf(1, usage_text
);
66 static _Noreturn
void perror_exit(const char* msg
) {
71 static int directory_exists(const char* dir
) {
73 return stat(dir
, &st
) == 0 && S_ISDIR(st
.st_mode
);
76 static time_t get_mtime(const char *fn
) {
78 if(stat(fn
, &st
)) return -1;
82 static void touch(const char* fn
) {
83 int fd
= open(fn
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
84 if(fd
!= -1) close(fd
);
87 /* returns index of username argument, 0 if no username passed, -1 on syntax error
88 * cmd_index will be set to the index of a -c argument, or 0 if its missing
89 * shell_index will be set to the index of a -s argument, or 0 if its missing
90 * login_shell will be set to 1 if the login shell option - was passed, 0 otherwise */
91 static int parse_args(int argc
, char** argv
,
92 int *login_shell
, int* cmd_index
, int *shell_index
) {
98 int i
, wantarg
= 0, res
= 0;
101 for(i
= 1; i
< argc
; i
++) {
112 } else if((p
[1] == 'c' || p
[1] == 's') && !p
[2]) {
113 if(i
== argc
- 1) return -1;
115 if(p
[1] == 'c') *cmd_index
= i
+ 1;
116 else *shell_index
= i
+ 1;
121 if(wantarg
) return -1;
125 /* return 1 if the password check succeeded, 0 otherwise */
126 int check_pass(const char* name
, const char *pass
, struct passwd
*pwd
) {
129 struct spwd
*shpwd
= getspnam(name
);
131 encpw
= shpwd
->sp_pwdp
;
134 encpw
= pwd
->pw_passwd
;
136 if(!encpw
|| !strcmp(encpw
, "x")) return 0;
137 const char* actpw
= crypt(pass
, encpw
);
138 if(!actpw
) perror_exit("crypt");
139 if(strcmp(actpw
, encpw
)) return 0;
143 extern char** environ
;
145 #define SU_DIR "/var/lib/su"
146 #define LOGIN_DELAY_SECS 1
147 #define STRIFY2(x) #x
148 #define STRIFY(x) STRIFY2(x)
149 #define LOGIN_DELAY_SECS_STR STRIFY(LOGIN_DELAY_SECS)
151 int main(int argc
, char** argv
) {
152 int login_shell
, cmd_index
, shell_index
, name_index
;
153 name_index
= parse_args(argc
, argv
, &login_shell
, &cmd_index
, &shell_index
);
154 if(name_index
== -1) usage();
157 int is_root
= (uid
== 0);
158 if(shell_index
&& !is_root
) usage();
160 const char* name
= name_index
? argv
[name_index
] : "root";
163 snprintf(uidfn
, sizeof uidfn
, "%s/%d", SU_DIR
, uid
);
165 if(!directory_exists(SU_DIR
)) {
166 if(geteuid() == 0 && mkdir(SU_DIR
, 0700) == -1)
167 dprintf(2, "creation of directory " SU_DIR
168 " for bruteforce prevention failed, consider creating it manually\n");
174 time_t mtime
= get_mtime(uidfn
), now
= time(0);
175 if(mtime
!= -1 && mtime
+ LOGIN_DELAY_SECS
> now
) {
177 dprintf(2, "warning: your system clock is in the past, check your CMOS battery.\n");
179 dprintf(2, "you need to wait for " LOGIN_DELAY_SECS_STR
180 " seconds before retrying.\n");
182 if(now
< mtime
) goto failed
;
186 pass
= getpass("enter password:");
188 if(!pass
) perror_exit("getpass");
191 struct passwd
*pwd
= getpwnam(name
);
192 if(!pwd
) goto failed
;
193 if(!is_root
&& !check_pass(name
, pass
, pwd
)) {
196 dprintf(2, "login failed\n");
200 if(initgroups(name
, pwd
->pw_gid
)) perror_exit("initgroups");
201 if(setgid(pwd
->pw_gid
)) perror_exit("setgid");
202 if(setuid(pwd
->pw_uid
)) perror_exit("setuid");
204 char* const* new_argv
;
206 char* shell
= shell_index
? argv
[shell_index
] : pwd
->pw_shell
;
207 char* shell_argv0
= shell
;
209 snprintf(shellbuf
, sizeof shellbuf
, "-%s", shell
);
210 shell_argv0
= shellbuf
;
211 setenv("LOGNAME", name
, 1);
212 if(getenv("USER")) setenv("USER", name
, 1);
215 new_argv
= (char* const[]) {shell_argv0
, argv
[cmd_index
-1], argv
[cmd_index
], 0};
217 new_argv
= (char* const[]) {shell_argv0
, 0};
220 setenv("HOME", pwd
->pw_dir
, 1);
221 if(login_shell
) chdir(pwd
->pw_dir
);
223 if(execve(shell
, new_argv
, environ
)) perror_exit("execve");