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
34 static DBusConnection *connection = NULL;
35 static guint agent_watch = 0;
36 static gchar *agent_path = NULL;
37 static gchar *agent_sender = NULL;
39 static void agent_free(void)
50 static void agent_disconnect(DBusConnection *connection, void *data)
57 int __connman_agent_register(const char *sender, const char *path)
59 DBG("sender %s path %s", sender, path);
61 if (agent_path != NULL)
64 agent_sender = g_strdup(sender);
65 agent_path = g_strdup(path);
67 agent_watch = g_dbus_add_disconnect_watch(connection, sender,
68 agent_disconnect, NULL, NULL);
73 int __connman_agent_unregister(const char *sender, const char *path)
75 DBG("sender %s path %s", sender, path);
77 if (agent_path == NULL)
81 g_dbus_remove_watch(connection, agent_watch);
88 static connman_bool_t check_reply_has_dict(DBusMessage *reply)
90 const char *signature = DBUS_TYPE_ARRAY_AS_STRING
91 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
92 DBUS_TYPE_STRING_AS_STRING
93 DBUS_TYPE_VARIANT_AS_STRING
94 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
96 if (dbus_message_has_signature(reply, signature) == TRUE)
99 connman_warn("Reply %s to %s from %s has wrong signature %s",
101 dbus_message_get_interface(reply),
102 dbus_message_get_sender(reply),
103 dbus_message_get_signature(reply));
108 struct request_input_reply {
109 struct connman_service *service;
110 authentication_cb_t callback;
114 static void request_input_passphrase_reply(DBusPendingCall *call, void *user_data)
116 struct request_input_reply *passphrase_reply = user_data;
117 connman_bool_t values_received = FALSE;
118 connman_bool_t wps = FALSE;
119 const char *error = NULL;
120 char *identity = NULL;
121 char *passphrase = NULL;
126 DBusMessageIter iter, dict;
127 DBusMessage *reply = dbus_pending_call_steal_reply(call);
129 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
130 error = dbus_message_get_error_name(reply);
134 if (check_reply_has_dict(reply) == FALSE)
137 values_received = TRUE;
139 dbus_message_iter_init(reply, &iter);
140 dbus_message_iter_recurse(&iter, &dict);
141 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
142 DBusMessageIter entry, value;
144 dbus_message_iter_recurse(&dict, &entry);
145 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
148 dbus_message_iter_get_basic(&entry, &key);
150 if (g_str_equal(key, "Identity")) {
151 dbus_message_iter_next(&entry);
152 if (dbus_message_iter_get_arg_type(&entry)
153 != DBUS_TYPE_VARIANT)
155 dbus_message_iter_recurse(&entry, &value);
156 dbus_message_iter_get_basic(&value, &identity);
158 } else if (g_str_equal(key, "Passphrase")) {
159 dbus_message_iter_next(&entry);
160 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
162 dbus_message_iter_recurse(&entry, &value);
163 dbus_message_iter_get_basic(&value, &passphrase);
165 } else if (g_str_equal(key, "WPS")) {
168 dbus_message_iter_next(&entry);
169 if (dbus_message_iter_get_arg_type(&entry)
170 != DBUS_TYPE_VARIANT)
172 dbus_message_iter_recurse(&entry, &value);
173 dbus_message_iter_get_basic(&value, &wpspin);
175 } else if (g_str_equal(key, "Name")) {
176 dbus_message_iter_next(&entry);
177 if (dbus_message_iter_get_arg_type(&entry)
178 != DBUS_TYPE_VARIANT)
180 dbus_message_iter_recurse(&entry, &value);
181 dbus_message_iter_get_basic(&value, &name);
182 name_len = strlen(name);
183 } else if (g_str_equal(key, "SSID")) {
184 dbus_message_iter_next(&entry);
185 if (dbus_message_iter_get_arg_type(&entry)
186 != DBUS_TYPE_VARIANT)
188 dbus_message_iter_recurse(&entry, &value);
189 if (dbus_message_iter_get_arg_type(&value)
190 != DBUS_TYPE_VARIANT)
192 if (dbus_message_iter_get_element_type(&value)
193 != DBUS_TYPE_VARIANT)
195 dbus_message_iter_get_fixed_array(&value, &name,
198 dbus_message_iter_next(&dict);
202 passphrase_reply->callback(passphrase_reply->service, values_received,
204 identity, passphrase,
206 passphrase_reply->user_data);
207 connman_service_unref(passphrase_reply->service);
208 dbus_message_unref(reply);
209 dbus_pending_call_unref(call);
210 g_free(passphrase_reply);
213 static void request_input_append_alternates(DBusMessageIter *iter,
216 const char *str = user_data;
217 char **alternates, **alternative;
222 alternates = g_strsplit(str, ",", 0);
223 if (alternates == NULL)
226 for (alternative = alternates; *alternative != NULL; alternative++)
227 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
230 g_strfreev(alternates);
233 static void request_input_append_identity(DBusMessageIter *iter,
236 char *str = "string";
238 connman_dbus_dict_append_basic(iter, "Type",
239 DBUS_TYPE_STRING, &str);
241 connman_dbus_dict_append_basic(iter, "Requirement",
242 DBUS_TYPE_STRING, &str);
245 static void request_input_append_passphrase(DBusMessageIter *iter,
248 struct connman_service *service = user_data;
252 switch (__connman_service_get_security(service)) {
253 case CONNMAN_SERVICE_SECURITY_WEP:
256 case CONNMAN_SERVICE_SECURITY_PSK:
259 case CONNMAN_SERVICE_SECURITY_8021X:
260 phase2 = __connman_service_get_phase2(service);
262 if (phase2 != NULL && (
263 g_str_has_suffix(phase2, "GTC") == TRUE ||
264 g_str_has_suffix(phase2, "OTP") == TRUE))
267 value = "passphrase";
274 connman_dbus_dict_append_basic(iter, "Type",
275 DBUS_TYPE_STRING, &value);
277 connman_dbus_dict_append_basic(iter, "Requirement",
278 DBUS_TYPE_STRING, &value);
280 if (__connman_service_wps_enabled(service) == TRUE) {
281 connman_dbus_dict_append_array(iter, "Alternates",
283 request_input_append_alternates,
288 static void request_input_append_wps(DBusMessageIter *iter, void *user_data)
290 const char *str = "wpspin";
292 connman_dbus_dict_append_basic(iter, "Type",
293 DBUS_TYPE_STRING, &str);
295 connman_dbus_dict_append_basic(iter, "Requirement",
296 DBUS_TYPE_STRING, &str);
299 static void request_input_append_name(DBusMessageIter *iter, void *user_data)
301 const char *str = "string";
303 connman_dbus_dict_append_basic(iter, "Type",
304 DBUS_TYPE_STRING, &str);
306 connman_dbus_dict_append_basic(iter, "Requirement",
307 DBUS_TYPE_STRING, &str);
308 connman_dbus_dict_append_array(iter, "Alternates",
310 request_input_append_alternates,
314 static void request_input_append_ssid(DBusMessageIter *iter, void *user_data)
316 const char *str = "ssid";
318 connman_dbus_dict_append_basic(iter, "Type",
319 DBUS_TYPE_STRING, &str);
321 connman_dbus_dict_append_basic(iter, "Requirement",
322 DBUS_TYPE_STRING, &str);
325 static void request_input_append_password(DBusMessageIter *iter,
328 char *str = "passphrase";
330 connman_dbus_dict_append_basic(iter, "Type",
331 DBUS_TYPE_STRING, &str);
333 connman_dbus_dict_append_basic(iter, "Requirement",
334 DBUS_TYPE_STRING, &str);
337 static void request_input_append_previouspassphrase(DBusMessageIter *iter,
340 struct connman_service *service = user_data;
341 enum connman_service_security security;
342 const char *passphrase, *str = NULL;
344 passphrase = __connman_service_get_passphrase(service);
346 security = __connman_service_get_security(service);
348 case CONNMAN_SERVICE_SECURITY_WEP:
351 case CONNMAN_SERVICE_SECURITY_PSK:
355 * This should never happen: no passphrase is set if security is not
356 * one of the above.*/
361 connman_dbus_dict_append_basic(iter, "Type",
362 DBUS_TYPE_STRING, &str);
364 str = "informational";
365 connman_dbus_dict_append_basic(iter, "Requirement",
366 DBUS_TYPE_STRING, &str);
368 connman_dbus_dict_append_basic(iter, "Value",
369 DBUS_TYPE_STRING, &passphrase);
372 static void request_input_login_reply(DBusPendingCall *call, void *user_data)
374 struct request_input_reply *username_password_reply = user_data;
375 const char *error = NULL;
376 connman_bool_t values_received = FALSE;
377 char *username = NULL;
378 char *password = NULL;
380 DBusMessageIter iter, dict;
381 DBusMessage *reply = dbus_pending_call_steal_reply(call);
383 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
384 error = dbus_message_get_error_name(reply);
388 if (check_reply_has_dict(reply) == FALSE)
391 values_received = TRUE;
393 dbus_message_iter_init(reply, &iter);
394 dbus_message_iter_recurse(&iter, &dict);
395 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
396 DBusMessageIter entry, value;
398 dbus_message_iter_recurse(&dict, &entry);
399 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
402 dbus_message_iter_get_basic(&entry, &key);
404 if (g_str_equal(key, "Username")) {
405 dbus_message_iter_next(&entry);
406 if (dbus_message_iter_get_arg_type(&entry)
407 != DBUS_TYPE_VARIANT)
409 dbus_message_iter_recurse(&entry, &value);
410 dbus_message_iter_get_basic(&value, &username);
412 } else if (g_str_equal(key, "Password")) {
413 dbus_message_iter_next(&entry);
414 if (dbus_message_iter_get_arg_type(&entry) !=
417 dbus_message_iter_recurse(&entry, &value);
418 dbus_message_iter_get_basic(&value, &password);
421 dbus_message_iter_next(&dict);
425 username_password_reply->callback(username_password_reply->service,
426 values_received, NULL, 0,
429 username_password_reply->user_data);
430 connman_service_unref(username_password_reply->service);
431 dbus_message_unref(reply);
432 g_free(username_password_reply);
435 int __connman_agent_request_passphrase_input(struct connman_service *service,
436 authentication_cb_t callback, void *user_data)
438 DBusMessage *message;
440 DBusMessageIter iter;
441 DBusMessageIter dict;
442 DBusPendingCall *call;
443 struct request_input_reply *passphrase_reply;
445 if (service == NULL || agent_path == NULL || callback == NULL)
448 message = dbus_message_new_method_call(agent_sender, agent_path,
449 CONNMAN_AGENT_INTERFACE,
454 dbus_message_iter_init_append(message, &iter);
456 path = __connman_service_get_path(service);
457 dbus_message_iter_append_basic(&iter,
458 DBUS_TYPE_OBJECT_PATH, &path);
460 connman_dbus_dict_open(&iter, &dict);
462 if (__connman_service_is_hidden(service)) {
463 connman_dbus_dict_append_dict(&dict, "Name",
464 request_input_append_name, NULL);
465 connman_dbus_dict_append_dict(&dict, "SSID",
466 request_input_append_ssid, NULL);
469 if (__connman_service_get_security(service) ==
470 CONNMAN_SERVICE_SECURITY_8021X) {
471 connman_dbus_dict_append_dict(&dict, "Identity",
472 request_input_append_identity, service);
475 if (__connman_service_get_security(service) !=
476 CONNMAN_SERVICE_SECURITY_NONE) {
477 connman_dbus_dict_append_dict(&dict, "Passphrase",
478 request_input_append_passphrase, service);
480 if (__connman_service_get_passphrase(service) != NULL)
481 connman_dbus_dict_append_dict(&dict, "PreviousPassphrase",
482 request_input_append_previouspassphrase,
486 if (__connman_service_wps_enabled(service) == TRUE) {
487 connman_dbus_dict_append_dict(&dict, "WPS",
488 request_input_append_wps, NULL);
491 connman_dbus_dict_close(&iter, &dict);
493 passphrase_reply = g_try_new0(struct request_input_reply, 1);
494 if (passphrase_reply == NULL) {
495 dbus_message_unref(message);
499 if (dbus_connection_send_with_reply(connection, message, &call,
500 connman_timeout_input_request())
502 dbus_message_unref(message);
503 g_free(passphrase_reply);
508 dbus_message_unref(message);
509 g_free(passphrase_reply);
513 passphrase_reply->service = connman_service_ref(service);
514 passphrase_reply->callback = callback;
515 passphrase_reply->user_data = user_data;
517 dbus_pending_call_set_notify(call, request_input_passphrase_reply,
518 passphrase_reply, NULL);
520 dbus_message_unref(message);
525 int __connman_agent_request_login_input(struct connman_service *service,
526 authentication_cb_t callback, void *user_data)
528 DBusMessage *message;
530 DBusMessageIter iter;
531 DBusMessageIter dict;
532 DBusPendingCall *call;
533 struct request_input_reply *username_password_reply;
535 if (service == NULL || agent_path == NULL || callback == NULL)
538 message = dbus_message_new_method_call(agent_sender, agent_path,
539 CONNMAN_AGENT_INTERFACE,
544 dbus_message_iter_init_append(message, &iter);
546 path = __connman_service_get_path(service);
547 dbus_message_iter_append_basic(&iter,
548 DBUS_TYPE_OBJECT_PATH, &path);
550 connman_dbus_dict_open(&iter, &dict);
552 connman_dbus_dict_append_dict(&dict, "Username",
553 request_input_append_identity, service);
555 connman_dbus_dict_append_dict(&dict, "Password",
556 request_input_append_password, service);
558 connman_dbus_dict_close(&iter, &dict);
560 username_password_reply = g_try_new0(struct request_input_reply, 1);
561 if (username_password_reply == NULL) {
562 dbus_message_unref(message);
566 if (dbus_connection_send_with_reply(connection, message, &call,
567 connman_timeout_input_request())
569 dbus_message_unref(message);
570 g_free(username_password_reply);
575 dbus_message_unref(message);
576 g_free(username_password_reply);
580 username_password_reply->service = connman_service_ref(service);
581 username_password_reply->callback = callback;
582 username_password_reply->user_data = user_data;
584 dbus_pending_call_set_notify(call, request_input_login_reply,
585 username_password_reply, NULL);
587 dbus_message_unref(message);
592 struct request_browser_reply_data {
593 struct connman_service *service;
594 browser_authentication_cb_t callback;
598 static void request_browser_reply(DBusPendingCall *call, void *user_data)
600 struct request_browser_reply_data *browser_reply_data = user_data;
601 DBusMessage *reply = dbus_pending_call_steal_reply(call);
602 connman_bool_t result = FALSE;
603 const char *error = NULL;
605 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
606 error = dbus_message_get_error_name(reply);
613 browser_reply_data->callback(browser_reply_data->service, result,
614 error, browser_reply_data->user_data);
615 connman_service_unref(browser_reply_data->service);
616 dbus_message_unref(reply);
617 g_free(browser_reply_data);
620 int __connman_agent_request_browser(struct connman_service *service,
621 browser_authentication_cb_t callback,
622 const char *url, void *user_data)
624 struct request_browser_reply_data *browser_reply_data;
625 DBusPendingCall *call;
626 DBusMessage *message;
627 DBusMessageIter iter;
630 if (service == NULL || agent_path == NULL || callback == NULL)
636 message = dbus_message_new_method_call(agent_sender, agent_path,
637 CONNMAN_AGENT_INTERFACE,
642 dbus_message_iter_init_append(message, &iter);
644 path = __connman_service_get_path(service);
645 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &path);
647 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &url);
649 browser_reply_data = g_try_new0(struct request_browser_reply_data, 1);
650 if (browser_reply_data == NULL) {
651 dbus_message_unref(message);
655 if (dbus_connection_send_with_reply(connection, message, &call,
656 connman_timeout_browser_launch())
658 dbus_message_unref(message);
659 g_free(browser_reply_data);
664 dbus_message_unref(message);
665 g_free(browser_reply_data);
669 browser_reply_data->service = connman_service_ref(service);
670 browser_reply_data->callback = callback;
671 browser_reply_data->user_data = user_data;
673 dbus_pending_call_set_notify(call, request_browser_reply,
674 browser_reply_data, NULL);
676 dbus_message_unref(message);
681 struct report_error_data {
682 struct connman_service *service;
683 report_error_cb_t callback;
687 static void report_error_reply(DBusPendingCall *call, void *user_data)
689 struct report_error_data *report_error = user_data;
690 DBusMessage *reply = dbus_pending_call_steal_reply(call);
691 gboolean retry = FALSE;
692 const char *dbus_err;
694 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
695 dbus_err = dbus_message_get_error_name(reply);
696 if (dbus_err != NULL &&
698 CONNMAN_AGENT_INTERFACE ".Error.Retry") == 0)
702 report_error->callback(report_error->service, retry,
703 report_error->user_data);
704 connman_service_unref(report_error->service);
705 g_free(report_error);
706 dbus_message_unref(reply);
707 dbus_pending_call_unref(call);
710 int __connman_agent_report_error(struct connman_service *service,
712 report_error_cb_t callback, void *user_data)
714 DBusMessage *message;
715 DBusMessageIter iter;
717 struct report_error_data *report_error;
718 DBusPendingCall *call;
720 if (service == NULL || agent_path == NULL || error == NULL ||
724 message = dbus_message_new_method_call(agent_sender, agent_path,
725 CONNMAN_AGENT_INTERFACE,
730 dbus_message_iter_init_append(message, &iter);
732 path = __connman_service_get_path(service);
733 dbus_message_iter_append_basic(&iter,
734 DBUS_TYPE_OBJECT_PATH, &path);
735 dbus_message_iter_append_basic(&iter,
736 DBUS_TYPE_STRING, &error);
738 report_error = g_try_new0(struct report_error_data, 1);
739 if (report_error == NULL) {
740 dbus_message_unref(message);
744 if (dbus_connection_send_with_reply(connection, message, &call,
745 connman_timeout_input_request())
747 dbus_message_unref(message);
748 g_free(report_error);
753 dbus_message_unref(message);
754 g_free(report_error);
758 report_error->service = connman_service_ref(service);
759 report_error->callback = callback;
760 report_error->user_data = user_data;
761 dbus_pending_call_set_notify(call, report_error_reply,
763 dbus_message_unref(message);
768 int __connman_agent_init(void)
772 connection = connman_dbus_get_connection();
773 if (connection == NULL)
779 void __connman_agent_cleanup(void)
781 DBusMessage *message;
785 if (connection == NULL)
789 g_dbus_remove_watch(connection, agent_watch);
791 if (agent_path == NULL)
794 message = dbus_message_new_method_call(agent_sender, agent_path,
795 CONNMAN_AGENT_INTERFACE, "Release");
799 dbus_message_set_no_reply(message, TRUE);
801 g_dbus_send_message(connection, message);
805 dbus_connection_unref(connection);