plugin.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 "klish_plugin_sysrepo.h"
  18. const uint8_t kplugin_sysrepo_major = KPLUGIN_MAJOR;
  19. const uint8_t kplugin_sysrepo_minor = KPLUGIN_MINOR;
  20. static int kplugin_sysrepo_init_session(kcontext_t *context);
  21. static int kplugin_sysrepo_fini_session(kcontext_t *context);
  22. static bool_t free_udata(void *data)
  23. {
  24. srp_udata_t *udata = (srp_udata_t *)data;
  25. assert(udata);
  26. if (udata->path)
  27. faux_argv_free(udata->path);
  28. faux_free(udata);
  29. return BOOL_TRUE;
  30. }
  31. int kplugin_sysrepo_init(kcontext_t *context)
  32. {
  33. kplugin_t *plugin = NULL;
  34. srp_udata_t *udata = NULL;
  35. kscheme_t *scheme = NULL;
  36. assert(context);
  37. plugin = kcontext_plugin(context);
  38. assert(plugin);
  39. scheme = kcontext_scheme(context);
  40. assert(scheme);
  41. // Symbols
  42. // Session init/fini
  43. kplugin_set_init_session_fn(plugin, kplugin_sysrepo_init_session);
  44. kplugin_set_fini_session_fn(plugin, kplugin_sysrepo_fini_session);
  45. // Types
  46. kplugin_add_syms(plugin, ksym_new_ext("PLINE_SET", srp_PLINE_SET,
  47. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  48. kplugin_add_syms(plugin, ksym_new_ext("PLINE_DEL", srp_PLINE_DEL,
  49. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  50. kplugin_add_syms(plugin, ksym_new_ext("PLINE_EDIT", srp_PLINE_EDIT,
  51. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  52. kplugin_add_syms(plugin, ksym_new_ext("PLINE_EDIT_ABS", srp_PLINE_EDIT_ABS,
  53. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  54. kplugin_add_syms(plugin, ksym_new_ext("PLINE_INSERT_FROM", srp_PLINE_INSERT_FROM,
  55. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  56. kplugin_add_syms(plugin, ksym_new_ext("PLINE_INSERT_TO", srp_PLINE_INSERT_TO,
  57. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  58. kplugin_add_syms(plugin, ksym_new_ext("PLINE_SHOW", srp_PLINE_SHOW,
  59. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  60. kplugin_add_syms(plugin, ksym_new_ext("PLINE_SHOW_ABS", srp_PLINE_SHOW_ABS,
  61. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  62. // Completion/Help/Prompt
  63. kplugin_add_syms(plugin, ksym_new_ext("srp_compl", srp_compl,
  64. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  65. kplugin_add_syms(plugin, ksym_new_ext("srp_help", srp_help,
  66. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  67. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_set", srp_compl_set,
  68. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  69. kplugin_add_syms(plugin, ksym_new_ext("srp_help_set", srp_help_set,
  70. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  71. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_del", srp_compl_del,
  72. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  73. kplugin_add_syms(plugin, ksym_new_ext("srp_help_del", srp_help_del,
  74. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  75. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_edit", srp_compl_edit,
  76. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  77. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_edit_abs", srp_compl_edit_abs,
  78. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  79. kplugin_add_syms(plugin, ksym_new_ext("srp_help_edit", srp_help_edit,
  80. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  81. kplugin_add_syms(plugin, ksym_new_ext("srp_help_edit_abs", srp_help_edit_abs,
  82. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  83. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_insert", srp_compl_insert,
  84. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  85. kplugin_add_syms(plugin, ksym_new_ext("srp_help_insert", srp_help_insert,
  86. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  87. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_insert_to", srp_compl_insert_to,
  88. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  89. kplugin_add_syms(plugin, ksym_new_ext("srp_help_insert_to", srp_help_insert_to,
  90. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  91. kplugin_add_syms(plugin, ksym_new_ext("srp_prompt_edit_path", srp_prompt_edit_path,
  92. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  93. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_xpath", srp_compl_xpath,
  94. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  95. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_show", srp_compl_show,
  96. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  97. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_show_abs", srp_compl_show_abs,
  98. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  99. kplugin_add_syms(plugin, ksym_new_ext("srp_help_show", srp_help_show,
  100. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  101. kplugin_add_syms(plugin, ksym_new_ext("srp_help_show_abs", srp_help_show_abs,
  102. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  103. // Operations
  104. kplugin_add_syms(plugin, ksym_new_ext("srp_set", srp_set,
  105. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  106. kplugin_add_syms(plugin, ksym_new_ext("srp_del", srp_del,
  107. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  108. // Note: 'edit', 'top', 'up' must be sync to set current path
  109. kplugin_add_syms(plugin, ksym_new_ext("srp_edit", srp_edit,
  110. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  111. kplugin_add_syms(plugin, ksym_new_ext("srp_top", srp_top,
  112. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  113. kplugin_add_syms(plugin, ksym_new_ext("srp_up", srp_up,
  114. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  115. kplugin_add_syms(plugin, ksym_new_ext("srp_insert", srp_insert,
  116. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  117. kplugin_add_syms(plugin, ksym_new_ext("srp_verify", srp_verify,
  118. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  119. kplugin_add_syms(plugin, ksym_new_ext("srp_commit", srp_commit,
  120. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  121. kplugin_add_syms(plugin, ksym_new_ext("srp_reset", srp_reset,
  122. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  123. kplugin_add_syms(plugin, ksym_new_ext("srp_show_abs", srp_show_abs,
  124. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  125. kplugin_add_syms(plugin, ksym_new_ext("srp_show", srp_show,
  126. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  127. kplugin_add_syms(plugin, ksym_new_ext("srp_diff", srp_diff,
  128. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  129. kplugin_add_syms(plugin, ksym_new_ext("srp_deactivate", srp_deactivate,
  130. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  131. // User-data initialization
  132. udata = faux_zmalloc(sizeof(*udata));
  133. assert(udata);
  134. udata->path = NULL;
  135. udata->sr_conn = NULL;
  136. udata->sr_sess = NULL;
  137. udata->nacm_sub = NULL;
  138. // Settings
  139. pline_opts_init(&udata->opts);
  140. pline_opts_parse(kplugin_conf(plugin), &udata->opts);
  141. if (!kscheme_named_udata_new(scheme, SRP_UDATA_NAME, udata, free_udata))
  142. syslog(LOG_ERR, "Can't create name udata \"%s\"", SRP_UDATA_NAME);
  143. // Logging
  144. ly_log_options(LY_LOSTORE);
  145. return 0;
  146. }
  147. int kplugin_sysrepo_fini(kcontext_t *context)
  148. {
  149. context = context;
  150. return 0;
  151. }
  152. srp_udata_t *srp_udata(kcontext_t *context)
  153. {
  154. srp_udata_t *udata = NULL;
  155. assert(context);
  156. udata = (srp_udata_t *)kcontext_named_udata(context, SRP_UDATA_NAME);
  157. assert(udata);
  158. return udata;
  159. }
  160. pline_opts_t *srp_udata_opts(kcontext_t *context)
  161. {
  162. srp_udata_t *udata = NULL;
  163. assert(context);
  164. udata = srp_udata(context);
  165. assert(udata);
  166. return &udata->opts;
  167. }
  168. faux_argv_t *srp_udata_path(kcontext_t *context)
  169. {
  170. srp_udata_t *udata = NULL;
  171. assert(context);
  172. udata = srp_udata(context);
  173. assert(udata);
  174. return udata->path;
  175. }
  176. void srp_udata_set_path(kcontext_t *context, faux_argv_t *path)
  177. {
  178. srp_udata_t *udata = NULL;
  179. assert(context);
  180. udata = srp_udata(context);
  181. assert(udata);
  182. if (udata->path)
  183. faux_argv_free(udata->path);
  184. udata->path = path;
  185. }
  186. static bool_t kplugin_sysrepo_connect(kcontext_t *context)
  187. {
  188. srp_udata_t *udata = NULL;
  189. const char *user = NULL;
  190. assert(context);
  191. udata = srp_udata(context);
  192. assert(udata);
  193. // Remote user name
  194. user = ksession_user(kcontext_session(context));
  195. // Connect to Sysrepo
  196. if (sr_connect(SR_CONN_DEFAULT, &(udata->sr_conn))) {
  197. udata->sr_conn = NULL;
  198. syslog(LOG_ERR, "Can't connect to Sysrepo");
  199. return BOOL_FALSE;
  200. }
  201. if (sr_session_start(udata->sr_conn, SRP_REPO_EDIT, &(udata->sr_sess))) {
  202. sr_disconnect(udata->sr_conn);
  203. udata->sr_conn = NULL;
  204. syslog(LOG_ERR, "Can't connect create Sysrepo session");
  205. return BOOL_FALSE;
  206. }
  207. sr_session_set_orig_name(udata->sr_sess, user);
  208. // Init NACM session
  209. if (udata->opts.enable_nacm) {
  210. if (sr_nacm_init(udata->sr_sess, 0, &(udata->nacm_sub)) != SR_ERR_OK) {
  211. sr_disconnect(udata->sr_conn);
  212. udata->sr_conn = NULL;
  213. return BOOL_FALSE;
  214. }
  215. sr_nacm_set_user(udata->sr_sess, user);
  216. }
  217. syslog(LOG_INFO, "Start SysRepo session for \"%s\"", user);
  218. return BOOL_TRUE;
  219. }
  220. sr_session_ctx_t *srp_udata_sr_sess(kcontext_t *context)
  221. {
  222. srp_udata_t *udata = NULL;
  223. assert(context);
  224. udata = srp_udata(context);
  225. assert(udata);
  226. // Sysrepo is already connected
  227. if (udata->sr_conn)
  228. return udata->sr_sess;
  229. // Lazy connection to Sysrepo
  230. if (!kplugin_sysrepo_connect(context)) {
  231. fprintf(stderr, "Error: Can't connect to config storage");
  232. return NULL;
  233. }
  234. return udata->sr_sess;
  235. }
  236. static int kplugin_sysrepo_init_session(kcontext_t *context)
  237. {
  238. context = context; // Happy compiler
  239. return 0;
  240. }
  241. static int kplugin_sysrepo_fini_session(kcontext_t *context)
  242. {
  243. srp_udata_t *udata = NULL;
  244. assert(context);
  245. udata = srp_udata(context);
  246. assert(udata);
  247. // Due to lazy connect to sysrepo the connection can be down
  248. if (udata->sr_conn) {
  249. const char *user = NULL;
  250. if (udata->opts.enable_nacm) {
  251. sr_unsubscribe(udata->nacm_sub);
  252. sr_nacm_destroy();
  253. }
  254. sr_disconnect(udata->sr_conn);
  255. // Remote user name
  256. user = ksession_user(kcontext_session(context));
  257. syslog(LOG_INFO, "Stop SysRepo session for \"%s\"",
  258. user ? user : "<unknown>");
  259. }
  260. return 0;
  261. }