chiark / gitweb /
[PATCH] PATCH selinux for udev
[elogind.git] / selinux.h
1 #ifndef SELINUX_H
2 #define SELINUX_H
3
4 #ifndef USE_SELINUX
5
6 static inline void selinux_setfilecon(char *file, unsigned int mode) { }
7 static inline void selinux_setfscreatecon(char *file, unsigned int mode) {}
8 static inline void selinux_init(void) {}
9 static inline void selinux_restore(void) {}
10
11 #else
12
13 #include <selinux/selinux.h>
14 #include <stdio.h>
15 #include <limits.h>
16 #include <ctype.h>
17
18
19 static int selinux_enabled=-1;
20 static security_context_t prev_scontext=NULL;
21
22 static inline int is_selinux_running(void) {
23         if ( selinux_enabled==-1 ) 
24                 return selinux_enabled=is_selinux_enabled()>0;
25         return selinux_enabled;
26 }
27
28 static inline int selinux_get_media(char *path, int mode, char **media)
29 {
30   FILE *fp;
31   char buf[PATH_MAX];
32   char mediabuf[PATH_MAX];
33   *media=NULL;
34   if (!( mode && S_IFBLK )) {
35           return -1;
36   }
37   snprintf(buf,sizeof(buf), "/proc/ide/%s/media", basename(path));
38   fp=fopen(buf,"r");
39   if (fp) {
40           if (fgets(mediabuf,sizeof(mediabuf), fp)) {
41                   int size=strlen(mediabuf);
42                   while (size-- > 0) {
43                           if (isspace(mediabuf[size])) {
44                                   mediabuf[size]='\0';
45                           } else {
46                                   break;
47                           }
48                   }
49                   *media=strdup(mediabuf);
50                   info("selinux_get_media(%s)->%s \n", path, *media);
51           }
52     fclose(fp);
53     return 0;
54   } else {
55     return -1;
56   }
57 }
58
59 static inline void selinux_setfilecon(char *file, unsigned int mode) { 
60         if (is_selinux_running()) {
61                 security_context_t scontext=NULL;
62                 char *media;
63                 int ret=selinux_get_media(file, mode, &media);
64                 if ( ret== 0) {
65                         ret = matchmediacon(media, &scontext);
66                         free(media);
67                 } 
68                 if (ret==-1) 
69                         if (matchpathcon(file, mode, &scontext) < 0) {
70                                 dbg("matchpathcon(%s) failed\n", file);
71                                 return;
72                         } 
73                 if (setfilecon(file, scontext) < 0)
74                         dbg("setfiles %s failed with error '%s'",
75                             file, strerror(errno));
76                 freecon(scontext);
77         }
78 }
79
80 static inline void selinux_setfscreatecon(char *file, unsigned int mode) {
81         int retval = 0;
82         security_context_t scontext=NULL;
83
84         if (is_selinux_running()) {
85                 char *media;
86                 int ret=selinux_get_media(file, mode, &media);
87                 if ( ret== 0) {
88                         ret = matchmediacon(media, &scontext);
89                         free(media);
90                 } 
91
92                 if (ret==-1) 
93                         if (matchpathcon(file, mode, &scontext) < 0) {
94                                 dbg("matchpathcon(%s) failed\n", file);
95                                 return;
96                         } 
97
98                 retval=setfscreatecon(scontext);
99                 if (retval < 0)
100                         dbg("setfiles %s failed with error '%s'",
101                             file, strerror(errno));
102                 freecon(scontext);
103         }
104 }
105 static inline void selinux_init(void) {
106         /* record the present security context, for file-creation
107          * restoration creation purposes.
108          *
109          */
110
111         if (is_selinux_running())
112         {
113                 if (getfscreatecon(&prev_scontext) < 0) {
114                         dbg("getfscreatecon failed\n");
115                 }
116                 prev_scontext=NULL;
117         }
118 }
119 static inline void selinux_restore(void) {
120         if (is_selinux_running()) {
121                 /* reset the file create context to its former glory */
122                 if ( setfscreatecon(prev_scontext) < 0 )
123                         dbg("setfscreatecon failed\n");
124                 if (prev_scontext) {
125                         freecon(prev_scontext);
126                         prev_scontext=NULL;
127                 }
128         }
129 }
130 #endif /* USE_SELINUX */
131 #endif /* SELINUX_H */