-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsu.c
More file actions
70 lines (63 loc) · 1.84 KB
/
su.c
File metadata and controls
70 lines (63 loc) · 1.84 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#define _POSIX_C_SOURCE 200809L
#define _GNU_SOURCE
#include "password.h"
#include <ctype.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
extern char **environ;
int main(int argc, char **argv) {
if (argc > 2) {
fprintf(stderr, "usage: %s [login]\n", argv[0]);
return EXIT_FAILURE;
}
uid_t oth = 0;
struct passwd *othpwd = argc >= 2 ? getpwnam(argv[1]) : getpwuid(oth);
if (!othpwd) {
if (argc >= 2)
fprintf(stderr, "(%s: user '%s' not found)\n", argv[0], argv[1]);
else
fprintf(stderr, "(%s: user %d not found)\n", argv[0], oth);
return EXIT_FAILURE;
}
oth = othpwd->pw_uid;
// if not root uid, ask for password
uid_t cur = geteuid();
if (cur != 0 && cur != oth) {
char *password = NULL;
size_t passwordcap = 0;
ssize_t passwordlen;
do {
printf("password: ");
fflush(stdout);
passwordlen = getpassword(&password, &passwordcap, stdin);
if (passwordlen <= 0) {
fprintf(stderr, "(%s: cannot read: %s)", argv[0], strerror(errno));
abort();
}
while (isspace(password[passwordlen - 1]))
password[--passwordlen] = '\0';
} while (!checkpassword(othpwd, password));
}
// setup user environ
setenv("HOME", othpwd->pw_dir, 1);
setenv("SHELL", othpwd->pw_shell, 1);
chdir(othpwd->pw_dir);
umask(S_IWGRP | S_IWOTH);
// switch user/group
if (initgroups(othpwd->pw_name, oth) < 0 ||
setresgid(oth, oth, oth) < 0 ||
setresuid(oth, oth, oth) < 0) {
fprintf(stderr, "(%s: login fail: %s)\n", argv[0], strerror(errno));
abort();
}
// start shell
char *const shargv[] = {othpwd->pw_shell, NULL};
if (execve(othpwd->pw_shell, shargv, environ) < 0)
fprintf(stderr, "(%s: shell: %s)\n", argv[0], strerror(errno));
abort();
}