klish.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <getopt.h>
  12. #include <sys/socket.h>
  13. #include <sys/un.h>
  14. #include <syslog.h>
  15. #ifdef HAVE_LOCALE_H
  16. #include <locale.h>
  17. #endif
  18. #ifdef HAVE_LANGINFO_CODESET
  19. #include <langinfo.h>
  20. #endif
  21. #include <faux/faux.h>
  22. #include <faux/str.h>
  23. #include <faux/msg.h>
  24. #include <faux/list.h>
  25. #include <faux/file.h>
  26. #include <faux/eloop.h>
  27. #include <klish/ktp.h>
  28. #include <klish/ktp_session.h>
  29. #include "private.h"
  30. typedef enum {
  31. MODE_CMDLINE,
  32. MODE_FILES,
  33. MODE_STDIN,
  34. MODE_INTERACTIVE
  35. } client_mode_e;
  36. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  37. void *user_data);
  38. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  39. void *user_data);
  40. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  41. void *associated_data, void *user_data);
  42. static int ktp_sync_cmd(ktp_session_t *ktp, const char *line,
  43. const struct options *opts);
  44. int main(int argc, char **argv)
  45. {
  46. int retval = -1;
  47. struct options *opts = NULL;
  48. int unix_sock = -1;
  49. ktp_session_t *ktp = NULL;
  50. int retcode = 0;
  51. faux_eloop_t *eloop = NULL;
  52. int auth_rc = -1;
  53. client_mode_e mode = MODE_INTERACTIVE;
  54. #ifdef HAVE_LOCALE_H
  55. // Set current locale
  56. setlocale(LC_ALL, "");
  57. #endif
  58. // Parse command line options
  59. opts = opts_init();
  60. if (opts_parse(argc, argv, opts)) {
  61. fprintf(stderr, "Error: Can't parse command line options\n");
  62. goto err;
  63. }
  64. // Parse config file
  65. if (!access(opts->cfgfile, R_OK)) {
  66. if (!config_parse(opts->cfgfile, opts))
  67. goto err;
  68. } else if (opts->cfgfile_userdefined) {
  69. // User defined config must be found
  70. fprintf(stderr, "Error: Can't find config file %s\n",
  71. opts->cfgfile);
  72. goto err;
  73. }
  74. // Find out client mode
  75. if (faux_list_len(opts->commands) > 0)
  76. mode = MODE_CMDLINE;
  77. else if (faux_list_len(opts->files) > 0)
  78. mode = MODE_FILES;
  79. else if (!isatty(STDIN_FILENO))
  80. mode = MODE_STDIN;
  81. // Connect to server
  82. unix_sock = ktp_connect_unix(opts->unix_socket_path);
  83. if (unix_sock < 0) {
  84. fprintf(stderr, "Error: Can't connect to server\n");
  85. goto err;
  86. }
  87. // Eloop object
  88. eloop = faux_eloop_new(NULL);
  89. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  90. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  91. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  92. // KTP session
  93. ktp = ktp_session_new(unix_sock, eloop);
  94. assert(ktp);
  95. if (!ktp) {
  96. fprintf(stderr, "Error: Can't create klish session\n");
  97. goto err;
  98. }
  99. // Ignore SIGPIPE from server
  100. signal(SIGPIPE, SIG_IGN);
  101. // Auth for non-interactive modes. Interactive mode does auth itself
  102. if (mode != MODE_INTERACTIVE) {
  103. if (!ktp_sync_auth(ktp, &auth_rc) || (auth_rc < 0)) {
  104. fprintf(stderr, "Error: Can't auth\n");
  105. goto err;
  106. }
  107. }
  108. // These callback functions is only for batch mode. Interactive
  109. // mode can reassign them.
  110. ktp_session_set_cb(ktp, KTP_SESSION_CB_STDOUT, stdout_cb, NULL);
  111. ktp_session_set_cb(ktp, KTP_SESSION_CB_STDERR, stderr_cb, NULL);
  112. // Commands from cmdline
  113. if (mode == MODE_CMDLINE) {
  114. const char *line = NULL;
  115. faux_list_node_t *iter = faux_list_head(opts->commands);
  116. while ((line = faux_list_each(&iter))) {
  117. // Request to server
  118. retcode = ktp_sync_cmd(ktp, line, opts);
  119. // Stop-on-error
  120. if (opts->stop_on_error && (retcode != 0))
  121. break;
  122. }
  123. // Commands from files
  124. } else if (mode == MODE_FILES) {
  125. const char *filename = NULL;
  126. faux_list_node_t *iter = faux_list_head(opts->files);
  127. while ((filename = (const char *)faux_list_each(&iter))) {
  128. char *line = NULL;
  129. bool_t stop = BOOL_FALSE;
  130. faux_file_t *fd = faux_file_open(filename, O_RDONLY, 0);
  131. while ((line = faux_file_getline(fd))) {
  132. // Request to server
  133. retcode = ktp_sync_cmd(ktp, line, opts);
  134. faux_str_free(line);
  135. // Stop-on-error
  136. if (opts->stop_on_error && (retcode != 0)) {
  137. stop = BOOL_TRUE;
  138. break;
  139. }
  140. }
  141. faux_file_close(fd);
  142. if (stop)
  143. break;
  144. }
  145. // Commands from non-interactive STDIN
  146. } else if (mode == MODE_STDIN) {
  147. char *line = NULL;
  148. faux_file_t *fd = faux_file_fdopen(STDIN_FILENO);
  149. while ((line = faux_file_getline(fd))) {
  150. // Request to server
  151. retcode = ktp_sync_cmd(ktp, line, opts);
  152. faux_str_free(line);
  153. // Stop-on-error
  154. if (opts->stop_on_error && (retcode != 0))
  155. break;
  156. }
  157. faux_file_close(fd);
  158. // Interactive shell
  159. } else {
  160. // Interactive code is complex so move it to separate file
  161. retcode = klish_interactive_shell(ktp, opts);
  162. }
  163. retval = 0;
  164. err:
  165. ktp_session_free(ktp);
  166. faux_eloop_free(eloop);
  167. ktp_disconnect(unix_sock);
  168. opts_free(opts);
  169. if ((retval < 0) || (retcode != 0))
  170. return -1;
  171. return 0;
  172. }
  173. static int ktp_sync_cmd(ktp_session_t *ktp, const char *line,
  174. const struct options *opts)
  175. {
  176. faux_error_t *error = NULL;
  177. int retcode = -1;
  178. if (faux_str_is_empty(line))
  179. return 0;
  180. // Echo command
  181. if (!opts->quiet)
  182. fprintf(stderr, "%s\n", line);
  183. error = faux_error_new();
  184. if (!ktp_session_cmd(ktp, line, error, opts->dry_run)) {
  185. faux_error_free(error);
  186. return -1;
  187. }
  188. faux_eloop_loop(ktp_session_eloop(ktp));
  189. // If retcode is not available then variable will not be changed
  190. ktp_session_retcode(ktp, &retcode);
  191. if (faux_error_len(error) > 0) {
  192. fprintf(stderr, "Error:\n");
  193. faux_error_fshow(error, stderr);
  194. }
  195. faux_error_free(error);
  196. return retcode;
  197. }
  198. bool_t ktp_sync_auth(ktp_session_t *ktp, int *retcode)
  199. {
  200. faux_error_t *error = NULL;
  201. bool_t rc = BOOL_FALSE;
  202. assert(ktp);
  203. error = faux_error_new();
  204. if (!ktp_session_auth(ktp, error)) {
  205. faux_error_free(error);
  206. return BOOL_FALSE;
  207. }
  208. faux_eloop_loop(ktp_session_eloop(ktp));
  209. rc = ktp_session_retcode(ktp, retcode);
  210. if (faux_error_len(error) > 0) {
  211. fprintf(stderr, "Auth error:\n");
  212. faux_error_fshow(error, stderr);
  213. }
  214. faux_error_free(error);
  215. return rc;
  216. }
  217. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  218. void *user_data)
  219. {
  220. if (faux_write_block(STDOUT_FILENO, line, len) < 0)
  221. return BOOL_FALSE;
  222. ktp = ktp;
  223. user_data = user_data;
  224. return BOOL_TRUE;
  225. }
  226. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  227. void *user_data)
  228. {
  229. if (faux_write_block(STDERR_FILENO, line, len) < 0)
  230. return BOOL_FALSE;
  231. ktp = ktp;
  232. user_data = user_data;
  233. return BOOL_TRUE;
  234. }
  235. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  236. void *associated_data, void *user_data)
  237. {
  238. // Happy compiler
  239. eloop = eloop;
  240. type = type;
  241. associated_data = associated_data;
  242. user_data = user_data;
  243. return BOOL_FALSE; // Stop Event Loop
  244. }