session.h 11 KB

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