]> code.delx.au - pulseaudio/blob - polyp/caps.c
* make pa_sample_spec_snprint return point to written string
[pulseaudio] / polyp / 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 "log.h"
36 #include "caps.h"
37
38 /* Drop root rights when called SUID root */
39 void pa_drop_root(void) {
40 uid_t uid = getuid();
41
42 if (uid == 0 || geteuid() != 0)
43 return;
44
45 pa_log_info(__FILE__": dropping root rights.\n");
46
47 #if defined(HAVE_SETRESUID)
48 setresuid(uid, uid, uid);
49 #elif defined(HAVE_SETREUID)
50 setreuid(uid, uid);
51 #else
52 setuid(uid);
53 seteuid(uid);
54 #endif
55 }
56
57 #ifdef HAVE_SYS_CAPABILITY_H
58
59 /* Limit capabilities set to CAPSYS_NICE */
60 int pa_limit_caps(void) {
61 int r = -1;
62 cap_t caps;
63 cap_value_t nice_cap = CAP_SYS_NICE;
64
65 caps = cap_init();
66 assert(caps);
67
68 cap_clear(caps);
69
70 cap_set_flag(caps, CAP_EFFECTIVE, 1, &nice_cap, CAP_SET);
71 cap_set_flag(caps, CAP_PERMITTED, 1, &nice_cap, CAP_SET);
72
73 if (cap_set_proc(caps) < 0)
74 goto fail;
75
76 pa_log_info(__FILE__": dropped capabilities successfully.\n");
77
78 r = 0;
79
80 fail:
81 cap_free (caps);
82
83 return r;
84 }
85
86 /* Drop all capabilities, effectively becoming a normal user */
87 int pa_drop_caps(void) {
88 cap_t caps;
89 int r = -1;
90
91 caps = cap_init();
92 assert(caps);
93
94 cap_clear(caps);
95
96 if (cap_set_proc(caps) < 0) {
97 pa_log(__FILE__": failed to drop capabilities: %s\n", strerror(errno));
98 goto fail;
99 }
100
101 r = 0;
102
103 fail:
104 cap_free (caps);
105
106 return r;
107 }
108
109 #else
110
111 /* NOOPs in case capabilities are not available. */
112 int pa_limit_caps(void) {
113 return 0;
114 }
115
116 int pa_drop_caps(void) {
117 pa_drop_root();
118 return 0;
119 }
120
121 #endif
122