aboutsummaryrefslogtreecommitdiff
path: root/src/userprivs.c
blob: 13a2d872b3cb66a785a4927a35baa842222c3db1 (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
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2009-2013 Sergey Poznyakoff

   Wydawca 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 of the License, or (at your
   option) any later version.

   Wydawca 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 wydawca. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"

int
wydawca_userprivs(uid_t uid, gid_t gid, gid_t * grplist, size_t ngrp)
{
	int rc = 0;

	if (uid == 0)
		return 0;

	/* Reset group permissions */
	if (geteuid() == 0 && setgroups(ngrp, grplist)) {
		wy_log(LOG_CRIT, "setgroups(%lu, %lu...): %s",
		       (unsigned long)ngrp, (unsigned long)grplist[0],
		       strerror(errno));
		return rc;
	}

	/* 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)
		wy_log(LOG_CRIT, "setegid(%lu): %s",
		       (unsigned long)gid, strerror(errno));
#elif defined(HAVE_SETREGID)
	if ((rc = setregid(gid, gid)) < 0)
		wy_log(LOG_CRIT, "setregid(%lu,%lu)d: %s",
		       (unsigned long)gid, (unsigned long)gid,
		       strerror(errno));
#elif defined(HAVE_SETRESGID)
	if ((rc = setresgid(gid, gid, gid)) < 0)
		wy_log(LOG_CRIT, "setresgid(%lu,%lu,%lu): %s",
		       (unsigned long)gid, (unsigned long)gid,
		       (unsigned long)gid, strerror(errno));
#endif

	if (rc == 0 && gid != 0) {
		if ((rc = setgid(gid)) < 0 && getegid() != gid)
			wy_log(LOG_CRIT, "setgid(%lu): %s",
			       (unsigned long)gid, strerror(errno));
		if (rc == 0 && getegid() != gid) {
			wy_log(LOG_CRIT, _("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) {
					wy_log(LOG_CRIT,
					       "setreuid(%lu,-1): %s",
					       (unsigned long)uid,
					       strerror(errno));
					rc = 1;
				}
				if (setuid(uid) < 0) {
					wy_log(LOG_CRIT,
					       "setreuid(%lu,-1): %s",
					       (unsigned long)uid,
					       strerror(errno));
					rc = 1;
				}
			} else
#endif
			         {
				wy_log(LOG_CRIT, "setuid(%lu): %s",
				       (unsigned long)uid, strerror(errno));
				rc = 1;
			}
		}

		euid = geteuid();
		if (uid != 0 && setuid(0) == 0) {
			wy_log(LOG_CRIT,
			       _("seteuid(0) succeeded when it should not"));
			rc = 1;
		} else if (uid != euid && setuid(euid) == 0) {
			wy_log(LOG_CRIT,
			       _("cannot drop non-root setuid privileges"));
			rc = 1;
		}
	}

	return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.