svr-session.c 6.86 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
27
28
29
30
31
32
/*
 * Dropbear - a SSH2 server
 * 
 * Copyright (c) 2002,2003 Matt Johnston
 * 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. */

#include "includes.h"
#include "session.h"
#include "dbutil.h"
#include "packet.h"
#include "algo.h"
#include "buffer.h"
#include "dss.h"
#include "ssh.h"
33
#include "dbrandom.h"
34
35
36
37
#include "kex.h"
#include "channel.h"
#include "chansession.h"
#include "atomicio.h"
Matt Johnston's avatar
Matt Johnston committed
38
#include "tcpfwd.h"
39
40
#include "service.h"
#include "auth.h"
Matt Johnston's avatar
Matt Johnston committed
41
#include "runopts.h"
Matt Johnston's avatar
Matt Johnston committed
42
#include "crypto_desc.h"
43

Francois Perrad's avatar
Francois Perrad committed
44
static void svr_remoteclosed(void);
45

Matt Johnston's avatar
Matt Johnston committed
46
struct serversession svr_ses; /* GLOBAL */
47

48
49
50
static const packettype svr_packettypes[] = {
	{SSH_MSG_CHANNEL_DATA, recv_msg_channel_data},
	{SSH_MSG_CHANNEL_WINDOW_ADJUST, recv_msg_channel_window_adjust},
51
52
53
54
55
	{SSH_MSG_USERAUTH_REQUEST, recv_msg_userauth_request}, /* server */
	{SSH_MSG_SERVICE_REQUEST, recv_msg_service_request}, /* server */
	{SSH_MSG_KEXINIT, recv_msg_kexinit},
	{SSH_MSG_KEXDH_INIT, recv_msg_kexdh_init}, /* server */
	{SSH_MSG_NEWKEYS, recv_msg_newkeys},
56
57
58
59
60
	{SSH_MSG_GLOBAL_REQUEST, recv_msg_global_request_remotetcp},
	{SSH_MSG_CHANNEL_REQUEST, recv_msg_channel_request},
	{SSH_MSG_CHANNEL_OPEN, recv_msg_channel_open},
	{SSH_MSG_CHANNEL_EOF, recv_msg_channel_eof},
	{SSH_MSG_CHANNEL_CLOSE, recv_msg_channel_close},
61
62
63
64
	{SSH_MSG_CHANNEL_SUCCESS, ignore_recv_response},
	{SSH_MSG_CHANNEL_FAILURE, ignore_recv_response},
	{SSH_MSG_REQUEST_FAILURE, ignore_recv_response}, /* for keepalive */
	{SSH_MSG_REQUEST_SUCCESS, ignore_recv_response}, /* client */
65
#if DROPBEAR_LISTENERS
66
67
	{SSH_MSG_CHANNEL_OPEN_CONFIRMATION, recv_msg_channel_open_confirmation},
	{SSH_MSG_CHANNEL_OPEN_FAILURE, recv_msg_channel_open_failure},
68
#endif
69
70
71
72
	{0, 0} /* End */
};

static const struct ChanType *svr_chantypes[] = {
73
	&svrchansess,
74
#if DROPBEAR_SVR_LOCALTCPFWD
Matt Johnston's avatar
Matt Johnston committed
75
	&svr_chan_tcpdirect,
76
#endif
77
78
79
	NULL /* Null termination is mandatory. */
};

80
static void
81
svr_session_cleanup(void) {
82
83
	/* free potential public key options */
	svr_pubkey_options_cleanup();
84
85
86

	m_free(svr_ses.addrstring);
	m_free(svr_ses.remotehost);
87
88
	m_free(svr_ses.childpids);
	svr_ses.childpidsize = 0;
89
90
}

Matt Johnston's avatar
Matt Johnston committed
91
92
93
void svr_session(int sock, int childpipe) {
	char *host, *port;
	size_t len;
94

Matt Johnston's avatar
Matt Johnston committed
95
	common_session_init(sock, sock);
96
97
98

	/* Initialise server specific parts of the session */
	svr_ses.childpipe = childpipe;
99
#if DROPBEAR_VFORK
100
101
	svr_ses.server_pid = getpid();
#endif
Matt Johnston's avatar
Matt Johnston committed
102
	svr_authinitialise();
103
	chaninitialise(svr_chantypes);
104
105
	svr_chansessinitialise();

Matt Johnston's avatar
Matt Johnston committed
106
107
108
109
110
111
112
113
	/* for logging the remote address */
	get_socket_address(ses.sock_in, NULL, NULL, &host, &port, 0);
	len = strlen(host) + strlen(port) + 2;
	svr_ses.addrstring = m_malloc(len);
	snprintf(svr_ses.addrstring, len, "%s:%s", host, port);
	m_free(host);
	m_free(port);

114
115
116
	get_socket_address(ses.sock_in, NULL, NULL, 
			&svr_ses.remotehost, NULL, 1);

117
	/* set up messages etc */
Matt Johnston's avatar
Matt Johnston committed
118
	ses.remoteclosed = svr_remoteclosed;
119
	ses.extra_session_cleanup = svr_session_cleanup;
120

121
122
123
	/* packet handlers */
	ses.packettypes = svr_packettypes;

Matt Johnston's avatar
Matt Johnston committed
124
125
	ses.isserver = 1;

126
127
128
129
	/* We're ready to go now */
	sessinitdone = 1;

	/* exchange identification, version etc */
130
	send_session_identification();
131
132
	
	kexfirstinitialise(); /* initialise the kex state */
133
134
135
136

	/* start off with key exchange */
	send_msg_kexinit();

Matt Johnston's avatar
Matt Johnston committed
137
138
	/* Run the main for loop. NULL is for the dispatcher - only the client
	 * code makes use of it */
139
	session_loop(NULL);
Matt Johnston's avatar
Matt Johnston committed
140
141
142

	/* Not reached */

143
144
145
}

/* failure exit - format must be <= 100 chars */
146
void svr_dropbear_exit(int exitcode, const char* format, va_list param) {
147
148
	char exitmsg[150];
	char fullmsg[300];
149
	int i;
150

151
152
153
154
	/* Render the formatted exit message */
	vsnprintf(exitmsg, sizeof(exitmsg), format, param);

	/* Add the prefix depending on session/auth state */
155
156
	if (!sessinitdone) {
		/* before session init */
157
		snprintf(fullmsg, sizeof(fullmsg), "Early exit: %s", exitmsg);
Matt Johnston's avatar
Matt Johnston committed
158
	} else if (ses.authstate.authdone) {
159
		/* user has authenticated */
160
		snprintf(fullmsg, sizeof(fullmsg),
161
				"Exit (%s): %s", 
162
				ses.authstate.pw_name, exitmsg);
163
	} else if (ses.authstate.pw_name) {
164
		/* we have a potential user */
165
		snprintf(fullmsg, sizeof(fullmsg), 
Chocobo1's avatar
Chocobo1 committed
166
				"Exit before auth (user '%s', %u fails): %s",
167
				ses.authstate.pw_name, ses.authstate.failcount, exitmsg);
168
169
	} else {
		/* before userauth */
170
		snprintf(fullmsg, sizeof(fullmsg), "Exit before auth: %s", exitmsg);
171
172
	}

173
	dropbear_log(LOG_INFO, "%s", fullmsg);
174

175
#if DROPBEAR_VFORK
Matt Johnston's avatar
Matt Johnston committed
176
	/* For uclinux only the main server process should cleanup - we don't want
177
178
179
180
181
	 * forked children doing that */
	if (svr_ses.server_pid == getpid())
#endif
	{
		/* must be after we've done with username etc */
182
		session_cleanup();
183
	}
184

185
186
187
188
189
190
191
192
193
	if (svr_opts.hostkey) {
		sign_key_free(svr_opts.hostkey);
		svr_opts.hostkey = NULL;
	}
	for (i = 0; i < DROPBEAR_MAX_PORTS; i++) {
		m_free(svr_opts.addresses[i]);
		m_free(svr_opts.ports[i]);
	}

194
195
196
197
198
	exit(exitcode);

}

/* priority is priority as with syslog() */
199
void svr_dropbear_log(int priority, const char* format, va_list param) {
200
201
202
203
204
205
206
207
208

	char printbuf[1024];
	char datestr[20];
	time_t timesec;
	int havetrace = 0;

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

#ifndef DISABLE_SYSLOG
209
	if (opts.usingsyslog) {
210
211
212
213
214
215
		syslog(priority, "%s", printbuf);
	}
#endif

	/* if we are using DEBUG_TRACE, we want to print to stderr even if
	 * syslog is used, so it is included in error reports */
216
#if DEBUG_TRACE
217
	havetrace = debug_trace;
218
219
#endif

Matt Johnston's avatar
Matt Johnston committed
220
	if (!opts.usingsyslog || havetrace) {
221
		struct tm * local_tm = NULL;
222
		timesec = time(NULL);
223
224
225
		local_tm = localtime(&timesec);
		if (local_tm == NULL
			|| strftime(datestr, sizeof(datestr), "%b %d %H:%M:%S", 
226
						local_tm) == 0)
227
		{
228
			/* upon failure, just print the epoch-seconds time. */
229
			snprintf(datestr, sizeof(datestr), "%d", (int)timesec);
230
231
232
233
		}
		fprintf(stderr, "[%d] %s %s\n", getpid(), datestr, printbuf);
	}
}
234
235
236
237

/* called when the remote side closes the connection */
static void svr_remoteclosed() {

238
239
240
241
	m_close(ses.sock_in);
	m_close(ses.sock_out);
	ses.sock_in = -1;
	ses.sock_out = -1;
242
243
244
245
	dropbear_close("Exited normally");

}