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 if (dbus_message_iter_has_next(&iter) == FALSE)
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 struct connman_network *network;
181 network = __connman_service_get_network(
182 passphrase_reply->service);
186 connman_network_set_bool(network, "WiFi.UseWPS", wps);
188 if (wpspin != NULL && strlen(wpspin) > 0)
189 connman_network_set_string(network,
190 "WiFi.PinWPS", wpspin);
192 connman_network_set_string(network,
193 "WiFi.PinWPS", NULL);
197 passphrase_reply->callback(passphrase_reply->service, values_received,
199 identity, passphrase,
200 passphrase_reply->user_data);
201 connman_service_unref(passphrase_reply->service);
202 dbus_message_unref(reply);
203 g_free(passphrase_reply);
206 static void request_input_append_alternates(DBusMessageIter *iter,
209 const char *str = user_data;
210 char **alternates, **alternative;
215 alternates = g_strsplit(str, ",", 0);
216 if (alternates == NULL)
219 for (alternative = alternates; *alternative != NULL; alternative++)
220 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
223 g_strfreev(alternates);
226 static void request_input_append_identity(DBusMessageIter *iter,
229 char *str = "string";
231 connman_dbus_dict_append_basic(iter, "Type",
232 DBUS_TYPE_STRING, &str);
234 connman_dbus_dict_append_basic(iter, "Requirement",
235 DBUS_TYPE_STRING, &str);
238 static void request_input_append_passphrase(DBusMessageIter *iter,
241 struct connman_service *service = user_data;
245 switch (__connman_service_get_security(service)) {
246 case CONNMAN_SERVICE_SECURITY_WEP:
249 case CONNMAN_SERVICE_SECURITY_PSK:
252 case CONNMAN_SERVICE_SECURITY_8021X:
253 phase2 = __connman_service_get_phase2(service);
255 if (phase2 != NULL && (
256 g_str_has_suffix(phase2, "GTC") == TRUE ||
257 g_str_has_suffix(phase2, "OTP") == TRUE))
260 value = "passphrase";
267 connman_dbus_dict_append_basic(iter, "Type",
268 DBUS_TYPE_STRING, &value);
270 connman_dbus_dict_append_basic(iter, "Requirement",
271 DBUS_TYPE_STRING, &value);
273 if (__connman_service_wps_enabled(service) == TRUE) {
274 connman_dbus_dict_append_array(iter, "Alternates",
276 request_input_append_alternates,
281 static void request_input_append_wps(DBusMessageIter *iter, void *user_data)
283 const char *str = "wpspin";
285 connman_dbus_dict_append_basic(iter, "Type",
286 DBUS_TYPE_STRING, &str);
288 connman_dbus_dict_append_basic(iter, "Requirement",
289 DBUS_TYPE_STRING, &str);
292 static void request_input_append_name(DBusMessageIter *iter, void *user_data)
294 const char *str = "string";
296 connman_dbus_dict_append_basic(iter, "Type",
297 DBUS_TYPE_STRING, &str);
299 connman_dbus_dict_append_basic(iter, "Requirement",
300 DBUS_TYPE_STRING, &str);
301 connman_dbus_dict_append_array(iter, "Alternates",
303 request_input_append_alternates,
307 static void request_input_append_ssid(DBusMessageIter *iter, void *user_data)
309 const char *str = "ssid";
311 connman_dbus_dict_append_basic(iter, "Type",
312 DBUS_TYPE_STRING, &str);
314 connman_dbus_dict_append_basic(iter, "Requirement",
315 DBUS_TYPE_STRING, &str);
318 static void request_input_append_password(DBusMessageIter *iter,
321 char *str = "passphrase";
323 connman_dbus_dict_append_basic(iter, "Type",
324 DBUS_TYPE_STRING, &str);
326 connman_dbus_dict_append_basic(iter, "Requirement",
327 DBUS_TYPE_STRING, &str);
330 static void request_input_login_reply(DBusPendingCall *call, void *user_data)
332 struct request_input_reply *username_password_reply = user_data;
333 char *username = NULL;
334 char *password = NULL;
336 DBusMessageIter iter, dict;
337 DBusMessage *reply = dbus_pending_call_steal_reply(call);
339 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
342 dbus_message_iter_init(reply, &iter);
343 if (dbus_message_iter_has_next(&iter) == FALSE)
346 dbus_message_iter_recurse(&iter, &dict);
347 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
348 DBusMessageIter entry, value;
350 dbus_message_iter_recurse(&dict, &entry);
351 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
354 dbus_message_iter_get_basic(&entry, &key);
356 if (g_str_equal(key, "Username")) {
357 dbus_message_iter_next(&entry);
358 if (dbus_message_iter_get_arg_type(&entry)
359 != DBUS_TYPE_VARIANT)
361 dbus_message_iter_recurse(&entry, &value);
362 dbus_message_iter_get_basic(&value, &username);
364 } else if (g_str_equal(key, "Password")) {
365 dbus_message_iter_next(&entry);
366 if (dbus_message_iter_get_arg_type(&entry) !=
369 dbus_message_iter_recurse(&entry, &value);
370 dbus_message_iter_get_basic(&value, &password);
373 dbus_message_iter_next(&dict);
377 username_password_reply->callback(username_password_reply->service,
380 username_password_reply->user_data);
381 connman_service_unref(username_password_reply->service);
382 dbus_message_unref(reply);
383 g_free(username_password_reply);
386 int __connman_agent_request_passphrase_input(struct connman_service *service,
387 authentication_cb_t callback, void *user_data)
389 DBusMessage *message;
391 DBusMessageIter iter;
392 DBusMessageIter dict;
393 DBusPendingCall *call;
394 struct request_input_reply *passphrase_reply;
396 if (service == NULL || agent_path == NULL || callback == NULL)
399 message = dbus_message_new_method_call(agent_sender, agent_path,
400 CONNMAN_AGENT_INTERFACE,
405 dbus_message_iter_init_append(message, &iter);
407 path = __connman_service_get_path(service);
408 dbus_message_iter_append_basic(&iter,
409 DBUS_TYPE_OBJECT_PATH, &path);
411 connman_dbus_dict_open(&iter, &dict);
413 if (__connman_service_is_hidden(service)) {
414 connman_dbus_dict_append_dict(&dict, "Name",
415 request_input_append_name, NULL);
416 connman_dbus_dict_append_dict(&dict, "SSID",
417 request_input_append_ssid, NULL);
420 if (__connman_service_get_security(service) ==
421 CONNMAN_SERVICE_SECURITY_8021X) {
422 connman_dbus_dict_append_dict(&dict, "Identity",
423 request_input_append_identity, service);
426 connman_dbus_dict_append_dict(&dict, "Passphrase",
427 request_input_append_passphrase, service);
429 if (__connman_service_wps_enabled(service) == TRUE) {
430 connman_dbus_dict_append_dict(&dict, "WPS",
431 request_input_append_wps, NULL);
434 connman_dbus_dict_close(&iter, &dict);
436 passphrase_reply = g_try_new0(struct request_input_reply, 1);
437 if (passphrase_reply == NULL) {
438 dbus_message_unref(message);
442 if (dbus_connection_send_with_reply(connection, message,
443 &call, -1) == FALSE) {
444 dbus_message_unref(message);
445 g_free(passphrase_reply);
450 dbus_message_unref(message);
451 g_free(passphrase_reply);
455 passphrase_reply->service = connman_service_ref(service);
456 passphrase_reply->callback = callback;
457 passphrase_reply->user_data = user_data;
459 dbus_pending_call_set_notify(call, request_input_passphrase_reply,
460 passphrase_reply, NULL);
462 dbus_message_unref(message);
467 int __connman_agent_request_login_input(struct connman_service *service,
468 authentication_cb_t callback, void *user_data)
470 DBusMessage *message;
472 DBusMessageIter iter;
473 DBusMessageIter dict;
474 DBusPendingCall *call;
475 struct request_input_reply *username_password_reply;
477 if (service == NULL || agent_path == NULL || callback == NULL)
480 message = dbus_message_new_method_call(agent_sender, agent_path,
481 CONNMAN_AGENT_INTERFACE,
486 dbus_message_iter_init_append(message, &iter);
488 path = __connman_service_get_path(service);
489 dbus_message_iter_append_basic(&iter,
490 DBUS_TYPE_OBJECT_PATH, &path);
492 connman_dbus_dict_open(&iter, &dict);
494 connman_dbus_dict_append_dict(&dict, "Username",
495 request_input_append_identity, service);
497 connman_dbus_dict_append_dict(&dict, "Password",
498 request_input_append_password, service);
500 connman_dbus_dict_close(&iter, &dict);
502 username_password_reply = g_try_new0(struct request_input_reply, 1);
503 if (username_password_reply == NULL) {
504 dbus_message_unref(message);
508 if (dbus_connection_send_with_reply(connection, message,
509 &call, -1) == FALSE) {
510 dbus_message_unref(message);
511 g_free(username_password_reply);
516 dbus_message_unref(message);
517 g_free(username_password_reply);
521 username_password_reply->service = connman_service_ref(service);
522 username_password_reply->callback = callback;
523 username_password_reply->user_data = user_data;
525 dbus_pending_call_set_notify(call, request_input_login_reply,
526 username_password_reply, NULL);
528 dbus_message_unref(message);
533 struct report_error_data {
534 struct connman_service *service;
535 report_error_cb_t callback;
539 static void report_error_reply(DBusPendingCall *call, void *user_data)
541 struct report_error_data *report_error = user_data;
542 DBusMessage *reply = dbus_pending_call_steal_reply(call);
543 gboolean retry = FALSE;
544 const char *dbus_err;
546 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
547 dbus_err = dbus_message_get_error_name(reply);
548 if (dbus_err != NULL &&
550 CONNMAN_AGENT_INTERFACE ".Error.Retry") == 0)
554 report_error->callback(report_error->service, retry,
555 report_error->user_data);
556 connman_service_unref(report_error->service);
557 g_free(report_error);
558 dbus_message_unref(reply);
561 int __connman_agent_report_error(struct connman_service *service,
563 report_error_cb_t callback, void *user_data)
565 DBusMessage *message;
566 DBusMessageIter iter;
568 struct report_error_data *report_error;
569 DBusPendingCall *call;
571 if (service == NULL || agent_path == NULL || error == NULL ||
575 message = dbus_message_new_method_call(agent_sender, agent_path,
576 CONNMAN_AGENT_INTERFACE,
581 dbus_message_iter_init_append(message, &iter);
583 path = __connman_service_get_path(service);
584 dbus_message_iter_append_basic(&iter,
585 DBUS_TYPE_OBJECT_PATH, &path);
586 dbus_message_iter_append_basic(&iter,
587 DBUS_TYPE_STRING, &error);
589 report_error = g_try_new0(struct report_error_data, 1);
590 if (report_error == NULL) {
591 dbus_message_unref(message);
595 if (dbus_connection_send_with_reply(connection, message,
596 &call, -1) == FALSE) {
597 dbus_message_unref(message);
598 g_free(report_error);
603 dbus_message_unref(message);
604 g_free(report_error);
608 report_error->service = connman_service_ref(service);
609 report_error->callback = callback;
610 report_error->user_data = user_data;
611 dbus_pending_call_set_notify(call, report_error_reply,
613 dbus_message_unref(message);
618 int __connman_agent_init(void)
622 connection = connman_dbus_get_connection();
623 if (connection == NULL)
629 void __connman_agent_cleanup(void)
631 DBusMessage *message;
635 if (connection == NULL)
639 g_dbus_remove_watch(connection, agent_watch);
641 if (agent_path == NULL)
644 message = dbus_message_new_method_call(agent_sender, agent_path,
645 CONNMAN_AGENT_INTERFACE, "Release");
649 dbus_message_set_no_reply(message, TRUE);
651 g_dbus_send_message(connection, message);
655 dbus_connection_unref(connection);