]> code.delx.au - pulseaudio/blob - src/daemon/caps.c
remove all occurences of
[pulseaudio] / src / daemon / caps.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 #include <sys/types.h>
31
32 #ifdef HAVE_SYS_CAPABILITY_H
33 #include <sys/capability.h>
34 #endif
35
36 #include <pulsecore/core-error.h>
37
38 #include <pulsecore/log.h>
39
40 #include "caps.h"
41
42 /* Glibc <= 2.2 has broken unistd.h */
43 #if defined(linux) && (__GLIBC__ <= 2 && __GLIBC_MINOR__ <= 2)
44 int setresgid(gid_t r, gid_t e, gid_t s);
45 int setresuid(uid_t r, uid_t e, uid_t s);
46 #endif
47
48 #ifdef HAVE_GETUID
49
50 /* Drop root rights when called SUID root */
51 void pa_drop_root(void) {
52 uid_t uid = getuid();
53
54 if (uid == 0 || geteuid() != 0)
55 return;
56
57 pa_log_info("dropping root rights.");
58
59 #if defined(HAVE_SETRESUID)
60 setresuid(uid, uid, uid);
61 #elif defined(HAVE_SETREUID)
62 setreuid(uid, uid);
63 #else
64 setuid(uid);
65 seteuid(uid);
66 #endif
67 }
68
69 #else
70
71 void pa_drop_root(void) {
72 }
73
74 #endif
75
76 #ifdef HAVE_SYS_CAPABILITY_H
77
78 /* Limit capabilities set to CAPSYS_NICE */
79 int pa_limit_caps(void) {
80 int r = -1;
81 cap_t caps;
82 cap_value_t nice_cap = CAP_SYS_NICE;
83
84 /* Only drop caps when called SUID */
85 if (getuid() == 0)
86 return 0;
87
88 caps = cap_init();
89 assert(caps);
90
91 cap_clear(caps);
92
93 cap_set_flag(caps, CAP_EFFECTIVE, 1, &nice_cap, CAP_SET);
94 cap_set_flag(caps, CAP_PERMITTED, 1, &nice_cap, CAP_SET);
95
96 if (cap_set_proc(caps) < 0)
97 goto fail;
98
99 pa_log_info("dropped capabilities successfully.");
100
101 r = 0;
102
103 fail:
104 cap_free (caps);
105
106 return r;
107 }
108
109 /* Drop all capabilities, effectively becoming a normal user */
110 int pa_drop_caps(void) {
111 cap_t caps;
112 int r = -1;
113
114 /* Only drop caps when called SUID */
115 if (getuid() == 0)
116 return 0;
117
118 caps = cap_init();
119 assert(caps);
120
121 cap_clear(caps);
122
123 if (cap_set_proc(caps) < 0) {
124 pa_log("failed to drop capabilities: %s", pa_cstrerror(errno));
125 goto fail;
126 }
127
128 r = 0;
129
130 fail:
131 cap_free (caps);
132
133 return r;
134 }
135
136 #else
137
138 /* NOOPs in case capabilities are not available. */
139 int pa_limit_caps(void) {
140 return 0;
141 }
142
143 int pa_drop_caps(void) {
144 pa_drop_root();
145 return 0;
146 }
147
148 #endif
149