1
0

clish_config_callback.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "cliconf/net.h"
  15. #include "cliconf/buf.h"
  16. #include "cliconf/query.h"
  17. #include "clish/variable.h"
  18. static int send_request(conf_client_t * client, char *command);
  19. static int receive_answer(conf_client_t * client, conf_buf_t **data);
  20. static int process_answer(conf_client_t * client, char *str, conf_buf_t *buf, conf_buf_t **data);
  21. static int receive_data(conf_client_t * client, conf_buf_t *buf, conf_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. conf_client_t *client;
  31. conf_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. printf("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(stdout, "%s", str);
  146. fclose(fd);
  147. }
  148. if (buf) {
  149. char *str;
  150. conf_buf_lseek(buf, 0);
  151. while ((str = conf_buf_preparse(buf))) {
  152. if (strlen(str) == 0) {
  153. lub_string_free(str);
  154. break;
  155. }
  156. printf("%s\n", str);
  157. lub_string_free(str);
  158. }
  159. conf_buf_delete(buf);
  160. }
  161. break;
  162. }
  163. default:
  164. break;
  165. };
  166. return BOOL_TRUE;
  167. }
  168. /*--------------------------------------------------------- */
  169. static int send_request(conf_client_t * client, char *command)
  170. {
  171. if ((conf_client_connect(client) < 0))
  172. return -1;
  173. if (conf_client_send(client, command) < 0) {
  174. if (conf_client_reconnect(client) < 0)
  175. return -1;
  176. if (conf_client_send(client, command) < 0)
  177. return -1;
  178. }
  179. return 0;
  180. }
  181. static int receive_answer(conf_client_t * client, conf_buf_t **data)
  182. {
  183. conf_buf_t *buf;
  184. int nbytes;
  185. char *str;
  186. int retval = 0;
  187. int processed = 0;
  188. if ((conf_client_connect(client) < 0))
  189. return -1;
  190. buf = conf_buf_new(conf_client__get_sock(client));
  191. while ((!processed) && (nbytes = conf_buf_read(buf)) > 0) {
  192. while ((str = conf_buf_parse(buf))) {
  193. conf_buf_t *tmpdata = NULL;
  194. retval = process_answer(client, str, buf, &tmpdata);
  195. lub_string_free(str);
  196. if (retval < 0)
  197. return retval;
  198. if (retval == 0)
  199. processed = 1;
  200. if (tmpdata) {
  201. if (*data)
  202. conf_buf_delete(*data);
  203. *data = tmpdata;
  204. }
  205. }
  206. }
  207. conf_buf_delete(buf);
  208. return retval;
  209. }
  210. static int receive_data(conf_client_t * client, conf_buf_t *buf, conf_buf_t **data)
  211. {
  212. conf_buf_t *tmpdata;
  213. char *str;
  214. int retval = 0;
  215. int processed = 0;
  216. if ((conf_client_connect(client) < 0))
  217. return -1;
  218. tmpdata = conf_buf_new(conf_client__get_sock(client));
  219. do {
  220. while ((str = conf_buf_parse(buf))) {
  221. #ifdef DEBUG
  222. printf("RECV DATA: [%s]\n", str);
  223. #endif
  224. conf_buf_add(tmpdata, str, strlen(str) + 1);
  225. if (strlen(str) == 0) {
  226. processed = 1;
  227. lub_string_free(str);
  228. break;
  229. }
  230. lub_string_free(str);
  231. }
  232. } while ((!processed) && (conf_buf_read(buf)) > 0);
  233. if (!processed) {
  234. conf_buf_delete(tmpdata);
  235. *data = NULL;
  236. return -1;
  237. }
  238. *data = tmpdata;
  239. return 0;
  240. }
  241. static int process_answer(conf_client_t * client, char *str, conf_buf_t *buf, conf_buf_t **data)
  242. {
  243. int res;
  244. query_t *query;
  245. /* Parse query */
  246. query = query_new();
  247. res = query_parse_str(query, str);
  248. if (res < 0) {
  249. query_free(query);
  250. #ifdef DEBUG
  251. printf("CONFIG error: Cannot parse answer string.\n");
  252. #endif
  253. return -1;
  254. }
  255. #ifdef DEBUG
  256. printf("CONFIG answer: %s\n", str);
  257. // query_dump(query);
  258. #endif
  259. switch (query__get_op(query)) {
  260. case QUERY_OP_OK:
  261. res = 0;
  262. break;
  263. case QUERY_OP_ERROR:
  264. res = -1;
  265. break;
  266. case QUERY_OP_STREAM:
  267. if (receive_data(client, buf, data) < 0)
  268. res = -1;
  269. else
  270. res = 1; /* wait for another answer */
  271. break;
  272. default:
  273. res = -1;
  274. break;
  275. }
  276. /* Free resources */
  277. query_free(query);
  278. return res;
  279. }