1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <fcntl.h>
5 #include <stdbool.h>
6 #include <sys/socket.h>
7 #include <sys/types.h>
8 
9 #include "macro.h"
10 #include "label.h"
11 
12 #if HAVE_SELINUX
13 #include <selinux/selinux.h>
14 
15 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(char*, freecon, NULL);
16 #define _cleanup_freecon_ _cleanup_(freeconp)
17 #endif
18 
19 bool mac_selinux_use(void);
20 void mac_selinux_retest(void);
21 bool mac_selinux_enforcing(void);
22 
23 int mac_selinux_init(void);
24 void mac_selinux_maybe_reload(void);
25 void mac_selinux_finish(void);
26 
27 int mac_selinux_fix_container(const char *path, const char *inside_path, LabelFixFlags flags);
mac_selinux_fix(const char * path,LabelFixFlags flags)28 static inline int mac_selinux_fix(const char *path, LabelFixFlags flags) {
29         return mac_selinux_fix_container(path, path, flags);
30 }
31 
32 int mac_selinux_fix_container_fd(int fd, const char *path, const char *inside_path, LabelFixFlags flags);
mac_selinux_fix_fd(int fd,const char * path,LabelFixFlags flags)33 static inline int mac_selinux_fix_fd(int fd, const char *path, LabelFixFlags flags) {
34         return mac_selinux_fix_container_fd(fd, path, path, flags);
35 }
36 
37 int mac_selinux_apply(const char *path, const char *label);
38 int mac_selinux_apply_fd(int fd, const char *path, const char *label);
39 
40 int mac_selinux_get_create_label_from_exe(const char *exe, char **label);
41 int mac_selinux_get_our_label(char **label);
42 int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char *exec_label, char **label);
43 char* mac_selinux_free(char *label);
44 
45 int mac_selinux_create_file_prepare_at(int dirfd, const char *path, mode_t mode);
mac_selinux_create_file_prepare(const char * path,mode_t mode)46 static inline int mac_selinux_create_file_prepare(const char *path, mode_t mode) {
47         return mac_selinux_create_file_prepare_at(AT_FDCWD, path, mode);
48 }
49 int mac_selinux_create_file_prepare_label(const char *path, const char *label);
50 void mac_selinux_create_file_clear(void);
51 
52 int mac_selinux_create_socket_prepare(const char *label);
53 void mac_selinux_create_socket_clear(void);
54 
55 int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen);
56 
57 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, mac_selinux_free);
58