chiark / gitweb /
Prep v236 : Add missing SPDX-License-Identifier (2/9) src/basic
[elogind.git] / src / basic / label.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2010 Lennart Poettering
6
7   systemd is free software; you can redistribute it and/or modify it
8   under the terms of the GNU Lesser General Public License as published by
9   the Free Software Foundation; either version 2.1 of the License, or
10   (at your option) any later version.
11
12   systemd is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public License
18   along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include "label.h"
26 #include "macro.h"
27 #include "selinux-util.h"
28 #include "smack-util.h"
29
30 int label_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
31         int r, q;
32
33         r = mac_selinux_fix(path, ignore_enoent, ignore_erofs);
34         q = mac_smack_fix(path, ignore_enoent, ignore_erofs);
35
36         if (r < 0)
37                 return r;
38         if (q < 0)
39                 return q;
40
41         return 0;
42 }
43
44 int mkdir_label(const char *path, mode_t mode) {
45         int r;
46
47         assert(path);
48
49         r = mac_selinux_create_file_prepare(path, S_IFDIR);
50         if (r < 0)
51                 return r;
52
53         if (mkdir(path, mode) < 0)
54                 r = -errno;
55
56         mac_selinux_create_file_clear();
57
58         if (r < 0)
59                 return r;
60
61         return mac_smack_fix(path, false, false);
62 }
63
64 #if 0 /// UNNEEDED by elogind
65 int symlink_label(const char *old_path, const char *new_path) {
66         int r;
67
68         assert(old_path);
69         assert(new_path);
70
71         r = mac_selinux_create_file_prepare(new_path, S_IFLNK);
72         if (r < 0)
73                 return r;
74
75         if (symlink(old_path, new_path) < 0)
76                 r = -errno;
77
78         mac_selinux_create_file_clear();
79
80         if (r < 0)
81                 return r;
82
83         return mac_smack_fix(new_path, false, false);
84 }
85 #endif // 0