hook_config.c 5.1 KB

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