diff options
author | 2018-04-05 18:40:28 -0400 | |
---|---|---|
committer | 2018-04-05 18:40:28 -0400 | |
commit | 0992ba0b6ea4c01dee0fbcf30068e64be638162b (patch) | |
tree | fba3ab0abc43f95f8c7008540bae9ccd23d639af /src/lib/firejail_user.c | |
parent | Merge pull request #1864 from glitsj16/enchant (diff) | |
download | firejail-0992ba0b6ea4c01dee0fbcf30068e64be638162b.tar.gz firejail-0992ba0b6ea4c01dee0fbcf30068e64be638162b.tar.zst firejail-0992ba0b6ea4c01dee0fbcf30068e64be638162b.zip |
user access database in /etc/firejail/firejail.users - more to come
Diffstat (limited to 'src/lib/firejail_user.c')
-rw-r--r-- | src/lib/firejail_user.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/lib/firejail_user.c b/src/lib/firejail_user.c new file mode 100644 index 000000000..5d92aa133 --- /dev/null +++ b/src/lib/firejail_user.c | |||
@@ -0,0 +1,115 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014-2018 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 | |||
21 | // | ||
22 | // Firejail access database inplementation | ||
23 | // | ||
24 | // The database is a simple list of users allowed to run firejail SUID executable | ||
25 | // It is usually stored in /etc/firejail/firejail.users | ||
26 | // One username per line in the file | ||
27 | |||
28 | #include "../include/common.h" | ||
29 | #include <sys/types.h> | ||
30 | #include <pwd.h> | ||
31 | |||
32 | #define MAXBUF 4098 | ||
33 | static inline char *get_fname(void) { | ||
34 | char *fname; | ||
35 | if (asprintf(&fname, "%s/firejail.users", SYSCONFDIR) == -1) | ||
36 | errExit("asprintf"); | ||
37 | return fname; | ||
38 | } | ||
39 | |||
40 | // returns 1 if the user is found in the database or if the database was not created | ||
41 | int firejail_user_check(const char *name) { | ||
42 | assert(name); | ||
43 | |||
44 | // root allowed by default | ||
45 | if (strcmp(name, "root") == 0) | ||
46 | return 1; | ||
47 | |||
48 | // check file existence | ||
49 | char *fname = get_fname(); | ||
50 | if (access(fname, F_OK)) { | ||
51 | free(fname); | ||
52 | return 1; // assume the user doesn't care about access checking | ||
53 | } | ||
54 | |||
55 | FILE *fp = fopen(fname, "r"); | ||
56 | free(fname); | ||
57 | if (!fp) | ||
58 | return 0; | ||
59 | |||
60 | char buf[MAXBUF]; | ||
61 | while (fgets(buf, MAXBUF, fp)) { | ||
62 | // lines starting with # are comments | ||
63 | if (*buf == '#') | ||
64 | continue; | ||
65 | |||
66 | // remove \n | ||
67 | char *ptr = strchr(buf, '\n'); | ||
68 | if (ptr) | ||
69 | *ptr = '\0'; | ||
70 | |||
71 | // compare | ||
72 | if (strcmp(buf, name) == 0) { | ||
73 | fclose(fp); | ||
74 | return 1; | ||
75 | } | ||
76 | } | ||
77 | |||
78 | fclose(fp); | ||
79 | return 0; | ||
80 | } | ||
81 | |||
82 | // add a user to the database | ||
83 | void firejail_user_add(const char *name) { | ||
84 | assert(name); | ||
85 | |||
86 | // is this a real user? | ||
87 | struct passwd *pw = getpwnam(name); | ||
88 | if (!pw) { | ||
89 | fprintf(stderr, "Error: user %s not found on this system.\n", name); | ||
90 | return; | ||
91 | } | ||
92 | |||
93 | // check the user is not already in the database | ||
94 | char *fname = get_fname(); | ||
95 | assert(fname); | ||
96 | if (access(fname, F_OK) == 0) { | ||
97 | if (firejail_user_check(name)) { | ||
98 | printf("User %s already in the database\n", name); | ||
99 | return; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | printf("%s created\n", fname); | ||
104 | FILE *fp = fopen(fname, "a+"); | ||
105 | if (!fp) { | ||
106 | fprintf(stderr, "Error: cannot open %s\n", fname); | ||
107 | perror("fopen"); | ||
108 | free(fname); | ||
109 | return; | ||
110 | } | ||
111 | free(fname); | ||
112 | |||
113 | fprintf(fp, "%s\n", name); | ||
114 | fclose(fp); | ||
115 | } | ||