clish_config_callback.c 8.3 KB

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