Fixed issue #2495: "Show Reflog" dialog shows empty action for "push" entries
[TortoiseGit.git] / src / TortoisePlink / RLOGIN.C
blobc28f2c762d510e4b0070d15c1e9466fd2ecab179
1 /*\r
2  * Rlogin backend.\r
3  */\r
4 \r
5 #include <stdio.h>\r
6 #include <stdlib.h>\r
7 #include <limits.h>\r
8 #include <ctype.h>\r
9 \r
10 #include "putty.h"\r
12 #ifndef FALSE\r
13 #define FALSE 0\r
14 #endif\r
15 #ifndef TRUE\r
16 #define TRUE 1\r
17 #endif\r
19 #define RLOGIN_MAX_BACKLOG 4096\r
21 typedef struct rlogin_tag {\r
22     const struct plug_function_table *fn;\r
23     /* the above field _must_ be first in the structure */\r
25     Socket s;\r
26     int closed_on_socket_error;\r
27     int bufsize;\r
28     int firstbyte;\r
29     int cansize;\r
30     int term_width, term_height;\r
31     void *frontend;\r
33     Conf *conf;\r
35     /* In case we need to read a username from the terminal before starting */\r
36     prompts_t *prompt;\r
37 } *Rlogin;\r
39 static void rlogin_size(void *handle, int width, int height);\r
41 static void c_write(Rlogin rlogin, char *buf, int len)\r
42 {\r
43     int backlog = from_backend(rlogin->frontend, 0, buf, len);\r
44     sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);\r
45 }\r
47 static void rlogin_log(Plug plug, int type, SockAddr addr, int port,\r
48                        const char *error_msg, int error_code)\r
49 {\r
50     Rlogin rlogin = (Rlogin) plug;\r
51     char addrbuf[256], *msg;\r
53     sk_getaddr(addr, addrbuf, lenof(addrbuf));\r
55     if (type == 0)\r
56         msg = dupprintf("Connecting to %s port %d", addrbuf, port);\r
57     else\r
58         msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);\r
60     logevent(rlogin->frontend, msg);\r
61     sfree(msg);\r
62 }\r
64 static int rlogin_closing(Plug plug, const char *error_msg, int error_code,\r
65                           int calling_back)\r
66 {\r
67     Rlogin rlogin = (Rlogin) plug;\r
69     /*\r
70      * We don't implement independent EOF in each direction for Telnet\r
71      * connections; as soon as we get word that the remote side has\r
72      * sent us EOF, we wind up the whole connection.\r
73      */\r
75     if (rlogin->s) {\r
76         sk_close(rlogin->s);\r
77         rlogin->s = NULL;\r
78         if (error_msg)\r
79             rlogin->closed_on_socket_error = TRUE;\r
80         notify_remote_exit(rlogin->frontend);\r
81     }\r
82     if (error_msg) {\r
83         /* A socket error has occurred. */\r
84         logevent(rlogin->frontend, error_msg);\r
85         connection_fatal(rlogin->frontend, "%s", error_msg);\r
86     }                                  /* Otherwise, the remote side closed the connection normally. */\r
87     return 0;\r
88 }\r
90 static int rlogin_receive(Plug plug, int urgent, char *data, int len)\r
91 {\r
92     Rlogin rlogin = (Rlogin) plug;\r
93     if (urgent == 2) {\r
94         char c;\r
96         c = *data++;\r
97         len--;\r
98         if (c == '\x80') {\r
99             rlogin->cansize = 1;\r
100             rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);\r
101         }\r
102         /*\r
103          * We should flush everything (aka Telnet SYNCH) if we see\r
104          * 0x02, and we should turn off and on _local_ flow control\r
105          * on 0x10 and 0x20 respectively. I'm not convinced it's\r
106          * worth it...\r
107          */\r
108     } else {\r
109         /*\r
110          * Main rlogin protocol. This is really simple: the first\r
111          * byte is expected to be NULL and is ignored, and the rest\r
112          * is printed.\r
113          */\r
114         if (rlogin->firstbyte) {\r
115             if (data[0] == '\0') {\r
116                 data++;\r
117                 len--;\r
118             }\r
119             rlogin->firstbyte = 0;\r
120         }\r
121         if (len > 0)\r
122             c_write(rlogin, data, len);\r
123     }\r
124     return 1;\r
127 static void rlogin_sent(Plug plug, int bufsize)\r
129     Rlogin rlogin = (Rlogin) plug;\r
130     rlogin->bufsize = bufsize;\r
133 static void rlogin_startup(Rlogin rlogin, const char *ruser)\r
135     char z = 0;\r
136     char *p;\r
138     sk_write(rlogin->s, &z, 1);\r
139     p = conf_get_str(rlogin->conf, CONF_localusername);\r
140     sk_write(rlogin->s, p, strlen(p));\r
141     sk_write(rlogin->s, &z, 1);\r
142     sk_write(rlogin->s, ruser, strlen(ruser));\r
143     sk_write(rlogin->s, &z, 1);\r
144     p = conf_get_str(rlogin->conf, CONF_termtype);\r
145     sk_write(rlogin->s, p, strlen(p));\r
146     sk_write(rlogin->s, "/", 1);\r
147     p = conf_get_str(rlogin->conf, CONF_termspeed);\r
148     sk_write(rlogin->s, p, strspn(p, "0123456789"));\r
149     rlogin->bufsize = sk_write(rlogin->s, &z, 1);\r
151     rlogin->prompt = NULL;\r
154 /*\r
155  * Called to set up the rlogin connection.\r
156  * \r
157  * Returns an error message, or NULL on success.\r
158  *\r
159  * Also places the canonical host name into `realhost'. It must be\r
160  * freed by the caller.\r
161  */\r
162 static const char *rlogin_init(void *frontend_handle, void **backend_handle,\r
163                                Conf *conf,\r
164                                char *host, int port, char **realhost,\r
165                                int nodelay, int keepalive)\r
167     static const struct plug_function_table fn_table = {\r
168         rlogin_log,\r
169         rlogin_closing,\r
170         rlogin_receive,\r
171         rlogin_sent\r
172     };\r
173     SockAddr addr;\r
174     const char *err;\r
175     Rlogin rlogin;\r
176     char *ruser;\r
177     int addressfamily;\r
178     char *loghost;\r
180     rlogin = snew(struct rlogin_tag);\r
181     rlogin->fn = &fn_table;\r
182     rlogin->s = NULL;\r
183     rlogin->closed_on_socket_error = FALSE;\r
184     rlogin->frontend = frontend_handle;\r
185     rlogin->term_width = conf_get_int(conf, CONF_width);\r
186     rlogin->term_height = conf_get_int(conf, CONF_height);\r
187     rlogin->firstbyte = 1;\r
188     rlogin->cansize = 0;\r
189     rlogin->prompt = NULL;\r
190     rlogin->conf = conf_copy(conf);\r
191     *backend_handle = rlogin;\r
193     addressfamily = conf_get_int(conf, CONF_addressfamily);\r
194     /*\r
195      * Try to find host.\r
196      */\r
197     {\r
198         char *buf;\r
199         buf = dupprintf("Looking up host \"%s\"%s", host,\r
200                         (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :\r
201                          (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :\r
202                           "")));\r
203         logevent(rlogin->frontend, buf);\r
204         sfree(buf);\r
205     }\r
206     addr = name_lookup(host, port, realhost, conf, addressfamily);\r
207     if ((err = sk_addr_error(addr)) != NULL) {\r
208         sk_addr_free(addr);\r
209         return err;\r
210     }\r
212     if (port < 0)\r
213         port = 513;                    /* default rlogin port */\r
215     /*\r
216      * Open socket.\r
217      */\r
218     rlogin->s = new_connection(addr, *realhost, port, 1, 0,\r
219                                nodelay, keepalive, (Plug) rlogin, conf);\r
220     if ((err = sk_socket_error(rlogin->s)) != NULL)\r
221         return err;\r
223     loghost = conf_get_str(conf, CONF_loghost);\r
224     if (*loghost) {\r
225         char *colon;\r
227         sfree(*realhost);\r
228         *realhost = dupstr(loghost);\r
230         colon = host_strrchr(*realhost, ':');\r
231         if (colon)\r
232             *colon++ = '\0';\r
233     }\r
235     /*\r
236      * Send local username, remote username, terminal type and\r
237      * terminal speed - unless we don't have the remote username yet,\r
238      * in which case we prompt for it and may end up deferring doing\r
239      * anything else until the local prompt mechanism returns.\r
240      */\r
241     if ((ruser = get_remote_username(conf)) != NULL) {\r
242         rlogin_startup(rlogin, ruser);\r
243         sfree(ruser);\r
244     } else {\r
245         int ret;\r
247         rlogin->prompt = new_prompts(rlogin->frontend);\r
248         rlogin->prompt->to_server = TRUE;\r
249         rlogin->prompt->name = dupstr("Rlogin login name");\r
250         add_prompt(rlogin->prompt, dupstr("rlogin username: "), TRUE); \r
251         ret = get_userpass_input(rlogin->prompt, NULL, 0);\r
252         if (ret >= 0) {\r
253             rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);\r
254         }\r
255     }\r
257     return NULL;\r
260 static void rlogin_free(void *handle)\r
262     Rlogin rlogin = (Rlogin) handle;\r
264     if (rlogin->prompt)\r
265         free_prompts(rlogin->prompt);\r
266     if (rlogin->s)\r
267         sk_close(rlogin->s);\r
268     conf_free(rlogin->conf);\r
269     sfree(rlogin);\r
272 /*\r
273  * Stub routine (we don't have any need to reconfigure this backend).\r
274  */\r
275 static void rlogin_reconfig(void *handle, Conf *conf)\r
279 /*\r
280  * Called to send data down the rlogin connection.\r
281  */\r
282 static int rlogin_send(void *handle, char *buf, int len)\r
284     Rlogin rlogin = (Rlogin) handle;\r
286     if (rlogin->s == NULL)\r
287         return 0;\r
289     if (rlogin->prompt) {\r
290         /*\r
291          * We're still prompting for a username, and aren't talking\r
292          * directly to the network connection yet.\r
293          */\r
294         int ret = get_userpass_input(rlogin->prompt,\r
295                                      (unsigned char *)buf, len);\r
296         if (ret >= 0) {\r
297             rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);\r
298             /* that nulls out rlogin->prompt, so then we'll start sending\r
299              * data down the wire in the obvious way */\r
300         }\r
301     } else {\r
302         rlogin->bufsize = sk_write(rlogin->s, buf, len);\r
303     }\r
305     return rlogin->bufsize;\r
308 /*\r
309  * Called to query the current socket sendability status.\r
310  */\r
311 static int rlogin_sendbuffer(void *handle)\r
313     Rlogin rlogin = (Rlogin) handle;\r
314     return rlogin->bufsize;\r
317 /*\r
318  * Called to set the size of the window\r
319  */\r
320 static void rlogin_size(void *handle, int width, int height)\r
322     Rlogin rlogin = (Rlogin) handle;\r
323     char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };\r
325     rlogin->term_width = width;\r
326     rlogin->term_height = height;\r
328     if (rlogin->s == NULL || !rlogin->cansize)\r
329         return;\r
331     b[6] = rlogin->term_width >> 8;\r
332     b[7] = rlogin->term_width & 0xFF;\r
333     b[4] = rlogin->term_height >> 8;\r
334     b[5] = rlogin->term_height & 0xFF;\r
335     rlogin->bufsize = sk_write(rlogin->s, b, 12);\r
336     return;\r
339 /*\r
340  * Send rlogin special codes.\r
341  */\r
342 static void rlogin_special(void *handle, Telnet_Special code)\r
344     /* Do nothing! */\r
345     return;\r
348 /*\r
349  * Return a list of the special codes that make sense in this\r
350  * protocol.\r
351  */\r
352 static const struct telnet_special *rlogin_get_specials(void *handle)\r
354     return NULL;\r
357 static int rlogin_connected(void *handle)\r
359     Rlogin rlogin = (Rlogin) handle;\r
360     return rlogin->s != NULL;\r
363 static int rlogin_sendok(void *handle)\r
365     /* Rlogin rlogin = (Rlogin) handle; */\r
366     return 1;\r
369 static void rlogin_unthrottle(void *handle, int backlog)\r
371     Rlogin rlogin = (Rlogin) handle;\r
372     sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);\r
375 static int rlogin_ldisc(void *handle, int option)\r
377     /* Rlogin rlogin = (Rlogin) handle; */\r
378     return 0;\r
381 static void rlogin_provide_ldisc(void *handle, void *ldisc)\r
383     /* This is a stub. */\r
386 static void rlogin_provide_logctx(void *handle, void *logctx)\r
388     /* This is a stub. */\r
391 static int rlogin_exitcode(void *handle)\r
393     Rlogin rlogin = (Rlogin) handle;\r
394     if (rlogin->s != NULL)\r
395         return -1;                     /* still connected */\r
396     else if (rlogin->closed_on_socket_error)\r
397         return INT_MAX;     /* a socket error counts as an unclean exit */\r
398     else\r
399         /* If we ever implement RSH, we'll probably need to do this properly */\r
400         return 0;\r
403 /*\r
404  * cfg_info for rlogin does nothing at all.\r
405  */\r
406 static int rlogin_cfg_info(void *handle)\r
408     return 0;\r
411 Backend rlogin_backend = {\r
412     rlogin_init,\r
413     rlogin_free,\r
414     rlogin_reconfig,\r
415     rlogin_send,\r
416     rlogin_sendbuffer,\r
417     rlogin_size,\r
418     rlogin_special,\r
419     rlogin_get_specials,\r
420     rlogin_connected,\r
421     rlogin_exitcode,\r
422     rlogin_sendok,\r
423     rlogin_ldisc,\r
424     rlogin_provide_ldisc,\r
425     rlogin_provide_logctx,\r
426     rlogin_unthrottle,\r
427     rlogin_cfg_info,\r
428     "rlogin",\r
429     PROT_RLOGIN,\r
430     513\r
431 };\r