comparison cli-session.c @ 118:5312ca05ed48 private-rez

propagate of 717950f4061f1123659ee87c7c168805af920ab7 and 839f98f136788cc1466e4641bf796f96040a085d from branch 'matt.dbclient.authpam' to 'matt.dbclient.rez'
author Matt Johnston <matt@ucc.asn.au>
date Sun, 12 Sep 2004 04:56:50 +0000
parents 10f4d3319780
children fb7147e2fb04
comparison
equal deleted inserted replaced
57:3b2a5a1c4347 118:5312ca05ed48
1 /*
2 * Dropbear SSH
3 *
4 * Copyright (c) 2002,2003 Matt Johnston
5 * Copyright (c) 2004 by Mihnea Stoenescu
6 * All rights reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE. */
25
1 #include "includes.h" 26 #include "includes.h"
2 #include "session.h" 27 #include "session.h"
3 #include "dbutil.h" 28 #include "dbutil.h"
4 #include "kex.h" 29 #include "kex.h"
5 #include "ssh.h" 30 #include "ssh.h"
6 #include "packet.h" 31 #include "packet.h"
7 #include "tcpfwd-direct.h" 32 #include "tcpfwd.h"
8 #include "tcpfwd-remote.h"
9 #include "channel.h" 33 #include "channel.h"
10 #include "random.h" 34 #include "random.h"
11 #include "service.h" 35 #include "service.h"
12 #include "runopts.h" 36 #include "runopts.h"
13 #include "chansession.h" 37 #include "chansession.h"
20 struct clientsession cli_ses; /* GLOBAL */ 44 struct clientsession cli_ses; /* GLOBAL */
21 45
22 /* Sorted in decreasing frequency will be more efficient - data and window 46 /* Sorted in decreasing frequency will be more efficient - data and window
23 * should be first */ 47 * should be first */
24 static const packettype cli_packettypes[] = { 48 static const packettype cli_packettypes[] = {
25 /* TYPE, AUTHREQUIRED, FUNCTION */ 49 /* TYPE, FUNCTION */
26 {SSH_MSG_CHANNEL_DATA, recv_msg_channel_data}, 50 {SSH_MSG_CHANNEL_DATA, recv_msg_channel_data},
51 {SSH_MSG_CHANNEL_EXTENDED_DATA, recv_msg_channel_extended_data},
27 {SSH_MSG_CHANNEL_WINDOW_ADJUST, recv_msg_channel_window_adjust}, 52 {SSH_MSG_CHANNEL_WINDOW_ADJUST, recv_msg_channel_window_adjust},
28 {SSH_MSG_USERAUTH_FAILURE, recv_msg_userauth_failure}, /* client */ 53 {SSH_MSG_USERAUTH_FAILURE, recv_msg_userauth_failure}, /* client */
29 {SSH_MSG_USERAUTH_SUCCESS, recv_msg_userauth_success}, /* client */ 54 {SSH_MSG_USERAUTH_SUCCESS, recv_msg_userauth_success}, /* client */
30 {SSH_MSG_KEXINIT, recv_msg_kexinit}, 55 {SSH_MSG_KEXINIT, recv_msg_kexinit},
31 {SSH_MSG_KEXDH_REPLY, recv_msg_kexdh_reply}, /* client */ 56 {SSH_MSG_KEXDH_REPLY, recv_msg_kexdh_reply}, /* client */
32 {SSH_MSG_NEWKEYS, recv_msg_newkeys}, 57 {SSH_MSG_NEWKEYS, recv_msg_newkeys},
33 {SSH_MSG_SERVICE_ACCEPT, recv_msg_service_accept}, /* client */ 58 {SSH_MSG_SERVICE_ACCEPT, recv_msg_service_accept}, /* client */
34 {SSH_MSG_GLOBAL_REQUEST, recv_msg_global_request_remotetcp},
35 {SSH_MSG_CHANNEL_REQUEST, recv_msg_channel_request}, 59 {SSH_MSG_CHANNEL_REQUEST, recv_msg_channel_request},
36 {SSH_MSG_CHANNEL_OPEN, recv_msg_channel_open}, 60 {SSH_MSG_CHANNEL_OPEN, recv_msg_channel_open},
37 {SSH_MSG_CHANNEL_EOF, recv_msg_channel_eof}, 61 {SSH_MSG_CHANNEL_EOF, recv_msg_channel_eof},
38 {SSH_MSG_CHANNEL_CLOSE, recv_msg_channel_close}, 62 {SSH_MSG_CHANNEL_CLOSE, recv_msg_channel_close},
39 {SSH_MSG_CHANNEL_OPEN_CONFIRMATION, recv_msg_channel_open_confirmation}, 63 {SSH_MSG_CHANNEL_OPEN_CONFIRMATION, recv_msg_channel_open_confirmation},
40 {SSH_MSG_CHANNEL_OPEN_FAILURE, recv_msg_channel_open_failure}, 64 {SSH_MSG_CHANNEL_OPEN_FAILURE, recv_msg_channel_open_failure},
41 {SSH_MSG_USERAUTH_BANNER, recv_msg_userauth_banner}, /* client */ 65 {SSH_MSG_USERAUTH_BANNER, recv_msg_userauth_banner}, /* client */
42 #ifdef DROPBEAR_PUBKEY_AUTH 66 #ifdef ENABLE_CLI_PUBKEY_AUTH
43 {SSH_MSG_USERAUTH_PK_OK, recv_msg_userauth_pk_ok}, /* client */ 67 {SSH_MSG_USERAUTH_PK_OK, recv_msg_userauth_pk_ok}, /* client */
44 #endif 68 #endif
45 {0, 0} /* End */ 69 {0, 0} /* End */
46 }; 70 };
47 71
48 static const struct ChanType *cli_chantypes[] = { 72 static const struct ChanType *cli_chantypes[] = {
49 /* &chan_tcpdirect etc, though need to only allow if we've requested 73 #ifdef ENABLE_CLI_REMOTETCPFWD
50 * that forwarding */ 74 &cli_chan_tcpremote,
75 #endif
51 NULL /* Null termination */ 76 NULL /* Null termination */
52 }; 77 };
53 78
54 void cli_session(int sock, char* remotehost) { 79 void cli_session(int sock, char* remotehost) {
55 80
85 cli_ses.state = STATE_NOTHING; 110 cli_ses.state = STATE_NOTHING;
86 cli_ses.kex_state = KEX_NOTHING; 111 cli_ses.kex_state = KEX_NOTHING;
87 112
88 cli_ses.tty_raw_mode = 0; 113 cli_ses.tty_raw_mode = 0;
89 cli_ses.winchange = 0; 114 cli_ses.winchange = 0;
115
116 /* We store stdin's flags, so we can set them back on exit (otherwise
117 * busybox's ash isn't happy */
118 cli_ses.stdincopy = dup(STDIN_FILENO);
119 cli_ses.stdinflags = fcntl(STDIN_FILENO, F_GETFL, 0);
120
121 cli_ses.retval = EXIT_SUCCESS; /* Assume it's clean if we don't get a
122 specific exit status */
90 123
91 /* Auth */ 124 /* Auth */
92 cli_ses.lastpubkey = NULL; 125 cli_ses.lastpubkey = NULL;
93 cli_ses.lastauthtype = NULL; 126 cli_ses.lastauthtype = NULL;
94 127
177 cli_ses.state = SESSION_RUNNING; 210 cli_ses.state = SESSION_RUNNING;
178 return; 211 return;
179 */ 212 */
180 213
181 case USERAUTH_SUCCESS_RCVD: 214 case USERAUTH_SUCCESS_RCVD:
215 #ifdef ENABLE_CLI_LOCALTCPFWD
216 setup_localtcp();
217 #endif
218 #ifdef ENABLE_CLI_REMOTETCPFWD
219 setup_remotetcp();
220 #endif
182 cli_send_chansess_request(); 221 cli_send_chansess_request();
183 TRACE(("leave cli_sessionloop: cli_send_chansess_request")); 222 TRACE(("leave cli_sessionloop: cli_send_chansess_request"));
184 cli_ses.state = SESSION_RUNNING; 223 cli_ses.state = SESSION_RUNNING;
185 return; 224 return;
186 225
208 void cli_session_cleanup() { 247 void cli_session_cleanup() {
209 248
210 if (!sessinitdone) { 249 if (!sessinitdone) {
211 return; 250 return;
212 } 251 }
252
253 /* Set stdin back to non-blocking - busybox ash dies nastily
254 * if we don't revert the flags */
255 fcntl(cli_ses.stdincopy, F_SETFL, cli_ses.stdinflags);
256
213 cli_tty_cleanup(); 257 cli_tty_cleanup();
214 258
215 } 259 }
216 260
217 static void cli_finished() { 261 static void cli_finished() {
218 262
219 cli_session_cleanup(); 263 cli_session_cleanup();
220 common_session_cleanup(); 264 common_session_cleanup();
221 fprintf(stderr, "Connection to %s@%s:%s closed.\n", cli_opts.username, 265 fprintf(stderr, "Connection to %s@%s:%s closed.\n", cli_opts.username,
222 cli_opts.remotehost, cli_opts.remoteport); 266 cli_opts.remotehost, cli_opts.remoteport);
223 exit(EXIT_SUCCESS); 267 exit(cli_ses.retval);
224 } 268 }
225
226 269
227 270
228 /* called when the remote side closes the connection */ 271 /* called when the remote side closes the connection */
229 static void cli_remoteclosed() { 272 static void cli_remoteclosed() {
230 273