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