chiark / gitweb /
[PATCH] Deleted the udev_dbus extra as it didn't really work properly and HAL has...
[elogind.git] / extras / selinux / udev_selinux.c
1 /*
2  * udev_selinux.c
3  *
4  * Copyright (C) 2004 Daniel J Walsh <dwalsh@redhat.com>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <selinux/selinux.h>
26
27 #include "../../udev_lib.h"
28 #include "../../logging.h"
29
30 #ifdef LOG
31 unsigned char logname[LOGNAME_SIZE];
32 void log_message(int level, const char *format, ...)
33 {
34         va_list args;
35
36         va_start(args, format);
37         vsyslog(level, format, args);
38         va_end(args);
39 }
40 #endif
41
42 static void selinux_add_node(char *filename)
43 {
44         int retval;
45
46         if (is_selinux_enabled() > 0) {
47                 security_context_t scontext;
48                 retval = matchpathcon(filename, 0, &scontext);
49                 if (retval < 0) {
50                         dbg("matchpathcon(%s) failed\n", filename);
51                 } else {
52                         retval = setfilecon(filename,scontext);
53                         if (retval < 0)
54                                 dbg("setfiles %s failed with error '%s'",
55                                     filename, strerror(errno));
56                         free(scontext);
57                 }
58         }
59 }
60
61 int main(int argc, char *argv[], char *envp[])
62 {
63         char *action;
64         char *devname;
65         int retval = 0;
66
67         init_logging("udev_selinux");
68
69         action = get_action();
70         if (!action) {
71                 dbg("no action?");
72                 goto exit;
73         }
74         devname = get_devname();
75         if (!devname) {
76                 dbg("no devname?");
77                 goto exit;
78         }
79
80         if (strcmp(action, "add") == 0)
81                 selinux_add_node(devname);
82
83 exit:
84         return retval;
85 }