aboutsummaryrefslogtreecommitdiff
path: root/lib/userprivs.c
blob: 5106b522f7ac5282b5295ab0240272233ef9decb (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* This file is part of Mailfromd.
   Copyright (C) 2007-2019 Sergey Poznyakoff

   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 3, 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, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <mailutils/assoc.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/nls.h>
#include <mailutils/list.h>
#include <mailutils/iterator.h>
#include <mailutils/alloc.h>
#include <sysexits.h>
#include "libmf.h"

#define NGIDSINBLOCK 16

struct mf_gid_block {
	struct mf_gid_block *next;
	size_t count;
	size_t size;
	gid_t gid[1];
};
	
struct mf_gid_list {
	struct mf_gid_block *head, *tail;
	int sorted;
};

struct mf_gid_list *
mf_gid_list_alloc() 
{
	struct mf_gid_list *p = mu_alloc(sizeof(*p));
	p->head = p->tail = NULL;
	p->sorted = 0;
	return p;
}

void
mf_gid_list_free(struct mf_gid_list *gl)
{
	struct mf_gid_block *p;
	if (!gl)
		return;
	for (p = gl->head; p; ) {
		struct mf_gid_block *next = p->next;
		free(p);
		p = next;
	}
}

static void
mf_gid_list_add_block(struct mf_gid_list *gl)
{
	struct mf_gid_block *p = mu_alloc(sizeof(*p) + NGIDSINBLOCK);
	p->next = NULL;
	p->size = NGIDSINBLOCK;
	p->count = 0;
	if (gl->tail)
		gl->tail->next = p;
	else
		gl->head = p;
	gl->tail = p;
}
	
void
mf_gid_list_add(struct mf_gid_list *gl, gid_t gid)
{
	if (!gl->tail || gl->tail->count == gl->tail->size)
		mf_gid_list_add_block(gl);
	gl->tail->gid[gl->tail->count++] = gid;
	gl->sorted = 0;
}

struct mf_gid_list *
mf_gid_list_dup(struct mf_gid_list *src)
{
	struct mf_gid_list *dst = mf_gid_list_alloc();
	struct mf_gid_block *p;

	if (src) { 
		for (p = src->head; p; p = p->next) {
			size_t i;
			for (i = 0; i < p->count; i++)
				mf_gid_list_add(dst, p->gid[i]);
		}
	}
	
	return dst;
}

static int
gid_cmp(const void *a, const void *b)
{
	gid_t const *ga = a;
	gid_t const *gb = b;

	if (*ga < *gb)
		return -1;
	if (*ga > *gb)
		return 1;
	return 0;
}

void
mf_gid_list_array(struct mf_gid_list *gl, size_t *gc, gid_t **gv)
{
	struct mf_gid_block *p;
	size_t i, j;
	
	if (!gl || gl->head == NULL) {
		*gc = 0;
		*gv = NULL;
		return;
	}

	p = gl->head;
	if (p->next) {
		size_t n;
		struct mf_gid_block *q;
	
		for (n = 0, p = gl->head; p; p = p->next)
			n += p->count;
		p = mu_realloc(gl->head, sizeof(*p) + n);
		gl->head = p;
		n = p->count;
		for (q = p->next; q; ) {
			struct mf_gid_block *next = q->next;
			size_t i;
			for (i = 0; i < q->count; i++)
				p->gid[n++] = q->gid[i];
			free(q);
			q = next;
		}
		p->count = n;
		p->next = NULL;
	}

	if (!gl->sorted) {
		qsort(p->gid, p->count, sizeof(p->gid[0]), gid_cmp);

		for (i = j = 1; i < p->count; j++)
			if (p->gid[j-1] != p->gid[j])
				p->gid[i++] = p->gid[j];
		p->count = i;
		gl->sorted = 1;
	}
	
	*gc = p->count;
	*gv = p->gid;
}
	
void
get_user_groups(struct mf_gid_list *gl, const char *user)
{
	struct group *gr;
	
	setgrent();
	while ((gr = getgrent())) {
		char **p;
		for (p = gr->gr_mem; *p; p++)
			if (strcmp(*p, user) == 0) {
				mf_gid_list_add(gl, gr->gr_gid);
			}
	}
	endgrent();
}

/* Switch to the given UID/GID */
int
switch_to_privs(uid_t uid, gid_t gid, struct mf_gid_list *retain_groups)
{
	int rc = 0;
	struct mf_gid_list *gl;
	size_t gc;
	gid_t *gv;

	if (uid == 0) {
		mu_error(_("refusing to run as root"));
		return 1;
	}

	/* Create a list of supplementary groups */
	gl = mf_gid_list_dup(retain_groups);
	mf_gid_list_add(gl, gid ? gid : getegid());
	mf_gid_list_array(gl, &gc, &gv);

	/* Reset group permissions */
	if (geteuid() == 0 && setgroups(gc, gv)) {
		mu_error(_("setgroups failed: %s"),
			 mu_strerror(errno));
		rc = 1;
	}
	mf_gid_list_free(gl);
	
	/* Switch to the user's gid. On some OSes the effective gid must
	   be reset first */

#if defined(HAVE_SETEGID)
	if ((rc = setegid(gid)) < 0)
		mu_error(_("setegid(%lu) failed: %s"),
			 (unsigned long) gid, mu_strerror(errno));
#elif defined(HAVE_SETREGID)
	if ((rc = setregid(gid, gid)) < 0)
		mu_error(_("setregid(%lu,%lu) failed: %s"),
		         (unsigned long) gid, (unsigned long) gid,
			 mu_strerror(errno));
#elif defined(HAVE_SETRESGID)
	if ((rc = setresgid(gid, gid, gid)) < 0)
		mu_error(_("setresgid(%lu,%lu,%lu) failed: %s"),
		         (unsigned long) gid,
		         (unsigned long) gid,
		         (unsigned long) gid,
			 mu_strerror(errno));
#endif

	if (rc == 0 && gid != 0) {
		if ((rc = setgid(gid)) < 0 && getegid() != gid) 
			mu_error(_("setgid(%lu) failed: %s"),
				 (unsigned long) gid, mu_strerror(errno));
		if (rc == 0 && getegid() != gid) {
			mu_error(_("cannot set effective gid to %lu"),
			         (unsigned long) gid);
			rc = 1;
		}
	}

	/* Now reset uid */
	if (rc == 0 && uid != 0) {
		uid_t euid;

		if (setuid(uid)
		    || geteuid() != uid
		    || (getuid() != uid
			&& (geteuid() == 0 || getuid() == 0))) {
			
#if defined(HAVE_SETREUID)
			if (geteuid() != uid) {
				if (setreuid(uid, -1) < 0) { 
					mu_error(_("setreuid(%lu,-1) failed: %s"),
					         (unsigned long) uid,
						 mu_strerror(errno));
					rc = 1;
				}
				if (setuid(uid) < 0) {
					mu_error(_("second setuid(%lu) failed: %s"),
					         (unsigned long) uid,
						 mu_strerror(errno));
					rc = 1;
				}
			} else
#endif
				{
					mu_error(_("setuid(%lu) failed: %s"),
					         (unsigned long) uid,
						 mu_strerror(errno));
					rc = 1;
				}
		}
	
		euid = geteuid();
		if (uid != 0 && setuid(0) == 0) {
			mu_error(_("seteuid(0) succeeded when it should not"));
			rc = 1;
		} else if (uid != euid && setuid(euid) == 0) {
			mu_error(_("cannot drop non-root setuid privileges"));
			rc = 1;
		}

	}

	return rc;
}


static int
translate_item (void *item, void *data)
{
	struct mf_gid_list *dst = data;
	struct group *group = getgrnam(item);
	if (!group) {
		mu_error(_("unknown group: %s"), (char*) item);
		return 1;
	}
	mf_gid_list_add(dst, group->gr_gid);
	return 0;
}

static struct mf_gid_list *
grouplist_translate(mu_list_t src)
{
	struct mf_gid_list *dst = mf_gid_list_alloc();
	mu_list_foreach(src, translate_item, dst);
	return dst;
}
	
void
mf_priv_setup(struct mf_privs *privs)
{
	struct passwd *pw;
	struct mf_gid_list *grp;

	if (!privs || !privs->user)
		return;

	pw = getpwnam(privs->user);
	if (!pw) {
		mu_error(_("no such user: %s"), privs->user);
		exit(EX_CONFIG);
	}

	grp = grouplist_translate(privs->groups);
	if (privs->allgroups)
		get_user_groups(grp, privs->user);
	if (switch_to_privs(pw->pw_uid, pw->pw_gid, grp))
		exit(EX_SOFTWARE);
	mf_gid_list_free(grp);
}


void
mf_epriv_setup(struct mf_privs *privs)
{
	uid_t uid;
	gid_t gid;
	
	if (privs) {
		struct passwd *pw;
		if (!privs->user)
			return;

		pw = getpwnam(privs->user);
		if (!pw) {
			mu_error(_("No such user: %s"), privs->user);
			exit (EX_CONFIG);
		}
		uid = pw->pw_uid;
		gid = pw->pw_gid;
	} else {
		uid = 0;
		gid = 0;
	}
	
	if (setegid(gid)) {
		mu_error(_("cannot switch to EGID %lu: %s"),
			 (unsigned long) gid, mu_strerror(errno));
		exit(EX_USAGE);
	}
	if (seteuid(uid)) {
		mu_error(_("cannot switch to EUID %lu: %s"),
			 (unsigned long) uid, mu_strerror(errno));
		exit(EX_USAGE);
	}
}

Return to:

Send suggestions and report system problems to the System administrator.