ubusd_proto.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <arpa/inet.h>
  14. #include <unistd.h>
  15. #include "ubusd.h"
  16. struct blob_buf b;
  17. static struct avl_tree clients;
  18. static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
  19. typedef int (*ubus_cmd_cb)(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr);
  20. static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
  21. [UBUS_ATTR_SIGNATURE] = { .type = BLOB_ATTR_NESTED },
  22. [UBUS_ATTR_OBJTYPE] = { .type = BLOB_ATTR_INT32 },
  23. [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
  24. [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
  25. [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
  26. [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
  27. [UBUS_ATTR_USER] = { .type = BLOB_ATTR_STRING },
  28. [UBUS_ATTR_GROUP] = { .type = BLOB_ATTR_STRING },
  29. };
  30. struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
  31. {
  32. blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
  33. return attrbuf;
  34. }
  35. static void ubus_msg_close_fd(struct ubus_msg_buf *ub)
  36. {
  37. if (ub->fd < 0)
  38. return;
  39. close(ub->fd);
  40. ub->fd = -1;
  41. }
  42. static void ubus_msg_init(struct ubus_msg_buf *ub, uint8_t type, uint16_t seq, uint32_t peer)
  43. {
  44. ub->hdr.version = 0;
  45. ub->hdr.type = type;
  46. ub->hdr.seq = seq;
  47. ub->hdr.peer = peer;
  48. }
  49. static struct ubus_msg_buf *ubus_msg_from_blob(bool shared)
  50. {
  51. return ubus_msg_new(b.head, blob_raw_len(b.head), shared);
  52. }
  53. static struct ubus_msg_buf *ubus_reply_from_blob(struct ubus_msg_buf *ub, bool shared)
  54. {
  55. struct ubus_msg_buf *new;
  56. new = ubus_msg_from_blob(shared);
  57. if (!new)
  58. return NULL;
  59. ubus_msg_init(new, UBUS_MSG_DATA, ub->hdr.seq, ub->hdr.peer);
  60. return new;
  61. }
  62. void
  63. ubus_proto_send_msg_from_blob(struct ubus_client *cl, struct ubus_msg_buf *ub,
  64. uint8_t type)
  65. {
  66. /* keep the fd to be passed if it is UBUS_MSG_INVOKE */
  67. int fd = ub->fd;
  68. ub = ubus_reply_from_blob(ub, true);
  69. if (!ub)
  70. return;
  71. ub->hdr.type = type;
  72. ub->fd = fd;
  73. ubus_msg_send(cl, ub);
  74. ubus_msg_free(ub);
  75. }
  76. static bool ubusd_send_hello(struct ubus_client *cl)
  77. {
  78. struct ubus_msg_buf *ub;
  79. blob_buf_init(&b, 0);
  80. ub = ubus_msg_from_blob(true);
  81. if (!ub)
  82. return false;
  83. ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
  84. ubus_msg_send(cl, ub);
  85. ubus_msg_free(ub);
  86. return true;
  87. }
  88. static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  89. {
  90. ub->hdr.type = UBUS_MSG_DATA;
  91. ubus_msg_send(cl, ub);
  92. return 0;
  93. }
  94. static int ubusd_handle_remove_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  95. {
  96. struct ubus_object *obj;
  97. if (!attr[UBUS_ATTR_OBJID])
  98. return UBUS_STATUS_INVALID_ARGUMENT;
  99. obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
  100. if (!obj)
  101. return UBUS_STATUS_NOT_FOUND;
  102. if (obj->client != cl)
  103. return UBUS_STATUS_PERMISSION_DENIED;
  104. blob_buf_init(&b, 0);
  105. blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
  106. /* check if we're removing the object type as well */
  107. if (obj->type && obj->type->refcount == 1)
  108. blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
  109. ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
  110. ubusd_free_object(obj);
  111. return 0;
  112. }
  113. static int ubusd_handle_add_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  114. {
  115. struct ubus_object *obj;
  116. obj = ubusd_create_object(cl, attr);
  117. if (!obj)
  118. return UBUS_STATUS_INVALID_ARGUMENT;
  119. blob_buf_init(&b, 0);
  120. blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
  121. if (attr[UBUS_ATTR_SIGNATURE] && obj->type)
  122. blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
  123. ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
  124. return 0;
  125. }
  126. static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
  127. {
  128. struct ubus_method *m;
  129. int all_cnt = 0, cnt = 0;
  130. void *s;
  131. if (!obj->type)
  132. return;
  133. blob_buf_init(&b, 0);
  134. blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->path.key);
  135. blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
  136. blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
  137. s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
  138. list_for_each_entry(m, &obj->type->methods, list) {
  139. all_cnt++;
  140. if (!ubusd_acl_check(cl, obj->path.key, blobmsg_name(m->data), UBUS_ACL_ACCESS)) {
  141. blobmsg_add_blob(&b, m->data);
  142. cnt++;
  143. }
  144. }
  145. blob_nest_end(&b, s);
  146. if (cnt || !all_cnt)
  147. ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
  148. }
  149. static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  150. {
  151. struct ubus_object *obj;
  152. char *objpath;
  153. bool found = false;
  154. int len;
  155. if (!attr[UBUS_ATTR_OBJPATH]) {
  156. avl_for_each_element(&path, obj, path)
  157. ubusd_send_obj(cl, ub, obj);
  158. return 0;
  159. }
  160. objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
  161. len = strlen(objpath);
  162. if (objpath[len - 1] != '*') {
  163. obj = avl_find_element(&path, objpath, obj, path);
  164. if (!obj)
  165. return UBUS_STATUS_NOT_FOUND;
  166. ubusd_send_obj(cl, ub, obj);
  167. return 0;
  168. }
  169. objpath[--len] = 0;
  170. obj = avl_find_ge_element(&path, objpath, obj, path);
  171. if (!obj)
  172. return UBUS_STATUS_NOT_FOUND;
  173. while (!strncmp(objpath, obj->path.key, len)) {
  174. found = true;
  175. ubusd_send_obj(cl, ub, obj);
  176. if (obj == avl_last_element(&path, obj, path))
  177. break;
  178. obj = avl_next_element(obj, path);
  179. }
  180. if (!found)
  181. return UBUS_STATUS_NOT_FOUND;
  182. return 0;
  183. }
  184. static void
  185. ubusd_forward_invoke(struct ubus_client *cl, struct ubus_object *obj,
  186. const char *method, struct ubus_msg_buf *ub,
  187. struct blob_attr *data)
  188. {
  189. blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
  190. blob_put_string(&b, UBUS_ATTR_METHOD, method);
  191. if (cl->user)
  192. blob_put_string(&b, UBUS_ATTR_USER, cl->user);
  193. if (cl->group)
  194. blob_put_string(&b, UBUS_ATTR_GROUP, cl->group);
  195. if (data)
  196. blob_put(&b, UBUS_ATTR_DATA, blob_data(data), blob_len(data));
  197. ubus_proto_send_msg_from_blob(obj->client, ub, UBUS_MSG_INVOKE);
  198. }
  199. static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  200. {
  201. struct ubus_object *obj = NULL;
  202. struct ubus_id *id;
  203. const char *method;
  204. if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
  205. return UBUS_STATUS_INVALID_ARGUMENT;
  206. id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
  207. if (!id)
  208. return UBUS_STATUS_NOT_FOUND;
  209. obj = container_of(id, struct ubus_object, id);
  210. method = blob_data(attr[UBUS_ATTR_METHOD]);
  211. if (ubusd_acl_check(cl, obj->path.key, method, UBUS_ACL_ACCESS))
  212. return UBUS_STATUS_PERMISSION_DENIED;
  213. if (!obj->client)
  214. return obj->recv_msg(cl, ub, method, attr[UBUS_ATTR_DATA]);
  215. ub->hdr.peer = cl->id.id;
  216. blob_buf_init(&b, 0);
  217. ubusd_forward_invoke(cl, obj, method, ub, attr[UBUS_ATTR_DATA]);
  218. return -1;
  219. }
  220. static int ubusd_handle_notify(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  221. {
  222. struct ubus_object *obj = NULL;
  223. struct ubus_subscription *s;
  224. struct ubus_id *id;
  225. const char *method;
  226. bool no_reply = false;
  227. void *c;
  228. if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
  229. return UBUS_STATUS_INVALID_ARGUMENT;
  230. if (attr[UBUS_ATTR_NO_REPLY])
  231. no_reply = blob_get_int8(attr[UBUS_ATTR_NO_REPLY]);
  232. id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
  233. if (!id)
  234. return UBUS_STATUS_NOT_FOUND;
  235. obj = container_of(id, struct ubus_object, id);
  236. if (obj->client != cl)
  237. return UBUS_STATUS_PERMISSION_DENIED;
  238. if (!no_reply) {
  239. blob_buf_init(&b, 0);
  240. blob_put_int32(&b, UBUS_ATTR_OBJID, id->id);
  241. c = blob_nest_start(&b, UBUS_ATTR_SUBSCRIBERS);
  242. list_for_each_entry(s, &obj->subscribers, list) {
  243. blob_put_int32(&b, 0, s->subscriber->id.id);
  244. }
  245. blob_nest_end(&b, c);
  246. blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
  247. ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_STATUS);
  248. }
  249. ub->hdr.peer = cl->id.id;
  250. method = blob_data(attr[UBUS_ATTR_METHOD]);
  251. list_for_each_entry(s, &obj->subscribers, list) {
  252. blob_buf_init(&b, 0);
  253. if (no_reply)
  254. blob_put_int8(&b, UBUS_ATTR_NO_REPLY, 1);
  255. ubusd_forward_invoke(cl, s->subscriber, method, ub, attr[UBUS_ATTR_DATA]);
  256. }
  257. return -1;
  258. }
  259. static struct ubus_client *ubusd_get_client_by_id(uint32_t id)
  260. {
  261. struct ubus_id *clid;
  262. clid = ubus_find_id(&clients, id);
  263. if (!clid)
  264. return NULL;
  265. return container_of(clid, struct ubus_client, id);
  266. }
  267. static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  268. {
  269. struct ubus_object *obj;
  270. if (!attr[UBUS_ATTR_OBJID] ||
  271. (ub->hdr.type == UBUS_MSG_STATUS && !attr[UBUS_ATTR_STATUS]) ||
  272. (ub->hdr.type == UBUS_MSG_DATA && !attr[UBUS_ATTR_DATA]))
  273. goto out;
  274. obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
  275. if (!obj)
  276. goto out;
  277. if (cl != obj->client)
  278. goto out;
  279. cl = ubusd_get_client_by_id(ub->hdr.peer);
  280. if (!cl)
  281. goto out;
  282. ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
  283. ubus_msg_send(cl, ub);
  284. out:
  285. return -1;
  286. }
  287. static int ubusd_handle_add_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  288. {
  289. struct ubus_object *obj, *target;
  290. if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
  291. return UBUS_STATUS_INVALID_ARGUMENT;
  292. obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
  293. if (!obj)
  294. return UBUS_STATUS_NOT_FOUND;
  295. if (cl != obj->client)
  296. return UBUS_STATUS_INVALID_ARGUMENT;
  297. target = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_TARGET]));
  298. if (!target || !target->client)
  299. return UBUS_STATUS_NOT_FOUND;
  300. if (cl == target->client)
  301. return UBUS_STATUS_INVALID_ARGUMENT;
  302. if (!target->path.key) {
  303. if (strcmp(target->client->user, cl->user) && strcmp(target->client->group, cl->group))
  304. return UBUS_STATUS_NOT_FOUND;
  305. } else if (ubusd_acl_check(cl, target->path.key, NULL, UBUS_ACL_SUBSCRIBE)) {
  306. return UBUS_STATUS_NOT_FOUND;
  307. }
  308. ubus_subscribe(obj, target);
  309. return 0;
  310. }
  311. static int ubusd_handle_remove_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  312. {
  313. struct ubus_object *obj;
  314. struct ubus_subscription *s;
  315. uint32_t id;
  316. if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
  317. return UBUS_STATUS_INVALID_ARGUMENT;
  318. obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
  319. if (!obj)
  320. return UBUS_STATUS_NOT_FOUND;
  321. if (cl != obj->client)
  322. return UBUS_STATUS_INVALID_ARGUMENT;
  323. id = blob_get_u32(attr[UBUS_ATTR_TARGET]);
  324. list_for_each_entry(s, &obj->target_list, target_list) {
  325. if (s->target->id.id != id)
  326. continue;
  327. ubus_unsubscribe(s);
  328. return 0;
  329. }
  330. return UBUS_STATUS_NOT_FOUND;
  331. }
  332. static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
  333. [UBUS_MSG_PING] = ubusd_send_pong,
  334. [UBUS_MSG_ADD_OBJECT] = ubusd_handle_add_object,
  335. [UBUS_MSG_REMOVE_OBJECT] = ubusd_handle_remove_object,
  336. [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
  337. [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
  338. [UBUS_MSG_STATUS] = ubusd_handle_response,
  339. [UBUS_MSG_DATA] = ubusd_handle_response,
  340. [UBUS_MSG_SUBSCRIBE] = ubusd_handle_add_watch,
  341. [UBUS_MSG_UNSUBSCRIBE] = ubusd_handle_remove_watch,
  342. [UBUS_MSG_NOTIFY] = ubusd_handle_notify,
  343. };
  344. void ubusd_proto_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
  345. {
  346. ubus_cmd_cb cb = NULL;
  347. int ret;
  348. struct ubus_msg_buf *retmsg = cl->retmsg;
  349. int *retmsg_data = blob_data(blob_data(retmsg->data));
  350. retmsg->hdr.seq = ub->hdr.seq;
  351. retmsg->hdr.peer = ub->hdr.peer;
  352. if (ub->hdr.type < __UBUS_MSG_LAST)
  353. cb = handlers[ub->hdr.type];
  354. if (ub->hdr.type != UBUS_MSG_STATUS && ub->hdr.type != UBUS_MSG_INVOKE)
  355. ubus_msg_close_fd(ub);
  356. /* Note: no callback should free the `ub` buffer
  357. that's always done right after the callback finishes */
  358. if (cb)
  359. ret = cb(cl, ub, ubus_parse_msg(ub->data));
  360. else
  361. ret = UBUS_STATUS_INVALID_COMMAND;
  362. ubus_msg_free(ub);
  363. if (ret == -1)
  364. return;
  365. *retmsg_data = htonl(ret);
  366. ubus_msg_send(cl, retmsg);
  367. }
  368. static int ubusd_proto_init_retmsg(struct ubus_client *cl)
  369. {
  370. struct blob_buf *b = &cl->b;
  371. blob_buf_init(&cl->b, 0);
  372. blob_put_int32(&cl->b, UBUS_ATTR_STATUS, 0);
  373. /* we make the 'retmsg' buffer shared with the blob_buf b, to reduce mem duplication */
  374. cl->retmsg = ubus_msg_new(b->head, blob_raw_len(b->head), true);
  375. if (!cl->retmsg)
  376. return -1;
  377. cl->retmsg->hdr.type = UBUS_MSG_STATUS;
  378. return 0;
  379. }
  380. struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
  381. {
  382. struct ubus_client *cl;
  383. cl = calloc(1, sizeof(*cl));
  384. if (!cl)
  385. return NULL;
  386. if (ubusd_acl_init_client(cl, fd))
  387. goto free;
  388. INIT_LIST_HEAD(&cl->objects);
  389. cl->sock.fd = fd;
  390. cl->sock.cb = cb;
  391. cl->pending_msg_fd = -1;
  392. if (!ubus_alloc_id(&clients, &cl->id, 0))
  393. goto free;
  394. if (ubusd_proto_init_retmsg(cl))
  395. goto free;
  396. if (!ubusd_send_hello(cl))
  397. goto delete;
  398. return cl;
  399. delete:
  400. ubus_free_id(&clients, &cl->id);
  401. free:
  402. free(cl);
  403. return NULL;
  404. }
  405. void ubusd_proto_free_client(struct ubus_client *cl)
  406. {
  407. struct ubus_object *obj;
  408. while (!list_empty(&cl->objects)) {
  409. obj = list_first_entry(&cl->objects, struct ubus_object, list);
  410. ubusd_free_object(obj);
  411. }
  412. ubus_msg_free(cl->retmsg);
  413. blob_buf_free(&cl->b);
  414. ubusd_acl_free_client(cl);
  415. ubus_free_id(&clients, &cl->id);
  416. }
  417. void ubus_notify_subscription(struct ubus_object *obj)
  418. {
  419. bool active = !list_empty(&obj->subscribers);
  420. struct ubus_msg_buf *ub;
  421. blob_buf_init(&b, 0);
  422. blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
  423. blob_put_int8(&b, UBUS_ATTR_ACTIVE, active);
  424. ub = ubus_msg_from_blob(false);
  425. if (!ub)
  426. return;
  427. ubus_msg_init(ub, UBUS_MSG_NOTIFY, ++obj->invoke_seq, 0);
  428. ubus_msg_send(obj->client, ub);
  429. ubus_msg_free(ub);
  430. }
  431. void ubus_notify_unsubscribe(struct ubus_subscription *s)
  432. {
  433. struct ubus_msg_buf *ub;
  434. blob_buf_init(&b, 0);
  435. blob_put_int32(&b, UBUS_ATTR_OBJID, s->subscriber->id.id);
  436. blob_put_int32(&b, UBUS_ATTR_TARGET, s->target->id.id);
  437. ub = ubus_msg_from_blob(false);
  438. if (ub != NULL) {
  439. ubus_msg_init(ub, UBUS_MSG_UNSUBSCRIBE, ++s->subscriber->invoke_seq, 0);
  440. ubus_msg_send(s->subscriber->client, ub);
  441. ubus_msg_free(ub);
  442. }
  443. ubus_unsubscribe(s);
  444. }
  445. static void __constructor ubusd_proto_init(void)
  446. {
  447. ubus_init_id_tree(&clients);
  448. }