callback_config.c 4.8 KB

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