Add another missing bounds check in the SSH-1 private key loader.
[u/mdw/putty] / raw.c
CommitLineData
eaf1e20a 1/*
2 * "Raw" backend.
3 */
4
5e1a8e27 5#include <stdio.h>
6#include <stdlib.h>
96b720b3 7#include <limits.h>
5e1a8e27 8
9#include "putty.h"
10
11#ifndef FALSE
12#define FALSE 0
13#endif
14#ifndef TRUE
15#define TRUE 1
16#endif
17
5471d09a 18#define RAW_MAX_BACKLOG 4096
19
51470298 20typedef struct raw_backend_data {
21 const struct plug_function_table *fn;
22 /* the above field _must_ be first in the structure */
5e1a8e27 23
51470298 24 Socket s;
96b720b3 25 int closed_on_socket_error;
51470298 26 int bufsize;
27 void *frontend;
bc06669b 28 int sent_console_eof, sent_socket_eof;
51470298 29} *Raw;
5e1a8e27 30
51470298 31static void raw_size(void *handle, int width, int height);
32
33static void c_write(Raw raw, char *buf, int len)
32874aea 34{
51470298 35 int backlog = from_backend(raw->frontend, 0, buf, len);
36 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
5e1a8e27 37}
38
7555d6a5 39static void raw_log(Plug plug, int type, SockAddr addr, int port,
40 const char *error_msg, int error_code)
41{
42 Raw raw = (Raw) plug;
43 char addrbuf[256], *msg;
44
45 sk_getaddr(addr, addrbuf, lenof(addrbuf));
46
47 if (type == 0)
48 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
49 else
50 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
51
52 logevent(raw->frontend, msg);
53}
54
bc06669b 55static void raw_check_close(Raw raw)
56{
57 /*
58 * Called after we send EOF on either the socket or the console.
59 * Its job is to wind up the session once we have sent EOF on both.
60 */
61 if (raw->sent_console_eof && raw->sent_socket_eof) {
62 if (raw->s) {
63 sk_close(raw->s);
64 raw->s = NULL;
65 notify_remote_exit(raw->frontend);
66 }
67 }
68}
69
cbe2d68f 70static int raw_closing(Plug plug, const char *error_msg, int error_code,
32874aea 71 int calling_back)
72{
51470298 73 Raw raw = (Raw) plug;
74
7e78000d 75 if (error_msg) {
bc06669b 76 /* A socket error has occurred. */
77 if (raw->s) {
78 sk_close(raw->s);
79 raw->s = NULL;
96b720b3 80 raw->closed_on_socket_error = TRUE;
bc06669b 81 notify_remote_exit(raw->frontend);
82 }
83 logevent(raw->frontend, error_msg);
84 connection_fatal(raw->frontend, "%s", error_msg);
85 } else {
86 /* Otherwise, the remote side closed the connection normally. */
87 if (!raw->sent_console_eof && from_backend_eof(raw->frontend)) {
88 /*
89 * The front end wants us to close the outgoing side of the
90 * connection as soon as we see EOF from the far end.
91 */
92 if (!raw->sent_socket_eof) {
93 if (raw->s)
94 sk_write_eof(raw->s);
95 raw->sent_socket_eof= TRUE;
96 }
97 }
98 raw->sent_console_eof = TRUE;
99 raw_check_close(raw);
100 }
7e78000d 101 return 0;
102}
103
32874aea 104static int raw_receive(Plug plug, int urgent, char *data, int len)
105{
51470298 106 Raw raw = (Raw) plug;
107 c_write(raw, data, len);
8df7a775 108 return 1;
5e1a8e27 109}
110
3ad9d396 111static void raw_sent(Plug plug, int bufsize)
112{
51470298 113 Raw raw = (Raw) plug;
114 raw->bufsize = bufsize;
3ad9d396 115}
116
5e1a8e27 117/*
8df7a775 118 * Called to set up the raw connection.
119 *
5e1a8e27 120 * Returns an error message, or NULL on success.
121 *
6e1ebb76 122 * Also places the canonical host name into `realhost'. It must be
123 * freed by the caller.
5e1a8e27 124 */
cbe2d68f 125static const char *raw_init(void *frontend_handle, void **backend_handle,
4a693cfc 126 Conf *conf,
79bf227b 127 char *host, int port, char **realhost, int nodelay,
128 int keepalive)
32874aea 129{
51470298 130 static const struct plug_function_table fn_table = {
7555d6a5 131 raw_log,
7e78000d 132 raw_closing,
3ad9d396 133 raw_receive,
134 raw_sent
51470298 135 };
8df7a775 136 SockAddr addr;
cbe2d68f 137 const char *err;
51470298 138 Raw raw;
4a693cfc 139 int addressfamily;
140 char *loghost;
5e1a8e27 141
3d88e64d 142 raw = snew(struct raw_backend_data);
51470298 143 raw->fn = &fn_table;
144 raw->s = NULL;
96b720b3 145 raw->closed_on_socket_error = FALSE;
51470298 146 *backend_handle = raw;
bc06669b 147 raw->sent_console_eof = raw->sent_socket_eof = FALSE;
51470298 148
149 raw->frontend = frontend_handle;
887035a5 150
4a693cfc 151 addressfamily = conf_get_int(conf, CONF_addressfamily);
5e1a8e27 152 /*
153 * Try to find host.
154 */
3ad9d396 155 {
57356d63 156 char *buf;
05581745 157 buf = dupprintf("Looking up host \"%s\"%s", host,
4a693cfc 158 (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
159 (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
05581745 160 "")));
a8327734 161 logevent(raw->frontend, buf);
57356d63 162 sfree(buf);
3ad9d396 163 }
4a693cfc 164 addr = name_lookup(host, port, realhost, conf, addressfamily);
f85e6f6e 165 if ((err = sk_addr_error(addr)) != NULL) {
166 sk_addr_free(addr);
8df7a775 167 return err;
f85e6f6e 168 }
5e1a8e27 169
170 if (port < 0)
171 port = 23; /* default telnet port */
172
173 /*
174 * Open socket.
175 */
79bf227b 176 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,
4a693cfc 177 (Plug) raw, conf);
6f1e7b78 178 if ((err = sk_socket_error(raw->s)) != NULL)
8df7a775 179 return err;
5e1a8e27 180
4a693cfc 181 loghost = conf_get_str(conf, CONF_loghost);
182 if (*loghost) {
881da168 183 char *colon;
184
185 sfree(*realhost);
4a693cfc 186 *realhost = dupstr(loghost);
881da168 187 colon = strrchr(*realhost, ':');
188 if (colon) {
189 /*
190 * FIXME: if we ever update this aspect of ssh.c for
191 * IPv6 literal management, this should change in line
192 * with it.
193 */
194 *colon++ = '\0';
195 }
196 }
197
5e1a8e27 198 return NULL;
199}
200
fabd1805 201static void raw_free(void *handle)
202{
203 Raw raw = (Raw) handle;
204
205 if (raw->s)
206 sk_close(raw->s);
207 sfree(raw);
208}
209
5e1a8e27 210/*
86916870 211 * Stub routine (we don't have any need to reconfigure this backend).
212 */
4a693cfc 213static void raw_reconfig(void *handle, Conf *conf)
86916870 214{
215}
216
217/*
5e1a8e27 218 * Called to send data down the raw connection.
219 */
51470298 220static int raw_send(void *handle, char *buf, int len)
32874aea 221{
51470298 222 Raw raw = (Raw) handle;
223
224 if (raw->s == NULL)
b5a460cd 225 return 0;
5e1a8e27 226
51470298 227 raw->bufsize = sk_write(raw->s, buf, len);
5471d09a 228
51470298 229 return raw->bufsize;
5471d09a 230}
231
232/*
233 * Called to query the current socket sendability status.
234 */
51470298 235static int raw_sendbuffer(void *handle)
5471d09a 236{
51470298 237 Raw raw = (Raw) handle;
238 return raw->bufsize;
5e1a8e27 239}
240
241/*
242 * Called to set the size of the window
243 */
51470298 244static void raw_size(void *handle, int width, int height)
32874aea 245{
5e1a8e27 246 /* Do nothing! */
247 return;
248}
249
250/*
bc06669b 251 * Send raw special codes. We only handle outgoing EOF here.
5e1a8e27 252 */
51470298 253static void raw_special(void *handle, Telnet_Special code)
32874aea 254{
bc06669b 255 Raw raw = (Raw) handle;
256 if (code == TS_EOF && raw->s) {
257 sk_write_eof(raw->s);
258 raw->sent_socket_eof= TRUE;
259 raw_check_close(raw);
260 }
261
5e1a8e27 262 return;
263}
264
125105d1 265/*
266 * Return a list of the special codes that make sense in this
267 * protocol.
268 */
269static const struct telnet_special *raw_get_specials(void *handle)
270{
271 return NULL;
272}
273
6226c939 274static int raw_connected(void *handle)
32874aea 275{
51470298 276 Raw raw = (Raw) handle;
6226c939 277 return raw->s != NULL;
32874aea 278}
8ccc75b0 279
51470298 280static int raw_sendok(void *handle)
32874aea 281{
282 return 1;
283}
4017be6d 284
51470298 285static void raw_unthrottle(void *handle, int backlog)
5471d09a 286{
51470298 287 Raw raw = (Raw) handle;
288 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
5471d09a 289}
290
51470298 291static int raw_ldisc(void *handle, int option)
32874aea 292{
0965bee0 293 if (option == LD_EDIT || option == LD_ECHO)
32874aea 294 return 1;
0965bee0 295 return 0;
296}
297
b9d7bcad 298static void raw_provide_ldisc(void *handle, void *ldisc)
299{
300 /* This is a stub. */
301}
302
a8327734 303static void raw_provide_logctx(void *handle, void *logctx)
304{
305 /* This is a stub. */
306}
307
51470298 308static int raw_exitcode(void *handle)
d8d6c7e5 309{
0da1a790 310 Raw raw = (Raw) handle;
311 if (raw->s != NULL)
312 return -1; /* still connected */
96b720b3 313 else if (raw->closed_on_socket_error)
314 return INT_MAX; /* a socket error counts as an unclean exit */
0da1a790 315 else
316 /* Exit codes are a meaningless concept in the Raw protocol */
317 return 0;
d8d6c7e5 318}
319
f89c3294 320/*
321 * cfg_info for Raw does nothing at all.
322 */
323static int raw_cfg_info(void *handle)
324{
325 return 0;
326}
327
5e1a8e27 328Backend raw_backend = {
329 raw_init,
fabd1805 330 raw_free,
86916870 331 raw_reconfig,
5e1a8e27 332 raw_send,
5471d09a 333 raw_sendbuffer,
5e1a8e27 334 raw_size,
4017be6d 335 raw_special,
125105d1 336 raw_get_specials,
6226c939 337 raw_connected,
d8d6c7e5 338 raw_exitcode,
97db3be4 339 raw_sendok,
0965bee0 340 raw_ldisc,
b9d7bcad 341 raw_provide_ldisc,
a8327734 342 raw_provide_logctx,
5471d09a 343 raw_unthrottle,
f89c3294 344 raw_cfg_info,
9e164d82 345 "raw",
346 PROT_RAW,
a4451dd1 347 0
5e1a8e27 348};