cli-main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Dropbear - a SSH2 server
  3. * SSH client implementation
  4. *
  5. * Copyright (c) 2002,2003 Matt Johnston
  6. * Copyright (c) 2004 by Mihnea Stoenescu
  7. * All rights reserved.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE. */
  26. #include "includes.h"
  27. #include "dbutil.h"
  28. #include "runopts.h"
  29. #include "session.h"
  30. #include "dbrandom.h"
  31. #include "crypto_desc.h"
  32. #include "netio.h"
  33. static void cli_dropbear_exit(int exitcode, const char* format, va_list param) ATTRIB_NORETURN;
  34. static void cli_dropbear_log(int priority, const char* format, va_list param);
  35. #ifdef ENABLE_CLI_PROXYCMD
  36. static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out);
  37. static void kill_proxy_sighandler(int signo);
  38. #endif
  39. #if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI)
  40. #if defined(DBMULTI_dbclient) && defined(DROPBEAR_MULTI)
  41. int cli_main(int argc, char ** argv) {
  42. #else
  43. int main(int argc, char ** argv) {
  44. #endif
  45. int sock_in, sock_out;
  46. struct dropbear_progress_connection *progress = NULL;
  47. _dropbear_exit = cli_dropbear_exit;
  48. _dropbear_log = cli_dropbear_log;
  49. disallow_core();
  50. seedrandom();
  51. crypto_init();
  52. cli_getopts(argc, argv);
  53. #ifndef DISABLE_SYSLOG
  54. if (opts.usingsyslog) {
  55. startsyslog("dbclient");
  56. }
  57. #endif
  58. TRACE(("user='%s' host='%s' port='%s'", cli_opts.username,
  59. cli_opts.remotehost, cli_opts.remoteport))
  60. if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
  61. dropbear_exit("signal() error");
  62. }
  63. pid_t proxy_cmd_pid = 0;
  64. #ifdef ENABLE_CLI_PROXYCMD
  65. if (cli_opts.proxycmd) {
  66. cli_proxy_cmd(&sock_in, &sock_out, &proxy_cmd_pid);
  67. m_free(cli_opts.proxycmd);
  68. if (signal(SIGINT, kill_proxy_sighandler) == SIG_ERR ||
  69. signal(SIGTERM, kill_proxy_sighandler) == SIG_ERR ||
  70. signal(SIGHUP, kill_proxy_sighandler) == SIG_ERR) {
  71. dropbear_exit("signal() error");
  72. }
  73. } else
  74. #endif
  75. {
  76. progress = connect_remote(cli_opts.remotehost, cli_opts.remoteport, cli_connected, &ses);
  77. sock_in = sock_out = -1;
  78. }
  79. cli_session(sock_in, sock_out, progress, proxy_cmd_pid);
  80. /* not reached */
  81. return -1;
  82. }
  83. #endif /* DBMULTI stuff */
  84. static void cli_dropbear_exit(int exitcode, const char* format, va_list param) {
  85. char exitmsg[150];
  86. char fullmsg[300];
  87. /* Note that exit message must be rendered before session cleanup */
  88. /* Render the formatted exit message */
  89. vsnprintf(exitmsg, sizeof(exitmsg), format, param);
  90. /* Add the prefix depending on session/auth state */
  91. if (!sessinitdone) {
  92. snprintf(fullmsg, sizeof(fullmsg), "Exited: %s", exitmsg);
  93. } else {
  94. snprintf(fullmsg, sizeof(fullmsg),
  95. "Connection to %s@%s:%s exited: %s",
  96. cli_opts.username, cli_opts.remotehost,
  97. cli_opts.remoteport, exitmsg);
  98. }
  99. /* Do the cleanup first, since then the terminal will be reset */
  100. session_cleanup();
  101. /* Avoid printing onwards from terminal cruft */
  102. fprintf(stderr, "\n");
  103. dropbear_log(LOG_INFO, "%s", fullmsg);
  104. exit(exitcode);
  105. }
  106. static void cli_dropbear_log(int priority,
  107. const char* format, va_list param) {
  108. char printbuf[1024];
  109. vsnprintf(printbuf, sizeof(printbuf), format, param);
  110. #ifndef DISABLE_SYSLOG
  111. if (opts.usingsyslog) {
  112. syslog(priority, "%s", printbuf);
  113. }
  114. #endif
  115. fprintf(stderr, "%s: %s\n", cli_opts.progname, printbuf);
  116. fflush(stderr);
  117. }
  118. static void exec_proxy_cmd(void *user_data_cmd) {
  119. const char *cmd = user_data_cmd;
  120. char *usershell;
  121. usershell = m_strdup(get_user_shell());
  122. run_shell_command(cmd, ses.maxfd, usershell);
  123. dropbear_exit("Failed to run '%s'\n", cmd);
  124. }
  125. #ifdef ENABLE_CLI_PROXYCMD
  126. static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out) {
  127. char * ex_cmd = NULL;
  128. size_t ex_cmdlen;
  129. int ret;
  130. fill_passwd(cli_opts.own_user);
  131. ex_cmdlen = strlen(cli_opts.proxycmd) + 6; /* "exec " + command + '\0' */
  132. ex_cmd = m_malloc(ex_cmdlen);
  133. snprintf(ex_cmd, ex_cmdlen, "exec %s", cli_opts.proxycmd);
  134. ret = spawn_command(exec_proxy_cmd, ex_cmd,
  135. sock_out, sock_in, NULL, pid_out);
  136. m_free(ex_cmd);
  137. if (ret == DROPBEAR_FAILURE) {
  138. dropbear_exit("Failed running proxy command");
  139. *sock_in = *sock_out = -1;
  140. }
  141. }
  142. static void kill_proxy_sighandler(int UNUSED(signo)) {
  143. kill_proxy_command();
  144. _exit(1);
  145. }
  146. #endif /* ENABLE_CLI_PROXYCMD */