chiark / gitweb /
bus: rework sd_bus_error APIs
[elogind.git] / src / core / smack-setup.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright (C) 2013 Intel Corporation
7   Authors:
8         Nathaniel Chen <nathaniel.chen@intel.com>
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published
12   by the Free Software Foundation; either version 2.1 of the License,
13   or (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <stdio.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <sys/vfs.h>
30 #include <fcntl.h>
31 #include <sys/types.h>
32 #include <dirent.h>
33 #include <sys/mount.h>
34 #include <stdint.h>
35
36 #include "macro.h"
37 #include "smack-setup.h"
38 #include "util.h"
39 #include "fileio.h"
40 #include "log.h"
41 #include "label.h"
42
43 #define SMACK_CONFIG "/etc/smack/accesses.d/"
44 #define CIPSO_CONFIG "/etc/smack/cipso.d/"
45
46 #ifdef HAVE_SMACK
47
48 static int write_rules(const char* dstpath, const char* srcdir) {
49         _cleanup_fclose_ FILE *dst = NULL;
50         _cleanup_closedir_ DIR *dir = NULL;
51         struct dirent *entry;
52         char buf[NAME_MAX];
53         int dfd = -1;
54         int r = 0;
55
56         dst = fopen(dstpath, "we");
57         if (!dst)  {
58                 if (errno != ENOENT)
59                         log_warning("Failed to open %s: %m", dstpath);
60                 return -errno; /* negative error */
61         }
62
63         /* write rules to dst from every file in the directory */
64         dir = opendir(srcdir);
65         if (!dir) {
66                 if (errno != ENOENT)
67                         log_warning("Failed to opendir %s: %m", srcdir);
68                 return errno; /* positive on purpose */
69         }
70
71         dfd = dirfd(dir);
72         assert(dfd >= 0);
73
74         FOREACH_DIRENT(entry, dir, return 0) {
75                 int fd;
76                 _cleanup_fclose_ FILE *policy = NULL;
77
78                 fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
79                 if (fd < 0) {
80                         if (r == 0)
81                                 r = -errno;
82                         log_warning("Failed to open %s: %m", entry->d_name);
83                         continue;
84                 }
85
86                 policy = fdopen(fd, "re");
87                 if (!policy) {
88                         if (r == 0)
89                                 r = -errno;
90                         close_nointr_nofail(fd);
91                         log_error("Failed to open %s: %m", entry->d_name);
92                         continue;
93                 }
94
95                 /* load2 write rules in the kernel require a line buffered stream */
96                 FOREACH_LINE(buf, policy,
97                              log_error("Failed to read line from %s: %m",
98                                        entry->d_name)) {
99                         if (!fputs(buf, dst)) {
100                                 if (r == 0)
101                                         r = -EINVAL;
102                                 log_error("Failed to write line to %s", dstpath);
103                                 break;
104                         }
105                         if (fflush(dst)) {
106                                 if (r == 0)
107                                         r = -errno;
108                                 log_error("Failed to flush writes to %s: %m", dstpath);
109                                 break;
110                         }
111                 }
112         }
113
114        return r;
115 }
116
117 #endif
118
119 int smack_setup(void) {
120
121 #ifdef HAVE_SMACK
122
123         int r;
124
125         r = write_rules("/sys/fs/smackfs/load2", SMACK_CONFIG);
126         switch(r) {
127         case -ENOENT:
128                 log_debug("Smack is not enabled in the kernel.");
129                 return 0;
130         case ENOENT:
131                 log_debug("Smack access rules directory " SMACK_CONFIG " not found");
132                 return 0;
133         case 0:
134                 log_info("Successfully loaded Smack policies.");
135                 break;
136         default:
137                 log_warning("Failed to load Smack access rules: %s, ignoring.",
138                             strerror(abs(r)));
139                 return 0;
140         }
141
142 #ifdef SMACK_RUN_LABEL
143         r = write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL);
144         if (r)
145                 log_warning("Failed to set SMACK label \"%s\" on self: %s",
146                             SMACK_RUN_LABEL, strerror(-r));
147 #endif
148
149         r = write_rules("/sys/fs/smackfs/cipso2", CIPSO_CONFIG);
150         switch(r) {
151         case -ENOENT:
152                 log_debug("Smack/CIPSO is not enabled in the kernel.");
153                 return 0;
154         case ENOENT:
155                 log_debug("Smack/CIPSO access rules directory " CIPSO_CONFIG " not found");
156                 return 0;
157         case 0:
158                 log_info("Successfully loaded Smack/CIPSO policies.");
159                 return 0;
160         default:
161                 log_warning("Failed to load Smack/CIPSO access rules: %s, ignoring.",
162                             strerror(abs(r)));
163                 return 0;
164         }
165
166 #endif
167
168         return 0;
169 }