syms.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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)
  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);
  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);
  103. }
  104. int srp_help(kcontext_t *context)
  105. {
  106. return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_ALL, BOOL_TRUE);
  107. }
  108. int srp_compl_set(kcontext_t *context)
  109. {
  110. return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_SET, BOOL_TRUE);
  111. }
  112. int srp_help_set(kcontext_t *context)
  113. {
  114. return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_SET, BOOL_TRUE);
  115. }
  116. int srp_compl_del(kcontext_t *context)
  117. {
  118. return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_DEL, 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);
  123. }
  124. int srp_compl_edit(kcontext_t *context)
  125. {
  126. return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_EDIT, BOOL_TRUE);
  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);
  131. }
  132. int srp_help_edit(kcontext_t *context)
  133. {
  134. return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_EDIT, BOOL_TRUE);
  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);
  139. }
  140. int srp_compl_insert(kcontext_t *context)
  141. {
  142. return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_INSERT, 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);
  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);
  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. if (cur_path)
  971. args = faux_argv_dup(cur_path);
  972. else
  973. args = faux_argv_new();
  974. faux_argv_parse(args, line);
  975. pline = pline_parse(sess, args, opts);
  976. faux_argv_free(args);
  977. if (!pline || pline->invalid) {
  978. err_num++;
  979. fprintf(stderr, "Error: Illegal: %s\n", line);
  980. } else {
  981. faux_list_node_t *iter = NULL;
  982. pexpr_t *expr = NULL;
  983. iter = faux_list_head(pline->exprs);
  984. while ((expr = (pexpr_t *)faux_list_each(&iter))) {
  985. if (!(expr->pat & PT_SET)) {
  986. err_num++;
  987. fprintf(stderr, "Error: Illegal expression for set operation\n");
  988. break;
  989. }
  990. if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
  991. SR_ERR_OK) {
  992. err_num++;
  993. fprintf(stderr, "Error: Can't set data\n");
  994. break;
  995. }
  996. }
  997. }
  998. if (stop_on_error && (err_num > 0)) {
  999. sr_discard_changes(sess);
  1000. goto out;
  1001. }
  1002. pline_free(pline);
  1003. faux_str_free(line);
  1004. }
  1005. if (sr_has_changes(sess)) {
  1006. if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
  1007. sr_discard_changes(sess);
  1008. fprintf(stderr, "Error: Can't apply changes\n");
  1009. goto out;
  1010. }
  1011. }
  1012. ret = 0;
  1013. out:
  1014. faux_file_close(file);
  1015. if (opts->enable_nacm) {
  1016. sr_unsubscribe(nacm_sub);
  1017. sr_nacm_destroy();
  1018. }
  1019. sr_disconnect(conn);
  1020. return ret;
  1021. }