]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/smack-setup.c
bus: add APIs for negotiating what is attached to messages
[thirdparty/systemd.git] / src / core / smack-setup.c
CommitLineData
ffbd2c4d
NC
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
a4783bd1 42#define SMACK_CONFIG "/etc/smack/accesses.d/"
abbacb1d 43#define CIPSO_CONFIG "/etc/smack/cipso/"
ffbd2c4d 44
a4783bd1
ZJS
45static int write_rules(const char* dstpath, const char* srcdir) {
46 _cleanup_fclose_ FILE *dst = NULL;
ffbd2c4d
NC
47 _cleanup_closedir_ DIR *dir = NULL;
48 struct dirent *entry;
49 char buf[NAME_MAX];
50 int dfd = -1;
a4783bd1 51 int r = 0;
ffbd2c4d 52
a4783bd1
ZJS
53 dst = fopen(dstpath, "we");
54 if (!dst) {
55 if (errno != ENOENT)
56 log_warning("Failed to open %s: %m", dstpath);
57 return -errno; /* negative error */
ffbd2c4d
NC
58 }
59
a4783bd1
ZJS
60 /* write rules to dst from every file in the directory */
61 dir = opendir(srcdir);
ffbd2c4d 62 if (!dir) {
a4783bd1
ZJS
63 if (errno != ENOENT)
64 log_warning("Failed to opendir %s: %m", srcdir);
65 return errno; /* positive on purpose */
ffbd2c4d
NC
66 }
67
68 dfd = dirfd(dir);
fea7838e 69 assert(dfd >= 0);
ffbd2c4d
NC
70
71 FOREACH_DIRENT(entry, dir, return 0) {
a4783bd1 72 int fd;
ffbd2c4d 73 _cleanup_fclose_ FILE *policy = NULL;
ffbd2c4d 74
a4783bd1
ZJS
75 fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
76 if (fd < 0) {
77 if (r == 0)
78 r = -errno;
79 log_warning("Failed to open %s: %m", entry->d_name);
ffbd2c4d
NC
80 continue;
81 }
82
a4783bd1 83 policy = fdopen(fd, "re");
ffbd2c4d 84 if (!policy) {
a4783bd1
ZJS
85 if (r == 0)
86 r = -errno;
87 close_nointr_nofail(fd);
88 log_error("Failed to open %s: %m", entry->d_name);
ffbd2c4d
NC
89 continue;
90 }
91
ffbd2c4d 92 /* load2 write rules in the kernel require a line buffered stream */
fea7838e 93 FOREACH_LINE(buf, policy,
a4783bd1 94 log_error("Failed to read line from %s: %m",
fea7838e 95 entry->d_name)) {
a4783bd1
ZJS
96 if (!fputs(buf, dst)) {
97 if (r == 0)
98 r = -EINVAL;
99 log_error("Failed to write line to %s", dstpath);
100 break;
101 }
102 if (fflush(dst)) {
103 if (r == 0)
104 r = -errno;
105 log_error("Failed to flush writes to %s: %m", dstpath);
106 break;
107 }
ffbd2c4d
NC
108 }
109 }
110
a4783bd1
ZJS
111 return r;
112}
113
ffbd2c4d 114
a4783bd1
ZJS
115int smack_setup(void) {
116 int r;
117
118 r = write_rules("/sys/fs/smackfs/load2", SMACK_CONFIG);
119 switch(r) {
120 case -ENOENT:
121 log_debug("Smack is not enabled in the kernel.");
122 return 0;
123 case ENOENT:
124 log_debug("Smack access rules directory " SMACK_CONFIG " not found");
125 return 0;
126 case 0:
127 log_info("Successfully loaded Smack policies.");
abbacb1d
NC
128 break;
129 default:
130 log_warning("Failed to load Smack access rules: %s, ignoring.",
131 strerror(abs(r)));
132 return 0;
133 }
134
135 r = write_rules("/sys/fs/smackfs/cipso2", CIPSO_CONFIG);
136 switch(r) {
137 case -ENOENT:
138 log_debug("Smack/CIPSO is not enabled in the kernel.");
139 return 0;
140 case ENOENT:
141 log_debug("Smack/CIPSO access rules directory " CIPSO_CONFIG " not found");
142 return 0;
143 case 0:
144 log_info("Successfully loaded Smack/CIPSO policies.");
a4783bd1
ZJS
145 return 0;
146 default:
abbacb1d 147 log_warning("Failed to load Smack/CIPSO access rules: %s, ignoring.",
a4783bd1
ZJS
148 strerror(abs(r)));
149 return 0;
150 }
ffbd2c4d 151}