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