hook_config.c 5.1 KB

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