diff options
author | netblue30 <netblue30@yahoo.com> | 2016-01-23 10:14:03 -0500 |
---|---|---|
committer | netblue30 <netblue30@yahoo.com> | 2016-01-23 10:14:03 -0500 |
commit | 7583e1dac92497a64dc438d3538408ff55141a7b (patch) | |
tree | 885fc33e3838b181dbb083ae9df449a79216d6a8 /src | |
parent | fixed .asoundrc problems for Debian sid (diff) | |
download | firejail-7583e1dac92497a64dc438d3538408ff55141a7b.tar.gz firejail-7583e1dac92497a64dc438d3538408ff55141a7b.tar.zst firejail-7583e1dac92497a64dc438d3538408ff55141a7b.zip |
added --user command
Diffstat (limited to 'src')
-rw-r--r-- | src/firejail/firejail.h | 3 | ||||
-rw-r--r-- | src/firejail/main.c | 1 | ||||
-rw-r--r-- | src/firejail/usage.c | 1 | ||||
-rw-r--r-- | src/firejail/user.c | 114 | ||||
-rw-r--r-- | src/man/firejail.txt | 9 |
5 files changed, 128 insertions, 0 deletions
diff --git a/src/firejail/firejail.h b/src/firejail/firejail.h index 21ca6c508..a2afd4a8d 100644 --- a/src/firejail/firejail.h +++ b/src/firejail/firejail.h | |||
@@ -505,5 +505,8 @@ void fs_logger_print_log(pid_t pid); | |||
505 | // run_symlink.c | 505 | // run_symlink.c |
506 | void run_symlink(int argc, char **argv); | 506 | void run_symlink(int argc, char **argv); |
507 | 507 | ||
508 | // user.c | ||
509 | void check_user(int argc, char **argv); | ||
510 | |||
508 | #endif | 511 | #endif |
509 | 512 | ||
diff --git a/src/firejail/main.c b/src/firejail/main.c index 2ae3213ee..7afbf9ce3 100644 --- a/src/firejail/main.c +++ b/src/firejail/main.c | |||
@@ -514,6 +514,7 @@ int main(int argc, char **argv) { | |||
514 | else { | 514 | else { |
515 | // check --output option and execute it; | 515 | // check --output option and execute it; |
516 | check_output(argc, argv); // the function will not return if --output option was found | 516 | check_output(argc, argv); // the function will not return if --output option was found |
517 | check_user(argc, argv); // the function will not return if --user option was found | ||
517 | } | 518 | } |
518 | 519 | ||
519 | // parse arguments | 520 | // parse arguments |
diff --git a/src/firejail/usage.c b/src/firejail/usage.c index 9197baae2..d3ebefaae 100644 --- a/src/firejail/usage.c +++ b/src/firejail/usage.c | |||
@@ -286,6 +286,7 @@ void usage(void) { | |||
286 | printf("\t--tracelog - add a syslog message for every access to files or\n"); | 286 | printf("\t--tracelog - add a syslog message for every access to files or\n"); |
287 | printf("\t\tdirectoires blacklisted by the security profile.\n\n"); | 287 | printf("\t\tdirectoires blacklisted by the security profile.\n\n"); |
288 | printf("\t--tree - print a tree of all sandboxed processes.\n\n"); | 288 | printf("\t--tree - print a tree of all sandboxed processes.\n\n"); |
289 | printf("\t--user=new_user - switch the user before starting the sandbox.\n\n"); | ||
289 | printf("\t--version - print program version and exit.\n\n"); | 290 | printf("\t--version - print program version and exit.\n\n"); |
290 | printf("\t--whitelist=dirname_or_filename - whitelist directory or file.\n\n"); | 291 | printf("\t--whitelist=dirname_or_filename - whitelist directory or file.\n\n"); |
291 | printf("\t--zsh - use /usr/bin/zsh as default shell.\n\n"); | 292 | printf("\t--zsh - use /usr/bin/zsh as default shell.\n\n"); |
diff --git a/src/firejail/user.c b/src/firejail/user.c new file mode 100644 index 000000000..e5f7848e8 --- /dev/null +++ b/src/firejail/user.c | |||
@@ -0,0 +1,114 @@ | |||
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 | int i; | ||
30 | char *user = NULL; | ||
31 | |||
32 | int found = 0; | ||
33 | for (i = 1; i < argc; i++) { | ||
34 | // check options | ||
35 | if (strcmp(argv[i], "--") == 0) | ||
36 | break; | ||
37 | if (strncmp(argv[i], "--", 2) != 0) | ||
38 | break; | ||
39 | |||
40 | // check user option | ||
41 | if (strncmp(argv[i], "--user=", 7) == 0) { | ||
42 | found = 1; | ||
43 | user = argv[i] + 7; | ||
44 | break; | ||
45 | } | ||
46 | } | ||
47 | if (!found) | ||
48 | return; | ||
49 | |||
50 | // check root | ||
51 | if (getuid() != 0) { | ||
52 | fprintf(stderr, "Error: you need to be root to use --user command line option\n"); | ||
53 | exit(1); | ||
54 | } | ||
55 | |||
56 | // switch user | ||
57 | struct passwd *pw = getpwnam(user); | ||
58 | if (!pw) { | ||
59 | fprintf(stderr, "Error: cannot find user %s\n", user); | ||
60 | exit(1); | ||
61 | } | ||
62 | |||
63 | printf("Switching to user %s, UID %d, GID %d\n", user, pw->pw_uid, pw->pw_gid); | ||
64 | int rv = initgroups(user, pw->pw_gid); | ||
65 | if (rv == -1) { | ||
66 | perror("initgroups"); | ||
67 | fprintf(stderr, "Error: cannot switch to user %s\n", user); | ||
68 | } | ||
69 | |||
70 | rv = setgid(pw->pw_gid); | ||
71 | if (rv == -1) { | ||
72 | perror("setgid"); | ||
73 | fprintf(stderr, "Error: cannot switch to user %s\n", user); | ||
74 | } | ||
75 | |||
76 | rv = setuid(pw->pw_uid); | ||
77 | if (rv == -1) { | ||
78 | perror("setuid"); | ||
79 | fprintf(stderr, "Error: cannot switch to user %s\n", user); | ||
80 | } | ||
81 | |||
82 | // build the new command line | ||
83 | int len = 0; | ||
84 | for (i = 0; i < argc; i++) { | ||
85 | len += strlen(argv[i]) + 1; // + ' ' | ||
86 | } | ||
87 | |||
88 | char *cmd = malloc(len + 1); // + '\0' | ||
89 | if (!cmd) | ||
90 | errExit("malloc"); | ||
91 | |||
92 | char *ptr = cmd; | ||
93 | int first = 1; | ||
94 | for (i = 0; i < argc; i++) { | ||
95 | if (strncmp(argv[i], "--user=", 7) == 0 && first) { | ||
96 | first = 0; | ||
97 | continue; | ||
98 | } | ||
99 | |||
100 | ptr += sprintf(ptr, "%s ", argv[i]); | ||
101 | } | ||
102 | |||
103 | // run command | ||
104 | char *a[4]; | ||
105 | a[0] = "/bin/bash"; | ||
106 | a[1] = "-c"; | ||
107 | a[2] = cmd; | ||
108 | a[3] = NULL; | ||
109 | |||
110 | execvp(a[0], a); | ||
111 | |||
112 | perror("execvp"); | ||
113 | exit(1); | ||
114 | } | ||
diff --git a/src/man/firejail.txt b/src/man/firejail.txt index 13eccbdce..ef65530db 100644 --- a/src/man/firejail.txt +++ b/src/man/firejail.txt | |||
@@ -1441,6 +1441,15 @@ $ firejail \-\-tree | |||
1441 | .br | 1441 | .br |
1442 | 11970:netblue:transmission-gtk | 1442 | 11970:netblue:transmission-gtk |
1443 | .TP | 1443 | .TP |
1444 | \fB\-\-user=new-user | ||
1445 | Switch the user before starting the sandbox. This command should be run as root. | ||
1446 | .br | ||
1447 | |||
1448 | .br | ||
1449 | Example: | ||
1450 | .br | ||
1451 | # firejail \-\-user=www-data | ||
1452 | .TP | ||
1444 | \fB\-\-version | 1453 | \fB\-\-version |
1445 | Print program version and exit. | 1454 | Print program version and exit. |
1446 | .br | 1455 | .br |