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_login_reply(DBusPendingCall *call, void *user_data)
339 struct request_input_reply *username_password_reply = user_data;
340 const char *error = NULL;
341 connman_bool_t values_received = FALSE;
342 char *username = NULL;
343 char *password = NULL;
345 DBusMessageIter iter, dict;
346 DBusMessage *reply = dbus_pending_call_steal_reply(call);
348 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
349 error = dbus_message_get_error_name(reply);
353 if (check_reply_has_dict(reply) == FALSE)
356 values_received = TRUE;
358 dbus_message_iter_init(reply, &iter);
359 dbus_message_iter_recurse(&iter, &dict);
360 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
361 DBusMessageIter entry, value;
363 dbus_message_iter_recurse(&dict, &entry);
364 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
367 dbus_message_iter_get_basic(&entry, &key);
369 if (g_str_equal(key, "Username")) {
370 dbus_message_iter_next(&entry);
371 if (dbus_message_iter_get_arg_type(&entry)
372 != DBUS_TYPE_VARIANT)
374 dbus_message_iter_recurse(&entry, &value);
375 dbus_message_iter_get_basic(&value, &username);
377 } else if (g_str_equal(key, "Password")) {
378 dbus_message_iter_next(&entry);
379 if (dbus_message_iter_get_arg_type(&entry) !=
382 dbus_message_iter_recurse(&entry, &value);
383 dbus_message_iter_get_basic(&value, &password);
386 dbus_message_iter_next(&dict);
390 username_password_reply->callback(username_password_reply->service,
391 values_received, NULL, 0,
394 username_password_reply->user_data);
395 connman_service_unref(username_password_reply->service);
396 dbus_message_unref(reply);
397 g_free(username_password_reply);
400 int __connman_agent_request_passphrase_input(struct connman_service *service,
401 authentication_cb_t callback, void *user_data)
403 DBusMessage *message;
405 DBusMessageIter iter;
406 DBusMessageIter dict;
407 DBusPendingCall *call;
408 struct request_input_reply *passphrase_reply;
410 if (service == NULL || agent_path == NULL || callback == NULL)
413 message = dbus_message_new_method_call(agent_sender, agent_path,
414 CONNMAN_AGENT_INTERFACE,
419 dbus_message_iter_init_append(message, &iter);
421 path = __connman_service_get_path(service);
422 dbus_message_iter_append_basic(&iter,
423 DBUS_TYPE_OBJECT_PATH, &path);
425 connman_dbus_dict_open(&iter, &dict);
427 if (__connman_service_is_hidden(service)) {
428 connman_dbus_dict_append_dict(&dict, "Name",
429 request_input_append_name, NULL);
430 connman_dbus_dict_append_dict(&dict, "SSID",
431 request_input_append_ssid, NULL);
434 if (__connman_service_get_security(service) ==
435 CONNMAN_SERVICE_SECURITY_8021X) {
436 connman_dbus_dict_append_dict(&dict, "Identity",
437 request_input_append_identity, service);
440 if (__connman_service_get_security(service) !=
441 CONNMAN_SERVICE_SECURITY_NONE) {
442 connman_dbus_dict_append_dict(&dict, "Passphrase",
443 request_input_append_passphrase, service);
446 if (__connman_service_wps_enabled(service) == TRUE) {
447 connman_dbus_dict_append_dict(&dict, "WPS",
448 request_input_append_wps, NULL);
451 connman_dbus_dict_close(&iter, &dict);
453 passphrase_reply = g_try_new0(struct request_input_reply, 1);
454 if (passphrase_reply == NULL) {
455 dbus_message_unref(message);
459 if (dbus_connection_send_with_reply(connection, message,
460 &call, -1) == FALSE) {
461 dbus_message_unref(message);
462 g_free(passphrase_reply);
467 dbus_message_unref(message);
468 g_free(passphrase_reply);
472 passphrase_reply->service = connman_service_ref(service);
473 passphrase_reply->callback = callback;
474 passphrase_reply->user_data = user_data;
476 dbus_pending_call_set_notify(call, request_input_passphrase_reply,
477 passphrase_reply, NULL);
479 dbus_message_unref(message);
484 int __connman_agent_request_login_input(struct connman_service *service,
485 authentication_cb_t callback, void *user_data)
487 DBusMessage *message;
489 DBusMessageIter iter;
490 DBusMessageIter dict;
491 DBusPendingCall *call;
492 struct request_input_reply *username_password_reply;
494 if (service == NULL || agent_path == NULL || callback == NULL)
497 message = dbus_message_new_method_call(agent_sender, agent_path,
498 CONNMAN_AGENT_INTERFACE,
503 dbus_message_iter_init_append(message, &iter);
505 path = __connman_service_get_path(service);
506 dbus_message_iter_append_basic(&iter,
507 DBUS_TYPE_OBJECT_PATH, &path);
509 connman_dbus_dict_open(&iter, &dict);
511 connman_dbus_dict_append_dict(&dict, "Username",
512 request_input_append_identity, service);
514 connman_dbus_dict_append_dict(&dict, "Password",
515 request_input_append_password, service);
517 connman_dbus_dict_close(&iter, &dict);
519 username_password_reply = g_try_new0(struct request_input_reply, 1);
520 if (username_password_reply == NULL) {
521 dbus_message_unref(message);
525 if (dbus_connection_send_with_reply(connection, message,
526 &call, -1) == FALSE) {
527 dbus_message_unref(message);
528 g_free(username_password_reply);
533 dbus_message_unref(message);
534 g_free(username_password_reply);
538 username_password_reply->service = connman_service_ref(service);
539 username_password_reply->callback = callback;
540 username_password_reply->user_data = user_data;
542 dbus_pending_call_set_notify(call, request_input_login_reply,
543 username_password_reply, NULL);
545 dbus_message_unref(message);
550 struct request_browser_reply_data {
551 struct connman_service *service;
552 browser_authentication_cb_t callback;
556 static void request_browser_reply(DBusPendingCall *call, void *user_data)
558 struct request_browser_reply_data *browser_reply_data = user_data;
559 DBusMessage *reply = dbus_pending_call_steal_reply(call);
560 connman_bool_t result = FALSE;
561 const char *error = NULL;
563 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
564 error = dbus_message_get_error_name(reply);
571 browser_reply_data->callback(browser_reply_data->service, result,
572 error, browser_reply_data->user_data);
573 connman_service_unref(browser_reply_data->service);
574 dbus_message_unref(reply);
575 g_free(browser_reply_data);
578 int __connman_agent_request_browser(struct connman_service *service,
579 browser_authentication_cb_t callback,
580 const char *url, void *user_data)
582 struct request_browser_reply_data *browser_reply_data;
583 DBusPendingCall *call;
584 DBusMessage *message;
585 DBusMessageIter iter;
588 if (service == NULL || agent_path == NULL || callback == NULL)
594 message = dbus_message_new_method_call(agent_sender, agent_path,
595 CONNMAN_AGENT_INTERFACE,
600 dbus_message_iter_init_append(message, &iter);
602 path = __connman_service_get_path(service);
603 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &path);
605 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &url);
607 browser_reply_data = g_try_new0(struct request_browser_reply_data, 1);
608 if (browser_reply_data == NULL) {
609 dbus_message_unref(message);
613 if (dbus_connection_send_with_reply(connection, message,
614 &call, -1) == FALSE) {
615 dbus_message_unref(message);
616 g_free(browser_reply_data);
621 dbus_message_unref(message);
622 g_free(browser_reply_data);
626 browser_reply_data->service = connman_service_ref(service);
627 browser_reply_data->callback = callback;
628 browser_reply_data->user_data = user_data;
630 dbus_pending_call_set_notify(call, request_browser_reply,
631 browser_reply_data, NULL);
633 dbus_message_unref(message);
638 struct report_error_data {
639 struct connman_service *service;
640 report_error_cb_t callback;
644 static void report_error_reply(DBusPendingCall *call, void *user_data)
646 struct report_error_data *report_error = user_data;
647 DBusMessage *reply = dbus_pending_call_steal_reply(call);
648 gboolean retry = FALSE;
649 const char *dbus_err;
651 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
652 dbus_err = dbus_message_get_error_name(reply);
653 if (dbus_err != NULL &&
655 CONNMAN_AGENT_INTERFACE ".Error.Retry") == 0)
659 report_error->callback(report_error->service, retry,
660 report_error->user_data);
661 connman_service_unref(report_error->service);
662 g_free(report_error);
663 dbus_message_unref(reply);
664 dbus_pending_call_unref(call);
667 int __connman_agent_report_error(struct connman_service *service,
669 report_error_cb_t callback, void *user_data)
671 DBusMessage *message;
672 DBusMessageIter iter;
674 struct report_error_data *report_error;
675 DBusPendingCall *call;
677 if (service == NULL || agent_path == NULL || error == NULL ||
681 message = dbus_message_new_method_call(agent_sender, agent_path,
682 CONNMAN_AGENT_INTERFACE,
687 dbus_message_iter_init_append(message, &iter);
689 path = __connman_service_get_path(service);
690 dbus_message_iter_append_basic(&iter,
691 DBUS_TYPE_OBJECT_PATH, &path);
692 dbus_message_iter_append_basic(&iter,
693 DBUS_TYPE_STRING, &error);
695 report_error = g_try_new0(struct report_error_data, 1);
696 if (report_error == NULL) {
697 dbus_message_unref(message);
701 if (dbus_connection_send_with_reply(connection, message,
702 &call, -1) == FALSE) {
703 dbus_message_unref(message);
704 g_free(report_error);
709 dbus_message_unref(message);
710 g_free(report_error);
714 report_error->service = connman_service_ref(service);
715 report_error->callback = callback;
716 report_error->user_data = user_data;
717 dbus_pending_call_set_notify(call, report_error_reply,
719 dbus_message_unref(message);
724 int __connman_agent_init(void)
728 connection = connman_dbus_get_connection();
729 if (connection == NULL)
735 void __connman_agent_cleanup(void)
737 DBusMessage *message;
741 if (connection == NULL)
745 g_dbus_remove_watch(connection, agent_watch);
747 if (agent_path == NULL)
750 message = dbus_message_new_method_call(agent_sender, agent_path,
751 CONNMAN_AGENT_INTERFACE, "Release");
755 dbus_message_set_no_reply(message, TRUE);
757 g_dbus_send_message(connection, message);
761 dbus_connection_unref(connection);