cli-main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #include "fuzz.h"
  34. #if DROPBEAR_CLI_PROXYCMD
  35. static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out);
  36. static void kill_proxy_sighandler(int signo);
  37. #endif
  38. #if defined(DBMULTI_dbclient) || !DROPBEAR_MULTI
  39. #if defined(DBMULTI_dbclient) && DROPBEAR_MULTI
  40. int cli_main(int argc, char ** argv) {
  41. #else
  42. int main(int argc, char ** argv) {
  43. #endif
  44. int sock_in, sock_out;
  45. struct dropbear_progress_connection *progress = NULL;
  46. pid_t proxy_cmd_pid = 0;
  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. if (cli_opts.bind_address) {
  59. DEBUG1(("connect to: user=%s host=%s/%s bind_address=%s:%s", cli_opts.username,
  60. cli_opts.remotehost, cli_opts.remoteport, cli_opts.bind_address, cli_opts.bind_port))
  61. } else {
  62. DEBUG1(("connect to: user=%s host=%s/%s",cli_opts.username,cli_opts.remotehost,cli_opts.remoteport))
  63. }
  64. if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
  65. dropbear_exit("signal() error");
  66. }
  67. #if DROPBEAR_CLI_PROXYCMD
  68. if (cli_opts.proxycmd) {
  69. cli_proxy_cmd(&sock_in, &sock_out, &proxy_cmd_pid);
  70. m_free(cli_opts.proxycmd);
  71. if (signal(SIGINT, kill_proxy_sighandler) == SIG_ERR ||
  72. signal(SIGTERM, kill_proxy_sighandler) == SIG_ERR ||
  73. signal(SIGHUP, kill_proxy_sighandler) == SIG_ERR) {
  74. dropbear_exit("signal() error");
  75. }
  76. } else
  77. #endif
  78. {
  79. progress = connect_remote(cli_opts.remotehost, cli_opts.remoteport,
  80. cli_connected, &ses, cli_opts.bind_address, cli_opts.bind_port,
  81. DROPBEAR_PRIO_LOWDELAY);
  82. sock_in = sock_out = -1;
  83. }
  84. cli_session(sock_in, sock_out, progress, proxy_cmd_pid);
  85. /* not reached */
  86. return -1;
  87. }
  88. #endif /* DBMULTI stuff */
  89. static void exec_proxy_cmd(const void *user_data_cmd) {
  90. const char *cmd = user_data_cmd;
  91. char *usershell;
  92. usershell = m_strdup(get_user_shell());
  93. run_shell_command(cmd, ses.maxfd, usershell);
  94. dropbear_exit("Failed to run '%s'\n", cmd);
  95. }
  96. #if DROPBEAR_CLI_PROXYCMD
  97. static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out) {
  98. char * ex_cmd = NULL;
  99. size_t ex_cmdlen;
  100. int ret;
  101. /* File descriptor "-j &3" */
  102. if (*cli_opts.proxycmd == '&') {
  103. char *p = cli_opts.proxycmd + 1;
  104. int sock = strtoul(p, &p, 10);
  105. /* must be a single number, and not stdin/stdout/stderr */
  106. if (sock > 2 && sock < 1024 && *p == '\0') {
  107. *sock_in = sock;
  108. *sock_out = sock;
  109. return;
  110. }
  111. }
  112. /* Normal proxycommand */
  113. /* So that spawn_command knows which shell to run */
  114. fill_passwd(cli_opts.own_user);
  115. ex_cmdlen = strlen(cli_opts.proxycmd) + 6; /* "exec " + command + '\0' */
  116. ex_cmd = m_malloc(ex_cmdlen);
  117. snprintf(ex_cmd, ex_cmdlen, "exec %s", cli_opts.proxycmd);
  118. ret = spawn_command(exec_proxy_cmd, ex_cmd,
  119. sock_out, sock_in, NULL, pid_out);
  120. DEBUG1(("cmd: %s pid=%d", ex_cmd,*pid_out))
  121. m_free(ex_cmd);
  122. if (ret == DROPBEAR_FAILURE) {
  123. dropbear_exit("Failed running proxy command");
  124. *sock_in = *sock_out = -1;
  125. }
  126. }
  127. static void kill_proxy_sighandler(int UNUSED(signo)) {
  128. kill_proxy_command();
  129. _exit(1);
  130. }
  131. #endif /* DROPBEAR_CLI_PROXYCMD */