agent: Rewrite how PreviousPassphrase field is handled
[platform/upstream/connman.git] / src / agent.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <gdbus.h>
31
32 #include "connman.h"
33
34 static DBusConnection *connection = NULL;
35 static guint agent_watch = 0;
36 static gchar *agent_path = NULL;
37 static gchar *agent_sender = NULL;
38
39 static void agent_free(void)
40 {
41         agent_watch = 0;
42
43         g_free(agent_sender);
44         agent_sender = NULL;
45
46         g_free(agent_path);
47         agent_path = NULL;
48 }
49
50 static void agent_disconnect(DBusConnection *connection, void *data)
51 {
52         DBG("data %p", data);
53
54         agent_free();
55 }
56
57 int __connman_agent_register(const char *sender, const char *path)
58 {
59         DBG("sender %s path %s", sender, path);
60
61         if (agent_path != NULL)
62                 return -EEXIST;
63
64         agent_sender = g_strdup(sender);
65         agent_path = g_strdup(path);
66
67         agent_watch = g_dbus_add_disconnect_watch(connection, sender,
68                                                 agent_disconnect, NULL, NULL);
69
70         return 0;
71 }
72
73 int __connman_agent_unregister(const char *sender, const char *path)
74 {
75         DBG("sender %s path %s", sender, path);
76
77         if (agent_path == NULL)
78                 return -ESRCH;
79
80         if (agent_watch > 0)
81                 g_dbus_remove_watch(connection, agent_watch);
82
83         agent_free();
84
85         return 0;
86 }
87
88 static connman_bool_t check_reply_has_dict(DBusMessage *reply)
89 {
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;
95
96         if (dbus_message_has_signature(reply, signature) == TRUE)
97                 return TRUE;
98
99         connman_warn("Reply %s to %s from %s has wrong signature %s",
100                         signature,
101                         dbus_message_get_interface(reply),
102                         dbus_message_get_sender(reply),
103                         dbus_message_get_signature(reply));
104
105         return FALSE;
106 }
107
108 struct request_input_reply {
109         struct connman_service *service;
110         authentication_cb_t callback;
111         void *user_data;
112 };
113
114 static void request_input_passphrase_reply(DBusPendingCall *call, void *user_data)
115 {
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;
122         char *wpspin = NULL;
123         char *key;
124         char *name = NULL;
125         int name_len = 0;
126         DBusMessageIter iter, dict;
127         DBusMessage *reply = dbus_pending_call_steal_reply(call);
128
129         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
130                 error = dbus_message_get_error_name(reply);
131                 goto done;
132         }
133
134         if (check_reply_has_dict(reply) == FALSE)
135                 goto done;
136
137         values_received = TRUE;
138
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;
143
144                 dbus_message_iter_recurse(&dict, &entry);
145                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
146                         break;
147
148                 dbus_message_iter_get_basic(&entry, &key);
149
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)
154                                 break;
155                         dbus_message_iter_recurse(&entry, &value);
156                         dbus_message_iter_get_basic(&value, &identity);
157
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)
161                                 break;
162                         dbus_message_iter_recurse(&entry, &value);
163                         dbus_message_iter_get_basic(&value, &passphrase);
164
165                 } else if (g_str_equal(key, "WPS")) {
166                         wps = TRUE;
167
168                         dbus_message_iter_next(&entry);
169                         if (dbus_message_iter_get_arg_type(&entry)
170                                                         != DBUS_TYPE_VARIANT)
171                                 break;
172                         dbus_message_iter_recurse(&entry, &value);
173                         dbus_message_iter_get_basic(&value, &wpspin);
174                         break;
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)
179                                 break;
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)
187                                 break;
188                         dbus_message_iter_recurse(&entry, &value);
189                         if (dbus_message_iter_get_arg_type(&value)
190                                                         != DBUS_TYPE_VARIANT)
191                                 break;
192                         if (dbus_message_iter_get_element_type(&value)
193                                                         != DBUS_TYPE_VARIANT)
194                                 break;
195                         dbus_message_iter_get_fixed_array(&value, &name,
196                                                         &name_len);
197                 }
198                 dbus_message_iter_next(&dict);
199         }
200
201 done:
202         passphrase_reply->callback(passphrase_reply->service, values_received,
203                                 name, name_len,
204                                 identity, passphrase,
205                                 wps, wpspin, error,
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);
211 }
212
213 static void request_input_append_alternates(DBusMessageIter *iter,
214                                                         void *user_data)
215 {
216         const char *str = user_data;
217         char **alternates, **alternative;
218
219         if (str == NULL)
220                 return;
221
222         alternates = g_strsplit(str, ",", 0);
223         if (alternates == NULL)
224                 return;
225
226         for (alternative = alternates; *alternative != NULL; alternative++)
227                 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
228                                                                 alternative);
229
230         g_strfreev(alternates);
231 }
232
233 static void request_input_append_identity(DBusMessageIter *iter,
234                                                         void *user_data)
235 {
236         char *str = "string";
237
238         connman_dbus_dict_append_basic(iter, "Type",
239                                 DBUS_TYPE_STRING, &str);
240         str = "mandatory";
241         connman_dbus_dict_append_basic(iter, "Requirement",
242                                 DBUS_TYPE_STRING, &str);
243 }
244
245 static void request_input_append_passphrase(DBusMessageIter *iter,
246                                                         void *user_data)
247 {
248         struct connman_service *service = user_data;
249         char *value;
250         const char *phase2;
251
252         switch (__connman_service_get_security(service)) {
253         case CONNMAN_SERVICE_SECURITY_WEP:
254                 value = "wep";
255                 break;
256         case CONNMAN_SERVICE_SECURITY_PSK:
257                 value = "psk";
258                 break;
259         case CONNMAN_SERVICE_SECURITY_8021X:
260                 phase2 = __connman_service_get_phase2(service);
261
262                 if (phase2 != NULL && (
263                                 g_str_has_suffix(phase2, "GTC") == TRUE ||
264                                 g_str_has_suffix(phase2, "OTP") == TRUE))
265                         value = "response";
266                 else
267                         value = "passphrase";
268
269                 break;
270         default:
271                 value = "string";
272                 break;
273         }
274         connman_dbus_dict_append_basic(iter, "Type",
275                                 DBUS_TYPE_STRING, &value);
276         value = "mandatory";
277         connman_dbus_dict_append_basic(iter, "Requirement",
278                                 DBUS_TYPE_STRING, &value);
279
280         if (__connman_service_wps_enabled(service) == TRUE) {
281                 connman_dbus_dict_append_array(iter, "Alternates",
282                                         DBUS_TYPE_STRING,
283                                         request_input_append_alternates,
284                                         "WPS");
285         }
286 }
287
288 static void request_input_append_wps(DBusMessageIter *iter, void *user_data)
289 {
290         const char *str = "wpspin";
291
292         connman_dbus_dict_append_basic(iter, "Type",
293                                 DBUS_TYPE_STRING, &str);
294         str = "alternate";
295         connman_dbus_dict_append_basic(iter, "Requirement",
296                                 DBUS_TYPE_STRING, &str);
297 }
298
299 static void request_input_append_name(DBusMessageIter *iter, void *user_data)
300 {
301         const char *str = "string";
302
303         connman_dbus_dict_append_basic(iter, "Type",
304                                 DBUS_TYPE_STRING, &str);
305         str = "mandatory";
306         connman_dbus_dict_append_basic(iter, "Requirement",
307                                 DBUS_TYPE_STRING, &str);
308         connman_dbus_dict_append_array(iter, "Alternates",
309                                 DBUS_TYPE_STRING,
310                                 request_input_append_alternates,
311                                 "SSID");
312 }
313
314 static void request_input_append_ssid(DBusMessageIter *iter, void *user_data)
315 {
316         const char *str = "ssid";
317
318         connman_dbus_dict_append_basic(iter, "Type",
319                                 DBUS_TYPE_STRING, &str);
320         str = "alternate";
321         connman_dbus_dict_append_basic(iter, "Requirement",
322                                 DBUS_TYPE_STRING, &str);
323 }
324
325 static void request_input_append_password(DBusMessageIter *iter,
326                                                         void *user_data)
327 {
328         char *str = "passphrase";
329
330         connman_dbus_dict_append_basic(iter, "Type",
331                                 DBUS_TYPE_STRING, &str);
332         str = "mandatory";
333         connman_dbus_dict_append_basic(iter, "Requirement",
334                                 DBUS_TYPE_STRING, &str);
335 }
336
337 struct previous_passphrase_data {
338         const char *passphrase;
339         const char *type;
340 };
341
342 static void request_input_append_previouspassphrase(DBusMessageIter *iter,
343                                                         void *user_data)
344 {
345         struct previous_passphrase_data *data = user_data;
346         const char *requirement = "informational";
347
348         connman_dbus_dict_append_basic(iter, "Type",
349                                 DBUS_TYPE_STRING, &data->type);
350
351         connman_dbus_dict_append_basic(iter, "Requirement",
352                                 DBUS_TYPE_STRING, &requirement);
353
354         connman_dbus_dict_append_basic(iter, "Value",
355                                 DBUS_TYPE_STRING, &data->passphrase);
356 }
357
358 static void previous_passphrase_handler(DBusMessageIter *iter,
359                                         struct connman_service *service)
360 {
361         enum connman_service_security security;
362         struct previous_passphrase_data data;
363
364         data.passphrase = __connman_service_get_passphrase(service);
365         if (data.passphrase == NULL)
366                 return;
367
368         security = __connman_service_get_security(service);
369         switch (security) {
370         case CONNMAN_SERVICE_SECURITY_WEP:
371                 data.type = "wep";
372                 break;
373         case CONNMAN_SERVICE_SECURITY_PSK:
374                 data.type  = "psk";
375                 break;
376         /*
377          * This should never happen: no passphrase is set if security is not
378          * one of the above. */
379         default:
380                 break;
381         }
382
383         connman_dbus_dict_append_dict(iter, "PreviousPassphrase",
384                         request_input_append_previouspassphrase, &data);
385 }
386
387 static void request_input_login_reply(DBusPendingCall *call, void *user_data)
388 {
389         struct request_input_reply *username_password_reply = user_data;
390         const char *error = NULL;
391         connman_bool_t values_received = FALSE;
392         char *username = NULL;
393         char *password = NULL;
394         char *key;
395         DBusMessageIter iter, dict;
396         DBusMessage *reply = dbus_pending_call_steal_reply(call);
397
398         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
399                 error = dbus_message_get_error_name(reply);
400                 goto done;
401         }
402
403         if (check_reply_has_dict(reply) == FALSE)
404                 goto done;
405
406         values_received = TRUE;
407
408         dbus_message_iter_init(reply, &iter);
409         dbus_message_iter_recurse(&iter, &dict);
410         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
411                 DBusMessageIter entry, value;
412
413                 dbus_message_iter_recurse(&dict, &entry);
414                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
415                         break;
416
417                 dbus_message_iter_get_basic(&entry, &key);
418
419                 if (g_str_equal(key, "Username")) {
420                         dbus_message_iter_next(&entry);
421                         if (dbus_message_iter_get_arg_type(&entry)
422                                                         != DBUS_TYPE_VARIANT)
423                                 break;
424                         dbus_message_iter_recurse(&entry, &value);
425                         dbus_message_iter_get_basic(&value, &username);
426
427                 } else if (g_str_equal(key, "Password")) {
428                         dbus_message_iter_next(&entry);
429                         if (dbus_message_iter_get_arg_type(&entry) !=
430                                                         DBUS_TYPE_VARIANT)
431                                 break;
432                         dbus_message_iter_recurse(&entry, &value);
433                         dbus_message_iter_get_basic(&value, &password);
434                 }
435
436                 dbus_message_iter_next(&dict);
437         }
438
439 done:
440         username_password_reply->callback(username_password_reply->service,
441                                         values_received, NULL, 0,
442                                         username, password,
443                                         FALSE, NULL, error,
444                                         username_password_reply->user_data);
445         connman_service_unref(username_password_reply->service);
446         dbus_message_unref(reply);
447         g_free(username_password_reply);
448 }
449
450 int __connman_agent_request_passphrase_input(struct connman_service *service,
451                                 authentication_cb_t callback, void *user_data)
452 {
453         DBusMessage *message;
454         const char *path;
455         DBusMessageIter iter;
456         DBusMessageIter dict;
457         DBusPendingCall *call;
458         struct request_input_reply *passphrase_reply;
459
460         if (service == NULL || agent_path == NULL || callback == NULL)
461                 return -ESRCH;
462
463         message = dbus_message_new_method_call(agent_sender, agent_path,
464                                         CONNMAN_AGENT_INTERFACE,
465                                         "RequestInput");
466         if (message == NULL)
467                 return -ENOMEM;
468
469         dbus_message_iter_init_append(message, &iter);
470
471         path = __connman_service_get_path(service);
472         dbus_message_iter_append_basic(&iter,
473                                 DBUS_TYPE_OBJECT_PATH, &path);
474
475         connman_dbus_dict_open(&iter, &dict);
476
477         if (__connman_service_is_hidden(service)) {
478                 connman_dbus_dict_append_dict(&dict, "Name",
479                                         request_input_append_name, NULL);
480                 connman_dbus_dict_append_dict(&dict, "SSID",
481                                         request_input_append_ssid, NULL);
482         }
483
484         if (__connman_service_get_security(service) ==
485                         CONNMAN_SERVICE_SECURITY_8021X) {
486                 connman_dbus_dict_append_dict(&dict, "Identity",
487                                         request_input_append_identity, service);
488         }
489
490         if (__connman_service_get_security(service) !=
491                         CONNMAN_SERVICE_SECURITY_NONE) {
492                 connman_dbus_dict_append_dict(&dict, "Passphrase",
493                                         request_input_append_passphrase, service);
494
495                 previous_passphrase_handler(&dict, service);
496         }
497
498         if (__connman_service_wps_enabled(service) == TRUE) {
499             connman_dbus_dict_append_dict(&dict, "WPS",
500                                 request_input_append_wps, NULL);
501         }
502
503         connman_dbus_dict_close(&iter, &dict);
504
505         passphrase_reply = g_try_new0(struct request_input_reply, 1);
506         if (passphrase_reply == NULL) {
507                 dbus_message_unref(message);
508                 return -ENOMEM;
509         }
510
511         if (dbus_connection_send_with_reply(connection, message, &call,
512                                         connman_timeout_input_request())
513                         == FALSE) {
514                 dbus_message_unref(message);
515                 g_free(passphrase_reply);
516                 return -ESRCH;
517         }
518
519         if (call == NULL) {
520                 dbus_message_unref(message);
521                 g_free(passphrase_reply);
522                 return -ESRCH;
523         }
524
525         passphrase_reply->service = connman_service_ref(service);
526         passphrase_reply->callback = callback;
527         passphrase_reply->user_data = user_data;
528
529         dbus_pending_call_set_notify(call, request_input_passphrase_reply,
530                                 passphrase_reply, NULL);
531
532         dbus_message_unref(message);
533
534         return -EINPROGRESS;
535 }
536
537 int __connman_agent_request_login_input(struct connman_service *service,
538                                 authentication_cb_t callback, void *user_data)
539 {
540         DBusMessage *message;
541         const char *path;
542         DBusMessageIter iter;
543         DBusMessageIter dict;
544         DBusPendingCall *call;
545         struct request_input_reply *username_password_reply;
546
547         if (service == NULL || agent_path == NULL || callback == NULL)
548                 return -ESRCH;
549
550         message = dbus_message_new_method_call(agent_sender, agent_path,
551                                         CONNMAN_AGENT_INTERFACE,
552                                         "RequestInput");
553         if (message == NULL)
554                 return -ENOMEM;
555
556         dbus_message_iter_init_append(message, &iter);
557
558         path = __connman_service_get_path(service);
559         dbus_message_iter_append_basic(&iter,
560                                 DBUS_TYPE_OBJECT_PATH, &path);
561
562         connman_dbus_dict_open(&iter, &dict);
563
564         connman_dbus_dict_append_dict(&dict, "Username",
565                                 request_input_append_identity, service);
566
567         connman_dbus_dict_append_dict(&dict, "Password",
568                                 request_input_append_password, service);
569
570         connman_dbus_dict_close(&iter, &dict);
571
572         username_password_reply = g_try_new0(struct request_input_reply, 1);
573         if (username_password_reply == NULL) {
574                 dbus_message_unref(message);
575                 return -ENOMEM;
576         }
577
578         if (dbus_connection_send_with_reply(connection, message, &call,
579                                         connman_timeout_input_request())
580                         == FALSE) {
581                 dbus_message_unref(message);
582                 g_free(username_password_reply);
583                 return -ESRCH;
584         }
585
586         if (call == NULL) {
587                 dbus_message_unref(message);
588                 g_free(username_password_reply);
589                 return -ESRCH;
590         }
591
592         username_password_reply->service = connman_service_ref(service);
593         username_password_reply->callback = callback;
594         username_password_reply->user_data = user_data;
595
596         dbus_pending_call_set_notify(call, request_input_login_reply,
597                                                 username_password_reply, NULL);
598
599         dbus_message_unref(message);
600
601         return -EINPROGRESS;
602 }
603
604 struct request_browser_reply_data {
605         struct connman_service *service;
606         browser_authentication_cb_t callback;
607         void *user_data;
608 };
609
610 static void request_browser_reply(DBusPendingCall *call, void *user_data)
611 {
612         struct request_browser_reply_data *browser_reply_data = user_data;
613         DBusMessage *reply = dbus_pending_call_steal_reply(call);
614         connman_bool_t result = FALSE;
615         const char *error = NULL;
616
617         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
618                 error = dbus_message_get_error_name(reply);
619                 goto done;
620         }
621
622         result = TRUE;
623
624 done:
625         browser_reply_data->callback(browser_reply_data->service, result,
626                                         error, browser_reply_data->user_data);
627         connman_service_unref(browser_reply_data->service);
628         dbus_message_unref(reply);
629         g_free(browser_reply_data);
630 }
631
632 int __connman_agent_request_browser(struct connman_service *service,
633                                 browser_authentication_cb_t callback,
634                                 const char *url, void *user_data)
635 {
636         struct request_browser_reply_data *browser_reply_data;
637         DBusPendingCall *call;
638         DBusMessage *message;
639         DBusMessageIter iter;
640         const char *path;
641
642         if (service == NULL || agent_path == NULL || callback == NULL)
643                 return -ESRCH;
644
645         if (url == NULL)
646                 url = "";
647
648         message = dbus_message_new_method_call(agent_sender, agent_path,
649                                         CONNMAN_AGENT_INTERFACE,
650                                         "RequestBrowser");
651         if (message == NULL)
652                 return -ENOMEM;
653
654         dbus_message_iter_init_append(message, &iter);
655
656         path = __connman_service_get_path(service);
657         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &path);
658
659         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &url);
660
661         browser_reply_data = g_try_new0(struct request_browser_reply_data, 1);
662         if (browser_reply_data == NULL) {
663                 dbus_message_unref(message);
664                 return -ENOMEM;
665         }
666
667         if (dbus_connection_send_with_reply(connection, message, &call,
668                                         connman_timeout_browser_launch())
669                         == FALSE) {
670                 dbus_message_unref(message);
671                 g_free(browser_reply_data);
672                 return -ESRCH;
673         }
674
675         if (call == NULL) {
676                 dbus_message_unref(message);
677                 g_free(browser_reply_data);
678                 return -ESRCH;
679         }
680
681         browser_reply_data->service = connman_service_ref(service);
682         browser_reply_data->callback = callback;
683         browser_reply_data->user_data = user_data;
684
685         dbus_pending_call_set_notify(call, request_browser_reply,
686                                                 browser_reply_data, NULL);
687
688         dbus_message_unref(message);
689
690         return -EINPROGRESS;
691 }
692
693 struct report_error_data {
694         struct connman_service *service;
695         report_error_cb_t callback;
696         void *user_data;
697 };
698
699 static void report_error_reply(DBusPendingCall *call, void *user_data)
700 {
701         struct report_error_data *report_error = user_data;
702         DBusMessage *reply = dbus_pending_call_steal_reply(call);
703         gboolean retry = FALSE;
704         const char *dbus_err;
705
706         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
707                 dbus_err = dbus_message_get_error_name(reply);
708                 if (dbus_err != NULL &&
709                         strcmp(dbus_err,
710                                 CONNMAN_AGENT_INTERFACE ".Error.Retry") == 0)
711                         retry = TRUE;
712         }
713
714         report_error->callback(report_error->service, retry,
715                         report_error->user_data);
716         connman_service_unref(report_error->service);
717         g_free(report_error);
718         dbus_message_unref(reply);
719         dbus_pending_call_unref(call);
720 }
721
722 int __connman_agent_report_error(struct connman_service *service,
723                                 const char *error,
724                                 report_error_cb_t callback, void *user_data)
725 {
726         DBusMessage *message;
727         DBusMessageIter iter;
728         const char *path;
729         struct report_error_data *report_error;
730         DBusPendingCall *call;
731
732         if (service == NULL || agent_path == NULL || error == NULL ||
733                 callback == NULL)
734                 return -ESRCH;
735
736         message = dbus_message_new_method_call(agent_sender, agent_path,
737                                         CONNMAN_AGENT_INTERFACE,
738                                         "ReportError");
739         if (message == NULL)
740                 return -ENOMEM;
741
742         dbus_message_iter_init_append(message, &iter);
743
744         path = __connman_service_get_path(service);
745         dbus_message_iter_append_basic(&iter,
746                                 DBUS_TYPE_OBJECT_PATH, &path);
747         dbus_message_iter_append_basic(&iter,
748                                 DBUS_TYPE_STRING, &error);
749
750         report_error = g_try_new0(struct report_error_data, 1);
751         if (report_error == NULL) {
752                 dbus_message_unref(message);
753                 return -ENOMEM;
754         }
755
756         if (dbus_connection_send_with_reply(connection, message, &call,
757                                         connman_timeout_input_request())
758                         == FALSE) {
759                 dbus_message_unref(message);
760                 g_free(report_error);
761                 return -ESRCH;
762         }
763
764         if (call == NULL) {
765                 dbus_message_unref(message);
766                 g_free(report_error);
767                 return -ESRCH;
768         }
769
770         report_error->service = connman_service_ref(service);
771         report_error->callback = callback;
772         report_error->user_data = user_data;
773         dbus_pending_call_set_notify(call, report_error_reply,
774                                 report_error, NULL);
775         dbus_message_unref(message);
776
777         return -EINPROGRESS;
778 }
779
780 int __connman_agent_init(void)
781 {
782         DBG("");
783
784         connection = connman_dbus_get_connection();
785         if (connection == NULL)
786                 return -1;
787
788         return 0;
789 }
790
791 void __connman_agent_cleanup(void)
792 {
793         DBusMessage *message;
794
795         DBG("");
796
797         if (connection == NULL)
798                 return;
799
800         if (agent_watch > 0)
801                 g_dbus_remove_watch(connection, agent_watch);
802
803         if (agent_path == NULL)
804                 return;
805
806         message = dbus_message_new_method_call(agent_sender, agent_path,
807                                         CONNMAN_AGENT_INTERFACE, "Release");
808         if (message == NULL)
809                 return;
810
811         dbus_message_set_no_reply(message, TRUE);
812
813         g_dbus_send_message(connection, message);
814
815         agent_free();
816
817         dbus_connection_unref(connection);
818 }