syms.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <syslog.h>
  6. #include <faux/faux.h>
  7. #include <faux/str.h>
  8. #include <faux/argv.h>
  9. #include <faux/list.h>
  10. #include <faux/error.h>
  11. #include <faux/file.h>
  12. #include <klish/khelper.h>
  13. #include <klish/kplugin.h>
  14. #include <klish/kentry.h>
  15. #include <klish/kscheme.h>
  16. #include <klish/kcontext.h>
  17. #include <klish/kpargv.h>
  18. #include <sysrepo.h>
  19. #include <sysrepo/xpath.h>
  20. #include <sysrepo/values.h>
  21. #include <sysrepo/netconf_acm.h>
  22. #include "klish_plugin_sysrepo.h"
  23. #define ERRORMSG "Error: "
  24. #define ARG_PATH "path"
  25. #define ARG_FROM_PATH "from_path"
  26. #define ARG_TO_PATH "to_path"
  27. // Print sysrepo session errors
  28. static void srp_print_errors(sr_session_ctx_t *session)
  29. {
  30. const sr_error_info_t *err_info = NULL;
  31. int rc = 0;
  32. unsigned int i = 0;
  33. if (!session)
  34. return;
  35. rc = sr_session_get_error(session, &err_info);
  36. if ((rc != SR_ERR_OK) || !err_info)
  37. return;
  38. // Show the first error only. Because probably next errors are
  39. // originated from internal sysrepo code but is not from subscribers.
  40. // for (i = 0; i < err_info->err_count; i++)
  41. for (i = 0; i < (err_info->err_count < 1 ? err_info->err_count : 1); i++)
  42. fprintf(stderr, ERRORMSG "%s\n", err_info->err[i].message);
  43. }
  44. // Print sysrepo session errors and then specified error
  45. static void srp_error(sr_session_ctx_t *session, const char *fmt, ...)
  46. {
  47. srp_print_errors(session);
  48. if (fmt) {
  49. va_list argptr;
  50. va_start(argptr, fmt);
  51. vfprintf(stderr, fmt, argptr);
  52. va_end(argptr);
  53. }
  54. }
  55. static faux_argv_t *param2argv(const faux_argv_t *cur_path,
  56. const kpargv_t *pargv, const char *entry_name)
  57. {
  58. faux_list_node_t *iter = NULL;
  59. faux_list_t *pargs = NULL;
  60. faux_argv_t *args = NULL;
  61. kparg_t *parg = NULL;
  62. assert(pargv);
  63. if (!pargv)
  64. return NULL;
  65. pargs = kpargv_find_multi(pargv, entry_name);
  66. if (cur_path)
  67. args = faux_argv_dup(cur_path);
  68. else
  69. args = faux_argv_new();
  70. iter = faux_list_head(pargs);
  71. while ((parg = (kparg_t *)faux_list_each(&iter))) {
  72. faux_argv_add(args, kparg_value(parg));
  73. }
  74. faux_list_free(pargs);
  75. return args;
  76. }
  77. // Candidate from pargv contains possible begin of current word (that must be
  78. // completed). kpargv's list don't contain candidate but only already parsed
  79. // words.
  80. static int srp_compl_or_help(kcontext_t *context, bool_t help,
  81. pt_e enabled_ptypes, bool_t use_cur_path, bool_t existing_nodes_only)
  82. {
  83. faux_argv_t *args = NULL;
  84. pline_t *pline = NULL;
  85. sr_session_ctx_t *sess = NULL;
  86. const char *entry_name = NULL;
  87. faux_argv_t *cur_path = NULL;
  88. assert(context);
  89. sess = srp_udata_sr_sess(context);
  90. if (use_cur_path)
  91. cur_path = (faux_argv_t *)srp_udata_path(context);
  92. entry_name = kentry_name(kcontext_candidate_entry(context));
  93. args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
  94. pline = pline_parse(sess, args, srp_udata_opts(context));
  95. faux_argv_free(args);
  96. pline_print_completions(pline, help, enabled_ptypes, existing_nodes_only);
  97. pline_free(pline);
  98. return 0;
  99. }
  100. int srp_compl(kcontext_t *context)
  101. {
  102. return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_ALL, BOOL_TRUE, BOOL_FALSE);
  103. }
  104. int srp_help(kcontext_t *context)
  105. {
  106. return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_ALL, BOOL_TRUE, BOOL_FALSE);
  107. }
  108. int srp_compl_set(kcontext_t *context)
  109. {
  110. return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_SET, BOOL_TRUE, BOOL_FALSE);
  111. }
  112. int srp_help_set(kcontext_t *context)
  113. {
  114. return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_SET, BOOL_TRUE, BOOL_FALSE);
  115. }
  116. int srp_compl_del(kcontext_t *context)
  117. {
  118. return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_DEL, BOOL_TRUE, BOOL_TRUE);
  119. }
  120. int srp_help_del(kcontext_t *context)
  121. {
  122. return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_DEL, BOOL_TRUE, BOOL_TRUE);
  123. }
  124. int srp_compl_edit(kcontext_t *context)
  125. {
  126. return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_EDIT, BOOL_TRUE, BOOL_FALSE);
  127. }
  128. int srp_compl_edit_abs(kcontext_t *context)
  129. {
  130. return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_EDIT, BOOL_FALSE, BOOL_FALSE);
  131. }
  132. int srp_help_edit(kcontext_t *context)
  133. {
  134. return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_EDIT, BOOL_TRUE, BOOL_FALSE);
  135. }
  136. int srp_help_edit_abs(kcontext_t *context)
  137. {
  138. return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_EDIT, BOOL_FALSE, BOOL_FALSE);
  139. }
  140. int srp_compl_insert(kcontext_t *context)
  141. {
  142. return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_INSERT, BOOL_TRUE, BOOL_TRUE);
  143. }
  144. int srp_help_insert(kcontext_t *context)
  145. {
  146. return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_INSERT, BOOL_TRUE, BOOL_TRUE);
  147. }
  148. int srp_prompt_edit_path(kcontext_t *context)
  149. {
  150. faux_argv_t *cur_path = NULL;
  151. char *path = NULL;
  152. assert(context);
  153. cur_path = (faux_argv_t *)srp_udata_path(context);
  154. if (cur_path)
  155. path = faux_argv_line(cur_path);
  156. kcontext_printf(context, "[edit%s%s]\n",
  157. path ? " " : "", path ? path : "");
  158. faux_str_free(path);
  159. return 0;
  160. }
  161. static int srp_check_type(kcontext_t *context,
  162. pt_e not_accepted_nodes, size_t max_expr_num, bool_t use_cur_path)
  163. {
  164. int ret = -1;
  165. faux_argv_t *args = NULL;
  166. pline_t *pline = NULL;
  167. sr_session_ctx_t *sess = NULL;
  168. const char *entry_name = NULL;
  169. const char *value = NULL;
  170. pexpr_t *expr = NULL;
  171. size_t expr_num = 0;
  172. faux_argv_t *cur_path = NULL;
  173. assert(context);
  174. sess = srp_udata_sr_sess(context);
  175. if (use_cur_path)
  176. cur_path = (faux_argv_t *)srp_udata_path(context);
  177. entry_name = kentry_name(kcontext_candidate_entry(context));
  178. value = kcontext_candidate_value(context);
  179. args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
  180. if (value)
  181. faux_argv_add(args, value);
  182. pline = pline_parse(sess, args, srp_udata_opts(context));
  183. faux_argv_free(args);
  184. if (pline->invalid)
  185. goto err;
  186. expr_num = faux_list_len(pline->exprs);
  187. if (expr_num < 1)
  188. goto err;
  189. if ((max_expr_num > 0) && // '0' means unlimited
  190. (expr_num > max_expr_num))
  191. goto err;
  192. expr = pline_current_expr(pline);
  193. if (expr->pat & not_accepted_nodes)
  194. goto err;
  195. ret = 0;
  196. err:
  197. pline_free(pline);
  198. return ret;
  199. }
  200. int srp_PLINE_SET(kcontext_t *context)
  201. {
  202. return srp_check_type(context, PT_NOT_SET, 0, BOOL_TRUE);
  203. }
  204. int srp_PLINE_DEL(kcontext_t *context)
  205. {
  206. return srp_check_type(context, PT_NOT_DEL, 1, BOOL_TRUE);
  207. }
  208. int srp_PLINE_EDIT(kcontext_t *context)
  209. {
  210. return srp_check_type(context, PT_NOT_EDIT, 1, BOOL_TRUE);
  211. }
  212. int srp_PLINE_EDIT_ABS(kcontext_t *context)
  213. {
  214. return srp_check_type(context, PT_NOT_EDIT, 1, BOOL_FALSE);
  215. }
  216. int srp_PLINE_INSERT_FROM(kcontext_t *context)
  217. {
  218. return srp_check_type(context, PT_NOT_INSERT, 1, BOOL_TRUE);
  219. }
  220. static faux_argv_t *assemble_insert_to(sr_session_ctx_t *sess, const kpargv_t *pargv,
  221. faux_argv_t *cur_path, const char *candidate_value, pline_opts_t *opts)
  222. {
  223. faux_argv_t *args = NULL;
  224. faux_argv_t *insert_to = NULL;
  225. pline_t *pline = NULL;
  226. pexpr_t *expr = NULL;
  227. size_t i = 0;
  228. assert(sess);
  229. args = param2argv(cur_path, pargv, ARG_FROM_PATH);
  230. pline = pline_parse(sess, args, opts);
  231. expr = pline_current_expr(pline);
  232. for (i = 0; i < (expr->args_num - expr->list_pos); i++) {
  233. faux_argv_node_t *iter = faux_argv_iterr(args);
  234. faux_argv_del(args, iter);
  235. }
  236. insert_to = param2argv(args, pargv, ARG_TO_PATH);
  237. faux_argv_free(args);
  238. if (candidate_value)
  239. faux_argv_add(insert_to, candidate_value);
  240. pline_free(pline);
  241. return insert_to;
  242. }
  243. int srp_PLINE_INSERT_TO(kcontext_t *context)
  244. {
  245. int ret = -1;
  246. faux_argv_t *args = NULL;
  247. pline_t *pline = NULL;
  248. sr_session_ctx_t *sess = NULL;
  249. const char *value = NULL;
  250. pexpr_t *expr = NULL;
  251. size_t expr_num = 0;
  252. faux_argv_t *cur_path = NULL;
  253. assert(context);
  254. sess = srp_udata_sr_sess(context);
  255. cur_path = (faux_argv_t *)srp_udata_path(context);
  256. value = kcontext_candidate_value(context);
  257. args = assemble_insert_to(sess, kcontext_parent_pargv(context),
  258. cur_path, value, srp_udata_opts(context));
  259. pline = pline_parse(sess, args, srp_udata_opts(context));
  260. faux_argv_free(args);
  261. if (pline->invalid)
  262. goto err;
  263. expr_num = faux_list_len(pline->exprs);
  264. if (expr_num != 1)
  265. goto err;
  266. expr = pline_current_expr(pline);
  267. if (expr->pat & PT_NOT_INSERT)
  268. goto err;
  269. ret = 0;
  270. err:
  271. pline_free(pline);
  272. return ret;
  273. }
  274. static int srp_compl_or_help_insert_to(kcontext_t *context, bool_t help)
  275. {
  276. faux_argv_t *args = NULL;
  277. pline_t *pline = NULL;
  278. sr_session_ctx_t *sess = NULL;
  279. faux_argv_t *cur_path = NULL;
  280. assert(context);
  281. sess = srp_udata_sr_sess(context);
  282. cur_path = (faux_argv_t *)srp_udata_path(context);
  283. args = assemble_insert_to(sess, kcontext_parent_pargv(context),
  284. cur_path, NULL, srp_udata_opts(context));
  285. pline = pline_parse(sess, args, srp_udata_opts(context));
  286. faux_argv_free(args);
  287. pline_print_completions(pline, help, PT_COMPL_INSERT, BOOL_TRUE);
  288. pline_free(pline);
  289. return 0;
  290. }
  291. int srp_compl_insert_to(kcontext_t *context)
  292. {
  293. return srp_compl_or_help_insert_to(context, BOOL_FALSE);
  294. }
  295. int srp_help_insert_to(kcontext_t *context)
  296. {
  297. return srp_compl_or_help_insert_to(context, BOOL_TRUE);
  298. }
  299. int srp_set(kcontext_t *context)
  300. {
  301. int ret = 0;
  302. faux_argv_t *args = NULL;
  303. pline_t *pline = NULL;
  304. sr_session_ctx_t *sess = NULL;
  305. faux_list_node_t *iter = NULL;
  306. pexpr_t *expr = NULL;
  307. size_t err_num = 0;
  308. faux_argv_t *cur_path = NULL;
  309. assert(context);
  310. sess = srp_udata_sr_sess(context);
  311. cur_path = (faux_argv_t *)srp_udata_path(context);
  312. args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
  313. pline = pline_parse(sess, args, srp_udata_opts(context));
  314. faux_argv_free(args);
  315. if (pline->invalid) {
  316. fprintf(stderr, ERRORMSG "Invalid set request\n");
  317. ret = -1;
  318. goto cleanup;
  319. }
  320. iter = faux_list_head(pline->exprs);
  321. while ((expr = (pexpr_t *)faux_list_each(&iter))) {
  322. if (!(expr->pat & PT_SET)) {
  323. err_num++;
  324. fprintf(stderr, ERRORMSG "Illegal expression for set operation\n");
  325. break;
  326. }
  327. if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
  328. SR_ERR_OK) {
  329. err_num++;
  330. srp_error(sess, ERRORMSG "Can't set data\n");
  331. break;
  332. }
  333. }
  334. if (err_num > 0)
  335. ret = -1;
  336. if (!sr_has_changes(sess))
  337. goto cleanup;
  338. if (err_num > 0) {
  339. sr_discard_changes(sess);
  340. goto cleanup;
  341. }
  342. if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
  343. sr_discard_changes(sess);
  344. srp_error(sess, ERRORMSG "Can't apply changes\n");
  345. goto cleanup;
  346. }
  347. cleanup:
  348. pline_free(pline);
  349. return ret;
  350. }
  351. int srp_del(kcontext_t *context)
  352. {
  353. int ret = -1;
  354. faux_argv_t *args = NULL;
  355. pline_t *pline = NULL;
  356. sr_session_ctx_t *sess = NULL;
  357. pexpr_t *expr = NULL;
  358. faux_argv_t *cur_path = NULL;
  359. assert(context);
  360. sess = srp_udata_sr_sess(context);
  361. cur_path = (faux_argv_t *)srp_udata_path(context);
  362. args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
  363. pline = pline_parse(sess, args, srp_udata_opts(context));
  364. faux_argv_free(args);
  365. if (pline->invalid) {
  366. fprintf(stderr, ERRORMSG "Invalid 'del' request\n");
  367. goto err;
  368. }
  369. if (faux_list_len(pline->exprs) > 1) {
  370. fprintf(stderr, ERRORMSG "Can't delete more than one object\n");
  371. goto err;
  372. }
  373. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  374. if (!(expr->pat & PT_DEL)) {
  375. fprintf(stderr, ERRORMSG "Illegal expression for 'del' operation\n");
  376. goto err;
  377. }
  378. if (sr_delete_item(sess, expr->xpath, 0) != SR_ERR_OK) {
  379. srp_error(sess, ERRORMSG "Can't delete data\n");
  380. goto err;
  381. }
  382. if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
  383. sr_discard_changes(sess);
  384. srp_error(sess, ERRORMSG "Can't apply changes\n");
  385. goto err;
  386. }
  387. ret = 0;
  388. err:
  389. pline_free(pline);
  390. return ret;
  391. }
  392. int srp_edit(kcontext_t *context)
  393. {
  394. int ret = -1;
  395. faux_argv_t *args = NULL;
  396. pline_t *pline = NULL;
  397. sr_session_ctx_t *sess = NULL;
  398. pexpr_t *expr = NULL;
  399. faux_argv_t *cur_path = NULL;
  400. assert(context);
  401. sess = srp_udata_sr_sess(context);
  402. cur_path = (faux_argv_t *)srp_udata_path(context);
  403. args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
  404. pline = pline_parse(sess, args, srp_udata_opts(context));
  405. if (pline->invalid) {
  406. fprintf(stderr, ERRORMSG "Invalid 'edit' request\n");
  407. goto err;
  408. }
  409. if (faux_list_len(pline->exprs) > 1) {
  410. fprintf(stderr, ERRORMSG "Can't process more than one object\n");
  411. goto err;
  412. }
  413. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  414. if (!(expr->pat & PT_EDIT)) {
  415. fprintf(stderr, ERRORMSG "Illegal expression for 'edit' operation\n");
  416. goto err;
  417. }
  418. if (sr_set_item_str(sess, expr->xpath, NULL, NULL, 0) != SR_ERR_OK) {
  419. srp_error(sess, ERRORMSG "Can't set editing data\n");
  420. goto err;
  421. }
  422. if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
  423. sr_discard_changes(sess);
  424. srp_error(sess, ERRORMSG "Can't apply changes\n");
  425. goto err;
  426. }
  427. // Set new current path
  428. srp_udata_set_path(context, args);
  429. ret = 0;
  430. err:
  431. if (ret < 0)
  432. faux_argv_free(args);
  433. pline_free(pline);
  434. return ret;
  435. }
  436. int srp_top(kcontext_t *context)
  437. {
  438. assert(context);
  439. srp_udata_set_path(context, NULL);
  440. return 0;
  441. }
  442. int srp_up(kcontext_t *context)
  443. {
  444. sr_session_ctx_t *sess = NULL;
  445. faux_argv_t *cur_path = NULL;
  446. faux_argv_node_t *iter = NULL;
  447. assert(context);
  448. sess = srp_udata_sr_sess(context);
  449. cur_path = (faux_argv_t *)srp_udata_path(context);
  450. if (!cur_path)
  451. return -1; // It's top level and can't level up
  452. // Remove last arguments one by one and wait for legal edit-like pline
  453. while (faux_argv_len(cur_path) > 0) {
  454. pline_t *pline = NULL;
  455. pexpr_t *expr = NULL;
  456. size_t len = 0;
  457. iter = faux_argv_iterr(cur_path);
  458. faux_argv_del(cur_path, iter);
  459. pline = pline_parse(sess, cur_path, srp_udata_opts(context));
  460. if (pline->invalid) {
  461. pline_free(pline);
  462. continue;
  463. }
  464. len = faux_list_len(pline->exprs);
  465. if (len != 1) {
  466. pline_free(pline);
  467. continue;
  468. }
  469. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  470. if (!(expr->pat & PT_EDIT)) {
  471. pline_free(pline);
  472. continue;
  473. }
  474. // Here new path is ok
  475. pline_free(pline);
  476. break;
  477. }
  478. // Don't store empty path
  479. if (faux_argv_len(cur_path) == 0)
  480. srp_udata_set_path(context, NULL);
  481. return 0;
  482. }
  483. int srp_insert(kcontext_t *context)
  484. {
  485. int ret = -1;
  486. pline_t *pline = NULL;
  487. pline_t *pline_to = NULL;
  488. sr_session_ctx_t *sess = NULL;
  489. pexpr_t *expr = NULL;
  490. pexpr_t *expr_to = NULL;
  491. faux_argv_t *cur_path = NULL;
  492. faux_argv_t *insert_from = NULL;
  493. faux_argv_t *insert_to = NULL;
  494. sr_move_position_t position = SR_MOVE_LAST;
  495. kpargv_t *pargv = NULL;
  496. const char *list_keys = NULL;
  497. const char *leaflist_value = NULL;
  498. assert(context);
  499. sess = srp_udata_sr_sess(context);
  500. cur_path = (faux_argv_t *)srp_udata_path(context);
  501. pargv = kcontext_pargv(context);
  502. // 'from' argument
  503. insert_from = param2argv(cur_path, pargv, ARG_FROM_PATH);
  504. pline = pline_parse(sess, insert_from, srp_udata_opts(context));
  505. faux_argv_free(insert_from);
  506. if (pline->invalid) {
  507. fprintf(stderr, ERRORMSG "Invalid 'from' expression\n");
  508. goto err;
  509. }
  510. if (faux_list_len(pline->exprs) > 1) {
  511. fprintf(stderr, ERRORMSG "Can't process more than one object\n");
  512. goto err;
  513. }
  514. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  515. if (!(expr->pat & PT_INSERT)) {
  516. fprintf(stderr, ERRORMSG "Illegal 'from' expression for 'insert' operation\n");
  517. goto err;
  518. }
  519. // Position
  520. if (kpargv_find(pargv, "first"))
  521. position = SR_MOVE_FIRST;
  522. else if (kpargv_find(pargv, "last"))
  523. position = SR_MOVE_LAST;
  524. else if (kpargv_find(pargv, "before"))
  525. position = SR_MOVE_BEFORE;
  526. else if (kpargv_find(pargv, "after"))
  527. position = SR_MOVE_AFTER;
  528. else {
  529. fprintf(stderr, ERRORMSG "Illegal 'position' argument\n");
  530. goto err;
  531. }
  532. // 'to' argument
  533. if ((SR_MOVE_BEFORE == position) || (SR_MOVE_AFTER == position)) {
  534. insert_to = assemble_insert_to(sess, pargv, cur_path,
  535. NULL, srp_udata_opts(context));
  536. pline_to = pline_parse(sess, insert_to, srp_udata_opts(context));
  537. faux_argv_free(insert_to);
  538. if (pline_to->invalid) {
  539. fprintf(stderr, ERRORMSG "Invalid 'to' expression\n");
  540. goto err;
  541. }
  542. if (faux_list_len(pline_to->exprs) > 1) {
  543. fprintf(stderr, ERRORMSG "Can't process more than one object\n");
  544. goto err;
  545. }
  546. expr_to = (pexpr_t *)faux_list_data(faux_list_head(pline_to->exprs));
  547. if (!(expr_to->pat & PT_INSERT)) {
  548. fprintf(stderr, ERRORMSG "Illegal 'to' expression for 'insert' operation\n");
  549. goto err;
  550. }
  551. if (PAT_LIST_KEY == expr_to->pat)
  552. list_keys = expr_to->last_keys;
  553. else // PATH_LEAFLIST_VALUE
  554. leaflist_value = expr_to->last_keys;
  555. }
  556. if (sr_move_item(sess, expr->xpath, position,
  557. list_keys, leaflist_value, NULL, 0) != SR_ERR_OK) {
  558. srp_error(sess, ERRORMSG "Can't move element\n");
  559. goto err;
  560. }
  561. if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
  562. sr_discard_changes(sess);
  563. srp_error(sess, ERRORMSG "Can't apply changes\n");
  564. goto err;
  565. }
  566. ret = 0;
  567. err:
  568. pline_free(pline);
  569. pline_free(pline_to);
  570. return ret;
  571. }
  572. int srp_verify(kcontext_t *context)
  573. {
  574. int ret = -1;
  575. sr_session_ctx_t *sess = NULL;
  576. assert(context);
  577. sess = srp_udata_sr_sess(context);
  578. // Validate candidate config
  579. if (sr_validate(sess, NULL, 0) != SR_ERR_OK) {
  580. srp_error(sess, ERRORMSG "Invalid candidate configuration\n");
  581. goto err;
  582. }
  583. ret = 0;
  584. err:
  585. return ret;
  586. }
  587. int srp_commit(kcontext_t *context)
  588. {
  589. int ret = -1;
  590. sr_session_ctx_t *sess = NULL;
  591. assert(context);
  592. sess = srp_udata_sr_sess(context);
  593. // Validate candidate config. The copy operation is not enough to fully
  594. // verify candidate config. It verifies only the part of it. So verify
  595. // before commit
  596. if (sr_validate(sess, NULL, 0) != SR_ERR_OK) {
  597. srp_error(sess, ERRORMSG "Invalid candidate configuration\n");
  598. goto err;
  599. }
  600. // Copy candidate to running-config
  601. if (sr_session_switch_ds(sess, SR_DS_RUNNING)) {
  602. srp_error(sess, ERRORMSG "Can't connect to running-config data store\n");
  603. goto err;
  604. }
  605. if (sr_copy_config(sess, NULL, SRP_REPO_EDIT, 0) != SR_ERR_OK) {
  606. srp_error(sess, ERRORMSG "Can't commit to running-config\n");
  607. goto err;
  608. }
  609. // Copy running-config to startup-config
  610. if (sr_session_switch_ds(sess, SR_DS_STARTUP)) {
  611. srp_error(sess, ERRORMSG "Can't connect to startup-config data store\n");
  612. goto err;
  613. }
  614. if (sr_copy_config(sess, NULL, SR_DS_RUNNING, 0) != SR_ERR_OK) {
  615. srp_error(sess, ERRORMSG "Can't store data to startup-config\n");
  616. goto err;
  617. }
  618. ret = 0;
  619. err:
  620. sr_session_switch_ds(sess, SRP_REPO_EDIT);
  621. return ret;
  622. }
  623. int srp_reset(kcontext_t *context)
  624. {
  625. int ret = -1;
  626. sr_session_ctx_t *sess = NULL;
  627. assert(context);
  628. sess = srp_udata_sr_sess(context);
  629. // Copy running-config to candidate config
  630. if (sr_copy_config(sess, NULL, SR_DS_RUNNING, 0) != SR_ERR_OK) {
  631. srp_error(sess, ERRORMSG "Can't reset to running-config\n");
  632. goto err;
  633. }
  634. ret = 0;
  635. err:
  636. return ret;
  637. }
  638. int srp_show_xml(kcontext_t *context)
  639. {
  640. int ret = -1;
  641. faux_argv_t *args = NULL;
  642. pline_t *pline = NULL;
  643. sr_session_ctx_t *sess = NULL;
  644. pexpr_t *expr = NULL;
  645. faux_argv_t *cur_path = NULL;
  646. sr_data_t *data = NULL;
  647. struct ly_out *out = NULL;
  648. assert(context);
  649. sess = srp_udata_sr_sess(context);
  650. cur_path = (faux_argv_t *)srp_udata_path(context);
  651. args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
  652. pline = pline_parse(sess, args, srp_udata_opts(context));
  653. faux_argv_free(args);
  654. if (pline->invalid) {
  655. fprintf(stderr, ERRORMSG "Invalid 'show' request\n");
  656. goto err;
  657. }
  658. if (faux_list_len(pline->exprs) > 1) {
  659. fprintf(stderr, ERRORMSG "Can't process more than one object\n");
  660. goto err;
  661. }
  662. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  663. if (!(expr->pat & PT_EDIT)) {
  664. fprintf(stderr, ERRORMSG "Illegal expression for 'show' operation\n");
  665. goto err;
  666. }
  667. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  668. srp_error(sess, ERRORMSG "Can't get specified subtree\n");
  669. goto err;
  670. }
  671. if (!data) // Not found
  672. goto err;
  673. ly_out_new_file(stdout, &out);
  674. lyd_print_tree(out, data->tree, LYD_XML, 0);
  675. ly_out_free(out, NULL, 0);
  676. // child = lyd_child(data->tree);
  677. // if (child) {
  678. // ly_out_new_file(stdout, &out);
  679. // lyd_print_all(out, child, LYD_XML, 0);
  680. // }
  681. struct lyd_meta *meta = lyd_find_meta(data->tree->meta, NULL, "junos-configuration-metadata:active");
  682. if (meta)
  683. printf("META\n");
  684. sr_release_data(data);
  685. ret = 0;
  686. err:
  687. pline_free(pline);
  688. return ret;
  689. }
  690. static int show(kcontext_t *context, sr_datastore_t ds,
  691. const char *path_var, bool_t use_cur_path)
  692. {
  693. int ret = -1;
  694. faux_argv_t *args = NULL;
  695. pline_t *pline = NULL;
  696. sr_session_ctx_t *sess = NULL;
  697. pexpr_t *expr = NULL;
  698. faux_argv_t *cur_path = NULL;
  699. char *xpath = NULL;
  700. assert(context);
  701. sess = srp_udata_sr_sess(context);
  702. if (ds != SRP_REPO_EDIT)
  703. sr_session_switch_ds(sess, ds);
  704. if (use_cur_path)
  705. cur_path = (faux_argv_t *)srp_udata_path(context);
  706. if (kpargv_find(kcontext_pargv(context), path_var) || cur_path) {
  707. args = param2argv(cur_path, kcontext_pargv(context), path_var);
  708. pline = pline_parse(sess, args, srp_udata_opts(context));
  709. faux_argv_free(args);
  710. if (pline->invalid) {
  711. fprintf(stderr, ERRORMSG "Invalid 'show' request\n");
  712. goto err;
  713. }
  714. if (faux_list_len(pline->exprs) > 1) {
  715. fprintf(stderr, ERRORMSG "Can't process more than one object\n");
  716. goto err;
  717. }
  718. if (!(expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs)))) {
  719. fprintf(stderr, ERRORMSG "Can't get expression\n");
  720. goto err;
  721. }
  722. if (!(expr->pat & PT_EDIT)) {
  723. fprintf(stderr, ERRORMSG "Illegal expression for 'show' operation\n");
  724. goto err;
  725. }
  726. if (!expr->xpath) {
  727. fprintf(stderr, ERRORMSG "Empty expression for 'show' operation\n");
  728. goto err;
  729. }
  730. xpath = expr->xpath;
  731. }
  732. show_xpath(sess, xpath, srp_udata_opts(context));
  733. ret = 0;
  734. err:
  735. pline_free(pline);
  736. if (ds != SRP_REPO_EDIT)
  737. sr_session_switch_ds(sess, SRP_REPO_EDIT);
  738. return ret;
  739. }
  740. static int show_path(kcontext_t *context, bool_t use_cur_path)
  741. {
  742. sr_datastore_t ds = SRP_REPO_EDIT;
  743. const char *script = NULL;
  744. assert(context);
  745. script = kcontext_script(context);
  746. if (!faux_str_is_empty(script))
  747. if (!kly_str2ds(script, strlen(script), &ds))
  748. ds = SRP_REPO_EDIT;
  749. return show(context, ds, ARG_PATH, use_cur_path);
  750. }
  751. int srp_show_abs(kcontext_t *context)
  752. {
  753. return show_path(context, BOOL_FALSE);
  754. }
  755. int srp_show(kcontext_t *context)
  756. {
  757. return show_path(context, BOOL_TRUE);
  758. }
  759. int srp_deactivate(kcontext_t *context)
  760. {
  761. int ret = -1;
  762. faux_argv_t *args = NULL;
  763. pline_t *pline = NULL;
  764. sr_session_ctx_t *sess = NULL;
  765. pexpr_t *expr = NULL;
  766. faux_argv_t *cur_path = NULL;
  767. sr_data_t *data = NULL;
  768. assert(context);
  769. sess = srp_udata_sr_sess(context);
  770. cur_path = (faux_argv_t *)srp_udata_path(context);
  771. args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
  772. pline = pline_parse(sess, args, srp_udata_opts(context));
  773. faux_argv_free(args);
  774. if (pline->invalid) {
  775. fprintf(stderr, ERRORMSG "Invalid 'show' request\n");
  776. goto err;
  777. }
  778. if (faux_list_len(pline->exprs) > 1) {
  779. fprintf(stderr, ERRORMSG "Can't process more than one object\n");
  780. goto err;
  781. }
  782. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  783. if (!(expr->pat & PT_DEL)) {
  784. fprintf(stderr, ERRORMSG "Illegal expression for 'show' operation\n");
  785. goto err;
  786. }
  787. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  788. srp_error(sess, ERRORMSG "Can't get specified subtree\n");
  789. goto err;
  790. }
  791. if (!data) // Not found
  792. goto err;
  793. if (lyd_new_meta(LYD_CTX(data->tree), data->tree, NULL,
  794. "junos-configuration-metadata:active", "false", 0, NULL)) {
  795. fprintf(stderr, ERRORMSG "Can't deactivate\n");
  796. goto err;
  797. }
  798. struct lyd_meta *meta = lyd_find_meta(data->tree->meta, NULL, "junos-configuration-metadata:active");
  799. if (meta)
  800. printf("META\n");
  801. if (sr_has_changes(sess))
  802. fprintf(stderr, ERRORMSG "Has changes\n");
  803. if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
  804. sr_discard_changes(sess);
  805. srp_error(sess, ERRORMSG "Can't apply changes\n");
  806. }
  807. sr_release_data(data);
  808. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  809. srp_error(sess, ERRORMSG "Can't get specified subtree\n");
  810. goto err;
  811. }
  812. if (!data) // Not found
  813. goto err;
  814. struct ly_out *out = NULL;
  815. ly_out_new_file(stdout, &out);
  816. lyd_print_tree(out, data->tree, LYD_XML, 0);
  817. ly_out_free(out, NULL, 0);
  818. sr_release_data(data);
  819. ret = 0;
  820. err:
  821. pline_free(pline);
  822. return ret;
  823. }
  824. int srp_diff(kcontext_t *context)
  825. {
  826. int ret = -1;
  827. pline_t *pline = NULL;
  828. sr_session_ctx_t *sess = NULL;
  829. sr_data_t *data1 = NULL;
  830. sr_data_t *data2 = NULL;
  831. faux_argv_t *cur_path = NULL;
  832. const char *xpath = NULL;
  833. struct lyd_node *diff = NULL;
  834. pline_opts_t masked_opts = {};
  835. assert(context);
  836. sess = srp_udata_sr_sess(context);
  837. cur_path = (faux_argv_t *)srp_udata_path(context);
  838. if (kpargv_find(kcontext_pargv(context), ARG_PATH) || cur_path) {
  839. faux_argv_t *args = NULL;
  840. pexpr_t *expr = NULL;
  841. args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
  842. pline = pline_parse(sess, args, srp_udata_opts(context));
  843. faux_argv_free(args);
  844. if (pline->invalid) {
  845. fprintf(stderr, ERRORMSG "Invalid 'show' request\n");
  846. goto err;
  847. }
  848. if (faux_list_len(pline->exprs) > 1) {
  849. fprintf(stderr, ERRORMSG "Can't process more than one object\n");
  850. goto err;
  851. }
  852. if (!(expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs)))) {
  853. fprintf(stderr, ERRORMSG "Can't get expression\n");
  854. goto err;
  855. }
  856. if (!(expr->pat & PT_EDIT)) {
  857. fprintf(stderr, ERRORMSG "Illegal expression for 'show' operation\n");
  858. goto err;
  859. }
  860. if (!expr->xpath) {
  861. fprintf(stderr, ERRORMSG "Empty expression for 'show' operation\n");
  862. goto err;
  863. }
  864. xpath = expr->xpath;
  865. }
  866. if (!xpath)
  867. xpath = "/*";
  868. if (sr_get_data(sess, xpath, 0, 0, 0, &data2) != SR_ERR_OK) {
  869. srp_error(sess, ERRORMSG "Can't get specified subtree\n");
  870. goto err;
  871. }
  872. // Running config
  873. sr_session_switch_ds(sess, SR_DS_RUNNING);
  874. if (sr_get_data(sess, xpath, 0, 0, 0, &data1) != SR_ERR_OK) {
  875. srp_error(sess, ERRORMSG "Can't get specified subtree\n");
  876. goto err;
  877. }
  878. if (lyd_diff_siblings(data1 ? data1->tree : NULL, data2 ? data2->tree : NULL,
  879. 0, &diff) != LY_SUCCESS) {
  880. srp_error(sess, ERRORMSG "Can't generate diff\n");
  881. goto err;
  882. }
  883. // Hack to don't show oneliners within diff. Mask oneliners flag
  884. masked_opts = *srp_udata_opts(context);
  885. masked_opts.oneliners = BOOL_FALSE;
  886. show_subtree(diff, 0, DIFF_OP_NONE, &masked_opts, BOOL_FALSE);
  887. lyd_free_siblings(diff);
  888. ret = 0;
  889. err:
  890. if (data1)
  891. sr_release_data(data1);
  892. if (data2)
  893. sr_release_data(data2);
  894. pline_free(pline);
  895. sr_session_switch_ds(sess, SRP_REPO_EDIT);
  896. return ret;
  897. }
  898. int srp_compl_xpath(kcontext_t *context)
  899. {
  900. sr_session_ctx_t *sess = NULL;
  901. sr_val_t *vals = NULL;
  902. size_t val_num = 0;
  903. size_t i = 0;
  904. const char *script = NULL;
  905. const char *raw_xpath = NULL;
  906. sr_datastore_t ds = SRP_REPO_EDIT;
  907. assert(context);
  908. script = kcontext_script(context);
  909. if (faux_str_is_empty(script))
  910. return -1;
  911. if (!kly_parse_ext_xpath(script, &raw_xpath, &ds))
  912. return -1;
  913. sess = srp_udata_sr_sess(context);
  914. if (ds != SRP_REPO_EDIT)
  915. sr_session_switch_ds(sess, ds);
  916. sr_get_items(sess, raw_xpath, 0, 0, &vals, &val_num);
  917. for (i = 0; i < val_num; i++) {
  918. char *tmp = sr_val_to_str(&vals[i]);
  919. if (!tmp)
  920. continue;
  921. printf("%s\n", tmp);
  922. free(tmp);
  923. }
  924. sr_free_values(vals, val_num);
  925. if (ds != SRP_REPO_EDIT)
  926. sr_session_switch_ds(sess, SRP_REPO_EDIT);
  927. return 0;
  928. }
  929. // Function for mass config strings load. It can load stream of KPath strings
  930. // (without "set" command, only path and value). Function doesn't use
  931. // pre-connected session because it can be executed within FILTER or utility.
  932. int srp_mass_set(int fd, sr_datastore_t ds, const faux_argv_t *cur_path,
  933. const pline_opts_t *opts, const char *user, bool_t stop_on_error)
  934. {
  935. int ret = -1;
  936. int err = SR_ERR_OK;
  937. sr_conn_ctx_t *conn = NULL;
  938. sr_session_ctx_t *sess = NULL;
  939. faux_file_t *file = NULL;
  940. char *line = NULL;
  941. size_t err_num = 0;
  942. sr_subscription_ctx_t *nacm_sub = NULL;
  943. err = sr_connect(SR_CONN_DEFAULT, &conn);
  944. if (err) {
  945. fprintf(stderr, "Error: Can't connect to sysrepo\n");
  946. goto out;
  947. }
  948. err = sr_session_start(conn, ds, &sess);
  949. if (err) {
  950. fprintf(stderr, "Error: Can't start session\n");
  951. goto out;
  952. }
  953. sr_session_set_orig_name(sess, user);
  954. // Init NACM session
  955. if (opts->enable_nacm) {
  956. if (sr_nacm_init(sess, 0, &nacm_sub) != SR_ERR_OK) {
  957. fprintf(stderr, "Error: Can't init NACM\n");
  958. goto out;
  959. }
  960. sr_nacm_set_user(sess, user);
  961. }
  962. file = faux_file_fdopen(fd);
  963. if (!file) {
  964. fprintf(stderr, "Error: Can't open input stream\n");
  965. goto out;
  966. }
  967. while ((line = faux_file_getline(file))) {
  968. pline_t *pline = NULL;
  969. faux_argv_t *args = NULL;
  970. // Don't process empty strings and strings with only spaces
  971. if (!faux_str_has_content(line)) {
  972. faux_str_free(line);
  973. continue;
  974. }
  975. // Add current sysrepo path
  976. if (cur_path)
  977. args = faux_argv_dup(cur_path);
  978. else
  979. args = faux_argv_new();
  980. faux_argv_parse(args, line);
  981. pline = pline_parse(sess, args, opts);
  982. faux_argv_free(args);
  983. if (!pline || pline->invalid) {
  984. err_num++;
  985. fprintf(stderr, "Error: Illegal: %s\n", line);
  986. } else {
  987. faux_list_node_t *iter = NULL;
  988. pexpr_t *expr = NULL;
  989. iter = faux_list_head(pline->exprs);
  990. while ((expr = (pexpr_t *)faux_list_each(&iter))) {
  991. if (!(expr->pat & PT_SET)) {
  992. err_num++;
  993. fprintf(stderr, "Error: Illegal expression for set operation\n");
  994. break;
  995. }
  996. if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
  997. SR_ERR_OK) {
  998. err_num++;
  999. fprintf(stderr, "Error: Can't set data\n");
  1000. break;
  1001. }
  1002. }
  1003. }
  1004. if (stop_on_error && (err_num > 0)) {
  1005. sr_discard_changes(sess);
  1006. goto out;
  1007. }
  1008. pline_free(pline);
  1009. faux_str_free(line);
  1010. }
  1011. if (sr_has_changes(sess)) {
  1012. if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
  1013. sr_discard_changes(sess);
  1014. fprintf(stderr, "Error: Can't apply changes\n");
  1015. goto out;
  1016. }
  1017. }
  1018. ret = 0;
  1019. out:
  1020. faux_file_close(file);
  1021. if (opts->enable_nacm) {
  1022. sr_unsubscribe(nacm_sub);
  1023. sr_nacm_destroy();
  1024. }
  1025. sr_disconnect(conn);
  1026. return ret;
  1027. }