dbus_new_helpers.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "utils/includes.h"
  16. #include "utils/common.h"
  17. #include "utils/eloop.h"
  18. #include "dbus_common.h"
  19. #include "dbus_common_i.h"
  20. #include "dbus_new.h"
  21. #include "dbus_new_helpers.h"
  22. #include "dbus_dict_helpers.h"
  23. static dbus_bool_t fill_dict_with_properties(
  24. DBusMessageIter *dict_iter,
  25. const struct wpa_dbus_property_desc *props,
  26. const char *interface, void *user_data, DBusError *error)
  27. {
  28. DBusMessageIter entry_iter;
  29. const struct wpa_dbus_property_desc *dsc;
  30. for (dsc = props; dsc && dsc->dbus_property; dsc++) {
  31. /* Only return properties for the requested D-Bus interface */
  32. if (os_strncmp(dsc->dbus_interface, interface,
  33. WPAS_DBUS_INTERFACE_MAX) != 0)
  34. continue;
  35. /* Skip write-only properties */
  36. if (dsc->getter == NULL)
  37. continue;
  38. if (!dbus_message_iter_open_container(dict_iter,
  39. DBUS_TYPE_DICT_ENTRY,
  40. NULL, &entry_iter)) {
  41. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  42. "no memory");
  43. return FALSE;
  44. }
  45. if (!dbus_message_iter_append_basic(&entry_iter,
  46. DBUS_TYPE_STRING,
  47. &dsc->dbus_property)) {
  48. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  49. "no memory");
  50. return FALSE;
  51. }
  52. /* An error getting a property fails the request entirely */
  53. if (!dsc->getter(&entry_iter, error, user_data))
  54. return FALSE;
  55. dbus_message_iter_close_container(dict_iter, &entry_iter);
  56. }
  57. return TRUE;
  58. }
  59. /**
  60. * get_all_properties - Responds for GetAll properties calls on object
  61. * @message: Message with GetAll call
  62. * @interface: interface name which properties will be returned
  63. * @property_dsc: list of object's properties
  64. * Returns: Message with dict of variants as argument with properties values
  65. *
  66. * Iterates over all properties registered with object and execute getters
  67. * of those, which are readable and which interface matches interface
  68. * specified as argument. Returned message contains one dict argument
  69. * with properties names as keys and theirs values as values.
  70. */
  71. static DBusMessage * get_all_properties(DBusMessage *message, char *interface,
  72. struct wpa_dbus_object_desc *obj_dsc)
  73. {
  74. DBusMessage *reply;
  75. DBusMessageIter iter, dict_iter;
  76. DBusError error;
  77. reply = dbus_message_new_method_return(message);
  78. if (reply == NULL) {
  79. wpa_printf(MSG_ERROR, "%s: out of memory creating dbus reply",
  80. __func__);
  81. return NULL;
  82. }
  83. dbus_message_iter_init_append(reply, &iter);
  84. if (!wpa_dbus_dict_open_write(&iter, &dict_iter)) {
  85. wpa_printf(MSG_ERROR, "%s: out of memory creating reply",
  86. __func__);
  87. dbus_message_unref(reply);
  88. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  89. "out of memory");
  90. return reply;
  91. }
  92. dbus_error_init(&error);
  93. if (!fill_dict_with_properties(&dict_iter, obj_dsc->properties,
  94. interface, obj_dsc->user_data, &error))
  95. {
  96. dbus_message_unref(reply);
  97. reply = wpas_dbus_reply_new_from_error(message, &error,
  98. DBUS_ERROR_INVALID_ARGS,
  99. "No readable properties"
  100. " in this interface");
  101. dbus_error_free(&error);
  102. return reply;
  103. }
  104. wpa_dbus_dict_close_write(&iter, &dict_iter);
  105. return reply;
  106. }
  107. static int is_signature_correct(DBusMessage *message,
  108. const struct wpa_dbus_method_desc *method_dsc)
  109. {
  110. /* According to DBus documentation max length of signature is 255 */
  111. #define MAX_SIG_LEN 256
  112. char registered_sig[MAX_SIG_LEN], *pos;
  113. const char *sig = dbus_message_get_signature(message);
  114. int ret;
  115. const struct wpa_dbus_argument *arg;
  116. pos = registered_sig;
  117. *pos = '\0';
  118. for (arg = method_dsc->args; arg && arg->name; arg++) {
  119. if (arg->dir == ARG_IN) {
  120. size_t blen = registered_sig + MAX_SIG_LEN - pos;
  121. ret = os_snprintf(pos, blen, "%s", arg->type);
  122. if (ret < 0 || (size_t) ret >= blen)
  123. return 0;
  124. pos += ret;
  125. }
  126. }
  127. return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
  128. }
  129. static DBusMessage * properties_get_all(DBusMessage *message, char *interface,
  130. struct wpa_dbus_object_desc *obj_dsc)
  131. {
  132. if (os_strcmp(dbus_message_get_signature(message), "s") != 0)
  133. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  134. NULL);
  135. return get_all_properties(message, interface, obj_dsc);
  136. }
  137. static DBusMessage * properties_get(DBusMessage *message,
  138. const struct wpa_dbus_property_desc *dsc,
  139. void *user_data)
  140. {
  141. DBusMessage *reply;
  142. DBusMessageIter iter;
  143. DBusError error;
  144. if (os_strcmp(dbus_message_get_signature(message), "ss")) {
  145. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  146. NULL);
  147. }
  148. if (dsc->getter == NULL) {
  149. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  150. "Property is write-only");
  151. }
  152. reply = dbus_message_new_method_return(message);
  153. dbus_message_iter_init_append(reply, &iter);
  154. dbus_error_init(&error);
  155. if (dsc->getter(&iter, &error, user_data) == FALSE) {
  156. dbus_message_unref(reply);
  157. reply = wpas_dbus_reply_new_from_error(
  158. message, &error, DBUS_ERROR_FAILED,
  159. "Failed to read property");
  160. dbus_error_free(&error);
  161. }
  162. return reply;
  163. }
  164. static DBusMessage * properties_set(DBusMessage *message,
  165. const struct wpa_dbus_property_desc *dsc,
  166. void *user_data)
  167. {
  168. DBusMessage *reply;
  169. DBusMessageIter iter;
  170. DBusError error;
  171. if (os_strcmp(dbus_message_get_signature(message), "ssv")) {
  172. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  173. NULL);
  174. }
  175. if (dsc->setter == NULL) {
  176. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  177. "Property is read-only");
  178. }
  179. dbus_message_iter_init(message, &iter);
  180. /* Skip the interface name and the property name */
  181. dbus_message_iter_next(&iter);
  182. dbus_message_iter_next(&iter);
  183. /* Iter will now point to the property's new value */
  184. dbus_error_init(&error);
  185. if (dsc->setter(&iter, &error, user_data) == TRUE) {
  186. /* Success */
  187. reply = dbus_message_new_method_return(message);
  188. } else {
  189. reply = wpas_dbus_reply_new_from_error(
  190. message, &error, DBUS_ERROR_FAILED,
  191. "Failed to set property");
  192. dbus_error_free(&error);
  193. }
  194. return reply;
  195. }
  196. static DBusMessage *
  197. properties_get_or_set(DBusMessage *message, DBusMessageIter *iter,
  198. char *interface,
  199. struct wpa_dbus_object_desc *obj_dsc)
  200. {
  201. const struct wpa_dbus_property_desc *property_dsc;
  202. char *property;
  203. const char *method;
  204. method = dbus_message_get_member(message);
  205. property_dsc = obj_dsc->properties;
  206. /* Second argument: property name (DBUS_TYPE_STRING) */
  207. if (!dbus_message_iter_next(iter) ||
  208. dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) {
  209. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  210. NULL);
  211. }
  212. dbus_message_iter_get_basic(iter, &property);
  213. while (property_dsc && property_dsc->dbus_property) {
  214. /* compare property names and
  215. * interfaces */
  216. if (!os_strncmp(property_dsc->dbus_property, property,
  217. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  218. !os_strncmp(property_dsc->dbus_interface, interface,
  219. WPAS_DBUS_INTERFACE_MAX))
  220. break;
  221. property_dsc++;
  222. }
  223. if (property_dsc == NULL || property_dsc->dbus_property == NULL) {
  224. wpa_printf(MSG_DEBUG, "no property handler for %s.%s on %s",
  225. interface, property,
  226. dbus_message_get_path(message));
  227. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  228. "No such property");
  229. }
  230. if (os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  231. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) == 0)
  232. return properties_get(message, property_dsc,
  233. obj_dsc->user_data);
  234. return properties_set(message, property_dsc, obj_dsc->user_data);
  235. }
  236. static DBusMessage * properties_handler(DBusMessage *message,
  237. struct wpa_dbus_object_desc *obj_dsc)
  238. {
  239. DBusMessageIter iter;
  240. char *interface;
  241. const char *method;
  242. method = dbus_message_get_member(message);
  243. dbus_message_iter_init(message, &iter);
  244. if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  245. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  246. !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
  247. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  248. !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  249. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  250. /* First argument: interface name (DBUS_TYPE_STRING) */
  251. if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
  252. {
  253. return dbus_message_new_error(message,
  254. DBUS_ERROR_INVALID_ARGS,
  255. NULL);
  256. }
  257. dbus_message_iter_get_basic(&iter, &interface);
  258. if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  259. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  260. /* GetAll */
  261. return properties_get_all(message, interface, obj_dsc);
  262. }
  263. /* Get or Set */
  264. return properties_get_or_set(message, &iter, interface,
  265. obj_dsc);
  266. }
  267. return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
  268. NULL);
  269. }
  270. static DBusMessage * msg_method_handler(DBusMessage *message,
  271. struct wpa_dbus_object_desc *obj_dsc)
  272. {
  273. const struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
  274. const char *method;
  275. const char *msg_interface;
  276. method = dbus_message_get_member(message);
  277. msg_interface = dbus_message_get_interface(message);
  278. /* try match call to any registered method */
  279. while (method_dsc && method_dsc->dbus_method) {
  280. /* compare method names and interfaces */
  281. if (!os_strncmp(method_dsc->dbus_method, method,
  282. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  283. !os_strncmp(method_dsc->dbus_interface, msg_interface,
  284. WPAS_DBUS_INTERFACE_MAX))
  285. break;
  286. method_dsc++;
  287. }
  288. if (method_dsc == NULL || method_dsc->dbus_method == NULL) {
  289. wpa_printf(MSG_DEBUG, "no method handler for %s.%s on %s",
  290. msg_interface, method,
  291. dbus_message_get_path(message));
  292. return dbus_message_new_error(message,
  293. DBUS_ERROR_UNKNOWN_METHOD, NULL);
  294. }
  295. if (!is_signature_correct(message, method_dsc)) {
  296. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  297. NULL);
  298. }
  299. return method_dsc->method_handler(message,
  300. obj_dsc->user_data);
  301. }
  302. /**
  303. * message_handler - Handles incoming DBus messages
  304. * @connection: DBus connection on which message was received
  305. * @message: Received message
  306. * @user_data: pointer to description of object to which message was sent
  307. * Returns: Returns information whether message was handled or not
  308. *
  309. * Reads message interface and method name, then checks if they matches one
  310. * of the special cases i.e. introspection call or properties get/getall/set
  311. * methods and handles it. Else it iterates over registered methods list
  312. * and tries to match method's name and interface to those read from message
  313. * If appropriate method was found its handler function is called and
  314. * response is sent. Otherwise, the DBUS_ERROR_UNKNOWN_METHOD error message
  315. * will be sent.
  316. */
  317. static DBusHandlerResult message_handler(DBusConnection *connection,
  318. DBusMessage *message, void *user_data)
  319. {
  320. struct wpa_dbus_object_desc *obj_dsc = user_data;
  321. const char *method;
  322. const char *path;
  323. const char *msg_interface;
  324. DBusMessage *reply;
  325. /* get method, interface and path the message is addressed to */
  326. method = dbus_message_get_member(message);
  327. path = dbus_message_get_path(message);
  328. msg_interface = dbus_message_get_interface(message);
  329. if (!method || !path || !msg_interface)
  330. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  331. wpa_printf(MSG_MSGDUMP, "dbus: %s.%s (%s)",
  332. msg_interface, method, path);
  333. /* if message is introspection method call */
  334. if (!os_strncmp(WPA_DBUS_INTROSPECTION_METHOD, method,
  335. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  336. !os_strncmp(WPA_DBUS_INTROSPECTION_INTERFACE, msg_interface,
  337. WPAS_DBUS_INTERFACE_MAX)) {
  338. #ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
  339. reply = wpa_dbus_introspect(message, obj_dsc);
  340. #else /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  341. reply = dbus_message_new_error(
  342. message, DBUS_ERROR_UNKNOWN_METHOD,
  343. "wpa_supplicant was compiled without "
  344. "introspection support.");
  345. #endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  346. } else if (!os_strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
  347. WPAS_DBUS_INTERFACE_MAX)) {
  348. /* if message is properties method call */
  349. reply = properties_handler(message, obj_dsc);
  350. } else {
  351. reply = msg_method_handler(message, obj_dsc);
  352. }
  353. /* If handler succeed returning NULL, reply empty message */
  354. if (!reply)
  355. reply = dbus_message_new_method_return(message);
  356. if (reply) {
  357. if (!dbus_message_get_no_reply(message))
  358. dbus_connection_send(connection, reply, NULL);
  359. dbus_message_unref(reply);
  360. }
  361. wpa_dbus_flush_all_changed_properties(connection);
  362. return DBUS_HANDLER_RESULT_HANDLED;
  363. }
  364. /**
  365. * free_dbus_object_desc - Frees object description data structure
  366. * @connection: DBus connection
  367. * @obj_dsc: Object description to free
  368. *
  369. * Frees each of properties, methods and signals description lists and
  370. * the object description structure itself.
  371. */
  372. void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
  373. {
  374. if (!obj_dsc)
  375. return;
  376. /* free handler's argument */
  377. if (obj_dsc->user_data_free_func)
  378. obj_dsc->user_data_free_func(obj_dsc->user_data);
  379. os_free(obj_dsc->path);
  380. os_free(obj_dsc->prop_changed_flags);
  381. os_free(obj_dsc);
  382. }
  383. static void free_dbus_object_desc_cb(DBusConnection *connection, void *obj_dsc)
  384. {
  385. free_dbus_object_desc(obj_dsc);
  386. }
  387. /**
  388. * wpa_dbus_ctrl_iface_init - Initialize dbus control interface
  389. * @application_data: Pointer to application specific data structure
  390. * @dbus_path: DBus path to interface object
  391. * @dbus_service: DBus service name to register with
  392. * @messageHandler: a pointer to function which will handle dbus messages
  393. * coming on interface
  394. * Returns: 0 on success, -1 on failure
  395. *
  396. * Initialize the dbus control interface and start receiving commands from
  397. * external programs over the bus.
  398. */
  399. int wpa_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface,
  400. char *dbus_path, char *dbus_service,
  401. struct wpa_dbus_object_desc *obj_desc)
  402. {
  403. DBusError error;
  404. int ret = -1;
  405. DBusObjectPathVTable wpa_vtable = {
  406. &free_dbus_object_desc_cb, &message_handler,
  407. NULL, NULL, NULL, NULL
  408. };
  409. obj_desc->connection = iface->con;
  410. obj_desc->path = os_strdup(dbus_path);
  411. /* Register the message handler for the global dbus interface */
  412. if (!dbus_connection_register_object_path(iface->con,
  413. dbus_path, &wpa_vtable,
  414. obj_desc)) {
  415. wpa_printf(MSG_ERROR, "dbus: Could not set up message "
  416. "handler");
  417. return -1;
  418. }
  419. /* Register our service with the message bus */
  420. dbus_error_init(&error);
  421. switch (dbus_bus_request_name(iface->con, dbus_service,
  422. 0, &error)) {
  423. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  424. ret = 0;
  425. break;
  426. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  427. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  428. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  429. wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
  430. "already registered");
  431. break;
  432. default:
  433. wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
  434. "%s %s", error.name, error.message);
  435. break;
  436. }
  437. dbus_error_free(&error);
  438. if (ret != 0)
  439. return -1;
  440. wpa_printf(MSG_DEBUG, "Providing DBus service '%s'.", dbus_service);
  441. return 0;
  442. }
  443. /**
  444. * wpa_dbus_register_object_per_iface - Register a new object with dbus
  445. * @ctrl_iface: pointer to dbus private data
  446. * @path: DBus path to object
  447. * @ifname: interface name
  448. * @obj_desc: description of object's methods, signals and properties
  449. * Returns: 0 on success, -1 on error
  450. *
  451. * Registers a new interface with dbus and assigns it a dbus object path.
  452. */
  453. int wpa_dbus_register_object_per_iface(
  454. struct wpas_dbus_priv *ctrl_iface,
  455. const char *path, const char *ifname,
  456. struct wpa_dbus_object_desc *obj_desc)
  457. {
  458. DBusConnection *con;
  459. DBusError error;
  460. DBusObjectPathVTable vtable = {
  461. &free_dbus_object_desc_cb, &message_handler,
  462. NULL, NULL, NULL, NULL
  463. };
  464. /* Do nothing if the control interface is not turned on */
  465. if (ctrl_iface == NULL)
  466. return 0;
  467. con = ctrl_iface->con;
  468. obj_desc->connection = con;
  469. obj_desc->path = os_strdup(path);
  470. dbus_error_init(&error);
  471. /* Register the message handler for the interface functions */
  472. if (!dbus_connection_try_register_object_path(con, path, &vtable,
  473. obj_desc, &error)) {
  474. if (!os_strcmp(error.name, DBUS_ERROR_OBJECT_PATH_IN_USE)) {
  475. wpa_printf(MSG_DEBUG, "dbus: %s", error.message);
  476. } else {
  477. wpa_printf(MSG_ERROR, "dbus: Could not set up message "
  478. "handler for interface %s object %s",
  479. ifname, path);
  480. wpa_printf(MSG_ERROR, "dbus error: %s", error.name);
  481. wpa_printf(MSG_ERROR, "dbus: %s", error.message);
  482. }
  483. dbus_error_free(&error);
  484. return -1;
  485. }
  486. dbus_error_free(&error);
  487. return 0;
  488. }
  489. static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx);
  490. /**
  491. * wpa_dbus_unregister_object_per_iface - Unregisters DBus object
  492. * @ctrl_iface: Pointer to dbus private data
  493. * @path: DBus path to object which will be unregistered
  494. * Returns: Zero on success and -1 on failure
  495. *
  496. * Unregisters DBus object given by its path
  497. */
  498. int wpa_dbus_unregister_object_per_iface(
  499. struct wpas_dbus_priv *ctrl_iface, const char *path)
  500. {
  501. DBusConnection *con = ctrl_iface->con;
  502. struct wpa_dbus_object_desc *obj_desc = NULL;
  503. dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
  504. if (!obj_desc) {
  505. wpa_printf(MSG_ERROR, "dbus: %s: Could not obtain object's "
  506. "private data: %s", __func__, path);
  507. } else {
  508. eloop_cancel_timeout(flush_object_timeout_handler, con,
  509. obj_desc);
  510. }
  511. if (!dbus_connection_unregister_object_path(con, path))
  512. return -1;
  513. return 0;
  514. }
  515. static dbus_bool_t put_changed_properties(
  516. const struct wpa_dbus_object_desc *obj_dsc, const char *interface,
  517. DBusMessageIter *dict_iter, int clear_changed)
  518. {
  519. DBusMessageIter entry_iter;
  520. const struct wpa_dbus_property_desc *dsc;
  521. int i;
  522. DBusError error;
  523. for (dsc = obj_dsc->properties, i = 0; dsc && dsc->dbus_property;
  524. dsc++, i++) {
  525. if (obj_dsc->prop_changed_flags == NULL ||
  526. !obj_dsc->prop_changed_flags[i])
  527. continue;
  528. if (os_strcmp(dsc->dbus_interface, interface) != 0)
  529. continue;
  530. if (clear_changed)
  531. obj_dsc->prop_changed_flags[i] = 0;
  532. if (!dbus_message_iter_open_container(dict_iter,
  533. DBUS_TYPE_DICT_ENTRY,
  534. NULL, &entry_iter))
  535. return FALSE;
  536. if (!dbus_message_iter_append_basic(&entry_iter,
  537. DBUS_TYPE_STRING,
  538. &dsc->dbus_property))
  539. return FALSE;
  540. dbus_error_init(&error);
  541. if (!dsc->getter(&entry_iter, &error, obj_dsc->user_data)) {
  542. if (dbus_error_is_set (&error)) {
  543. wpa_printf(MSG_ERROR, "dbus: %s: Cannot get "
  544. "new value of property %s: (%s) %s",
  545. __func__, dsc->dbus_property,
  546. error.name, error.message);
  547. } else {
  548. wpa_printf(MSG_ERROR, "dbus: %s: Cannot get "
  549. "new value of property %s",
  550. __func__, dsc->dbus_property);
  551. }
  552. dbus_error_free(&error);
  553. return FALSE;
  554. }
  555. if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
  556. return FALSE;
  557. }
  558. return TRUE;
  559. }
  560. static void do_send_prop_changed_signal(
  561. DBusConnection *con, const char *path, const char *interface,
  562. const struct wpa_dbus_object_desc *obj_dsc)
  563. {
  564. DBusMessage *msg;
  565. DBusMessageIter signal_iter, dict_iter;
  566. msg = dbus_message_new_signal(path, DBUS_INTERFACE_PROPERTIES,
  567. "PropertiesChanged");
  568. if (msg == NULL)
  569. return;
  570. dbus_message_iter_init_append(msg, &signal_iter);
  571. if (!dbus_message_iter_append_basic(&signal_iter, DBUS_TYPE_STRING,
  572. &interface))
  573. goto err;
  574. /* Changed properties dict */
  575. if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
  576. "{sv}", &dict_iter))
  577. goto err;
  578. if (!put_changed_properties(obj_dsc, interface, &dict_iter, 0))
  579. goto err;
  580. if (!dbus_message_iter_close_container(&signal_iter, &dict_iter))
  581. goto err;
  582. /* Invalidated properties array (empty) */
  583. if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
  584. "s", &dict_iter))
  585. goto err;
  586. if (!dbus_message_iter_close_container(&signal_iter, &dict_iter))
  587. goto err;
  588. dbus_connection_send(con, msg, NULL);
  589. out:
  590. dbus_message_unref(msg);
  591. return;
  592. err:
  593. wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
  594. __func__);
  595. goto out;
  596. }
  597. static void do_send_deprecated_prop_changed_signal(
  598. DBusConnection *con, const char *path, const char *interface,
  599. const struct wpa_dbus_object_desc *obj_dsc)
  600. {
  601. DBusMessage *msg;
  602. DBusMessageIter signal_iter, dict_iter;
  603. msg = dbus_message_new_signal(path, interface, "PropertiesChanged");
  604. if (msg == NULL)
  605. return;
  606. dbus_message_iter_init_append(msg, &signal_iter);
  607. if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
  608. "{sv}", &dict_iter))
  609. goto err;
  610. if (!put_changed_properties(obj_dsc, interface, &dict_iter, 1))
  611. goto err;
  612. if (!dbus_message_iter_close_container(&signal_iter, &dict_iter))
  613. goto err;
  614. dbus_connection_send(con, msg, NULL);
  615. out:
  616. dbus_message_unref(msg);
  617. return;
  618. err:
  619. wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
  620. __func__);
  621. goto out;
  622. }
  623. static void send_prop_changed_signal(
  624. DBusConnection *con, const char *path, const char *interface,
  625. const struct wpa_dbus_object_desc *obj_dsc)
  626. {
  627. /*
  628. * First, send property change notification on the standardized
  629. * org.freedesktop.DBus.Properties interface. This call will not
  630. * clear the property change bits, so that they are preserved for
  631. * the call that follows.
  632. */
  633. do_send_prop_changed_signal(con, path, interface, obj_dsc);
  634. /*
  635. * Now send PropertiesChanged on our own interface for backwards
  636. * compatibility. This is deprecated and will be removed in a future
  637. * release.
  638. */
  639. do_send_deprecated_prop_changed_signal(con, path, interface, obj_dsc);
  640. /* Property change bits have now been cleared. */
  641. }
  642. static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx)
  643. {
  644. DBusConnection *con = eloop_ctx;
  645. struct wpa_dbus_object_desc *obj_desc = timeout_ctx;
  646. wpa_printf(MSG_DEBUG, "dbus: %s: Timeout - sending changed properties "
  647. "of object %s", __func__, obj_desc->path);
  648. wpa_dbus_flush_object_changed_properties(con, obj_desc->path);
  649. }
  650. static void recursive_flush_changed_properties(DBusConnection *con,
  651. const char *path)
  652. {
  653. char **objects = NULL;
  654. char subobj_path[WPAS_DBUS_OBJECT_PATH_MAX];
  655. int i;
  656. wpa_dbus_flush_object_changed_properties(con, path);
  657. if (!dbus_connection_list_registered(con, path, &objects))
  658. goto out;
  659. for (i = 0; objects[i]; i++) {
  660. os_snprintf(subobj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  661. "%s/%s", path, objects[i]);
  662. recursive_flush_changed_properties(con, subobj_path);
  663. }
  664. out:
  665. dbus_free_string_array(objects);
  666. }
  667. /**
  668. * wpa_dbus_flush_all_changed_properties - Send all PropertiesChanged signals
  669. * @con: DBus connection
  670. *
  671. * Traverses through all registered objects and sends PropertiesChanged for
  672. * each properties.
  673. */
  674. void wpa_dbus_flush_all_changed_properties(DBusConnection *con)
  675. {
  676. recursive_flush_changed_properties(con, WPAS_DBUS_NEW_PATH);
  677. }
  678. /**
  679. * wpa_dbus_flush_object_changed_properties - Send PropertiesChanged for object
  680. * @con: DBus connection
  681. * @path: path to a DBus object for which PropertiesChanged will be sent.
  682. *
  683. * Iterates over all properties registered with object and for each interface
  684. * containing properties marked as changed, sends a PropertiesChanged signal
  685. * containing names and new values of properties that have changed.
  686. *
  687. * You need to call this function after wpa_dbus_mark_property_changed()
  688. * if you want to send PropertiesChanged signal immediately (i.e., without
  689. * waiting timeout to expire). PropertiesChanged signal for an object is sent
  690. * automatically short time after first marking property as changed. All
  691. * PropertiesChanged signals are sent automatically after responding on DBus
  692. * message, so if you marked a property changed as a result of DBus call
  693. * (e.g., param setter), you usually do not need to call this function.
  694. */
  695. void wpa_dbus_flush_object_changed_properties(DBusConnection *con,
  696. const char *path)
  697. {
  698. struct wpa_dbus_object_desc *obj_desc = NULL;
  699. const struct wpa_dbus_property_desc *dsc;
  700. int i;
  701. dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
  702. if (!obj_desc)
  703. return;
  704. eloop_cancel_timeout(flush_object_timeout_handler, con, obj_desc);
  705. dsc = obj_desc->properties;
  706. for (dsc = obj_desc->properties, i = 0; dsc && dsc->dbus_property;
  707. dsc++, i++) {
  708. if (obj_desc->prop_changed_flags == NULL ||
  709. !obj_desc->prop_changed_flags[i])
  710. continue;
  711. send_prop_changed_signal(con, path, dsc->dbus_interface,
  712. obj_desc);
  713. }
  714. }
  715. #define WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT 5000
  716. /**
  717. * wpa_dbus_mark_property_changed - Mark a property as changed and
  718. * @iface: dbus priv struct
  719. * @path: path to DBus object which property has changed
  720. * @interface: interface containing changed property
  721. * @property: property name which has changed
  722. *
  723. * Iterates over all properties registered with an object and marks the one
  724. * given in parameters as changed. All parameters registered for an object
  725. * within a single interface will be aggregated together and sent in one
  726. * PropertiesChanged signal when function
  727. * wpa_dbus_flush_object_changed_properties() is called.
  728. */
  729. void wpa_dbus_mark_property_changed(struct wpas_dbus_priv *iface,
  730. const char *path, const char *interface,
  731. const char *property)
  732. {
  733. struct wpa_dbus_object_desc *obj_desc = NULL;
  734. const struct wpa_dbus_property_desc *dsc;
  735. int i = 0;
  736. if (iface == NULL)
  737. return;
  738. dbus_connection_get_object_path_data(iface->con, path,
  739. (void **) &obj_desc);
  740. if (!obj_desc) {
  741. wpa_printf(MSG_ERROR, "dbus: wpa_dbus_property_changed: "
  742. "could not obtain object's private data: %s", path);
  743. return;
  744. }
  745. for (dsc = obj_desc->properties; dsc && dsc->dbus_property; dsc++, i++)
  746. if (os_strcmp(property, dsc->dbus_property) == 0 &&
  747. os_strcmp(interface, dsc->dbus_interface) == 0) {
  748. if (obj_desc->prop_changed_flags)
  749. obj_desc->prop_changed_flags[i] = 1;
  750. break;
  751. }
  752. if (!dsc || !dsc->dbus_property) {
  753. wpa_printf(MSG_ERROR, "dbus: wpa_dbus_property_changed: "
  754. "no property %s in object %s", property, path);
  755. return;
  756. }
  757. if (!eloop_is_timeout_registered(flush_object_timeout_handler,
  758. iface->con, obj_desc->path)) {
  759. eloop_register_timeout(0, WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT,
  760. flush_object_timeout_handler,
  761. iface->con, obj_desc);
  762. }
  763. }
  764. /**
  765. * wpa_dbus_get_object_properties - Put object's properties into dictionary
  766. * @iface: dbus priv struct
  767. * @path: path to DBus object which properties will be obtained
  768. * @interface: interface name which properties will be obtained
  769. * @iter: DBus message iter at which to append property dictionary.
  770. *
  771. * Iterates over all properties registered with object and execute getters
  772. * of those, which are readable and which interface matches interface
  773. * specified as argument. Obtained properties values are stored in
  774. * dict_iter dictionary.
  775. */
  776. dbus_bool_t wpa_dbus_get_object_properties(struct wpas_dbus_priv *iface,
  777. const char *path,
  778. const char *interface,
  779. DBusMessageIter *iter)
  780. {
  781. struct wpa_dbus_object_desc *obj_desc = NULL;
  782. DBusMessageIter dict_iter;
  783. DBusError error;
  784. dbus_connection_get_object_path_data(iface->con, path,
  785. (void **) &obj_desc);
  786. if (!obj_desc) {
  787. wpa_printf(MSG_ERROR, "dbus: %s: could not obtain object's "
  788. "private data: %s", __func__, path);
  789. return FALSE;
  790. }
  791. if (!wpa_dbus_dict_open_write(iter, &dict_iter)) {
  792. wpa_printf(MSG_ERROR, "dbus: %s: failed to open message dict",
  793. __func__);
  794. return FALSE;
  795. }
  796. dbus_error_init(&error);
  797. if (!fill_dict_with_properties(&dict_iter, obj_desc->properties,
  798. interface, obj_desc->user_data,
  799. &error)) {
  800. wpa_printf(MSG_ERROR, "dbus: %s: failed to get object"
  801. " properties: (%s) %s", __func__,
  802. dbus_error_is_set(&error) ? error.name : "none",
  803. dbus_error_is_set(&error) ? error.message : "none");
  804. dbus_error_free(&error);
  805. return FALSE;
  806. }
  807. return wpa_dbus_dict_close_write(iter, &dict_iter);
  808. }
  809. /**
  810. * wpas_dbus_new_decompose_object_path - Decompose an interface object path into parts
  811. * @path: The dbus object path
  812. * @p2p_persistent_group: indicates whether to parse the path as a P2P
  813. * persistent group object
  814. * @network: (out) the configured network this object path refers to, if any
  815. * @bssid: (out) the scanned bssid this object path refers to, if any
  816. * Returns: The object path of the network interface this path refers to
  817. *
  818. * For a given object path, decomposes the object path into object id, network,
  819. * and BSSID parts, if those parts exist.
  820. */
  821. char *wpas_dbus_new_decompose_object_path(const char *path,
  822. int p2p_persistent_group,
  823. char **network,
  824. char **bssid)
  825. {
  826. const unsigned int dev_path_prefix_len =
  827. os_strlen(WPAS_DBUS_NEW_PATH_INTERFACES "/");
  828. char *obj_path_only;
  829. char *next_sep;
  830. /* Be a bit paranoid about path */
  831. if (!path || os_strncmp(path, WPAS_DBUS_NEW_PATH_INTERFACES "/",
  832. dev_path_prefix_len))
  833. return NULL;
  834. /* Ensure there's something at the end of the path */
  835. if ((path + dev_path_prefix_len)[0] == '\0')
  836. return NULL;
  837. obj_path_only = os_strdup(path);
  838. if (obj_path_only == NULL)
  839. return NULL;
  840. next_sep = os_strchr(obj_path_only + dev_path_prefix_len, '/');
  841. if (next_sep != NULL) {
  842. const char *net_part = os_strstr(
  843. next_sep, p2p_persistent_group ?
  844. WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/" :
  845. WPAS_DBUS_NEW_NETWORKS_PART "/");
  846. const char *bssid_part = os_strstr(
  847. next_sep, WPAS_DBUS_NEW_BSSIDS_PART "/");
  848. if (network && net_part) {
  849. /* Deal with a request for a configured network */
  850. const char *net_name = net_part +
  851. os_strlen(p2p_persistent_group ?
  852. WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART
  853. "/" :
  854. WPAS_DBUS_NEW_NETWORKS_PART "/");
  855. *network = NULL;
  856. if (os_strlen(net_name))
  857. *network = os_strdup(net_name);
  858. } else if (bssid && bssid_part) {
  859. /* Deal with a request for a scanned BSSID */
  860. const char *bssid_name = bssid_part +
  861. os_strlen(WPAS_DBUS_NEW_BSSIDS_PART "/");
  862. if (os_strlen(bssid_name))
  863. *bssid = os_strdup(bssid_name);
  864. else
  865. *bssid = NULL;
  866. }
  867. /* Cut off interface object path before "/" */
  868. *next_sep = '\0';
  869. }
  870. return obj_path_only;
  871. }
  872. /**
  873. * wpas_dbus_reply_new_from_error - Create a new D-Bus error message from a
  874. * dbus error structure
  875. * @message: The original request message for which the error is a reply
  876. * @error: The error containing a name and a descriptive error cause
  877. * @fallback_name: A generic error name if @error was not set
  878. * @fallback_string: A generic error string if @error was not set
  879. * Returns: A new D-Bus error message
  880. *
  881. * Given a DBusMessage structure, creates a new D-Bus error message using
  882. * the error name and string contained in that structure.
  883. */
  884. DBusMessage * wpas_dbus_reply_new_from_error(DBusMessage *message,
  885. DBusError *error,
  886. const char *fallback_name,
  887. const char *fallback_string)
  888. {
  889. if (error && error->name && error->message) {
  890. return dbus_message_new_error(message, error->name,
  891. error->message);
  892. }
  893. if (fallback_name && fallback_string) {
  894. return dbus_message_new_error(message, fallback_name,
  895. fallback_string);
  896. }
  897. return NULL;
  898. }