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