chiark / gitweb /
core: expose consumed CPU time per unit
[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 <stdlib.h>
28 #include <fcntl.h>
29 #include <dirent.h>
30
31 #include "macro.h"
32 #include "smack-setup.h"
33 #include "util.h"
34 #include "fileio.h"
35 #include "log.h"
36
37 #define SMACK_CONFIG "/etc/smack/accesses.d/"
38 #define CIPSO_CONFIG "/etc/smack/cipso.d/"
39
40 #ifdef HAVE_SMACK
41
42 static int write_rules(const char* dstpath, const char* srcdir) {
43         _cleanup_fclose_ FILE *dst = NULL;
44         _cleanup_closedir_ DIR *dir = NULL;
45         struct dirent *entry;
46         char buf[NAME_MAX];
47         int dfd = -1;
48         int r = 0;
49
50         dst = fopen(dstpath, "we");
51         if (!dst)  {
52                 if (errno != ENOENT)
53                         log_warning_errno(errno, "Failed to open %s: %m", dstpath);
54                 return -errno; /* negative error */
55         }
56
57         /* write rules to dst from every file in the directory */
58         dir = opendir(srcdir);
59         if (!dir) {
60                 if (errno != ENOENT)
61                         log_warning_errno(errno, "Failed to opendir %s: %m", srcdir);
62                 return errno; /* positive on purpose */
63         }
64
65         dfd = dirfd(dir);
66         assert(dfd >= 0);
67
68         FOREACH_DIRENT(entry, dir, return 0) {
69                 int fd;
70                 _cleanup_fclose_ FILE *policy = NULL;
71
72                 fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
73                 if (fd < 0) {
74                         if (r == 0)
75                                 r = -errno;
76                         log_warning_errno(errno, "Failed to open %s: %m", entry->d_name);
77                         continue;
78                 }
79
80                 policy = fdopen(fd, "re");
81                 if (!policy) {
82                         if (r == 0)
83                                 r = -errno;
84                         safe_close(fd);
85                         log_error_errno(errno, "Failed to open %s: %m", entry->d_name);
86                         continue;
87                 }
88
89                 /* load2 write rules in the kernel require a line buffered stream */
90                 FOREACH_LINE(buf, policy,
91                              log_error_errno(errno, "Failed to read line from %s: %m",
92                                        entry->d_name)) {
93                         if (!fputs(buf, dst)) {
94                                 if (r == 0)
95                                         r = -EINVAL;
96                                 log_error("Failed to write line to %s", dstpath);
97                                 break;
98                         }
99                         if (fflush(dst)) {
100                                 if (r == 0)
101                                         r = -errno;
102                                 log_error_errno(errno, "Failed to flush writes to %s: %m", dstpath);
103                                 break;
104                         }
105                 }
106         }
107
108        return r;
109 }
110
111 #endif
112
113 int mac_smack_setup(bool *loaded_policy) {
114
115 #ifdef HAVE_SMACK
116
117         int r;
118
119         assert(loaded_policy);
120
121         r = write_rules("/sys/fs/smackfs/load2", SMACK_CONFIG);
122         switch(r) {
123         case -ENOENT:
124                 log_debug("Smack is not enabled in the kernel.");
125                 return 0;
126         case ENOENT:
127                 log_debug("Smack access rules directory " SMACK_CONFIG " not found");
128                 return 0;
129         case 0:
130                 log_info("Successfully loaded Smack policies.");
131                 break;
132         default:
133                 log_warning("Failed to load Smack access rules: %s, ignoring.",
134                             strerror(abs(r)));
135                 return 0;
136         }
137
138 #ifdef SMACK_RUN_LABEL
139         r = write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL);
140         if (r)
141                 log_warning("Failed to set SMACK label \"%s\" on self: %s",
142                             SMACK_RUN_LABEL, strerror(-r));
143 #endif
144
145         r = write_rules("/sys/fs/smackfs/cipso2", CIPSO_CONFIG);
146         switch(r) {
147         case -ENOENT:
148                 log_debug("Smack/CIPSO is not enabled in the kernel.");
149                 return 0;
150         case ENOENT:
151                 log_debug("Smack/CIPSO access rules directory " CIPSO_CONFIG " not found");
152                 return 0;
153         case 0:
154                 log_info("Successfully loaded Smack/CIPSO policies.");
155                 break;
156         default:
157                 log_warning("Failed to load Smack/CIPSO access rules: %s, ignoring.",
158                             strerror(abs(r)));
159                 return 0;
160         }
161
162         *loaded_policy = true;
163
164 #endif
165
166         return 0;
167 }