clish_config_callback.c 7.9 KB

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