chiark / gitweb /
smack-setup: extract rule writing into a separate function
[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 "log.h"
40 #include "label.h"
41
42 #define SMACK_CONFIG "/etc/smack/accesses.d/"
43
44 static int write_rules(const char* dstpath, const char* srcdir) {
45         _cleanup_fclose_ FILE *dst = NULL;
46         _cleanup_closedir_ DIR *dir = NULL;
47         struct dirent *entry;
48         char buf[NAME_MAX];
49         int dfd = -1;
50         int r = 0;
51
52         dst = fopen(dstpath, "we");
53         if (!dst)  {
54                 if (errno != ENOENT)
55                         log_warning("Failed to open %s: %m", dstpath);
56                 return -errno; /* negative error */
57         }
58
59         /* write rules to dst from every file in the directory */
60         dir = opendir(srcdir);
61         if (!dir) {
62                 if (errno != ENOENT)
63                         log_warning("Failed to opendir %s: %m", srcdir);
64                 return errno; /* positive on purpose */
65         }
66
67         dfd = dirfd(dir);
68         assert(dfd >= 0);
69
70         FOREACH_DIRENT(entry, dir, return 0) {
71                 int fd;
72                 _cleanup_fclose_ FILE *policy = NULL;
73
74                 fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
75                 if (fd < 0) {
76                         if (r == 0)
77                                 r = -errno;
78                         log_warning("Failed to open %s: %m", entry->d_name);
79                         continue;
80                 }
81
82                 policy = fdopen(fd, "re");
83                 if (!policy) {
84                         if (r == 0)
85                                 r = -errno;
86                         close_nointr_nofail(fd);
87                         log_error("Failed to open %s: %m", entry->d_name);
88                         continue;
89                 }
90
91                 /* load2 write rules in the kernel require a line buffered stream */
92                 FOREACH_LINE(buf, policy,
93                              log_error("Failed to read line from %s: %m",
94                                        entry->d_name)) {
95                         if (!fputs(buf, dst)) {
96                                 if (r == 0)
97                                         r = -EINVAL;
98                                 log_error("Failed to write line to %s", dstpath);
99                                 break;
100                         }
101                         if (fflush(dst)) {
102                                 if (r == 0)
103                                         r = -errno;
104                                 log_error("Failed to flush writes to %s: %m", dstpath);
105                                 break;
106                         }
107                 }
108         }
109
110        return r;
111 }
112
113
114 int smack_setup(void) {
115         int r;
116
117         r = write_rules("/sys/fs/smackfs/load2", SMACK_CONFIG);
118         switch(r) {
119         case -ENOENT:
120                 log_debug("Smack is not enabled in the kernel.");
121                 return 0;
122         case ENOENT:
123                 log_debug("Smack access rules directory " SMACK_CONFIG " not found");
124                 return 0;
125         case 0:
126                 log_info("Successfully loaded Smack policies.");
127                 return 0;
128         default:
129                 log_warning("Failed to load smack access rules: %s, ignoring.",
130                             strerror(abs(r)));
131                 return 0;
132         }
133 }