hook_config.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "lub/conv.h"
  20. #include "clish/shell.h"
  21. static int send_request(konf_client_t * client, char *command);
  22. /*--------------------------------------------------------- */
  23. static unsigned short str2ushort(const char *str)
  24. {
  25. unsigned short num = 0;
  26. lub_conv_atous(str, &num, 0);
  27. return num;
  28. }
  29. /*--------------------------------------------------------- */
  30. /* Return values:
  31. * 0 - success
  32. * !=0 - fail
  33. */
  34. CLISH_HOOK_CONFIG(clish_hook_config)
  35. {
  36. clish_shell_t *this = clish_context__get_shell(clish_context);
  37. const clish_command_t *cmd = clish_context__get_cmd(clish_context);
  38. clish_config_t *config;
  39. char *command = NULL;
  40. konf_client_t *client;
  41. konf_buf_t *buf = NULL;
  42. char *str = NULL;
  43. char *tstr;
  44. char tmp[PATH_MAX + 100];
  45. clish_config_op_e op;
  46. unsigned int num;
  47. const char *escape_chars = lub_string_esc_quoted;
  48. if (!this)
  49. return 0;
  50. client = clish_shell__get_client(this);
  51. if (!client)
  52. return 0;
  53. config = clish_command__get_config(cmd);
  54. op = clish_config__get_op(config);
  55. switch (op) {
  56. case CLISH_CONFIG_NONE:
  57. return 0;
  58. case CLISH_CONFIG_SET:
  59. /* Add set operation */
  60. lub_string_cat(&command, "-s");
  61. /* Add entered line */
  62. tstr = clish_shell__get_line(clish_context);
  63. str = lub_string_encode(tstr, escape_chars);
  64. lub_string_free(tstr);
  65. lub_string_cat(&command, " -l \"");
  66. lub_string_cat(&command, str);
  67. lub_string_cat(&command, "\"");
  68. lub_string_free(str);
  69. /* Add splitter */
  70. if (!clish_config__get_splitter(config))
  71. lub_string_cat(&command, " -i");
  72. /* Add unique */
  73. if (!clish_config__get_unique(config))
  74. lub_string_cat(&command, " -n");
  75. break;
  76. case CLISH_CONFIG_UNSET:
  77. /* Add unset operation */
  78. lub_string_cat(&command, "-u");
  79. break;
  80. case CLISH_CONFIG_DUMP:
  81. /* Add dump operation */
  82. lub_string_cat(&command, "-d");
  83. /* Add filename */
  84. str = clish_shell_expand(clish_config__get_file(config), SHELL_VAR_ACTION, clish_context);
  85. if (str) {
  86. lub_string_cat(&command, " -f \"");
  87. if (str[0] != '\0')
  88. lub_string_cat(&command, str);
  89. else
  90. lub_string_cat(&command, "/tmp/running-config");
  91. lub_string_cat(&command, "\"");
  92. lub_string_free(str);
  93. }
  94. break;
  95. default:
  96. return -1;
  97. };
  98. /* Add pattern */
  99. if ((CLISH_CONFIG_SET == op) || (CLISH_CONFIG_UNSET == op)) {
  100. tstr = clish_shell_expand(clish_config__get_pattern(config), SHELL_VAR_REGEX, clish_context);
  101. if (!tstr) {
  102. lub_string_free(command);
  103. return -1;
  104. }
  105. str = lub_string_encode(tstr, escape_chars);
  106. lub_string_free(tstr);
  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, clish_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, clish_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. tinyrl_printf(clish_shell__get_tinyrl(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 0;
  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. }