callback_config.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <limits.h>
  14. #include "internal.h"
  15. #include "konf/net.h"
  16. #include "konf/buf.h"
  17. #include "konf/query.h"
  18. #include "lub/string.h"
  19. #include "clish/variable.h"
  20. static int send_request(konf_client_t * client, char *command);
  21. /*--------------------------------------------------------- */
  22. bool_t clish_config_callback(clish_context_t *context)
  23. {
  24. clish_shell_t *this = context->shell;
  25. const clish_command_t *cmd = context->cmd;
  26. clish_pargv_t *pargv = context->pargv;
  27. char *command = NULL;
  28. konf_client_t *client;
  29. konf_buf_t *buf = NULL;
  30. const char *viewid = NULL;
  31. char *str = NULL;
  32. char tmp[PATH_MAX + 100];
  33. clish_config_operation_t op;
  34. if (!this)
  35. return BOOL_TRUE;
  36. client = clish_shell__get_client(this);
  37. if (!client)
  38. return BOOL_TRUE;
  39. viewid = clish_shell__get_viewid(this);
  40. op = clish_command__get_cfg_op(cmd);
  41. switch (op) {
  42. case CLISH_CONFIG_NONE:
  43. return BOOL_TRUE;
  44. case CLISH_CONFIG_SET:
  45. /* Add set operation */
  46. lub_string_cat(&command, "-s");
  47. /* Add entered line */
  48. str = clish_shell__get_line(cmd, pargv);
  49. lub_string_cat(&command, " -l \"");
  50. lub_string_cat(&command, str);
  51. lub_string_cat(&command, "\"");
  52. lub_string_free(str);
  53. /* Add splitter */
  54. if (clish_command__get_splitter(cmd) == BOOL_FALSE)
  55. lub_string_cat(&command, " -i");
  56. /* Add unique */
  57. if (clish_command__get_unique(cmd) == BOOL_FALSE)
  58. lub_string_cat(&command, " -n");
  59. break;
  60. case CLISH_CONFIG_UNSET:
  61. /* Add unset operation */
  62. lub_string_cat(&command, "-u");
  63. break;
  64. case CLISH_CONFIG_DUMP:
  65. /* Add dump operation */
  66. lub_string_cat(&command, "-d");
  67. /* Add filename */
  68. str = clish_command__get_file(cmd, context);
  69. if (str) {
  70. lub_string_cat(&command, " -f \"");
  71. if (str[0] != '\0')
  72. lub_string_cat(&command, str);
  73. else
  74. lub_string_cat(&command, "/tmp/running-config");
  75. lub_string_cat(&command, "\"");
  76. lub_string_free(str);
  77. }
  78. break;
  79. default:
  80. return BOOL_FALSE;
  81. };
  82. /* Add pattern */
  83. if ((CLISH_CONFIG_SET == op) || (CLISH_CONFIG_UNSET == op)) {
  84. str = clish_command__get_pattern(cmd, context);
  85. if (!str) {
  86. lub_string_free(command);
  87. return BOOL_FALSE;
  88. }
  89. lub_string_cat(&command, " -r \"");
  90. lub_string_cat(&command, str);
  91. lub_string_cat(&command, "\"");
  92. lub_string_free(str);
  93. }
  94. /* Add priority */
  95. if (clish_command__get_priority(cmd) != 0) {
  96. snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
  97. clish_command__get_priority(cmd));
  98. tmp[sizeof(tmp) - 1] = '\0';
  99. lub_string_cat(&command, tmp);
  100. }
  101. /* Add sequence */
  102. if (clish_command__is_seq(cmd)) {
  103. snprintf(tmp, sizeof(tmp) - 1, " -q %u",
  104. clish_command__get_seq(cmd, context));
  105. tmp[sizeof(tmp) - 1] = '\0';
  106. lub_string_cat(&command, tmp);
  107. }
  108. /* Add pwd */
  109. str = clish_shell__get_pwd_full(this,
  110. clish_command__get_cfg_depth(cmd, context));
  111. if (str) {
  112. lub_string_cat(&command, " ");
  113. lub_string_cat(&command, str);
  114. lub_string_free(str);
  115. }
  116. #ifdef DEBUG
  117. fprintf(stderr, "CONFIG request: %s\n", command);
  118. #endif
  119. if (send_request(client, command) < 0) {
  120. fprintf(stderr, "Cannot write to the running-config.\n");
  121. }
  122. if (konf_client_recv_answer(client, &buf) < 0) {
  123. fprintf(stderr, "The error while request to the config daemon.\n");
  124. }
  125. lub_string_free(command);
  126. /* Postprocessing. Get data from daemon etc. */
  127. switch (op) {
  128. case CLISH_CONFIG_DUMP:
  129. if (buf) {
  130. konf_buf_lseek(buf, 0);
  131. while ((str = konf_buf_preparse(buf))) {
  132. if (strlen(str) == 0) {
  133. lub_string_free(str);
  134. break;
  135. }
  136. fprintf(clish_shell__get_ostream(this),
  137. "%s\n", str);
  138. lub_string_free(str);
  139. }
  140. konf_buf_delete(buf);
  141. }
  142. break;
  143. default:
  144. break;
  145. };
  146. return BOOL_TRUE;
  147. }
  148. /*--------------------------------------------------------- */
  149. static int send_request(konf_client_t * client, char *command)
  150. {
  151. if ((konf_client_connect(client) < 0))
  152. return -1;
  153. if (konf_client_send(client, command) < 0) {
  154. if (konf_client_reconnect(client) < 0)
  155. return -1;
  156. if (konf_client_send(client, command) < 0)
  157. return -1;
  158. }
  159. return 0;
  160. }