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 struct request_input_reply {
89 struct connman_service *service;
90 authentication_cb_t callback;
94 static void request_input_passphrase_reply(DBusPendingCall *call, void *user_data)
96 struct request_input_reply *passphrase_reply = user_data;
97 connman_bool_t values_received = FALSE;
98 connman_bool_t wps = FALSE;
99 const char *error = NULL;
100 char *identity = NULL;
101 char *passphrase = NULL;
106 DBusMessageIter iter, dict;
107 DBusMessage *reply = dbus_pending_call_steal_reply(call);
109 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
110 error = dbus_message_get_error_name(reply);
114 values_received = TRUE;
116 dbus_message_iter_init(reply, &iter);
117 dbus_message_iter_recurse(&iter, &dict);
118 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
119 DBusMessageIter entry, value;
121 dbus_message_iter_recurse(&dict, &entry);
122 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
125 dbus_message_iter_get_basic(&entry, &key);
127 if (g_str_equal(key, "Identity")) {
128 dbus_message_iter_next(&entry);
129 if (dbus_message_iter_get_arg_type(&entry)
130 != DBUS_TYPE_VARIANT)
132 dbus_message_iter_recurse(&entry, &value);
133 dbus_message_iter_get_basic(&value, &identity);
135 } else if (g_str_equal(key, "Passphrase")) {
136 dbus_message_iter_next(&entry);
137 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
139 dbus_message_iter_recurse(&entry, &value);
140 dbus_message_iter_get_basic(&value, &passphrase);
142 } else if (g_str_equal(key, "WPS")) {
145 dbus_message_iter_next(&entry);
146 if (dbus_message_iter_get_arg_type(&entry)
147 != DBUS_TYPE_VARIANT)
149 dbus_message_iter_recurse(&entry, &value);
150 dbus_message_iter_get_basic(&value, &wpspin);
152 } else if (g_str_equal(key, "Name")) {
153 dbus_message_iter_next(&entry);
154 if (dbus_message_iter_get_arg_type(&entry)
155 != DBUS_TYPE_VARIANT)
157 dbus_message_iter_recurse(&entry, &value);
158 dbus_message_iter_get_basic(&value, &name);
159 name_len = strlen(name);
160 } else if (g_str_equal(key, "SSID")) {
161 dbus_message_iter_next(&entry);
162 if (dbus_message_iter_get_arg_type(&entry)
163 != DBUS_TYPE_VARIANT)
165 dbus_message_iter_recurse(&entry, &value);
166 if (dbus_message_iter_get_arg_type(&value)
167 != DBUS_TYPE_VARIANT)
169 if (dbus_message_iter_get_element_type(&value)
170 != DBUS_TYPE_VARIANT)
172 dbus_message_iter_get_fixed_array(&value, &name,
175 dbus_message_iter_next(&dict);
179 passphrase_reply->callback(passphrase_reply->service, values_received,
181 identity, passphrase,
183 passphrase_reply->user_data);
184 connman_service_unref(passphrase_reply->service);
185 dbus_message_unref(reply);
186 dbus_pending_call_unref(call);
187 g_free(passphrase_reply);
190 static void request_input_append_alternates(DBusMessageIter *iter,
193 const char *str = user_data;
194 char **alternates, **alternative;
199 alternates = g_strsplit(str, ",", 0);
200 if (alternates == NULL)
203 for (alternative = alternates; *alternative != NULL; alternative++)
204 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
207 g_strfreev(alternates);
210 static void request_input_append_identity(DBusMessageIter *iter,
213 char *str = "string";
215 connman_dbus_dict_append_basic(iter, "Type",
216 DBUS_TYPE_STRING, &str);
218 connman_dbus_dict_append_basic(iter, "Requirement",
219 DBUS_TYPE_STRING, &str);
222 static void request_input_append_passphrase(DBusMessageIter *iter,
225 struct connman_service *service = user_data;
229 switch (__connman_service_get_security(service)) {
230 case CONNMAN_SERVICE_SECURITY_WEP:
233 case CONNMAN_SERVICE_SECURITY_PSK:
236 case CONNMAN_SERVICE_SECURITY_8021X:
237 phase2 = __connman_service_get_phase2(service);
239 if (phase2 != NULL && (
240 g_str_has_suffix(phase2, "GTC") == TRUE ||
241 g_str_has_suffix(phase2, "OTP") == TRUE))
244 value = "passphrase";
251 connman_dbus_dict_append_basic(iter, "Type",
252 DBUS_TYPE_STRING, &value);
254 connman_dbus_dict_append_basic(iter, "Requirement",
255 DBUS_TYPE_STRING, &value);
257 if (__connman_service_wps_enabled(service) == TRUE) {
258 connman_dbus_dict_append_array(iter, "Alternates",
260 request_input_append_alternates,
265 static void request_input_append_wps(DBusMessageIter *iter, void *user_data)
267 const char *str = "wpspin";
269 connman_dbus_dict_append_basic(iter, "Type",
270 DBUS_TYPE_STRING, &str);
272 connman_dbus_dict_append_basic(iter, "Requirement",
273 DBUS_TYPE_STRING, &str);
276 static void request_input_append_name(DBusMessageIter *iter, void *user_data)
278 const char *str = "string";
280 connman_dbus_dict_append_basic(iter, "Type",
281 DBUS_TYPE_STRING, &str);
283 connman_dbus_dict_append_basic(iter, "Requirement",
284 DBUS_TYPE_STRING, &str);
285 connman_dbus_dict_append_array(iter, "Alternates",
287 request_input_append_alternates,
291 static void request_input_append_ssid(DBusMessageIter *iter, void *user_data)
293 const char *str = "ssid";
295 connman_dbus_dict_append_basic(iter, "Type",
296 DBUS_TYPE_STRING, &str);
298 connman_dbus_dict_append_basic(iter, "Requirement",
299 DBUS_TYPE_STRING, &str);
302 static void request_input_append_password(DBusMessageIter *iter,
305 char *str = "passphrase";
307 connman_dbus_dict_append_basic(iter, "Type",
308 DBUS_TYPE_STRING, &str);
310 connman_dbus_dict_append_basic(iter, "Requirement",
311 DBUS_TYPE_STRING, &str);
314 static void request_input_login_reply(DBusPendingCall *call, void *user_data)
316 struct request_input_reply *username_password_reply = user_data;
317 const char *error = NULL;
318 char *username = NULL;
319 char *password = NULL;
321 DBusMessageIter iter, dict;
322 DBusMessage *reply = dbus_pending_call_steal_reply(call);
324 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
325 error = dbus_message_get_error_name(reply);
329 dbus_message_iter_init(reply, &iter);
330 dbus_message_iter_recurse(&iter, &dict);
331 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
332 DBusMessageIter entry, value;
334 dbus_message_iter_recurse(&dict, &entry);
335 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
338 dbus_message_iter_get_basic(&entry, &key);
340 if (g_str_equal(key, "Username")) {
341 dbus_message_iter_next(&entry);
342 if (dbus_message_iter_get_arg_type(&entry)
343 != DBUS_TYPE_VARIANT)
345 dbus_message_iter_recurse(&entry, &value);
346 dbus_message_iter_get_basic(&value, &username);
348 } else if (g_str_equal(key, "Password")) {
349 dbus_message_iter_next(&entry);
350 if (dbus_message_iter_get_arg_type(&entry) !=
353 dbus_message_iter_recurse(&entry, &value);
354 dbus_message_iter_get_basic(&value, &password);
357 dbus_message_iter_next(&dict);
361 username_password_reply->callback(username_password_reply->service,
365 username_password_reply->user_data);
366 connman_service_unref(username_password_reply->service);
367 dbus_message_unref(reply);
368 g_free(username_password_reply);
371 int __connman_agent_request_passphrase_input(struct connman_service *service,
372 authentication_cb_t callback, void *user_data)
374 DBusMessage *message;
376 DBusMessageIter iter;
377 DBusMessageIter dict;
378 DBusPendingCall *call;
379 struct request_input_reply *passphrase_reply;
381 if (service == NULL || agent_path == NULL || callback == NULL)
384 message = dbus_message_new_method_call(agent_sender, agent_path,
385 CONNMAN_AGENT_INTERFACE,
390 dbus_message_iter_init_append(message, &iter);
392 path = __connman_service_get_path(service);
393 dbus_message_iter_append_basic(&iter,
394 DBUS_TYPE_OBJECT_PATH, &path);
396 connman_dbus_dict_open(&iter, &dict);
398 if (__connman_service_is_hidden(service)) {
399 connman_dbus_dict_append_dict(&dict, "Name",
400 request_input_append_name, NULL);
401 connman_dbus_dict_append_dict(&dict, "SSID",
402 request_input_append_ssid, NULL);
405 if (__connman_service_get_security(service) ==
406 CONNMAN_SERVICE_SECURITY_8021X) {
407 connman_dbus_dict_append_dict(&dict, "Identity",
408 request_input_append_identity, service);
411 if (__connman_service_get_security(service) !=
412 CONNMAN_SERVICE_SECURITY_NONE) {
413 connman_dbus_dict_append_dict(&dict, "Passphrase",
414 request_input_append_passphrase, service);
417 if (__connman_service_wps_enabled(service) == TRUE) {
418 connman_dbus_dict_append_dict(&dict, "WPS",
419 request_input_append_wps, NULL);
422 connman_dbus_dict_close(&iter, &dict);
424 passphrase_reply = g_try_new0(struct request_input_reply, 1);
425 if (passphrase_reply == NULL) {
426 dbus_message_unref(message);
430 if (dbus_connection_send_with_reply(connection, message,
431 &call, -1) == FALSE) {
432 dbus_message_unref(message);
433 g_free(passphrase_reply);
438 dbus_message_unref(message);
439 g_free(passphrase_reply);
443 passphrase_reply->service = connman_service_ref(service);
444 passphrase_reply->callback = callback;
445 passphrase_reply->user_data = user_data;
447 dbus_pending_call_set_notify(call, request_input_passphrase_reply,
448 passphrase_reply, NULL);
450 dbus_message_unref(message);
455 int __connman_agent_request_login_input(struct connman_service *service,
456 authentication_cb_t callback, void *user_data)
458 DBusMessage *message;
460 DBusMessageIter iter;
461 DBusMessageIter dict;
462 DBusPendingCall *call;
463 struct request_input_reply *username_password_reply;
465 if (service == NULL || agent_path == NULL || callback == NULL)
468 message = dbus_message_new_method_call(agent_sender, agent_path,
469 CONNMAN_AGENT_INTERFACE,
474 dbus_message_iter_init_append(message, &iter);
476 path = __connman_service_get_path(service);
477 dbus_message_iter_append_basic(&iter,
478 DBUS_TYPE_OBJECT_PATH, &path);
480 connman_dbus_dict_open(&iter, &dict);
482 connman_dbus_dict_append_dict(&dict, "Username",
483 request_input_append_identity, service);
485 connman_dbus_dict_append_dict(&dict, "Password",
486 request_input_append_password, service);
488 connman_dbus_dict_close(&iter, &dict);
490 username_password_reply = g_try_new0(struct request_input_reply, 1);
491 if (username_password_reply == NULL) {
492 dbus_message_unref(message);
496 if (dbus_connection_send_with_reply(connection, message,
497 &call, -1) == FALSE) {
498 dbus_message_unref(message);
499 g_free(username_password_reply);
504 dbus_message_unref(message);
505 g_free(username_password_reply);
509 username_password_reply->service = connman_service_ref(service);
510 username_password_reply->callback = callback;
511 username_password_reply->user_data = user_data;
513 dbus_pending_call_set_notify(call, request_input_login_reply,
514 username_password_reply, NULL);
516 dbus_message_unref(message);
521 struct request_browser_reply_data {
522 struct connman_service *service;
523 browser_authentication_cb_t callback;
527 static void request_browser_reply(DBusPendingCall *call, void *user_data)
529 struct request_browser_reply_data *browser_reply_data = user_data;
530 DBusMessage *reply = dbus_pending_call_steal_reply(call);
531 connman_bool_t result = FALSE;
532 const char *error = NULL;
534 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
535 error = dbus_message_get_error_name(reply);
542 browser_reply_data->callback(browser_reply_data->service, result,
543 error, browser_reply_data->user_data);
544 connman_service_unref(browser_reply_data->service);
545 dbus_message_unref(reply);
546 g_free(browser_reply_data);
549 int __connman_agent_request_browser(struct connman_service *service,
550 browser_authentication_cb_t callback,
551 const char *url, void *user_data)
553 struct request_browser_reply_data *browser_reply_data;
554 DBusPendingCall *call;
555 DBusMessage *message;
556 DBusMessageIter iter;
559 if (service == NULL || agent_path == NULL || callback == NULL)
565 message = dbus_message_new_method_call(agent_sender, agent_path,
566 CONNMAN_AGENT_INTERFACE,
571 dbus_message_iter_init_append(message, &iter);
573 path = __connman_service_get_path(service);
574 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &path);
576 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &url);
578 browser_reply_data = g_try_new0(struct request_browser_reply_data, 1);
579 if (browser_reply_data == NULL) {
580 dbus_message_unref(message);
584 if (dbus_connection_send_with_reply(connection, message,
585 &call, -1) == FALSE) {
586 dbus_message_unref(message);
587 g_free(browser_reply_data);
592 dbus_message_unref(message);
593 g_free(browser_reply_data);
597 browser_reply_data->service = connman_service_ref(service);
598 browser_reply_data->callback = callback;
599 browser_reply_data->user_data = user_data;
601 dbus_pending_call_set_notify(call, request_browser_reply,
602 browser_reply_data, NULL);
604 dbus_message_unref(message);
609 struct report_error_data {
610 struct connman_service *service;
611 report_error_cb_t callback;
615 static void report_error_reply(DBusPendingCall *call, void *user_data)
617 struct report_error_data *report_error = user_data;
618 DBusMessage *reply = dbus_pending_call_steal_reply(call);
619 gboolean retry = FALSE;
620 const char *dbus_err;
622 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
623 dbus_err = dbus_message_get_error_name(reply);
624 if (dbus_err != NULL &&
626 CONNMAN_AGENT_INTERFACE ".Error.Retry") == 0)
630 report_error->callback(report_error->service, retry,
631 report_error->user_data);
632 connman_service_unref(report_error->service);
633 g_free(report_error);
634 dbus_message_unref(reply);
637 int __connman_agent_report_error(struct connman_service *service,
639 report_error_cb_t callback, void *user_data)
641 DBusMessage *message;
642 DBusMessageIter iter;
644 struct report_error_data *report_error;
645 DBusPendingCall *call;
647 if (service == NULL || agent_path == NULL || error == NULL ||
651 message = dbus_message_new_method_call(agent_sender, agent_path,
652 CONNMAN_AGENT_INTERFACE,
657 dbus_message_iter_init_append(message, &iter);
659 path = __connman_service_get_path(service);
660 dbus_message_iter_append_basic(&iter,
661 DBUS_TYPE_OBJECT_PATH, &path);
662 dbus_message_iter_append_basic(&iter,
663 DBUS_TYPE_STRING, &error);
665 report_error = g_try_new0(struct report_error_data, 1);
666 if (report_error == NULL) {
667 dbus_message_unref(message);
671 if (dbus_connection_send_with_reply(connection, message,
672 &call, -1) == FALSE) {
673 dbus_message_unref(message);
674 g_free(report_error);
679 dbus_message_unref(message);
680 g_free(report_error);
684 report_error->service = connman_service_ref(service);
685 report_error->callback = callback;
686 report_error->user_data = user_data;
687 dbus_pending_call_set_notify(call, report_error_reply,
689 dbus_message_unref(message);
694 int __connman_agent_init(void)
698 connection = connman_dbus_get_connection();
699 if (connection == NULL)
705 void __connman_agent_cleanup(void)
707 DBusMessage *message;
711 if (connection == NULL)
715 g_dbus_remove_watch(connection, agent_watch);
717 if (agent_path == NULL)
720 message = dbus_message_new_method_call(agent_sender, agent_path,
721 CONNMAN_AGENT_INTERFACE, "Release");
725 dbus_message_set_no_reply(message, TRUE);
727 g_dbus_send_message(connection, message);
731 dbus_connection_unref(connection);