]> code.delx.au - pulseaudio/blob - polyp/caps.c
Make the whole stuff LGPL only
[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 void pa_drop_root(void) {
39 uid_t uid = getuid();
40
41 if (uid == 0 || geteuid() != 0)
42 return;
43
44 pa_log(__FILE__": dropping root rights.\n");
45
46 setreuid(uid, uid);
47 /* setuid(uid);
48 seteuid(uid);*/
49 }
50
51 #ifdef HAVE_SYS_CAPABILITY_H
52
53 int pa_limit_caps(void) {
54 int r = -1;
55 cap_t caps;
56 cap_value_t nice_cap = CAP_SYS_NICE;
57
58 caps = cap_init();
59 assert(caps);
60
61 cap_clear(caps);
62
63 cap_set_flag(caps, CAP_EFFECTIVE, 1, &nice_cap, CAP_SET);
64 cap_set_flag(caps, CAP_PERMITTED, 1, &nice_cap, CAP_SET);
65
66 if (cap_set_proc(caps) < 0)
67 goto fail;
68
69 pa_log(__FILE__": dropped capabilities successfully.\n");
70
71 r = 0;
72
73 fail:
74 cap_free (caps);
75
76 return r;
77 }
78
79 int pa_drop_caps(void) {
80 cap_t caps;
81 int r = -1;
82
83 caps = cap_init();
84 assert(caps);
85
86 cap_clear(caps);
87
88 if (cap_set_proc(caps) < 0) {
89 pa_log(__FILE__": failed to drop capabilities: %s\n", strerror(errno));
90 goto fail;
91 }
92
93 r = 0;
94
95 fail:
96 cap_free (caps);
97
98 return r;
99 }
100
101 #else
102
103 int pa_limit_caps(void) {
104 return 0;
105 }
106
107 int pa_drop_caps(void) {
108 pa_drop_root();
109 return 0;
110 }
111
112 #endif
113