dbus_new_helpers.c 30 KB

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