Mercurial > dropbear
annotate cli-session.c @ 37:0913e2ee3545
we're nearly there yet
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 30 Jul 2004 03:02:19 +0000 |
parents | 0ad5fb979f42 |
children | 0883c0906870 |
rev | line source |
---|---|
26 | 1 #include "includes.h" |
2 #include "session.h" | |
3 #include "dbutil.h" | |
4 #include "kex.h" | |
5 #include "ssh.h" | |
6 #include "packet.h" | |
7 #include "tcpfwd-direct.h" | |
8 #include "tcpfwd-remote.h" | |
9 #include "channel.h" | |
10 #include "random.h" | |
33 | 11 #include "service.h" |
26 | 12 |
13 static void cli_remoteclosed(); | |
14 static void cli_sessionloop(); | |
33 | 15 static void cli_session_init(); |
26 | 16 |
17 struct clientsession cli_ses; /* GLOBAL */ | |
18 | |
19 static const packettype cli_packettypes[] = { | |
20 /* TYPE, AUTHREQUIRED, FUNCTION */ | |
21 {SSH_MSG_KEXINIT, recv_msg_kexinit}, | |
22 {SSH_MSG_KEXDH_REPLY, recv_msg_kexdh_reply}, // client | |
23 {SSH_MSG_NEWKEYS, recv_msg_newkeys}, | |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
24 {SSH_MSG_SERVICE_ACCEPT, recv_msg_service_accept}, // client |
26 | 25 {SSH_MSG_CHANNEL_DATA, recv_msg_channel_data}, |
26 {SSH_MSG_CHANNEL_WINDOW_ADJUST, recv_msg_channel_window_adjust}, | |
27 {SSH_MSG_GLOBAL_REQUEST, recv_msg_global_request_remotetcp}, | |
28 {SSH_MSG_CHANNEL_REQUEST, recv_msg_channel_request}, | |
29 {SSH_MSG_CHANNEL_OPEN, recv_msg_channel_open}, | |
30 {SSH_MSG_CHANNEL_EOF, recv_msg_channel_eof}, | |
31 {SSH_MSG_CHANNEL_CLOSE, recv_msg_channel_close}, | |
32 {SSH_MSG_CHANNEL_OPEN_CONFIRMATION, recv_msg_channel_open_confirmation}, | |
33 {SSH_MSG_CHANNEL_OPEN_FAILURE, recv_msg_channel_open_failure}, | |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
34 {SSH_MSG_USERAUTH_FAILURE, recv_msg_userauth_failure}, // client |
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
35 {SSH_MSG_USERAUTH_SUCCESS, recv_msg_userauth_success}, // client |
26 | 36 {0, 0} /* End */ |
37 }; | |
38 | |
39 static const struct ChanType *cli_chantypes[] = { | |
40 /* &chan_tcpdirect etc, though need to only allow if we've requested | |
41 * that forwarding */ | |
42 NULL /* Null termination */ | |
43 }; | |
33 | 44 |
26 | 45 void cli_session(int sock, char* remotehost) { |
46 | |
47 crypto_init(); | |
48 common_session_init(sock, remotehost); | |
49 | |
50 chaninitialise(cli_chantypes); | |
51 | |
52 | |
33 | 53 /* Set up cli_ses vars */ |
54 cli_session_init(); | |
26 | 55 |
56 /* Ready to go */ | |
57 sessinitdone = 1; | |
58 | |
59 /* Exchange identification */ | |
60 session_identification(); | |
61 | |
62 seedrandom(); | |
63 | |
64 send_msg_kexinit(); | |
65 | |
66 /* XXX here we do stuff differently */ | |
67 | |
68 session_loop(cli_sessionloop); | |
69 | |
70 /* Not reached */ | |
71 | |
33 | 72 } |
26 | 73 |
33 | 74 static void cli_session_init() { |
75 | |
76 cli_ses.state = STATE_NOTHING; | |
77 cli_ses.kex_state = KEX_NOTHING; | |
78 | |
79 /* For printing "remote host closed" for the user */ | |
80 ses.remoteclosed = cli_remoteclosed; | |
81 ses.buf_match_algo = cli_buf_match_algo; | |
82 | |
83 /* packet handlers */ | |
84 ses.packettypes = cli_packettypes; | |
35
0ad5fb979f42
set the isserver flag (oops)
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
85 |
0ad5fb979f42
set the isserver flag (oops)
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
86 ses.isserver = 0; |
26 | 87 } |
88 | |
33 | 89 /* This function drives the progress of the session - it initiates KEX, |
90 * service, userauth and channel requests */ | |
26 | 91 static void cli_sessionloop() { |
92 | |
33 | 93 TRACE(("enter cli_sessionloop")); |
94 | |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
95 if (ses.lastpacket == SSH_MSG_KEXINIT && cli_ses.kex_state == KEX_NOTHING) { |
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
96 cli_ses.kex_state = KEXINIT_RCVD; |
33 | 97 } |
98 | |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
99 if (cli_ses.kex_state == KEXINIT_RCVD) { |
33 | 100 |
101 /* We initiate the KEXDH. If DH wasn't the correct type, the KEXINIT | |
102 * negotiation would have failed. */ | |
103 send_msg_kexdh_init(); | |
104 cli_ses.kex_state = KEXDH_INIT_SENT; | |
105 TRACE(("leave cli_sessionloop: done with KEXINIT_RCVD")); | |
106 return; | |
107 } | |
108 | |
109 /* A KEX has finished, so we should go back to our KEX_NOTHING state */ | |
110 if (cli_ses.kex_state != KEX_NOTHING && ses.kexstate.recvkexinit == 0 | |
111 && ses.kexstate.sentkexinit == 0) { | |
112 cli_ses.kex_state = KEX_NOTHING; | |
113 } | |
114 | |
115 /* We shouldn't do anything else if a KEX is in progress */ | |
116 if (cli_ses.kex_state != KEX_NOTHING) { | |
117 TRACE(("leave cli_sessionloop: kex_state != KEX_NOTHING")); | |
118 return; | |
119 } | |
120 | |
121 /* We should exit if we haven't donefirstkex: we shouldn't reach here | |
122 * in normal operation */ | |
123 if (ses.kexstate.donefirstkex == 0) { | |
124 TRACE(("XXX XXX might be bad! leave cli_sessionloop: haven't donefirstkex")); | |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
125 return; |
33 | 126 } |
127 | |
26 | 128 switch (cli_ses.state) { |
129 | |
33 | 130 case STATE_NOTHING: |
131 /* We've got the transport layer sorted, we now need to request | |
132 * userauth */ | |
133 send_msg_service_request(SSH_SERVICE_USERAUTH); | |
134 cli_ses.state = SERVICE_AUTH_REQ_SENT; | |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
135 TRACE(("leave cli_sessionloop: sent userauth service req")); |
33 | 136 return; |
26 | 137 |
33 | 138 /* userauth code */ |
139 case SERVICE_AUTH_ACCEPT_RCVD: | |
140 cli_auth_getmethods(); | |
141 cli_ses.state = USERAUTH_METHODS_SENT; | |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
142 TRACE(("leave cli_sessionloop: sent userauth methods req")); |
33 | 143 return; |
144 | |
145 case USERAUTH_FAIL_RCVD: | |
146 cli_auth_try(); | |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
147 TRACE(("leave cli_sessionloop: cli_auth_try")); |
33 | 148 return; |
149 | |
37 | 150 /* |
151 case USERAUTH_SUCCESS_RCVD: | |
152 send_msg_service_request(SSH_SERVICE_CONNECTION); | |
153 cli_ses.state = SERVICE_CONN_REQ_SENT; | |
154 TRACE(("leave cli_sessionloop: sent ssh-connection service req")); | |
155 return; | |
156 */ | |
157 | |
158 case USERAUTH_SUCCESS_RCVD: | |
159 cli_send_chansess_request(); | |
160 TRACE(("leave cli_sessionloop: cli_send_chansess_request")); | |
161 cli_ses.state = SESSION_RUNNING; | |
162 return; | |
163 | |
33 | 164 /* XXX more here needed */ |
165 | |
166 | |
167 default: | |
168 break; | |
26 | 169 } |
170 | |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
171 TRACE(("leave cli_sessionloop: fell out")); |
26 | 172 |
173 } | |
174 | |
175 /* called when the remote side closes the connection */ | |
176 static void cli_remoteclosed() { | |
177 | |
178 /* XXX TODO perhaps print a friendlier message if we get this but have | |
179 * already sent/received disconnect message(s) ??? */ | |
180 close(ses.sock); | |
181 ses.sock = -1; | |
33 | 182 dropbear_exit("remote closed the connection"); |
26 | 183 } |