summaryrefslogtreecommitdiff
path: root/src/CommonAPI/DBus/DBusAddressTranslator.cpp
blob: 0c653dcf6796c13ad99d72621c3b747765319670 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (C) 2013-2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifdef WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif

#include <sys/stat.h>

#include <algorithm>

#include <CommonAPI/IniFileReader.hpp>
#include <CommonAPI/Logger.hpp>
#include <CommonAPI/Runtime.hpp>
#include <CommonAPI/DBus/DBusAddressTranslator.hpp>

namespace CommonAPI {
namespace DBus {

const char *COMMONAPI_DBUS_DEFAULT_CONFIG_FILE = "commonapi-dbus.ini";
const char *COMMONAPI_DBUS_DEFAULT_CONFIG_FOLDER = "/etc/";


std::shared_ptr<DBusAddressTranslator> DBusAddressTranslator::get() {
	static std::shared_ptr<DBusAddressTranslator> theTranslator = std::make_shared<DBusAddressTranslator>();
	return theTranslator;
}

DBusAddressTranslator::DBusAddressTranslator()
	: defaultDomain_("local"), orgFreedesktopDBusPeerMapped_(false) {
	init();

	isDefault_ = ("dbus" == Runtime::get()->getDefaultBinding());
}

void
DBusAddressTranslator::init() {
	// Determine default configuration file
	const char *config = getenv("COMMONAPI_DBUS_DEFAULT_CONFIG");
	if (config) {
		defaultConfig_ = config;
	} else {
		defaultConfig_ = COMMONAPI_DBUS_DEFAULT_CONFIG_FOLDER;
		defaultConfig_ += "/";
		defaultConfig_ += COMMONAPI_DBUS_DEFAULT_CONFIG_FILE;
	}

	(void)readConfiguration();
}

bool
DBusAddressTranslator::translate(const std::string &_key, DBusAddress &_value) {
	return translate(CommonAPI::Address(_key), _value);
}

bool
DBusAddressTranslator::translate(const CommonAPI::Address &_key, DBusAddress &_value) {
	bool result(true);
	std::lock_guard<std::mutex> itsLock(mutex_);

	const auto it = forwards_.find(_key);
	if (it != forwards_.end()) {
		_value = it->second;
	} else if (isDefault_) {
		std::string interfaceName(_key.getInterface());
		std::string objectPath("/" + _key.getInstance());
		std::replace(objectPath.begin(), objectPath.end(), '.', '/');
		std::string service(_key.getInterface() + "_" + _key.getInstance());

		if (isValid(service, '.', false, false, true)
		 && isValid(objectPath, '/', true)
		 && isValid(interfaceName, '.')) {
			_value.setInterface(interfaceName);
			_value.setObjectPath(objectPath);
			_value.setService(service);

			forwards_.insert({ _key, _value });
			backwards_.insert({ _value, _key });
		}
	} else {
		result = false;
	}

	return result;
}

bool
DBusAddressTranslator::translate(const DBusAddress &_key, std::string &_value) {
	CommonAPI::Address address;
	if (translate(_key, address)) {
		_value = address.getAddress();
		return true;
	}
	return false;
}

bool
DBusAddressTranslator::translate(const DBusAddress &_key, CommonAPI::Address &_value) {
	bool result(true);
	std::lock_guard<std::mutex> itsLock(mutex_);

	const auto it = backwards_.find(_key);
	if (it != backwards_.end()) {
		_value = it->second;
	} else if (isDefault_) {
		if (isValid(_key.getObjectPath(), '/', true) && isValid(_key.getInterface(), '.')) {
			std::string interfaceName(_key.getInterface());
			std::string instance(_key.getObjectPath().substr(1));
			std::replace(instance.begin(), instance.end(), '/', '.');

			_value.setDomain(defaultDomain_);
			_value.setInterface(interfaceName);
			_value.setInstance(instance);

			forwards_.insert({_value, _key});
			backwards_.insert({_key, _value});
		} else {
			result = false;
		}
	} else {
		result = false;
	}

	return result;
}


void
DBusAddressTranslator::insert(
		const std::string &_address,
		const std::string &_service, const std::string &_path, const std::string &_interface, const bool _objPathStartWithDigits) {

	if (isValid(_service, '.',
			(_service.length() > 0 && _service[0] == ':'),
			(_service.length() > 0 && _service[0] == ':'),
			true)
	  && isValid(_path, '/', true, _objPathStartWithDigits)
	  && isValid(_interface, '.')) {
		CommonAPI::Address address(_address);
		DBusAddress dbusAddress(_service, _path, _interface);

		std::lock_guard<std::mutex> itsLock(mutex_);
		auto fw = forwards_.find(address);
		auto bw = backwards_.find(dbusAddress);
		if (fw == forwards_.end() && bw == backwards_.end()) {
			forwards_[address] = dbusAddress;
			backwards_[dbusAddress] = address;
			COMMONAPI_DEBUG(
				"Added address mapping: ", address, " <--> ", dbusAddress);
			if (!orgFreedesktopDBusPeerMapped_) {
				orgFreedesktopDBusPeerMapped_ = (_interface == "org.freedesktop.DBus.Peer");
				if (orgFreedesktopDBusPeerMapped_) {
					COMMONAPI_DEBUG("org.freedesktop.DBus.Peer mapped");
				}
			}
		} else if(bw != backwards_.end() && bw->second != address) {
			COMMONAPI_ERROR("Trying to overwrite existing DBus address "
					"which is already mapped to a CommonAPI address: ",
					dbusAddress, " <--> ", _address);
		} else if(fw != forwards_.end() && fw->second != dbusAddress) {
			COMMONAPI_ERROR("Trying to overwrite existing CommonAPI address "
					"which is already mapped to a DBus address: ",
					_address, " <--> ", dbusAddress);
		}
	}
}

bool
DBusAddressTranslator::readConfiguration() {
#define MAX_PATH_LEN 255
	std::string config;
	char currentDirectory[MAX_PATH_LEN];
#ifdef WIN32
	if (GetCurrentDirectory(MAX_PATH_LEN, currentDirectory)) {
#else
	if (getcwd(currentDirectory, MAX_PATH_LEN)) {
#endif
		config = currentDirectory;
		config += "/";
		config += COMMONAPI_DBUS_DEFAULT_CONFIG_FILE;

		struct stat s;
		if (stat(config.c_str(), &s) != 0) {
			config = defaultConfig_;
		}
	}

	IniFileReader reader;
	if (!reader.load(config))
		return false;

	for (auto itsMapping : reader.getSections()) {
		if(itsMapping.first == "segments") {
		    std::map<std::string, std::string> mappings = itsMapping.second->getMappings();
		    ConnectionId_t connectionId;
		    std::string busType;
		    for(auto const &it : mappings) {
		        connectionId = it.first;
		        busType = it.second;
		        if(busType == "SESSION") {
		            dbusTypes_.insert({ connectionId, DBusType_t::SESSION });
		        } else if (busType == "SYSTEM") {
		            dbusTypes_.insert({ connectionId, DBusType_t::SYSTEM });
		        } else {
		            COMMONAPI_FATAL("Invalid bus type specified in .ini file, "
		                    "choose one of {SYSTEM, SESSION}");
		            continue;
		        }
		        COMMONAPI_INFO("D-Bus bus type for connection: " + connectionId +
		                " is set to: " + busType + " via ini file");
		    }
		    continue;
		}

		CommonAPI::Address itsAddress(itsMapping.first);

		std::string service = itsMapping.second->getValue("service");
		std::string path = itsMapping.second->getValue("path");
		std::string interfaceName = itsMapping.second->getValue("interface");

		insert(itsMapping.first, service, path, interfaceName);
	}

	return true;
}

bool
DBusAddressTranslator::isValid(
		const std::string &_name, const char _separator,
		bool _ignoreFirst, bool _isAllowedToStartWithDigit, bool _isBusName) const {
	// DBus addresses must contain at least one separator
	std::size_t separatorPos = _name.find(_separator);
	if (separatorPos == std::string::npos) {
		COMMONAPI_ERROR(
			"Invalid name \'", _name,
			"\'. Contains no \'", _separator, "\'");
		return false;
	}

	bool isInitial(true);
	std::size_t start(0);

	if (_ignoreFirst) {
		start = 1;
		if (separatorPos == 0) {
			// accept "root-only" i.e. '/' object path
			if (1 == _name.length()) {
				return true;
			}
			separatorPos = _name.find(_separator, separatorPos+1);
		}
	}

	while (start != std::string::npos) {
		// DBus names parts must not be empty
		std::string part;

		if (isInitial) {
			isInitial = false;
		} else {
			start++;
		}

		if (separatorPos == std::string::npos) {
			part = _name.substr(start);
		} else {
			part = _name.substr(start, separatorPos-start);
		}

		if ("" == part) {
			COMMONAPI_ERROR(
				"Invalid interface name \'", _name,
				"\'. Must not contain empty parts.");
			return false;
		}

		// DBus name parts must not start with a digit (not valid for unique names)
		if (!_isAllowedToStartWithDigit) {
			if (part[0] >= '0' && part[0] <= '9') {
				COMMONAPI_ERROR(
					"Invalid interface name \'", _name,
					"\'. First character must not be a digit.");
				return false;
			}
		}

		// DBus name parts consist of the ASCII characters [0-9][A-Z][a-z]_,
		for (auto c : part) {
			// bus names may additionally contain [-]
			if (_isBusName && c == '-')
				continue;

			if (c < '0' ||
				(c > '9' && c < 'A') ||
				(c > 'Z' && c < '_') ||
				(c > '_' && c < 'a') ||
				c > 'z') {
				COMMONAPI_ERROR(
					"Invalid interface name \'", _name,
					"\'. Contains illegal character \'", c,
					"\'. Only \'[0-9][A-Z][a-z]_\' are allowed.");
				return false;
			}
		}

		start = separatorPos;
		separatorPos = _name.find(_separator, separatorPos+1);
	}

	// DBus names must not exceed the maximum length
	if (_name.length() > DBUS_MAXIMUM_NAME_LENGTH) {
		COMMONAPI_ERROR(
			"Invalid interface name \'", _name,
			"\'. Size exceeds maximum size.");
		return false;
	}

	return true;
}

DBusType_t
DBusAddressTranslator::getDBusBusType(const ConnectionId_t &_connectionId) const {
	auto itsDbusTypesIterator = dbusTypes_.find(_connectionId);
	if(itsDbusTypesIterator != dbusTypes_.end()) {
	    return itsDbusTypesIterator->second;
	} else {
	    return DBusType_t::SESSION;
	}
}

bool DBusAddressTranslator::isOrgFreedesktopDBusPeerMapped() const {
	return orgFreedesktopDBusPeerMapped_;
}

} // namespace DBus
} // namespace CommonAPI