session.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  1. /*
  2. * rpcd - UBUS RPC server
  3. *
  4. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  5. * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #define _GNU_SOURCE /* crypt() */
  20. #include <libubox/avl-cmp.h>
  21. #include <libubox/blobmsg.h>
  22. #include <libubox/utils.h>
  23. #include <libubus.h>
  24. #include <fnmatch.h>
  25. #include <glob.h>
  26. #include <uci.h>
  27. #include <limits.h>
  28. #ifdef HAVE_SHADOW
  29. #include <shadow.h>
  30. #endif
  31. #include <rpcd/session.h>
  32. static struct avl_tree sessions;
  33. static struct blob_buf buf;
  34. static LIST_HEAD(create_callbacks);
  35. static LIST_HEAD(destroy_callbacks);
  36. enum {
  37. RPC_SN_TIMEOUT,
  38. __RPC_SN_MAX,
  39. };
  40. static const struct blobmsg_policy new_policy[__RPC_SN_MAX] = {
  41. [RPC_SN_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
  42. };
  43. enum {
  44. RPC_SI_SID,
  45. __RPC_SI_MAX,
  46. };
  47. static const struct blobmsg_policy sid_policy[__RPC_SI_MAX] = {
  48. [RPC_SI_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
  49. };
  50. enum {
  51. RPC_SS_SID,
  52. RPC_SS_VALUES,
  53. __RPC_SS_MAX,
  54. };
  55. static const struct blobmsg_policy set_policy[__RPC_SS_MAX] = {
  56. [RPC_SS_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
  57. [RPC_SS_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
  58. };
  59. enum {
  60. RPC_SG_SID,
  61. RPC_SG_KEYS,
  62. __RPC_SG_MAX,
  63. };
  64. static const struct blobmsg_policy get_policy[__RPC_SG_MAX] = {
  65. [RPC_SG_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
  66. [RPC_SG_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
  67. };
  68. enum {
  69. RPC_SA_SID,
  70. RPC_SA_SCOPE,
  71. RPC_SA_OBJECTS,
  72. __RPC_SA_MAX,
  73. };
  74. static const struct blobmsg_policy acl_policy[__RPC_SA_MAX] = {
  75. [RPC_SA_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
  76. [RPC_SA_SCOPE] = { .name = "scope", .type = BLOBMSG_TYPE_STRING },
  77. [RPC_SA_OBJECTS] = { .name = "objects", .type = BLOBMSG_TYPE_ARRAY },
  78. };
  79. enum {
  80. RPC_SP_SID,
  81. RPC_SP_SCOPE,
  82. RPC_SP_OBJECT,
  83. RPC_SP_FUNCTION,
  84. __RPC_SP_MAX,
  85. };
  86. static const struct blobmsg_policy perm_policy[__RPC_SP_MAX] = {
  87. [RPC_SP_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
  88. [RPC_SP_SCOPE] = { .name = "scope", .type = BLOBMSG_TYPE_STRING },
  89. [RPC_SP_OBJECT] = { .name = "object", .type = BLOBMSG_TYPE_STRING },
  90. [RPC_SP_FUNCTION] = { .name = "function", .type = BLOBMSG_TYPE_STRING },
  91. };
  92. enum {
  93. RPC_DUMP_SID,
  94. RPC_DUMP_TIMEOUT,
  95. RPC_DUMP_EXPIRES,
  96. RPC_DUMP_DATA,
  97. __RPC_DUMP_MAX,
  98. };
  99. static const struct blobmsg_policy dump_policy[__RPC_DUMP_MAX] = {
  100. [RPC_DUMP_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
  101. [RPC_DUMP_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
  102. [RPC_DUMP_EXPIRES] = { .name = "expires", .type = BLOBMSG_TYPE_INT64 },
  103. [RPC_DUMP_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
  104. };
  105. enum {
  106. RPC_L_USERNAME,
  107. RPC_L_PASSWORD,
  108. RPC_L_TIMEOUT,
  109. __RPC_L_MAX,
  110. };
  111. static const struct blobmsg_policy login_policy[__RPC_L_MAX] = {
  112. [RPC_L_USERNAME] = { .name = "username", .type = BLOBMSG_TYPE_STRING },
  113. [RPC_L_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
  114. [RPC_L_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
  115. };
  116. /*
  117. * Keys in the AVL tree contain all pattern characters up to the first wildcard.
  118. * To look up entries, start with the last entry that has a key less than or
  119. * equal to the method name, then work backwards as long as the AVL key still
  120. * matches its counterpart in the object name
  121. */
  122. #define uh_foreach_matching_acl_prefix(_acl, _avl, _obj, _func) \
  123. for (_acl = avl_find_le_element(_avl, _obj, _acl, avl); \
  124. _acl; \
  125. _acl = avl_is_first(_avl, &(_acl)->avl) ? NULL : \
  126. avl_prev_element((_acl), avl))
  127. #define uh_foreach_matching_acl(_acl, _avl, _obj, _func) \
  128. uh_foreach_matching_acl_prefix(_acl, _avl, _obj, _func) \
  129. if (!strncmp((_acl)->object, _obj, (_acl)->sort_len) && \
  130. !fnmatch((_acl)->object, (_obj), FNM_NOESCAPE) && \
  131. !fnmatch((_acl)->function, (_func), FNM_NOESCAPE))
  132. static int
  133. rpc_random(char *dest)
  134. {
  135. unsigned char buf[16] = { 0 };
  136. FILE *f;
  137. int i;
  138. int ret;
  139. f = fopen("/dev/urandom", "r");
  140. if (!f)
  141. return -1;
  142. ret = fread(buf, 1, sizeof(buf), f);
  143. fclose(f);
  144. if (ret < 0)
  145. return ret;
  146. for (i = 0; i < sizeof(buf); i++)
  147. sprintf(dest + (i<<1), "%02x", buf[i]);
  148. return 0;
  149. }
  150. static void
  151. rpc_session_dump_data(struct rpc_session *ses, struct blob_buf *b)
  152. {
  153. struct rpc_session_data *d;
  154. avl_for_each_element(&ses->data, d, avl) {
  155. blobmsg_add_field(b, blobmsg_type(d->attr), blobmsg_name(d->attr),
  156. blobmsg_data(d->attr), blobmsg_data_len(d->attr));
  157. }
  158. }
  159. static void
  160. rpc_session_dump_acls(struct rpc_session *ses, struct blob_buf *b)
  161. {
  162. struct rpc_session_acl *acl;
  163. struct rpc_session_acl_scope *acl_scope;
  164. const char *lastobj = NULL;
  165. const char *lastscope = NULL;
  166. void *c = NULL, *d = NULL;
  167. avl_for_each_element(&ses->acls, acl_scope, avl) {
  168. if (!lastscope || strcmp(acl_scope->avl.key, lastscope))
  169. {
  170. if (c) blobmsg_close_table(b, c);
  171. c = blobmsg_open_table(b, acl_scope->avl.key);
  172. lastobj = NULL;
  173. }
  174. d = NULL;
  175. avl_for_each_element(&acl_scope->acls, acl, avl) {
  176. if (!lastobj || strcmp(acl->object, lastobj))
  177. {
  178. if (d) blobmsg_close_array(b, d);
  179. d = blobmsg_open_array(b, acl->object);
  180. }
  181. blobmsg_add_string(b, NULL, acl->function);
  182. lastobj = acl->object;
  183. }
  184. if (d) blobmsg_close_array(b, d);
  185. }
  186. if (c) blobmsg_close_table(b, c);
  187. }
  188. static void
  189. rpc_session_to_blob(struct rpc_session *ses, bool acls)
  190. {
  191. void *c;
  192. blob_buf_init(&buf, 0);
  193. blobmsg_add_string(&buf, "ubus_rpc_session", ses->id);
  194. blobmsg_add_u32(&buf, "timeout", ses->timeout);
  195. blobmsg_add_u64(&buf, "expires", uloop_timeout_remaining64(&ses->t) / 1000);
  196. if (acls) {
  197. c = blobmsg_open_table(&buf, "acls");
  198. rpc_session_dump_acls(ses, &buf);
  199. blobmsg_close_table(&buf, c);
  200. }
  201. c = blobmsg_open_table(&buf, "data");
  202. rpc_session_dump_data(ses, &buf);
  203. blobmsg_close_table(&buf, c);
  204. }
  205. static void
  206. rpc_session_dump(struct rpc_session *ses, struct ubus_context *ctx,
  207. struct ubus_request_data *req)
  208. {
  209. rpc_session_to_blob(ses, true);
  210. ubus_send_reply(ctx, req, buf.head);
  211. }
  212. static void
  213. rpc_touch_session(struct rpc_session *ses)
  214. {
  215. if (ses->timeout > 0)
  216. uloop_timeout_set(&ses->t, ses->timeout * 1000);
  217. }
  218. static void
  219. rpc_session_destroy(struct rpc_session *ses)
  220. {
  221. struct rpc_session_acl *acl, *nacl;
  222. struct rpc_session_acl_scope *acl_scope, *nacl_scope;
  223. struct rpc_session_data *data, *ndata;
  224. struct rpc_session_cb *cb;
  225. list_for_each_entry(cb, &destroy_callbacks, list)
  226. cb->cb(ses, cb->priv);
  227. uloop_timeout_cancel(&ses->t);
  228. avl_for_each_element_safe(&ses->acls, acl_scope, avl, nacl_scope) {
  229. avl_remove_all_elements(&acl_scope->acls, acl, avl, nacl)
  230. free(acl);
  231. avl_delete(&ses->acls, &acl_scope->avl);
  232. free(acl_scope);
  233. }
  234. avl_remove_all_elements(&ses->data, data, avl, ndata)
  235. free(data);
  236. avl_delete(&sessions, &ses->avl);
  237. free(ses);
  238. }
  239. static void rpc_session_timeout(struct uloop_timeout *t)
  240. {
  241. struct rpc_session *ses;
  242. ses = container_of(t, struct rpc_session, t);
  243. rpc_session_destroy(ses);
  244. }
  245. static struct rpc_session *
  246. rpc_session_new(void)
  247. {
  248. struct rpc_session *ses;
  249. ses = calloc(1, sizeof(*ses));
  250. if (!ses)
  251. return NULL;
  252. ses->avl.key = ses->id;
  253. avl_init(&ses->acls, avl_strcmp, true, NULL);
  254. avl_init(&ses->data, avl_strcmp, false, NULL);
  255. ses->t.cb = rpc_session_timeout;
  256. return ses;
  257. }
  258. static struct rpc_session *
  259. rpc_session_create(int timeout)
  260. {
  261. struct rpc_session *ses;
  262. struct rpc_session_cb *cb;
  263. ses = rpc_session_new();
  264. if (!ses)
  265. return NULL;
  266. if (rpc_random(ses->id))
  267. return NULL;
  268. ses->timeout = timeout;
  269. avl_insert(&sessions, &ses->avl);
  270. rpc_touch_session(ses);
  271. list_for_each_entry(cb, &create_callbacks, list)
  272. cb->cb(ses, cb->priv);
  273. return ses;
  274. }
  275. static struct rpc_session *
  276. rpc_session_get(const char *id)
  277. {
  278. struct rpc_session *ses;
  279. ses = avl_find_element(&sessions, id, ses, avl);
  280. if (!ses)
  281. return NULL;
  282. rpc_touch_session(ses);
  283. return ses;
  284. }
  285. static int
  286. rpc_handle_create(struct ubus_context *ctx, struct ubus_object *obj,
  287. struct ubus_request_data *req, const char *method,
  288. struct blob_attr *msg)
  289. {
  290. struct rpc_session *ses;
  291. struct blob_attr *tb;
  292. int timeout = RPC_DEFAULT_SESSION_TIMEOUT;
  293. blobmsg_parse(new_policy, __RPC_SN_MAX, &tb, blob_data(msg), blob_len(msg));
  294. if (tb)
  295. timeout = blobmsg_get_u32(tb);
  296. ses = rpc_session_create(timeout);
  297. if (ses)
  298. rpc_session_dump(ses, ctx, req);
  299. return 0;
  300. }
  301. static int
  302. rpc_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
  303. struct ubus_request_data *req, const char *method,
  304. struct blob_attr *msg)
  305. {
  306. struct rpc_session *ses;
  307. struct blob_attr *tb;
  308. blobmsg_parse(sid_policy, __RPC_SI_MAX, &tb, blob_data(msg), blob_len(msg));
  309. if (!tb) {
  310. avl_for_each_element(&sessions, ses, avl)
  311. rpc_session_dump(ses, ctx, req);
  312. return 0;
  313. }
  314. ses = rpc_session_get(blobmsg_data(tb));
  315. if (!ses)
  316. return UBUS_STATUS_NOT_FOUND;
  317. rpc_session_dump(ses, ctx, req);
  318. return 0;
  319. }
  320. static int
  321. uh_id_len(const char *str)
  322. {
  323. return strcspn(str, "*?[");
  324. }
  325. static int
  326. rpc_session_grant(struct rpc_session *ses,
  327. const char *scope, const char *object, const char *function)
  328. {
  329. struct rpc_session_acl *acl;
  330. struct rpc_session_acl_scope *acl_scope;
  331. char *new_scope, *new_obj, *new_func, *new_id;
  332. int id_len;
  333. if (!object || !function)
  334. return UBUS_STATUS_INVALID_ARGUMENT;
  335. acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
  336. if (acl_scope) {
  337. uh_foreach_matching_acl_prefix(acl, &acl_scope->acls, object, function) {
  338. if (!strcmp(acl->object, object) &&
  339. !strcmp(acl->function, function))
  340. return 0;
  341. }
  342. }
  343. if (!acl_scope) {
  344. acl_scope = calloc_a(sizeof(*acl_scope),
  345. &new_scope, strlen(scope) + 1);
  346. if (!acl_scope)
  347. return UBUS_STATUS_UNKNOWN_ERROR;
  348. acl_scope->avl.key = strcpy(new_scope, scope);
  349. avl_init(&acl_scope->acls, avl_strcmp, true, NULL);
  350. avl_insert(&ses->acls, &acl_scope->avl);
  351. }
  352. id_len = uh_id_len(object);
  353. acl = calloc_a(sizeof(*acl),
  354. &new_obj, strlen(object) + 1,
  355. &new_func, strlen(function) + 1,
  356. &new_id, id_len + 1);
  357. if (!acl)
  358. return UBUS_STATUS_UNKNOWN_ERROR;
  359. acl->object = strcpy(new_obj, object);
  360. acl->function = strcpy(new_func, function);
  361. acl->avl.key = strncpy(new_id, object, id_len);
  362. avl_insert(&acl_scope->acls, &acl->avl);
  363. return 0;
  364. }
  365. static int
  366. rpc_session_revoke(struct rpc_session *ses,
  367. const char *scope, const char *object, const char *function)
  368. {
  369. struct rpc_session_acl *acl, *next;
  370. struct rpc_session_acl_scope *acl_scope;
  371. int id_len;
  372. char *id;
  373. acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
  374. if (!acl_scope)
  375. return 0;
  376. if (!object && !function) {
  377. avl_remove_all_elements(&acl_scope->acls, acl, avl, next)
  378. free(acl);
  379. avl_delete(&ses->acls, &acl_scope->avl);
  380. free(acl_scope);
  381. return 0;
  382. }
  383. id_len = uh_id_len(object);
  384. id = alloca(id_len + 1);
  385. strncpy(id, object, id_len);
  386. id[id_len] = 0;
  387. acl = avl_find_element(&acl_scope->acls, id, acl, avl);
  388. while (acl) {
  389. if (!avl_is_last(&acl_scope->acls, &acl->avl))
  390. next = avl_next_element(acl, avl);
  391. else
  392. next = NULL;
  393. if (strcmp(id, acl->avl.key) != 0)
  394. break;
  395. if (!strcmp(acl->object, object) &&
  396. !strcmp(acl->function, function)) {
  397. avl_delete(&acl_scope->acls, &acl->avl);
  398. free(acl);
  399. }
  400. acl = next;
  401. }
  402. if (avl_is_empty(&acl_scope->acls)) {
  403. avl_delete(&ses->acls, &acl_scope->avl);
  404. free(acl_scope);
  405. }
  406. return 0;
  407. }
  408. static int
  409. rpc_handle_acl(struct ubus_context *ctx, struct ubus_object *obj,
  410. struct ubus_request_data *req, const char *method,
  411. struct blob_attr *msg)
  412. {
  413. struct rpc_session *ses;
  414. struct blob_attr *tb[__RPC_SA_MAX];
  415. struct blob_attr *attr, *sattr;
  416. const char *object, *function;
  417. const char *scope = "ubus";
  418. int rem1, rem2;
  419. int (*cb)(struct rpc_session *ses,
  420. const char *scope, const char *object, const char *function);
  421. blobmsg_parse(acl_policy, __RPC_SA_MAX, tb, blob_data(msg), blob_len(msg));
  422. if (!tb[RPC_SA_SID])
  423. return UBUS_STATUS_INVALID_ARGUMENT;
  424. ses = rpc_session_get(blobmsg_data(tb[RPC_SA_SID]));
  425. if (!ses)
  426. return UBUS_STATUS_NOT_FOUND;
  427. if (tb[RPC_SA_SCOPE])
  428. scope = blobmsg_data(tb[RPC_SA_SCOPE]);
  429. if (!strcmp(method, "grant"))
  430. cb = rpc_session_grant;
  431. else
  432. cb = rpc_session_revoke;
  433. if (!tb[RPC_SA_OBJECTS])
  434. return cb(ses, scope, NULL, NULL);
  435. blobmsg_for_each_attr(attr, tb[RPC_SA_OBJECTS], rem1) {
  436. if (blobmsg_type(attr) != BLOBMSG_TYPE_ARRAY)
  437. continue;
  438. object = NULL;
  439. function = NULL;
  440. blobmsg_for_each_attr(sattr, attr, rem2) {
  441. if (blobmsg_type(sattr) != BLOBMSG_TYPE_STRING)
  442. continue;
  443. if (!object)
  444. object = blobmsg_data(sattr);
  445. else if (!function)
  446. function = blobmsg_data(sattr);
  447. else
  448. break;
  449. }
  450. if (object && function)
  451. cb(ses, scope, object, function);
  452. }
  453. return 0;
  454. }
  455. static bool
  456. rpc_session_acl_allowed(struct rpc_session *ses, const char *scope,
  457. const char *obj, const char *fun)
  458. {
  459. struct rpc_session_acl *acl;
  460. struct rpc_session_acl_scope *acl_scope;
  461. acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
  462. if (acl_scope) {
  463. uh_foreach_matching_acl(acl, &acl_scope->acls, obj, fun)
  464. return true;
  465. }
  466. return false;
  467. }
  468. static int
  469. rpc_handle_access(struct ubus_context *ctx, struct ubus_object *obj,
  470. struct ubus_request_data *req, const char *method,
  471. struct blob_attr *msg)
  472. {
  473. struct rpc_session *ses;
  474. struct blob_attr *tb[__RPC_SP_MAX];
  475. const char *scope = "ubus";
  476. bool allow;
  477. blobmsg_parse(perm_policy, __RPC_SP_MAX, tb, blob_data(msg), blob_len(msg));
  478. if (!tb[RPC_SP_SID])
  479. return UBUS_STATUS_INVALID_ARGUMENT;
  480. ses = rpc_session_get(blobmsg_data(tb[RPC_SP_SID]));
  481. if (!ses)
  482. return UBUS_STATUS_NOT_FOUND;
  483. blob_buf_init(&buf, 0);
  484. if (tb[RPC_SP_OBJECT] && tb[RPC_SP_FUNCTION])
  485. {
  486. if (tb[RPC_SP_SCOPE])
  487. scope = blobmsg_data(tb[RPC_SP_SCOPE]);
  488. allow = rpc_session_acl_allowed(ses, scope,
  489. blobmsg_data(tb[RPC_SP_OBJECT]),
  490. blobmsg_data(tb[RPC_SP_FUNCTION]));
  491. blobmsg_add_u8(&buf, "access", allow);
  492. }
  493. else
  494. {
  495. rpc_session_dump_acls(ses, &buf);
  496. }
  497. ubus_send_reply(ctx, req, buf.head);
  498. return 0;
  499. }
  500. static void
  501. rpc_session_set(struct rpc_session *ses, struct blob_attr *val)
  502. {
  503. struct rpc_session_data *data;
  504. data = avl_find_element(&ses->data, blobmsg_name(val), data, avl);
  505. if (data) {
  506. avl_delete(&ses->data, &data->avl);
  507. free(data);
  508. }
  509. data = calloc(1, sizeof(*data) + blob_pad_len(val));
  510. if (!data)
  511. return;
  512. memcpy(data->attr, val, blob_pad_len(val));
  513. data->avl.key = blobmsg_name(data->attr);
  514. avl_insert(&ses->data, &data->avl);
  515. }
  516. static int
  517. rpc_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
  518. struct ubus_request_data *req, const char *method,
  519. struct blob_attr *msg)
  520. {
  521. struct rpc_session *ses;
  522. struct blob_attr *tb[__RPC_SS_MAX];
  523. struct blob_attr *attr;
  524. int rem;
  525. blobmsg_parse(set_policy, __RPC_SS_MAX, tb, blob_data(msg), blob_len(msg));
  526. if (!tb[RPC_SS_SID] || !tb[RPC_SS_VALUES])
  527. return UBUS_STATUS_INVALID_ARGUMENT;
  528. ses = rpc_session_get(blobmsg_data(tb[RPC_SS_SID]));
  529. if (!ses)
  530. return UBUS_STATUS_NOT_FOUND;
  531. blobmsg_for_each_attr(attr, tb[RPC_SS_VALUES], rem) {
  532. if (!blobmsg_name(attr)[0])
  533. continue;
  534. rpc_session_set(ses, attr);
  535. }
  536. return 0;
  537. }
  538. static int
  539. rpc_handle_get(struct ubus_context *ctx, struct ubus_object *obj,
  540. struct ubus_request_data *req, const char *method,
  541. struct blob_attr *msg)
  542. {
  543. struct rpc_session *ses;
  544. struct rpc_session_data *data;
  545. struct blob_attr *tb[__RPC_SG_MAX];
  546. struct blob_attr *attr;
  547. void *c;
  548. int rem;
  549. blobmsg_parse(get_policy, __RPC_SG_MAX, tb, blob_data(msg), blob_len(msg));
  550. if (!tb[RPC_SG_SID])
  551. return UBUS_STATUS_INVALID_ARGUMENT;
  552. ses = rpc_session_get(blobmsg_data(tb[RPC_SG_SID]));
  553. if (!ses)
  554. return UBUS_STATUS_NOT_FOUND;
  555. blob_buf_init(&buf, 0);
  556. c = blobmsg_open_table(&buf, "values");
  557. if (tb[RPC_SG_KEYS])
  558. blobmsg_for_each_attr(attr, tb[RPC_SG_KEYS], rem) {
  559. if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
  560. continue;
  561. data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
  562. if (!data)
  563. continue;
  564. blobmsg_add_field(&buf, blobmsg_type(data->attr),
  565. blobmsg_name(data->attr),
  566. blobmsg_data(data->attr),
  567. blobmsg_data_len(data->attr));
  568. }
  569. else
  570. rpc_session_dump_data(ses, &buf);
  571. blobmsg_close_table(&buf, c);
  572. ubus_send_reply(ctx, req, buf.head);
  573. return 0;
  574. }
  575. static int
  576. rpc_handle_unset(struct ubus_context *ctx, struct ubus_object *obj,
  577. struct ubus_request_data *req, const char *method,
  578. struct blob_attr *msg)
  579. {
  580. struct rpc_session *ses;
  581. struct rpc_session_data *data, *ndata;
  582. struct blob_attr *tb[__RPC_SA_MAX];
  583. struct blob_attr *attr;
  584. int rem;
  585. blobmsg_parse(get_policy, __RPC_SG_MAX, tb, blob_data(msg), blob_len(msg));
  586. if (!tb[RPC_SG_SID])
  587. return UBUS_STATUS_INVALID_ARGUMENT;
  588. ses = rpc_session_get(blobmsg_data(tb[RPC_SG_SID]));
  589. if (!ses)
  590. return UBUS_STATUS_NOT_FOUND;
  591. if (!tb[RPC_SG_KEYS]) {
  592. avl_remove_all_elements(&ses->data, data, avl, ndata)
  593. free(data);
  594. return 0;
  595. }
  596. blobmsg_for_each_attr(attr, tb[RPC_SG_KEYS], rem) {
  597. if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
  598. continue;
  599. data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
  600. if (!data)
  601. continue;
  602. avl_delete(&ses->data, &data->avl);
  603. free(data);
  604. }
  605. return 0;
  606. }
  607. static int
  608. rpc_handle_destroy(struct ubus_context *ctx, struct ubus_object *obj,
  609. struct ubus_request_data *req, const char *method,
  610. struct blob_attr *msg)
  611. {
  612. struct rpc_session *ses;
  613. struct blob_attr *tb;
  614. blobmsg_parse(sid_policy, __RPC_SI_MAX, &tb, blob_data(msg), blob_len(msg));
  615. if (!tb)
  616. return UBUS_STATUS_INVALID_ARGUMENT;
  617. if (!strcmp(blobmsg_get_string(tb), RPC_DEFAULT_SESSION_ID))
  618. return UBUS_STATUS_PERMISSION_DENIED;
  619. ses = rpc_session_get(blobmsg_data(tb));
  620. if (!ses)
  621. return UBUS_STATUS_NOT_FOUND;
  622. rpc_session_destroy(ses);
  623. return 0;
  624. }
  625. static bool
  626. rpc_login_test_password(const char *hash, const char *password)
  627. {
  628. char *crypt_hash;
  629. /* password is not set */
  630. if (!hash || !*hash)
  631. {
  632. return true;
  633. }
  634. /* password hash refers to shadow/passwd */
  635. else if (!strncmp(hash, "$p$", 3))
  636. {
  637. #ifdef HAVE_SHADOW
  638. struct spwd *sp = getspnam(hash + 3);
  639. if (!sp)
  640. return false;
  641. return rpc_login_test_password(sp->sp_pwdp, password);
  642. #else
  643. struct passwd *pw = getpwnam(hash + 3);
  644. if (!pw)
  645. return false;
  646. return rpc_login_test_password(pw->pw_passwd, password);
  647. #endif
  648. }
  649. crypt_hash = crypt(password, hash);
  650. return (crypt_hash && !strcmp(crypt_hash, hash));
  651. }
  652. static struct uci_section *
  653. rpc_login_test_login(struct uci_context *uci,
  654. const char *username, const char *password)
  655. {
  656. struct uci_package *p = NULL;
  657. struct uci_section *s;
  658. struct uci_element *e;
  659. struct uci_ptr ptr = { .package = "rpcd" };
  660. if (!uci_lookup_ptr(uci, &ptr, NULL, false) && ptr.p) {
  661. uci_unload(uci, ptr.p);
  662. ptr.flags = 0;
  663. ptr.p = NULL;
  664. }
  665. uci_load(uci, ptr.package, &p);
  666. if (!p)
  667. return false;
  668. uci_foreach_element(&p->sections, e)
  669. {
  670. s = uci_to_section(e);
  671. if (strcmp(s->type, "login"))
  672. continue;
  673. ptr.section = s->e.name;
  674. ptr.s = NULL;
  675. /* test for matching username */
  676. ptr.option = "username";
  677. ptr.o = NULL;
  678. if (uci_lookup_ptr(uci, &ptr, NULL, true))
  679. continue;
  680. if (ptr.o->type != UCI_TYPE_STRING)
  681. continue;
  682. if (strcmp(ptr.o->v.string, username))
  683. continue;
  684. /* If password is NULL, we're restoring ACLs for an existing session,
  685. * in this case do not check the password again. */
  686. if (!password)
  687. return ptr.s;
  688. /* test for matching password */
  689. ptr.option = "password";
  690. ptr.o = NULL;
  691. if (uci_lookup_ptr(uci, &ptr, NULL, true))
  692. continue;
  693. if (ptr.o->type != UCI_TYPE_STRING)
  694. continue;
  695. if (rpc_login_test_password(ptr.o->v.string, password))
  696. return ptr.s;
  697. }
  698. return NULL;
  699. }
  700. static bool
  701. rpc_login_test_permission(struct uci_section *s,
  702. const char *perm, const char *group)
  703. {
  704. const char *p;
  705. struct uci_option *o;
  706. struct uci_element *e, *l;
  707. /* If the login section is not provided, we're setting up acls for the
  708. * default session, in this case uncondionally allow access to the
  709. * "unauthenticated" access group */
  710. if (!s) {
  711. return !strcmp(group, "unauthenticated");
  712. }
  713. uci_foreach_element(&s->options, e)
  714. {
  715. o = uci_to_option(e);
  716. if (o->type != UCI_TYPE_LIST)
  717. continue;
  718. if (strcmp(o->e.name, perm))
  719. continue;
  720. /* Match negative expressions first. If a negative expression matches
  721. * the current group name then deny access. */
  722. uci_foreach_element(&o->v.list, l) {
  723. p = l->name;
  724. if (!p || *p != '!')
  725. continue;
  726. while (isspace(*++p));
  727. if (!*p)
  728. continue;
  729. if (!fnmatch(p, group, 0))
  730. return false;
  731. }
  732. uci_foreach_element(&o->v.list, l) {
  733. if (!l->name || !*l->name || *l->name == '!')
  734. continue;
  735. if (!fnmatch(l->name, group, 0))
  736. return true;
  737. }
  738. }
  739. /* make sure that write permission implies read permission */
  740. if (!strcmp(perm, "read"))
  741. return rpc_login_test_permission(s, "write", group);
  742. return false;
  743. }
  744. static void
  745. rpc_login_setup_acl_scope(struct rpc_session *ses,
  746. struct blob_attr *acl_perm,
  747. struct blob_attr *acl_scope)
  748. {
  749. struct blob_attr *acl_obj, *acl_func;
  750. int rem, rem2;
  751. /*
  752. * Parse ACL scopes in table notation.
  753. *
  754. * "<scope>": {
  755. * "<object>": [
  756. * "<function>",
  757. * "<function>",
  758. * ...
  759. * ]
  760. * }
  761. */
  762. if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_TABLE) {
  763. blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
  764. if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_ARRAY)
  765. continue;
  766. blobmsg_for_each_attr(acl_func, acl_obj, rem2) {
  767. if (blobmsg_type(acl_func) != BLOBMSG_TYPE_STRING)
  768. continue;
  769. rpc_session_grant(ses, blobmsg_name(acl_scope),
  770. blobmsg_name(acl_obj),
  771. blobmsg_data(acl_func));
  772. }
  773. }
  774. }
  775. /*
  776. * Parse ACL scopes in array notation. The permission ("read" or "write")
  777. * will be used as function name for each object.
  778. *
  779. * "<scope>": [
  780. * "<object>",
  781. * "<object>",
  782. * ...
  783. * ]
  784. */
  785. else if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_ARRAY) {
  786. blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
  787. if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_STRING)
  788. continue;
  789. rpc_session_grant(ses, blobmsg_name(acl_scope),
  790. blobmsg_data(acl_obj),
  791. blobmsg_name(acl_perm));
  792. }
  793. }
  794. }
  795. static void
  796. rpc_login_setup_acl_file(struct rpc_session *ses, struct uci_section *login,
  797. const char *path)
  798. {
  799. struct blob_buf acl = { 0 };
  800. struct blob_attr *acl_group, *acl_perm, *acl_scope;
  801. int rem, rem2, rem3;
  802. blob_buf_init(&acl, 0);
  803. if (!blobmsg_add_json_from_file(&acl, path)) {
  804. fprintf(stderr, "Failed to parse %s\n", path);
  805. goto out;
  806. }
  807. /* Iterate access groups in toplevel object */
  808. blob_for_each_attr(acl_group, acl.head, rem) {
  809. /* Iterate permission objects in each access group object */
  810. blobmsg_for_each_attr(acl_perm, acl_group, rem2) {
  811. if (blobmsg_type(acl_perm) != BLOBMSG_TYPE_TABLE)
  812. continue;
  813. /* Only "read" and "write" permissions are defined */
  814. if (strcmp(blobmsg_name(acl_perm), "read") &&
  815. strcmp(blobmsg_name(acl_perm), "write"))
  816. continue;
  817. /*
  818. * Check if the current user context specifies the current
  819. * "read" or "write" permission in the given access group.
  820. */
  821. if (!rpc_login_test_permission(login, blobmsg_name(acl_perm),
  822. blobmsg_name(acl_group)))
  823. continue;
  824. /* Iterate scope objects within the permission object */
  825. blobmsg_for_each_attr(acl_scope, acl_perm, rem3) {
  826. /* Setup the scopes of the access group */
  827. rpc_login_setup_acl_scope(ses, acl_perm, acl_scope);
  828. /*
  829. * Add the access group itself as object to the "access-group"
  830. * meta scope and the the permission level ("read" or "write")
  831. * as function, so
  832. * "<group>": {
  833. * "<permission>": {
  834. * "<scope>": ...
  835. * }
  836. * }
  837. * becomes
  838. * "access-group": {
  839. * "<group>": [
  840. * "<permission>"
  841. * ]
  842. * }
  843. *
  844. * This allows session clients to easily query the allowed
  845. * access groups without having to test access of each single
  846. * <scope>/<object>/<function> tuple defined in a group.
  847. */
  848. rpc_session_grant(ses, "access-group",
  849. blobmsg_name(acl_group),
  850. blobmsg_name(acl_perm));
  851. }
  852. }
  853. }
  854. out:
  855. blob_buf_free(&acl);
  856. }
  857. static void
  858. rpc_login_setup_acls(struct rpc_session *ses, struct uci_section *login)
  859. {
  860. int i;
  861. glob_t gl;
  862. if (glob(RPC_SESSION_ACL_DIR "/*.json", 0, NULL, &gl))
  863. return;
  864. for (i = 0; i < gl.gl_pathc; i++)
  865. rpc_login_setup_acl_file(ses, login, gl.gl_pathv[i]);
  866. globfree(&gl);
  867. }
  868. static struct rpc_session *
  869. rpc_reclaim_apply_session(const char *expected_username)
  870. {
  871. struct rpc_session_data *username;
  872. struct rpc_session *ses;
  873. if (!apply_sid[0])
  874. return NULL;
  875. ses = rpc_session_get(apply_sid);
  876. if (!ses)
  877. return NULL;
  878. username = avl_find_element(&ses->data, "username", username, avl);
  879. if (!username || blobmsg_type(username->attr) != BLOBMSG_TYPE_STRING)
  880. return NULL;
  881. if (strcmp(blobmsg_get_string(username->attr), expected_username))
  882. return NULL;
  883. return ses;
  884. }
  885. static int
  886. rpc_handle_login(struct ubus_context *ctx, struct ubus_object *obj,
  887. struct ubus_request_data *req, const char *method,
  888. struct blob_attr *msg)
  889. {
  890. struct uci_context *uci = NULL;
  891. struct uci_section *login;
  892. struct rpc_session *ses;
  893. struct blob_attr *tb[__RPC_L_MAX];
  894. int timeout = RPC_DEFAULT_SESSION_TIMEOUT;
  895. int rv = 0;
  896. blobmsg_parse(login_policy, __RPC_L_MAX, tb, blob_data(msg), blob_len(msg));
  897. if (!tb[RPC_L_USERNAME] || !tb[RPC_L_PASSWORD]) {
  898. rv = UBUS_STATUS_INVALID_ARGUMENT;
  899. goto out;
  900. }
  901. uci = uci_alloc_context();
  902. if (!uci) {
  903. rv = UBUS_STATUS_UNKNOWN_ERROR;
  904. goto out;
  905. }
  906. login = rpc_login_test_login(uci, blobmsg_get_string(tb[RPC_L_USERNAME]),
  907. blobmsg_get_string(tb[RPC_L_PASSWORD]));
  908. if (!login) {
  909. rv = UBUS_STATUS_PERMISSION_DENIED;
  910. goto out;
  911. }
  912. if (tb[RPC_L_TIMEOUT])
  913. timeout = blobmsg_get_u32(tb[RPC_L_TIMEOUT]);
  914. /*
  915. * attempt to reclaim a pending apply session, but only accept it
  916. * if the username matches, otherwise perform a new login
  917. */
  918. ses = rpc_reclaim_apply_session(blobmsg_get_string(tb[RPC_L_USERNAME]));
  919. if (!ses)
  920. ses = rpc_session_create(timeout);
  921. if (!ses) {
  922. rv = UBUS_STATUS_UNKNOWN_ERROR;
  923. goto out;
  924. }
  925. rpc_login_setup_acls(ses, login);
  926. rpc_session_set(ses, tb[RPC_L_USERNAME]);
  927. rpc_session_dump(ses, ctx, req);
  928. out:
  929. if (uci)
  930. uci_free_context(uci);
  931. return rv;
  932. }
  933. static bool
  934. rpc_validate_sid(const char *id)
  935. {
  936. if (!id)
  937. return false;
  938. if (strlen(id) != RPC_SID_LEN)
  939. return false;
  940. while (*id)
  941. if (!isxdigit(*id++))
  942. return false;
  943. return true;
  944. }
  945. static int
  946. rpc_blob_to_file(const char *path, struct blob_attr *attr)
  947. {
  948. int fd, len;
  949. fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
  950. if (fd < 0)
  951. return fd;
  952. len = write(fd, attr, blob_pad_len(attr));
  953. close(fd);
  954. if (len != blob_pad_len(attr))
  955. {
  956. unlink(path);
  957. return -1;
  958. }
  959. return len;
  960. }
  961. static struct blob_attr *
  962. rpc_blob_from_file(const char *path)
  963. {
  964. int fd = -1, len;
  965. struct stat s;
  966. struct blob_attr head, *attr = NULL;
  967. if (stat(path, &s) || !S_ISREG(s.st_mode))
  968. return NULL;
  969. fd = open(path, O_RDONLY);
  970. if (fd < 0)
  971. goto fail;
  972. len = read(fd, &head, sizeof(head));
  973. if (len != sizeof(head) || blob_pad_len(&head) != s.st_size)
  974. goto fail;
  975. attr = calloc(1, s.st_size);
  976. if (!attr)
  977. goto fail;
  978. memcpy(attr, &head, sizeof(head));
  979. len += read(fd, (char *)attr + sizeof(head), s.st_size - sizeof(head));
  980. if (len != blob_pad_len(&head))
  981. goto fail;
  982. close(fd);
  983. return attr;
  984. fail:
  985. if (fd >= 0)
  986. close(fd);
  987. if (attr)
  988. free(attr);
  989. return NULL;
  990. }
  991. static bool
  992. rpc_session_from_blob(struct uci_context *uci, struct blob_attr *attr)
  993. {
  994. int i, rem;
  995. const char *user = NULL;
  996. struct rpc_session *ses;
  997. struct uci_section *login;
  998. struct blob_attr *tb[__RPC_DUMP_MAX], *data;
  999. blobmsg_parse(dump_policy, __RPC_DUMP_MAX, tb,
  1000. blob_data(attr), blob_len(attr));
  1001. for (i = 0; i < __RPC_DUMP_MAX; i++)
  1002. if (!tb[i])
  1003. return false;
  1004. ses = rpc_session_new();
  1005. if (!ses)
  1006. return false;
  1007. memcpy(ses->id, blobmsg_data(tb[RPC_DUMP_SID]), RPC_SID_LEN);
  1008. ses->timeout = blobmsg_get_u32(tb[RPC_DUMP_TIMEOUT]);
  1009. blobmsg_for_each_attr(data, tb[RPC_DUMP_DATA], rem) {
  1010. rpc_session_set(ses, data);
  1011. if (blobmsg_type(data) != BLOBMSG_TYPE_STRING)
  1012. continue;
  1013. if (!strcmp(blobmsg_name(data), "username"))
  1014. user = blobmsg_get_string(data);
  1015. }
  1016. if (uci && user) {
  1017. login = rpc_login_test_login(uci, user, NULL);
  1018. if (login)
  1019. rpc_login_setup_acls(ses, login);
  1020. }
  1021. avl_insert(&sessions, &ses->avl);
  1022. uloop_timeout_set(&ses->t, blobmsg_get_u64(tb[RPC_DUMP_EXPIRES]) * 1000);
  1023. return true;
  1024. }
  1025. int rpc_session_api_init(struct ubus_context *ctx)
  1026. {
  1027. struct rpc_session *ses;
  1028. static const struct ubus_method session_methods[] = {
  1029. UBUS_METHOD("create", rpc_handle_create, new_policy),
  1030. UBUS_METHOD("list", rpc_handle_list, sid_policy),
  1031. UBUS_METHOD("grant", rpc_handle_acl, acl_policy),
  1032. UBUS_METHOD("revoke", rpc_handle_acl, acl_policy),
  1033. UBUS_METHOD("access", rpc_handle_access, perm_policy),
  1034. UBUS_METHOD("set", rpc_handle_set, set_policy),
  1035. UBUS_METHOD("get", rpc_handle_get, get_policy),
  1036. UBUS_METHOD("unset", rpc_handle_unset, get_policy),
  1037. UBUS_METHOD("destroy", rpc_handle_destroy, sid_policy),
  1038. UBUS_METHOD("login", rpc_handle_login, login_policy),
  1039. };
  1040. static struct ubus_object_type session_type =
  1041. UBUS_OBJECT_TYPE("rpcd-plugin-session", session_methods);
  1042. static struct ubus_object obj = {
  1043. .name = "session",
  1044. .type = &session_type,
  1045. .methods = session_methods,
  1046. .n_methods = ARRAY_SIZE(session_methods),
  1047. };
  1048. avl_init(&sessions, avl_strcmp, false, NULL);
  1049. /* setup the default session */
  1050. ses = rpc_session_new();
  1051. if (ses) {
  1052. strcpy(ses->id, RPC_DEFAULT_SESSION_ID);
  1053. rpc_login_setup_acls(ses, NULL);
  1054. avl_insert(&sessions, &ses->avl);
  1055. }
  1056. return ubus_add_object(ctx, &obj);
  1057. }
  1058. bool rpc_session_access(const char *sid, const char *scope,
  1059. const char *object, const char *function)
  1060. {
  1061. struct rpc_session *ses = rpc_session_get(sid);
  1062. if (!ses)
  1063. return false;
  1064. return rpc_session_acl_allowed(ses, scope, object, function);
  1065. }
  1066. void rpc_session_create_cb(struct rpc_session_cb *cb)
  1067. {
  1068. if (cb && cb->cb)
  1069. list_add(&cb->list, &create_callbacks);
  1070. }
  1071. void rpc_session_destroy_cb(struct rpc_session_cb *cb)
  1072. {
  1073. if (cb && cb->cb)
  1074. list_add(&cb->list, &destroy_callbacks);
  1075. }
  1076. void rpc_session_freeze(void)
  1077. {
  1078. struct stat s;
  1079. struct rpc_session *ses;
  1080. char path[PATH_MAX];
  1081. if (stat(RPC_SESSION_DIRECTORY, &s))
  1082. mkdir(RPC_SESSION_DIRECTORY, 0700);
  1083. avl_for_each_element(&sessions, ses, avl) {
  1084. /* skip default session */
  1085. if (!strcmp(ses->id, RPC_DEFAULT_SESSION_ID))
  1086. continue;
  1087. snprintf(path, sizeof(path) - 1, RPC_SESSION_DIRECTORY "/%s", ses->id);
  1088. rpc_session_to_blob(ses, false);
  1089. rpc_blob_to_file(path, buf.head);
  1090. }
  1091. }
  1092. void rpc_session_thaw(void)
  1093. {
  1094. DIR *d;
  1095. char path[PATH_MAX];
  1096. struct dirent *e;
  1097. struct blob_attr *attr;
  1098. struct uci_context *uci;
  1099. d = opendir(RPC_SESSION_DIRECTORY);
  1100. if (!d)
  1101. return;
  1102. uci = uci_alloc_context();
  1103. if (!uci)
  1104. return;
  1105. while ((e = readdir(d)) != NULL) {
  1106. if (!rpc_validate_sid(e->d_name))
  1107. continue;
  1108. snprintf(path, sizeof(path) - 1,
  1109. RPC_SESSION_DIRECTORY "/%s", e->d_name);
  1110. attr = rpc_blob_from_file(path);
  1111. if (attr) {
  1112. rpc_session_from_blob(uci, attr);
  1113. free(attr);
  1114. }
  1115. unlink(path);
  1116. }
  1117. closedir(d);
  1118. uci_free_context(uci);
  1119. }