session.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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)
  681. continue;
  682. if (ptr.o->type != UCI_TYPE_STRING)
  683. continue;
  684. if (strcmp(ptr.o->v.string, username))
  685. continue;
  686. /* If password is NULL, we're restoring ACLs for an existing session,
  687. * in this case do not check the password again. */
  688. if (!password)
  689. return ptr.s;
  690. /* test for matching password */
  691. ptr.option = "password";
  692. ptr.o = NULL;
  693. if (uci_lookup_ptr(uci, &ptr, NULL, true))
  694. continue;
  695. if (!ptr.o)
  696. continue;
  697. if (ptr.o->type != UCI_TYPE_STRING)
  698. continue;
  699. if (rpc_login_test_password(ptr.o->v.string, password))
  700. return ptr.s;
  701. }
  702. return NULL;
  703. }
  704. static bool
  705. rpc_login_test_permission(struct uci_section *s,
  706. const char *perm, const char *group)
  707. {
  708. const char *p;
  709. struct uci_option *o;
  710. struct uci_element *e, *l;
  711. /* If the login section is not provided, we're setting up acls for the
  712. * default session, in this case uncondionally allow access to the
  713. * "unauthenticated" access group */
  714. if (!s) {
  715. return !strcmp(group, "unauthenticated");
  716. }
  717. uci_foreach_element(&s->options, e)
  718. {
  719. o = uci_to_option(e);
  720. if (o->type != UCI_TYPE_LIST)
  721. continue;
  722. if (strcmp(o->e.name, perm))
  723. continue;
  724. /* Match negative expressions first. If a negative expression matches
  725. * the current group name then deny access. */
  726. uci_foreach_element(&o->v.list, l) {
  727. p = l->name;
  728. if (!p || *p != '!')
  729. continue;
  730. while (isspace(*++p));
  731. if (!*p)
  732. continue;
  733. if (!fnmatch(p, group, 0))
  734. return false;
  735. }
  736. uci_foreach_element(&o->v.list, l) {
  737. if (!l->name || !*l->name || *l->name == '!')
  738. continue;
  739. if (!fnmatch(l->name, group, 0))
  740. return true;
  741. }
  742. }
  743. /* make sure that write permission implies read permission */
  744. if (!strcmp(perm, "read"))
  745. return rpc_login_test_permission(s, "write", group);
  746. return false;
  747. }
  748. static void
  749. rpc_login_setup_acl_scope(struct rpc_session *ses,
  750. struct blob_attr *acl_perm,
  751. struct blob_attr *acl_scope)
  752. {
  753. struct blob_attr *acl_obj, *acl_func;
  754. int rem, rem2;
  755. /*
  756. * Parse ACL scopes in table notation.
  757. *
  758. * "<scope>": {
  759. * "<object>": [
  760. * "<function>",
  761. * "<function>",
  762. * ...
  763. * ]
  764. * }
  765. */
  766. if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_TABLE) {
  767. blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
  768. if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_ARRAY)
  769. continue;
  770. blobmsg_for_each_attr(acl_func, acl_obj, rem2) {
  771. if (blobmsg_type(acl_func) != BLOBMSG_TYPE_STRING)
  772. continue;
  773. rpc_session_grant(ses, blobmsg_name(acl_scope),
  774. blobmsg_name(acl_obj),
  775. blobmsg_data(acl_func));
  776. }
  777. }
  778. }
  779. /*
  780. * Parse ACL scopes in array notation. The permission ("read" or "write")
  781. * will be used as function name for each object.
  782. *
  783. * "<scope>": [
  784. * "<object>",
  785. * "<object>",
  786. * ...
  787. * ]
  788. */
  789. else if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_ARRAY) {
  790. blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
  791. if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_STRING)
  792. continue;
  793. rpc_session_grant(ses, blobmsg_name(acl_scope),
  794. blobmsg_data(acl_obj),
  795. blobmsg_name(acl_perm));
  796. }
  797. }
  798. }
  799. static void
  800. rpc_login_setup_acl_file(struct rpc_session *ses, struct uci_section *login,
  801. const char *path)
  802. {
  803. struct blob_buf acl = { 0 };
  804. struct blob_attr *acl_group, *acl_perm, *acl_scope;
  805. int rem, rem2, rem3;
  806. blob_buf_init(&acl, 0);
  807. if (!blobmsg_add_json_from_file(&acl, path)) {
  808. fprintf(stderr, "Failed to parse %s\n", path);
  809. goto out;
  810. }
  811. /* Iterate access groups in toplevel object */
  812. blob_for_each_attr(acl_group, acl.head, rem) {
  813. /* Iterate permission objects in each access group object */
  814. blobmsg_for_each_attr(acl_perm, acl_group, rem2) {
  815. if (blobmsg_type(acl_perm) != BLOBMSG_TYPE_TABLE)
  816. continue;
  817. /* Only "read" and "write" permissions are defined */
  818. if (strcmp(blobmsg_name(acl_perm), "read") &&
  819. strcmp(blobmsg_name(acl_perm), "write"))
  820. continue;
  821. /*
  822. * Check if the current user context specifies the current
  823. * "read" or "write" permission in the given access group.
  824. */
  825. if (!rpc_login_test_permission(login, blobmsg_name(acl_perm),
  826. blobmsg_name(acl_group)))
  827. continue;
  828. /* Iterate scope objects within the permission object */
  829. blobmsg_for_each_attr(acl_scope, acl_perm, rem3) {
  830. /* Setup the scopes of the access group */
  831. rpc_login_setup_acl_scope(ses, acl_perm, acl_scope);
  832. /*
  833. * Add the access group itself as object to the "access-group"
  834. * meta scope and the the permission level ("read" or "write")
  835. * as function, so
  836. * "<group>": {
  837. * "<permission>": {
  838. * "<scope>": ...
  839. * }
  840. * }
  841. * becomes
  842. * "access-group": {
  843. * "<group>": [
  844. * "<permission>"
  845. * ]
  846. * }
  847. *
  848. * This allows session clients to easily query the allowed
  849. * access groups without having to test access of each single
  850. * <scope>/<object>/<function> tuple defined in a group.
  851. */
  852. rpc_session_grant(ses, "access-group",
  853. blobmsg_name(acl_group),
  854. blobmsg_name(acl_perm));
  855. }
  856. }
  857. }
  858. out:
  859. blob_buf_free(&acl);
  860. }
  861. static void
  862. rpc_login_setup_acls(struct rpc_session *ses, struct uci_section *login)
  863. {
  864. int i;
  865. glob_t gl;
  866. if (glob(RPC_SESSION_ACL_DIR "/*.json", 0, NULL, &gl))
  867. return;
  868. for (i = 0; i < gl.gl_pathc; i++)
  869. rpc_login_setup_acl_file(ses, login, gl.gl_pathv[i]);
  870. globfree(&gl);
  871. }
  872. static struct rpc_session *
  873. rpc_reclaim_apply_session(const char *expected_username)
  874. {
  875. struct rpc_session_data *username;
  876. struct rpc_session *ses;
  877. if (!apply_sid[0])
  878. return NULL;
  879. ses = rpc_session_get(apply_sid);
  880. if (!ses)
  881. return NULL;
  882. username = avl_find_element(&ses->data, "username", username, avl);
  883. if (!username || blobmsg_type(username->attr) != BLOBMSG_TYPE_STRING)
  884. return NULL;
  885. if (strcmp(blobmsg_get_string(username->attr), expected_username))
  886. return NULL;
  887. return ses;
  888. }
  889. static int
  890. rpc_handle_login(struct ubus_context *ctx, struct ubus_object *obj,
  891. struct ubus_request_data *req, const char *method,
  892. struct blob_attr *msg)
  893. {
  894. struct uci_context *uci = NULL;
  895. struct uci_section *login;
  896. struct rpc_session *ses;
  897. struct blob_attr *tb[__RPC_L_MAX];
  898. int timeout = RPC_DEFAULT_SESSION_TIMEOUT;
  899. int rv = 0;
  900. blobmsg_parse(login_policy, __RPC_L_MAX, tb, blob_data(msg), blob_len(msg));
  901. if (!tb[RPC_L_USERNAME] || !tb[RPC_L_PASSWORD]) {
  902. rv = UBUS_STATUS_INVALID_ARGUMENT;
  903. goto out;
  904. }
  905. uci = uci_alloc_context();
  906. if (!uci) {
  907. rv = UBUS_STATUS_UNKNOWN_ERROR;
  908. goto out;
  909. }
  910. login = rpc_login_test_login(uci, blobmsg_get_string(tb[RPC_L_USERNAME]),
  911. blobmsg_get_string(tb[RPC_L_PASSWORD]));
  912. if (!login) {
  913. rv = UBUS_STATUS_PERMISSION_DENIED;
  914. goto out;
  915. }
  916. if (tb[RPC_L_TIMEOUT])
  917. timeout = blobmsg_get_u32(tb[RPC_L_TIMEOUT]);
  918. /*
  919. * attempt to reclaim a pending apply session, but only accept it
  920. * if the username matches, otherwise perform a new login
  921. */
  922. ses = rpc_reclaim_apply_session(blobmsg_get_string(tb[RPC_L_USERNAME]));
  923. if (!ses)
  924. ses = rpc_session_create(timeout);
  925. if (!ses) {
  926. rv = UBUS_STATUS_UNKNOWN_ERROR;
  927. goto out;
  928. }
  929. rpc_login_setup_acls(ses, login);
  930. rpc_session_set(ses, tb[RPC_L_USERNAME]);
  931. rpc_session_dump(ses, ctx, req);
  932. out:
  933. if (uci)
  934. uci_free_context(uci);
  935. return rv;
  936. }
  937. static bool
  938. rpc_validate_sid(const char *id)
  939. {
  940. if (!id)
  941. return false;
  942. if (strlen(id) != RPC_SID_LEN)
  943. return false;
  944. while (*id)
  945. if (!isxdigit(*id++))
  946. return false;
  947. return true;
  948. }
  949. static int
  950. rpc_blob_to_file(const char *path, struct blob_attr *attr)
  951. {
  952. int fd, len;
  953. fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
  954. if (fd < 0)
  955. return fd;
  956. len = write(fd, attr, blob_pad_len(attr));
  957. close(fd);
  958. if (len != blob_pad_len(attr))
  959. {
  960. unlink(path);
  961. return -1;
  962. }
  963. return len;
  964. }
  965. static struct blob_attr *
  966. rpc_blob_from_file(const char *path)
  967. {
  968. int fd = -1, len;
  969. struct stat s;
  970. struct blob_attr head, *attr = NULL;
  971. if (stat(path, &s) || !S_ISREG(s.st_mode))
  972. return NULL;
  973. fd = open(path, O_RDONLY);
  974. if (fd < 0)
  975. goto fail;
  976. len = read(fd, &head, sizeof(head));
  977. if (len != sizeof(head) || blob_pad_len(&head) != s.st_size)
  978. goto fail;
  979. attr = calloc(1, s.st_size);
  980. if (!attr)
  981. goto fail;
  982. memcpy(attr, &head, sizeof(head));
  983. len += read(fd, (char *)attr + sizeof(head), s.st_size - sizeof(head));
  984. if (len != blob_pad_len(&head))
  985. goto fail;
  986. close(fd);
  987. return attr;
  988. fail:
  989. if (fd >= 0)
  990. close(fd);
  991. if (attr)
  992. free(attr);
  993. return NULL;
  994. }
  995. static bool
  996. rpc_session_from_blob(struct uci_context *uci, struct blob_attr *attr)
  997. {
  998. int i, rem;
  999. const char *user = NULL;
  1000. struct rpc_session *ses;
  1001. struct uci_section *login;
  1002. struct blob_attr *tb[__RPC_DUMP_MAX], *data;
  1003. blobmsg_parse(dump_policy, __RPC_DUMP_MAX, tb,
  1004. blob_data(attr), blob_len(attr));
  1005. for (i = 0; i < __RPC_DUMP_MAX; i++)
  1006. if (!tb[i])
  1007. return false;
  1008. ses = rpc_session_new();
  1009. if (!ses)
  1010. return false;
  1011. memcpy(ses->id, blobmsg_data(tb[RPC_DUMP_SID]), RPC_SID_LEN);
  1012. ses->timeout = blobmsg_get_u32(tb[RPC_DUMP_TIMEOUT]);
  1013. blobmsg_for_each_attr(data, tb[RPC_DUMP_DATA], rem) {
  1014. rpc_session_set(ses, data);
  1015. if (blobmsg_type(data) != BLOBMSG_TYPE_STRING)
  1016. continue;
  1017. if (!strcmp(blobmsg_name(data), "username"))
  1018. user = blobmsg_get_string(data);
  1019. }
  1020. if (uci && user) {
  1021. login = rpc_login_test_login(uci, user, NULL);
  1022. if (login)
  1023. rpc_login_setup_acls(ses, login);
  1024. }
  1025. avl_insert(&sessions, &ses->avl);
  1026. uloop_timeout_set(&ses->t, blobmsg_get_u64(tb[RPC_DUMP_EXPIRES]) * 1000);
  1027. return true;
  1028. }
  1029. int rpc_session_api_init(struct ubus_context *ctx)
  1030. {
  1031. struct rpc_session *ses;
  1032. static const struct ubus_method session_methods[] = {
  1033. UBUS_METHOD("create", rpc_handle_create, new_policy),
  1034. UBUS_METHOD("list", rpc_handle_list, sid_policy),
  1035. UBUS_METHOD("grant", rpc_handle_acl, acl_policy),
  1036. UBUS_METHOD("revoke", rpc_handle_acl, acl_policy),
  1037. UBUS_METHOD("access", rpc_handle_access, perm_policy),
  1038. UBUS_METHOD("set", rpc_handle_set, set_policy),
  1039. UBUS_METHOD("get", rpc_handle_get, get_policy),
  1040. UBUS_METHOD("unset", rpc_handle_unset, get_policy),
  1041. UBUS_METHOD("destroy", rpc_handle_destroy, sid_policy),
  1042. UBUS_METHOD("login", rpc_handle_login, login_policy),
  1043. };
  1044. static struct ubus_object_type session_type =
  1045. UBUS_OBJECT_TYPE("rpcd-plugin-session", session_methods);
  1046. static struct ubus_object obj = {
  1047. .name = "session",
  1048. .type = &session_type,
  1049. .methods = session_methods,
  1050. .n_methods = ARRAY_SIZE(session_methods),
  1051. };
  1052. avl_init(&sessions, avl_strcmp, false, NULL);
  1053. /* setup the default session */
  1054. ses = rpc_session_new();
  1055. if (ses) {
  1056. strcpy(ses->id, RPC_DEFAULT_SESSION_ID);
  1057. rpc_login_setup_acls(ses, NULL);
  1058. avl_insert(&sessions, &ses->avl);
  1059. }
  1060. return ubus_add_object(ctx, &obj);
  1061. }
  1062. bool rpc_session_access(const char *sid, const char *scope,
  1063. const char *object, const char *function)
  1064. {
  1065. struct rpc_session *ses = rpc_session_get(sid);
  1066. if (!ses)
  1067. return false;
  1068. return rpc_session_acl_allowed(ses, scope, object, function);
  1069. }
  1070. void rpc_session_create_cb(struct rpc_session_cb *cb)
  1071. {
  1072. if (cb && cb->cb)
  1073. list_add(&cb->list, &create_callbacks);
  1074. }
  1075. void rpc_session_destroy_cb(struct rpc_session_cb *cb)
  1076. {
  1077. if (cb && cb->cb)
  1078. list_add(&cb->list, &destroy_callbacks);
  1079. }
  1080. void rpc_session_freeze(void)
  1081. {
  1082. struct stat s;
  1083. struct rpc_session *ses;
  1084. char path[PATH_MAX];
  1085. if (stat(RPC_SESSION_DIRECTORY, &s))
  1086. mkdir(RPC_SESSION_DIRECTORY, 0700);
  1087. avl_for_each_element(&sessions, ses, avl) {
  1088. /* skip default session */
  1089. if (!strcmp(ses->id, RPC_DEFAULT_SESSION_ID))
  1090. continue;
  1091. snprintf(path, sizeof(path) - 1, RPC_SESSION_DIRECTORY "/%s", ses->id);
  1092. rpc_session_to_blob(ses, false);
  1093. rpc_blob_to_file(path, buf.head);
  1094. }
  1095. }
  1096. void rpc_session_thaw(void)
  1097. {
  1098. DIR *d;
  1099. char path[PATH_MAX];
  1100. struct dirent *e;
  1101. struct blob_attr *attr;
  1102. struct uci_context *uci;
  1103. d = opendir(RPC_SESSION_DIRECTORY);
  1104. if (!d)
  1105. return;
  1106. uci = uci_alloc_context();
  1107. if (!uci)
  1108. return;
  1109. while ((e = readdir(d)) != NULL) {
  1110. if (!rpc_validate_sid(e->d_name))
  1111. continue;
  1112. snprintf(path, sizeof(path) - 1,
  1113. RPC_SESSION_DIRECTORY "/%s", e->d_name);
  1114. attr = rpc_blob_from_file(path);
  1115. if (attr) {
  1116. rpc_session_from_blob(uci, attr);
  1117. free(attr);
  1118. }
  1119. unlink(path);
  1120. }
  1121. closedir(d);
  1122. uci_free_context(uci);
  1123. }