MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / dropbear / cli-main.c
blob273f59c03b2bd56e15f3126e1bd467f1d08b0ea6
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"
32 static void cli_dropbear_exit(int exitcode, const char* format, va_list param);
33 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);
37 #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
46 int sock_in, sock_out;
47 char* error = NULL;
49 _dropbear_exit = cli_dropbear_exit;
50 _dropbear_log = cli_dropbear_log;
52 disallow_core();
54 cli_getopts(argc, argv);
56 TRACE(("user='%s' host='%s' port='%s'", cli_opts.username,
57 cli_opts.remotehost, cli_opts.remoteport))
59 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
60 dropbear_exit("signal() error");
63 #ifdef ENABLE_CLI_PROXYCMD
64 if (cli_opts.proxycmd) {
65 cli_proxy_cmd(&sock_in, &sock_out);
66 m_free(cli_opts.proxycmd);
67 } else
68 #endif
70 int sock = connect_remote(cli_opts.remotehost, cli_opts.remoteport,
71 0, &error);
72 sock_in = sock_out = sock;
75 if (sock_in < 0) {
76 dropbear_exit("%s", error);
79 cli_session(sock_in, sock_out);
81 /* not reached */
82 return -1;
84 #endif /* DBMULTI stuff */
86 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) {
88 char fmtbuf[300];
90 if (!sessinitdone) {
91 snprintf(fmtbuf, sizeof(fmtbuf), "Exited: %s",
92 format);
93 } else {
94 snprintf(fmtbuf, sizeof(fmtbuf),
95 "Connection to %s@%s:%s exited: %s",
96 cli_opts.username, cli_opts.remotehost,
97 cli_opts.remoteport, format);
100 /* Do the cleanup first, since then the terminal will be reset */
101 cli_session_cleanup();
102 common_session_cleanup();
104 _dropbear_log(LOG_INFO, fmtbuf, param);
106 exit(exitcode);
109 static void cli_dropbear_log(int UNUSED(priority),
110 const char* format, va_list param) {
112 char printbuf[1024];
114 vsnprintf(printbuf, sizeof(printbuf), format, param);
116 fprintf(stderr, "%s: %s\n", cli_opts.progname, printbuf);
120 static void exec_proxy_cmd(void *user_data_cmd) {
121 const char *cmd = user_data_cmd;
122 char *usershell;
124 usershell = m_strdup(get_user_shell());
125 run_shell_command(cmd, ses.maxfd, usershell);
126 dropbear_exit("Failed to run '%s'\n", cmd);
129 #ifdef ENABLE_CLI_PROXYCMD
130 static void cli_proxy_cmd(int *sock_in, int *sock_out) {
131 int ret;
133 fill_passwd(cli_opts.own_user);
135 ret = spawn_command(exec_proxy_cmd, cli_opts.proxycmd,
136 sock_out, sock_in, NULL, NULL);
137 if (ret == DROPBEAR_FAILURE) {
138 dropbear_exit("Failed running proxy command");
139 *sock_in = *sock_out = -1;
142 #endif // ENABLE_CLI_PROXYCMD