WPS: Add configurable option for processing credentials externally
[mech_eap.git] / wpa_supplicant / ctrl_iface_dbus.c
1 /*
2  * WPA Supplicant / dbus-based control interface
3  * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eloop.h"
19 #include "config.h"
20 #include "wpa_supplicant_i.h"
21 #include "wps/wps.h"
22 #include "ctrl_iface_dbus.h"
23 #include "ctrl_iface_dbus_handlers.h"
24
25 #define _DBUS_VERSION (DBUS_VERSION_MAJOR << 8 | DBUS_VERSION_MINOR)
26 #define DBUS_VER(major, minor) ((major) << 8 | (minor))
27
28 #if _DBUS_VERSION < DBUS_VER(1,1)
29 #define dbus_watch_get_unix_fd dbus_watch_get_fd
30 #endif
31
32
33 struct ctrl_iface_dbus_priv {
34         DBusConnection *con;
35         int should_dispatch;
36         struct wpa_global *global;
37
38         u32 next_objid;
39 };
40
41
42 static void process_watch(struct ctrl_iface_dbus_priv *iface,
43                           DBusWatch *watch, eloop_event_type type)
44 {
45         dbus_connection_ref(iface->con);
46
47         iface->should_dispatch = 0;
48
49         if (type == EVENT_TYPE_READ)
50                 dbus_watch_handle(watch, DBUS_WATCH_READABLE);
51         else if (type == EVENT_TYPE_WRITE)
52                 dbus_watch_handle(watch, DBUS_WATCH_WRITABLE);
53         else if (type == EVENT_TYPE_EXCEPTION)
54                 dbus_watch_handle(watch, DBUS_WATCH_ERROR);
55
56         if (iface->should_dispatch) {
57                 while (dbus_connection_get_dispatch_status(iface->con) ==
58                        DBUS_DISPATCH_DATA_REMAINS)
59                         dbus_connection_dispatch(iface->con);
60                 iface->should_dispatch = 0;
61         }
62
63         dbus_connection_unref(iface->con);
64 }
65
66
67 static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
68 {
69         process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
70 }
71
72
73 static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
74 {
75         process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
76 }
77
78
79 static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
80 {
81         process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
82 }
83
84
85 static void connection_setup_add_watch(struct ctrl_iface_dbus_priv *iface,
86                                        DBusWatch *watch)
87 {
88         unsigned int flags;
89         int fd;
90
91         if (!dbus_watch_get_enabled(watch))
92                 return;
93
94         flags = dbus_watch_get_flags(watch);
95         fd = dbus_watch_get_unix_fd(watch);
96
97         eloop_register_sock(fd, EVENT_TYPE_EXCEPTION, process_watch_exception,
98                             iface, watch);
99
100         if (flags & DBUS_WATCH_READABLE) {
101                 eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
102                                     iface, watch);
103         }
104         if (flags & DBUS_WATCH_WRITABLE) {
105                 eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
106                                     iface, watch);
107         }
108
109         dbus_watch_set_data(watch, iface, NULL);
110 }
111
112
113 static void connection_setup_remove_watch(struct ctrl_iface_dbus_priv *iface,
114                                           DBusWatch *watch)
115 {
116         unsigned int flags;
117         int fd;
118
119         flags = dbus_watch_get_flags(watch);
120         fd = dbus_watch_get_unix_fd(watch);
121
122         eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
123
124         if (flags & DBUS_WATCH_READABLE)
125                 eloop_unregister_sock(fd, EVENT_TYPE_READ);
126         if (flags & DBUS_WATCH_WRITABLE)
127                 eloop_unregister_sock(fd, EVENT_TYPE_WRITE);
128
129         dbus_watch_set_data(watch, NULL, NULL);
130 }
131
132
133 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
134 {
135         connection_setup_add_watch(data, watch);
136         return TRUE;
137 }
138
139
140 static void remove_watch(DBusWatch *watch, void *data)
141 {
142         connection_setup_remove_watch(data, watch);
143 }
144
145
146 static void watch_toggled(DBusWatch *watch, void *data)
147 {
148         if (dbus_watch_get_enabled(watch))
149                 add_watch(watch, data);
150         else
151                 remove_watch(watch, data);
152 }
153
154
155 static void process_timeout(void *eloop_ctx, void *sock_ctx)
156 {
157         DBusTimeout *timeout = sock_ctx;
158
159         dbus_timeout_handle(timeout);
160 }
161
162
163 static void connection_setup_add_timeout(struct ctrl_iface_dbus_priv *iface,
164                                          DBusTimeout *timeout)
165 {
166         if (!dbus_timeout_get_enabled(timeout))
167                 return;
168
169         eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
170                                process_timeout, iface, timeout);
171
172         dbus_timeout_set_data(timeout, iface, NULL);
173 }
174
175
176 static void connection_setup_remove_timeout(struct ctrl_iface_dbus_priv *iface,
177                                             DBusTimeout *timeout)
178 {
179         eloop_cancel_timeout(process_timeout, iface, timeout);
180         dbus_timeout_set_data(timeout, NULL, NULL);
181 }
182
183
184 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
185 {
186         if (!dbus_timeout_get_enabled(timeout))
187                 return TRUE;
188
189         connection_setup_add_timeout(data, timeout);
190
191         return TRUE;
192 }
193
194
195 static void remove_timeout(DBusTimeout *timeout, void *data)
196 {
197         connection_setup_remove_timeout(data, timeout);
198 }
199
200
201 static void timeout_toggled(DBusTimeout *timeout, void *data)
202 {
203         if (dbus_timeout_get_enabled(timeout))
204                 add_timeout(timeout, data);
205         else
206                 remove_timeout(timeout, data);
207 }
208
209
210 static void process_wakeup_main(int sig, void *eloop_ctx, void *signal_ctx)
211 {
212         struct ctrl_iface_dbus_priv *iface = signal_ctx;
213
214         if (sig != SIGPOLL || !iface->con)
215                 return;
216
217         if (dbus_connection_get_dispatch_status(iface->con) !=
218             DBUS_DISPATCH_DATA_REMAINS)
219                 return;
220
221         /* Only dispatch once - we do not want to starve other events */
222         dbus_connection_ref(iface->con);
223         dbus_connection_dispatch(iface->con);
224         dbus_connection_unref(iface->con);
225 }
226
227
228 /**
229  * wakeup_main - Attempt to wake our mainloop up
230  * @data: dbus control interface private data
231  *
232  * Try to wake up the main eloop so it will process
233  * dbus events that may have happened.
234  */
235 static void wakeup_main(void *data)
236 {
237         struct ctrl_iface_dbus_priv *iface = data;
238
239         /* Use SIGPOLL to break out of the eloop select() */
240         raise(SIGPOLL);
241         iface->should_dispatch = 1;
242 }
243
244
245 /**
246  * connection_setup_wakeup_main - Tell dbus about our wakeup_main function
247  * @iface: dbus control interface private data
248  * Returns: 0 on success, -1 on failure
249  *
250  * Register our wakeup_main handler with dbus
251  */
252 static int connection_setup_wakeup_main(struct ctrl_iface_dbus_priv *iface)
253 {
254         if (eloop_register_signal(SIGPOLL, process_wakeup_main, iface))
255                 return -1;
256
257         dbus_connection_set_wakeup_main_function(iface->con, wakeup_main,
258                                                  iface, NULL);
259
260         return 0;
261 }
262
263
264 /**
265  * wpa_supplicant_dbus_next_objid - Return next available object id
266  * @iface: dbus control interface private data
267  * Returns: Object id
268  */
269 u32 wpa_supplicant_dbus_next_objid (struct ctrl_iface_dbus_priv *iface)
270 {
271         return iface->next_objid++;
272 }
273
274
275 /**
276  * wpas_dbus_decompose_object_path - Decompose an interface object path into parts
277  * @path: The dbus object path
278  * @network: (out) the configured network this object path refers to, if any
279  * @bssid: (out) the scanned bssid this object path refers to, if any
280  * Returns: The object path of the network interface this path refers to
281  *
282  * For a given object path, decomposes the object path into object id, network,
283  * and BSSID parts, if those parts exist.
284  */
285 char * wpas_dbus_decompose_object_path(const char *path, char **network,
286                                        char **bssid)
287 {
288         const unsigned int dev_path_prefix_len =
289                 strlen(WPAS_DBUS_PATH_INTERFACES "/");
290         char *obj_path_only;
291         char *next_sep;
292
293         /* Be a bit paranoid about path */
294         if (!path || strncmp(path, WPAS_DBUS_PATH_INTERFACES "/",
295                              dev_path_prefix_len))
296                 return NULL;
297
298         /* Ensure there's something at the end of the path */
299         if ((path + dev_path_prefix_len)[0] == '\0')
300                 return NULL;
301
302         obj_path_only = strdup(path);
303         if (obj_path_only == NULL)
304                 return NULL;
305
306         next_sep = strchr(obj_path_only + dev_path_prefix_len, '/');
307         if (next_sep != NULL) {
308                 const char *net_part = strstr(next_sep,
309                                               WPAS_DBUS_NETWORKS_PART "/");
310                 const char *bssid_part = strstr(next_sep,
311                                                 WPAS_DBUS_BSSIDS_PART "/");
312
313                 if (network && net_part) {
314                         /* Deal with a request for a configured network */
315                         const char *net_name = net_part +
316                                 strlen(WPAS_DBUS_NETWORKS_PART "/");
317                         *network = NULL;
318                         if (strlen(net_name))
319                                 *network = strdup(net_name);
320                 } else if (bssid && bssid_part) {
321                         /* Deal with a request for a scanned BSSID */
322                         const char *bssid_name = bssid_part +
323                                 strlen(WPAS_DBUS_BSSIDS_PART "/");
324                         if (strlen(bssid_name))
325                                 *bssid = strdup(bssid_name);
326                         else
327                                 *bssid = NULL;
328                 }
329
330                 /* Cut off interface object path before "/" */
331                 *next_sep = '\0';
332         }
333
334         return obj_path_only;
335 }
336
337
338 /**
339  * wpas_dbus_new_invalid_iface_error - Return a new invalid interface error message
340  * @message: Pointer to incoming dbus message this error refers to
341  * Returns: A dbus error message
342  *
343  * Convenience function to create and return an invalid interface error
344  */
345 DBusMessage * wpas_dbus_new_invalid_iface_error(DBusMessage *message)
346 {
347         return dbus_message_new_error(message, WPAS_ERROR_INVALID_IFACE,
348                                       "wpa_supplicant knows nothing about "
349                                       "this interface.");
350 }
351
352
353 /**
354  * wpas_dbus_new_invalid_network_error - Return a new invalid network error message
355  * @message: Pointer to incoming dbus message this error refers to
356  * Returns: a dbus error message
357  *
358  * Convenience function to create and return an invalid network error
359  */
360 DBusMessage * wpas_dbus_new_invalid_network_error(DBusMessage *message)
361 {
362         return dbus_message_new_error(message, WPAS_ERROR_INVALID_NETWORK,
363                                       "The requested network does not exist.");
364 }
365
366
367 /**
368  * wpas_dbus_new_invalid_bssid_error - Return a new invalid bssid error message
369  * @message: Pointer to incoming dbus message this error refers to
370  * Returns: a dbus error message
371  *
372  * Convenience function to create and return an invalid bssid error
373  */
374 static DBusMessage * wpas_dbus_new_invalid_bssid_error(DBusMessage *message)
375 {
376         return dbus_message_new_error(message, WPAS_ERROR_INVALID_BSSID,
377                                       "The BSSID requested was invalid.");
378 }
379
380
381 /**
382  * wpas_dispatch_network_method - dispatch messages for configured networks
383  * @message: the incoming dbus message
384  * @wpa_s: a network interface's data
385  * @network_id: id of the configured network we're interested in
386  * Returns: a reply dbus message, or a dbus error message
387  *
388  * This function dispatches all incoming dbus messages for configured networks.
389  */
390 static DBusMessage * wpas_dispatch_network_method(DBusMessage *message,
391                                                   struct wpa_supplicant *wpa_s,
392                                                   int network_id)
393 {
394         DBusMessage *reply = NULL;
395         const char *method = dbus_message_get_member(message);
396         struct wpa_ssid *ssid;
397
398         ssid = wpa_config_get_network(wpa_s->conf, network_id);
399         if (ssid == NULL)
400                 return wpas_dbus_new_invalid_network_error(message);
401
402         if (!strcmp(method, "set"))
403                 reply = wpas_dbus_iface_set_network(message, wpa_s, ssid);
404         else if (!strcmp(method, "enable"))
405                 reply = wpas_dbus_iface_enable_network(message, wpa_s, ssid);
406         else if (!strcmp(method, "disable"))
407                 reply = wpas_dbus_iface_disable_network(message, wpa_s, ssid);
408
409         return reply;
410 }
411
412
413 /**
414  * wpas_dispatch_bssid_method - dispatch messages for scanned networks
415  * @message: the incoming dbus message
416  * @wpa_s: a network interface's data
417  * @bssid: bssid of the scanned network we're interested in
418  * Returns: a reply dbus message, or a dbus error message
419  *
420  * This function dispatches all incoming dbus messages for scanned networks.
421  */
422 static DBusMessage * wpas_dispatch_bssid_method(DBusMessage *message,
423                                                 struct wpa_supplicant *wpa_s,
424                                                 const char *bssid)
425 {
426         DBusMessage *reply = NULL;
427         const char *method = dbus_message_get_member(message);
428         struct wpa_scan_res *res = NULL;
429         size_t i;
430
431         /* Ensure we actually have scan data */
432         if (wpa_s->scan_res == NULL &&
433             wpa_supplicant_get_scan_results(wpa_s) < 0) {
434                 reply = wpas_dbus_new_invalid_bssid_error(message);
435                 goto out;
436         }
437
438         /* Find the bssid's scan data */
439         for (i = 0; i < wpa_s->scan_res->num; i++) {
440                 struct wpa_scan_res *search_res = wpa_s->scan_res->res[i];
441                 char mac_str[18];
442
443                 memset(mac_str, 0, sizeof(mac_str));
444                 snprintf(mac_str, sizeof(mac_str) - 1, WPAS_DBUS_BSSID_FORMAT,
445                          MAC2STR(search_res->bssid));
446                 if (!strcmp(bssid, mac_str)) {
447                         res = search_res;
448                         break;
449                 }
450         }
451
452         if (!res) {
453                 reply = wpas_dbus_new_invalid_bssid_error(message);
454                 goto out;
455         }
456
457         /* Dispatch the method call against the scanned bssid */
458         if (!strcmp(method, "properties"))
459                 reply = wpas_dbus_bssid_properties(message, wpa_s, res);
460
461 out:
462         return reply;
463 }
464
465
466 /**
467  * wpas_iface_message_handler - Dispatch messages for interfaces or networks
468  * @connection: Connection to the system message bus
469  * @message: An incoming dbus message
470  * @user_data: A pointer to a dbus control interface data structure
471  * Returns: Whether or not the message was handled
472  *
473  * This function dispatches all incoming dbus messages for network interfaces,
474  * or objects owned by them, such as scanned BSSIDs and configured networks.
475  */
476 static DBusHandlerResult wpas_iface_message_handler(DBusConnection *connection,
477                                                     DBusMessage *message,
478                                                     void *user_data)
479 {
480         struct wpa_supplicant *wpa_s = user_data;
481         const char *method = dbus_message_get_member(message);
482         const char *path = dbus_message_get_path(message);
483         const char *msg_interface = dbus_message_get_interface(message);
484         char *iface_obj_path = NULL;
485         char *network = NULL;
486         char *bssid = NULL;
487         DBusMessage *reply = NULL;
488
489         /* Caller must specify a message interface */
490         if (!msg_interface)
491                 goto out;
492
493         iface_obj_path = wpas_dbus_decompose_object_path(path, &network,
494                                                          &bssid);
495         if (iface_obj_path == NULL) {
496                 reply = wpas_dbus_new_invalid_iface_error(message);
497                 goto out;
498         }
499
500         /* Make sure the message's object path actually refers to the
501          * wpa_supplicant structure it's supposed to (which is wpa_s)
502          */
503         if (wpa_supplicant_get_iface_by_dbus_path(wpa_s->global,
504                                                   iface_obj_path) != wpa_s) {
505                 reply = wpas_dbus_new_invalid_iface_error(message);
506                 goto out;
507         }
508
509         if (network && !strcmp(msg_interface, WPAS_DBUS_IFACE_NETWORK)) {
510                 /* A method for one of this interface's configured networks */
511                 int nid = strtoul(network, NULL, 10);
512                 if (errno != EINVAL)
513                         reply = wpas_dispatch_network_method(message, wpa_s,
514                                                              nid);
515                 else
516                         reply = wpas_dbus_new_invalid_network_error(message);
517         } else if (bssid && !strcmp(msg_interface, WPAS_DBUS_IFACE_BSSID)) {
518                 /* A method for one of this interface's scanned BSSIDs */
519                 reply = wpas_dispatch_bssid_method(message, wpa_s, bssid);
520         } else if (!strcmp(msg_interface, WPAS_DBUS_IFACE_INTERFACE)) {
521                 /* A method for an interface only. */
522                 if (!strcmp(method, "scan"))
523                         reply = wpas_dbus_iface_scan(message, wpa_s);
524                 else if (!strcmp(method, "scanResults"))
525                         reply = wpas_dbus_iface_scan_results(message, wpa_s);
526                 else if (!strcmp(method, "addNetwork"))
527                         reply = wpas_dbus_iface_add_network(message, wpa_s);
528                 else if (!strcmp(method, "removeNetwork"))
529                         reply = wpas_dbus_iface_remove_network(message, wpa_s);
530                 else if (!strcmp(method, "selectNetwork"))
531                         reply = wpas_dbus_iface_select_network(message, wpa_s);
532                 else if (!strcmp(method, "capabilities"))
533                         reply = wpas_dbus_iface_capabilities(message, wpa_s);
534                 else if (!strcmp(method, "disconnect"))
535                         reply = wpas_dbus_iface_disconnect(message, wpa_s);
536                 else if (!strcmp(method, "setAPScan"))
537                         reply = wpas_dbus_iface_set_ap_scan(message, wpa_s);
538                 else if (!strcmp(method, "setSmartcardModules"))
539                         reply = wpas_dbus_iface_set_smartcard_modules(message,
540                                                                       wpa_s);
541                 else if (!strcmp(method, "state"))
542                         reply = wpas_dbus_iface_get_state(message, wpa_s);
543                 else if (!strcmp(method, "setBlobs"))
544                         reply = wpas_dbus_iface_set_blobs(message, wpa_s);
545                 else if (!strcmp(method, "removeBlobs"))
546                         reply = wpas_dbus_iface_remove_blobs(message, wpa_s);
547         }
548
549         /* If the message was handled, send back the reply */
550         if (reply) {
551                 dbus_connection_send(connection, reply, NULL);
552                 dbus_message_unref(reply);
553         }
554
555 out:
556         free(iface_obj_path);
557         free(network);
558         free(bssid);
559         return reply ? DBUS_HANDLER_RESULT_HANDLED :
560                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
561 }
562
563
564 /**
565  * wpas_message_handler - dispatch incoming dbus messages
566  * @connection: connection to the system message bus
567  * @message: an incoming dbus message
568  * @user_data: a pointer to a dbus control interface data structure
569  * Returns: whether or not the message was handled
570  *
571  * This function dispatches all incoming dbus messages to the correct
572  * handlers, depending on what the message's target object path is,
573  * and what the method call is.
574  */
575 static DBusHandlerResult wpas_message_handler(DBusConnection *connection,
576         DBusMessage *message, void *user_data)
577 {
578         struct ctrl_iface_dbus_priv *ctrl_iface = user_data;
579         const char *method;
580         const char *path;
581         const char *msg_interface;
582         DBusMessage *reply = NULL;
583
584         method = dbus_message_get_member(message);
585         path = dbus_message_get_path(message);
586         msg_interface = dbus_message_get_interface(message);
587         if (!method || !path || !ctrl_iface || !msg_interface)
588                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
589
590         /* Validate the method interface */
591         if (strcmp(msg_interface, WPAS_DBUS_INTERFACE) != 0)
592                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
593
594         if (!strcmp(path, WPAS_DBUS_PATH)) {
595                 /* dispatch methods against our global dbus interface here */
596                 if (!strcmp(method, "addInterface")) {
597                         reply = wpas_dbus_global_add_interface(
598                                 message, ctrl_iface->global);
599                 } else if (!strcmp(method, "removeInterface")) {
600                         reply = wpas_dbus_global_remove_interface(
601                                 message, ctrl_iface->global);
602                 } else if (!strcmp(method, "getInterface")) {
603                         reply = wpas_dbus_global_get_interface(
604                                 message, ctrl_iface->global);
605                 }
606         }
607
608         /* If the message was handled, send back the reply */
609         if (reply) {
610                 dbus_connection_send(connection, reply, NULL);
611                 dbus_message_unref(reply);
612         }
613
614         return reply ? DBUS_HANDLER_RESULT_HANDLED :
615                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
616 }
617
618
619 /**
620  * wpa_supplicant_dbus_notify_scan_results - Send a scan results signal
621  * @wpa_s: %wpa_supplicant network interface data
622  * Returns: 0 on success, -1 on failure
623  *
624  * Notify listeners that this interface has updated scan results.
625  */
626 void wpa_supplicant_dbus_notify_scan_results(struct wpa_supplicant *wpa_s)
627 {
628         struct ctrl_iface_dbus_priv *iface = wpa_s->global->dbus_ctrl_iface;
629         DBusMessage *_signal;
630         const char *path;
631
632         /* Do nothing if the control interface is not turned on */
633         if (iface == NULL)
634                 return;
635
636         path = wpa_supplicant_get_dbus_path(wpa_s);
637         if (path == NULL) {
638                 perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
639                        "interface didn't have a dbus path");
640                 wpa_printf(MSG_ERROR,
641                            "wpa_supplicant_dbus_notify_scan_results[dbus]: "
642                            "interface didn't have a dbus path; can't send "
643                            "scan result signal.");
644                 return;
645         }
646         _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
647                                           "ScanResultsAvailable");
648         if (_signal == NULL) {
649                 perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
650                        "couldn't create dbus signal; likely out of memory");
651                 wpa_printf(MSG_ERROR, "dbus control interface: not enough "
652                            "memory to send scan results signal.");
653                 return;
654         }
655         dbus_connection_send(iface->con, _signal, NULL);
656         dbus_message_unref(_signal);
657 }
658
659
660 /**
661  * wpa_supplicant_dbus_notify_state_change - Send a state change signal
662  * @wpa_s: %wpa_supplicant network interface data
663  * @new_state: new state wpa_supplicant is entering
664  * @old_state: old state wpa_supplicant is leaving
665  * Returns: 0 on success, -1 on failure
666  *
667  * Notify listeners that wpa_supplicant has changed state
668  */
669 void wpa_supplicant_dbus_notify_state_change(struct wpa_supplicant *wpa_s,
670                                              wpa_states new_state,
671                                              wpa_states old_state)
672 {
673         struct ctrl_iface_dbus_priv *iface;
674         DBusMessage *_signal = NULL;
675         const char *path;
676         const char *new_state_str, *old_state_str;
677
678         /* Do nothing if the control interface is not turned on */
679         if (wpa_s->global == NULL)
680                 return;
681         iface = wpa_s->global->dbus_ctrl_iface;
682         if (iface == NULL)
683                 return;
684
685         /* Only send signal if state really changed */
686         if (new_state == old_state)
687                 return;
688
689         path = wpa_supplicant_get_dbus_path(wpa_s);
690         if (path == NULL) {
691                 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
692                        "interface didn't have a dbus path");
693                 wpa_printf(MSG_ERROR,
694                            "wpa_supplicant_dbus_notify_state_change[dbus]: "
695                            "interface didn't have a dbus path; can't send "
696                            "signal.");
697                 return;
698         }
699         _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
700                                           "StateChange");
701         if (_signal == NULL) {
702                 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
703                        "couldn't create dbus signal; likely out of memory");
704                 wpa_printf(MSG_ERROR,
705                            "wpa_supplicant_dbus_notify_state_change[dbus]: "
706                            "couldn't create dbus signal; likely out of "
707                            "memory.");
708                 return;
709         }
710
711         new_state_str = wpa_supplicant_state_txt(new_state);
712         old_state_str = wpa_supplicant_state_txt(old_state);
713         if (new_state_str == NULL || old_state_str == NULL) {
714                 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
715                        "couldn't convert state strings");
716                 wpa_printf(MSG_ERROR,
717                            "wpa_supplicant_dbus_notify_state_change[dbus]: "
718                            "couldn't convert state strings.");
719                 goto out;
720         }
721
722         if (!dbus_message_append_args(_signal,
723                                       DBUS_TYPE_STRING, &new_state_str,
724                                       DBUS_TYPE_STRING, &old_state_str,
725                                       DBUS_TYPE_INVALID)) {
726                 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
727                        "not enough memory to construct state change signal.");
728                 wpa_printf(MSG_ERROR,
729                            "wpa_supplicant_dbus_notify_state_change[dbus]: "
730                            "not enough memory to construct state change "
731                            "signal.");
732                 goto out;
733         }
734
735         dbus_connection_send(iface->con, _signal, NULL);
736
737 out:
738         dbus_message_unref(_signal);
739 }
740
741
742 #ifdef CONFIG_WPS
743 void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
744                                          const struct wps_credential *cred)
745 {
746         struct ctrl_iface_dbus_priv *iface;
747         DBusMessage *_signal = NULL;
748         const char *path;
749
750         /* Do nothing if the control interface is not turned on */
751         if (wpa_s->global == NULL)
752                 return;
753         iface = wpa_s->global->dbus_ctrl_iface;
754         if (iface == NULL)
755                 return;
756
757         path = wpa_supplicant_get_dbus_path(wpa_s);
758         if (path == NULL) {
759                 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
760                        "interface didn't have a dbus path");
761                 wpa_printf(MSG_ERROR,
762                            "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
763                            "interface didn't have a dbus path; can't send "
764                            "signal.");
765                 return;
766         }
767         _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
768                                           "WpsCred");
769         if (_signal == NULL) {
770                 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
771                        "couldn't create dbus signal; likely out of memory");
772                 wpa_printf(MSG_ERROR,
773                            "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
774                            "couldn't create dbus signal; likely out of "
775                            "memory.");
776                 return;
777         }
778
779         if (!dbus_message_append_args(_signal,
780                                       DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
781                                       &cred->cred_attr, cred->cred_attr_len,
782                                       DBUS_TYPE_INVALID)) {
783                 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
784                        "not enough memory to construct signal.");
785                 wpa_printf(MSG_ERROR,
786                            "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
787                            "not enough memory to construct signal.");
788                 goto out;
789         }
790
791         dbus_connection_send(iface->con, _signal, NULL);
792
793 out:
794         dbus_message_unref(_signal);
795 }
796 #endif /* CONFIG_WPS */
797
798
799 /**
800  * integrate_with_eloop - Register our mainloop integration with dbus
801  * @connection: connection to the system message bus
802  * @iface: a dbus control interface data structure
803  * Returns: 0 on success, -1 on failure
804  *
805  * We register our mainloop integration functions with dbus here.
806  */
807 static int integrate_with_eloop(DBusConnection *connection,
808         struct ctrl_iface_dbus_priv *iface)
809 {
810         if (!dbus_connection_set_watch_functions(connection, add_watch,
811                                                  remove_watch, watch_toggled,
812                                                  iface, NULL)) {
813                 perror("dbus_connection_set_watch_functions[dbus]");
814                 wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
815                 return -1;
816         }
817
818         if (!dbus_connection_set_timeout_functions(connection, add_timeout,
819                                                    remove_timeout,
820                                                    timeout_toggled, iface,
821                                                    NULL)) {
822                 perror("dbus_connection_set_timeout_functions[dbus]");
823                 wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
824                 return -1;
825         }
826
827         if (connection_setup_wakeup_main(iface) < 0) {
828                 perror("connection_setup_wakeup_main[dbus]");
829                 wpa_printf(MSG_ERROR, "Could not setup main wakeup function.");
830                 return -1;
831         }
832
833         return 0;
834 }
835
836
837 /**
838  * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
839  *     claiming bus name
840  * @eloop_ctx: the DBusConnection to dispatch on
841  * @timeout_ctx: unused
842  *
843  * If clients are quick to notice that wpa_supplicant claimed its bus name,
844  * there may have been messages that came in before initialization was
845  * all finished.  Dispatch those here.
846  */
847 static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
848 {
849         DBusConnection *con = eloop_ctx;
850
851         while (dbus_connection_get_dispatch_status(con) ==
852                DBUS_DISPATCH_DATA_REMAINS)
853                 dbus_connection_dispatch(con);
854 }
855
856
857 /**
858  * wpa_supplicant_dbus_ctrl_iface_init - Initialize dbus control interface
859  * @global: Pointer to global data from wpa_supplicant_init()
860  * Returns: Pointer to dbus_ctrl_iface date or %NULL on failure
861  *
862  * Initialize the dbus control interface and start receiving commands from
863  * external programs over the bus.
864  */
865 struct ctrl_iface_dbus_priv *
866 wpa_supplicant_dbus_ctrl_iface_init(struct wpa_global *global)
867 {
868         struct ctrl_iface_dbus_priv *iface;
869         DBusError error;
870         int ret = -1;
871         DBusObjectPathVTable wpas_vtable = {
872                 NULL, &wpas_message_handler, NULL, NULL, NULL, NULL
873         };
874
875         iface = os_zalloc(sizeof(struct ctrl_iface_dbus_priv));
876         if (iface == NULL)
877                 return NULL;
878
879         iface->global = global;
880
881         /* Get a reference to the system bus */
882         dbus_error_init(&error);
883         iface->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
884         dbus_error_free(&error);
885         if (!iface->con) {
886                 perror("dbus_bus_get[ctrl_iface_dbus]");
887                 wpa_printf(MSG_ERROR, "Could not acquire the system bus.");
888                 goto fail;
889         }
890
891         /* Tell dbus about our mainloop integration functions */
892         if (integrate_with_eloop(iface->con, iface))
893                 goto fail;
894
895         /* Register the message handler for the global dbus interface */
896         if (!dbus_connection_register_object_path(iface->con,
897                                                   WPAS_DBUS_PATH, &wpas_vtable,
898                                                   iface)) {
899                 perror("dbus_connection_register_object_path[dbus]");
900                 wpa_printf(MSG_ERROR, "Could not set up DBus message "
901                            "handler.");
902                 goto fail;
903         }
904
905         /* Register our service with the message bus */
906         dbus_error_init(&error);
907         switch (dbus_bus_request_name(iface->con, WPAS_DBUS_SERVICE,
908                                       0, &error)) {
909         case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
910                 ret = 0;
911                 break;
912         case DBUS_REQUEST_NAME_REPLY_EXISTS:
913         case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
914         case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
915                 perror("dbus_bus_request_name[dbus]");
916                 wpa_printf(MSG_ERROR, "Could not request DBus service name: "
917                            "already registered.");
918                 break;
919         default:
920                 perror("dbus_bus_request_name[dbus]");
921                 wpa_printf(MSG_ERROR, "Could not request DBus service name: "
922                            "%s %s.", error.name, error.message);
923                 break;
924         }
925         dbus_error_free(&error);
926
927         if (ret != 0)
928                 goto fail;
929
930         wpa_printf(MSG_DEBUG, "Providing DBus service '" WPAS_DBUS_SERVICE
931                    "'.");
932
933         /*
934          * Dispatch initial DBus messages that may have come in since the bus
935          * name was claimed above. Happens when clients are quick to notice the
936          * wpa_supplicant service.
937          *
938          * FIXME: is there a better solution to this problem?
939          */
940         eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
941                                iface->con, NULL);
942
943         return iface;
944
945 fail:
946         wpa_supplicant_dbus_ctrl_iface_deinit(iface);
947         return NULL;
948 }
949
950
951 /**
952  * wpa_supplicant_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface
953  * @iface: Pointer to dbus private data from
954  * wpa_supplicant_dbus_ctrl_iface_init()
955  *
956  * Deinitialize the dbus control interface that was initialized with
957  * wpa_supplicant_dbus_ctrl_iface_init().
958  */
959 void wpa_supplicant_dbus_ctrl_iface_deinit(struct ctrl_iface_dbus_priv *iface)
960 {
961         if (iface == NULL)
962                 return;
963
964         if (iface->con) {
965                 eloop_cancel_timeout(dispatch_initial_dbus_messages,
966                                      iface->con, NULL);
967                 dbus_connection_set_watch_functions(iface->con, NULL, NULL,
968                                                     NULL, NULL, NULL);
969                 dbus_connection_set_timeout_functions(iface->con, NULL, NULL,
970                                                       NULL, NULL, NULL);
971                 dbus_connection_unref(iface->con);
972         }
973
974         memset(iface, 0, sizeof(struct ctrl_iface_dbus_priv));
975         free(iface);
976 }
977
978
979 /**
980  * wpas_dbus_register_new_iface - Register a new interface with dbus
981  * @wpa_s: %wpa_supplicant interface description structure to register
982  * Returns: 0 on success, -1 on error
983  *
984  * Registers a new interface with dbus and assigns it a dbus object path.
985  */
986 int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
987 {
988         struct ctrl_iface_dbus_priv *ctrl_iface =
989                 wpa_s->global->dbus_ctrl_iface;
990         DBusConnection * con;
991         u32 next;
992         DBusObjectPathVTable vtable = {
993                 NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
994         };
995         char *path;
996         int ret = -1;
997
998         /* Do nothing if the control interface is not turned on */
999         if (ctrl_iface == NULL)
1000                 return 0;
1001
1002         con = ctrl_iface->con;
1003         next = wpa_supplicant_dbus_next_objid(ctrl_iface);
1004
1005         /* Create and set the interface's object path */
1006         path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
1007         if (path == NULL)
1008                 return -1;
1009         snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
1010                  WPAS_DBUS_PATH_INTERFACES "/%u",
1011                  next);
1012         if (wpa_supplicant_set_dbus_path(wpa_s, path)) {
1013                 wpa_printf(MSG_DEBUG,
1014                            "Failed to set dbus path for interface %s",
1015                            wpa_s->ifname);
1016                 goto out;
1017         }
1018
1019         /* Register the message handler for the interface functions */
1020         if (!dbus_connection_register_fallback(con, path, &vtable, wpa_s)) {
1021                 perror("wpas_dbus_register_iface [dbus]");
1022                 wpa_printf(MSG_ERROR, "Could not set up DBus message "
1023                            "handler for interface %s.", wpa_s->ifname);
1024                 goto out;
1025         }
1026         ret = 0;
1027
1028 out:
1029         free(path);
1030         return ret;
1031 }
1032
1033
1034 /**
1035  * wpas_dbus_unregister_iface - Unregister an interface from dbus
1036  * @wpa_s: wpa_supplicant interface structure
1037  * Returns: 0 on success, -1 on failure
1038  *
1039  * Unregisters the interface with dbus
1040  */
1041 int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
1042 {
1043         struct ctrl_iface_dbus_priv *ctrl_iface;
1044         DBusConnection *con;
1045         const char *path;
1046
1047         /* Do nothing if the control interface is not turned on */
1048         if (wpa_s == NULL || wpa_s->global == NULL)
1049                 return 0;
1050         ctrl_iface = wpa_s->global->dbus_ctrl_iface;
1051         if (ctrl_iface == NULL)
1052                 return 0;
1053
1054         con = ctrl_iface->con;
1055         path = wpa_supplicant_get_dbus_path(wpa_s);
1056
1057         if (!dbus_connection_unregister_object_path(con, path))
1058                 return -1;
1059
1060         free(wpa_s->dbus_path);
1061         wpa_s->dbus_path = NULL;
1062
1063         return 0;
1064 }
1065
1066
1067 /**
1068  * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
1069  * @global: Pointer to global data from wpa_supplicant_init()
1070  * @path: Pointer to a dbus object path representing an interface
1071  * Returns: Pointer to the interface or %NULL if not found
1072  */
1073 struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
1074         struct wpa_global *global, const char *path)
1075 {
1076         struct wpa_supplicant *wpa_s;
1077
1078         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1079                 if (strcmp(wpa_s->dbus_path, path) == 0)
1080                         return wpa_s;
1081         }
1082         return NULL;
1083 }
1084
1085
1086 /**
1087  * wpa_supplicant_set_dbus_path - Assign a dbus path to an interface
1088  * @wpa_s: wpa_supplicant interface structure
1089  * @path: dbus path to set on the interface
1090  * Returns: 0 on succes, -1 on error
1091  */
1092 int wpa_supplicant_set_dbus_path(struct wpa_supplicant *wpa_s,
1093                                   const char *path)
1094 {
1095         u32 len = strlen (path);
1096         if (len >= WPAS_DBUS_OBJECT_PATH_MAX)
1097                 return -1;
1098         if (wpa_s->dbus_path)
1099                 return -1;
1100         wpa_s->dbus_path = strdup(path);
1101         return 0;
1102 }
1103
1104
1105 /**
1106  * wpa_supplicant_get_dbus_path - Get an interface's dbus path
1107  * @wpa_s: %wpa_supplicant interface structure
1108  * Returns: Interface's dbus object path, or %NULL on error
1109  */
1110 const char * wpa_supplicant_get_dbus_path(struct wpa_supplicant *wpa_s)
1111 {
1112         return wpa_s->dbus_path;
1113 }