]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - putty.h
8277bbc2ec8916a2b11afcdabf6897faa7f6e17f
[PuTTY.git] / putty.h
1 #ifndef PUTTY_PUTTY_H
2 #define PUTTY_PUTTY_H
3
4 #include <stddef.h>                    /* for wchar_t */
5
6 /*
7  * Global variables. Most modules declare these `extern', but
8  * window.c will do `#define PUTTY_DO_GLOBALS' before including this
9  * module, and so will get them properly defined.
10  */
11 #ifndef GLOBAL
12 #ifdef PUTTY_DO_GLOBALS
13 #define GLOBAL
14 #else
15 #define GLOBAL extern
16 #endif
17 #endif
18
19 #ifndef DONE_TYPEDEFS
20 #define DONE_TYPEDEFS
21 typedef struct config_tag Config;
22 typedef struct backend_tag Backend;
23 typedef struct terminal_tag Terminal;
24 #endif
25
26 #include "puttyps.h"
27 #include "network.h"
28 #include "misc.h"
29
30 /*
31  * Fingerprints of the PGP master keys that can be used to establish a trust
32  * path between an executable and other files.
33  */
34 #define PGP_RSA_MASTER_KEY_FP \
35     "8F 15 97 DA 25 30 AB 0D  88 D1 92 54 11 CF 0C 4C"
36 #define PGP_DSA_MASTER_KEY_FP \
37     "313C 3E76 4B74 C2C5 F2AE  83A8 4F5E 6DF5 6A93 B34E"
38
39 /* Three attribute types: 
40  * The ATTRs (normal attributes) are stored with the characters in
41  * the main display arrays
42  *
43  * The TATTRs (temporary attributes) are generated on the fly, they
44  * can overlap with characters but not with normal attributes.
45  *
46  * The LATTRs (line attributes) are an entirely disjoint space of
47  * flags.
48  * 
49  * The DATTRs (display attributes) are internal to terminal.c (but
50  * defined here because their values have to match the others
51  * here); they reuse the TATTR_* space but are always masked off
52  * before sending to the front end.
53  *
54  * ATTR_INVALID is an illegal colour combination.
55  */
56
57 #define TATTR_ACTCURS       0x40000000UL      /* active cursor (block) */
58 #define TATTR_PASCURS       0x20000000UL      /* passive cursor (box) */
59 #define TATTR_RIGHTCURS     0x10000000UL      /* cursor-on-RHS */
60 #define TATTR_COMBINING     0x80000000UL      /* combining characters */
61
62 #define DATTR_STARTRUN      0x80000000UL   /* start of redraw run */
63
64 #define TDATTR_MASK         0xF0000000UL
65 #define TATTR_MASK (TDATTR_MASK)
66 #define DATTR_MASK (TDATTR_MASK)
67
68 #define LATTR_NORM   0x00000000UL
69 #define LATTR_WIDE   0x00000001UL
70 #define LATTR_TOP    0x00000002UL
71 #define LATTR_BOT    0x00000003UL
72 #define LATTR_MODE   0x00000003UL
73 #define LATTR_WRAPPED 0x00000010UL     /* this line wraps to next */
74 #define LATTR_WRAPPED2 0x00000020UL    /* with WRAPPED: CJK wide character
75                                           wrapped to next line, so last
76                                           single-width cell is empty */
77
78 #define ATTR_INVALID 0x03FFFFU
79
80 /* Like Linux use the F000 page for direct to font. */
81 #define CSET_OEMCP   0x0000F000UL      /* OEM Codepage DTF */
82 #define CSET_ACP     0x0000F100UL      /* Ansi Codepage DTF */
83
84 /* These are internal use overlapping with the UTF-16 surrogates */
85 #define CSET_ASCII   0x0000D800UL      /* normal ASCII charset ESC ( B */
86 #define CSET_LINEDRW 0x0000D900UL      /* line drawing charset ESC ( 0 */
87 #define CSET_SCOACS  0x0000DA00UL      /* SCO Alternate charset */
88 #define CSET_GBCHR   0x0000DB00UL      /* UK variant   charset ESC ( A */
89 #define CSET_MASK    0xFFFFFF00UL      /* Character set mask */
90
91 #define DIRECT_CHAR(c) ((c&0xFFFFFC00)==0xD800)
92 #define DIRECT_FONT(c) ((c&0xFFFFFE00)==0xF000)
93
94 #define UCSERR       (CSET_LINEDRW|'a') /* UCS Format error character. */
95 /*
96  * UCSWIDE is a special value used in the terminal data to signify
97  * the character cell containing the right-hand half of a CJK wide
98  * character. We use 0xDFFF because it's part of the surrogate
99  * range and hence won't be used for anything else (it's impossible
100  * to input it via UTF-8 because our UTF-8 decoder correctly
101  * rejects surrogates).
102  */
103 #define UCSWIDE      0xDFFF
104
105 #define ATTR_NARROW  0x800000U
106 #define ATTR_WIDE    0x400000U
107 #define ATTR_BOLD    0x040000U
108 #define ATTR_UNDER   0x080000U
109 #define ATTR_REVERSE 0x100000U
110 #define ATTR_BLINK   0x200000U
111 #define ATTR_FGMASK  0x0001FFU
112 #define ATTR_BGMASK  0x03FE00U
113 #define ATTR_COLOURS 0x03FFFFU
114 #define ATTR_FGSHIFT 0
115 #define ATTR_BGSHIFT 9
116
117 /*
118  * The definitive list of colour numbers stored in terminal
119  * attribute words is kept here. It is:
120  * 
121  *  - 0-7 are ANSI colours (KRGYBMCW).
122  *  - 8-15 are the bold versions of those colours.
123  *  - 16-255 are the remains of the xterm 256-colour mode (a
124  *    216-colour cube with R at most significant and B at least,
125  *    followed by a uniform series of grey shades running between
126  *    black and white but not including either on grounds of
127  *    redundancy).
128  *  - 256 is default foreground
129  *  - 257 is default bold foreground
130  *  - 258 is default background
131  *  - 259 is default bold background
132  *  - 260 is cursor foreground
133  *  - 261 is cursor background
134  */
135
136 #define ATTR_DEFFG   (256 << ATTR_FGSHIFT)
137 #define ATTR_DEFBG   (258 << ATTR_BGSHIFT)
138 #define ATTR_DEFAULT (ATTR_DEFFG | ATTR_DEFBG)
139
140 struct sesslist {
141     int nsessions;
142     char **sessions;
143     char *buffer;                      /* so memory can be freed later */
144 };
145
146 struct unicode_data {
147     char **uni_tbl;
148     int dbcs_screenfont;
149     int font_codepage;
150     int line_codepage;
151     wchar_t unitab_scoacs[256];
152     wchar_t unitab_line[256];
153     wchar_t unitab_font[256];
154     wchar_t unitab_xterm[256];
155     wchar_t unitab_oemcp[256];
156     unsigned char unitab_ctrl[256];
157 };
158
159 #define LGXF_OVR  1                    /* existing logfile overwrite */
160 #define LGXF_APN  0                    /* existing logfile append */
161 #define LGXF_ASK -1                    /* existing logfile ask */
162 #define LGTYP_NONE  0                  /* logmode: no logging */
163 #define LGTYP_ASCII 1                  /* logmode: pure ascii */
164 #define LGTYP_DEBUG 2                  /* logmode: all chars of traffic */
165 #define LGTYP_PACKETS 3                /* logmode: SSH data packets */
166
167 typedef enum {
168     /* Actual special commands. Originally Telnet, but some codes have
169      * been re-used for similar specials in other protocols. */
170     TS_AYT, TS_BRK, TS_SYNCH, TS_EC, TS_EL, TS_GA, TS_NOP, TS_ABORT,
171     TS_AO, TS_IP, TS_SUSP, TS_EOR, TS_EOF, TS_LECHO, TS_RECHO, TS_PING,
172     TS_EOL,
173     /* Special command for SSH. */
174     TS_REKEY,
175     /* POSIX-style signals. (not Telnet) */
176     TS_SIGABRT, TS_SIGALRM, TS_SIGFPE,  TS_SIGHUP,  TS_SIGILL,
177     TS_SIGINT,  TS_SIGKILL, TS_SIGPIPE, TS_SIGQUIT, TS_SIGSEGV,
178     TS_SIGTERM, TS_SIGUSR1, TS_SIGUSR2,
179     /* Pseudo-specials used for constructing the specials menu. */
180     TS_SEP,         /* Separator */
181     TS_SUBMENU,     /* Start a new submenu with specified name */
182     TS_EXITMENU     /* Exit current submenu or end of specials */
183 } Telnet_Special;
184
185 struct telnet_special {
186     const char *name;
187     int code;
188 };
189
190 typedef enum {
191     MBT_NOTHING,
192     MBT_LEFT, MBT_MIDDLE, MBT_RIGHT,   /* `raw' button designations */
193     MBT_SELECT, MBT_EXTEND, MBT_PASTE, /* `cooked' button designations */
194     MBT_WHEEL_UP, MBT_WHEEL_DOWN       /* mouse wheel */
195 } Mouse_Button;
196
197 typedef enum {
198     MA_NOTHING, MA_CLICK, MA_2CLK, MA_3CLK, MA_DRAG, MA_RELEASE
199 } Mouse_Action;
200
201 /* Keyboard modifiers -- keys the user is actually holding down */
202
203 #define PKM_SHIFT       0x01
204 #define PKM_CONTROL     0x02
205 #define PKM_META        0x04
206 #define PKM_ALT         0x08
207
208 /* Keyboard flags that aren't really modifiers */
209 #define PKF_CAPSLOCK    0x10
210 #define PKF_NUMLOCK     0x20
211 #define PKF_REPEAT      0x40
212
213 /* Stand-alone keysyms for function keys */
214
215 typedef enum {
216     PK_NULL,            /* No symbol for this key */
217     /* Main keypad keys */
218     PK_ESCAPE, PK_TAB, PK_BACKSPACE, PK_RETURN, PK_COMPOSE,
219     /* Editing keys */
220     PK_HOME, PK_INSERT, PK_DELETE, PK_END, PK_PAGEUP, PK_PAGEDOWN,
221     /* Cursor keys */
222     PK_UP, PK_DOWN, PK_RIGHT, PK_LEFT, PK_REST,
223     /* Numeric keypad */                        /* Real one looks like: */
224     PK_PF1, PK_PF2, PK_PF3, PK_PF4,             /* PF1 PF2 PF3 PF4 */
225     PK_KPCOMMA, PK_KPMINUS, PK_KPDECIMAL,       /*  7   8   9   -  */
226     PK_KP0, PK_KP1, PK_KP2, PK_KP3, PK_KP4,     /*  4   5   6   ,  */
227     PK_KP5, PK_KP6, PK_KP7, PK_KP8, PK_KP9,     /*  1   2   3  en- */
228     PK_KPBIGPLUS, PK_KPENTER,                   /*    0     .  ter */
229     /* Top row */
230     PK_F1,  PK_F2,  PK_F3,  PK_F4,  PK_F5,
231     PK_F6,  PK_F7,  PK_F8,  PK_F9,  PK_F10,
232     PK_F11, PK_F12, PK_F13, PK_F14, PK_F15,
233     PK_F16, PK_F17, PK_F18, PK_F19, PK_F20,
234     PK_PAUSE
235 } Key_Sym;
236
237 #define PK_ISEDITING(k) ((k) >= PK_HOME && (k) <= PK_PAGEDOWN)
238 #define PK_ISCURSOR(k)  ((k) >= PK_UP && (k) <= PK_REST)
239 #define PK_ISKEYPAD(k)  ((k) >= PK_PF1 && (k) <= PK_KPENTER)
240 #define PK_ISFKEY(k)    ((k) >= PK_F1 && (k) <= PK_F20)
241
242 enum {
243     VT_XWINDOWS, VT_OEMANSI, VT_OEMONLY, VT_POORMAN, VT_UNICODE
244 };
245
246 enum {
247     /*
248      * SSH-2 key exchange algorithms
249      */
250     KEX_WARN,
251     KEX_DHGROUP1,
252     KEX_DHGROUP14,
253     KEX_DHGEX,
254     KEX_MAX
255 };
256
257 enum {
258     /*
259      * SSH ciphers (both SSH-1 and SSH-2)
260      */
261     CIPHER_WARN,                       /* pseudo 'cipher' */
262     CIPHER_3DES,
263     CIPHER_BLOWFISH,
264     CIPHER_AES,                        /* (SSH-2 only) */
265     CIPHER_DES,
266     CIPHER_ARCFOUR,
267     CIPHER_MAX                         /* no. ciphers (inc warn) */
268 };
269
270 enum {
271     /*
272      * Several different bits of the PuTTY configuration seem to be
273      * three-way settings whose values are `always yes', `always
274      * no', and `decide by some more complex automated means'. This
275      * is true of line discipline options (local echo and line
276      * editing), proxy DNS, Close On Exit, and SSH server bug
277      * workarounds. Accordingly I supply a single enum here to deal
278      * with them all.
279      */
280     FORCE_ON, FORCE_OFF, AUTO
281 };
282
283 enum {
284     /*
285      * Proxy types.
286      */
287     PROXY_NONE, PROXY_SOCKS4, PROXY_SOCKS5,
288     PROXY_HTTP, PROXY_TELNET, PROXY_CMD
289 };
290
291 enum {
292     /*
293      * Line discipline options which the backend might try to control.
294      */
295     LD_EDIT,                           /* local line editing */
296     LD_ECHO                            /* local echo */
297 };
298
299 enum {
300     /* Protocol back ends. (cfg.protocol) */
301     PROT_RAW, PROT_TELNET, PROT_RLOGIN, PROT_SSH
302 };
303
304 enum {
305     /* Bell settings (cfg.beep) */
306     BELL_DISABLED, BELL_DEFAULT, BELL_VISUAL, BELL_WAVEFILE, BELL_PCSPEAKER
307 };
308
309 enum {
310     /* Taskbar flashing indication on bell (cfg.beep_ind) */
311     B_IND_DISABLED, B_IND_FLASH, B_IND_STEADY
312 };
313
314 enum {
315     /* Resize actions (cfg.resize_action) */
316     RESIZE_TERM, RESIZE_DISABLED, RESIZE_FONT, RESIZE_EITHER
317 };
318
319 enum {
320     /* Function key types (cfg.funky_type) */
321     FUNKY_TILDE,
322     FUNKY_LINUX,
323     FUNKY_XTERM,
324     FUNKY_VT400,
325     FUNKY_VT100P,
326     FUNKY_SCO
327 };
328
329 enum {
330     FQ_DEFAULT, FQ_ANTIALIASED, FQ_NONANTIALIASED, FQ_CLEARTYPE
331 };
332
333 extern const char *const ttymodes[];
334
335 enum {
336     /*
337      * Network address types. Used for specifying choice of IPv4/v6
338      * in config; also used in proxy.c to indicate whether a given
339      * host name has already been resolved or will be resolved at
340      * the proxy end.
341      */
342     ADDRTYPE_UNSPEC, ADDRTYPE_IPV4, ADDRTYPE_IPV6, ADDRTYPE_NAME
343 };
344
345 struct backend_tag {
346     const char *(*init) (void *frontend_handle, void **backend_handle,
347                          Config *cfg,
348                          char *host, int port, char **realhost, int nodelay,
349                          int keepalive);
350     void (*free) (void *handle);
351     /* back->reconfig() passes in a replacement configuration. */
352     void (*reconfig) (void *handle, Config *cfg);
353     /* back->send() returns the current amount of buffered data. */
354     int (*send) (void *handle, char *buf, int len);
355     /* back->sendbuffer() does the same thing but without attempting a send */
356     int (*sendbuffer) (void *handle);
357     void (*size) (void *handle, int width, int height);
358     void (*special) (void *handle, Telnet_Special code);
359     const struct telnet_special *(*get_specials) (void *handle);
360     int (*connected) (void *handle);
361     int (*exitcode) (void *handle);
362     /* If back->sendok() returns FALSE, data sent to it from the frontend
363      * may be lost. */
364     int (*sendok) (void *handle);
365     int (*ldisc) (void *handle, int);
366     void (*provide_ldisc) (void *handle, void *ldisc);
367     void (*provide_logctx) (void *handle, void *logctx);
368     /*
369      * back->unthrottle() tells the back end that the front end
370      * buffer is clearing.
371      */
372     void (*unthrottle) (void *handle, int);
373     int (*cfg_info) (void *handle);
374     int default_port;
375 };
376
377 extern struct backend_list {
378     int protocol;
379     char *name;
380     Backend *backend;
381 } backends[];
382
383 /*
384  * Suggested default protocol provided by the backend link module.
385  * The application is free to ignore this.
386  */
387 extern const int be_default_protocol;
388
389 /*
390  * Name of this particular application, for use in the config box
391  * and other pieces of text.
392  */
393 extern const char *const appname;
394
395 /*
396  * IMPORTANT POLICY POINT: everything in this structure which wants
397  * to be treated like an integer must be an actual, honest-to-
398  * goodness `int'. No enum-typed variables. This is because parts
399  * of the code will want to pass around `int *' pointers to them
400  * and we can't run the risk of porting to some system on which the
401  * enum comes out as a different size from int.
402  */
403 struct config_tag {
404     /* Basic options */
405     char host[512];
406     int port;
407     int protocol;
408     int addressfamily;
409     int close_on_exit;
410     int warn_on_close;
411     int ping_interval;                 /* in seconds */
412     int tcp_nodelay;
413     int tcp_keepalives;
414     /* Proxy options */
415     char proxy_exclude_list[512];
416     int proxy_dns;
417     int even_proxy_localhost;
418     int proxy_type;
419     char proxy_host[512];
420     int proxy_port;
421     char proxy_username[128];
422     char proxy_password[128];
423     char proxy_telnet_command[512];
424     /* SSH options */
425     char remote_cmd[512];
426     char remote_cmd2[512];             /* fallback if the first fails
427                                         * (used internally for scp) */
428     char *remote_cmd_ptr;              /* might point to a larger command
429                                         * but never for loading/saving */
430     char *remote_cmd_ptr2;             /* might point to a larger command
431                                         * but never for loading/saving */
432     int nopty;
433     int compression;
434     int ssh_kexlist[KEX_MAX];
435     int ssh_rekey_time;                /* in minutes */
436     char ssh_rekey_data[16];
437     int tryagent;
438     int agentfwd;
439     int change_username;               /* allow username switching in SSH-2 */
440     int ssh_cipherlist[CIPHER_MAX];
441     Filename keyfile;
442     int sshprot;                       /* use v1 or v2 when both available */
443     int ssh2_des_cbc;                  /* "des-cbc" unrecommended SSH-2 cipher */
444     int ssh_no_userauth;               /* bypass "ssh-userauth" (SSH-2 only) */
445     int try_tis_auth;
446     int try_ki_auth;
447     int ssh_subsys;                    /* run a subsystem rather than a command */
448     int ssh_subsys2;                   /* fallback to go with remote_cmd2 */
449     int ssh_no_shell;                  /* avoid running a shell */
450     /* Telnet options */
451     char termtype[32];
452     char termspeed[32];
453     char ttymodes[768];                /* MODE\tVvalue\0MODE\tA\0\0 */
454     char environmt[1024];              /* VAR\tvalue\0VAR\tvalue\0\0 */
455     char username[100];
456     char localusername[100];
457     int rfc_environ;
458     int passive_telnet;
459     /* Keyboard options */
460     int bksp_is_delete;
461     int rxvt_homeend;
462     int funky_type;
463     int no_applic_c;                   /* totally disable app cursor keys */
464     int no_applic_k;                   /* totally disable app keypad */
465     int no_mouse_rep;                  /* totally disable mouse reporting */
466     int no_remote_resize;              /* disable remote resizing */
467     int no_alt_screen;                 /* disable alternate screen */
468     int no_remote_wintitle;            /* disable remote retitling */
469     int no_dbackspace;                 /* disable destructive backspace */
470     int no_remote_charset;             /* disable remote charset config */
471     int no_remote_qtitle;              /* disable remote win title query */
472     int app_cursor;
473     int app_keypad;
474     int nethack_keypad;
475     int telnet_keyboard;
476     int telnet_newline;
477     int alt_f4;                        /* is it special? */
478     int alt_space;                     /* is it special? */
479     int alt_only;                      /* is it special? */
480     int localecho;
481     int localedit;
482     int alwaysontop;
483     int fullscreenonaltenter;
484     int scroll_on_key;
485     int scroll_on_disp;
486     int erase_to_scrollback;
487     int compose_key;
488     int ctrlaltkeys;
489     char wintitle[256];                /* initial window title */
490     /* Terminal options */
491     int savelines;
492     int dec_om;
493     int wrap_mode;
494     int lfhascr;
495     int cursor_type;                   /* 0=block 1=underline 2=vertical */
496     int blink_cur;
497     int beep;
498     int beep_ind;
499     int bellovl;                       /* bell overload protection active? */
500     int bellovl_n;                     /* number of bells to cause overload */
501     int bellovl_t;                     /* time interval for overload (seconds) */
502     int bellovl_s;                     /* period of silence to re-enable bell (s) */
503     Filename bell_wavefile;
504     int scrollbar;
505     int scrollbar_in_fullscreen;
506     int resize_action;
507     int bce;
508     int blinktext;
509     int win_name_always;
510     int width, height;
511     FontSpec font;
512     int font_quality;
513     Filename logfilename;
514     int logtype;
515     int logxfovr;
516     int logflush;
517     int logomitpass;
518     int logomitdata;
519     int hide_mouseptr;
520     int sunken_edge;
521     int window_border;
522     char answerback[256];
523     char printer[128];
524     int arabicshaping;
525     int bidi;
526     /* Colour options */
527     int ansi_colour;
528     int xterm_256_colour;
529     int system_colour;
530     int try_palette;
531     int bold_colour;
532     unsigned char colours[22][3];
533     /* Selection options */
534     int mouse_is_xterm;
535     int rect_select;
536     int rawcnp;
537     int rtf_paste;
538     int mouse_override;
539     short wordness[256];
540     /* translations */
541     int vtmode;
542     char line_codepage[128];
543     int cjk_ambig_wide;
544     int utf8_override;
545     int xlat_capslockcyr;
546     /* X11 forwarding */
547     int x11_forward;
548     char x11_display[128];
549     int x11_auth;
550     /* port forwarding */
551     int lport_acceptall; /* accept conns from hosts other than localhost */
552     int rport_acceptall; /* same for remote forwarded ports (SSH-2 only) */
553     /*
554      * The port forwarding string contains a number of
555      * NUL-terminated substrings, terminated in turn by an empty
556      * string (i.e. a second NUL immediately after the previous
557      * one). Each string can be of one of the following forms:
558      * 
559      *   [LR]localport\thost:port
560      *   [LR]localaddr:localport\thost:port
561      *   Dlocalport
562      *   Dlocaladdr:localport
563      */
564     char portfwd[1024];
565     /* SSH bug compatibility modes */
566     int sshbug_ignore1, sshbug_plainpw1, sshbug_rsa1,
567         sshbug_hmac2, sshbug_derivekey2, sshbug_rsapad2,
568         sshbug_pksessid2, sshbug_rekey2;
569     /* Options for pterm. Should split out into platform-dependent part. */
570     int stamp_utmp;
571     int login_shell;
572     int scrollbar_on_left;
573     int shadowbold;
574     FontSpec boldfont;
575     FontSpec widefont;
576     FontSpec wideboldfont;
577     int shadowboldoffset;
578 };
579
580 /*
581  * Some global flags denoting the type of application.
582  * 
583  * FLAG_VERBOSE is set when the user requests verbose details.
584  * 
585  * FLAG_STDERR is set in command-line applications (which have a
586  * functioning stderr that it makes sense to write to) and not in
587  * GUI applications (which don't).
588  * 
589  * FLAG_INTERACTIVE is set when a full interactive shell session is
590  * being run, _either_ because no remote command has been provided
591  * _or_ because the application is GUI and can't run non-
592  * interactively.
593  * 
594  * These flags describe the type of _application_ - they wouldn't
595  * vary between individual sessions - and so it's OK to have this
596  * variable be GLOBAL.
597  * 
598  * Note that additional flags may be defined in platform-specific
599  * headers. It's probably best if those ones start from 0x1000, to
600  * avoid collision.
601  */
602 #define FLAG_VERBOSE     0x0001
603 #define FLAG_STDERR      0x0002
604 #define FLAG_INTERACTIVE 0x0004
605 GLOBAL int flags;
606
607 /*
608  * Likewise, these two variables are set up when the application
609  * initialises, and inform all default-settings accesses after
610  * that.
611  */
612 GLOBAL int default_protocol;
613 GLOBAL int default_port;
614
615 /*
616  * This is set TRUE by cmdline.c iff a session is loaded with "-load".
617  */
618 GLOBAL int loaded_session;
619
620 struct RSAKey;                         /* be a little careful of scope */
621
622 /*
623  * Mechanism for getting text strings such as usernames and passwords
624  * from the front-end.
625  * The fields are mostly modelled after SSH's keyboard-interactive auth.
626  * FIXME We should probably mandate a character set/encoding (probably UTF-8).
627  *
628  * Since many of the pieces of text involved may be chosen by the server,
629  * the caller must take care to ensure that the server can't spoof locally-
630  * generated prompts such as key passphrase prompts. Some ground rules:
631  *  - If the front-end needs to truncate a string, it should lop off the
632  *    end.
633  *  - The front-end should filter out any dangerous characters and
634  *    generally not trust the strings. (But \n is required to behave
635  *    vaguely sensibly, at least in `instruction', and ideally in
636  *    `prompt[]' too.)
637  */
638 typedef struct {
639     char *prompt;
640     int echo;
641     char *result;       /* allocated/freed by caller */
642     size_t result_len;
643 } prompt_t;
644 typedef struct {
645     /*
646      * Indicates whether the information entered is to be used locally
647      * (for instance a key passphrase prompt), or is destined for the wire.
648      * This is a hint only; the front-end is at liberty not to use this
649      * information (so the caller should ensure that the supplied text is
650      * sufficient).
651      */
652     int to_server;
653     char *name;         /* Short description, perhaps for dialog box title */
654     int name_reqd;      /* Display of `name' required or optional? */
655     char *instruction;  /* Long description, maybe with embedded newlines */
656     int instr_reqd;     /* Display of `instruction' required or optional? */
657     size_t n_prompts;
658     prompt_t **prompts;
659     void *frontend;
660     void *data;         /* slot for housekeeping data, managed by
661                          * get_userpass_input(); initially NULL */
662 } prompts_t;
663 prompts_t *new_prompts(void *frontend);
664 void add_prompt(prompts_t *p, char *promptstr, int echo, size_t len);
665 /* Burn the evidence. (Assumes _all_ strings want free()ing.) */
666 void free_prompts(prompts_t *p);
667
668 /*
669  * Exports from the front end.
670  */
671 void request_resize(void *frontend, int, int);
672 void do_text(Context, int, int, wchar_t *, int, unsigned long, int);
673 void do_cursor(Context, int, int, wchar_t *, int, unsigned long, int);
674 int char_width(Context ctx, int uc);
675 #ifdef OPTIMISE_SCROLL
676 void do_scroll(Context, int, int, int);
677 #endif
678 void set_title(void *frontend, char *);
679 void set_icon(void *frontend, char *);
680 void set_sbar(void *frontend, int, int, int);
681 Context get_ctx(void *frontend);
682 void free_ctx(Context);
683 void palette_set(void *frontend, int, int, int, int);
684 void palette_reset(void *frontend);
685 void write_aclip(void *frontend, char *, int, int);
686 void write_clip(void *frontend, wchar_t *, int *, int, int);
687 void get_clip(void *frontend, wchar_t **, int *);
688 void optimised_move(void *frontend, int, int, int);
689 void set_raw_mouse_mode(void *frontend, int);
690 void connection_fatal(void *frontend, char *, ...);
691 void fatalbox(char *, ...);
692 void modalfatalbox(char *, ...);
693 #ifdef macintosh
694 #pragma noreturn(fatalbox)
695 #pragma noreturn(modalfatalbox)
696 #endif
697 void do_beep(void *frontend, int);
698 void begin_session(void *frontend);
699 void sys_cursor(void *frontend, int x, int y);
700 void request_paste(void *frontend);
701 void frontend_keypress(void *frontend);
702 void ldisc_update(void *frontend, int echo, int edit);
703 /* It's the backend's responsibility to invoke this at the start of a
704  * connection, if necessary; it can also invoke it later if the set of
705  * special commands changes. It does not need to invoke it at session
706  * shutdown. */
707 void update_specials_menu(void *frontend);
708 int from_backend(void *frontend, int is_stderr, const char *data, int len);
709 int from_backend_untrusted(void *frontend, const char *data, int len);
710 void notify_remote_exit(void *frontend);
711 /* Get a sensible value for a tty mode. NULL return = don't set.
712  * Otherwise, returned value should be freed by caller. */
713 char *get_ttymode(void *frontend, const char *mode);
714 /*
715  * >0 = `got all results, carry on'
716  * 0  = `user cancelled' (FIXME distinguish "give up entirely" and "next auth"?)
717  * <0 = `please call back later with more in/inlen'
718  */
719 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen);
720 #define OPTIMISE_IS_SCROLL 1
721
722 void set_iconic(void *frontend, int iconic);
723 void move_window(void *frontend, int x, int y);
724 void set_zorder(void *frontend, int top);
725 void refresh_window(void *frontend);
726 void set_zoomed(void *frontend, int zoomed);
727 int is_iconic(void *frontend);
728 void get_window_pos(void *frontend, int *x, int *y);
729 void get_window_pixels(void *frontend, int *x, int *y);
730 char *get_window_title(void *frontend, int icon);
731 /* Hint from backend to frontend about time-consuming operations.
732  * Initial state is assumed to be BUSY_NOT. */
733 enum {
734     BUSY_NOT,       /* Not busy, all user interaction OK */
735     BUSY_WAITING,   /* Waiting for something; local event loops still running
736                        so some local interaction (e.g. menus) OK, but network
737                        stuff is suspended */
738     BUSY_CPU        /* Locally busy (e.g. crypto); user interaction suspended */
739 };
740 void set_busy_status(void *frontend, int status);
741
742 void cleanup_exit(int);
743
744 /*
745  * Exports from noise.c.
746  */
747 void noise_get_heavy(void (*func) (void *, int));
748 void noise_get_light(void (*func) (void *, int));
749 void noise_regular(void);
750 void noise_ultralight(unsigned long data);
751 void random_save_seed(void);
752 void random_destroy_seed(void);
753
754 /*
755  * Exports from settings.c.
756  */
757 char *save_settings(char *section, int do_host, Config * cfg);
758 void save_open_settings(void *sesskey, int do_host, Config *cfg);
759 void load_settings(char *section, int do_host, Config * cfg);
760 void load_open_settings(void *sesskey, int do_host, Config *cfg);
761 void get_sesslist(struct sesslist *, int allocate);
762 void do_defaults(char *, Config *);
763 void registry_cleanup(void);
764
765 /*
766  * Functions used by settings.c to provide platform-specific
767  * default settings.
768  * 
769  * (The integer one is expected to return `def' if it has no clear
770  * opinion of its own. This is because there's no integer value
771  * which I can reliably set aside to indicate `nil'. The string
772  * function is perfectly all right returning NULL, of course. The
773  * Filename and FontSpec functions are _not allowed_ to fail to
774  * return, since these defaults _must_ be per-platform.)
775  */
776 char *platform_default_s(const char *name);
777 int platform_default_i(const char *name, int def);
778 Filename platform_default_filename(const char *name);
779 FontSpec platform_default_fontspec(const char *name);
780
781 /*
782  * Exports from terminal.c.
783  */
784
785 Terminal *term_init(Config *, struct unicode_data *, void *);
786 void term_free(Terminal *);
787 void term_size(Terminal *, int, int, int);
788 void term_paint(Terminal *, Context, int, int, int, int, int);
789 void term_scroll(Terminal *, int, int);
790 void term_pwron(Terminal *, int);
791 void term_clrsb(Terminal *);
792 void term_mouse(Terminal *, Mouse_Button, Mouse_Button, Mouse_Action,
793                 int,int,int,int,int);
794 void term_key(Terminal *, Key_Sym, wchar_t *, size_t, unsigned int,
795               unsigned int);
796 void term_deselect(Terminal *);
797 void term_update(Terminal *);
798 void term_invalidate(Terminal *);
799 void term_blink(Terminal *, int set_cursor);
800 void term_do_paste(Terminal *);
801 int term_paste_pending(Terminal *);
802 void term_paste(Terminal *);
803 void term_nopaste(Terminal *);
804 int term_ldisc(Terminal *, int option);
805 void term_copyall(Terminal *);
806 void term_reconfig(Terminal *, Config *);
807 void term_seen_key_event(Terminal *); 
808 int term_data(Terminal *, int is_stderr, const char *data, int len);
809 int term_data_untrusted(Terminal *, const char *data, int len);
810 void term_provide_resize_fn(Terminal *term,
811                             void (*resize_fn)(void *, int, int),
812                             void *resize_ctx);
813 void term_provide_logctx(Terminal *term, void *logctx);
814 void term_set_focus(Terminal *term, int has_focus);
815 char *term_get_ttymode(Terminal *term, const char *mode);
816 int term_get_userpass_input(Terminal *term, prompts_t *p,
817                             unsigned char *in, int inlen);
818
819 /*
820  * Exports from logging.c.
821  */
822 void *log_init(void *frontend, Config *cfg);
823 void log_free(void *logctx);
824 void log_reconfig(void *logctx, Config *cfg);
825 void logfopen(void *logctx);
826 void logfclose(void *logctx);
827 void logtraffic(void *logctx, unsigned char c, int logmode);
828 void logflush(void *logctx);
829 void log_eventlog(void *logctx, const char *string);
830 enum { PKT_INCOMING, PKT_OUTGOING };
831 enum { PKTLOG_EMIT, PKTLOG_BLANK, PKTLOG_OMIT };
832 struct logblank_t {
833     int offset;
834     int len;
835     int type;
836 };
837 void log_packet(void *logctx, int direction, int type,
838                 char *texttype, void *data, int len,
839                 int n_blanks, const struct logblank_t *blanks);
840
841 /*
842  * Exports from testback.c
843  */
844
845 extern Backend null_backend;
846 extern Backend loop_backend;
847
848 /*
849  * Exports from raw.c.
850  */
851
852 extern Backend raw_backend;
853
854 /*
855  * Exports from rlogin.c.
856  */
857
858 extern Backend rlogin_backend;
859
860 /*
861  * Exports from telnet.c.
862  */
863
864 extern Backend telnet_backend;
865
866 /*
867  * Exports from ssh.c.
868  */
869 extern Backend ssh_backend;
870
871 /*
872  * Exports from ldisc.c.
873  */
874 void *ldisc_create(Config *, Terminal *, Backend *, void *, void *);
875 void ldisc_free(void *);
876 void ldisc_send(void *handle, char *buf, int len, int interactive);
877
878 /*
879  * Exports from ldiscucs.c.
880  */
881 void lpage_send(void *, int codepage, char *buf, int len, int interactive);
882 void luni_send(void *, wchar_t * widebuf, int len, int interactive);
883
884 /*
885  * Exports from sshrand.c.
886  */
887
888 void random_add_noise(void *noise, int length);
889 int random_byte(void);
890 void random_get_savedata(void **data, int *len);
891 extern int random_active;
892 /* The random number subsystem is activated if at least one other entity
893  * within the program expresses an interest in it. So each SSH session
894  * calls random_ref on startup and random_unref on shutdown. */
895 void random_ref(void);
896 void random_unref(void);
897
898 /*
899  * Exports from pinger.c.
900  */
901 typedef struct pinger_tag *Pinger;
902 Pinger pinger_new(Config *cfg, Backend *back, void *backhandle);
903 void pinger_reconfig(Pinger, Config *oldcfg, Config *newcfg);
904 void pinger_free(Pinger);
905
906 /*
907  * Exports from misc.c.
908  */
909
910 #include "misc.h"
911
912 /*
913  * Exports from version.c.
914  */
915 extern char ver[];
916
917 /*
918  * Exports from unicode.c.
919  */
920 #ifndef CP_UTF8
921 #define CP_UTF8 65001
922 #endif
923 /* void init_ucs(void); -- this is now in platform-specific headers */
924 int is_dbcs_leadbyte(int codepage, char byte);
925 int mb_to_wc(int codepage, int flags, char *mbstr, int mblen,
926              wchar_t *wcstr, int wclen);
927 int wc_to_mb(int codepage, int flags, wchar_t *wcstr, int wclen,
928              char *mbstr, int mblen, char *defchr, int *defused,
929              struct unicode_data *ucsdata);
930 wchar_t xlat_uskbd2cyrllic(int ch);
931 int check_compose(int first, int second);
932 int decode_codepage(char *cp_name);
933 const char *cp_enumerate (int index);
934 const char *cp_name(int codepage);
935 void get_unitab(int codepage, wchar_t * unitab, int ftype);
936
937 /*
938  * Exports from wcwidth.c
939  */
940 int mk_wcwidth(wchar_t ucs);
941 int mk_wcswidth(const wchar_t *pwcs, size_t n);
942 int mk_wcwidth_cjk(wchar_t ucs);
943 int mk_wcswidth_cjk(const wchar_t *pwcs, size_t n);
944
945 /*
946  * Exports from mscrypto.c
947  */
948 #ifdef MSCRYPTOAPI
949 int crypto_startup();
950 void crypto_wrapup();
951 #endif
952
953 /*
954  * Exports from pageantc.c.
955  * 
956  * agent_query returns 1 for here's-a-response, and 0 for query-in-
957  * progress. In the latter case there will be a call to `callback'
958  * at some future point, passing callback_ctx as the first
959  * parameter and the actual reply data as the second and third.
960  * 
961  * The response may be a NULL pointer (in either of the synchronous
962  * or asynchronous cases), which indicates failure to receive a
963  * response.
964  */
965 int agent_query(void *in, int inlen, void **out, int *outlen,
966                 void (*callback)(void *, void *, int), void *callback_ctx);
967 int agent_exists(void);
968
969 /*
970  * Exports from wildcard.c
971  */
972 const char *wc_error(int value);
973 int wc_match(const char *wildcard, const char *target);
974 int wc_unescape(char *output, const char *wildcard);
975
976 /*
977  * Exports from frontend (windlg.c etc)
978  */
979 void logevent(void *frontend, const char *);
980 void pgp_fingerprints(void);
981 /*
982  * verify_ssh_host_key() can return one of three values:
983  * 
984  *  - +1 means `key was OK' (either already known or the user just
985  *    approved it) `so continue with the connection'
986  * 
987  *  - 0 means `key was not OK, abandon the connection'
988  * 
989  *  - -1 means `I've initiated enquiries, please wait to be called
990  *    back via the provided function with a result that's either 0
991  *    or +1'.
992  */
993 int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
994                         char *keystr, char *fingerprint,
995                         void (*callback)(void *ctx, int result), void *ctx);
996 /*
997  * askalg has the same set of return values as verify_ssh_host_key.
998  */
999 int askalg(void *frontend, const char *algtype, const char *algname,
1000            void (*callback)(void *ctx, int result), void *ctx);
1001 /*
1002  * askappend can return four values:
1003  * 
1004  *  - 2 means overwrite the log file
1005  *  - 1 means append to the log file
1006  *  - 0 means cancel logging for this session
1007  *  - -1 means please wait.
1008  */
1009 int askappend(void *frontend, Filename filename,
1010               void (*callback)(void *ctx, int result), void *ctx);
1011
1012 /*
1013  * Exports from console frontends (wincons.c, uxcons.c)
1014  * that aren't equivalents to things in windlg.c et al.
1015  */
1016 extern int console_batch_mode;
1017 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen);
1018 void console_provide_logctx(void *logctx);
1019 int is_interactive(void);
1020
1021 /*
1022  * Exports from printing.c.
1023  */
1024 typedef struct printer_enum_tag printer_enum;
1025 typedef struct printer_job_tag printer_job;
1026 printer_enum *printer_start_enum(int *nprinters);
1027 char *printer_get_name(printer_enum *, int);
1028 void printer_finish_enum(printer_enum *);
1029 printer_job *printer_start_job(char *printer);
1030 void printer_job_data(printer_job *, void *, int);
1031 void printer_finish_job(printer_job *);
1032
1033 /*
1034  * Exports from cmdline.c (and also cmdline_error(), which is
1035  * defined differently in various places and required _by_
1036  * cmdline.c).
1037  */
1038 int cmdline_process_param(char *, char *, int, Config *);
1039 void cmdline_run_saved(Config *);
1040 void cmdline_cleanup(void);
1041 int cmdline_get_passwd_input(prompts_t *p, unsigned char *in, int inlen);
1042 #define TOOLTYPE_FILETRANSFER 1
1043 #define TOOLTYPE_NONNETWORK 2
1044 extern int cmdline_tooltype;
1045
1046 void cmdline_error(char *, ...);
1047
1048 /*
1049  * Exports from config.c.
1050  */
1051 struct controlbox;
1052 void setup_config_box(struct controlbox *b, int midsession,
1053                       int protocol, int protcfginfo);
1054
1055 /*
1056  * Exports from minibidi.c.
1057  */
1058 typedef struct bidi_char {
1059     wchar_t origwc, wc;
1060     unsigned short index;
1061 } bidi_char;
1062 int do_bidi(bidi_char *line, int count);
1063 int do_shape(bidi_char *line, bidi_char *to, int count);
1064
1065 /*
1066  * X11 auth mechanisms we know about.
1067  */
1068 enum {
1069     X11_NO_AUTH,
1070     X11_MIT,                           /* MIT-MAGIC-COOKIE-1 */
1071     X11_XDM,                           /* XDM-AUTHORIZATION-1 */
1072     X11_NAUTHS
1073 };
1074 extern const char *const x11_authnames[];  /* declared in x11fwd.c */
1075
1076 /*
1077  * Miscellaneous exports from the platform-specific code.
1078  */
1079 Filename filename_from_str(const char *string);
1080 const char *filename_to_str(const Filename *fn);
1081 int filename_equal(Filename f1, Filename f2);
1082 int filename_is_null(Filename fn);
1083 char *get_username(void);              /* return value needs freeing */
1084 char *get_random_data(int bytes);      /* used in cmdgen.c */
1085
1086 /*
1087  * Exports and imports from timing.c.
1088  *
1089  * schedule_timer() asks the front end to schedule a callback to a
1090  * timer function in a given number of ticks. The returned value is
1091  * the time (in ticks since an arbitrary offset) at which the
1092  * callback can be expected. This value will also be passed as the
1093  * `now' parameter to the callback function. Hence, you can (for
1094  * example) schedule an event at a particular time by calling
1095  * schedule_timer() and storing the return value in your context
1096  * structure as the time when that event is due. The first time a
1097  * callback function gives you that value or more as `now', you do
1098  * the thing.
1099  * 
1100  * expire_timer_context() drops all current timers associated with
1101  * a given value of ctx (for when you're about to free ctx).
1102  * 
1103  * run_timers() is called from the front end when it has reason to
1104  * think some timers have reached their moment, or when it simply
1105  * needs to know how long to wait next. We pass it the time we
1106  * think it is. It returns TRUE and places the time when the next
1107  * timer needs to go off in `next', or alternatively it returns
1108  * FALSE if there are no timers at all pending.
1109  * 
1110  * timer_change_notify() must be supplied by the front end; it
1111  * notifies the front end that a new timer has been added to the
1112  * list which is sooner than any existing ones. It provides the
1113  * time when that timer needs to go off.
1114  * 
1115  * *** FRONT END IMPLEMENTORS NOTE:
1116  * 
1117  * There's an important subtlety in the front-end implementation of
1118  * the timer interface. When a front end is given a `next' value,
1119  * either returned from run_timers() or via timer_change_notify(),
1120  * it should ensure that it really passes _that value_ as the `now'
1121  * parameter to its next run_timers call. It should _not_ simply
1122  * call GETTICKCOUNT() to get the `now' parameter when invoking
1123  * run_timers().
1124  * 
1125  * The reason for this is that an OS's system clock might not agree
1126  * exactly with the timing mechanisms it supplies to wait for a
1127  * given interval. I'll illustrate this by the simple example of
1128  * Unix Plink, which uses timeouts to select() in a way which for
1129  * these purposes can simply be considered to be a wait() function.
1130  * Suppose, for the sake of argument, that this wait() function
1131  * tends to return early by 1%. Then a possible sequence of actions
1132  * is:
1133  * 
1134  *  - run_timers() tells the front end that the next timer firing
1135  *    is 10000ms from now.
1136  *  - Front end calls wait(10000ms), but according to
1137  *    GETTICKCOUNT() it has only waited for 9900ms.
1138  *  - Front end calls run_timers() again, passing time T-100ms as
1139  *    `now'.
1140  *  - run_timers() does nothing, and says the next timer firing is
1141  *    still 100ms from now.
1142  *  - Front end calls wait(100ms), which only waits for 99ms.
1143  *  - Front end calls run_timers() yet again, passing time T-1ms.
1144  *  - run_timers() says there's still 1ms to wait.
1145  *  - Front end calls wait(1ms).
1146  * 
1147  * If you're _lucky_ at this point, wait(1ms) will actually wait
1148  * for 1ms and you'll only have woken the program up three times.
1149  * If you're unlucky, wait(1ms) might do nothing at all due to
1150  * being below some minimum threshold, and you might find your
1151  * program spends the whole of the last millisecond tight-looping
1152  * between wait() and run_timers().
1153  * 
1154  * Instead, what you should do is to _save_ the precise `next'
1155  * value provided by run_timers() or via timer_change_notify(), and
1156  * use that precise value as the input to the next run_timers()
1157  * call. So:
1158  * 
1159  *  - run_timers() tells the front end that the next timer firing
1160  *    is at time T, 10000ms from now.
1161  *  - Front end calls wait(10000ms).
1162  *  - Front end then immediately calls run_timers() and passes it
1163  *    time T, without stopping to check GETTICKCOUNT() at all.
1164  * 
1165  * This guarantees that the program wakes up only as many times as
1166  * there are actual timer actions to be taken, and that the timing
1167  * mechanism will never send it into a tight loop.
1168  * 
1169  * (It does also mean that the timer action in the above example
1170  * will occur 100ms early, but this is not generally critical. And
1171  * the hypothetical 1% error in wait() will be partially corrected
1172  * for anyway when, _after_ run_timers() returns, you call
1173  * GETTICKCOUNT() and compare the result with the returned `next'
1174  * value to find out how long you have to make your next wait().)
1175  */
1176 typedef void (*timer_fn_t)(void *ctx, long now);
1177 long schedule_timer(int ticks, timer_fn_t fn, void *ctx);
1178 void expire_timer_context(void *ctx);
1179 int run_timers(long now, long *next);
1180 void timer_change_notify(long next);
1181
1182 #endif