clish_config_callback.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * clish_config_callback.c
  3. *
  4. *
  5. * Callback hook to execute config operations.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <sys/types.h>
  10. #include <assert.h>
  11. #include <sys/socket.h>
  12. #include <sys/un.h>
  13. #include <limits.h>
  14. #include "private.h"
  15. #include "konf/net.h"
  16. #include "konf/buf.h"
  17. #include "konf/query.h"
  18. #include "clish/variable.h"
  19. static int send_request(konf_client_t * client, char *command);
  20. static int receive_answer(konf_client_t * client, konf_buf_t **data);
  21. static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data);
  22. static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data);
  23. /*--------------------------------------------------------- */
  24. bool_t
  25. clish_config_callback(const clish_shell_t * this,
  26. const clish_command_t * cmd, clish_pargv_t * pargv)
  27. {
  28. unsigned i;
  29. char *command = NULL;
  30. konf_client_t *client;
  31. konf_buf_t *buf = NULL;
  32. const char *viewid = NULL;
  33. char *str = NULL;
  34. char tmp[PATH_MAX + 100];
  35. clish_config_operation_t op;
  36. if (!this)
  37. return BOOL_TRUE;
  38. viewid = clish_shell__get_viewid(this);
  39. op = clish_command__get_cfg_op(cmd);
  40. switch (op) {
  41. case CLISH_CONFIG_NONE:
  42. return BOOL_TRUE;
  43. case CLISH_CONFIG_SET:
  44. /* Add set operation */
  45. lub_string_cat(&command, "-s");
  46. /* Add entered line */
  47. str = clish_variable__get_line(cmd, pargv);
  48. lub_string_cat(&command, " -l \"");
  49. lub_string_cat(&command, str);
  50. lub_string_cat(&command, "\"");
  51. lub_string_free(str);
  52. /* Add splitter */
  53. if (clish_command__get_splitter(cmd) == BOOL_FALSE)
  54. lub_string_cat(&command, " -i");
  55. /* Add unique */
  56. if (clish_command__get_unique(cmd) == BOOL_FALSE)
  57. lub_string_cat(&command, " -n");
  58. break;
  59. case CLISH_CONFIG_UNSET:
  60. /* Add unset operation */
  61. lub_string_cat(&command, "-u");
  62. break;
  63. case CLISH_CONFIG_DUMP:
  64. /* Add dump operation */
  65. lub_string_cat(&command, "-d");
  66. /* Add filename */
  67. str = clish_command__get_file(cmd, viewid, pargv);
  68. if (str) {
  69. lub_string_cat(&command, " -f \"");
  70. if (str[0] != '\0')
  71. lub_string_cat(&command, str);
  72. else
  73. lub_string_cat(&command, "/tmp/running-config");
  74. lub_string_cat(&command, "\"");
  75. lub_string_free(str);
  76. }
  77. break;
  78. default:
  79. return BOOL_FALSE;
  80. };
  81. /* Add pattern */
  82. if ((CLISH_CONFIG_SET == op) || (CLISH_CONFIG_UNSET == op)) {
  83. str = clish_command__get_pattern(cmd, viewid, pargv);
  84. if (!str) {
  85. lub_string_free(command);
  86. return BOOL_FALSE;
  87. }
  88. lub_string_cat(&command, " -r \"");
  89. lub_string_cat(&command, str);
  90. lub_string_cat(&command, "\"");
  91. lub_string_free(str);
  92. }
  93. /* Add priority */
  94. if (clish_command__get_priority(cmd) != 0) {
  95. snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
  96. clish_command__get_priority(cmd));
  97. tmp[sizeof(tmp) - 1] = '\0';
  98. lub_string_cat(&command, tmp);
  99. }
  100. /* Add sequence */
  101. if (clish_command__is_seq(cmd)) {
  102. snprintf(tmp, sizeof(tmp) - 1, " -q %u",
  103. clish_command__get_seq(cmd,
  104. viewid, pargv));
  105. tmp[sizeof(tmp) - 1] = '\0';
  106. lub_string_cat(&command, tmp);
  107. }
  108. /* Add pwd */
  109. str = clish_shell__get_pwd_full(this,
  110. clish_command__get_cfg_depth(cmd, viewid, pargv));
  111. if (str) {
  112. lub_string_cat(&command, " ");
  113. lub_string_cat(&command, str);
  114. lub_string_free(str);
  115. }
  116. client = clish_shell__get_client(this);
  117. #ifdef DEBUG
  118. fprintf(stderr, "CONFIG request: %s\n", command);
  119. #endif
  120. if (send_request(client, command) < 0) {
  121. fprintf(stderr, "Cannot write to the running-config.\n");
  122. }
  123. if (receive_answer(client, &buf) < 0) {
  124. fprintf(stderr, "Cannot get answer from config daemon.\n");
  125. }
  126. lub_string_free(command);
  127. /* Postprocessing. Get data from daemon etc. */
  128. switch (op) {
  129. case CLISH_CONFIG_DUMP:
  130. str = clish_command__get_file(cmd, viewid, pargv);
  131. if (str) {
  132. FILE *fd = fopen(str, "r");
  133. lub_string_free(str);
  134. if (!fd)
  135. break;
  136. while (fgets(tmp, sizeof(tmp), fd))
  137. fprintf(clish_shell__get_ostream(this),
  138. "%s", tmp);
  139. fclose(fd);
  140. }
  141. if (buf) {
  142. konf_buf_lseek(buf, 0);
  143. while ((str = konf_buf_preparse(buf))) {
  144. if (strlen(str) == 0) {
  145. lub_string_free(str);
  146. break;
  147. }
  148. fprintf(clish_shell__get_ostream(this),
  149. "%s\n", str);
  150. lub_string_free(str);
  151. }
  152. konf_buf_delete(buf);
  153. }
  154. break;
  155. default:
  156. break;
  157. };
  158. return BOOL_TRUE;
  159. }
  160. /*--------------------------------------------------------- */
  161. static int send_request(konf_client_t * client, char *command)
  162. {
  163. if ((konf_client_connect(client) < 0))
  164. return -1;
  165. if (konf_client_send(client, command) < 0) {
  166. if (konf_client_reconnect(client) < 0)
  167. return -1;
  168. if (konf_client_send(client, command) < 0)
  169. return -1;
  170. }
  171. return 0;
  172. }
  173. static int receive_answer(konf_client_t * client, konf_buf_t **data)
  174. {
  175. konf_buf_t *buf;
  176. int nbytes;
  177. char *str;
  178. int retval = 0;
  179. int processed = 0;
  180. if ((konf_client_connect(client) < 0))
  181. return -1;
  182. buf = konf_buf_new(konf_client__get_sock(client));
  183. while ((!processed) && (nbytes = konf_buf_read(buf)) > 0) {
  184. while ((str = konf_buf_parse(buf))) {
  185. konf_buf_t *tmpdata = NULL;
  186. retval = process_answer(client, str, buf, &tmpdata);
  187. lub_string_free(str);
  188. if (retval < 0) {
  189. konf_buf_delete(buf);
  190. return retval;
  191. }
  192. if (retval == 0)
  193. processed = 1;
  194. if (tmpdata) {
  195. if (*data)
  196. konf_buf_delete(*data);
  197. *data = tmpdata;
  198. }
  199. }
  200. }
  201. konf_buf_delete(buf);
  202. return retval;
  203. }
  204. static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data)
  205. {
  206. konf_buf_t *tmpdata;
  207. char *str;
  208. int retval = 0;
  209. int processed = 0;
  210. if ((konf_client_connect(client) < 0))
  211. return -1;
  212. tmpdata = konf_buf_new(konf_client__get_sock(client));
  213. do {
  214. while ((str = konf_buf_parse(buf))) {
  215. #ifdef DEBUG
  216. fprintf(stderr, "RECV DATA: [%s]\n", str);
  217. #endif
  218. konf_buf_add(tmpdata, str, strlen(str) + 1);
  219. if (strlen(str) == 0) {
  220. processed = 1;
  221. lub_string_free(str);
  222. break;
  223. }
  224. lub_string_free(str);
  225. }
  226. } while ((!processed) && (konf_buf_read(buf)) > 0);
  227. if (!processed) {
  228. konf_buf_delete(tmpdata);
  229. *data = NULL;
  230. return -1;
  231. }
  232. *data = tmpdata;
  233. return 0;
  234. }
  235. static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data)
  236. {
  237. int res;
  238. konf_query_t *query;
  239. /* Parse query */
  240. query = konf_query_new();
  241. res = konf_query_parse_str(query, str);
  242. if (res < 0) {
  243. konf_query_free(query);
  244. #ifdef DEBUG
  245. fprintf(stderr, "CONFIG error: Cannot parse answer string.\n");
  246. #endif
  247. return -1;
  248. }
  249. #ifdef DEBUG
  250. fprintf(stderr, "CONFIG answer: %s\n", str);
  251. // konf_query_dump(query);
  252. #endif
  253. switch (konf_query__get_op(query)) {
  254. case KONF_QUERY_OP_OK:
  255. res = 0;
  256. break;
  257. case KONF_QUERY_OP_ERROR:
  258. res = -1;
  259. break;
  260. case KONF_QUERY_OP_STREAM:
  261. if (receive_data(client, buf, data) < 0)
  262. res = -1;
  263. else
  264. res = 1; /* wait for another answer */
  265. break;
  266. default:
  267. res = -1;
  268. break;
  269. }
  270. /* Free resources */
  271. konf_query_free(query);
  272. return res;
  273. }