5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 dbus_bool_t connman_dbus_validate_ident(const char *ident)
39 for (i = 0; i < strlen(ident); i++) {
40 if (ident[i] >= '0' && ident[i] <= '9')
42 if (ident[i] >= 'a' && ident[i] <= 'z')
44 if (ident[i] >= 'A' && ident[i] <= 'Z')
52 char *connman_dbus_encode_string(const char *value)
62 str = g_string_new(NULL);
66 for (i = 0; i < size; i++) {
67 const char tmp = value[i];
68 if ((tmp < '0' || tmp > '9') && (tmp < 'A' || tmp > 'Z') &&
69 (tmp < 'a' || tmp > 'z'))
70 g_string_append_printf(str, "_%02x", tmp);
72 str = g_string_append_c(str, tmp);
75 return g_string_free(str, FALSE);
78 void connman_dbus_property_append_basic(DBusMessageIter *iter,
79 const char *key, int type, void *val)
81 DBusMessageIter value;
82 const char *signature;
84 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
87 case DBUS_TYPE_BOOLEAN:
88 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
90 case DBUS_TYPE_STRING:
91 signature = DBUS_TYPE_STRING_AS_STRING;
94 signature = DBUS_TYPE_BYTE_AS_STRING;
96 case DBUS_TYPE_UINT16:
97 signature = DBUS_TYPE_UINT16_AS_STRING;
100 signature = DBUS_TYPE_INT16_AS_STRING;
102 case DBUS_TYPE_UINT32:
103 signature = DBUS_TYPE_UINT32_AS_STRING;
105 case DBUS_TYPE_INT32:
106 signature = DBUS_TYPE_INT32_AS_STRING;
108 case DBUS_TYPE_UINT64:
109 signature = DBUS_TYPE_UINT64_AS_STRING;
111 case DBUS_TYPE_INT64:
112 signature = DBUS_TYPE_INT64_AS_STRING;
114 case DBUS_TYPE_OBJECT_PATH:
115 signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
118 signature = DBUS_TYPE_VARIANT_AS_STRING;
122 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
124 dbus_message_iter_append_basic(&value, type, val);
125 dbus_message_iter_close_container(iter, &value);
128 void connman_dbus_property_append_dict(DBusMessageIter *iter, const char *key,
129 connman_dbus_append_cb_t function, void *user_data)
131 DBusMessageIter value, dict;
133 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
135 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
136 DBUS_TYPE_ARRAY_AS_STRING
137 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
138 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
139 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &value);
141 connman_dbus_dict_open(&value, &dict);
143 function(&dict, user_data);
144 connman_dbus_dict_close(&value, &dict);
146 dbus_message_iter_close_container(iter, &value);
149 void connman_dbus_property_append_fixed_array(DBusMessageIter *iter,
150 const char *key, int type, void *val, int len)
152 DBusMessageIter value, array;
153 const char *variant_sig, *array_sig;
157 variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING;
158 array_sig = DBUS_TYPE_BYTE_AS_STRING;
164 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
166 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
167 variant_sig, &value);
169 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
171 dbus_message_iter_append_fixed_array(&array, type, val, len);
172 dbus_message_iter_close_container(&value, &array);
174 dbus_message_iter_close_container(iter, &value);
177 void connman_dbus_property_append_array(DBusMessageIter *iter,
178 const char *key, int type,
179 connman_dbus_append_cb_t function, void *user_data)
181 DBusMessageIter value, array;
182 const char *variant_sig, *array_sig;
185 case DBUS_TYPE_STRING:
186 variant_sig = DBUS_TYPE_ARRAY_AS_STRING
187 DBUS_TYPE_STRING_AS_STRING;
188 array_sig = DBUS_TYPE_STRING_AS_STRING;
190 case DBUS_TYPE_OBJECT_PATH:
191 variant_sig = DBUS_TYPE_ARRAY_AS_STRING
192 DBUS_TYPE_OBJECT_PATH_AS_STRING;
193 array_sig = DBUS_TYPE_OBJECT_PATH_AS_STRING;
195 case DBUS_TYPE_DICT_ENTRY:
196 variant_sig = DBUS_TYPE_ARRAY_AS_STRING
197 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
198 DBUS_TYPE_ARRAY_AS_STRING
199 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
200 DBUS_TYPE_STRING_AS_STRING
201 DBUS_TYPE_VARIANT_AS_STRING
202 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
203 DBUS_STRUCT_END_CHAR_AS_STRING;
204 array_sig = DBUS_STRUCT_BEGIN_CHAR_AS_STRING
205 DBUS_TYPE_ARRAY_AS_STRING
206 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
207 DBUS_TYPE_STRING_AS_STRING
208 DBUS_TYPE_VARIANT_AS_STRING
209 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
210 DBUS_STRUCT_END_CHAR_AS_STRING;
216 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
218 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
219 variant_sig, &value);
221 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
224 function(&array, user_data);
225 dbus_message_iter_close_container(&value, &array);
227 dbus_message_iter_close_container(iter, &value);
230 static DBusConnection *connection = NULL;
232 dbus_bool_t connman_dbus_property_changed_basic(const char *path,
233 const char *interface, const char *key,
237 DBusMessageIter iter;
242 signal = dbus_message_new_signal(path, interface, "PropertyChanged");
246 dbus_message_iter_init_append(signal, &iter);
247 connman_dbus_property_append_basic(&iter, key, type, val);
249 g_dbus_send_message(connection, signal);
254 dbus_bool_t connman_dbus_property_changed_dict(const char *path,
255 const char *interface, const char *key,
256 connman_dbus_append_cb_t function, void *user_data)
259 DBusMessageIter iter;
264 signal = dbus_message_new_signal(path, interface, "PropertyChanged");
268 dbus_message_iter_init_append(signal, &iter);
269 connman_dbus_property_append_dict(&iter, key, function, user_data);
271 g_dbus_send_message(connection, signal);
276 dbus_bool_t connman_dbus_property_changed_array(const char *path,
277 const char *interface, const char *key, int type,
278 connman_dbus_append_cb_t function, void *user_data)
281 DBusMessageIter iter;
286 signal = dbus_message_new_signal(path, interface, "PropertyChanged");
290 dbus_message_iter_init_append(signal, &iter);
291 connman_dbus_property_append_array(&iter, key, type,
292 function, user_data);
294 g_dbus_send_message(connection, signal);
299 dbus_bool_t connman_dbus_setting_changed_basic(const char *owner,
300 const char *path, const char *key,
304 DBusMessageIter array, dict;
309 msg = dbus_message_new_method_call(owner, path,
310 CONNMAN_NOTIFICATION_INTERFACE,
315 dbus_message_iter_init_append(msg, &array);
316 connman_dbus_dict_open(&array, &dict);
318 connman_dbus_dict_append_basic(&dict, key, type, val);
320 connman_dbus_dict_close(&array, &dict);
322 g_dbus_send_message(connection, msg);
327 dbus_bool_t connman_dbus_setting_changed_dict(const char *owner,
328 const char *path, const char *key,
329 connman_dbus_append_cb_t function,
333 DBusMessageIter array, dict;
338 msg = dbus_message_new_method_call(owner, path,
339 CONNMAN_NOTIFICATION_INTERFACE,
344 dbus_message_iter_init_append(msg, &array);
345 connman_dbus_dict_open(&array, &dict);
347 connman_dbus_dict_append_dict(&dict, key, function, user_data);
349 connman_dbus_dict_close(&array, &dict);
351 g_dbus_send_message(connection, msg);
356 dbus_bool_t connman_dbus_setting_changed_array(const char *owner,
357 const char *path, const char *key, int type,
358 connman_dbus_append_cb_t function,
362 DBusMessageIter array, dict;
367 msg = dbus_message_new_method_call(owner, path,
368 CONNMAN_NOTIFICATION_INTERFACE,
373 dbus_message_iter_init_append(msg, &array);
374 connman_dbus_dict_open(&array, &dict);
376 connman_dbus_dict_append_array(&dict, key, type, function, user_data);
378 connman_dbus_dict_close(&array, &dict);
380 g_dbus_send_message(connection, msg);
385 dbus_bool_t __connman_dbus_append_objpath_dict_array(DBusMessage *msg,
386 connman_dbus_append_cb_t function, void *user_data)
388 DBusMessageIter iter, array;
390 if (!msg || !function)
393 dbus_message_iter_init_append(msg, &iter);
394 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
395 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
396 DBUS_TYPE_OBJECT_PATH_AS_STRING
397 DBUS_TYPE_ARRAY_AS_STRING
398 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
399 DBUS_TYPE_STRING_AS_STRING
400 DBUS_TYPE_VARIANT_AS_STRING
401 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
402 DBUS_STRUCT_END_CHAR_AS_STRING, &array);
404 function(&array, user_data);
406 dbus_message_iter_close_container(&iter, &array);
411 dbus_bool_t __connman_dbus_append_objpath_array(DBusMessage *msg,
412 connman_dbus_append_cb_t function, void *user_data)
414 DBusMessageIter iter, array;
416 if (!msg || !function)
419 dbus_message_iter_init_append(msg, &iter);
420 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
421 DBUS_TYPE_OBJECT_PATH_AS_STRING, &array);
423 function(&array, user_data);
425 dbus_message_iter_close_container(&iter, &array);
430 struct callback_data {
435 static void get_connection_unix_user_reply(DBusPendingCall *call,
438 struct callback_data *data = user_data;
439 connman_dbus_get_connection_unix_user_cb_t cb = data->cb;
440 DBusMessageIter iter;
443 unsigned int uid = 0;
445 reply = dbus_pending_call_steal_reply(call);
447 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
448 DBG("Failed to retrieve UID");
453 if (!dbus_message_has_signature(reply, "u")) {
454 DBG("Message signature is wrong");
459 dbus_message_iter_init(reply, &iter);
460 dbus_message_iter_get_basic(&iter, &uid);
463 (*cb)(uid, data->user_data, err);
465 dbus_message_unref(reply);
467 dbus_pending_call_unref(call);
470 int connman_dbus_get_connection_unix_user(DBusConnection *connection,
471 const char *bus_name,
472 connman_dbus_get_connection_unix_user_cb_t func,
475 struct callback_data *data;
476 DBusPendingCall *call;
477 DBusMessage *msg = NULL;
480 data = g_try_new0(struct callback_data, 1);
482 DBG("Can't allocate data structure");
486 msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
488 "GetConnectionUnixUser");
490 DBG("Can't allocate new message");
495 dbus_message_append_args(msg, DBUS_TYPE_STRING, &bus_name,
498 if (!dbus_connection_send_with_reply(connection, msg, &call, -1)) {
499 DBG("Failed to execute method call");
505 DBG("D-Bus connection not available");
511 data->user_data = user_data;
513 dbus_pending_call_set_notify(call, get_connection_unix_user_reply,
516 dbus_message_unref(msg);
521 dbus_message_unref(msg);
527 static unsigned char *parse_context(DBusMessage *msg)
529 DBusMessageIter iter, array;
530 unsigned char *ctx, *p;
533 dbus_message_iter_init(msg, &iter);
534 dbus_message_iter_recurse(&iter, &array);
535 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_BYTE) {
538 dbus_message_iter_next(&array);
544 ctx = g_try_malloc0(size + 1);
550 dbus_message_iter_init(msg, &iter);
551 dbus_message_iter_recurse(&iter, &array);
552 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_BYTE) {
553 dbus_message_iter_get_basic(&array, p);
556 dbus_message_iter_next(&array);
562 static void selinux_get_context_reply(DBusPendingCall *call, void *user_data)
564 struct callback_data *data = user_data;
565 connman_dbus_get_context_cb_t cb = data->cb;
567 unsigned char *context = NULL;
570 reply = dbus_pending_call_steal_reply(call);
572 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
573 DBG("Failed to retrieve SELinux context");
578 if (!dbus_message_has_signature(reply, "ay")) {
579 DBG("Message signature is wrong");
584 context = parse_context(reply);
587 (*cb)(context, data->user_data, err);
591 dbus_message_unref(reply);
593 dbus_pending_call_unref(call);
596 int connman_dbus_get_selinux_context(DBusConnection *connection,
598 connman_dbus_get_context_cb_t func,
601 struct callback_data *data;
602 DBusPendingCall *call;
603 DBusMessage *msg = NULL;
609 data = g_try_new0(struct callback_data, 1);
611 DBG("Can't allocate data structure");
615 msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
617 "GetConnectionSELinuxSecurityContext");
619 DBG("Can't allocate new message");
624 dbus_message_append_args(msg, DBUS_TYPE_STRING, &service,
627 if (!dbus_connection_send_with_reply(connection, msg, &call, -1)) {
628 DBG("Failed to execute method call");
634 DBG("D-Bus connection not available");
640 data->user_data = user_data;
642 dbus_pending_call_set_notify(call, selinux_get_context_reply,
645 dbus_message_unref(msg);
650 dbus_message_unref(msg);
656 void connman_dbus_reply_pending(DBusMessage *pending,
657 int error, const char *path)
663 reply = __connman_error_failed(pending, error);
665 g_dbus_send_message(connection, reply);
669 sender = dbus_message_get_interface(pending);
671 path = dbus_message_get_path(pending);
673 DBG("sender %s path %s", sender, path);
675 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
676 g_dbus_send_reply(connection, pending,
677 DBUS_TYPE_OBJECT_PATH, &path,
680 g_dbus_send_reply(connection, pending,
684 dbus_message_unref(pending);
688 DBusConnection *connman_dbus_get_connection(void)
693 return dbus_connection_ref(connection);
696 int __connman_dbus_init(DBusConnection *conn)
705 void __connman_dbus_cleanup(void)