]> git.proxmox.com Git - systemd.git/blame - src/basic/label.c
New upstream version 236
[systemd.git] / src / basic / label.c
CommitLineData
52ad194e 1/* SPDX-License-Identifier: LGPL-2.1+ */
663996b3
MS
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
4c89c718
MP
21#include <errno.h>
22#include <sys/stat.h>
23#include <unistd.h>
24
db2df898 25#include "label.h"
4c89c718 26#include "macro.h"
e735f4d4
MP
27#include "selinux-util.h"
28#include "smack-util.h"
663996b3 29
60f067b4 30int label_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
5eef597e 31 int r, q;
663996b3 32
5eef597e
MP
33 r = mac_selinux_fix(path, ignore_enoent, ignore_erofs);
34 q = mac_smack_fix(path, ignore_enoent, ignore_erofs);
663996b3 35
663996b3 36 if (r < 0)
5eef597e
MP
37 return r;
38 if (q < 0)
39 return q;
663996b3
MS
40
41 return 0;
42}
43
5eef597e
MP
44int mkdir_label(const char *path, mode_t mode) {
45 int r;
663996b3 46
5eef597e 47 assert(path);
663996b3 48
5eef597e 49 r = mac_selinux_create_file_prepare(path, S_IFDIR);
663996b3 50 if (r < 0)
5eef597e 51 return r;
663996b3 52
5eef597e
MP
53 if (mkdir(path, mode) < 0)
54 r = -errno;
60f067b4 55
5eef597e 56 mac_selinux_create_file_clear();
60f067b4 57
5eef597e
MP
58 if (r < 0)
59 return r;
60f067b4 60
5eef597e 61 return mac_smack_fix(path, false, false);
663996b3
MS
62}
63
5eef597e 64int symlink_label(const char *old_path, const char *new_path) {
60f067b4 65 int r;
663996b3 66
5eef597e
MP
67 assert(old_path);
68 assert(new_path);
663996b3 69
5eef597e 70 r = mac_selinux_create_file_prepare(new_path, S_IFLNK);
663996b3 71 if (r < 0)
5eef597e 72 return r;
663996b3 73
5eef597e
MP
74 if (symlink(old_path, new_path) < 0)
75 r = -errno;
60f067b4 76
5eef597e 77 mac_selinux_create_file_clear();
60f067b4 78
5eef597e
MP
79 if (r < 0)
80 return r;
60f067b4 81
5eef597e 82 return mac_smack_fix(new_path, false, false);
60f067b4 83}