clish_config_callback.c 7.8 KB

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