callback_config.c 4.8 KB

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