]> code.delx.au - pulseaudio/blob - src/daemon/caps.c
Don't include util.h from core-util.h as it is not needed by many users.
[pulseaudio] / src / daemon / caps.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <string.h>
30
31 #ifdef HAVE_SYS_CAPABILITY_H
32 #include <sys/capability.h>
33 #endif
34
35 #include <polypcore/log.h>
36
37 #include "caps.h"
38
39 /* Glibc <= 2.2 has broken unistd.h */
40 #if defined(linux) && (__GLIBC__ <= 2 && __GLIBC_MINOR__ <= 2)
41 int setresgid(gid_t r, gid_t e, gid_t s);
42 int setresuid(uid_t r, uid_t e, uid_t s);
43 #endif
44
45 #ifdef HAVE_GETUID
46
47 /* Drop root rights when called SUID root */
48 void pa_drop_root(void) {
49 uid_t uid = getuid();
50
51 if (uid == 0 || geteuid() != 0)
52 return;
53
54 pa_log_info(__FILE__": dropping root rights.");
55
56 #if defined(HAVE_SETRESUID)
57 setresuid(uid, uid, uid);
58 #elif defined(HAVE_SETREUID)
59 setreuid(uid, uid);
60 #else
61 setuid(uid);
62 seteuid(uid);
63 #endif
64 }
65
66 #else
67
68 void pa_drop_root(void) {
69 }
70
71 #endif
72
73 #ifdef HAVE_SYS_CAPABILITY_H
74
75 /* Limit capabilities set to CAPSYS_NICE */
76 int pa_limit_caps(void) {
77 int r = -1;
78 cap_t caps;
79 cap_value_t nice_cap = CAP_SYS_NICE;
80
81 caps = cap_init();
82 assert(caps);
83
84 cap_clear(caps);
85
86 cap_set_flag(caps, CAP_EFFECTIVE, 1, &nice_cap, CAP_SET);
87 cap_set_flag(caps, CAP_PERMITTED, 1, &nice_cap, CAP_SET);
88
89 if (cap_set_proc(caps) < 0)
90 goto fail;
91
92 pa_log_info(__FILE__": dropped capabilities successfully.");
93
94 r = 0;
95
96 fail:
97 cap_free (caps);
98
99 return r;
100 }
101
102 /* Drop all capabilities, effectively becoming a normal user */
103 int pa_drop_caps(void) {
104 cap_t caps;
105 int r = -1;
106
107 caps = cap_init();
108 assert(caps);
109
110 cap_clear(caps);
111
112 if (cap_set_proc(caps) < 0) {
113 pa_log(__FILE__": failed to drop capabilities: %s", strerror(errno));
114 goto fail;
115 }
116
117 r = 0;
118
119 fail:
120 cap_free (caps);
121
122 return r;
123 }
124
125 #else
126
127 /* NOOPs in case capabilities are not available. */
128 int pa_limit_caps(void) {
129 return 0;
130 }
131
132 int pa_drop_caps(void) {
133 pa_drop_root();
134 return 0;
135 }
136
137 #endif
138