chiark / gitweb /
keymap: move from udev-extras
[elogind.git] / libudev / libudev-selinux-private.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stddef.h>
15 #include <stdarg.h>
16 #include <unistd.h>
17 #include <selinux/selinux.h>
18
19 #include "libudev.h"
20 #include "libudev-private.h"
21
22 static int selinux_enabled;
23 security_context_t selinux_prev_scontext;
24
25 void udev_selinux_init(struct udev *udev)
26 {
27         /* record the present security context */
28         selinux_enabled = (is_selinux_enabled() > 0);
29         info(udev, "selinux=%i\n", selinux_enabled);
30         if (!selinux_enabled)
31                 return;
32         matchpathcon_init_prefix(NULL, udev_get_dev_path(udev));
33         if (getfscreatecon(&selinux_prev_scontext) < 0) {
34                 err(udev, "getfscreatecon failed\n");
35                 selinux_prev_scontext = NULL;
36         }
37 }
38
39 void udev_selinux_exit(struct udev *udev)
40 {
41         if (!selinux_enabled)
42                 return;
43         freecon(selinux_prev_scontext);
44         selinux_prev_scontext = NULL;
45 }
46
47 void udev_selinux_lsetfilecon(struct udev *udev, const char *file, unsigned int mode)
48 {
49         security_context_t scontext = NULL;
50
51         if (!selinux_enabled)
52                 return;
53         if (matchpathcon(file, mode, &scontext) < 0) {
54                 err(udev, "matchpathcon(%s) failed\n", file);
55                 return;
56         } 
57         if (lsetfilecon(file, scontext) < 0)
58                 err(udev, "setfilecon %s failed: %m\n", file);
59         freecon(scontext);
60 }
61
62 void udev_selinux_setfscreatecon(struct udev *udev, const char *file, unsigned int mode)
63 {
64         security_context_t scontext = NULL;
65
66         if (!selinux_enabled)
67                 return;
68         if (matchpathcon(file, mode, &scontext) < 0) {
69                 err(udev, "matchpathcon(%s) failed\n", file);
70                 return;
71         }
72         if (setfscreatecon(scontext) < 0)
73                 err(udev, "setfscreatecon %s failed: %m\n", file);
74         freecon(scontext);
75 }
76
77 void udev_selinux_resetfscreatecon(struct udev *udev)
78 {
79         if (!selinux_enabled)
80                 return;
81         if (setfscreatecon(selinux_prev_scontext) < 0)
82                 err(udev, "setfscreatecon failed: %m\n");
83 }