chiark / gitweb /
4f1b2bb2d1ae627acc0b9d16f8d731640f64d3a7
[elogind.git] / udev / udev-selinux.c
1 /*
2  * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stddef.h>
21 #include <stdarg.h>
22 #include <unistd.h>
23 #include <selinux/selinux.h>
24
25 #include "udev.h"
26
27 static int selinux_enabled;
28 security_context_t selinux_prev_scontext;
29
30 void udev_selinux_init(struct udev *udev)
31 {
32         /* record the present security context */
33         selinux_enabled = (is_selinux_enabled() > 0);
34         info(udev, "selinux=%i\n", selinux_enabled);
35         if (!selinux_enabled)
36                 return;
37         matchpathcon_init_prefix(NULL, udev_get_dev_path(udev));
38         if (getfscreatecon(&selinux_prev_scontext) < 0) {
39                 err(udev, "getfscreatecon failed\n");
40                 selinux_prev_scontext = NULL;
41         }
42 }
43
44 void udev_selinux_exit(struct udev *udev)
45 {
46         if (!selinux_enabled)
47                 return;
48         freecon(selinux_prev_scontext);
49         selinux_prev_scontext = NULL;
50 }
51
52 void udev_selinux_lsetfilecon(struct udev *udev, const char *file, unsigned int mode)
53 {
54         security_context_t scontext = NULL;
55
56         if (!selinux_enabled)
57                 return;
58         if (matchpathcon(file, mode, &scontext) < 0) {
59                 err(udev, "matchpathcon(%s) failed\n", file);
60                 return;
61         } 
62         if (lsetfilecon(file, scontext) < 0)
63                 err(udev, "setfilecon %s failed: %m\n", file);
64         freecon(scontext);
65 }
66
67 void udev_selinux_setfscreatecon(struct udev *udev, const char *file, unsigned int mode)
68 {
69         security_context_t scontext = NULL;
70
71         if (!selinux_enabled)
72                 return;
73         if (matchpathcon(file, mode, &scontext) < 0) {
74                 err(udev, "matchpathcon(%s) failed\n", file);
75                 return;
76         }
77         if (setfscreatecon(scontext) < 0)
78                 err(udev, "setfscreatecon %s failed: %m\n", file);
79         freecon(scontext);
80 }
81
82 void udev_selinux_resetfscreatecon(struct udev *udev)
83 {
84         if (!selinux_enabled)
85                 return;
86         if (setfscreatecon(selinux_prev_scontext) < 0)
87                 err(udev, "setfscreatecon failed: %m\n");
88 }