plugin.c 10 KB

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