syms.c 30 KB

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