2 * Dropbear - a SSH2 server
3 * SSH client implementation
5 * Copyright (c) 2002,2003 Matt Johnston
6 * Copyright (c) 2004 by Mihnea Stoenescu
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
32 #include "crypto_desc.h"
34 static void cli_dropbear_exit(int exitcode
, const char* format
, va_list param
) ATTRIB_NORETURN
;
35 static void cli_dropbear_log(int priority
, const char* format
, va_list param
);
37 #ifdef ENABLE_CLI_PROXYCMD
38 static void cli_proxy_cmd(int *sock_in
, int *sock_out
);
41 #if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI)
42 #if defined(DBMULTI_dbclient) && defined(DROPBEAR_MULTI)
43 int cli_main(int argc
, char ** argv
) {
45 int main(int argc
, char ** argv
) {
48 int sock_in
, sock_out
;
51 _dropbear_exit
= cli_dropbear_exit
;
52 _dropbear_log
= cli_dropbear_log
;
59 cli_getopts(argc
, argv
);
61 TRACE(("user='%s' host='%s' port='%s'", cli_opts
.username
,
62 cli_opts
.remotehost
, cli_opts
.remoteport
))
64 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
) {
65 dropbear_exit("signal() error");
68 #ifdef ENABLE_CLI_PROXYCMD
69 if (cli_opts
.proxycmd
) {
70 cli_proxy_cmd(&sock_in
, &sock_out
);
71 m_free(cli_opts
.proxycmd
);
75 int sock
= connect_remote(cli_opts
.remotehost
, cli_opts
.remoteport
,
77 sock_in
= sock_out
= sock
;
78 if (cli_opts
.wantpty
) {
79 set_sock_priority(sock
, DROPBEAR_PRIO_LOWDELAY
);
84 dropbear_exit("%s", error
);
87 cli_session(sock_in
, sock_out
);
92 #endif /* DBMULTI stuff */
94 static void cli_dropbear_exit(int exitcode
, const char* format
, va_list param
) {
99 snprintf(fmtbuf
, sizeof(fmtbuf
), "Exited: %s",
102 snprintf(fmtbuf
, sizeof(fmtbuf
),
103 "Connection to %s@%s:%s exited: %s",
104 cli_opts
.username
, cli_opts
.remotehost
,
105 cli_opts
.remoteport
, format
);
108 /* Do the cleanup first, since then the terminal will be reset */
111 _dropbear_log(LOG_INFO
, fmtbuf
, param
);
116 static void cli_dropbear_log(int UNUSED(priority
),
117 const char* format
, va_list param
) {
121 vsnprintf(printbuf
, sizeof(printbuf
), format
, param
);
123 fprintf(stderr
, "%s: %s\n", cli_opts
.progname
, printbuf
);
127 static void exec_proxy_cmd(void *user_data_cmd
) {
128 const char *cmd
= user_data_cmd
;
131 usershell
= m_strdup(get_user_shell());
132 run_shell_command(cmd
, ses
.maxfd
, usershell
);
133 dropbear_exit("Failed to run '%s'\n", cmd
);
136 #ifdef ENABLE_CLI_PROXYCMD
137 static void cli_proxy_cmd(int *sock_in
, int *sock_out
) {
140 fill_passwd(cli_opts
.own_user
);
142 ret
= spawn_command(exec_proxy_cmd
, cli_opts
.proxycmd
,
143 sock_out
, sock_in
, NULL
, NULL
);
144 if (ret
== DROPBEAR_FAILURE
) {
145 dropbear_exit("Failed running proxy command");
146 *sock_in
= *sock_out
= -1;
149 #endif /* ENABLE_CLI_PROXYCMD */