plugin.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. int kplugin_sysrepo_init(kcontext_t *context)
  23. {
  24. kplugin_t *plugin = NULL;
  25. srp_udata_t *udata = NULL;
  26. assert(context);
  27. plugin = kcontext_plugin(context);
  28. assert(plugin);
  29. // Symbols
  30. // Session init/fini
  31. kplugin_set_init_session_fn(plugin, kplugin_sysrepo_init_session);
  32. kplugin_set_fini_session_fn(plugin, kplugin_sysrepo_fini_session);
  33. // Types
  34. kplugin_add_syms(plugin, ksym_new_ext("PLINE_SET", srp_PLINE_SET,
  35. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  36. kplugin_add_syms(plugin, ksym_new_ext("PLINE_DEL", srp_PLINE_DEL,
  37. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  38. kplugin_add_syms(plugin, ksym_new_ext("PLINE_EDIT", srp_PLINE_EDIT,
  39. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  40. kplugin_add_syms(plugin, ksym_new_ext("PLINE_EDIT_ABS", srp_PLINE_EDIT_ABS,
  41. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  42. kplugin_add_syms(plugin, ksym_new_ext("PLINE_INSERT_FROM", srp_PLINE_INSERT_FROM,
  43. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  44. kplugin_add_syms(plugin, ksym_new_ext("PLINE_INSERT_TO", srp_PLINE_INSERT_TO,
  45. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  46. // Completion/Help/Prompt
  47. kplugin_add_syms(plugin, ksym_new_ext("srp_compl", srp_compl,
  48. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  49. kplugin_add_syms(plugin, ksym_new_ext("srp_help", srp_help,
  50. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  51. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_set", srp_compl_set,
  52. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  53. kplugin_add_syms(plugin, ksym_new_ext("srp_help_set", srp_help_set,
  54. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  55. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_del", srp_compl_del,
  56. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  57. kplugin_add_syms(plugin, ksym_new_ext("srp_help_del", srp_help_del,
  58. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  59. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_edit", srp_compl_edit,
  60. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  61. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_edit_abs", srp_compl_edit_abs,
  62. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  63. kplugin_add_syms(plugin, ksym_new_ext("srp_help_edit", srp_help_edit,
  64. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  65. kplugin_add_syms(plugin, ksym_new_ext("srp_help_edit_abs", srp_help_edit_abs,
  66. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  67. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_insert", srp_compl_insert,
  68. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  69. kplugin_add_syms(plugin, ksym_new_ext("srp_help_insert", srp_help_insert,
  70. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  71. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_insert_to", srp_compl_insert_to,
  72. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  73. kplugin_add_syms(plugin, ksym_new_ext("srp_help_insert_to", srp_help_insert_to,
  74. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  75. kplugin_add_syms(plugin, ksym_new_ext("srp_prompt_edit_path", srp_prompt_edit_path,
  76. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  77. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_xpath", srp_compl_xpath,
  78. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  79. // Operations
  80. kplugin_add_syms(plugin, ksym_new_ext("srp_set", srp_set,
  81. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  82. kplugin_add_syms(plugin, ksym_new_ext("srp_del", srp_del,
  83. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  84. // Note: 'edit', 'top', 'up' must be sync to set current path
  85. kplugin_add_syms(plugin, ksym_new_ext("srp_edit", srp_edit,
  86. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  87. kplugin_add_syms(plugin, ksym_new_ext("srp_top", srp_top,
  88. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  89. kplugin_add_syms(plugin, ksym_new_ext("srp_up", srp_up,
  90. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
  91. kplugin_add_syms(plugin, ksym_new_ext("srp_insert", srp_insert,
  92. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  93. kplugin_add_syms(plugin, ksym_new_ext("srp_verify", srp_verify,
  94. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  95. kplugin_add_syms(plugin, ksym_new_ext("srp_commit", srp_commit,
  96. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  97. kplugin_add_syms(plugin, ksym_new_ext("srp_reset", srp_reset,
  98. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  99. kplugin_add_syms(plugin, ksym_new_ext("srp_show_abs", srp_show_abs,
  100. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  101. kplugin_add_syms(plugin, ksym_new_ext("srp_show", srp_show,
  102. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  103. kplugin_add_syms(plugin, ksym_new_ext("srp_diff", srp_diff,
  104. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  105. kplugin_add_syms(plugin, ksym_new_ext("srp_deactivate", srp_deactivate,
  106. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
  107. // User-data initialization
  108. udata = faux_zmalloc(sizeof(*udata));
  109. assert(udata);
  110. udata->path = NULL;
  111. udata->sr_conn = NULL;
  112. udata->sr_sess = NULL;
  113. udata->nacm_sub = NULL;
  114. // Settings
  115. pline_opts_init(&udata->opts);
  116. pline_opts_parse(kplugin_conf(plugin), &udata->opts);
  117. kplugin_set_udata(plugin, udata);
  118. // Logging
  119. ly_log_options(LY_LOSTORE);
  120. return 0;
  121. }
  122. int kplugin_sysrepo_fini(kcontext_t *context)
  123. {
  124. srp_udata_t *udata = NULL;
  125. assert(context);
  126. // Free plugin's user-data
  127. udata = (srp_udata_t *)kcontext_udata(context);
  128. assert(udata);
  129. if (udata->path)
  130. faux_argv_free(udata->path);
  131. faux_free(udata);
  132. return 0;
  133. }
  134. pline_opts_t *srp_udata_opts(kcontext_t *context)
  135. {
  136. srp_udata_t *udata = NULL;
  137. assert(context);
  138. udata = (srp_udata_t *)kcontext_udata(context);
  139. assert(udata);
  140. return &udata->opts;
  141. }
  142. faux_argv_t *srp_udata_path(kcontext_t *context)
  143. {
  144. srp_udata_t *udata = NULL;
  145. assert(context);
  146. udata = (srp_udata_t *)kcontext_udata(context);
  147. assert(udata);
  148. return udata->path;
  149. }
  150. void srp_udata_set_path(kcontext_t *context, faux_argv_t *path)
  151. {
  152. srp_udata_t *udata = NULL;
  153. assert(context);
  154. udata = (srp_udata_t *)kcontext_udata(context);
  155. assert(udata);
  156. if (udata->path)
  157. faux_argv_free(udata->path);
  158. udata->path = path;
  159. }
  160. sr_session_ctx_t *srp_udata_sr_sess(kcontext_t *context)
  161. {
  162. srp_udata_t *udata = NULL;
  163. assert(context);
  164. udata = (srp_udata_t *)kcontext_udata(context);
  165. assert(udata);
  166. return udata->sr_sess;
  167. }
  168. static int kplugin_sysrepo_init_session(kcontext_t *context)
  169. {
  170. srp_udata_t *udata = NULL;
  171. const char *user = NULL;
  172. assert(context);
  173. udata = (srp_udata_t *)kcontext_udata(context);
  174. assert(udata);
  175. // Remote user name
  176. user = ksession_user(kcontext_session(context));
  177. // Connect to Sysrepo
  178. if (sr_connect(SR_CONN_DEFAULT, &(udata->sr_conn))) {
  179. syslog(LOG_ERR, "Can't connect to Sysrepo");
  180. return -1;
  181. }
  182. if (sr_session_start(udata->sr_conn, SRP_REPO_EDIT, &(udata->sr_sess))) {
  183. syslog(LOG_ERR, "Can't connect create Sysrepo session");
  184. sr_disconnect(udata->sr_conn);
  185. return -1;
  186. }
  187. sr_session_set_orig_name(udata->sr_sess, user);
  188. // Init NACM session
  189. if (udata->opts.enable_nacm) {
  190. if (sr_nacm_init(udata->sr_sess, 0, &(udata->nacm_sub)) != SR_ERR_OK) {
  191. sr_disconnect(udata->sr_conn);
  192. return -1;
  193. }
  194. sr_nacm_set_user(udata->sr_sess, user);
  195. }
  196. syslog(LOG_INFO, "Start SysRepo session for \"%s\"", user);
  197. return 0;
  198. }
  199. static int kplugin_sysrepo_fini_session(kcontext_t *context)
  200. {
  201. srp_udata_t *udata = NULL;
  202. const char *user = NULL;
  203. assert(context);
  204. udata = (srp_udata_t *)kcontext_udata(context);
  205. assert(udata);
  206. // Remote user name
  207. user = ksession_user(kcontext_session(context));
  208. if (udata->opts.enable_nacm) {
  209. sr_unsubscribe(udata->nacm_sub);
  210. sr_nacm_destroy();
  211. }
  212. sr_disconnect(udata->sr_conn);
  213. syslog(LOG_INFO, "Stop SysRepo session for \"%s\"", user ? user : "<unknown>");
  214. return 0;
  215. }