summaryrefslogtreecommitdiff
path: root/src/internal/internal.cpp
blob: 84f25b1a93fa5937d13cf4bb40dc733ac684b93d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

#include <iostream>
#include <string>
#include <dbuspolicy1/libdbuspolicy1.h>
#include "xml_parser.hpp"
#include "policy.hpp"
#include "naive_policy_checker.hpp"
#include "internal.h"

#include "../libdbuspolicy1-private.h"

static ldp_xml_parser::NaivePolicyChecker policy_checker;

static const char* get_str(const char* const szstr) {
    return (szstr != NULL) ? szstr : "";
}

static const char**  get_strv(const char *s, const char** result) {
	int i = 0;
	unsigned k = 0;
	if (s) {
		while (s[i] && k < KDBUS_CONN_MAX_NAMES + 1) {
			char c;
			while ((c = s[i++]) && ' ' != c);
			result[k++] = s;
		    s += i;
			i = 0;
	    }
		if (k >= KDBUS_CONN_MAX_NAMES + 1)
			return NULL;
        if (k)
            result[k++] = NULL;
	}
	if (!k) {
		result[0] = "";
		result[1] = NULL;
	}
	return result;
}

int __internal_init(bool bus_type, const char* const config_name)
{
    ldp_xml_parser::XmlParser p;
	p.registerAdapter(policy_checker.generateAdapter());
    auto err = p.parsePolicy(bus_type, get_str(config_name));
    return err.get();
}

pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;

void __internal_init_once()
{
	tslog::init();
}

void __internal_init_flush_logs()
{
	if (tslog::enabled()) {
		pthread_mutex_lock(&g_mutex);
		std::cout << std::flush;
		pthread_mutex_unlock(&g_mutex);
	}
}

void __internal_enter()
{
	if (tslog::enabled())
		pthread_mutex_lock(&g_mutex);
}
void __internal_exit()
{
	if (tslog::enabled())
		pthread_mutex_unlock(&g_mutex);
}

int __internal_can_send(bool bus_type,
						const uid_t user,
						const gid_t group,
						const char* const label,
						const char* const destination,
						const char* const path,
						const char* const interface,
						const char* const member,
						int type)
{
    const char* names[KDBUS_CONN_MAX_NAMES+1];
    const char** ns = get_strv(destination, names);
	if (!ns) {
		if (tslog::verbose())
			std::cout << "Destination too long: "<<destination<<std::endl;
		return false;
	}
	return static_cast<int>(policy_checker.check(bus_type, user, group, label, ns, interface, member, path, static_cast<ldp_xml_parser::MessageType>(type), ldp_xml_parser::MessageDirection::SEND));
}

int __internal_can_send_multi_dest(bool bus_type,
						 const uid_t user,
						 const gid_t group,
						 const char* const label,
						 const char** const destination,
						 const char* const path,
						 const char* const interface,
						 const char* const member,
						 int type)
{
	return static_cast<int>(policy_checker.check(bus_type, user, group, label, destination, interface, member, path, static_cast<ldp_xml_parser::MessageType>(type), ldp_xml_parser::MessageDirection::SEND));
}

int __internal_can_recv(bool bus_type,
                            const uid_t user,
                            const gid_t group,
                            const char* const label,
                            const char* const sender,
                            const char* const path,
                            const char* const interface,
                            const char* const member,
                            int type)
{
    const char* names[KDBUS_CONN_MAX_NAMES+1];
    const char** ns = get_strv(sender, names);
	return static_cast<int>(policy_checker.check(bus_type, user, group, label, ns, interface, member, path, static_cast<ldp_xml_parser::MessageType>(type), ldp_xml_parser::MessageDirection::RECEIVE));
}

int __internal_can_own(bool bus_type,
                            const uid_t user,
                            const gid_t group,
                            const char* const label,
                            const char* const service)
{
	return static_cast<int>(policy_checker.check(bus_type, user, group, label, service));
}