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