clish_config_callback.c 8.3 KB

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