clish_config_callback.c 7.8 KB

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