1
0

callback_config.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 *tstr;
  50. char tmp[PATH_MAX + 100];
  51. clish_config_op_t op;
  52. unsigned int num;
  53. const char *escape_chars = lub_string_esc_quoted;
  54. if (!this)
  55. return BOOL_TRUE;
  56. client = clish_shell__get_client(this);
  57. if (!client)
  58. return BOOL_TRUE;
  59. config = clish_command__get_config(cmd);
  60. op = clish_config__get_op(config);
  61. switch (op) {
  62. case CLISH_CONFIG_NONE:
  63. return BOOL_TRUE;
  64. case CLISH_CONFIG_SET:
  65. /* Add set operation */
  66. lub_string_cat(&command, "-s");
  67. /* Add entered line */
  68. tstr = clish_shell__get_line(context);
  69. str = lub_string_encode(tstr, escape_chars);
  70. lub_string_free(tstr);
  71. lub_string_cat(&command, " -l \"");
  72. lub_string_cat(&command, str);
  73. lub_string_cat(&command, "\"");
  74. lub_string_free(str);
  75. /* Add splitter */
  76. if (!clish_config__get_splitter(config))
  77. lub_string_cat(&command, " -i");
  78. /* Add unique */
  79. if (!clish_config__get_unique(config))
  80. lub_string_cat(&command, " -n");
  81. break;
  82. case CLISH_CONFIG_UNSET:
  83. /* Add unset operation */
  84. lub_string_cat(&command, "-u");
  85. break;
  86. case CLISH_CONFIG_DUMP:
  87. /* Add dump operation */
  88. lub_string_cat(&command, "-d");
  89. /* Add filename */
  90. str = clish_shell_expand(clish_config__get_file(config), SHELL_VAR_ACTION, context);
  91. if (str) {
  92. lub_string_cat(&command, " -f \"");
  93. if (str[0] != '\0')
  94. lub_string_cat(&command, str);
  95. else
  96. lub_string_cat(&command, "/tmp/running-config");
  97. lub_string_cat(&command, "\"");
  98. lub_string_free(str);
  99. }
  100. break;
  101. default:
  102. return BOOL_FALSE;
  103. };
  104. /* Add pattern */
  105. if ((CLISH_CONFIG_SET == op) || (CLISH_CONFIG_UNSET == op)) {
  106. tstr = clish_shell_expand(clish_config__get_pattern(config), SHELL_VAR_REGEX, context);
  107. if (!tstr) {
  108. lub_string_free(command);
  109. return BOOL_FALSE;
  110. }
  111. str = lub_string_encode(tstr, escape_chars);
  112. lub_string_free(tstr);
  113. lub_string_cat(&command, " -r \"");
  114. lub_string_cat(&command, str);
  115. lub_string_cat(&command, "\"");
  116. lub_string_free(str);
  117. }
  118. /* Add priority */
  119. if (clish_config__get_priority(config) != 0) {
  120. snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
  121. clish_config__get_priority(config));
  122. tmp[sizeof(tmp) - 1] = '\0';
  123. lub_string_cat(&command, tmp);
  124. }
  125. /* Add sequence */
  126. if (clish_config__get_seq(config)) {
  127. str = clish_shell_expand(clish_config__get_seq(config), SHELL_VAR_ACTION, context);
  128. snprintf(tmp, sizeof(tmp) - 1, " -q %u", str2ushort(str));
  129. tmp[sizeof(tmp) - 1] = '\0';
  130. lub_string_cat(&command, tmp);
  131. lub_string_free(str);
  132. }
  133. /* Add pwd */
  134. if (clish_config__get_depth(config)) {
  135. str = clish_shell_expand(clish_config__get_depth(config), SHELL_VAR_ACTION, context);
  136. num = str2ushort(str);
  137. lub_string_free(str);
  138. } else {
  139. num = clish_command__get_depth(cmd);
  140. }
  141. str = clish_shell__get_pwd_full(this, num);
  142. if (str) {
  143. lub_string_cat(&command, " ");
  144. lub_string_cat(&command, str);
  145. lub_string_free(str);
  146. }
  147. #ifdef DEBUG
  148. fprintf(stderr, "CONFIG request: %s\n", command);
  149. #endif
  150. if (send_request(client, command) < 0) {
  151. fprintf(stderr, "Cannot write to the running-config.\n");
  152. }
  153. if (konf_client_recv_answer(client, &buf) < 0) {
  154. fprintf(stderr, "The error while request to the config daemon.\n");
  155. }
  156. lub_string_free(command);
  157. /* Postprocessing. Get data from daemon etc. */
  158. switch (op) {
  159. case CLISH_CONFIG_DUMP:
  160. if (buf) {
  161. konf_buf_lseek(buf, 0);
  162. while ((str = konf_buf_preparse(buf))) {
  163. if (strlen(str) == 0) {
  164. lub_string_free(str);
  165. break;
  166. }
  167. fprintf(clish_shell__get_ostream(this),
  168. "%s\n", str);
  169. lub_string_free(str);
  170. }
  171. konf_buf_delete(buf);
  172. }
  173. break;
  174. default:
  175. break;
  176. };
  177. return BOOL_TRUE;
  178. }
  179. /*--------------------------------------------------------- */
  180. static int send_request(konf_client_t * client, char *command)
  181. {
  182. if ((konf_client_connect(client) < 0))
  183. return -1;
  184. if (konf_client_send(client, command) < 0) {
  185. if (konf_client_reconnect(client) < 0)
  186. return -1;
  187. if (konf_client_send(client, command) < 0)
  188. return -1;
  189. }
  190. return 0;
  191. }