Mercurial > dropbear
comparison cli-chansession.c @ 40:b4874d772210
- Added terminal mode handling etc for the client, and window change
- Refactored the terminal-mode handling for the server
- Improved session closing for the client
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 01 Aug 2004 08:54:01 +0000 |
parents | 0883c0906870 |
children | 18eccbfb9641 |
comparison
equal
deleted
inserted
replaced
39:0883c0906870 | 40:b4874d772210 |
---|---|
4 #include "session.h" | 4 #include "session.h" |
5 #include "dbutil.h" | 5 #include "dbutil.h" |
6 #include "channel.h" | 6 #include "channel.h" |
7 #include "ssh.h" | 7 #include "ssh.h" |
8 #include "runopts.h" | 8 #include "runopts.h" |
9 #include "termcodes.h" | |
9 | 10 |
10 static void cli_closechansess(struct Channel *channel); | 11 static void cli_closechansess(struct Channel *channel); |
11 static int cli_initchansess(struct Channel *channel); | 12 static int cli_initchansess(struct Channel *channel); |
12 | 13 |
13 static void start_channel_request(struct Channel *channel, unsigned char *type); | 14 static void start_channel_request(struct Channel *channel, unsigned char *type); |
14 | 15 |
15 static void send_chansess_pty_req(struct Channel *channel); | 16 static void send_chansess_pty_req(struct Channel *channel); |
16 static void send_chansess_shell_req(struct Channel *channel); | 17 static void send_chansess_shell_req(struct Channel *channel); |
17 | 18 |
18 static void cli_tty_setup(); | 19 static void cli_tty_setup(); |
19 static void cli_tty_cleanup(); | 20 void cli_tty_cleanup(); |
20 | 21 |
21 static const struct ChanType clichansess = { | 22 static const struct ChanType clichansess = { |
22 0, /* sepfds */ | 23 0, /* sepfds */ |
23 "session", /* name */ | 24 "session", /* name */ |
24 cli_initchansess, /* inithandler */ | 25 cli_initchansess, /* inithandler */ |
47 buf_putint(ses.writepayload, channel->remotechan); | 48 buf_putint(ses.writepayload, channel->remotechan); |
48 | 49 |
49 buf_putstring(ses.writepayload, type, strlen(type)); | 50 buf_putstring(ses.writepayload, type, strlen(type)); |
50 | 51 |
51 } | 52 } |
52 | |
53 | 53 |
54 /* Taken from OpenSSH's sshtty.c: | 54 /* Taken from OpenSSH's sshtty.c: |
55 * RCSID("OpenBSD: sshtty.c,v 1.5 2003/09/19 17:43:35 markus Exp "); */ | 55 * RCSID("OpenBSD: sshtty.c,v 1.5 2003/09/19 17:43:35 markus Exp "); */ |
56 static void cli_tty_setup() { | 56 static void cli_tty_setup() { |
57 | 57 |
89 | 89 |
90 cli_ses.tty_raw_mode = 1; | 90 cli_ses.tty_raw_mode = 1; |
91 TRACE(("leave cli_tty_setup")); | 91 TRACE(("leave cli_tty_setup")); |
92 } | 92 } |
93 | 93 |
94 static void cli_tty_cleanup() { | 94 void cli_tty_cleanup() { |
95 | 95 |
96 TRACE(("enter cli_tty_cleanup")); | 96 TRACE(("enter cli_tty_cleanup")); |
97 | 97 |
98 if (cli_ses.tty_raw_mode == 0) { | 98 if (cli_ses.tty_raw_mode == 0) { |
99 TRACE(("leave cli_tty_cleanup: not in raw mode")); | 99 TRACE(("leave cli_tty_cleanup: not in raw mode")); |
100 return; | |
100 } | 101 } |
101 | 102 |
102 if (tcsetattr(STDIN_FILENO, TCSADRAIN, &cli_ses.saved_tio) == -1) { | 103 if (tcsetattr(STDIN_FILENO, TCSADRAIN, &cli_ses.saved_tio) == -1) { |
103 dropbear_log(LOG_WARNING, "Failed restoring TTY"); | 104 dropbear_log(LOG_WARNING, "Failed restoring TTY"); |
104 } else { | 105 } else { |
106 } | 107 } |
107 | 108 |
108 TRACE(("leave cli_tty_cleanup")); | 109 TRACE(("leave cli_tty_cleanup")); |
109 } | 110 } |
110 | 111 |
112 static void put_termcodes() { | |
113 | |
114 TRACE(("enter put_termcodes")); | |
115 | |
116 struct termios tio; | |
117 unsigned int sshcode; | |
118 const struct TermCode *termcode; | |
119 unsigned int value; | |
120 unsigned int mapcode; | |
121 | |
122 unsigned int bufpos1, bufpos2; | |
123 | |
124 if (tcgetattr(STDIN_FILENO, &tio) == -1) { | |
125 dropbear_log(LOG_WARNING, "Failed reading termmodes"); | |
126 buf_putint(ses.writepayload, 1); /* Just the terminator */ | |
127 buf_putbyte(ses.writepayload, 0); /* TTY_OP_END */ | |
128 return; | |
129 } | |
130 | |
131 bufpos1 = ses.writepayload->pos; | |
132 buf_putint(ses.writepayload, 0); /* A placeholder for the final length */ | |
133 | |
134 /* As with Dropbear server, we ignore baud rates for now */ | |
135 for (sshcode = 1; sshcode < MAX_TERMCODE; sshcode++) { | |
136 | |
137 termcode = &termcodes[sshcode]; | |
138 mapcode = termcode->mapcode; | |
139 | |
140 switch (termcode->type) { | |
141 | |
142 case TERMCODE_NONE: | |
143 continue; | |
144 | |
145 case TERMCODE_CONTROLCHAR: | |
146 value = tio.c_cc[mapcode]; | |
147 break; | |
148 | |
149 case TERMCODE_INPUT: | |
150 value = tio.c_iflag & mapcode; | |
151 break; | |
152 | |
153 case TERMCODE_OUTPUT: | |
154 value = tio.c_oflag & mapcode; | |
155 break; | |
156 | |
157 case TERMCODE_LOCAL: | |
158 value = tio.c_lflag & mapcode; | |
159 break; | |
160 | |
161 case TERMCODE_CONTROL: | |
162 value = tio.c_cflag & mapcode; | |
163 break; | |
164 | |
165 default: | |
166 continue; | |
167 | |
168 } | |
169 | |
170 /* If we reach here, we have something to say */ | |
171 buf_putbyte(ses.writepayload, sshcode); | |
172 buf_putint(ses.writepayload, value); | |
173 } | |
174 | |
175 buf_putbyte(ses.writepayload, 0); /* THE END, aka TTY_OP_END */ | |
176 | |
177 /* Put the string length at the start of the buffer */ | |
178 bufpos2 = ses.writepayload->pos; | |
179 | |
180 buf_setpos(ses.writepayload, bufpos1); /* Jump back */ | |
181 buf_putint(ses.writepayload, bufpos2 - bufpos1); /* len(termcodes) */ | |
182 buf_setpos(ses.writepayload, bufpos2); /* Back where we were */ | |
183 | |
184 TRACE(("leave put_termcodes")); | |
185 } | |
186 | |
187 static void put_winsize() { | |
188 | |
189 struct winsize ws; | |
190 | |
191 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0) { | |
192 /* Some sane defaults */ | |
193 ws.ws_row = 25; | |
194 ws.ws_col = 80; | |
195 ws.ws_xpixel = 0; | |
196 ws.ws_ypixel = 0; | |
197 } | |
198 | |
199 buf_putint(ses.writepayload, ws.ws_col); /* Cols */ | |
200 buf_putint(ses.writepayload, ws.ws_row); /* Rows */ | |
201 buf_putint(ses.writepayload, ws.ws_xpixel); /* Width */ | |
202 buf_putint(ses.writepayload, ws.ws_ypixel); /* Height */ | |
203 | |
204 } | |
205 | |
111 static void send_chansess_pty_req(struct Channel *channel) { | 206 static void send_chansess_pty_req(struct Channel *channel) { |
112 | 207 |
113 unsigned char* termmodes = "\0"; | |
114 unsigned char* term = NULL; | 208 unsigned char* term = NULL; |
115 int termc = 80, termr = 25, termw = 0, termh = 0; /* XXX TODO matt */ | |
116 | 209 |
117 TRACE(("enter send_chansess_pty_req")); | 210 TRACE(("enter send_chansess_pty_req")); |
211 | |
118 start_channel_request(channel, "pty-req"); | 212 start_channel_request(channel, "pty-req"); |
119 | 213 |
214 /* Don't want replies */ | |
215 buf_putbyte(ses.writepayload, 0); | |
216 | |
217 /* Get the terminal */ | |
120 term = getenv("TERM"); | 218 term = getenv("TERM"); |
121 if (term == NULL) { | 219 if (term == NULL) { |
122 term = "vt100"; | 220 term = "vt100"; /* Seems a safe default */ |
123 } | 221 } |
124 | |
125 /* XXX TODO */ | |
126 buf_putbyte(ses.writepayload, 0); /* Don't want replies */ | |
127 buf_putstring(ses.writepayload, term, strlen(term)); | 222 buf_putstring(ses.writepayload, term, strlen(term)); |
128 buf_putint(ses.writepayload, termc); /* Cols */ | 223 |
129 buf_putint(ses.writepayload, termr); /* Rows */ | 224 /* Window size */ |
130 buf_putint(ses.writepayload, termw); /* Width */ | 225 put_winsize(); |
131 buf_putint(ses.writepayload, termh); /* Height */ | 226 |
132 | 227 /* Terminal mode encoding */ |
133 buf_putstring(ses.writepayload, termmodes, 1); /* XXX TODO */ | 228 put_termcodes(); |
134 //m_free(termmodes); | |
135 | 229 |
136 encrypt_packet(); | 230 encrypt_packet(); |
137 TRACE(("leave send_chansess_pty_req")); | 231 TRACE(("leave send_chansess_pty_req")); |
138 } | 232 } |
139 | 233 |
169 | 263 |
170 if (cli_opts.wantpty) { | 264 if (cli_opts.wantpty) { |
171 send_chansess_pty_req(channel); | 265 send_chansess_pty_req(channel); |
172 } | 266 } |
173 | 267 |
174 cli_opts.cmd = "df"; | |
175 send_chansess_shell_req(channel); | 268 send_chansess_shell_req(channel); |
176 | 269 |
177 if (cli_opts.wantpty) { | 270 if (cli_opts.wantpty) { |
178 cli_tty_setup(); | 271 cli_tty_setup(); |
179 } | 272 } |