clish_config_callback.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. konf_buf_delete(buf);
  235. return retval;
  236. }
  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. }