c91409da |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
b6c680d4 |
3 | #include <ctype.h> |
c91409da |
4 | |
5 | #include "putty.h" |
6 | |
7 | #ifndef FALSE |
8 | #define FALSE 0 |
9 | #endif |
10 | #ifndef TRUE |
11 | #define TRUE 1 |
12 | #endif |
13 | |
5471d09a |
14 | #define RLOGIN_MAX_BACKLOG 4096 |
15 | |
51470298 |
16 | typedef struct rlogin_tag { |
17 | const struct plug_function_table *fn; |
18 | /* the above field _must_ be first in the structure */ |
c91409da |
19 | |
51470298 |
20 | Socket s; |
21 | int bufsize; |
22 | int term_width, term_height; |
23 | void *frontend; |
24 | } *Rlogin; |
c91409da |
25 | |
51470298 |
26 | static void rlogin_size(void *handle, int width, int height); |
27 | |
28 | static void c_write(Rlogin rlogin, char *buf, int len) |
32874aea |
29 | { |
51470298 |
30 | int backlog = from_backend(rlogin->frontend, 0, buf, len); |
31 | sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG); |
c91409da |
32 | } |
33 | |
32874aea |
34 | static int rlogin_closing(Plug plug, char *error_msg, int error_code, |
35 | int calling_back) |
36 | { |
51470298 |
37 | Rlogin rlogin = (Rlogin) plug; |
38 | if (rlogin->s) { |
39 | sk_close(rlogin->s); |
40 | rlogin->s = NULL; |
f3ab576e |
41 | } |
7e78000d |
42 | if (error_msg) { |
32874aea |
43 | /* A socket error has occurred. */ |
a8327734 |
44 | logevent(rlogin->frontend, error_msg); |
247308b5 |
45 | connection_fatal("%s", error_msg); |
32874aea |
46 | } /* Otherwise, the remote side closed the connection normally. */ |
7e78000d |
47 | return 0; |
48 | } |
49 | |
32874aea |
50 | static int rlogin_receive(Plug plug, int urgent, char *data, int len) |
51 | { |
51470298 |
52 | Rlogin rlogin = (Rlogin) plug; |
c91409da |
53 | if (urgent == 2) { |
32874aea |
54 | char c; |
55 | |
56 | c = *data++; |
57 | len--; |
58 | if (c == '\x80') |
51470298 |
59 | rlogin_size(rlogin, rlogin->term_width, rlogin->term_height); |
32874aea |
60 | /* |
61 | * We should flush everything (aka Telnet SYNCH) if we see |
62 | * 0x02, and we should turn off and on _local_ flow control |
63 | * on 0x10 and 0x20 respectively. I'm not convinced it's |
64 | * worth it... |
65 | */ |
8a5fb9e3 |
66 | } else { |
32874aea |
67 | /* |
68 | * Main rlogin protocol. This is really simple: the first |
69 | * byte is expected to be NULL and is ignored, and the rest |
70 | * is printed. |
71 | */ |
72 | static int firstbyte = 1; |
73 | if (firstbyte) { |
74 | if (data[0] == '\0') { |
75 | data++; |
76 | len--; |
77 | } |
78 | firstbyte = 0; |
79 | } |
2b0c045b |
80 | if (len > 0) |
51470298 |
81 | c_write(rlogin, data, len); |
c91409da |
82 | } |
c91409da |
83 | return 1; |
84 | } |
85 | |
3ad9d396 |
86 | static void rlogin_sent(Plug plug, int bufsize) |
87 | { |
51470298 |
88 | Rlogin rlogin = (Rlogin) plug; |
89 | rlogin->bufsize = bufsize; |
3ad9d396 |
90 | } |
91 | |
c91409da |
92 | /* |
93 | * Called to set up the rlogin connection. |
94 | * |
95 | * Returns an error message, or NULL on success. |
96 | * |
6e1ebb76 |
97 | * Also places the canonical host name into `realhost'. It must be |
98 | * freed by the caller. |
c91409da |
99 | */ |
51470298 |
100 | static char *rlogin_init(void *frontend_handle, void **backend_handle, |
86916870 |
101 | Config *cfg, |
887035a5 |
102 | char *host, int port, char **realhost, int nodelay) |
32874aea |
103 | { |
51470298 |
104 | static const struct plug_function_table fn_table = { |
7e78000d |
105 | rlogin_closing, |
3ad9d396 |
106 | rlogin_receive, |
107 | rlogin_sent |
51470298 |
108 | }; |
c91409da |
109 | SockAddr addr; |
110 | char *err; |
51470298 |
111 | Rlogin rlogin; |
c91409da |
112 | |
51470298 |
113 | rlogin = smalloc(sizeof(*rlogin)); |
114 | rlogin->fn = &fn_table; |
115 | rlogin->s = NULL; |
116 | rlogin->frontend = frontend_handle; |
86916870 |
117 | rlogin->term_width = cfg->width; |
118 | rlogin->term_height = cfg->height; |
51470298 |
119 | *backend_handle = rlogin; |
887035a5 |
120 | |
c91409da |
121 | /* |
122 | * Try to find host. |
123 | */ |
3ad9d396 |
124 | { |
57356d63 |
125 | char *buf; |
126 | buf = dupprintf("Looking up host \"%s\"", host); |
a8327734 |
127 | logevent(rlogin->frontend, buf); |
57356d63 |
128 | sfree(buf); |
3ad9d396 |
129 | } |
b7a189f3 |
130 | addr = name_lookup(host, port, realhost); |
6f1e7b78 |
131 | if ((err = sk_addr_error(addr)) != NULL) |
c91409da |
132 | return err; |
133 | |
134 | if (port < 0) |
135 | port = 513; /* default rlogin port */ |
136 | |
137 | /* |
138 | * Open socket. |
139 | */ |
3ad9d396 |
140 | { |
57356d63 |
141 | char *buf, addrbuf[100]; |
3ad9d396 |
142 | sk_getaddr(addr, addrbuf, 100); |
57356d63 |
143 | buf = dupprintf("Connecting to %s port %d", addrbuf, port); |
a8327734 |
144 | logevent(rlogin->frontend, buf); |
57356d63 |
145 | sfree(buf); |
3ad9d396 |
146 | } |
51470298 |
147 | rlogin->s = new_connection(addr, *realhost, port, 1, 0, |
148 | nodelay, (Plug) rlogin); |
6f1e7b78 |
149 | if ((err = sk_socket_error(rlogin->s)) != NULL) |
c91409da |
150 | return err; |
151 | |
152 | sk_addr_free(addr); |
153 | |
154 | /* |
155 | * Send local username, remote username, terminal/speed |
156 | */ |
157 | |
158 | { |
32874aea |
159 | char z = 0; |
160 | char *p; |
51470298 |
161 | sk_write(rlogin->s, &z, 1); |
86916870 |
162 | sk_write(rlogin->s, cfg->localusername, |
163 | strlen(cfg->localusername)); |
51470298 |
164 | sk_write(rlogin->s, &z, 1); |
86916870 |
165 | sk_write(rlogin->s, cfg->username, |
166 | strlen(cfg->username)); |
51470298 |
167 | sk_write(rlogin->s, &z, 1); |
86916870 |
168 | sk_write(rlogin->s, cfg->termtype, |
169 | strlen(cfg->termtype)); |
51470298 |
170 | sk_write(rlogin->s, "/", 1); |
86916870 |
171 | for (p = cfg->termspeed; isdigit(*p); p++) continue; |
172 | sk_write(rlogin->s, cfg->termspeed, p - cfg->termspeed); |
51470298 |
173 | rlogin->bufsize = sk_write(rlogin->s, &z, 1); |
c91409da |
174 | } |
175 | |
c91409da |
176 | return NULL; |
177 | } |
178 | |
179 | /* |
86916870 |
180 | * Stub routine (we don't have any need to reconfigure this backend). |
181 | */ |
182 | static void rlogin_reconfig(void *handle, Config *cfg) |
183 | { |
184 | } |
185 | |
186 | /* |
c91409da |
187 | * Called to send data down the rlogin connection. |
188 | */ |
51470298 |
189 | static int rlogin_send(void *handle, char *buf, int len) |
32874aea |
190 | { |
51470298 |
191 | Rlogin rlogin = (Rlogin) handle; |
192 | |
193 | if (rlogin->s == NULL) |
b5a460cd |
194 | return 0; |
c91409da |
195 | |
51470298 |
196 | rlogin->bufsize = sk_write(rlogin->s, buf, len); |
5471d09a |
197 | |
51470298 |
198 | return rlogin->bufsize; |
5471d09a |
199 | } |
200 | |
201 | /* |
202 | * Called to query the current socket sendability status. |
203 | */ |
51470298 |
204 | static int rlogin_sendbuffer(void *handle) |
5471d09a |
205 | { |
51470298 |
206 | Rlogin rlogin = (Rlogin) handle; |
207 | return rlogin->bufsize; |
c91409da |
208 | } |
209 | |
210 | /* |
211 | * Called to set the size of the window |
212 | */ |
51470298 |
213 | static void rlogin_size(void *handle, int width, int height) |
32874aea |
214 | { |
51470298 |
215 | Rlogin rlogin = (Rlogin) handle; |
e418595a |
216 | char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 }; |
c91409da |
217 | |
51470298 |
218 | rlogin->term_width = width; |
219 | rlogin->term_height = height; |
f278d6f8 |
220 | |
51470298 |
221 | if (rlogin->s == NULL) |
cf293dd4 |
222 | return; |
f278d6f8 |
223 | |
51470298 |
224 | b[6] = rlogin->term_width >> 8; |
225 | b[7] = rlogin->term_width & 0xFF; |
226 | b[4] = rlogin->term_height >> 8; |
227 | b[5] = rlogin->term_height & 0xFF; |
228 | rlogin->bufsize = sk_write(rlogin->s, b, 12); |
c91409da |
229 | return; |
230 | } |
231 | |
232 | /* |
233 | * Send rlogin special codes. |
234 | */ |
51470298 |
235 | static void rlogin_special(void *handle, Telnet_Special code) |
32874aea |
236 | { |
c91409da |
237 | /* Do nothing! */ |
238 | return; |
239 | } |
240 | |
51470298 |
241 | static Socket rlogin_socket(void *handle) |
32874aea |
242 | { |
51470298 |
243 | Rlogin rlogin = (Rlogin) handle; |
244 | return rlogin->s; |
32874aea |
245 | } |
c91409da |
246 | |
51470298 |
247 | static int rlogin_sendok(void *handle) |
32874aea |
248 | { |
68a49acb |
249 | /* Rlogin rlogin = (Rlogin) handle; */ |
32874aea |
250 | return 1; |
251 | } |
c91409da |
252 | |
51470298 |
253 | static void rlogin_unthrottle(void *handle, int backlog) |
5471d09a |
254 | { |
51470298 |
255 | Rlogin rlogin = (Rlogin) handle; |
256 | sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG); |
5471d09a |
257 | } |
258 | |
51470298 |
259 | static int rlogin_ldisc(void *handle, int option) |
32874aea |
260 | { |
68a49acb |
261 | /* Rlogin rlogin = (Rlogin) handle; */ |
0965bee0 |
262 | return 0; |
263 | } |
264 | |
b9d7bcad |
265 | static void rlogin_provide_ldisc(void *handle, void *ldisc) |
266 | { |
267 | /* This is a stub. */ |
268 | } |
269 | |
a8327734 |
270 | static void rlogin_provide_logctx(void *handle, void *logctx) |
271 | { |
272 | /* This is a stub. */ |
273 | } |
274 | |
51470298 |
275 | static int rlogin_exitcode(void *handle) |
d8d6c7e5 |
276 | { |
68a49acb |
277 | /* Rlogin rlogin = (Rlogin) handle; */ |
d8d6c7e5 |
278 | /* If we ever implement RSH, we'll probably need to do this properly */ |
279 | return 0; |
280 | } |
281 | |
c91409da |
282 | Backend rlogin_backend = { |
283 | rlogin_init, |
86916870 |
284 | rlogin_reconfig, |
c91409da |
285 | rlogin_send, |
5471d09a |
286 | rlogin_sendbuffer, |
c91409da |
287 | rlogin_size, |
288 | rlogin_special, |
289 | rlogin_socket, |
d8d6c7e5 |
290 | rlogin_exitcode, |
c91409da |
291 | rlogin_sendok, |
0965bee0 |
292 | rlogin_ldisc, |
b9d7bcad |
293 | rlogin_provide_ldisc, |
a8327734 |
294 | rlogin_provide_logctx, |
5471d09a |
295 | rlogin_unthrottle, |
c91409da |
296 | 1 |
297 | }; |