2004-08-14 17:54:20 +00:00
|
|
|
/*
|
|
|
|
* Dropbear - a SSH2 server
|
|
|
|
* SSH client implementation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002,2003 Matt Johnston
|
|
|
|
* Copyright (c) 2004 by Mihnea Stoenescu
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE. */
|
|
|
|
|
2004-07-27 16:30:46 +00:00
|
|
|
#include "includes.h"
|
|
|
|
#include "dbutil.h"
|
|
|
|
#include "runopts.h"
|
|
|
|
#include "session.h"
|
2013-11-14 14:05:47 +00:00
|
|
|
#include "dbrandom.h"
|
2013-05-03 15:07:48 +00:00
|
|
|
#include "crypto_desc.h"
|
2015-02-20 15:16:38 +00:00
|
|
|
#include "netio.h"
|
2004-07-26 02:44:20 +00:00
|
|
|
|
2011-04-07 12:59:18 +00:00
|
|
|
static void cli_dropbear_exit(int exitcode, const char* format, va_list param) ATTRIB_NORETURN;
|
2004-07-27 16:30:46 +00:00
|
|
|
static void cli_dropbear_log(int priority, const char* format, va_list param);
|
|
|
|
|
2009-06-08 14:53:29 +00:00
|
|
|
#ifdef ENABLE_CLI_PROXYCMD
|
2015-12-03 13:22:29 +00:00
|
|
|
static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out);
|
|
|
|
static void killchild(int signo);
|
2009-06-08 14:53:29 +00:00
|
|
|
#endif
|
2008-09-15 14:40:30 +00:00
|
|
|
|
2004-07-27 16:30:46 +00:00
|
|
|
#if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI)
|
|
|
|
#if defined(DBMULTI_dbclient) && defined(DROPBEAR_MULTI)
|
|
|
|
int cli_main(int argc, char ** argv) {
|
|
|
|
#else
|
2004-07-26 02:44:20 +00:00
|
|
|
int main(int argc, char ** argv) {
|
2004-07-27 16:30:46 +00:00
|
|
|
#endif
|
2004-07-26 02:44:20 +00:00
|
|
|
|
2008-09-15 12:51:50 +00:00
|
|
|
int sock_in, sock_out;
|
2015-02-18 16:32:00 +00:00
|
|
|
struct dropbear_progress_connection *progress = NULL;
|
2004-07-26 02:44:20 +00:00
|
|
|
|
|
|
|
_dropbear_exit = cli_dropbear_exit;
|
|
|
|
_dropbear_log = cli_dropbear_log;
|
|
|
|
|
2007-02-12 10:43:44 +00:00
|
|
|
disallow_core();
|
|
|
|
|
2013-05-03 15:07:48 +00:00
|
|
|
seedrandom();
|
|
|
|
crypto_init();
|
|
|
|
|
2004-07-26 02:44:20 +00:00
|
|
|
cli_getopts(argc, argv);
|
|
|
|
|
2015-12-01 18:55:34 +00:00
|
|
|
#ifndef DISABLE_SYSLOG
|
|
|
|
if (opts.usingsyslog) {
|
|
|
|
startsyslog("dbclient");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-07-27 16:30:46 +00:00
|
|
|
TRACE(("user='%s' host='%s' port='%s'", cli_opts.username,
|
2005-01-02 20:25:56 +00:00
|
|
|
cli_opts.remotehost, cli_opts.remoteport))
|
2004-07-27 16:30:46 +00:00
|
|
|
|
2004-08-27 14:39:01 +00:00
|
|
|
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
|
|
|
|
dropbear_exit("signal() error");
|
|
|
|
}
|
|
|
|
|
2015-12-03 13:22:29 +00:00
|
|
|
pid_t proxy_cmd_pid = 0;
|
2008-09-15 14:40:30 +00:00
|
|
|
#ifdef ENABLE_CLI_PROXYCMD
|
|
|
|
if (cli_opts.proxycmd) {
|
2015-12-03 13:22:29 +00:00
|
|
|
cli_proxy_cmd(&sock_in, &sock_out, &proxy_cmd_pid);
|
2009-06-12 14:58:43 +00:00
|
|
|
m_free(cli_opts.proxycmd);
|
2015-12-03 13:22:29 +00:00
|
|
|
if (signal(SIGINT, killchild) == SIG_ERR ||
|
|
|
|
signal(SIGTERM, killchild) == SIG_ERR ||
|
|
|
|
signal(SIGHUP, killchild) == SIG_ERR) {
|
|
|
|
dropbear_exit("signal() error");
|
|
|
|
}
|
2008-09-15 12:51:50 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2015-02-20 15:16:38 +00:00
|
|
|
progress = connect_remote(cli_opts.remotehost, cli_opts.remoteport, cli_connected, &ses);
|
2015-02-18 14:46:15 +00:00
|
|
|
sock_in = sock_out = -1;
|
2004-07-26 02:44:20 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 13:22:29 +00:00
|
|
|
cli_session(sock_in, sock_out, progress, proxy_cmd_pid);
|
2004-07-26 02:44:20 +00:00
|
|
|
|
|
|
|
/* not reached */
|
|
|
|
return -1;
|
|
|
|
}
|
2004-07-27 16:30:46 +00:00
|
|
|
#endif /* DBMULTI stuff */
|
|
|
|
|
|
|
|
static void cli_dropbear_exit(int exitcode, const char* format, va_list param) {
|
|
|
|
|
|
|
|
char fmtbuf[300];
|
2015-03-03 12:53:00 +00:00
|
|
|
char exitmsg[500];
|
2004-07-27 16:30:46 +00:00
|
|
|
|
|
|
|
if (!sessinitdone) {
|
2011-02-23 15:50:30 +00:00
|
|
|
snprintf(fmtbuf, sizeof(fmtbuf), "Exited: %s",
|
2004-07-27 16:30:46 +00:00
|
|
|
format);
|
|
|
|
} else {
|
|
|
|
snprintf(fmtbuf, sizeof(fmtbuf),
|
2011-02-23 15:50:30 +00:00
|
|
|
"Connection to %s@%s:%s exited: %s",
|
2004-07-27 16:30:46 +00:00
|
|
|
cli_opts.username, cli_opts.remotehost,
|
|
|
|
cli_opts.remoteport, format);
|
|
|
|
}
|
|
|
|
|
2015-03-03 12:53:00 +00:00
|
|
|
/* Arguments to the exit printout may be unsafe to use after session_cleanup() */
|
|
|
|
vsnprintf(exitmsg, sizeof(exitmsg), fmtbuf, param);
|
|
|
|
|
2004-08-01 08:54:01 +00:00
|
|
|
/* Do the cleanup first, since then the terminal will be reset */
|
2013-04-01 14:26:55 +00:00
|
|
|
session_cleanup();
|
2014-07-08 16:13:17 +00:00
|
|
|
/* Avoid printing onwards from terminal cruft */
|
|
|
|
fprintf(stderr, "\n");
|
2004-08-01 08:54:01 +00:00
|
|
|
|
2015-03-03 12:53:00 +00:00
|
|
|
dropbear_log(LOG_INFO, "%s", exitmsg);;
|
2004-07-27 16:30:46 +00:00
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
2015-12-01 18:55:34 +00:00
|
|
|
static void cli_dropbear_log(int priority,
|
2004-08-26 13:16:40 +00:00
|
|
|
const char* format, va_list param) {
|
2004-07-27 16:30:46 +00:00
|
|
|
|
|
|
|
char printbuf[1024];
|
|
|
|
|
|
|
|
vsnprintf(printbuf, sizeof(printbuf), format, param);
|
|
|
|
|
2015-12-01 18:55:34 +00:00
|
|
|
#ifndef DISABLE_SYSLOG
|
|
|
|
if (opts.usingsyslog) {
|
|
|
|
syslog(priority, "%s", printbuf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-08-01 08:54:01 +00:00
|
|
|
fprintf(stderr, "%s: %s\n", cli_opts.progname, printbuf);
|
2014-07-08 16:13:17 +00:00
|
|
|
fflush(stderr);
|
2004-07-27 16:30:46 +00:00
|
|
|
}
|
2008-09-15 14:40:30 +00:00
|
|
|
|
|
|
|
static void exec_proxy_cmd(void *user_data_cmd) {
|
|
|
|
const char *cmd = user_data_cmd;
|
|
|
|
char *usershell;
|
|
|
|
|
|
|
|
usershell = m_strdup(get_user_shell());
|
|
|
|
run_shell_command(cmd, ses.maxfd, usershell);
|
|
|
|
dropbear_exit("Failed to run '%s'\n", cmd);
|
|
|
|
}
|
|
|
|
|
2009-06-08 14:53:29 +00:00
|
|
|
#ifdef ENABLE_CLI_PROXYCMD
|
2015-12-03 13:22:29 +00:00
|
|
|
static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out) {
|
2008-09-15 14:40:30 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
fill_passwd(cli_opts.own_user);
|
|
|
|
|
|
|
|
ret = spawn_command(exec_proxy_cmd, cli_opts.proxycmd,
|
2015-12-03 13:22:29 +00:00
|
|
|
sock_out, sock_in, NULL, pid_out);
|
2008-09-15 14:40:30 +00:00
|
|
|
if (ret == DROPBEAR_FAILURE) {
|
|
|
|
dropbear_exit("Failed running proxy command");
|
|
|
|
*sock_in = *sock_out = -1;
|
|
|
|
}
|
|
|
|
}
|
2015-12-03 13:22:29 +00:00
|
|
|
|
2015-12-15 14:09:55 +00:00
|
|
|
static void killchild(int signo) {
|
2015-12-03 13:22:29 +00:00
|
|
|
kill_proxy_command();
|
2015-12-15 14:09:55 +00:00
|
|
|
if (signo) {
|
2015-12-03 13:22:29 +00:00
|
|
|
_exit(1);
|
2015-12-15 14:09:55 +00:00
|
|
|
}
|
2015-12-03 13:22:29 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2013-11-14 14:03:30 +00:00
|
|
|
#endif /* ENABLE_CLI_PROXYCMD */
|