5 * Copyright (C) 2007-2010 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 char *identity = NULL;
100 char *passphrase = NULL;
105 DBusMessageIter iter, dict;
106 DBusMessage *reply = dbus_pending_call_steal_reply(call);
108 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
111 values_received = TRUE;
113 dbus_message_iter_init(reply, &iter);
114 dbus_message_iter_recurse(&iter, &dict);
115 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
116 DBusMessageIter entry, value;
118 dbus_message_iter_recurse(&dict, &entry);
119 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
122 dbus_message_iter_get_basic(&entry, &key);
124 if (g_str_equal(key, "Identity")) {
125 dbus_message_iter_next(&entry);
126 if (dbus_message_iter_get_arg_type(&entry)
127 != DBUS_TYPE_VARIANT)
129 dbus_message_iter_recurse(&entry, &value);
130 dbus_message_iter_get_basic(&value, &identity);
132 } else if (g_str_equal(key, "Passphrase")) {
133 dbus_message_iter_next(&entry);
134 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
136 dbus_message_iter_recurse(&entry, &value);
137 dbus_message_iter_get_basic(&value, &passphrase);
139 } else if (g_str_equal(key, "WPS")) {
142 dbus_message_iter_next(&entry);
143 if (dbus_message_iter_get_arg_type(&entry)
144 != DBUS_TYPE_VARIANT)
146 dbus_message_iter_recurse(&entry, &value);
147 dbus_message_iter_get_basic(&value, &wpspin);
149 } else if (g_str_equal(key, "Name")) {
150 dbus_message_iter_next(&entry);
151 if (dbus_message_iter_get_arg_type(&entry)
152 != DBUS_TYPE_VARIANT)
154 dbus_message_iter_recurse(&entry, &value);
155 dbus_message_iter_get_basic(&value, &name);
156 name_len = strlen(name);
157 } else if (g_str_equal(key, "SSID")) {
158 dbus_message_iter_next(&entry);
159 if (dbus_message_iter_get_arg_type(&entry)
160 != DBUS_TYPE_VARIANT)
162 dbus_message_iter_recurse(&entry, &value);
163 if (dbus_message_iter_get_arg_type(&value)
164 != DBUS_TYPE_VARIANT)
166 if (dbus_message_iter_get_element_type(&value)
167 != DBUS_TYPE_VARIANT)
169 dbus_message_iter_get_fixed_array(&value, &name,
172 dbus_message_iter_next(&dict);
176 struct connman_network *network;
178 network = __connman_service_get_network(
179 passphrase_reply->service);
183 connman_network_set_bool(network, "WiFi.UseWPS", wps);
185 if (wpspin != NULL && strlen(wpspin) > 0)
186 connman_network_set_string(network,
187 "WiFi.PinWPS", wpspin);
189 connman_network_set_string(network,
190 "WiFi.PinWPS", NULL);
194 passphrase_reply->callback(passphrase_reply->service, values_received,
196 identity, passphrase,
197 passphrase_reply->user_data);
198 connman_service_unref(passphrase_reply->service);
199 dbus_message_unref(reply);
200 g_free(passphrase_reply);
203 static void request_input_append_alternates(DBusMessageIter *iter,
206 const char *str = user_data;
207 char **alternates, **alternative;
212 alternates = g_strsplit(str, ",", 0);
213 if (alternates == NULL)
216 for (alternative = alternates; *alternative != NULL; alternative++)
217 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
220 g_strfreev(alternates);
223 static void request_input_append_identity(DBusMessageIter *iter,
226 char *str = "string";
228 connman_dbus_dict_append_basic(iter, "Type",
229 DBUS_TYPE_STRING, &str);
231 connman_dbus_dict_append_basic(iter, "Requirement",
232 DBUS_TYPE_STRING, &str);
235 static void request_input_append_passphrase(DBusMessageIter *iter,
238 struct connman_service *service = user_data;
242 switch (__connman_service_get_security(service)) {
243 case CONNMAN_SERVICE_SECURITY_WEP:
246 case CONNMAN_SERVICE_SECURITY_PSK:
249 case CONNMAN_SERVICE_SECURITY_8021X:
250 phase2 = __connman_service_get_phase2(service);
252 if (phase2 != NULL && (
253 g_str_has_suffix(phase2, "GTC") == TRUE ||
254 g_str_has_suffix(phase2, "OTP") == TRUE))
257 value = "passphrase";
264 connman_dbus_dict_append_basic(iter, "Type",
265 DBUS_TYPE_STRING, &value);
267 connman_dbus_dict_append_basic(iter, "Requirement",
268 DBUS_TYPE_STRING, &value);
270 if (__connman_service_wps_enabled(service) == TRUE) {
271 connman_dbus_dict_append_array(iter, "Alternates",
273 request_input_append_alternates,
278 static void request_input_append_wps(DBusMessageIter *iter, void *user_data)
280 const char *str = "wpspin";
282 connman_dbus_dict_append_basic(iter, "Type",
283 DBUS_TYPE_STRING, &str);
285 connman_dbus_dict_append_basic(iter, "Requirement",
286 DBUS_TYPE_STRING, &str);
289 static void request_input_append_name(DBusMessageIter *iter, void *user_data)
291 const char *str = "string";
293 connman_dbus_dict_append_basic(iter, "Type",
294 DBUS_TYPE_STRING, &str);
296 connman_dbus_dict_append_basic(iter, "Requirement",
297 DBUS_TYPE_STRING, &str);
298 connman_dbus_dict_append_array(iter, "Alternates",
300 request_input_append_alternates,
304 static void request_input_append_ssid(DBusMessageIter *iter, void *user_data)
306 const char *str = "ssid";
308 connman_dbus_dict_append_basic(iter, "Type",
309 DBUS_TYPE_STRING, &str);
311 connman_dbus_dict_append_basic(iter, "Requirement",
312 DBUS_TYPE_STRING, &str);
315 static void request_input_append_password(DBusMessageIter *iter,
318 char *str = "passphrase";
320 connman_dbus_dict_append_basic(iter, "Type",
321 DBUS_TYPE_STRING, &str);
323 connman_dbus_dict_append_basic(iter, "Requirement",
324 DBUS_TYPE_STRING, &str);
327 static void request_input_login_reply(DBusPendingCall *call, void *user_data)
329 struct request_input_reply *username_password_reply = user_data;
330 char *username = NULL;
331 char *password = NULL;
333 DBusMessageIter iter, dict;
334 DBusMessage *reply = dbus_pending_call_steal_reply(call);
336 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
339 dbus_message_iter_init(reply, &iter);
340 dbus_message_iter_recurse(&iter, &dict);
341 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
342 DBusMessageIter entry, value;
344 dbus_message_iter_recurse(&dict, &entry);
345 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
348 dbus_message_iter_get_basic(&entry, &key);
350 if (g_str_equal(key, "Username")) {
351 dbus_message_iter_next(&entry);
352 if (dbus_message_iter_get_arg_type(&entry)
353 != DBUS_TYPE_VARIANT)
355 dbus_message_iter_recurse(&entry, &value);
356 dbus_message_iter_get_basic(&value, &username);
358 } else if (g_str_equal(key, "Password")) {
359 dbus_message_iter_next(&entry);
360 if (dbus_message_iter_get_arg_type(&entry) !=
363 dbus_message_iter_recurse(&entry, &value);
364 dbus_message_iter_get_basic(&value, &password);
367 dbus_message_iter_next(&dict);
371 username_password_reply->callback(username_password_reply->service,
374 username_password_reply->user_data);
375 connman_service_unref(username_password_reply->service);
376 dbus_message_unref(reply);
377 g_free(username_password_reply);
380 int __connman_agent_request_passphrase_input(struct connman_service *service,
381 authentication_cb_t callback, void *user_data)
383 DBusMessage *message;
385 DBusMessageIter iter;
386 DBusMessageIter dict;
387 DBusPendingCall *call;
388 struct request_input_reply *passphrase_reply;
390 if (service == NULL || agent_path == NULL || callback == NULL)
393 message = dbus_message_new_method_call(agent_sender, agent_path,
394 CONNMAN_AGENT_INTERFACE,
399 dbus_message_iter_init_append(message, &iter);
401 path = __connman_service_get_path(service);
402 dbus_message_iter_append_basic(&iter,
403 DBUS_TYPE_OBJECT_PATH, &path);
405 connman_dbus_dict_open(&iter, &dict);
407 if (__connman_service_is_hidden(service)) {
408 connman_dbus_dict_append_dict(&dict, "Name",
409 request_input_append_name, NULL);
410 connman_dbus_dict_append_dict(&dict, "SSID",
411 request_input_append_ssid, NULL);
414 if (__connman_service_get_security(service) ==
415 CONNMAN_SERVICE_SECURITY_8021X) {
416 connman_dbus_dict_append_dict(&dict, "Identity",
417 request_input_append_identity, service);
420 connman_dbus_dict_append_dict(&dict, "Passphrase",
421 request_input_append_passphrase, service);
423 if (__connman_service_wps_enabled(service) == TRUE) {
424 connman_dbus_dict_append_dict(&dict, "WPS",
425 request_input_append_wps, NULL);
428 connman_dbus_dict_close(&iter, &dict);
430 passphrase_reply = g_try_new0(struct request_input_reply, 1);
431 if (passphrase_reply == NULL) {
432 dbus_message_unref(message);
436 if (dbus_connection_send_with_reply(connection, message,
437 &call, -1) == FALSE) {
438 dbus_message_unref(message);
439 g_free(passphrase_reply);
444 dbus_message_unref(message);
445 g_free(passphrase_reply);
449 passphrase_reply->service = connman_service_ref(service);
450 passphrase_reply->callback = callback;
451 passphrase_reply->user_data = user_data;
453 dbus_pending_call_set_notify(call, request_input_passphrase_reply,
454 passphrase_reply, NULL);
456 dbus_message_unref(message);
461 int __connman_agent_request_login_input(struct connman_service *service,
462 authentication_cb_t callback, void *user_data)
464 DBusMessage *message;
466 DBusMessageIter iter;
467 DBusMessageIter dict;
468 DBusPendingCall *call;
469 struct request_input_reply *username_password_reply;
471 if (service == NULL || agent_path == NULL || callback == NULL)
474 message = dbus_message_new_method_call(agent_sender, agent_path,
475 CONNMAN_AGENT_INTERFACE,
480 dbus_message_iter_init_append(message, &iter);
482 path = __connman_service_get_path(service);
483 dbus_message_iter_append_basic(&iter,
484 DBUS_TYPE_OBJECT_PATH, &path);
486 connman_dbus_dict_open(&iter, &dict);
488 connman_dbus_dict_append_dict(&dict, "Username",
489 request_input_append_identity, service);
491 connman_dbus_dict_append_dict(&dict, "Password",
492 request_input_append_password, service);
494 connman_dbus_dict_close(&iter, &dict);
496 username_password_reply = g_try_new0(struct request_input_reply, 1);
497 if (username_password_reply == NULL) {
498 dbus_message_unref(message);
502 if (dbus_connection_send_with_reply(connection, message,
503 &call, -1) == FALSE) {
504 dbus_message_unref(message);
505 g_free(username_password_reply);
510 dbus_message_unref(message);
511 g_free(username_password_reply);
515 username_password_reply->service = connman_service_ref(service);
516 username_password_reply->callback = callback;
517 username_password_reply->user_data = user_data;
519 dbus_pending_call_set_notify(call, request_input_login_reply,
520 username_password_reply, NULL);
522 dbus_message_unref(message);
527 int __connman_agent_request_browser(struct connman_service *service,
528 browser_authentication_cb_t callback,
529 const char *url, void *user_data)
531 DBusPendingCall *call;
532 DBusMessage *message;
533 DBusMessageIter iter;
536 if (service == NULL || agent_path == NULL || callback == NULL)
542 message = dbus_message_new_method_call(agent_sender, agent_path,
543 CONNMAN_AGENT_INTERFACE,
548 dbus_message_iter_init_append(message, &iter);
550 path = __connman_service_get_path(service);
551 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &path);
553 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &url);
555 if (dbus_connection_send_with_reply(connection, message,
556 &call, -1) == FALSE) {
557 dbus_message_unref(message);
562 dbus_message_unref(message);
566 dbus_message_unref(message);
571 struct report_error_data {
572 struct connman_service *service;
573 report_error_cb_t callback;
577 static void report_error_reply(DBusPendingCall *call, void *user_data)
579 struct report_error_data *report_error = user_data;
580 DBusMessage *reply = dbus_pending_call_steal_reply(call);
581 gboolean retry = FALSE;
582 const char *dbus_err;
584 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
585 dbus_err = dbus_message_get_error_name(reply);
586 if (dbus_err != NULL &&
588 CONNMAN_AGENT_INTERFACE ".Error.Retry") == 0)
592 report_error->callback(report_error->service, retry,
593 report_error->user_data);
594 connman_service_unref(report_error->service);
595 g_free(report_error);
596 dbus_message_unref(reply);
599 int __connman_agent_report_error(struct connman_service *service,
601 report_error_cb_t callback, void *user_data)
603 DBusMessage *message;
604 DBusMessageIter iter;
606 struct report_error_data *report_error;
607 DBusPendingCall *call;
609 if (service == NULL || agent_path == NULL || error == NULL ||
613 message = dbus_message_new_method_call(agent_sender, agent_path,
614 CONNMAN_AGENT_INTERFACE,
619 dbus_message_iter_init_append(message, &iter);
621 path = __connman_service_get_path(service);
622 dbus_message_iter_append_basic(&iter,
623 DBUS_TYPE_OBJECT_PATH, &path);
624 dbus_message_iter_append_basic(&iter,
625 DBUS_TYPE_STRING, &error);
627 report_error = g_try_new0(struct report_error_data, 1);
628 if (report_error == NULL) {
629 dbus_message_unref(message);
633 if (dbus_connection_send_with_reply(connection, message,
634 &call, -1) == FALSE) {
635 dbus_message_unref(message);
636 g_free(report_error);
641 dbus_message_unref(message);
642 g_free(report_error);
646 report_error->service = connman_service_ref(service);
647 report_error->callback = callback;
648 report_error->user_data = user_data;
649 dbus_pending_call_set_notify(call, report_error_reply,
651 dbus_message_unref(message);
656 int __connman_agent_init(void)
660 connection = connman_dbus_get_connection();
661 if (connection == NULL)
667 void __connman_agent_cleanup(void)
669 DBusMessage *message;
673 if (connection == NULL)
677 g_dbus_remove_watch(connection, agent_watch);
679 if (agent_path == NULL)
682 message = dbus_message_new_method_call(agent_sender, agent_path,
683 CONNMAN_AGENT_INTERFACE, "Release");
687 dbus_message_set_no_reply(message, TRUE);
689 g_dbus_send_message(connection, message);
693 dbus_connection_unref(connection);