dropbear: update to 2013.62
[tomato.git] / release / src / router / dropbear / cli-main.c
blob3db8f2ff4d0ece3ff8ae01f8ef3538aadb9a7081
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:
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
25 * SOFTWARE. */
27 #include "includes.h"
28 #include "dbutil.h"
29 #include "runopts.h"
30 #include "session.h"
31 #include "dbrandom.h"
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);
39 #endif
41 #if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI)
42 #if defined(DBMULTI_dbclient) && defined(DROPBEAR_MULTI)
43 int cli_main(int argc, char ** argv) {
44 #else
45 int main(int argc, char ** argv) {
46 #endif
48 int sock_in, sock_out;
49 char* error = NULL;
51 _dropbear_exit = cli_dropbear_exit;
52 _dropbear_log = cli_dropbear_log;
54 disallow_core();
56 seedrandom();
57 crypto_init();
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);
72 } else
73 #endif
75 int sock = connect_remote(cli_opts.remotehost, cli_opts.remoteport,
76 0, &error);
77 sock_in = sock_out = sock;
78 if (cli_opts.wantpty) {
79 set_sock_priority(sock, DROPBEAR_PRIO_LOWDELAY);
83 if (sock_in < 0) {
84 dropbear_exit("%s", error);
87 cli_session(sock_in, sock_out);
89 /* not reached */
90 return -1;
92 #endif /* DBMULTI stuff */
94 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) {
96 char fmtbuf[300];
98 if (!sessinitdone) {
99 snprintf(fmtbuf, sizeof(fmtbuf), "Exited: %s",
100 format);
101 } else {
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 */
109 session_cleanup();
111 _dropbear_log(LOG_INFO, fmtbuf, param);
113 exit(exitcode);
116 static void cli_dropbear_log(int UNUSED(priority),
117 const char* format, va_list param) {
119 char printbuf[1024];
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;
129 char *usershell;
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) {
138 int ret;
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 */