session.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  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. */
  24. #ifndef DROPBEAR_SESSION_H_
  25. #define DROPBEAR_SESSION_H_
  26. #include "includes.h"
  27. #include "options.h"
  28. #include "buffer.h"
  29. #include "signkey.h"
  30. #include "kex.h"
  31. #include "auth.h"
  32. #include "channel.h"
  33. #include "queue.h"
  34. #include "listener.h"
  35. #include "packet.h"
  36. #include "tcpfwd.h"
  37. #include "chansession.h"
  38. #include "dbutil.h"
  39. #include "netio.h"
  40. extern int sessinitdone; /* Is set to 0 somewhere */
  41. extern int exitflag;
  42. void common_session_init(int sock_in, int sock_out);
  43. void session_loop(void(*loophandler)()) ATTRIB_NORETURN;
  44. void session_cleanup(void);
  45. void send_session_identification(void);
  46. void send_msg_ignore(void);
  47. void ignore_recv_response(void);
  48. void update_channel_prio(void);
  49. const char* get_user_shell(void);
  50. void fill_passwd(const char* username);
  51. /* Server */
  52. void svr_session(int sock, int childpipe) ATTRIB_NORETURN;
  53. void svr_dropbear_exit(int exitcode, const char* format, va_list param) ATTRIB_NORETURN;
  54. void svr_dropbear_log(int priority, const char* format, va_list param);
  55. /* Client */
  56. void cli_session(int sock_in, int sock_out, struct dropbear_progress_connection *progress, pid_t proxy_cmd_pid) ATTRIB_NORETURN;
  57. void cli_connected(int result, int sock, void* userdata, const char *errstring);
  58. void cleantext(char* dirtytext);
  59. void kill_proxy_command(void);
  60. /* crypto parameters that are stored individually for transmit and receive */
  61. struct key_context_directional {
  62. const struct dropbear_cipher *algo_crypt;
  63. const struct dropbear_cipher_mode *crypt_mode;
  64. const struct dropbear_hash *algo_mac;
  65. int hash_index; /* lookup for libtomcrypt */
  66. int algo_comp; /* compression */
  67. #ifndef DISABLE_ZLIB
  68. z_streamp zstream;
  69. #endif
  70. /* actual keys */
  71. union {
  72. symmetric_CBC cbc;
  73. #ifdef DROPBEAR_ENABLE_CTR_MODE
  74. symmetric_CTR ctr;
  75. #endif
  76. } cipher_state;
  77. unsigned char mackey[MAX_MAC_LEN];
  78. int valid;
  79. };
  80. struct key_context {
  81. struct key_context_directional recv;
  82. struct key_context_directional trans;
  83. const struct dropbear_kex *algo_kex;
  84. int algo_hostkey;
  85. int allow_compress; /* whether compression has started (useful in
  86. zlib@openssh.com delayed compression case) */
  87. };
  88. struct packetlist;
  89. struct packetlist {
  90. struct packetlist *next;
  91. buffer * payload;
  92. };
  93. struct sshsession {
  94. /* Is it a client or server? */
  95. unsigned char isserver;
  96. time_t connect_time; /* time the connection was established
  97. (cleared after auth once we're not
  98. respecting AUTH_TIMEOUT any more).
  99. A monotonic time, not realworld */
  100. int sock_in;
  101. int sock_out;
  102. /* remotehost will be initially NULL as we delay
  103. * reading the remote version string. it will be set
  104. * by the time any recv_() packet methods are called */
  105. char *remoteident;
  106. int maxfd; /* the maximum file descriptor to check with select() */
  107. /* Packet buffers/values etc */
  108. buffer *writepayload; /* Unencrypted payload to write - this is used
  109. throughout the code, as handlers fill out this
  110. buffer with the packet to send. */
  111. struct Queue writequeue; /* A queue of encrypted packets to send */
  112. unsigned int writequeue_len; /* Number of bytes pending to send in writequeue */
  113. buffer *readbuf; /* From the wire, decrypted in-place */
  114. buffer *payload; /* Post-decompression, the actual SSH packet.
  115. May have extra data at the beginning, will be
  116. passed to packet processing functions positioned past
  117. that, see payload_beginning */
  118. unsigned int payload_beginning;
  119. unsigned int transseq, recvseq; /* Sequence IDs */
  120. /* Packet-handling flags */
  121. const packettype * packettypes; /* Packet handler mappings for this
  122. session, see process-packet.c */
  123. unsigned dataallowed : 1; /* whether we can send data packets or we are in
  124. the middle of a KEX or something */
  125. unsigned char requirenext; /* byte indicating what packets we require next,
  126. or 0x00 for any. */
  127. unsigned char ignorenext; /* whether to ignore the next packet,
  128. used for kex_follows stuff */
  129. unsigned char lastpacket; /* What the last received packet type was */
  130. int signal_pipe[2]; /* stores endpoints of a self-pipe used for
  131. race-free signal handling */
  132. m_list conn_pending;
  133. /* time of the last packet send/receive, for keepalive. Not real-world clock */
  134. time_t last_packet_time_keepalive_sent;
  135. time_t last_packet_time_keepalive_recv;
  136. time_t last_packet_time_any_sent;
  137. time_t last_packet_time_idle; /* time of the last packet transmission or receive, for
  138. idle timeout purposes so ignores SSH_MSG_IGNORE
  139. or responses to keepalives. Not real-world clock */
  140. /* KEX/encryption related */
  141. struct KEXState kexstate;
  142. struct key_context *keys;
  143. struct key_context *newkeys;
  144. buffer *session_id; /* this is the hash from the first kex */
  145. /* The below are used temporarily during kex, are freed after use */
  146. mp_int * dh_K; /* SSH_MSG_KEXDH_REPLY and sending SSH_MSH_NEWKEYS */
  147. buffer *hash; /* the session hash */
  148. buffer* kexhashbuf; /* session hash buffer calculated from various packets*/
  149. buffer* transkexinit; /* the kexinit packet we send should be kept so we
  150. can add it to the hash when generating keys */
  151. /* Enables/disables compression */
  152. algo_type *compress_algos;
  153. /* a list of queued replies that should be sent after a KEX has
  154. concluded (ie, while dataallowed was unset)*/
  155. struct packetlist *reply_queue_head, *reply_queue_tail;
  156. void(*remoteclosed)(void); /* A callback to handle closure of the
  157. remote connection */
  158. void(*extra_session_cleanup)(void); /* client or server specific cleanup */
  159. void(*send_kex_first_guess)(void);
  160. struct AuthState authstate; /* Common amongst client and server, since most
  161. struct elements are common */
  162. /* Channel related */
  163. struct Channel ** channels; /* these pointers may be null */
  164. unsigned int chansize; /* the number of Channel*s allocated for channels */
  165. unsigned int chancount; /* the number of Channel*s in use */
  166. const struct ChanType **chantypes; /* The valid channel types */
  167. int channel_signal_pending; /* Flag set by sigchld handler */
  168. /* TCP priority level for the main "port 22" tcp socket */
  169. enum dropbear_prio socket_prio;
  170. /* TCP forwarding - where manage listeners */
  171. struct Listener ** listeners;
  172. unsigned int listensize;
  173. /* Whether to allow binding to privileged ports (<1024). This doesn't
  174. * really belong here, but nowhere else fits nicely */
  175. int allowprivport;
  176. };
  177. struct serversession {
  178. /* Server specific options */
  179. int childpipe; /* kept open until we successfully authenticate */
  180. /* userauth */
  181. struct ChildPid * childpids; /* array of mappings childpid<->channel */
  182. unsigned int childpidsize;
  183. /* Used to avoid a race in the exit returncode handling - see
  184. * svr-chansession.c for details */
  185. struct exitinfo lastexit;
  186. /* The numeric address they connected from, used for logging */
  187. char * addrstring;
  188. /* The resolved remote address, used for lastlog etc */
  189. char *remotehost;
  190. #ifdef USE_VFORK
  191. pid_t server_pid;
  192. #endif
  193. };
  194. typedef enum {
  195. KEX_NOTHING,
  196. KEXINIT_RCVD,
  197. KEXDH_INIT_SENT,
  198. KEXDONE
  199. } cli_kex_state;
  200. typedef enum {
  201. STATE_NOTHING,
  202. USERAUTH_WAIT,
  203. USERAUTH_REQ_SENT,
  204. USERAUTH_FAIL_RCVD,
  205. USERAUTH_SUCCESS_RCVD,
  206. SESSION_RUNNING
  207. } cli_state;
  208. struct clientsession {
  209. /* XXX - move these to kexstate? */
  210. struct kex_dh_param *dh_param;
  211. struct kex_ecdh_param *ecdh_param;
  212. struct kex_curve25519_param *curve25519_param;
  213. const struct dropbear_kex *param_kex_algo; /* KEX algorithm corresponding to current dh_e and dh_x */
  214. cli_kex_state kex_state; /* Used for progressing KEX */
  215. cli_state state; /* Used to progress auth/channelsession etc */
  216. unsigned donefirstkex : 1; /* Set when we set sentnewkeys, never reset */
  217. int tty_raw_mode; /* Whether we're in raw mode (and have to clean up) */
  218. struct termios saved_tio;
  219. int stdincopy;
  220. int stdinflags;
  221. int stdoutcopy;
  222. int stdoutflags;
  223. int stderrcopy;
  224. int stderrflags;
  225. /* for escape char handling */
  226. int last_char;
  227. int winchange; /* Set to 1 when a windowchange signal happens */
  228. int lastauthtype; /* either AUTH_TYPE_PUBKEY or AUTH_TYPE_PASSWORD,
  229. for the last type of auth we tried */
  230. int ignore_next_auth_response;
  231. #ifdef ENABLE_CLI_INTERACT_AUTH
  232. int auth_interact_failed; /* flag whether interactive auth can still
  233. be used */
  234. int interact_request_received; /* flag whether we've received an
  235. info request from the server for
  236. interactive auth.*/
  237. #endif
  238. int cipher_none_after_auth; /* Set to 1 if the user requested "none"
  239. auth */
  240. sign_key *lastprivkey;
  241. int retval; /* What the command exit status was - we emulate it */
  242. #if 0
  243. TODO
  244. struct AgentkeyList *agentkeys; /* Keys to use for public-key auth */
  245. #endif
  246. pid_t proxy_cmd_pid;
  247. };
  248. /* Global structs storing the state */
  249. extern struct sshsession ses;
  250. #ifdef DROPBEAR_SERVER
  251. extern struct serversession svr_ses;
  252. #endif /* DROPBEAR_SERVER */
  253. #ifdef DROPBEAR_CLIENT
  254. extern struct clientsession cli_ses;
  255. #endif /* DROPBEAR_CLIENT */
  256. #endif /* DROPBEAR_SESSION_H_ */