cli-main.c 4.94 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * 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. */

Matt Johnston's avatar
Matt Johnston committed
27
28
29
30
#include "includes.h"
#include "dbutil.h"
#include "runopts.h"
#include "session.h"
31
#include "dbrandom.h"
Matt Johnston's avatar
Matt Johnston committed
32
#include "crypto_desc.h"
33
#include "netio.h"
Matt Johnston's avatar
Matt Johnston committed
34

35
static void cli_dropbear_exit(int exitcode, const char* format, va_list param) ATTRIB_NORETURN;
Matt Johnston's avatar
Matt Johnston committed
36
37
static void cli_dropbear_log(int priority, const char* format, va_list param);

38
#if DROPBEAR_CLI_PROXYCMD
39
static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out);
40
static void kill_proxy_sighandler(int signo);
41
#endif
42

43
44
#if defined(DBMULTI_dbclient) || !DROPBEAR_MULTI
#if defined(DBMULTI_dbclient) && DROPBEAR_MULTI
Matt Johnston's avatar
Matt Johnston committed
45
46
int cli_main(int argc, char ** argv) {
#else
Matt Johnston's avatar
Matt Johnston committed
47
int main(int argc, char ** argv) {
Matt Johnston's avatar
Matt Johnston committed
48
#endif
Matt Johnston's avatar
Matt Johnston committed
49

50
	int sock_in, sock_out;
51
	struct dropbear_progress_connection *progress = NULL;
Matt Johnston's avatar
Matt Johnston committed
52
53
54
55

	_dropbear_exit = cli_dropbear_exit;
	_dropbear_log = cli_dropbear_log;

Matt Johnston's avatar
Matt Johnston committed
56
57
	disallow_core();

Matt Johnston's avatar
Matt Johnston committed
58
59
60
	seedrandom();
	crypto_init();

Matt Johnston's avatar
Matt Johnston committed
61
62
	cli_getopts(argc, argv);

63
64
65
66
67
68
#ifndef DISABLE_SYSLOG
	if (opts.usingsyslog) {
		startsyslog("dbclient");
	}
#endif

Matt Johnston's avatar
Matt Johnston committed
69
	TRACE(("user='%s' host='%s' port='%s'", cli_opts.username,
70
				cli_opts.remotehost, cli_opts.remoteport))
Matt Johnston's avatar
Matt Johnston committed
71

72
73
74
75
	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
		dropbear_exit("signal() error");
	}

76
	pid_t proxy_cmd_pid = 0;
77
#if DROPBEAR_CLI_PROXYCMD
78
	if (cli_opts.proxycmd) {
79
		cli_proxy_cmd(&sock_in, &sock_out, &proxy_cmd_pid);
80
		m_free(cli_opts.proxycmd);
81
82
83
		if (signal(SIGINT, kill_proxy_sighandler) == SIG_ERR ||
			signal(SIGTERM, kill_proxy_sighandler) == SIG_ERR ||
			signal(SIGHUP, kill_proxy_sighandler) == SIG_ERR) {
84
85
			dropbear_exit("signal() error");
		}
86
87
88
	} else
#endif
	{
89
		progress = connect_remote(cli_opts.remotehost, cli_opts.remoteport, cli_connected, &ses);
Matt Johnston's avatar
Matt Johnston committed
90
		sock_in = sock_out = -1;
Matt Johnston's avatar
Matt Johnston committed
91
92
	}

93
	cli_session(sock_in, sock_out, progress, proxy_cmd_pid);
Matt Johnston's avatar
Matt Johnston committed
94
95
96
97

	/* not reached */
	return -1;
}
Matt Johnston's avatar
Matt Johnston committed
98
99
100
101
102
#endif /* DBMULTI stuff */

static void cli_dropbear_exit(int exitcode, const char* format, va_list param) {

	char fmtbuf[300];
103
	char exitmsg[500];
Matt Johnston's avatar
Matt Johnston committed
104
105

	if (!sessinitdone) {
106
		snprintf(fmtbuf, sizeof(fmtbuf), "Exited: %s",
Matt Johnston's avatar
Matt Johnston committed
107
108
109
				format);
	} else {
		snprintf(fmtbuf, sizeof(fmtbuf), 
110
				"Connection to %[email protected]%s:%s exited: %s", 
Matt Johnston's avatar
Matt Johnston committed
111
112
113
114
				cli_opts.username, cli_opts.remotehost, 
				cli_opts.remoteport, format);
	}

115
116
117
	/* Arguments to the exit printout may be unsafe to use after session_cleanup() */
	vsnprintf(exitmsg, sizeof(exitmsg), fmtbuf, param);

118
	/* Do the cleanup first, since then the terminal will be reset */
119
	session_cleanup();
120
121
	/* Avoid printing onwards from terminal cruft */
	fprintf(stderr, "\n");
122

123
	dropbear_log(LOG_INFO, "%s", exitmsg);;
Matt Johnston's avatar
Matt Johnston committed
124
125
126
	exit(exitcode);
}

127
static void cli_dropbear_log(int priority,
128
		const char* format, va_list param) {
Matt Johnston's avatar
Matt Johnston committed
129
130
131
132
133

	char printbuf[1024];

	vsnprintf(printbuf, sizeof(printbuf), format, param);

134
135
136
137
138
139
#ifndef DISABLE_SYSLOG
	if (opts.usingsyslog) {
		syslog(priority, "%s", printbuf);
	}
#endif

140
	fprintf(stderr, "%s: %s\n", cli_opts.progname, printbuf);
141
	fflush(stderr);
Matt Johnston's avatar
Matt Johnston committed
142
}
143
144
145
146
147
148
149
150
151
152

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);
}

153
#if DROPBEAR_CLI_PROXYCMD
154
static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out) {
Matt Johnston's avatar
Matt Johnston committed
155
156
	char * ex_cmd = NULL;
	size_t ex_cmdlen;
157
158
159
160
	int ret;

	fill_passwd(cli_opts.own_user);

Matt Johnston's avatar
Matt Johnston committed
161
162
163
164
165
	ex_cmdlen = strlen(cli_opts.proxycmd) + 6; /* "exec " + command + '\0' */
	ex_cmd = m_malloc(ex_cmdlen);
	snprintf(ex_cmd, ex_cmdlen, "exec %s", cli_opts.proxycmd);

	ret = spawn_command(exec_proxy_cmd, ex_cmd,
166
			sock_out, sock_in, NULL, pid_out);
Matt Johnston's avatar
Matt Johnston committed
167
	m_free(ex_cmd);
168
169
170
171
172
	if (ret == DROPBEAR_FAILURE) {
		dropbear_exit("Failed running proxy command");
		*sock_in = *sock_out = -1;
	}
}
173

174
static void kill_proxy_sighandler(int UNUSED(signo)) {
175
	kill_proxy_command();
176
	_exit(1);
177
}
178
#endif /* DROPBEAR_CLI_PROXYCMD */