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,
195 identity, passphrase,
196 passphrase_reply->user_data);
197 connman_service_unref(passphrase_reply->service);
198 dbus_message_unref(reply);
199 g_free(passphrase_reply);
202 static void request_input_append_alternates(DBusMessageIter *iter,
205 const char *str = user_data;
206 char **alternates, **alternative;
211 alternates = g_strsplit(str, ",", 0);
212 if (alternates == NULL)
215 for (alternative = alternates; *alternative != NULL; alternative++)
216 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
219 g_strfreev(alternates);
222 static void request_input_append_identity(DBusMessageIter *iter,
225 char *str = "string";
227 connman_dbus_dict_append_basic(iter, "Type",
228 DBUS_TYPE_STRING, &str);
230 connman_dbus_dict_append_basic(iter, "Requirement",
231 DBUS_TYPE_STRING, &str);
234 static void request_input_append_passphrase(DBusMessageIter *iter,
237 struct connman_service *service = user_data;
241 switch (__connman_service_get_security(service)) {
242 case CONNMAN_SERVICE_SECURITY_WEP:
245 case CONNMAN_SERVICE_SECURITY_PSK:
248 case CONNMAN_SERVICE_SECURITY_8021X:
249 phase2 = __connman_service_get_phase2(service);
251 if (phase2 != NULL && (
252 g_str_has_suffix(phase2, "GTC") == TRUE ||
253 g_str_has_suffix(phase2, "OTP") == TRUE))
256 value = "passphrase";
263 connman_dbus_dict_append_basic(iter, "Type",
264 DBUS_TYPE_STRING, &value);
266 connman_dbus_dict_append_basic(iter, "Requirement",
267 DBUS_TYPE_STRING, &value);
269 if (__connman_service_wps_enabled(service) == TRUE) {
270 connman_dbus_dict_append_array(iter, "Alternates",
272 request_input_append_alternates,
277 static void request_input_append_wps(DBusMessageIter *iter, void *user_data)
279 const char *str = "wpspin";
281 connman_dbus_dict_append_basic(iter, "Type",
282 DBUS_TYPE_STRING, &str);
284 connman_dbus_dict_append_basic(iter, "Requirement",
285 DBUS_TYPE_STRING, &str);
288 static void request_input_append_name(DBusMessageIter *iter, void *user_data)
290 const char *str = "string";
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);
297 connman_dbus_dict_append_array(iter, "Alternates",
299 request_input_append_alternates,
303 static void request_input_append_ssid(DBusMessageIter *iter, void *user_data)
305 const char *str = "ssid";
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_append_password(DBusMessageIter *iter,
317 char *str = "passphrase";
319 connman_dbus_dict_append_basic(iter, "Type",
320 DBUS_TYPE_STRING, &str);
322 connman_dbus_dict_append_basic(iter, "Requirement",
323 DBUS_TYPE_STRING, &str);
326 static void request_input_login_reply(DBusPendingCall *call, void *user_data)
328 struct request_input_reply *username_password_reply = user_data;
329 char *username = NULL;
330 char *password = NULL;
332 DBusMessageIter iter, dict;
333 DBusMessage *reply = dbus_pending_call_steal_reply(call);
335 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
338 dbus_message_iter_init(reply, &iter);
339 dbus_message_iter_recurse(&iter, &dict);
340 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
341 DBusMessageIter entry, value;
343 dbus_message_iter_recurse(&dict, &entry);
344 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
347 dbus_message_iter_get_basic(&entry, &key);
349 if (g_str_equal(key, "Username")) {
350 dbus_message_iter_next(&entry);
351 if (dbus_message_iter_get_arg_type(&entry)
352 != DBUS_TYPE_VARIANT)
354 dbus_message_iter_recurse(&entry, &value);
355 dbus_message_iter_get_basic(&value, &username);
357 } else if (g_str_equal(key, "Password")) {
358 dbus_message_iter_next(&entry);
359 if (dbus_message_iter_get_arg_type(&entry) !=
362 dbus_message_iter_recurse(&entry, &value);
363 dbus_message_iter_get_basic(&value, &password);
366 dbus_message_iter_next(&dict);
370 username_password_reply->callback(username_password_reply->service,
373 username_password_reply->user_data);
374 connman_service_unref(username_password_reply->service);
375 dbus_message_unref(reply);
376 g_free(username_password_reply);
379 int __connman_agent_request_passphrase_input(struct connman_service *service,
380 authentication_cb_t callback, void *user_data)
382 DBusMessage *message;
384 DBusMessageIter iter;
385 DBusMessageIter dict;
386 DBusPendingCall *call;
387 struct request_input_reply *passphrase_reply;
389 if (service == NULL || agent_path == NULL || callback == NULL)
392 message = dbus_message_new_method_call(agent_sender, agent_path,
393 CONNMAN_AGENT_INTERFACE,
398 dbus_message_iter_init_append(message, &iter);
400 path = __connman_service_get_path(service);
401 dbus_message_iter_append_basic(&iter,
402 DBUS_TYPE_OBJECT_PATH, &path);
404 connman_dbus_dict_open(&iter, &dict);
406 if (__connman_service_is_hidden(service)) {
407 connman_dbus_dict_append_dict(&dict, "Name",
408 request_input_append_name, NULL);
409 connman_dbus_dict_append_dict(&dict, "SSID",
410 request_input_append_ssid, NULL);
413 if (__connman_service_get_security(service) ==
414 CONNMAN_SERVICE_SECURITY_8021X) {
415 connman_dbus_dict_append_dict(&dict, "Identity",
416 request_input_append_identity, service);
419 connman_dbus_dict_append_dict(&dict, "Passphrase",
420 request_input_append_passphrase, service);
422 if (__connman_service_wps_enabled(service) == TRUE) {
423 connman_dbus_dict_append_dict(&dict, "WPS",
424 request_input_append_wps, NULL);
427 connman_dbus_dict_close(&iter, &dict);
429 passphrase_reply = g_try_new0(struct request_input_reply, 1);
430 if (passphrase_reply == NULL) {
431 dbus_message_unref(message);
435 if (dbus_connection_send_with_reply(connection, message,
436 &call, -1) == FALSE) {
437 dbus_message_unref(message);
438 g_free(passphrase_reply);
443 dbus_message_unref(message);
444 g_free(passphrase_reply);
448 passphrase_reply->service = connman_service_ref(service);
449 passphrase_reply->callback = callback;
450 passphrase_reply->user_data = user_data;
452 dbus_pending_call_set_notify(call, request_input_passphrase_reply,
453 passphrase_reply, NULL);
455 dbus_message_unref(message);
460 int __connman_agent_request_login_input(struct connman_service *service,
461 authentication_cb_t callback, void *user_data)
463 DBusMessage *message;
465 DBusMessageIter iter;
466 DBusMessageIter dict;
467 DBusPendingCall *call;
468 struct request_input_reply *username_password_reply;
470 if (service == NULL || agent_path == NULL || callback == NULL)
473 message = dbus_message_new_method_call(agent_sender, agent_path,
474 CONNMAN_AGENT_INTERFACE,
479 dbus_message_iter_init_append(message, &iter);
481 path = __connman_service_get_path(service);
482 dbus_message_iter_append_basic(&iter,
483 DBUS_TYPE_OBJECT_PATH, &path);
485 connman_dbus_dict_open(&iter, &dict);
487 connman_dbus_dict_append_dict(&dict, "Username",
488 request_input_append_identity, service);
490 connman_dbus_dict_append_dict(&dict, "Password",
491 request_input_append_password, service);
493 connman_dbus_dict_close(&iter, &dict);
495 username_password_reply = g_try_new0(struct request_input_reply, 1);
496 if (username_password_reply == NULL) {
497 dbus_message_unref(message);
501 if (dbus_connection_send_with_reply(connection, message,
502 &call, -1) == FALSE) {
503 dbus_message_unref(message);
504 g_free(username_password_reply);
509 dbus_message_unref(message);
510 g_free(username_password_reply);
514 username_password_reply->service = connman_service_ref(service);
515 username_password_reply->callback = callback;
516 username_password_reply->user_data = user_data;
518 dbus_pending_call_set_notify(call, request_input_login_reply,
519 username_password_reply, NULL);
521 dbus_message_unref(message);
526 struct report_error_data {
527 struct connman_service *service;
528 report_error_cb_t callback;
532 static void report_error_reply(DBusPendingCall *call, void *user_data)
534 struct report_error_data *report_error = user_data;
535 DBusMessage *reply = dbus_pending_call_steal_reply(call);
536 gboolean retry = FALSE;
537 const char *dbus_err;
539 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
540 dbus_err = dbus_message_get_error_name(reply);
541 if (dbus_err != NULL &&
543 CONNMAN_AGENT_INTERFACE ".Error.Retry") == 0)
547 report_error->callback(report_error->service, retry,
548 report_error->user_data);
549 connman_service_unref(report_error->service);
550 g_free(report_error);
551 dbus_message_unref(reply);
554 int __connman_agent_report_error(struct connman_service *service,
556 report_error_cb_t callback, void *user_data)
558 DBusMessage *message;
559 DBusMessageIter iter;
561 struct report_error_data *report_error;
562 DBusPendingCall *call;
564 if (service == NULL || agent_path == NULL || error == NULL ||
568 message = dbus_message_new_method_call(agent_sender, agent_path,
569 CONNMAN_AGENT_INTERFACE,
574 dbus_message_iter_init_append(message, &iter);
576 path = __connman_service_get_path(service);
577 dbus_message_iter_append_basic(&iter,
578 DBUS_TYPE_OBJECT_PATH, &path);
579 dbus_message_iter_append_basic(&iter,
580 DBUS_TYPE_STRING, &error);
582 report_error = g_try_new0(struct report_error_data, 1);
583 if (report_error == NULL) {
584 dbus_message_unref(message);
588 if (dbus_connection_send_with_reply(connection, message,
589 &call, -1) == FALSE) {
590 dbus_message_unref(message);
591 g_free(report_error);
596 dbus_message_unref(message);
597 g_free(report_error);
601 report_error->service = connman_service_ref(service);
602 report_error->callback = callback;
603 report_error->user_data = user_data;
604 dbus_pending_call_set_notify(call, report_error_reply,
606 dbus_message_unref(message);
611 int __connman_agent_init(void)
615 connection = connman_dbus_get_connection();
616 if (connection == NULL)
622 void __connman_agent_cleanup(void)
624 DBusMessage *message;
628 if (connection == NULL)
632 g_dbus_remove_watch(connection, agent_watch);
634 if (agent_path == NULL)
637 message = dbus_message_new_method_call(agent_sender, agent_path,
638 CONNMAN_AGENT_INTERFACE, "Release");
642 dbus_message_set_no_reply(message, TRUE);
644 g_dbus_send_message(connection, message);
648 dbus_connection_unref(connection);