| 1 |
#pragma once |
| 2 |
|
| 3 |
/* |
| 4 |
* D-Bus Policy Converter |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <c-list.h> |
| 8 |
#include <c-rbtree.h> |
| 9 |
#include <c-stdaux.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <systemd/sd-bus.h> |
| 12 |
#include "launch/config.h" |
| 13 |
|
| 14 |
typedef struct Policy Policy; |
| 15 |
typedef struct PolicyEntries PolicyEntries; |
| 16 |
typedef struct PolicyNode PolicyNode; |
| 17 |
typedef struct PolicyRecord PolicyRecord; |
| 18 |
|
| 19 |
#define POLICY_PRIORITY_DEFAULT (UINT64_C(1)) |
| 20 |
|
| 21 |
struct PolicyRecord { |
| 22 |
CList link; |
| 23 |
|
| 24 |
bool verdict; |
| 25 |
uint64_t priority; |
| 26 |
|
| 27 |
union { |
| 28 |
struct { |
| 29 |
bool prefix; |
| 30 |
const char *name; |
| 31 |
} own; |
| 32 |
|
| 33 |
struct { |
| 34 |
const char *name; |
| 35 |
const char *path; |
| 36 |
const char *interface; |
| 37 |
const char *member; |
| 38 |
unsigned int type; |
| 39 |
unsigned int broadcast; |
| 40 |
uint64_t min_fds; |
| 41 |
uint64_t max_fds; |
| 42 |
} xmit; |
| 43 |
|
| 44 |
struct { |
| 45 |
const char *name; |
| 46 |
const char *context; |
| 47 |
} selinux; |
| 48 |
}; |
| 49 |
}; |
| 50 |
|
| 51 |
#define POLICY_RECORD_INIT_CONNECT(_x) { \ |
| 52 |
.link = C_LIST_INIT((_x).link), \ |
| 53 |
} |
| 54 |
|
| 55 |
#define POLICY_RECORD_INIT_OWN(_x) { \ |
| 56 |
.link = C_LIST_INIT((_x).link), \ |
| 57 |
} |
| 58 |
|
| 59 |
#define POLICY_RECORD_INIT_XMIT(_x) { \ |
| 60 |
.link = C_LIST_INIT((_x).link), \ |
| 61 |
} |
| 62 |
|
| 63 |
#define POLICY_RECORD_INIT_SELINUX(_x) { \ |
| 64 |
.link = C_LIST_INIT((_x).link), \ |
| 65 |
} |
| 66 |
|
| 67 |
struct PolicyEntries { |
| 68 |
CList connect_list; |
| 69 |
CList own_list; |
| 70 |
CList send_list; |
| 71 |
CList recv_list; |
| 72 |
}; |
| 73 |
|
| 74 |
#define POLICY_ENTRIES_NULL(_x) { \ |
| 75 |
.connect_list = C_LIST_INIT((_x).connect_list), \ |
| 76 |
.own_list = C_LIST_INIT((_x).own_list), \ |
| 77 |
.send_list = C_LIST_INIT((_x).send_list), \ |
| 78 |
.recv_list = C_LIST_INIT((_x).recv_list), \ |
| 79 |
} |
| 80 |
|
| 81 |
struct PolicyNode { |
| 82 |
uint32_t uidgid; |
| 83 |
CRBNode policy_node; |
| 84 |
|
| 85 |
PolicyEntries entries; |
| 86 |
}; |
| 87 |
|
| 88 |
#define POLICY_NODE_NULL(_x) { \ |
| 89 |
.uidgid = -1, \ |
| 90 |
.policy_node = C_RBNODE_INIT((_x).policy_node), \ |
| 91 |
.entries = POLICY_ENTRIES_NULL((_x).entries), \ |
| 92 |
} |
| 93 |
|
| 94 |
struct Policy { |
| 95 |
uint64_t i_priority; |
| 96 |
|
| 97 |
PolicyEntries default_entries; |
| 98 |
PolicyEntries at_console_entries; |
| 99 |
PolicyEntries no_console_entries; |
| 100 |
|
| 101 |
CRBTree uid_tree; |
| 102 |
CRBTree gid_tree; |
| 103 |
|
| 104 |
CList selinux_list; |
| 105 |
unsigned int apparmor_mode; |
| 106 |
char *bus_type; |
| 107 |
}; |
| 108 |
|
| 109 |
#define POLICY_INIT(_x) { \ |
| 110 |
.i_priority = POLICY_PRIORITY_DEFAULT, \ |
| 111 |
.default_entries = POLICY_ENTRIES_NULL((_x).default_entries), \ |
| 112 |
.at_console_entries = POLICY_ENTRIES_NULL((_x).at_console_entries), \ |
| 113 |
.no_console_entries = POLICY_ENTRIES_NULL((_x).no_console_entries), \ |
| 114 |
.uid_tree = C_RBTREE_INIT, \ |
| 115 |
.gid_tree = C_RBTREE_INIT, \ |
| 116 |
.selinux_list = C_LIST_INIT((_x).selinux_list), \ |
| 117 |
.apparmor_mode = CONFIG_APPARMOR_ENABLED, \ |
| 118 |
.bus_type = NULL, \ |
| 119 |
} |
| 120 |
|
| 121 |
/* records */ |
| 122 |
|
| 123 |
int policy_record_new_connect(PolicyRecord **recordp); |
| 124 |
int policy_record_new_own(PolicyRecord **recordp); |
| 125 |
int policy_record_new_xmit(PolicyRecord **recordp); |
| 126 |
int policy_record_new_selinux(PolicyRecord **recordp); |
| 127 |
PolicyRecord *policy_record_free(PolicyRecord *record); |
| 128 |
|
| 129 |
C_DEFINE_CLEANUP(PolicyRecord *, policy_record_free); |
| 130 |
|
| 131 |
/* policy */ |
| 132 |
|
| 133 |
void policy_init(Policy *policy); |
| 134 |
void policy_deinit(Policy *policy); |
| 135 |
|
| 136 |
int policy_import(Policy *policy, ConfigRoot *root); |
| 137 |
void policy_optimize(Policy *policy); |
| 138 |
int policy_export(Policy *policy, sd_bus_message *m, uint32_t *at_console_uids, size_t n_at_console_uids); |
| 139 |
|
| 140 |
C_DEFINE_CLEANUP(Policy *, policy_deinit); |