remove unused variable
[dropbear.git] / session.h
blob355cf03bdc7aa0d575bab080a3ba11f995773372
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"
41 extern int sessinitdone; /* Is set to 0 somewhere */
42 extern int exitflag;
44 void common_session_init(int sock_in, int sock_out);
45 void session_loop(void(*loophandler)());
46 void common_session_cleanup();
47 void session_identification();
48 void send_msg_ignore();
50 const char* get_user_shell();
51 void fill_passwd(const char* username);
53 /* Server */
54 void svr_session(int sock, int childpipe);
55 void svr_dropbear_exit(int exitcode, const char* format, va_list param);
56 void svr_dropbear_log(int priority, const char* format, va_list param);
58 /* Client */
59 void cli_session(int sock_in, int sock_out);
60 void cli_session_cleanup();
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; /* NULL for none */
66 const struct dropbear_cipher_mode *crypt_mode;
67 const struct dropbear_hash *algo_mac; /* NULL for none */
68 int hash_index; /* lookup for libtomcrypt */
69 char algo_comp; /* compression */
70 #ifndef DISABLE_ZLIB
71 z_streamp zstream;
72 #endif
73 /* actual keys */
74 union {
75 symmetric_CBC cbc;
76 #ifdef DROPBEAR_ENABLE_CTR_MODE
77 symmetric_CTR ctr;
78 #endif
79 } cipher_state;
80 unsigned char mackey[MAX_MAC_KEY];
83 struct key_context {
85 struct key_context_directional recv;
86 struct key_context_directional trans;
88 char algo_kex;
89 char algo_hostkey;
91 int allow_compress; /* whether compression has started (useful in
92 zlib@openssh.com delayed compression case) */
95 struct packetlist;
96 struct packetlist {
97 struct packetlist *next;
98 buffer * payload;
101 struct sshsession {
103 /* Is it a client or server? */
104 unsigned char isserver;
106 time_t connect_time; /* time the connection was established
107 (cleared after auth once we're not
108 respecting AUTH_TIMEOUT any more) */
110 int sock_in;
111 int sock_out;
113 unsigned char *remoteident;
115 int maxfd; /* the maximum file descriptor to check with select() */
118 /* Packet buffers/values etc */
119 buffer *writepayload; /* Unencrypted payload to write - this is used
120 throughout the code, as handlers fill out this
121 buffer with the packet to send. */
122 struct Queue writequeue; /* A queue of encrypted packets to send */
123 buffer *readbuf; /* From the wire, decrypted in-place */
124 buffer *payload; /* Post-decompression, the actual SSH packet */
125 unsigned int transseq, recvseq; /* Sequence IDs */
127 /* Packet-handling flags */
128 const packettype * packettypes; /* Packet handler mappings for this
129 session, see process-packet.c */
131 unsigned dataallowed : 1; /* whether we can send data packets or we are in
132 the middle of a KEX or something */
134 unsigned char requirenext; /* byte indicating what packet we require next,
135 or 0x00 for any */
137 unsigned char ignorenext; /* whether to ignore the next packet,
138 used for kex_follows stuff */
140 unsigned char lastpacket; /* What the last received packet type was */
142 int signal_pipe[2]; /* stores endpoints of a self-pipe used for
143 race-free signal handling */
145 time_t last_trx_packet_time; /* time of the last packet transmission, for
146 keepalive purposes */
148 time_t last_packet_time; /* time of the last packet transmission or receive, for
149 idle timeout purposes */
152 /* KEX/encryption related */
153 struct KEXState kexstate;
154 struct key_context *keys;
155 struct key_context *newkeys;
156 unsigned char *session_id; /* this is the hash from the first kex */
157 /* The below are used temorarily during kex, are freed after use */
158 mp_int * dh_K; /* SSH_MSG_KEXDH_REPLY and sending SSH_MSH_NEWKEYS */
159 unsigned char hash[SHA1_HASH_SIZE]; /* the hash*/
160 buffer* kexhashbuf; /* session hash buffer calculated from various packets*/
161 buffer* transkexinit; /* the kexinit packet we send should be kept so we
162 can add it to the hash when generating keys */
164 /* Enables/disables compression */
165 algo_type *compress_algos;
167 /* a list of queued replies that should be sent after a KEX has
168 concluded (ie, while dataallowed was unset)*/
169 struct packetlist *reply_queue_head, *reply_queue_tail;
171 algo_type*(*buf_match_algo)(buffer*buf, algo_type localalgos[],
172 int *goodguess); /* The function to use to choose which algorithm
173 to use from the ones presented by the remote
174 side. Is specific to the client/server mode,
175 hence the function-pointer callback.*/
177 void(*remoteclosed)(); /* A callback to handle closure of the
178 remote connection */
181 struct AuthState authstate; /* Common amongst client and server, since most
182 struct elements are common */
184 /* Channel related */
185 struct Channel ** channels; /* these pointers may be null */
186 unsigned int chansize; /* the number of Channel*s allocated for channels */
187 unsigned int chancount; /* the number of Channel*s in use */
188 const struct ChanType **chantypes; /* The valid channel types */
191 /* TCP forwarding - where manage listeners */
192 struct Listener ** listeners;
193 unsigned int listensize;
195 /* Whether to allow binding to privileged ports (<1024). This doesn't
196 * really belong here, but nowhere else fits nicely */
197 int allowprivport;
201 struct serversession {
203 /* Server specific options */
204 int childpipe; /* kept open until we successfully authenticate */
205 /* userauth */
207 struct ChildPid * childpids; /* array of mappings childpid<->channel */
208 unsigned int childpidsize;
210 /* Used to avoid a race in the exit returncode handling - see
211 * svr-chansession.c for details */
212 struct exitinfo lastexit;
214 /* The numeric address they connected from, used for logging */
215 char * addrstring;
217 /* The resolved remote address, used for lastlog etc */
218 char *remotehost;
220 #ifdef __uClinux__
221 pid_t server_pid;
222 #endif
226 typedef enum {
227 KEX_NOTHING,
228 KEXINIT_RCVD,
229 KEXDH_INIT_SENT,
230 KEXDONE
231 } cli_kex_state;
233 typedef enum {
234 STATE_NOTHING,
235 SERVICE_AUTH_REQ_SENT,
236 SERVICE_AUTH_ACCEPT_RCVD,
237 SERVICE_CONN_REQ_SENT,
238 SERVICE_CONN_ACCEPT_RCVD,
239 USERAUTH_REQ_SENT,
240 USERAUTH_FAIL_RCVD,
241 USERAUTH_SUCCESS_RCVD,
242 SESSION_RUNNING
243 } cli_state;
245 struct clientsession {
247 mp_int *dh_e, *dh_x; /* Used during KEX */
248 cli_kex_state kex_state; /* Used for progressing KEX */
249 cli_state state; /* Used to progress auth/channelsession etc */
250 unsigned donefirstkex : 1; /* Set when we set sentnewkeys, never reset */
252 int tty_raw_mode; /* Whether we're in raw mode (and have to clean up) */
253 struct termios saved_tio;
254 int stdincopy;
255 int stdinflags;
256 int stdoutcopy;
257 int stdoutflags;
258 int stderrcopy;
259 int stderrflags;
261 int winchange; /* Set to 1 when a windowchange signal happens */
263 int lastauthtype; /* either AUTH_TYPE_PUBKEY or AUTH_TYPE_PASSWORD,
264 for the last type of auth we tried */
265 #ifdef ENABLE_CLI_INTERACT_AUTH
266 int auth_interact_failed; /* flag whether interactive auth can still
267 be used */
268 int interact_request_received; /* flag whether we've received an
269 info request from the server for
270 interactive auth.*/
271 #endif
272 sign_key *lastprivkey;
274 int retval; /* What the command exit status was - we emulate it */
275 #if 0
276 TODO
277 struct AgentkeyList *agentkeys; /* Keys to use for public-key auth */
278 #endif
282 /* Global structs storing the state */
283 extern struct sshsession ses;
285 #ifdef DROPBEAR_SERVER
286 extern struct serversession svr_ses;
287 #endif /* DROPBEAR_SERVER */
289 #ifdef DROPBEAR_CLIENT
290 extern struct clientsession cli_ses;
291 #endif /* DROPBEAR_CLIENT */
293 #endif /* _SESSION_H_ */