chiark / gitweb /
Add function to open temp files in selinux mode
[elogind.git] / src / shared / smack-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Intel Corporation
7
8   Author: Auke Kok <auke-jan.h.kok@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 by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (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 <unistd.h>
25 #include <string.h>
26 #include <sys/xattr.h>
27
28 #include "smack-util.h"
29
30 bool use_smack(void) {
31 #ifdef HAVE_SMACK
32         static int use_smack_cached = -1;
33
34         if (use_smack_cached < 0)
35                 use_smack_cached = access("/sys/fs/smackfs/", F_OK) >= 0;
36
37         return use_smack_cached;
38 #else
39         return false;
40 #endif
41
42 }
43
44 int smack_label_path(const char *path, const char *label) {
45 #ifdef HAVE_SMACK
46         if (!use_smack())
47                 return 0;
48
49         if (label)
50                 return setxattr(path, "security.SMACK64", label, strlen(label), 0);
51         else
52                 return lremovexattr(path, "security.SMACK64");
53 #else
54         return 0;
55 #endif
56 }
57
58 int smack_label_fd(int fd, const char *label) {
59 #ifdef HAVE_SMACK
60         if (!use_smack())
61                 return 0;
62
63         return fsetxattr(fd, "security.SMACK64", label, strlen(label), 0);
64 #else
65         return 0;
66 #endif
67 }
68
69 int smack_label_ip_out_fd(int fd, const char *label) {
70 #ifdef HAVE_SMACK
71         if (!use_smack())
72                 return 0;
73
74         return fsetxattr(fd, "security.SMACK64IPOUT", label, strlen(label), 0);
75 #else
76         return 0;
77 #endif
78 }
79
80 int smack_label_ip_in_fd(int fd, const char *label) {
81 #ifdef HAVE_SMACK
82         if (!use_smack())
83                 return 0;
84
85         return fsetxattr(fd, "security.SMACK64IPIN", label, strlen(label), 0);
86 #else
87         return 0;
88 #endif
89 }