aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/firejail_user.c
blob: b0f56a19a48a77f6a1da4729a49a00ba19f0f088 (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (C) 2014-2018 Firejail Authors
 *
 * This file is part of firejail project
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

//
// Firejail access database inplementation
//
// The database is a simple list of users allowed to run firejail SUID executable
// It is usually stored in /etc/firejail/firejail.users
// One username per line in the file

#include "../include/common.h"
#include "../include/firejail_user.h"
#include <sys/types.h>
#include <pwd.h>

#define MAXBUF 4098

// minimum values for uid and gid extracted from /etc/login.defs
int uid_min = 0;
int gid_min = 0;

static void init_uid_gid_min(void) {
	if (uid_min != 0 && gid_min != 0)
		return;

	// read the real values from login.def
	FILE *fp = fopen("/etc/login.defs", "r");
	if (!fp)
		goto errexit;

	char buf[MAXBUF];
	while (fgets(buf, MAXBUF, fp)) {
		// comments
		if (*buf == '#')
			continue;
		// skip empty space
		char *ptr = buf;
		while (*ptr == ' ' || *ptr == '\t')
			ptr++;

		if (strncmp(ptr, "UID_MIN", 7) == 0) {
			int rv = sscanf(ptr + 7, "%d", &uid_min);
			if (rv != 1 || uid_min < 0) {
				fclose(fp);
				goto errexit;
			}
		}
		else if (strncmp(ptr, "GID_MIN", 7) == 0) {
			int rv = sscanf(ptr + 7, "%d", &gid_min);
			if (rv != 1 || gid_min < 0) {
				fclose(fp);
				goto errexit;
			}
		}

		if (uid_min != 0 && gid_min != 0)
			break;

	}
	fclose(fp);

	if (uid_min == 0 || gid_min == 0)
		goto errexit;
//printf("uid_min %d, gid_min %d\n", uid_min, gid_min);

	return;

errexit:
	fprintf(stderr, "Error: cannot read UID_MIN and/or GID_MIN from /etc/login.defs, using 1000 by default\n");
	uid_min = 1000;
	gid_min = 1000;
}



static inline char *get_fname(void) {
	char *fname;
	if (asprintf(&fname, "%s/firejail.users", SYSCONFDIR) == -1)
		errExit("asprintf");
	return fname;
}


// returns 1 if the user is found in the database or if the database was not created
int firejail_user_check(const char *name) {
	assert(name);
	init_uid_gid_min();

	// root is allowed to run firejail by default
	if (strcmp(name, "root") == 0)
		return 1;

	// user nobody is never allowed
	if (strcmp(name, "root") == 0)
		return 0;

	// check file existence
	char *fname = get_fname();
	if (access(fname, F_OK)) {
		free(fname);
		return 1;	// assume the user doesn't care about access checking
	}

	FILE *fp = fopen(fname, "r");
	free(fname);
	if (!fp)
		return 0;

	char buf[MAXBUF];
	while (fgets(buf, MAXBUF, fp)) {
		// lines starting with # are comments
		if (*buf == '#')
			continue;

		// remove \n
		char *ptr = strchr(buf, '\n');
		if (ptr)
			*ptr = '\0';

		// compare
		if (strcmp(buf, name) == 0) {
			fclose(fp);
			return 1;
		}
	}

	fclose(fp);
	return 0;
}

// add a user to the database
void firejail_user_add(const char *name) {
	assert(name);

	// is this a real user?
	struct passwd *pw = getpwnam(name);
	if (!pw) {
		fprintf(stderr, "Error: user %s not found on this system.\n", name);
		exit(1);
	}

	// check the user is not already in the database
	char *fname = get_fname();
	assert(fname);
	if (access(fname, F_OK) == 0) {
		if (firejail_user_check(name)) {
			printf("User %s already in the database\n", name);
			return;
		}
	}

	printf("%s created\n", fname);
	FILE *fp = fopen(fname, "a+");
	if (!fp) {
		fprintf(stderr, "Error: cannot open %s\n", fname);
		perror("fopen");
		free(fname);
		return;
	}
	free(fname);

	fprintf(fp, "%s\n", name);
	fclose(fp);
}