]> code.delx.au - pulseaudio/blob - src/daemon/caps.c
9540f0666f53ebb1a8b14cf94f4eb7cf821ab278
[pulseaudio] / src / daemon / caps.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <unistd.h>
28 #include <errno.h>
29 #include <sys/types.h>
30
31 #include <pulsecore/i18n.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/log.h>
34
35 #ifdef HAVE_SYS_CAPABILITY_H
36 #include <sys/capability.h>
37 #endif
38
39 #include "caps.h"
40
41 /* Glibc <= 2.2 has broken unistd.h */
42 #if defined(__linux__) && (__GLIBC__ <= 2 && __GLIBC_MINOR__ <= 2)
43 int setresgid(gid_t r, gid_t e, gid_t s);
44 int setresuid(uid_t r, uid_t e, uid_t s);
45 #endif
46
47 /* Drop root rights when called SUID root */
48 void pa_drop_root(void) {
49
50 #ifdef HAVE_GETUID
51 uid_t uid;
52 gid_t gid;
53
54 pa_log_debug(_("Cleaning up privileges."));
55 uid = getuid();
56 gid = getgid();
57
58 #if defined(HAVE_SETRESUID)
59 pa_assert_se(setresuid(uid, uid, uid) >= 0);
60 pa_assert_se(setresgid(gid, gid, gid) >= 0);
61 #elif defined(HAVE_SETREUID)
62 pa_assert_se(setreuid(uid, uid) >= 0);
63 pa_assert_se(setregid(gid, gid) >= 0);
64 #else
65 pa_assert_se(setuid(uid) >= 0);
66 pa_assert_se(seteuid(uid) >= 0);
67 pa_assert_se(setgid(gid) >= 0);
68 pa_assert_se(setegid(gid) >= 0);
69 #endif
70
71 pa_assert_se(getuid() == uid);
72 pa_assert_se(geteuid() == uid);
73 pa_assert_se(getgid() == gid);
74 pa_assert_se(getegid() == gid);
75
76 if (uid != 0)
77 pa_drop_caps();
78 #endif
79 }
80
81 void pa_drop_caps(void) {
82 #ifdef HAVE_SYS_CAPABILITY_H
83 #if defined(__linux__)
84 cap_t caps;
85 pa_assert_se(caps = cap_init());
86 pa_assert_se(cap_clear(caps) == 0);
87 pa_assert_se(cap_set_proc(caps) == 0);
88 pa_assert_se(cap_free(caps) == 0);
89 #else
90 #error "Don't know how to do capabilities on your system. Please send a patch."
91 #endif /* __linux__ */
92 #else /* HAVE_SYS_CAPABILITY_H */
93 pa_log_warn("Normally all extra capabilities would be dropped now, but "
94 "that's impossible because PulseAudio was built without "
95 "capabilities support.");
96 #endif
97 }