plugin.c 9.6 KB

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