1
0

clish_config_callback.c 6.7 KB

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