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
39 #include "chansession.h"
42 extern int sessinitdone
; /* Is set to 0 somewhere */
45 void common_session_init(int sock_in
, int sock_out
);
46 void session_loop(void(*loophandler
)());
47 void session_cleanup();
48 void send_session_identification();
49 void send_msg_ignore();
51 const char* get_user_shell();
52 void fill_passwd(const char* username
);
55 void svr_session(int sock
, int childpipe
);
56 void svr_dropbear_exit(int exitcode
, const char* format
, va_list param
) ATTRIB_NORETURN
;
57 void svr_dropbear_log(int priority
, const char* format
, va_list param
);
60 void cli_session(int sock_in
, int sock_out
);
61 void cleantext(unsigned char* dirtytext
);
63 /* crypto parameters that are stored individually for transmit and receive */
64 struct key_context_directional
{
65 const struct dropbear_cipher
*algo_crypt
;
66 const struct dropbear_cipher_mode
*crypt_mode
;
67 const struct dropbear_hash
*algo_mac
;
68 int hash_index
; /* lookup for libtomcrypt */
69 char algo_comp
; /* compression */
76 #ifdef DROPBEAR_ENABLE_CTR_MODE
80 unsigned char mackey
[MAX_MAC_LEN
];
86 struct key_context_directional recv
;
87 struct key_context_directional trans
;
92 int allow_compress
; /* whether compression has started (useful in
93 zlib@openssh.com delayed compression case) */
98 struct packetlist
*next
;
104 /* Is it a client or server? */
105 unsigned char isserver
;
107 time_t connect_time
; /* time the connection was established
108 (cleared after auth once we're not
109 respecting AUTH_TIMEOUT any more) */
114 /* remotehost will be initially NULL as we delay
115 * reading the remote version string. it will be set
116 * by the time any recv_() packet methods are called */
117 unsigned char *remoteident
;
119 int maxfd
; /* the maximum file descriptor to check with select() */
122 /* Packet buffers/values etc */
123 buffer
*writepayload
; /* Unencrypted payload to write - this is used
124 throughout the code, as handlers fill out this
125 buffer with the packet to send. */
126 struct Queue writequeue
; /* A queue of encrypted packets to send */
127 buffer
*readbuf
; /* From the wire, decrypted in-place */
128 buffer
*payload
; /* Post-decompression, the actual SSH packet */
129 unsigned int transseq
, recvseq
; /* Sequence IDs */
131 /* Packet-handling flags */
132 const packettype
* packettypes
; /* Packet handler mappings for this
133 session, see process-packet.c */
135 unsigned dataallowed
: 1; /* whether we can send data packets or we are in
136 the middle of a KEX or something */
138 unsigned char requirenext
[2]; /* bytes indicating what packets we require next,
139 or 0x00 for any. Second option can only be
140 used if the first byte is also set */
142 unsigned char ignorenext
; /* whether to ignore the next packet,
143 used for kex_follows stuff */
145 unsigned char lastpacket
; /* What the last received packet type was */
147 int signal_pipe
[2]; /* stores endpoints of a self-pipe used for
148 race-free signal handling */
150 time_t last_trx_packet_time
; /* time of the last packet transmission, for
151 keepalive purposes */
153 time_t last_packet_time
; /* time of the last packet transmission or receive, for
154 idle timeout purposes */
157 /* KEX/encryption related */
158 struct KEXState kexstate
;
159 struct key_context
*keys
;
160 struct key_context
*newkeys
;
161 unsigned char *session_id
; /* this is the hash from the first kex */
162 /* The below are used temorarily during kex, are freed after use */
163 mp_int
* dh_K
; /* SSH_MSG_KEXDH_REPLY and sending SSH_MSH_NEWKEYS */
164 unsigned char hash
[SHA1_HASH_SIZE
]; /* the hash*/
165 buffer
* kexhashbuf
; /* session hash buffer calculated from various packets*/
166 buffer
* transkexinit
; /* the kexinit packet we send should be kept so we
167 can add it to the hash when generating keys */
169 /* Enables/disables compression */
170 algo_type
*compress_algos
;
172 /* a list of queued replies that should be sent after a KEX has
173 concluded (ie, while dataallowed was unset)*/
174 struct packetlist
*reply_queue_head
, *reply_queue_tail
;
176 void(*remoteclosed
)(); /* A callback to handle closure of the
179 void(*extra_session_cleanup
)(); /* client or server specific cleanup */
180 void(*send_kex_first_guess
)();
182 struct AuthState authstate
; /* Common amongst client and server, since most
183 struct elements are common */
185 /* Channel related */
186 struct Channel
** channels
; /* these pointers may be null */
187 unsigned int chansize
; /* the number of Channel*s allocated for channels */
188 unsigned int chancount
; /* the number of Channel*s in use */
189 const struct ChanType
**chantypes
; /* The valid channel types */
192 /* TCP forwarding - where manage listeners */
193 struct Listener
** listeners
;
194 unsigned int listensize
;
196 /* Whether to allow binding to privileged ports (<1024). This doesn't
197 * really belong here, but nowhere else fits nicely */
202 struct serversession
{
204 /* Server specific options */
205 int childpipe
; /* kept open until we successfully authenticate */
208 struct ChildPid
* childpids
; /* array of mappings childpid<->channel */
209 unsigned int childpidsize
;
211 /* Used to avoid a race in the exit returncode handling - see
212 * svr-chansession.c for details */
213 struct exitinfo lastexit
;
215 /* The numeric address they connected from, used for logging */
218 /* The resolved remote address, used for lastlog etc */
238 USERAUTH_SUCCESS_RCVD
,
242 struct clientsession
{
244 mp_int
*dh_e
, *dh_x
; /* Used during KEX */
245 int dh_val_algo
; /* KEX algorithm corresponding to current dh_e and dh_x */
246 cli_kex_state kex_state
; /* Used for progressing KEX */
247 cli_state state
; /* Used to progress auth/channelsession etc */
248 unsigned donefirstkex
: 1; /* Set when we set sentnewkeys, never reset */
250 int tty_raw_mode
; /* Whether we're in raw mode (and have to clean up) */
251 struct termios saved_tio
;
259 /* for escape char handling */
262 int winchange
; /* Set to 1 when a windowchange signal happens */
264 int lastauthtype
; /* either AUTH_TYPE_PUBKEY or AUTH_TYPE_PASSWORD,
265 for the last type of auth we tried */
266 #ifdef ENABLE_CLI_INTERACT_AUTH
267 int auth_interact_failed
; /* flag whether interactive auth can still
269 int interact_request_received
; /* flag whether we've received an
270 info request from the server for
273 int cipher_none_after_auth
; /* Set to 1 if the user requested "none"
276 sign_key
*lastprivkey
;
278 int retval
; /* What the command exit status was - we emulate it */
281 struct AgentkeyList
*agentkeys
; /* Keys to use for public-key auth */
286 /* Global structs storing the state */
287 extern struct sshsession ses
;
289 #ifdef DROPBEAR_SERVER
290 extern struct serversession svr_ses
;
291 #endif /* DROPBEAR_SERVER */
293 #ifdef DROPBEAR_CLIENT
294 extern struct clientsession cli_ses
;
295 #endif /* DROPBEAR_CLIENT */
297 #endif /* _SESSION_H_ */