diff options
Diffstat (limited to 'src/firejail/user.c')
-rw-r--r-- | src/firejail/user.c | 115 |
1 files changed, 0 insertions, 115 deletions
diff --git a/src/firejail/user.c b/src/firejail/user.c deleted file mode 100644 index a2f34392c..000000000 --- a/src/firejail/user.c +++ /dev/null | |||
@@ -1,115 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014-2016 Firejail Authors | ||
3 | * | ||
4 | * This file is part of firejail project | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License along | ||
17 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | */ | ||
20 | #include "firejail.h" | ||
21 | #include <sys/types.h> | ||
22 | #include <sys/stat.h> | ||
23 | #include <unistd.h> | ||
24 | #include <grp.h> | ||
25 | #include <pwd.h> | ||
26 | |||
27 | |||
28 | void check_user(int argc, char **argv) { | ||
29 | EUID_ASSERT(); | ||
30 | int i; | ||
31 | char *user = NULL; | ||
32 | |||
33 | int found = 0; | ||
34 | for (i = 1; i < argc; i++) { | ||
35 | // check options | ||
36 | if (strcmp(argv[i], "--") == 0) | ||
37 | break; | ||
38 | if (strncmp(argv[i], "--", 2) != 0) | ||
39 | break; | ||
40 | |||
41 | // check user option | ||
42 | if (strncmp(argv[i], "--user=", 7) == 0) { | ||
43 | found = 1; | ||
44 | user = argv[i] + 7; | ||
45 | break; | ||
46 | } | ||
47 | } | ||
48 | if (!found) | ||
49 | return; | ||
50 | |||
51 | // check root | ||
52 | if (getuid() != 0) { | ||
53 | fprintf(stderr, "Error: you need to be root to use --user command line option\n"); | ||
54 | exit(1); | ||
55 | } | ||
56 | |||
57 | // switch user | ||
58 | struct passwd *pw = getpwnam(user); | ||
59 | if (!pw) { | ||
60 | fprintf(stderr, "Error: cannot find user %s\n", user); | ||
61 | exit(1); | ||
62 | } | ||
63 | |||
64 | printf("Switching to user %s, UID %d, GID %d\n", user, pw->pw_uid, pw->pw_gid); | ||
65 | int rv = initgroups(user, pw->pw_gid); | ||
66 | if (rv == -1) { | ||
67 | perror("initgroups"); | ||
68 | fprintf(stderr, "Error: cannot switch to user %s\n", user); | ||
69 | } | ||
70 | |||
71 | rv = setgid(pw->pw_gid); | ||
72 | if (rv == -1) { | ||
73 | perror("setgid"); | ||
74 | fprintf(stderr, "Error: cannot switch to user %s\n", user); | ||
75 | } | ||
76 | |||
77 | rv = setuid(pw->pw_uid); | ||
78 | if (rv == -1) { | ||
79 | perror("setuid"); | ||
80 | fprintf(stderr, "Error: cannot switch to user %s\n", user); | ||
81 | } | ||
82 | |||
83 | // build the new command line | ||
84 | int len = 0; | ||
85 | for (i = 0; i < argc; i++) { | ||
86 | len += strlen(argv[i]) + 1; // + ' ' | ||
87 | } | ||
88 | |||
89 | char *cmd = malloc(len + 1); // + '\0' | ||
90 | if (!cmd) | ||
91 | errExit("malloc"); | ||
92 | |||
93 | char *ptr = cmd; | ||
94 | int first = 1; | ||
95 | for (i = 0; i < argc; i++) { | ||
96 | if (strncmp(argv[i], "--user=", 7) == 0 && first) { | ||
97 | first = 0; | ||
98 | continue; | ||
99 | } | ||
100 | |||
101 | ptr += sprintf(ptr, "%s ", argv[i]); | ||
102 | } | ||
103 | |||
104 | // run command | ||
105 | char *a[4]; | ||
106 | a[0] = "/bin/bash"; | ||
107 | a[1] = "-c"; | ||
108 | a[2] = cmd; | ||
109 | a[3] = NULL; | ||
110 | |||
111 | execvp(a[0], a); | ||
112 | |||
113 | perror("execvp"); | ||
114 | exit(1); | ||
115 | } | ||