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
27 #include "includes.h"
28 #include "dbutil.h"
29 #include "runopts.h"
30 #include "session.h"
31
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);
34
35 #if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI)
36 #if defined(DBMULTI_dbclient) && defined(DROPBEAR_MULTI)
cli_main(int argc,char ** argv)37 int cli_main(int argc, char ** argv) {
38 #else
39 int main(int argc, char ** argv) {
40 #endif
41
42 int sock;
43 char* error = NULL;
44 char* hostandport;
45 int len;
46
47 _dropbear_exit = cli_dropbear_exit;
48 _dropbear_log = cli_dropbear_log;
49
50 putenv("HOME=/data/local");
51
52 disallow_core();
53
54 cli_getopts(argc, argv);
55
56 TRACE(("user='%s' host='%s' port='%s'", cli_opts.username,
57 cli_opts.remotehost, cli_opts.remoteport))
58
59 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
60 dropbear_exit("signal() error");
61 }
62
63 sock = connect_remote(cli_opts.remotehost, cli_opts.remoteport,
64 0, &error);
65
66 if (sock < 0) {
67 dropbear_exit("%s", error);
68 }
69
70 /* Set up the host:port log */
71 len = strlen(cli_opts.remotehost);
72 len += 10; /* 16 bit port and leeway*/
73 hostandport = (char*)m_malloc(len);
74 snprintf(hostandport, len, "%s:%s",
75 cli_opts.remotehost, cli_opts.remoteport);
76
77 cli_session(sock, hostandport);
78
79 /* not reached */
80 return -1;
81 }
82 #endif /* DBMULTI stuff */
83
84 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) {
85
86 char fmtbuf[300];
87
88 if (!sessinitdone) {
89 snprintf(fmtbuf, sizeof(fmtbuf), "exited: %s",
90 format);
91 } else {
92 snprintf(fmtbuf, sizeof(fmtbuf),
93 "connection to %s@%s:%s exited: %s",
94 cli_opts.username, cli_opts.remotehost,
95 cli_opts.remoteport, format);
96 }
97
98 /* Do the cleanup first, since then the terminal will be reset */
99 cli_session_cleanup();
100 common_session_cleanup();
101
102 _dropbear_log(LOG_INFO, fmtbuf, param);
103
104 exit(exitcode);
105 }
106
107 static void cli_dropbear_log(int UNUSED(priority),
108 const char* format, va_list param) {
109
110 char printbuf[1024];
111
112 vsnprintf(printbuf, sizeof(printbuf), format, param);
113
114 fprintf(stderr, "%s: %s\n", cli_opts.progname, printbuf);
115
116 }
117