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 DBUS_TYPE_STRING_AS_STRING;
187 array_sig = DBUS_TYPE_STRING_AS_STRING;
189 case DBUS_TYPE_OBJECT_PATH:
190 variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING;
191 array_sig = DBUS_TYPE_OBJECT_PATH_AS_STRING;
193 case DBUS_TYPE_DICT_ENTRY:
194 variant_sig = DBUS_TYPE_ARRAY_AS_STRING
195 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
196 DBUS_TYPE_ARRAY_AS_STRING
197 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
198 DBUS_TYPE_STRING_AS_STRING
199 DBUS_TYPE_VARIANT_AS_STRING
200 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
201 DBUS_STRUCT_END_CHAR_AS_STRING;
202 array_sig = DBUS_STRUCT_BEGIN_CHAR_AS_STRING
203 DBUS_TYPE_ARRAY_AS_STRING
204 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
205 DBUS_TYPE_STRING_AS_STRING
206 DBUS_TYPE_VARIANT_AS_STRING
207 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
208 DBUS_STRUCT_END_CHAR_AS_STRING;
214 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
216 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
217 variant_sig, &value);
219 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
222 function(&array, user_data);
223 dbus_message_iter_close_container(&value, &array);
225 dbus_message_iter_close_container(iter, &value);
228 static DBusConnection *connection = NULL;
230 dbus_bool_t connman_dbus_property_changed_basic(const char *path,
231 const char *interface, const char *key,
235 DBusMessageIter iter;
240 signal = dbus_message_new_signal(path, interface, "PropertyChanged");
244 dbus_message_iter_init_append(signal, &iter);
245 connman_dbus_property_append_basic(&iter, key, type, val);
247 g_dbus_send_message(connection, signal);
252 dbus_bool_t connman_dbus_property_changed_dict(const char *path,
253 const char *interface, const char *key,
254 connman_dbus_append_cb_t function, void *user_data)
257 DBusMessageIter iter;
262 signal = dbus_message_new_signal(path, interface, "PropertyChanged");
266 dbus_message_iter_init_append(signal, &iter);
267 connman_dbus_property_append_dict(&iter, key, function, user_data);
269 g_dbus_send_message(connection, signal);
274 dbus_bool_t connman_dbus_property_changed_array(const char *path,
275 const char *interface, const char *key, int type,
276 connman_dbus_append_cb_t function, void *user_data)
279 DBusMessageIter iter;
284 signal = dbus_message_new_signal(path, interface, "PropertyChanged");
288 dbus_message_iter_init_append(signal, &iter);
289 connman_dbus_property_append_array(&iter, key, type,
290 function, user_data);
292 g_dbus_send_message(connection, signal);
297 dbus_bool_t connman_dbus_setting_changed_basic(const char *owner,
298 const char *path, const char *key,
302 DBusMessageIter array, dict;
304 if (owner == NULL || path == NULL)
307 msg = dbus_message_new_method_call(owner, path,
308 CONNMAN_NOTIFICATION_INTERFACE,
313 dbus_message_iter_init_append(msg, &array);
314 connman_dbus_dict_open(&array, &dict);
316 connman_dbus_dict_append_basic(&dict, key, type, val);
318 connman_dbus_dict_close(&array, &dict);
320 g_dbus_send_message(connection, msg);
325 dbus_bool_t connman_dbus_setting_changed_dict(const char *owner,
326 const char *path, const char *key,
327 connman_dbus_append_cb_t function,
331 DBusMessageIter array, dict;
333 if (owner == NULL || path == NULL)
336 msg = dbus_message_new_method_call(owner, path,
337 CONNMAN_NOTIFICATION_INTERFACE,
342 dbus_message_iter_init_append(msg, &array);
343 connman_dbus_dict_open(&array, &dict);
345 connman_dbus_dict_append_dict(&dict, key, function, user_data);
347 connman_dbus_dict_close(&array, &dict);
349 g_dbus_send_message(connection, msg);
354 dbus_bool_t connman_dbus_setting_changed_array(const char *owner,
355 const char *path, const char *key, int type,
356 connman_dbus_append_cb_t function,
360 DBusMessageIter array, dict;
362 if (owner == NULL || path == NULL)
365 msg = dbus_message_new_method_call(owner, path,
366 CONNMAN_NOTIFICATION_INTERFACE,
371 dbus_message_iter_init_append(msg, &array);
372 connman_dbus_dict_open(&array, &dict);
374 connman_dbus_dict_append_array(&dict, key, type, function, user_data);
376 connman_dbus_dict_close(&array, &dict);
378 g_dbus_send_message(connection, msg);
383 dbus_bool_t __connman_dbus_append_objpath_dict_array(DBusMessage *msg,
384 connman_dbus_append_cb_t function, void *user_data)
386 DBusMessageIter iter, array;
388 if (msg == NULL || function == NULL)
391 dbus_message_iter_init_append(msg, &iter);
392 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
393 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
394 DBUS_TYPE_OBJECT_PATH_AS_STRING
395 DBUS_TYPE_ARRAY_AS_STRING
396 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
397 DBUS_TYPE_STRING_AS_STRING
398 DBUS_TYPE_VARIANT_AS_STRING
399 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
400 DBUS_STRUCT_END_CHAR_AS_STRING, &array);
402 function(&array, user_data);
404 dbus_message_iter_close_container(&iter, &array);
409 struct selinux_data {
410 connman_dbus_get_context_cb_t func;
414 static unsigned char *parse_context(DBusMessage *msg)
416 DBusMessageIter iter, array;
417 unsigned char *ctx, *p;
420 dbus_message_iter_init(msg, &iter);
421 dbus_message_iter_recurse(&iter, &array);
422 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_BYTE) {
425 dbus_message_iter_next(&array);
431 ctx = g_try_malloc0(size + 1);
437 dbus_message_iter_init(msg, &iter);
438 dbus_message_iter_recurse(&iter, &array);
439 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_BYTE) {
440 dbus_message_iter_get_basic(&array, p);
443 dbus_message_iter_next(&array);
449 static void selinux_get_context_reply(DBusPendingCall *call, void *user_data)
451 struct selinux_data *data = user_data;
453 unsigned char *context = NULL;
456 reply = dbus_pending_call_steal_reply(call);
458 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
459 DBG("Failed to retrieve SELinux context");
464 if (dbus_message_has_signature(reply, "ay") == FALSE) {
465 DBG("Message signature is wrong");
470 context = parse_context(reply);
473 (*data->func)(context, data->user_data, err);
477 dbus_message_unref(reply);
479 dbus_pending_call_unref(call);
482 int connman_dbus_get_selinux_context(DBusConnection *connection,
484 connman_dbus_get_context_cb_t func,
487 struct selinux_data *data;
488 DBusPendingCall *call;
489 DBusMessage *msg = NULL;
495 data = g_try_new0(struct selinux_data, 1);
497 DBG("Can't allocate data structure");
501 msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
503 "GetConnectionSELinuxSecurityContext");
505 DBG("Can't allocate new message");
510 dbus_message_append_args(msg, DBUS_TYPE_STRING, &service,
513 if (dbus_connection_send_with_reply(connection, msg,
514 &call, -1) == FALSE) {
515 DBG("Failed to execute method call");
521 DBG("D-Bus connection not available");
527 data->user_data = user_data;
529 dbus_pending_call_set_notify(call, selinux_get_context_reply,
532 dbus_message_unref(msg);
537 dbus_message_unref(msg);
543 DBusConnection *connman_dbus_get_connection(void)
545 if (connection == NULL)
548 return dbus_connection_ref(connection);
551 int __connman_dbus_init(DBusConnection *conn)
560 void __connman_dbus_cleanup(void)