2
0

uci.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  1. /*
  2. * rpcd - UBUS RPC server
  3. *
  4. * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <libgen.h>
  19. #include <glob.h>
  20. #include <libubox/blobmsg.h>
  21. #include <libubox/blobmsg_json.h>
  22. #include <rpcd/uci.h>
  23. #include <rpcd/exec.h>
  24. #include <rpcd/session.h>
  25. static struct blob_buf buf;
  26. static struct uci_context *cursor;
  27. static struct uloop_timeout apply_timer;
  28. static struct ubus_context *apply_ctx;
  29. char apply_sid[RPC_SID_LEN + 1];
  30. enum {
  31. RPC_G_CONFIG,
  32. RPC_G_SECTION,
  33. RPC_G_OPTION,
  34. RPC_G_TYPE,
  35. RPC_G_MATCH,
  36. RPC_G_SESSION,
  37. __RPC_G_MAX,
  38. };
  39. static const struct blobmsg_policy rpc_uci_get_policy[__RPC_G_MAX] = {
  40. [RPC_G_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
  41. [RPC_G_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
  42. [RPC_G_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
  43. [RPC_G_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
  44. [RPC_G_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
  45. [RPC_G_SESSION] = { .name = "ubus_rpc_session",
  46. .type = BLOBMSG_TYPE_STRING },
  47. };
  48. enum {
  49. RPC_A_CONFIG,
  50. RPC_A_TYPE,
  51. RPC_A_NAME,
  52. RPC_A_VALUES,
  53. RPC_A_SESSION,
  54. __RPC_A_MAX,
  55. };
  56. static const struct blobmsg_policy rpc_uci_add_policy[__RPC_A_MAX] = {
  57. [RPC_A_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
  58. [RPC_A_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
  59. [RPC_A_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
  60. [RPC_A_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
  61. [RPC_A_SESSION] = { .name = "ubus_rpc_session",
  62. .type = BLOBMSG_TYPE_STRING },
  63. };
  64. enum {
  65. RPC_S_CONFIG,
  66. RPC_S_SECTION,
  67. RPC_S_TYPE,
  68. RPC_S_MATCH,
  69. RPC_S_VALUES,
  70. RPC_S_SESSION,
  71. __RPC_S_MAX,
  72. };
  73. static const struct blobmsg_policy rpc_uci_set_policy[__RPC_S_MAX] = {
  74. [RPC_S_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
  75. [RPC_S_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
  76. [RPC_S_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
  77. [RPC_S_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
  78. [RPC_S_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
  79. [RPC_S_SESSION] = { .name = "ubus_rpc_session",
  80. .type = BLOBMSG_TYPE_STRING },
  81. };
  82. enum {
  83. RPC_D_CONFIG,
  84. RPC_D_SECTION,
  85. RPC_D_TYPE,
  86. RPC_D_MATCH,
  87. RPC_D_OPTION,
  88. RPC_D_OPTIONS,
  89. RPC_D_SESSION,
  90. __RPC_D_MAX,
  91. };
  92. static const struct blobmsg_policy rpc_uci_delete_policy[__RPC_D_MAX] = {
  93. [RPC_D_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
  94. [RPC_D_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
  95. [RPC_D_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
  96. [RPC_D_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
  97. [RPC_D_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
  98. [RPC_D_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_ARRAY },
  99. [RPC_D_SESSION] = { .name = "ubus_rpc_session",
  100. .type = BLOBMSG_TYPE_STRING },
  101. };
  102. enum {
  103. RPC_R_CONFIG,
  104. RPC_R_SECTION,
  105. RPC_R_OPTION,
  106. RPC_R_NAME,
  107. RPC_R_SESSION,
  108. __RPC_R_MAX,
  109. };
  110. static const struct blobmsg_policy rpc_uci_rename_policy[__RPC_R_MAX] = {
  111. [RPC_R_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
  112. [RPC_R_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
  113. [RPC_R_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
  114. [RPC_R_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
  115. [RPC_R_SESSION] = { .name = "ubus_rpc_session",
  116. .type = BLOBMSG_TYPE_STRING },
  117. };
  118. enum {
  119. RPC_O_CONFIG,
  120. RPC_O_SECTIONS,
  121. RPC_O_SESSION,
  122. __RPC_O_MAX,
  123. };
  124. static const struct blobmsg_policy rpc_uci_order_policy[__RPC_O_MAX] = {
  125. [RPC_O_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
  126. [RPC_O_SECTIONS] = { .name = "sections", .type = BLOBMSG_TYPE_ARRAY },
  127. [RPC_O_SESSION] = { .name = "ubus_rpc_session",
  128. .type = BLOBMSG_TYPE_STRING },
  129. };
  130. enum {
  131. RPC_C_CONFIG,
  132. RPC_C_SESSION,
  133. __RPC_C_MAX,
  134. };
  135. static const struct blobmsg_policy rpc_uci_config_policy[__RPC_C_MAX] = {
  136. [RPC_C_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
  137. [RPC_C_SESSION] = { .name = "ubus_rpc_session",
  138. .type = BLOBMSG_TYPE_STRING },
  139. };
  140. enum {
  141. RPC_T_ROLLBACK,
  142. RPC_T_TIMEOUT,
  143. RPC_T_SESSION,
  144. __RPC_T_MAX,
  145. };
  146. static const struct blobmsg_policy rpc_uci_apply_policy[__RPC_T_MAX] = {
  147. [RPC_T_ROLLBACK] = { .name = "rollback", .type = BLOBMSG_TYPE_BOOL },
  148. [RPC_T_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
  149. [RPC_T_SESSION] = { .name = "ubus_rpc_session",
  150. .type = BLOBMSG_TYPE_STRING },
  151. };
  152. enum {
  153. RPC_B_SESSION,
  154. __RPC_B_MAX,
  155. };
  156. static const struct blobmsg_policy rpc_uci_rollback_policy[__RPC_B_MAX] = {
  157. [RPC_B_SESSION] = { .name = "ubus_rpc_session",
  158. .type = BLOBMSG_TYPE_STRING },
  159. };
  160. /*
  161. * Validate a uci name
  162. */
  163. static bool
  164. rpc_uci_verify_str(const char *name, bool extended, bool type)
  165. {
  166. const char *c;
  167. char *e;
  168. if (!name || !*name)
  169. return false;
  170. if (extended && *name != '@')
  171. extended = false;
  172. for (c = name + extended; *c; c++)
  173. if (!isalnum(*c) && *c != '_' && ((!type && !extended) || *c != '-'))
  174. break;
  175. if (extended) {
  176. if (*c != '[')
  177. return false;
  178. strtol(++c, &e, 10);
  179. return (e > c && *e == ']' && *(e+1) == 0);
  180. }
  181. return (*c == 0);
  182. }
  183. /*
  184. * Check that string is a valid, shell compatible uci name
  185. */
  186. static bool rpc_uci_verify_name(const char *name) {
  187. return rpc_uci_verify_str(name, false, false);
  188. }
  189. /*
  190. * Check that string is a valid section type name
  191. */
  192. static bool rpc_uci_verify_type(const char *type) {
  193. return rpc_uci_verify_str(type, false, true);
  194. }
  195. /*
  196. * Check that the string is a valid section id, optionally in extended
  197. * lookup notation
  198. */
  199. static bool rpc_uci_verify_section(const char *section) {
  200. return rpc_uci_verify_str(section, true, false);
  201. }
  202. /*
  203. * Turn uci error state into ubus return code
  204. */
  205. static int
  206. rpc_uci_status(void)
  207. {
  208. switch (cursor->err)
  209. {
  210. case UCI_OK:
  211. return UBUS_STATUS_OK;
  212. case UCI_ERR_INVAL:
  213. return UBUS_STATUS_INVALID_ARGUMENT;
  214. case UCI_ERR_NOTFOUND:
  215. return UBUS_STATUS_NOT_FOUND;
  216. default:
  217. return UBUS_STATUS_UNKNOWN_ERROR;
  218. }
  219. }
  220. /*
  221. * Clear all save directories from the uci cursor and append the given path
  222. * as new save directory.
  223. */
  224. static void
  225. rpc_uci_replace_savedir(const char *path)
  226. {
  227. struct uci_element *e, *tmp;
  228. uci_foreach_element_safe(&cursor->delta_path, tmp, e) {
  229. if (e->name)
  230. free(e->name);
  231. free(e);
  232. }
  233. cursor->delta_path.prev = &cursor->delta_path;
  234. cursor->delta_path.next = &cursor->delta_path;
  235. if (path)
  236. uci_set_savedir(cursor, path);
  237. }
  238. /*
  239. * Setup per-session delta save directory. If the passed "sid" blob attribute
  240. * pointer is NULL then the precedure was not invoked through the ubus-rpc so
  241. * we do not perform session isolation and use the default save directory.
  242. */
  243. static void
  244. rpc_uci_set_savedir(struct blob_attr *sid)
  245. {
  246. char path[PATH_MAX];
  247. if (!sid)
  248. {
  249. rpc_uci_replace_savedir("/tmp/.uci");
  250. return;
  251. }
  252. snprintf(path, sizeof(path) - 1,
  253. RPC_UCI_SAVEDIR_PREFIX "%s", blobmsg_get_string(sid));
  254. rpc_uci_replace_savedir(path);
  255. }
  256. /*
  257. * Test read access to given config. If the passed "sid" blob attribute pointer
  258. * is NULL then the precedure was not invoked through the ubus-rpc so we do not
  259. * perform access control and always assume true.
  260. */
  261. static bool
  262. rpc_uci_read_access(struct blob_attr *sid, struct blob_attr *config)
  263. {
  264. rpc_uci_set_savedir(sid);
  265. if (!sid)
  266. return true;
  267. return rpc_session_access(blobmsg_data(sid), "uci",
  268. blobmsg_data(config), "read");
  269. }
  270. /*
  271. * Test write access to given config. If the passed "sid" blob attribute pointer
  272. * is NULL then the precedure was not invoked through the ubus-rpc so we do not
  273. * perform access control and always assume true.
  274. */
  275. static bool
  276. rpc_uci_write_access(struct blob_attr *sid, struct blob_attr *config)
  277. {
  278. rpc_uci_set_savedir(sid);
  279. if (!sid)
  280. return true;
  281. return rpc_session_access(blobmsg_data(sid), "uci",
  282. blobmsg_data(config), "write");
  283. }
  284. /*
  285. * Format applicable blob value as string and place a pointer to the string
  286. * buffer in "p". Uses a static string buffer.
  287. */
  288. static bool
  289. rpc_uci_format_blob(struct blob_attr *v, const char **p)
  290. {
  291. static char buf[21];
  292. *p = NULL;
  293. switch (blobmsg_type(v))
  294. {
  295. case BLOBMSG_TYPE_STRING:
  296. *p = blobmsg_data(v);
  297. break;
  298. case BLOBMSG_TYPE_INT64:
  299. snprintf(buf, sizeof(buf), "%"PRIu64, blobmsg_get_u64(v));
  300. *p = buf;
  301. break;
  302. case BLOBMSG_TYPE_INT32:
  303. snprintf(buf, sizeof(buf), "%u", blobmsg_get_u32(v));
  304. *p = buf;
  305. break;
  306. case BLOBMSG_TYPE_INT16:
  307. snprintf(buf, sizeof(buf), "%u", blobmsg_get_u16(v));
  308. *p = buf;
  309. break;
  310. case BLOBMSG_TYPE_INT8:
  311. snprintf(buf, sizeof(buf), "%u", !!blobmsg_get_u8(v));
  312. *p = buf;
  313. break;
  314. default:
  315. break;
  316. }
  317. return !!*p;
  318. }
  319. /*
  320. * Lookup the given uci_ptr and enable extended lookup format if the .section
  321. * value of the uci_ptr looks like extended syntax. Uses an internal copy
  322. * of the given uci_ptr to perform the lookup as failing extended section
  323. * lookup operations in libuci will zero our the uci_ptr struct.
  324. * Copies the internal uci_ptr back to given the uci_ptr on success.
  325. */
  326. static int
  327. rpc_uci_lookup(struct uci_ptr *ptr)
  328. {
  329. int rv;
  330. struct uci_ptr lookup = *ptr;
  331. if (!lookup.s && lookup.section && *lookup.section == '@')
  332. lookup.flags |= UCI_LOOKUP_EXTENDED;
  333. rv = uci_lookup_ptr(cursor, &lookup, NULL, true);
  334. if (!rv)
  335. *ptr = lookup;
  336. return rv;
  337. }
  338. /*
  339. * Checks whether the given uci_option object matches the given string value.
  340. * 1) If the uci_option is of type list, check whether any of the list elements
  341. * equals to the given string
  342. * 2) If the uci_option is of type string, parse it into space separated tokens
  343. * and check if any of the tokens equals to the given string.
  344. * Returns true if a list element or token matched the given string.
  345. */
  346. static bool
  347. rpc_uci_match_option(struct uci_option *o, const char *cmp)
  348. {
  349. struct uci_element *e;
  350. char *s, *p;
  351. if (o->type == UCI_TYPE_LIST)
  352. {
  353. uci_foreach_element(&o->v.list, e)
  354. if (e->name && !strcmp(e->name, cmp))
  355. return true;
  356. return false;
  357. }
  358. if (!o->v.string)
  359. return false;
  360. s = strdup(o->v.string);
  361. if (!s)
  362. return false;
  363. for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
  364. {
  365. if (!strcmp(p, cmp))
  366. {
  367. free(s);
  368. return true;
  369. }
  370. }
  371. free(s);
  372. return false;
  373. }
  374. /*
  375. * Checks whether the given uci_section matches the type and value blob attrs.
  376. * 1) Returns false if "type" is given and the section type does not match
  377. * the value specified in the "type" string blob attribute, else continue.
  378. * 2) Tests whether any key in the "matches" table blob attribute exists in
  379. * the given uci_section and whether each value is contained in the
  380. * corresponding uci option value (see rpc_uci_match_option()).
  381. * 3) A missing or empty "matches" table blob attribute is always considered
  382. * to be a match.
  383. * Returns true if "type" matches or is NULL and "matches" matches or is NULL.
  384. */
  385. static bool
  386. rpc_uci_match_section(struct uci_section *s,
  387. struct blob_attr *type, struct blob_attr *matches)
  388. {
  389. struct uci_element *e;
  390. struct blob_attr *cur;
  391. const char *cmp;
  392. bool match = false;
  393. bool empty = true;
  394. int rem;
  395. if (type && strcmp(s->type, blobmsg_data(type)))
  396. return false;
  397. if (!matches)
  398. return true;
  399. blobmsg_for_each_attr(cur, matches, rem)
  400. {
  401. if (!rpc_uci_format_blob(cur, &cmp))
  402. continue;
  403. uci_foreach_element(&s->options, e)
  404. {
  405. if (strcmp(e->name, blobmsg_name(cur)))
  406. continue;
  407. if (!rpc_uci_match_option(uci_to_option(e), cmp))
  408. return false;
  409. match = true;
  410. }
  411. empty = false;
  412. }
  413. return (empty || match);
  414. }
  415. /*
  416. * Dump the given uci_option value into the global blobmsg buffer and use
  417. * given "name" as key.
  418. * 1) If the uci_option is of type list, put a table into the blob buffer and
  419. * add each list item as string to it.
  420. * 2) If the uci_option is of type string, put its value directly into the blob
  421. * buffer.
  422. */
  423. static void
  424. rpc_uci_dump_option(struct uci_option *o, const char *name)
  425. {
  426. void *c;
  427. struct uci_element *e;
  428. switch (o->type)
  429. {
  430. case UCI_TYPE_STRING:
  431. blobmsg_add_string(&buf, name, o->v.string);
  432. break;
  433. case UCI_TYPE_LIST:
  434. c = blobmsg_open_array(&buf, name);
  435. uci_foreach_element(&o->v.list, e)
  436. blobmsg_add_string(&buf, NULL, e->name);
  437. blobmsg_close_array(&buf, c);
  438. break;
  439. default:
  440. break;
  441. }
  442. }
  443. /*
  444. * Dump the given uci_section object into the global blobmsg buffer and use
  445. * given "name" as key.
  446. * Puts a table into the blob buffer and puts each section option member value
  447. * as value into the table using the option name as key.
  448. * Adds three special keys ".anonymous", ".type" and ".name" which specify the
  449. * corresponding section properties.
  450. */
  451. static void
  452. rpc_uci_dump_section(struct uci_section *s, const char *name, int index)
  453. {
  454. void *c;
  455. struct uci_option *o;
  456. struct uci_element *e;
  457. c = blobmsg_open_table(&buf, name);
  458. blobmsg_add_u8(&buf, ".anonymous", s->anonymous);
  459. blobmsg_add_string(&buf, ".type", s->type);
  460. blobmsg_add_string(&buf, ".name", s->e.name);
  461. if (index >= 0)
  462. blobmsg_add_u32(&buf, ".index", index);
  463. uci_foreach_element(&s->options, e)
  464. {
  465. o = uci_to_option(e);
  466. rpc_uci_dump_option(o, o->e.name);
  467. }
  468. blobmsg_close_table(&buf, c);
  469. }
  470. /*
  471. * Dump the given uci_package object into the global blobmsg buffer and use
  472. * given "name" as key.
  473. * Puts a table into the blob buffer and puts each package section member as
  474. * value into the table using the section name as key.
  475. * Only dumps sections matching the given "type" and "matches", see explaination
  476. * of rpc_uci_match_section() for details.
  477. */
  478. static void
  479. rpc_uci_dump_package(struct uci_package *p, const char *name,
  480. struct blob_attr *type, struct blob_attr *matches)
  481. {
  482. void *c;
  483. struct uci_element *e;
  484. int i = -1;
  485. c = blobmsg_open_table(&buf, name);
  486. uci_foreach_element(&p->sections, e)
  487. {
  488. i++;
  489. if (!rpc_uci_match_section(uci_to_section(e), type, matches))
  490. continue;
  491. rpc_uci_dump_section(uci_to_section(e), e->name, i);
  492. }
  493. blobmsg_close_table(&buf, c);
  494. }
  495. static int
  496. rpc_uci_getcommon(struct ubus_context *ctx, struct ubus_request_data *req,
  497. struct blob_attr *msg, bool use_state)
  498. {
  499. struct blob_attr *tb[__RPC_G_MAX];
  500. struct uci_package *p = NULL;
  501. struct uci_ptr ptr = { 0 };
  502. blobmsg_parse(rpc_uci_get_policy, __RPC_G_MAX, tb,
  503. blob_data(msg), blob_len(msg));
  504. if (!tb[RPC_G_CONFIG])
  505. return UBUS_STATUS_INVALID_ARGUMENT;
  506. if (!rpc_uci_read_access(tb[RPC_G_SESSION], tb[RPC_G_CONFIG]))
  507. return UBUS_STATUS_PERMISSION_DENIED;
  508. ptr.package = blobmsg_data(tb[RPC_G_CONFIG]);
  509. if (use_state)
  510. uci_set_savedir(cursor, "/var/state");
  511. if (uci_load(cursor, ptr.package, &p))
  512. return rpc_uci_status();
  513. if (tb[RPC_G_SECTION])
  514. {
  515. ptr.section = blobmsg_data(tb[RPC_G_SECTION]);
  516. if (tb[RPC_G_OPTION])
  517. ptr.option = blobmsg_data(tb[RPC_G_OPTION]);
  518. }
  519. if (rpc_uci_lookup(&ptr) || !(ptr.flags & UCI_LOOKUP_COMPLETE))
  520. goto out;
  521. blob_buf_init(&buf, 0);
  522. switch (ptr.last->type)
  523. {
  524. case UCI_TYPE_PACKAGE:
  525. rpc_uci_dump_package(ptr.p, "values", tb[RPC_G_TYPE], tb[RPC_G_MATCH]);
  526. break;
  527. case UCI_TYPE_SECTION:
  528. rpc_uci_dump_section(ptr.s, "values", -1);
  529. break;
  530. case UCI_TYPE_OPTION:
  531. rpc_uci_dump_option(ptr.o, "value");
  532. break;
  533. default:
  534. break;
  535. }
  536. ubus_send_reply(ctx, req, buf.head);
  537. out:
  538. uci_unload(cursor, p);
  539. return rpc_uci_status();
  540. }
  541. static int
  542. rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
  543. struct ubus_request_data *req, const char *method,
  544. struct blob_attr *msg)
  545. {
  546. return rpc_uci_getcommon(ctx, req, msg, false);
  547. }
  548. static int
  549. rpc_uci_state(struct ubus_context *ctx, struct ubus_object *obj,
  550. struct ubus_request_data *req, const char *method,
  551. struct blob_attr *msg)
  552. {
  553. return rpc_uci_getcommon(ctx, req, msg, true);
  554. }
  555. static int
  556. rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
  557. struct ubus_request_data *req, const char *method,
  558. struct blob_attr *msg)
  559. {
  560. struct blob_attr *tb[__RPC_A_MAX];
  561. struct blob_attr *cur, *elem;
  562. struct uci_package *p = NULL;
  563. struct uci_section *s;
  564. struct uci_ptr ptr = { 0 };
  565. int rem, rem2, err = 0;
  566. blobmsg_parse(rpc_uci_add_policy, __RPC_A_MAX, tb,
  567. blob_data(msg), blob_len(msg));
  568. if (!tb[RPC_A_CONFIG] || !tb[RPC_A_TYPE])
  569. return UBUS_STATUS_INVALID_ARGUMENT;
  570. if (!rpc_uci_write_access(tb[RPC_A_SESSION], tb[RPC_A_CONFIG]))
  571. return UBUS_STATUS_PERMISSION_DENIED;
  572. if (!rpc_uci_verify_type(blobmsg_data(tb[RPC_A_TYPE])))
  573. return UBUS_STATUS_INVALID_ARGUMENT;
  574. if (tb[RPC_A_NAME] &&
  575. !rpc_uci_verify_name(blobmsg_data(tb[RPC_A_NAME])))
  576. return UBUS_STATUS_INVALID_ARGUMENT;
  577. ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
  578. if (uci_load(cursor, ptr.package, &p))
  579. return rpc_uci_status();
  580. /* add named section */
  581. if (tb[RPC_A_NAME])
  582. {
  583. ptr.section = blobmsg_data(tb[RPC_A_NAME]);
  584. ptr.value = blobmsg_data(tb[RPC_A_TYPE]);
  585. ptr.option = NULL;
  586. if (rpc_uci_lookup(&ptr) || uci_set(cursor, &ptr))
  587. goto out;
  588. }
  589. /* add anon section */
  590. else
  591. {
  592. if (uci_add_section(cursor, p, blobmsg_data(tb[RPC_A_TYPE]), &s) || !s)
  593. goto out;
  594. ptr.section = s->e.name;
  595. }
  596. if (tb[RPC_A_VALUES])
  597. {
  598. blobmsg_for_each_attr(cur, tb[RPC_A_VALUES], rem)
  599. {
  600. ptr.flags = 0;
  601. ptr.o = NULL;
  602. ptr.option = blobmsg_name(cur);
  603. if (!rpc_uci_verify_name(ptr.option))
  604. {
  605. if (!err)
  606. err = UBUS_STATUS_INVALID_ARGUMENT;
  607. continue;
  608. }
  609. if (rpc_uci_lookup(&ptr) || !ptr.s)
  610. {
  611. if (!err)
  612. err = UBUS_STATUS_NOT_FOUND;
  613. continue;
  614. }
  615. switch (blobmsg_type(cur))
  616. {
  617. case BLOBMSG_TYPE_ARRAY:
  618. blobmsg_for_each_attr(elem, cur, rem2)
  619. {
  620. if (!rpc_uci_format_blob(elem, &ptr.value))
  621. {
  622. if (!err)
  623. err = UBUS_STATUS_INVALID_ARGUMENT;
  624. continue;
  625. }
  626. uci_add_list(cursor, &ptr);
  627. }
  628. break;
  629. default:
  630. if (!rpc_uci_format_blob(cur, &ptr.value))
  631. {
  632. if (!err)
  633. err = UBUS_STATUS_INVALID_ARGUMENT;
  634. }
  635. else
  636. {
  637. uci_set(cursor, &ptr);
  638. }
  639. break;
  640. }
  641. }
  642. }
  643. if (!err)
  644. {
  645. uci_save(cursor, p);
  646. blob_buf_init(&buf, 0);
  647. blobmsg_add_string(&buf, "section", ptr.section);
  648. ubus_send_reply(ctx, req, buf.head);
  649. }
  650. out:
  651. uci_unload(cursor, p);
  652. return err ? err : rpc_uci_status();
  653. }
  654. /*
  655. * Turn value from a blob attribute into uci set operation
  656. * 1) if the blob is of type array, delete existing option (if any) and
  657. * emit uci add_list operations for each element
  658. * 2) if the blob is not an array but an option of type list exists,
  659. * delete existing list and emit uci set operation for the blob value
  660. * 3) in all other cases only emit a set operation if there is no existing
  661. * option of if the existing options value differs from the blob value
  662. */
  663. static int
  664. rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr)
  665. {
  666. struct blob_attr *cur;
  667. int rem, rv;
  668. ptr->flags = 0;
  669. ptr->o = NULL;
  670. ptr->option = blobmsg_name(opt);
  671. ptr->value = NULL;
  672. if (!rpc_uci_verify_name(ptr->option))
  673. return UBUS_STATUS_INVALID_ARGUMENT;
  674. if (rpc_uci_lookup(ptr) || !ptr->s)
  675. return UBUS_STATUS_NOT_FOUND;
  676. if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
  677. {
  678. if (ptr->o) {
  679. uci_delete(cursor, ptr);
  680. ptr->flags = 0;
  681. }
  682. rv = UBUS_STATUS_INVALID_ARGUMENT;
  683. blobmsg_for_each_attr(cur, opt, rem)
  684. {
  685. if (!rpc_uci_format_blob(cur, &ptr->value))
  686. continue;
  687. uci_add_list(cursor, ptr);
  688. rv = 0;
  689. }
  690. return rv;
  691. }
  692. else if (ptr->o && ptr->o->type == UCI_TYPE_LIST)
  693. {
  694. uci_delete(cursor, ptr);
  695. ptr->flags = 0;
  696. if (!rpc_uci_format_blob(opt, &ptr->value))
  697. return UBUS_STATUS_INVALID_ARGUMENT;
  698. uci_set(cursor, ptr);
  699. }
  700. else
  701. {
  702. if (!rpc_uci_format_blob(opt, &ptr->value))
  703. return UBUS_STATUS_INVALID_ARGUMENT;
  704. if (!ptr->o || !ptr->o->v.string || strcmp(ptr->o->v.string, ptr->value))
  705. uci_set(cursor, ptr);
  706. }
  707. return 0;
  708. }
  709. static int
  710. rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
  711. struct ubus_request_data *req, const char *method,
  712. struct blob_attr *msg)
  713. {
  714. struct blob_attr *tb[__RPC_S_MAX];
  715. struct blob_attr *cur;
  716. struct uci_package *p = NULL;
  717. struct uci_element *e;
  718. struct uci_ptr ptr = { 0 };
  719. int rem, rv, err = 0;
  720. blobmsg_parse(rpc_uci_set_policy, __RPC_S_MAX, tb,
  721. blob_data(msg), blob_len(msg));
  722. if (!tb[RPC_S_CONFIG] || !tb[RPC_S_VALUES] ||
  723. (!tb[RPC_S_SECTION] && !tb[RPC_S_TYPE] && !tb[RPC_S_MATCH]))
  724. return UBUS_STATUS_INVALID_ARGUMENT;
  725. if (!rpc_uci_write_access(tb[RPC_S_SESSION], tb[RPC_S_CONFIG]))
  726. return UBUS_STATUS_PERMISSION_DENIED;
  727. if (tb[RPC_S_SECTION] &&
  728. !rpc_uci_verify_section(blobmsg_data(tb[RPC_S_SECTION])))
  729. return UBUS_STATUS_INVALID_ARGUMENT;
  730. ptr.package = blobmsg_data(tb[RPC_S_CONFIG]);
  731. if (uci_load(cursor, ptr.package, &p))
  732. return rpc_uci_status();
  733. if (tb[RPC_S_SECTION])
  734. {
  735. ptr.section = blobmsg_data(tb[RPC_S_SECTION]);
  736. blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
  737. {
  738. rv = rpc_uci_merge_set(cur, &ptr);
  739. if (rv)
  740. err = rv;
  741. }
  742. }
  743. else
  744. {
  745. uci_foreach_element(&p->sections, e)
  746. {
  747. if (!rpc_uci_match_section(uci_to_section(e),
  748. tb[RPC_S_TYPE], tb[RPC_S_MATCH]))
  749. continue;
  750. ptr.s = NULL;
  751. ptr.section = e->name;
  752. blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
  753. {
  754. rv = rpc_uci_merge_set(cur, &ptr);
  755. if (rv)
  756. err = rv;
  757. }
  758. }
  759. }
  760. if (!err && !ptr.s)
  761. err = UBUS_STATUS_NOT_FOUND;
  762. if (!err)
  763. uci_save(cursor, p);
  764. uci_unload(cursor, p);
  765. return err ? err : rpc_uci_status();
  766. }
  767. /*
  768. * Delete option or section from uci specified by given blob attribute pointer
  769. * 1) if the blob is of type array, delete any option named after each element
  770. * 2) if the blob is of type string, delete the option named after its value
  771. * 3) if the blob is NULL, delete entire section
  772. */
  773. static int
  774. rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
  775. {
  776. struct blob_attr *cur;
  777. int rem, rv;
  778. if (rpc_uci_lookup(ptr) || !ptr->s)
  779. return UBUS_STATUS_NOT_FOUND;
  780. if (!opt)
  781. {
  782. ptr->o = NULL;
  783. ptr->option = NULL;
  784. uci_delete(cursor, ptr);
  785. return 0;
  786. }
  787. else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
  788. {
  789. rv = UBUS_STATUS_NOT_FOUND;
  790. blobmsg_for_each_attr(cur, opt, rem)
  791. {
  792. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  793. continue;
  794. ptr->o = NULL;
  795. ptr->option = blobmsg_data(cur);
  796. if (rpc_uci_lookup(ptr) || !ptr->o)
  797. continue;
  798. uci_delete(cursor, ptr);
  799. ptr->flags = 0;
  800. rv = 0;
  801. }
  802. return rv;
  803. }
  804. else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING)
  805. {
  806. ptr->o = NULL;
  807. ptr->option = blobmsg_data(opt);
  808. if (rpc_uci_lookup(ptr) || !ptr->o)
  809. return UBUS_STATUS_NOT_FOUND;
  810. uci_delete(cursor, ptr);
  811. return 0;
  812. }
  813. return UBUS_STATUS_INVALID_ARGUMENT;
  814. }
  815. static int
  816. rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
  817. struct ubus_request_data *req, const char *method,
  818. struct blob_attr *msg)
  819. {
  820. struct blob_attr *tb[__RPC_D_MAX];
  821. struct uci_package *p = NULL;
  822. struct uci_element *e, *tmp;
  823. struct uci_ptr ptr = { 0 };
  824. int err = 0;
  825. blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb,
  826. blob_data(msg), blob_len(msg));
  827. if (!tb[RPC_D_CONFIG] ||
  828. (!tb[RPC_D_SECTION] && !tb[RPC_D_TYPE] && !tb[RPC_D_MATCH]))
  829. return UBUS_STATUS_INVALID_ARGUMENT;
  830. if (!rpc_uci_write_access(tb[RPC_D_SESSION], tb[RPC_D_CONFIG]))
  831. return UBUS_STATUS_PERMISSION_DENIED;
  832. if (tb[RPC_D_TYPE] &&
  833. !rpc_uci_verify_type(blobmsg_data(tb[RPC_D_TYPE])))
  834. return UBUS_STATUS_INVALID_ARGUMENT;
  835. if (tb[RPC_D_SECTION] &&
  836. !rpc_uci_verify_section(blobmsg_data(tb[RPC_D_SECTION])))
  837. return UBUS_STATUS_INVALID_ARGUMENT;
  838. ptr.package = blobmsg_data(tb[RPC_D_CONFIG]);
  839. if (uci_load(cursor, ptr.package, &p))
  840. return rpc_uci_status();
  841. if (tb[RPC_D_SECTION])
  842. {
  843. ptr.section = blobmsg_data(tb[RPC_D_SECTION]);
  844. if (tb[RPC_D_OPTIONS])
  845. err = rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
  846. else
  847. err = rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
  848. }
  849. else
  850. {
  851. uci_foreach_element_safe(&p->sections, tmp, e)
  852. {
  853. if (!rpc_uci_match_section(uci_to_section(e),
  854. tb[RPC_D_TYPE], tb[RPC_D_MATCH]))
  855. continue;
  856. ptr.s = NULL;
  857. ptr.section = e->name;
  858. if (tb[RPC_D_OPTIONS])
  859. err = rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
  860. else
  861. err = rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
  862. }
  863. if (!err && !ptr.section)
  864. err = UBUS_STATUS_NOT_FOUND;
  865. }
  866. if (!err)
  867. uci_save(cursor, p);
  868. uci_unload(cursor, p);
  869. return err ? err : rpc_uci_status();
  870. }
  871. static int
  872. rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
  873. struct ubus_request_data *req, const char *method,
  874. struct blob_attr *msg)
  875. {
  876. struct blob_attr *tb[__RPC_R_MAX];
  877. struct uci_package *p = NULL;
  878. struct uci_ptr ptr = { 0 };
  879. blobmsg_parse(rpc_uci_rename_policy, __RPC_R_MAX, tb,
  880. blob_data(msg), blob_len(msg));
  881. if (!tb[RPC_R_CONFIG] || !tb[RPC_R_SECTION] || !tb[RPC_R_NAME])
  882. return UBUS_STATUS_INVALID_ARGUMENT;
  883. if (!rpc_uci_write_access(tb[RPC_R_SESSION], tb[RPC_R_CONFIG]))
  884. return UBUS_STATUS_PERMISSION_DENIED;
  885. ptr.package = blobmsg_data(tb[RPC_R_CONFIG]);
  886. ptr.section = blobmsg_data(tb[RPC_R_SECTION]);
  887. ptr.value = blobmsg_data(tb[RPC_R_NAME]);
  888. if (!rpc_uci_verify_name(ptr.value))
  889. return UBUS_STATUS_INVALID_ARGUMENT;
  890. if (tb[RPC_R_OPTION])
  891. ptr.option = blobmsg_data(tb[RPC_R_OPTION]);
  892. if (uci_load(cursor, ptr.package, &p))
  893. return rpc_uci_status();
  894. if (uci_lookup_ptr(cursor, &ptr, NULL, true))
  895. goto out;
  896. if ((ptr.option && !ptr.o) || !ptr.s)
  897. {
  898. cursor->err = UCI_ERR_NOTFOUND;
  899. goto out;
  900. }
  901. if (uci_rename(cursor, &ptr))
  902. goto out;
  903. uci_save(cursor, p);
  904. out:
  905. uci_unload(cursor, p);
  906. return rpc_uci_status();
  907. }
  908. static int
  909. rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
  910. struct ubus_request_data *req, const char *method,
  911. struct blob_attr *msg)
  912. {
  913. struct blob_attr *tb[__RPC_O_MAX];
  914. struct blob_attr *cur;
  915. struct uci_package *p = NULL;
  916. struct uci_ptr ptr = { 0 };
  917. int rem, i = 0, err = 0;
  918. blobmsg_parse(rpc_uci_order_policy, __RPC_O_MAX, tb,
  919. blob_data(msg), blob_len(msg));
  920. if (!tb[RPC_O_CONFIG] || !tb[RPC_O_SECTIONS])
  921. return UBUS_STATUS_INVALID_ARGUMENT;
  922. if (!rpc_uci_write_access(tb[RPC_O_SESSION], tb[RPC_O_CONFIG]))
  923. return UBUS_STATUS_PERMISSION_DENIED;
  924. ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
  925. if (uci_load(cursor, ptr.package, &p))
  926. return rpc_uci_status();
  927. blobmsg_for_each_attr(cur, tb[RPC_O_SECTIONS], rem)
  928. {
  929. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  930. {
  931. if (!err)
  932. err = UBUS_STATUS_INVALID_ARGUMENT;
  933. continue;
  934. }
  935. ptr.s = NULL;
  936. ptr.section = blobmsg_data(cur);
  937. if (uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.s)
  938. {
  939. if (!err)
  940. err = UBUS_STATUS_NOT_FOUND;
  941. continue;
  942. }
  943. uci_reorder_section(cursor, ptr.s, i++);
  944. }
  945. if (!err)
  946. uci_save(cursor, p);
  947. uci_unload(cursor, p);
  948. return err ? err : rpc_uci_status();
  949. }
  950. static void
  951. rpc_uci_dump_change(struct uci_delta *d)
  952. {
  953. void *c;
  954. const char *types[] = {
  955. [UCI_CMD_REORDER] = "order",
  956. [UCI_CMD_REMOVE] = "remove",
  957. [UCI_CMD_RENAME] = "rename",
  958. [UCI_CMD_ADD] = "add",
  959. [UCI_CMD_LIST_ADD] = "list-add",
  960. [UCI_CMD_LIST_DEL] = "list-del",
  961. [UCI_CMD_CHANGE] = "set",
  962. };
  963. if (!d->section)
  964. return;
  965. c = blobmsg_open_array(&buf, NULL);
  966. blobmsg_add_string(&buf, NULL, types[d->cmd]);
  967. blobmsg_add_string(&buf, NULL, d->section);
  968. if (d->e.name)
  969. blobmsg_add_string(&buf, NULL, d->e.name);
  970. if (d->value)
  971. {
  972. if (d->cmd == UCI_CMD_REORDER)
  973. blobmsg_add_u32(&buf, NULL, strtoul(d->value, NULL, 10));
  974. else
  975. blobmsg_add_string(&buf, NULL, d->value);
  976. }
  977. blobmsg_close_array(&buf, c);
  978. }
  979. static int
  980. rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj,
  981. struct ubus_request_data *req, const char *method,
  982. struct blob_attr *msg)
  983. {
  984. struct blob_attr *tb[__RPC_C_MAX];
  985. struct uci_package *p = NULL;
  986. struct uci_element *e;
  987. char **configs;
  988. void *c, *d;
  989. int i;
  990. blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
  991. blob_data(msg), blob_len(msg));
  992. if (tb[RPC_C_CONFIG])
  993. {
  994. if (!rpc_uci_read_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
  995. return UBUS_STATUS_PERMISSION_DENIED;
  996. if (uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p))
  997. return rpc_uci_status();
  998. blob_buf_init(&buf, 0);
  999. c = blobmsg_open_array(&buf, "changes");
  1000. uci_foreach_element(&p->saved_delta, e)
  1001. rpc_uci_dump_change(uci_to_delta(e));
  1002. blobmsg_close_array(&buf, c);
  1003. uci_unload(cursor, p);
  1004. ubus_send_reply(ctx, req, buf.head);
  1005. return rpc_uci_status();
  1006. }
  1007. rpc_uci_set_savedir(tb[RPC_C_SESSION]);
  1008. if (uci_list_configs(cursor, &configs))
  1009. return rpc_uci_status();
  1010. blob_buf_init(&buf, 0);
  1011. c = blobmsg_open_table(&buf, "changes");
  1012. for (i = 0; configs[i]; i++)
  1013. {
  1014. if (tb[RPC_C_SESSION] &&
  1015. !rpc_session_access(blobmsg_data(tb[RPC_C_SESSION]), "uci",
  1016. configs[i], "read"))
  1017. continue;
  1018. if (uci_load(cursor, configs[i], &p))
  1019. continue;
  1020. if (!uci_list_empty(&p->saved_delta))
  1021. {
  1022. d = blobmsg_open_array(&buf, configs[i]);
  1023. uci_foreach_element(&p->saved_delta, e)
  1024. rpc_uci_dump_change(uci_to_delta(e));
  1025. blobmsg_close_array(&buf, d);
  1026. }
  1027. uci_unload(cursor, p);
  1028. }
  1029. free(configs);
  1030. blobmsg_close_table(&buf, c);
  1031. ubus_send_reply(ctx, req, buf.head);
  1032. return 0;
  1033. }
  1034. static void
  1035. rpc_uci_trigger_event(struct ubus_context *ctx, const char *config)
  1036. {
  1037. char *pkg = strdup(config);
  1038. static struct blob_buf b;
  1039. uint32_t id;
  1040. if (!ubus_lookup_id(ctx, "service", &id)) {
  1041. void *c;
  1042. blob_buf_init(&b, 0);
  1043. blobmsg_add_string(&b, "type", "config.change");
  1044. c = blobmsg_open_table(&b, "data");
  1045. blobmsg_add_string(&b, "package", pkg);
  1046. blobmsg_close_table(&b, c);
  1047. ubus_invoke(ctx, id, "event", b.head, NULL, 0, 1000);
  1048. }
  1049. free(pkg);
  1050. }
  1051. static int
  1052. rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg, bool commit)
  1053. {
  1054. struct blob_attr *tb[__RPC_C_MAX];
  1055. struct uci_package *p = NULL;
  1056. struct uci_ptr ptr = { 0 };
  1057. if (apply_sid[0])
  1058. return UBUS_STATUS_PERMISSION_DENIED;
  1059. blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
  1060. blob_data(msg), blob_len(msg));
  1061. if (!tb[RPC_C_CONFIG])
  1062. return UBUS_STATUS_INVALID_ARGUMENT;
  1063. if (!rpc_uci_write_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
  1064. return UBUS_STATUS_PERMISSION_DENIED;
  1065. ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
  1066. if (commit)
  1067. {
  1068. if (!uci_load(cursor, ptr.package, &p))
  1069. {
  1070. uci_commit(cursor, &p, false);
  1071. uci_unload(cursor, p);
  1072. rpc_uci_trigger_event(ctx, blobmsg_get_string(tb[RPC_C_CONFIG]));
  1073. }
  1074. }
  1075. else
  1076. {
  1077. if (!uci_lookup_ptr(cursor, &ptr, NULL, true) && ptr.p)
  1078. {
  1079. uci_revert(cursor, &ptr);
  1080. uci_unload(cursor, ptr.p);
  1081. }
  1082. }
  1083. return rpc_uci_status();
  1084. }
  1085. static int
  1086. rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
  1087. struct ubus_request_data *req, const char *method,
  1088. struct blob_attr *msg)
  1089. {
  1090. return rpc_uci_revert_commit(ctx, msg, false);
  1091. }
  1092. static int
  1093. rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
  1094. struct ubus_request_data *req, const char *method,
  1095. struct blob_attr *msg)
  1096. {
  1097. return rpc_uci_revert_commit(ctx, msg, true);
  1098. }
  1099. static int
  1100. rpc_uci_configs(struct ubus_context *ctx, struct ubus_object *obj,
  1101. struct ubus_request_data *req, const char *method,
  1102. struct blob_attr *msg)
  1103. {
  1104. char **configs;
  1105. void *c;
  1106. int i;
  1107. if (uci_list_configs(cursor, &configs))
  1108. goto out;
  1109. blob_buf_init(&buf, 0);
  1110. c = blobmsg_open_array(&buf, "configs");
  1111. for (i = 0; configs[i]; i++)
  1112. blobmsg_add_string(&buf, NULL, configs[i]);
  1113. free(configs);
  1114. blobmsg_close_array(&buf, c);
  1115. ubus_send_reply(ctx, req, buf.head);
  1116. out:
  1117. return rpc_uci_status();
  1118. }
  1119. /*
  1120. * Remove given delta save directory (if any).
  1121. */
  1122. static void
  1123. rpc_uci_purge_dir(const char *path)
  1124. {
  1125. DIR *d;
  1126. struct stat s;
  1127. struct dirent *e;
  1128. char file[PATH_MAX];
  1129. if (stat(path, &s) || !S_ISDIR(s.st_mode))
  1130. return;
  1131. if ((d = opendir(path)) != NULL)
  1132. {
  1133. while ((e = readdir(d)) != NULL)
  1134. {
  1135. snprintf(file, sizeof(file) - 1, "%s/%s", path, e->d_name);
  1136. if (stat(file, &s) || !S_ISREG(s.st_mode))
  1137. continue;
  1138. unlink(file);
  1139. }
  1140. closedir(d);
  1141. rmdir(path);
  1142. }
  1143. }
  1144. static int
  1145. rpc_uci_apply_config(struct ubus_context *ctx, char *config)
  1146. {
  1147. struct uci_package *p = NULL;
  1148. if (!uci_load(cursor, config, &p)) {
  1149. uci_commit(cursor, &p, false);
  1150. uci_unload(cursor, p);
  1151. }
  1152. rpc_uci_trigger_event(ctx, config);
  1153. return 0;
  1154. }
  1155. static void
  1156. rpc_uci_copy_file(const char *src, const char *target, const char *file)
  1157. {
  1158. char tmp[256];
  1159. FILE *in, *out;
  1160. snprintf(tmp, sizeof(tmp), "%s%s", src, file);
  1161. in = fopen(tmp, "rb");
  1162. snprintf(tmp, sizeof(tmp), "%s%s", target, file);
  1163. out = fopen(tmp, "wb+");
  1164. if (in && out)
  1165. while (!feof(in)) {
  1166. int len = fread(tmp, 1, sizeof(tmp), in);
  1167. if(len > 0)
  1168. fwrite(tmp, 1, len, out);
  1169. }
  1170. if(in)
  1171. fclose(in);
  1172. if(out)
  1173. fclose(out);
  1174. }
  1175. static int
  1176. rpc_uci_apply_access(const char *sid, glob_t *gl)
  1177. {
  1178. struct stat s;
  1179. int i, c = 0;
  1180. if (gl->gl_pathc < 3)
  1181. return UBUS_STATUS_NO_DATA;
  1182. for (i = 0; i < gl->gl_pathc; i++) {
  1183. char *config = basename(gl->gl_pathv[i]);
  1184. if (*config == '.')
  1185. continue;
  1186. if (stat(gl->gl_pathv[i], &s) || !s.st_size)
  1187. continue;
  1188. if (!rpc_session_access(sid, "uci", config, "write"))
  1189. return UBUS_STATUS_PERMISSION_DENIED;
  1190. c++;
  1191. }
  1192. if (!c)
  1193. return UBUS_STATUS_NO_DATA;
  1194. return 0;
  1195. }
  1196. static void
  1197. rpc_uci_do_rollback(struct ubus_context *ctx, glob_t *gl)
  1198. {
  1199. int i, deny;
  1200. char tmp[PATH_MAX];
  1201. /* Test apply permission to see if the initiator session still exists.
  1202. * If it does, restore the delta files as well, else just restore the
  1203. * main configuration files. */
  1204. deny = apply_sid[0]
  1205. ? rpc_uci_apply_access(apply_sid, gl) : UBUS_STATUS_NOT_FOUND;
  1206. if (!deny) {
  1207. snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", apply_sid);
  1208. mkdir(tmp, 0700);
  1209. }
  1210. /* avoid merging unrelated uci changes when restoring old configs */
  1211. rpc_uci_replace_savedir("/dev/null");
  1212. for (i = 0; i < gl->gl_pathc; i++) {
  1213. char *config = basename(gl->gl_pathv[i]);
  1214. if (*config == '.')
  1215. continue;
  1216. rpc_uci_copy_file(RPC_SNAPSHOT_FILES, RPC_UCI_DIR, config);
  1217. rpc_uci_apply_config(ctx, config);
  1218. if (deny)
  1219. continue;
  1220. rpc_uci_copy_file(RPC_SNAPSHOT_DELTA, tmp, config);
  1221. }
  1222. rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
  1223. rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
  1224. uloop_timeout_cancel(&apply_timer);
  1225. memset(apply_sid, 0, sizeof(apply_sid));
  1226. apply_ctx = NULL;
  1227. }
  1228. static void
  1229. rpc_uci_apply_timeout(struct uloop_timeout *t)
  1230. {
  1231. glob_t gl;
  1232. char tmp[PATH_MAX];
  1233. snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
  1234. if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
  1235. return;
  1236. rpc_uci_do_rollback(apply_ctx, &gl);
  1237. globfree(&gl);
  1238. }
  1239. static int
  1240. rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj,
  1241. struct ubus_request_data *req, const char *method,
  1242. struct blob_attr *msg)
  1243. {
  1244. struct blob_attr *tb[__RPC_T_MAX];
  1245. int timeout = RPC_APPLY_TIMEOUT;
  1246. char tmp[PATH_MAX];
  1247. bool rollback = false;
  1248. int ret, i;
  1249. char *sid;
  1250. glob_t gl;
  1251. blobmsg_parse(rpc_uci_apply_policy, __RPC_T_MAX, tb,
  1252. blob_data(msg), blob_len(msg));
  1253. if (tb[RPC_T_ROLLBACK])
  1254. rollback = blobmsg_get_bool(tb[RPC_T_ROLLBACK]);
  1255. if (apply_sid[0] && rollback)
  1256. return UBUS_STATUS_PERMISSION_DENIED;
  1257. if (!tb[RPC_T_SESSION])
  1258. return UBUS_STATUS_INVALID_ARGUMENT;
  1259. sid = blobmsg_data(tb[RPC_T_SESSION]);
  1260. if (tb[RPC_T_TIMEOUT])
  1261. timeout = blobmsg_get_u32(tb[RPC_T_TIMEOUT]);
  1262. rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
  1263. rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
  1264. if (!apply_sid[0]) {
  1265. rpc_uci_set_savedir(tb[RPC_T_SESSION]);
  1266. mkdir(RPC_SNAPSHOT_FILES, 0700);
  1267. mkdir(RPC_SNAPSHOT_DELTA, 0700);
  1268. snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/*", sid);
  1269. if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
  1270. return UBUS_STATUS_NOT_FOUND;
  1271. snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
  1272. ret = rpc_uci_apply_access(sid, &gl);
  1273. if (ret) {
  1274. globfree(&gl);
  1275. return ret;
  1276. }
  1277. /* copy SID early because rpc_uci_apply_config() will clobber buf */
  1278. if (rollback)
  1279. strncpy(apply_sid, sid, RPC_SID_LEN);
  1280. for (i = 0; i < gl.gl_pathc; i++) {
  1281. char *config = basename(gl.gl_pathv[i]);
  1282. struct stat s;
  1283. if (*config == '.')
  1284. continue;
  1285. if (stat(gl.gl_pathv[i], &s) || !s.st_size)
  1286. continue;
  1287. rpc_uci_copy_file(RPC_UCI_DIR, RPC_SNAPSHOT_FILES, config);
  1288. rpc_uci_copy_file(tmp, RPC_SNAPSHOT_DELTA, config);
  1289. rpc_uci_apply_config(ctx, config);
  1290. }
  1291. globfree(&gl);
  1292. if (rollback) {
  1293. apply_timer.cb = rpc_uci_apply_timeout;
  1294. uloop_timeout_set(&apply_timer, timeout * 1000);
  1295. apply_ctx = ctx;
  1296. }
  1297. }
  1298. return 0;
  1299. }
  1300. static int
  1301. rpc_uci_confirm(struct ubus_context *ctx, struct ubus_object *obj,
  1302. struct ubus_request_data *req, const char *method,
  1303. struct blob_attr *msg)
  1304. {
  1305. struct blob_attr *tb[__RPC_B_MAX];
  1306. char *sid;
  1307. blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
  1308. blob_data(msg), blob_len(msg));
  1309. if (!tb[RPC_B_SESSION])
  1310. return UBUS_STATUS_INVALID_ARGUMENT;
  1311. sid = blobmsg_data(tb[RPC_B_SESSION]);
  1312. if (!apply_sid[0])
  1313. return UBUS_STATUS_NO_DATA;
  1314. if (strcmp(apply_sid, sid))
  1315. return UBUS_STATUS_PERMISSION_DENIED;
  1316. rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
  1317. rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
  1318. uloop_timeout_cancel(&apply_timer);
  1319. memset(apply_sid, 0, sizeof(apply_sid));
  1320. apply_ctx = NULL;
  1321. return 0;
  1322. }
  1323. static int
  1324. rpc_uci_rollback(struct ubus_context *ctx, struct ubus_object *obj,
  1325. struct ubus_request_data *req, const char *method,
  1326. struct blob_attr *msg)
  1327. {
  1328. struct blob_attr *tb[__RPC_B_MAX];
  1329. char tmp[PATH_MAX];
  1330. glob_t gl;
  1331. char *sid;
  1332. blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
  1333. blob_data(msg), blob_len(msg));
  1334. if (!apply_sid[0])
  1335. return UBUS_STATUS_NO_DATA;
  1336. if (!tb[RPC_B_SESSION])
  1337. return UBUS_STATUS_INVALID_ARGUMENT;
  1338. sid = blobmsg_data(tb[RPC_B_SESSION]);
  1339. if (strcmp(apply_sid, sid))
  1340. return UBUS_STATUS_PERMISSION_DENIED;
  1341. snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
  1342. if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
  1343. return UBUS_STATUS_NOT_FOUND;
  1344. rpc_uci_do_rollback(ctx, &gl);
  1345. globfree(&gl);
  1346. return 0;
  1347. }
  1348. static int
  1349. rpc_uci_reload(struct ubus_context *ctx, struct ubus_object *obj,
  1350. struct ubus_request_data *req, const char *method,
  1351. struct blob_attr *msg)
  1352. {
  1353. char * const cmd[2] = { "/sbin/reload_config", NULL };
  1354. if (!fork()) {
  1355. /* wait for the RPC call to complete */
  1356. sleep(2);
  1357. return execv(cmd[0], cmd);
  1358. }
  1359. return 0;
  1360. }
  1361. /*
  1362. * Session destroy callback to purge associated delta directory.
  1363. */
  1364. static void
  1365. rpc_uci_purge_savedir_cb(struct rpc_session *ses, void *priv)
  1366. {
  1367. char path[PATH_MAX];
  1368. snprintf(path, sizeof(path) - 1, RPC_UCI_SAVEDIR_PREFIX "%s", ses->id);
  1369. rpc_uci_purge_dir(path);
  1370. }
  1371. /*
  1372. * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
  1373. * This is used to clean up garbage when starting rpcd.
  1374. */
  1375. void rpc_uci_purge_savedirs(void)
  1376. {
  1377. int i;
  1378. glob_t gl;
  1379. if (!glob(RPC_UCI_SAVEDIR_PREFIX "*", 0, NULL, &gl))
  1380. {
  1381. for (i = 0; i < gl.gl_pathc; i++)
  1382. rpc_uci_purge_dir(gl.gl_pathv[i]);
  1383. globfree(&gl);
  1384. }
  1385. }
  1386. int rpc_uci_api_init(struct ubus_context *ctx)
  1387. {
  1388. static const struct ubus_method uci_methods[] = {
  1389. { .name = "configs", .handler = rpc_uci_configs },
  1390. UBUS_METHOD("get", rpc_uci_get, rpc_uci_get_policy),
  1391. UBUS_METHOD("state", rpc_uci_state, rpc_uci_get_policy),
  1392. UBUS_METHOD("add", rpc_uci_add, rpc_uci_add_policy),
  1393. UBUS_METHOD("set", rpc_uci_set, rpc_uci_set_policy),
  1394. UBUS_METHOD("delete", rpc_uci_delete, rpc_uci_delete_policy),
  1395. UBUS_METHOD("rename", rpc_uci_rename, rpc_uci_rename_policy),
  1396. UBUS_METHOD("order", rpc_uci_order, rpc_uci_order_policy),
  1397. UBUS_METHOD("changes", rpc_uci_changes, rpc_uci_config_policy),
  1398. UBUS_METHOD("revert", rpc_uci_revert, rpc_uci_config_policy),
  1399. UBUS_METHOD("commit", rpc_uci_commit, rpc_uci_config_policy),
  1400. UBUS_METHOD("apply", rpc_uci_apply, rpc_uci_apply_policy),
  1401. UBUS_METHOD("confirm", rpc_uci_confirm, rpc_uci_rollback_policy),
  1402. UBUS_METHOD("rollback", rpc_uci_rollback, rpc_uci_rollback_policy),
  1403. UBUS_METHOD_NOARG("reload_config", rpc_uci_reload),
  1404. };
  1405. static struct ubus_object_type uci_type =
  1406. UBUS_OBJECT_TYPE("rpcd-plugin-uci", uci_methods);
  1407. static struct ubus_object obj = {
  1408. .name = "uci",
  1409. .type = &uci_type,
  1410. .methods = uci_methods,
  1411. .n_methods = ARRAY_SIZE(uci_methods),
  1412. };
  1413. static struct rpc_session_cb cb = {
  1414. .cb = rpc_uci_purge_savedir_cb
  1415. };
  1416. cursor = uci_alloc_context();
  1417. if (!cursor)
  1418. return UBUS_STATUS_UNKNOWN_ERROR;
  1419. rpc_session_destroy_cb(&cb);
  1420. return ubus_add_object(ctx, &obj);
  1421. }