Yet another possible segfault path in the backends fixed. I don't
[u/mdw/putty] / rlogin.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5
6 #include "putty.h"
7
8 #ifndef FALSE
9 #define FALSE 0
10 #endif
11 #ifndef TRUE
12 #define TRUE 1
13 #endif
14
15 static Socket s = NULL;
16
17 static void rlogin_size(void);
18
19 static void c_write(char *buf, int len)
20 {
21 from_backend(0, buf, len);
22 }
23
24 static int rlogin_closing(Plug plug, char *error_msg, int error_code,
25 int calling_back)
26 {
27 if (s) {
28 sk_close(s);
29 s = NULL;
30 }
31 if (error_msg) {
32 /* A socket error has occurred. */
33 connection_fatal(error_msg);
34 } /* Otherwise, the remote side closed the connection normally. */
35 return 0;
36 }
37
38 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
39 {
40 if (urgent == 2) {
41 char c;
42
43 c = *data++;
44 len--;
45 if (c == '\x80')
46 rlogin_size();
47 /*
48 * We should flush everything (aka Telnet SYNCH) if we see
49 * 0x02, and we should turn off and on _local_ flow control
50 * on 0x10 and 0x20 respectively. I'm not convinced it's
51 * worth it...
52 */
53 } else {
54 /*
55 * Main rlogin protocol. This is really simple: the first
56 * byte is expected to be NULL and is ignored, and the rest
57 * is printed.
58 */
59 static int firstbyte = 1;
60 if (firstbyte) {
61 if (data[0] == '\0') {
62 data++;
63 len--;
64 }
65 firstbyte = 0;
66 }
67 c_write(data, len);
68 }
69 return 1;
70 }
71
72 /*
73 * Called to set up the rlogin connection.
74 *
75 * Returns an error message, or NULL on success.
76 *
77 * Also places the canonical host name into `realhost'. It must be
78 * freed by the caller.
79 */
80 static char *rlogin_init(char *host, int port, char **realhost)
81 {
82 static struct plug_function_table fn_table = {
83 rlogin_closing,
84 rlogin_receive
85 }, *fn_table_ptr = &fn_table;
86
87 SockAddr addr;
88 char *err;
89
90 /*
91 * Try to find host.
92 */
93 addr = sk_namelookup(host, realhost);
94 if ((err = sk_addr_error(addr)))
95 return err;
96
97 if (port < 0)
98 port = 513; /* default rlogin port */
99
100 /*
101 * Open socket.
102 */
103 s = sk_new(addr, port, 1, 0, &fn_table_ptr);
104 if ((err = sk_socket_error(s)))
105 return err;
106
107 sk_addr_free(addr);
108
109 /*
110 * Send local username, remote username, terminal/speed
111 */
112
113 {
114 char z = 0;
115 char *p;
116 sk_write(s, &z, 1);
117 sk_write(s, cfg.localusername, strlen(cfg.localusername));
118 sk_write(s, &z, 1);
119 sk_write(s, cfg.username, strlen(cfg.username));
120 sk_write(s, &z, 1);
121 sk_write(s, cfg.termtype, strlen(cfg.termtype));
122 sk_write(s, "/", 1);
123 for (p = cfg.termspeed; isdigit(*p); p++);
124 sk_write(s, cfg.termspeed, p - cfg.termspeed);
125 sk_write(s, &z, 1);
126 }
127
128 return NULL;
129 }
130
131 /*
132 * Called to send data down the rlogin connection.
133 */
134 static void rlogin_send(char *buf, int len)
135 {
136
137 if (s == NULL)
138 return;
139
140 sk_write(s, buf, len);
141 }
142
143 /*
144 * Called to set the size of the window
145 */
146 static void rlogin_size(void)
147 {
148 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
149
150 b[6] = cols >> 8;
151 b[7] = cols & 0xFF;
152 b[4] = rows >> 8;
153 b[5] = rows & 0xFF;
154 sk_write(s, b, 12);
155 return;
156 }
157
158 /*
159 * Send rlogin special codes.
160 */
161 static void rlogin_special(Telnet_Special code)
162 {
163 /* Do nothing! */
164 return;
165 }
166
167 static Socket rlogin_socket(void)
168 {
169 return s;
170 }
171
172 static int rlogin_sendok(void)
173 {
174 return 1;
175 }
176
177 static int rlogin_ldisc(int option)
178 {
179 return 0;
180 }
181
182 Backend rlogin_backend = {
183 rlogin_init,
184 rlogin_send,
185 rlogin_size,
186 rlogin_special,
187 rlogin_socket,
188 rlogin_sendok,
189 rlogin_ldisc,
190 1
191 };