plugin.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. // Completion/Help/Prompt
  59. kplugin_add_syms(plugin, ksym_new_ext("srp_compl", srp_compl,
  60. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  61. kplugin_add_syms(plugin, ksym_new_ext("srp_help", srp_help,
  62. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  63. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_set", srp_compl_set,
  64. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  65. kplugin_add_syms(plugin, ksym_new_ext("srp_help_set", srp_help_set,
  66. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  67. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_del", srp_compl_del,
  68. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  69. kplugin_add_syms(plugin, ksym_new_ext("srp_help_del", srp_help_del,
  70. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  71. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_edit", srp_compl_edit,
  72. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  73. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_edit_abs", srp_compl_edit_abs,
  74. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  75. kplugin_add_syms(plugin, ksym_new_ext("srp_help_edit", srp_help_edit,
  76. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  77. kplugin_add_syms(plugin, ksym_new_ext("srp_help_edit_abs", srp_help_edit_abs,
  78. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  79. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_insert", srp_compl_insert,
  80. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  81. kplugin_add_syms(plugin, ksym_new_ext("srp_help_insert", srp_help_insert,
  82. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  83. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_insert_to", srp_compl_insert_to,
  84. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  85. kplugin_add_syms(plugin, ksym_new_ext("srp_help_insert_to", srp_help_insert_to,
  86. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  87. kplugin_add_syms(plugin, ksym_new_ext("srp_prompt_edit_path", srp_prompt_edit_path,
  88. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  89. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_xpath", srp_compl_xpath,
  90. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  91. // Operations
  92. kplugin_add_syms(plugin, ksym_new_ext("srp_set", srp_set,
  93. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  94. kplugin_add_syms(plugin, ksym_new_ext("srp_del", srp_del,
  95. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  96. // Note: 'edit', 'top', 'up' must be sync to set current path
  97. kplugin_add_syms(plugin, ksym_new_ext("srp_edit", srp_edit,
  98. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  99. kplugin_add_syms(plugin, ksym_new_ext("srp_top", srp_top,
  100. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  101. kplugin_add_syms(plugin, ksym_new_ext("srp_up", srp_up,
  102. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  103. kplugin_add_syms(plugin, ksym_new_ext("srp_insert", srp_insert,
  104. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  105. kplugin_add_syms(plugin, ksym_new_ext("srp_verify", srp_verify,
  106. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  107. kplugin_add_syms(plugin, ksym_new_ext("srp_commit", srp_commit,
  108. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  109. kplugin_add_syms(plugin, ksym_new_ext("srp_reset", srp_reset,
  110. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  111. kplugin_add_syms(plugin, ksym_new_ext("srp_show_abs", srp_show_abs,
  112. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  113. kplugin_add_syms(plugin, ksym_new_ext("srp_show", srp_show,
  114. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  115. kplugin_add_syms(plugin, ksym_new_ext("srp_diff", srp_diff,
  116. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  117. kplugin_add_syms(plugin, ksym_new_ext("srp_deactivate", srp_deactivate,
  118. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  119. // User-data initialization
  120. udata = faux_zmalloc(sizeof(*udata));
  121. assert(udata);
  122. udata->path = NULL;
  123. udata->sr_conn = NULL;
  124. udata->sr_sess = NULL;
  125. udata->nacm_sub = NULL;
  126. // Settings
  127. pline_opts_init(&udata->opts);
  128. pline_opts_parse(kplugin_conf(plugin), &udata->opts);
  129. if (!kscheme_named_udata_new(scheme, SRP_UDATA_NAME, udata, free_udata))
  130. syslog(LOG_ERR, "Can't create name udata \"%s\"", SRP_UDATA_NAME);
  131. // Logging
  132. ly_log_options(LY_LOSTORE);
  133. return 0;
  134. }
  135. int kplugin_sysrepo_fini(kcontext_t *context)
  136. {
  137. context = context;
  138. return 0;
  139. }
  140. srp_udata_t *srp_udata(kcontext_t *context)
  141. {
  142. srp_udata_t *udata = NULL;
  143. assert(context);
  144. udata = (srp_udata_t *)kcontext_named_udata(context, SRP_UDATA_NAME);
  145. assert(udata);
  146. return udata;
  147. }
  148. pline_opts_t *srp_udata_opts(kcontext_t *context)
  149. {
  150. srp_udata_t *udata = NULL;
  151. assert(context);
  152. udata = srp_udata(context);
  153. assert(udata);
  154. return &udata->opts;
  155. }
  156. faux_argv_t *srp_udata_path(kcontext_t *context)
  157. {
  158. srp_udata_t *udata = NULL;
  159. assert(context);
  160. udata = srp_udata(context);
  161. assert(udata);
  162. return udata->path;
  163. }
  164. void srp_udata_set_path(kcontext_t *context, faux_argv_t *path)
  165. {
  166. srp_udata_t *udata = NULL;
  167. assert(context);
  168. udata = srp_udata(context);
  169. assert(udata);
  170. if (udata->path)
  171. faux_argv_free(udata->path);
  172. udata->path = path;
  173. }
  174. sr_session_ctx_t *srp_udata_sr_sess(kcontext_t *context)
  175. {
  176. srp_udata_t *udata = NULL;
  177. assert(context);
  178. udata = srp_udata(context);
  179. assert(udata);
  180. return udata->sr_sess;
  181. }
  182. static int kplugin_sysrepo_init_session(kcontext_t *context)
  183. {
  184. srp_udata_t *udata = NULL;
  185. const char *user = NULL;
  186. assert(context);
  187. udata = srp_udata(context);
  188. assert(udata);
  189. // Remote user name
  190. user = ksession_user(kcontext_session(context));
  191. // Connect to Sysrepo
  192. if (sr_connect(SR_CONN_DEFAULT, &(udata->sr_conn))) {
  193. syslog(LOG_ERR, "Can't connect to Sysrepo");
  194. return -1;
  195. }
  196. if (sr_session_start(udata->sr_conn, SRP_REPO_EDIT, &(udata->sr_sess))) {
  197. syslog(LOG_ERR, "Can't connect create Sysrepo session");
  198. sr_disconnect(udata->sr_conn);
  199. return -1;
  200. }
  201. sr_session_set_orig_name(udata->sr_sess, user);
  202. // Init NACM session
  203. if (udata->opts.enable_nacm) {
  204. if (sr_nacm_init(udata->sr_sess, 0, &(udata->nacm_sub)) != SR_ERR_OK) {
  205. sr_disconnect(udata->sr_conn);
  206. return -1;
  207. }
  208. sr_nacm_set_user(udata->sr_sess, user);
  209. }
  210. syslog(LOG_INFO, "Start SysRepo session for \"%s\"", user);
  211. return 0;
  212. }
  213. static int kplugin_sysrepo_fini_session(kcontext_t *context)
  214. {
  215. srp_udata_t *udata = NULL;
  216. const char *user = NULL;
  217. assert(context);
  218. udata = srp_udata(context);
  219. assert(udata);
  220. // Remote user name
  221. user = ksession_user(kcontext_session(context));
  222. if (udata->opts.enable_nacm) {
  223. sr_unsubscribe(udata->nacm_sub);
  224. sr_nacm_destroy();
  225. }
  226. sr_disconnect(udata->sr_conn);
  227. syslog(LOG_INFO, "Stop SysRepo session for \"%s\"", user ? user : "<unknown>");
  228. return 0;
  229. }