2
* Dropbear - a SSH2 server
4
* Copyright (c) 2002,2003 Matt Johnston
7
* Permission is hereby granted, free of charge, to any person obtaining a copy
8
* of this software and associated documentation files (the "Software"), to deal
9
* in the Software without restriction, including without limitation the rights
10
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
* copies of the Software, and to permit persons to whom the Software is
12
* furnished to do so, subject to the following conditions:
14
* The above copyright notice and this permission notice shall be included in
15
* all copies or substantial portions of the Software.
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36
#include "chansession.h"
43
static void svr_remoteclosed();
45
struct serversession svr_ses; /* GLOBAL */
47
static const packettype svr_packettypes[] = {
48
{SSH_MSG_CHANNEL_DATA, recv_msg_channel_data},
49
{SSH_MSG_CHANNEL_WINDOW_ADJUST, recv_msg_channel_window_adjust},
50
{SSH_MSG_USERAUTH_REQUEST, recv_msg_userauth_request}, /* server */
51
{SSH_MSG_SERVICE_REQUEST, recv_msg_service_request}, /* server */
52
{SSH_MSG_KEXINIT, recv_msg_kexinit},
53
{SSH_MSG_KEXDH_INIT, recv_msg_kexdh_init}, /* server */
54
{SSH_MSG_NEWKEYS, recv_msg_newkeys},
55
#ifdef ENABLE_SVR_REMOTETCPFWD
56
{SSH_MSG_GLOBAL_REQUEST, recv_msg_global_request_remotetcp},
58
{SSH_MSG_CHANNEL_REQUEST, recv_msg_channel_request},
59
{SSH_MSG_CHANNEL_OPEN, recv_msg_channel_open},
60
{SSH_MSG_CHANNEL_EOF, recv_msg_channel_eof},
61
{SSH_MSG_CHANNEL_CLOSE, recv_msg_channel_close},
62
#ifdef USING_LISTENERS
63
{SSH_MSG_CHANNEL_OPEN_CONFIRMATION, recv_msg_channel_open_confirmation},
64
{SSH_MSG_CHANNEL_OPEN_FAILURE, recv_msg_channel_open_failure},
69
static const struct ChanType *svr_chantypes[] = {
71
#ifdef ENABLE_SVR_LOCALTCPFWD
74
NULL /* Null termination is mandatory. */
77
void svr_session(int sock, int childpipe,
78
char* remotehost, char *addrstring) {
80
struct timeval timeout;
83
common_session_init(sock, remotehost);
85
/* Initialise server specific parts of the session */
86
svr_ses.childpipe = childpipe;
87
svr_ses.addrstring = addrstring;
89
chaninitialise(svr_chantypes);
90
svr_chansessinitialise();
92
if (gettimeofday(&timeout, 0) < 0) {
93
dropbear_exit("Error getting time");
96
ses.connecttimeout = timeout.tv_sec + AUTH_TIMEOUT;
98
/* set up messages etc */
99
ses.remoteclosed = svr_remoteclosed;
101
/* packet handlers */
102
ses.packettypes = svr_packettypes;
103
ses.buf_match_algo = svr_buf_match_algo;
107
/* We're ready to go now */
110
/* exchange identification, version etc */
111
session_identification();
115
/* start off with key exchange */
118
/* Run the main for loop. NULL is for the dispatcher - only the client
119
* code makes use of it */
126
/* failure exit - format must be <= 100 chars */
127
void svr_dropbear_exit(int exitcode, const char* format, va_list param) {
132
/* before session init */
133
snprintf(fmtbuf, sizeof(fmtbuf),
134
"premature exit: %s", format);
135
} else if (ses.authstate.authdone) {
136
/* user has authenticated */
137
snprintf(fmtbuf, sizeof(fmtbuf),
138
"exit after auth (%s): %s",
139
ses.authstate.printableuser, format);
140
} else if (ses.authstate.printableuser) {
141
/* we have a potential user */
142
snprintf(fmtbuf, sizeof(fmtbuf),
143
"exit before auth (user '%s', %d fails): %s",
144
ses.authstate.printableuser, ses.authstate.failcount, format);
146
/* before userauth */
147
snprintf(fmtbuf, sizeof(fmtbuf),
148
"exit before auth: %s", format);
151
_dropbear_log(LOG_INFO, fmtbuf, param);
153
/* must be after we've done with username etc */
154
common_session_cleanup();
160
/* priority is priority as with syslog() */
161
void svr_dropbear_log(int priority, const char* format, va_list param) {
168
vsnprintf(printbuf, sizeof(printbuf), format, param);
170
#ifndef DISABLE_SYSLOG
171
if (svr_opts.usingsyslog) {
172
syslog(priority, "%s", printbuf);
176
/* if we are using DEBUG_TRACE, we want to print to stderr even if
177
* syslog is used, so it is included in error reports */
179
havetrace = debug_trace;
182
if (!svr_opts.usingsyslog || havetrace)
184
timesec = time(NULL);
185
if (strftime(datestr, sizeof(datestr), "%b %d %H:%M:%S",
186
localtime(×ec)) == 0) {
187
datestr[0] = '?'; datestr[1] = '\0';
189
fprintf(stderr, "[%d] %s %s\n", getpid(), datestr, printbuf);
193
/* called when the remote side closes the connection */
194
static void svr_remoteclosed() {
198
dropbear_close("Exited normally");