plugin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. *
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <assert.h>
  8. #include <syslog.h>
  9. #include <sysrepo.h>
  10. #include <sysrepo/netconf_acm.h>
  11. #include <faux/faux.h>
  12. #include <faux/str.h>
  13. #include <faux/ini.h>
  14. #include <faux/conv.h>
  15. #include <klish/kplugin.h>
  16. #include <klish/kcontext.h>
  17. #include "private.h"
  18. const uint8_t kplugin_sysrepo_major = KPLUGIN_MAJOR;
  19. const uint8_t kplugin_sysrepo_minor = KPLUGIN_MINOR;
  20. static int parse_plugin_conf(const char *conf, pline_opts_t *opts);
  21. static int kplugin_sysrepo_init_session(kcontext_t *context);
  22. static int kplugin_sysrepo_fini_session(kcontext_t *context);
  23. int kplugin_sysrepo_init(kcontext_t *context)
  24. {
  25. kplugin_t *plugin = NULL;
  26. srp_udata_t *udata = NULL;
  27. assert(context);
  28. plugin = kcontext_plugin(context);
  29. assert(plugin);
  30. // Symbols
  31. // Session init/fini
  32. kplugin_set_init_session_fn(plugin, kplugin_sysrepo_init_session);
  33. kplugin_set_fini_session_fn(plugin, kplugin_sysrepo_fini_session);
  34. // Types
  35. kplugin_add_syms(plugin, ksym_new_ext("PLINE_SET", srp_PLINE_SET,
  36. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  37. kplugin_add_syms(plugin, ksym_new_ext("PLINE_DEL", srp_PLINE_DEL,
  38. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  39. kplugin_add_syms(plugin, ksym_new_ext("PLINE_EDIT", srp_PLINE_EDIT,
  40. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  41. kplugin_add_syms(plugin, ksym_new_ext("PLINE_EDIT_ABS", srp_PLINE_EDIT_ABS,
  42. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  43. kplugin_add_syms(plugin, ksym_new_ext("PLINE_INSERT_FROM", srp_PLINE_INSERT_FROM,
  44. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  45. kplugin_add_syms(plugin, ksym_new_ext("PLINE_INSERT_TO", srp_PLINE_INSERT_TO,
  46. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  47. // Completion/Help/Prompt
  48. kplugin_add_syms(plugin, ksym_new_ext("srp_compl", srp_compl,
  49. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  50. kplugin_add_syms(plugin, ksym_new_ext("srp_help", srp_help,
  51. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  52. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_set", srp_compl_set,
  53. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  54. kplugin_add_syms(plugin, ksym_new_ext("srp_help_set", srp_help_set,
  55. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  56. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_del", srp_compl_del,
  57. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  58. kplugin_add_syms(plugin, ksym_new_ext("srp_help_del", srp_help_del,
  59. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  60. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_edit", srp_compl_edit,
  61. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  62. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_edit_abs", srp_compl_edit_abs,
  63. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  64. kplugin_add_syms(plugin, ksym_new_ext("srp_help_edit", srp_help_edit,
  65. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  66. kplugin_add_syms(plugin, ksym_new_ext("srp_help_edit_abs", srp_help_edit_abs,
  67. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  68. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_insert", srp_compl_insert,
  69. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  70. kplugin_add_syms(plugin, ksym_new_ext("srp_help_insert", srp_help_insert,
  71. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  72. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_insert_to", srp_compl_insert_to,
  73. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  74. kplugin_add_syms(plugin, ksym_new_ext("srp_help_insert_to", srp_help_insert_to,
  75. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  76. kplugin_add_syms(plugin, ksym_new_ext("srp_prompt_edit_path", srp_prompt_edit_path,
  77. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  78. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_xpath", srp_compl_xpath,
  79. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  80. // Operations
  81. kplugin_add_syms(plugin, ksym_new_ext("srp_set", srp_set,
  82. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  83. kplugin_add_syms(plugin, ksym_new_ext("srp_del", srp_del,
  84. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  85. // Note: 'edit', 'top', 'up' must be sync to set current path
  86. kplugin_add_syms(plugin, ksym_new_ext("srp_edit", srp_edit,
  87. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  88. kplugin_add_syms(plugin, ksym_new_ext("srp_top", srp_top,
  89. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  90. kplugin_add_syms(plugin, ksym_new_ext("srp_up", srp_up,
  91. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  92. kplugin_add_syms(plugin, ksym_new_ext("srp_insert", srp_insert,
  93. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  94. kplugin_add_syms(plugin, ksym_new_ext("srp_verify", srp_verify,
  95. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  96. kplugin_add_syms(plugin, ksym_new_ext("srp_commit", srp_commit,
  97. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  98. kplugin_add_syms(plugin, ksym_new_ext("srp_reset", srp_reset,
  99. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  100. kplugin_add_syms(plugin, ksym_new_ext("srp_show_abs", srp_show_abs,
  101. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  102. kplugin_add_syms(plugin, ksym_new_ext("srp_show", srp_show,
  103. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  104. kplugin_add_syms(plugin, ksym_new_ext("srp_diff", srp_diff,
  105. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  106. kplugin_add_syms(plugin, ksym_new_ext("srp_deactivate", srp_deactivate,
  107. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  108. // User-data initialization
  109. udata = faux_zmalloc(sizeof(*udata));
  110. assert(udata);
  111. udata->path = NULL;
  112. udata->sr_conn = NULL;
  113. udata->sr_sess = NULL;
  114. udata->nacm_sub = NULL;
  115. // Settings
  116. udata->opts.begin_bracket = '{';
  117. udata->opts.end_bracket = '}';
  118. udata->opts.show_brackets = BOOL_TRUE;
  119. udata->opts.show_semicolons = BOOL_TRUE;
  120. udata->opts.first_key_w_stmt = BOOL_FALSE;
  121. udata->opts.keys_w_stmt = BOOL_TRUE;
  122. udata->opts.colorize = BOOL_TRUE;
  123. udata->opts.indent = 2;
  124. udata->opts.default_keys = BOOL_FALSE;
  125. udata->opts.show_default_keys = BOOL_FALSE;
  126. udata->opts.hide_passwords = BOOL_TRUE;
  127. udata->opts.enable_nacm = BOOL_FALSE;
  128. udata->opts.oneliners = BOOL_TRUE;
  129. parse_plugin_conf(kplugin_conf(plugin), &udata->opts);
  130. kplugin_set_udata(plugin, udata);
  131. // Logging
  132. ly_log_options(LY_LOSTORE);
  133. return 0;
  134. }
  135. int kplugin_sysrepo_fini(kcontext_t *context)
  136. {
  137. srp_udata_t *udata = NULL;
  138. assert(context);
  139. // Free plugin's user-data
  140. udata = (srp_udata_t *)kcontext_udata(context);
  141. assert(udata);
  142. if (udata->path)
  143. faux_argv_free(udata->path);
  144. faux_free(udata);
  145. return 0;
  146. }
  147. pline_opts_t *srp_udata_opts(kcontext_t *context)
  148. {
  149. srp_udata_t *udata = NULL;
  150. assert(context);
  151. udata = (srp_udata_t *)kcontext_udata(context);
  152. assert(udata);
  153. return &udata->opts;
  154. }
  155. faux_argv_t *srp_udata_path(kcontext_t *context)
  156. {
  157. srp_udata_t *udata = NULL;
  158. assert(context);
  159. udata = (srp_udata_t *)kcontext_udata(context);
  160. assert(udata);
  161. return udata->path;
  162. }
  163. void srp_udata_set_path(kcontext_t *context, faux_argv_t *path)
  164. {
  165. srp_udata_t *udata = NULL;
  166. assert(context);
  167. udata = (srp_udata_t *)kcontext_udata(context);
  168. assert(udata);
  169. if (udata->path)
  170. faux_argv_free(udata->path);
  171. udata->path = path;
  172. }
  173. sr_session_ctx_t *srp_udata_sr_sess(kcontext_t *context)
  174. {
  175. srp_udata_t *udata = NULL;
  176. assert(context);
  177. udata = (srp_udata_t *)kcontext_udata(context);
  178. assert(udata);
  179. return udata->sr_sess;
  180. }
  181. static int parse_plugin_conf(const char *conf, pline_opts_t *opts)
  182. {
  183. faux_ini_t *ini = NULL;
  184. const char *val = NULL;
  185. if (!opts)
  186. return -1;
  187. if (!conf)
  188. return 0; // Use defaults
  189. ini = faux_ini_new();
  190. if (!faux_ini_parse_str(ini, conf)) {
  191. faux_ini_free(ini);
  192. return -1;
  193. }
  194. if ((val = faux_ini_find(ini, "ShowBrackets"))) {
  195. if (faux_str_cmp(val, "y") == 0)
  196. opts->show_brackets = BOOL_TRUE;
  197. else if (faux_str_cmp(val, "n") == 0)
  198. opts->show_brackets = BOOL_FALSE;
  199. }
  200. if ((val = faux_ini_find(ini, "ShowSemicolons"))) {
  201. if (faux_str_cmp(val, "y") == 0)
  202. opts->show_semicolons = BOOL_TRUE;
  203. else if (faux_str_cmp(val, "n") == 0)
  204. opts->show_semicolons = BOOL_FALSE;
  205. }
  206. if ((val = faux_ini_find(ini, "FirstKeyWithStatement"))) {
  207. if (faux_str_cmp(val, "y") == 0)
  208. opts->first_key_w_stmt = BOOL_TRUE;
  209. else if (faux_str_cmp(val, "n") == 0)
  210. opts->first_key_w_stmt = BOOL_FALSE;
  211. }
  212. if ((val = faux_ini_find(ini, "KeysWithStatement"))) {
  213. if (faux_str_cmp(val, "y") == 0)
  214. opts->keys_w_stmt = BOOL_TRUE;
  215. else if (faux_str_cmp(val, "n") == 0)
  216. opts->keys_w_stmt = BOOL_FALSE;
  217. }
  218. if ((val = faux_ini_find(ini, "Colorize"))) {
  219. if (faux_str_cmp(val, "y") == 0)
  220. opts->colorize = BOOL_TRUE;
  221. else if (faux_str_cmp(val, "n") == 0)
  222. opts->colorize = BOOL_FALSE;
  223. }
  224. if ((val = faux_ini_find(ini, "Indent"))) {
  225. unsigned char indent = 0;
  226. if (faux_conv_atouc(val, &indent, 10))
  227. opts->indent = indent;
  228. }
  229. if ((val = faux_ini_find(ini, "DefaultKeys"))) {
  230. if (faux_str_cmp(val, "y") == 0)
  231. opts->default_keys = BOOL_TRUE;
  232. else if (faux_str_cmp(val, "n") == 0)
  233. opts->default_keys = BOOL_FALSE;
  234. }
  235. if ((val = faux_ini_find(ini, "ShowDefaultKeys"))) {
  236. if (faux_str_cmp(val, "y") == 0)
  237. opts->show_default_keys = BOOL_TRUE;
  238. else if (faux_str_cmp(val, "n") == 0)
  239. opts->show_default_keys = BOOL_FALSE;
  240. }
  241. if ((val = faux_ini_find(ini, "HidePasswords"))) {
  242. if (faux_str_cmp(val, "y") == 0)
  243. opts->hide_passwords = BOOL_TRUE;
  244. else if (faux_str_cmp(val, "n") == 0)
  245. opts->hide_passwords = BOOL_FALSE;
  246. }
  247. if ((val = faux_ini_find(ini, "EnableNACM"))) {
  248. if (faux_str_cmp(val, "y") == 0)
  249. opts->enable_nacm = BOOL_TRUE;
  250. else if (faux_str_cmp(val, "n") == 0)
  251. opts->enable_nacm = BOOL_FALSE;
  252. }
  253. if ((val = faux_ini_find(ini, "Oneliners"))) {
  254. if (faux_str_cmp(val, "y") == 0)
  255. opts->oneliners = BOOL_TRUE;
  256. else if (faux_str_cmp(val, "n") == 0)
  257. opts->oneliners = BOOL_FALSE;
  258. }
  259. faux_ini_free(ini);
  260. return 0;
  261. }
  262. static int kplugin_sysrepo_init_session(kcontext_t *context)
  263. {
  264. srp_udata_t *udata = NULL;
  265. const char *user = NULL;
  266. assert(context);
  267. udata = (srp_udata_t *)kcontext_udata(context);
  268. assert(udata);
  269. // Remote user name
  270. user = ksession_user(kcontext_session(context));
  271. // Connect to Sysrepo
  272. if (sr_connect(SR_CONN_DEFAULT, &(udata->sr_conn))) {
  273. syslog(LOG_ERR, "Can't connect to Sysrepo");
  274. return -1;
  275. }
  276. if (sr_session_start(udata->sr_conn, SRP_REPO_EDIT, &(udata->sr_sess))) {
  277. syslog(LOG_ERR, "Can't connect create Sysrepo session");
  278. sr_disconnect(udata->sr_conn);
  279. return -1;
  280. }
  281. sr_session_set_orig_name(udata->sr_sess, user);
  282. // Init NACM session
  283. if (udata->opts.enable_nacm) {
  284. if (sr_nacm_init(udata->sr_sess, 0, &(udata->nacm_sub)) != SR_ERR_OK) {
  285. sr_disconnect(udata->sr_conn);
  286. return -1;
  287. }
  288. sr_nacm_set_user(udata->sr_sess, user);
  289. }
  290. syslog(LOG_INFO, "Start SysRepo session for \"%s\"", user);
  291. return 0;
  292. }
  293. static int kplugin_sysrepo_fini_session(kcontext_t *context)
  294. {
  295. srp_udata_t *udata = NULL;
  296. const char *user = NULL;
  297. assert(context);
  298. udata = (srp_udata_t *)kcontext_udata(context);
  299. assert(udata);
  300. // Remote user name
  301. user = ksession_user(kcontext_session(context));
  302. if (udata->opts.enable_nacm) {
  303. sr_unsubscribe(udata->nacm_sub);
  304. sr_nacm_destroy();
  305. }
  306. sr_disconnect(udata->sr_conn);
  307. syslog(LOG_INFO, "Stop SysRepo session for \"%s\"", user ? user : "<unknown>");
  308. return 0;
  309. }