dropbear: update to 2015.67
[tomato.git] / release / src / router / dropbear / session.h
blobed0f5be16f2119c98a56a835b57a4778f1f7616a
1 /*
2 * Dropbear - a SSH2 server
3 *
4 * Copyright (c) 2002,2003 Matt Johnston
5 * All rights reserved.
6 *
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
23 * SOFTWARE. */
25 #ifndef _SESSION_H_
26 #define _SESSION_H_
28 #include "includes.h"
29 #include "options.h"
30 #include "buffer.h"
31 #include "signkey.h"
32 #include "kex.h"
33 #include "auth.h"
34 #include "channel.h"
35 #include "queue.h"
36 #include "listener.h"
37 #include "packet.h"
38 #include "tcpfwd.h"
39 #include "chansession.h"
40 #include "dbutil.h"
42 extern int sessinitdone; /* Is set to 0 somewhere */
43 extern int exitflag;
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();
50 void ignore_recv_response();
52 void update_channel_prio();
54 const char* get_user_shell();
55 void fill_passwd(const char* username);
57 /* Server */
58 void svr_session(int sock, int childpipe);
59 void svr_dropbear_exit(int exitcode, const char* format, va_list param) ATTRIB_NORETURN;
60 void svr_dropbear_log(int priority, const char* format, va_list param);
62 /* Client */
63 void cli_session(int sock_in, int sock_out);
64 void cleantext(unsigned char* dirtytext);
66 /* crypto parameters that are stored individually for transmit and receive */
67 struct key_context_directional {
68 const struct dropbear_cipher *algo_crypt;
69 const struct dropbear_cipher_mode *crypt_mode;
70 const struct dropbear_hash *algo_mac;
71 int hash_index; /* lookup for libtomcrypt */
72 int algo_comp; /* compression */
73 #ifndef DISABLE_ZLIB
74 z_streamp zstream;
75 #endif
76 /* actual keys */
77 union {
78 symmetric_CBC cbc;
79 #ifdef DROPBEAR_ENABLE_CTR_MODE
80 symmetric_CTR ctr;
81 #endif
82 } cipher_state;
83 unsigned char mackey[MAX_MAC_LEN];
84 int valid;
87 struct key_context {
89 struct key_context_directional recv;
90 struct key_context_directional trans;
92 const struct dropbear_kex *algo_kex;
93 int algo_hostkey;
95 int allow_compress; /* whether compression has started (useful in
96 zlib@openssh.com delayed compression case) */
99 struct packetlist;
100 struct packetlist {
101 struct packetlist *next;
102 buffer * payload;
105 struct sshsession {
107 /* Is it a client or server? */
108 unsigned char isserver;
110 int sock_in;
111 int sock_out;
113 /* remotehost will be initially NULL as we delay
114 * reading the remote version string. it will be set
115 * by the time any recv_() packet methods are called */
116 unsigned char *remoteident;
118 int maxfd; /* the maximum file descriptor to check with select() */
121 /* Packet buffers/values etc */
122 buffer *writepayload; /* Unencrypted payload to write - this is used
123 throughout the code, as handlers fill out this
124 buffer with the packet to send. */
125 struct Queue writequeue; /* A queue of encrypted packets to send */
126 buffer *readbuf; /* From the wire, decrypted in-place */
127 buffer *payload; /* Post-decompression, the actual SSH packet */
128 unsigned int transseq, recvseq; /* Sequence IDs */
130 /* Packet-handling flags */
131 const packettype * packettypes; /* Packet handler mappings for this
132 session, see process-packet.c */
134 unsigned dataallowed : 1; /* whether we can send data packets or we are in
135 the middle of a KEX or something */
137 unsigned char requirenext; /* byte indicating what packets we require next,
138 or 0x00 for any. */
140 unsigned char ignorenext; /* whether to ignore the next packet,
141 used for kex_follows stuff */
143 unsigned char lastpacket; /* What the last received packet type was */
145 int signal_pipe[2]; /* stores endpoints of a self-pipe used for
146 race-free signal handling */
148 /* time of the last packet send/receive, for keepalive. Not real-world clock */
149 time_t last_packet_time_keepalive_sent;
150 time_t last_packet_time_keepalive_recv;
151 time_t last_packet_time_any_sent;
153 time_t last_packet_time_idle; /* time of the last packet transmission or receive, for
154 idle timeout purposes so ignores SSH_MSG_IGNORE
155 or responses to keepalives. Not real-world clock */
158 /* KEX/encryption related */
159 struct KEXState kexstate;
160 struct key_context *keys;
161 struct key_context *newkeys;
162 buffer *session_id; /* this is the hash from the first kex */
163 /* The below are used temporarily during kex, are freed after use */
164 mp_int * dh_K; /* SSH_MSG_KEXDH_REPLY and sending SSH_MSH_NEWKEYS */
165 buffer *hash; /* the session hash */
166 buffer* kexhashbuf; /* session hash buffer calculated from various packets*/
167 buffer* transkexinit; /* the kexinit packet we send should be kept so we
168 can add it to the hash when generating keys */
170 /* Enables/disables compression */
171 algo_type *compress_algos;
173 /* a list of queued replies that should be sent after a KEX has
174 concluded (ie, while dataallowed was unset)*/
175 struct packetlist *reply_queue_head, *reply_queue_tail;
177 void(*remoteclosed)(); /* A callback to handle closure of the
178 remote connection */
180 void(*extra_session_cleanup)(); /* client or server specific cleanup */
181 void(*send_kex_first_guess)();
183 struct AuthState authstate; /* Common amongst client and server, since most
184 struct elements are common */
186 /* Channel related */
187 struct Channel ** channels; /* these pointers may be null */
188 unsigned int chansize; /* the number of Channel*s allocated for channels */
189 unsigned int chancount; /* the number of Channel*s in use */
190 const struct ChanType **chantypes; /* The valid channel types */
191 int channel_signal_pending; /* Flag set by sigchld handler */
193 /* TCP priority level for the main "port 22" tcp socket */
194 enum dropbear_prio socket_prio;
196 /* TCP forwarding - where manage listeners */
197 struct Listener ** listeners;
198 unsigned int listensize;
200 /* Whether to allow binding to privileged ports (<1024). This doesn't
201 * really belong here, but nowhere else fits nicely */
202 int allowprivport;
206 struct serversession {
208 /* Server specific options */
209 int childpipe; /* kept open until we successfully authenticate */
210 /* userauth */
212 struct ChildPid * childpids; /* array of mappings childpid<->channel */
213 unsigned int childpidsize;
215 /* Used to avoid a race in the exit returncode handling - see
216 * svr-chansession.c for details */
217 struct exitinfo lastexit;
219 /* The numeric address they connected from, used for logging */
220 char * addrstring;
222 /* The resolved remote address, used for lastlog etc */
223 char *remotehost;
225 time_t connect_time; /* time the connection was established
226 (cleared after auth once we're not
227 respecting AUTH_TIMEOUT any more).
228 A monotonic time, not realworld */
230 #ifdef USE_VFORK
231 pid_t server_pid;
232 #endif
236 typedef enum {
237 KEX_NOTHING,
238 KEXINIT_RCVD,
239 KEXDH_INIT_SENT,
240 KEXDONE
241 } cli_kex_state;
243 typedef enum {
244 STATE_NOTHING,
245 USERAUTH_WAIT,
246 USERAUTH_REQ_SENT,
247 USERAUTH_FAIL_RCVD,
248 USERAUTH_SUCCESS_RCVD,
249 SESSION_RUNNING
250 } cli_state;
252 struct clientsession {
254 /* XXX - move these to kexstate? */
255 struct kex_dh_param *dh_param;
256 struct kex_ecdh_param *ecdh_param;
257 struct kex_curve25519_param *curve25519_param;
258 const struct dropbear_kex *param_kex_algo; /* KEX algorithm corresponding to current dh_e and dh_x */
260 cli_kex_state kex_state; /* Used for progressing KEX */
261 cli_state state; /* Used to progress auth/channelsession etc */
262 unsigned donefirstkex : 1; /* Set when we set sentnewkeys, never reset */
264 int tty_raw_mode; /* Whether we're in raw mode (and have to clean up) */
265 struct termios saved_tio;
266 int stdincopy;
267 int stdinflags;
268 int stdoutcopy;
269 int stdoutflags;
270 int stderrcopy;
271 int stderrflags;
273 /* for escape char handling */
274 int last_char;
276 int winchange; /* Set to 1 when a windowchange signal happens */
278 int lastauthtype; /* either AUTH_TYPE_PUBKEY or AUTH_TYPE_PASSWORD,
279 for the last type of auth we tried */
280 int ignore_next_auth_response;
281 #ifdef ENABLE_CLI_INTERACT_AUTH
282 int auth_interact_failed; /* flag whether interactive auth can still
283 be used */
284 int interact_request_received; /* flag whether we've received an
285 info request from the server for
286 interactive auth.*/
288 int cipher_none_after_auth; /* Set to 1 if the user requested "none"
289 auth */
290 #endif
291 sign_key *lastprivkey;
293 int retval; /* What the command exit status was - we emulate it */
294 #if 0
295 TODO
296 struct AgentkeyList *agentkeys; /* Keys to use for public-key auth */
297 #endif
301 /* Global structs storing the state */
302 extern struct sshsession ses;
304 #ifdef DROPBEAR_SERVER
305 extern struct serversession svr_ses;
306 #endif /* DROPBEAR_SERVER */
308 #ifdef DROPBEAR_CLIENT
309 extern struct clientsession cli_ses;
310 #endif /* DROPBEAR_CLIENT */
312 #endif /* _SESSION_H_ */