clish_config_callback.c 8.0 KB

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