1
0

clish_config_callback.c 6.9 KB

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