1 /*
2  * config.c - the platform-independent parts of the PuTTY
3  * configuration box.
4  */
5 
6 #include <assert.h>
7 #include <stdlib.h>
8 
9 #include "putty.h"
10 #include "dialog.h"
11 #include "storage.h"
12 
13 #define PRINTER_DISABLED_STRING "None (printing disabled)"
14 
15 #define HOST_BOX_TITLE "Host Name (or IP address)"
16 #define PORT_BOX_TITLE "Port"
17 
conf_radiobutton_handler(union control * ctrl,dlgparam * dlg,void * data,int event)18 void conf_radiobutton_handler(union control *ctrl, dlgparam *dlg,
19                               void *data, int event)
20 {
21     int button;
22     Conf *conf = (Conf *)data;
23 
24     /*
25      * For a standard radio button set, the context parameter gives
26      * the primary key (CONF_foo), and the extra data per button
27      * gives the value the target field should take if that button
28      * is the one selected.
29      */
30     if (event == EVENT_REFRESH) {
31         int val = conf_get_int(conf, ctrl->radio.context.i);
32         for (button = 0; button < ctrl->radio.nbuttons; button++)
33             if (val == ctrl->radio.buttondata[button].i)
34                 break;
35         /* We expected that `break' to happen, in all circumstances. */
36         assert(button < ctrl->radio.nbuttons);
37         dlg_radiobutton_set(ctrl, dlg, button);
38     } else if (event == EVENT_VALCHANGE) {
39         button = dlg_radiobutton_get(ctrl, dlg);
40         assert(button >= 0 && button < ctrl->radio.nbuttons);
41         conf_set_int(conf, ctrl->radio.context.i,
42                      ctrl->radio.buttondata[button].i);
43     }
44 }
45 
conf_radiobutton_bool_handler(union control * ctrl,dlgparam * dlg,void * data,int event)46 void conf_radiobutton_bool_handler(union control *ctrl, dlgparam *dlg,
47                                    void *data, int event)
48 {
49     int button;
50     Conf *conf = (Conf *)data;
51 
52     /*
53      * Same as conf_radiobutton_handler, but using conf_set_bool in
54      * place of conf_set_int, because it's dealing with a bool-typed
55      * config option.
56      */
57     if (event == EVENT_REFRESH) {
58         int val = conf_get_bool(conf, ctrl->radio.context.i);
59         for (button = 0; button < ctrl->radio.nbuttons; button++)
60             if (val == ctrl->radio.buttondata[button].i)
61                 break;
62         /* We expected that `break' to happen, in all circumstances. */
63         assert(button < ctrl->radio.nbuttons);
64         dlg_radiobutton_set(ctrl, dlg, button);
65     } else if (event == EVENT_VALCHANGE) {
66         button = dlg_radiobutton_get(ctrl, dlg);
67         assert(button >= 0 && button < ctrl->radio.nbuttons);
68         conf_set_bool(conf, ctrl->radio.context.i,
69                       ctrl->radio.buttondata[button].i);
70     }
71 }
72 
73 #define CHECKBOX_INVERT (1<<30)
conf_checkbox_handler(union control * ctrl,dlgparam * dlg,void * data,int event)74 void conf_checkbox_handler(union control *ctrl, dlgparam *dlg,
75                            void *data, int event)
76 {
77     int key;
78     bool invert;
79     Conf *conf = (Conf *)data;
80 
81     /*
82      * For a standard checkbox, the context parameter gives the
83      * primary key (CONF_foo), optionally ORed with CHECKBOX_INVERT.
84      */
85     key = ctrl->checkbox.context.i;
86     if (key & CHECKBOX_INVERT) {
87         key &= ~CHECKBOX_INVERT;
88         invert = true;
89     } else
90         invert = false;
91 
92     /*
93      * C lacks a logical XOR, so the following code uses the idiom
94      * (!a ^ !b) to obtain the logical XOR of a and b. (That is, 1
95      * iff exactly one of a and b is nonzero, otherwise 0.)
96      */
97 
98     if (event == EVENT_REFRESH) {
99         bool val = conf_get_bool(conf, key);
100         dlg_checkbox_set(ctrl, dlg, (!val ^ !invert));
101     } else if (event == EVENT_VALCHANGE) {
102         conf_set_bool(conf, key, !dlg_checkbox_get(ctrl,dlg) ^ !invert);
103     }
104 }
105 
conf_editbox_handler(union control * ctrl,dlgparam * dlg,void * data,int event)106 void conf_editbox_handler(union control *ctrl, dlgparam *dlg,
107                           void *data, int event)
108 {
109     /*
110      * The standard edit-box handler expects the main `context'
111      * field to contain the primary key. The secondary `context2'
112      * field indicates the type of this field:
113      *
114      *  - if context2 > 0, the field is a string.
115      *  - if context2 == -1, the field is an int and the edit box
116      *    is numeric.
117      *  - if context2 < -1, the field is an int and the edit box is
118      *    _floating_, and (-context2) gives the scale. (E.g. if
119      *    context2 == -1000, then typing 1.2 into the box will set
120      *    the field to 1200.)
121      */
122     int key = ctrl->editbox.context.i;
123     int length = ctrl->editbox.context2.i;
124     Conf *conf = (Conf *)data;
125 
126     if (length > 0) {
127         if (event == EVENT_REFRESH) {
128             char *field = conf_get_str(conf, key);
129             dlg_editbox_set(ctrl, dlg, field);
130         } else if (event == EVENT_VALCHANGE) {
131             char *field = dlg_editbox_get(ctrl, dlg);
132             conf_set_str(conf, key, field);
133             sfree(field);
134         }
135     } else if (length < 0) {
136         if (event == EVENT_REFRESH) {
137             char str[80];
138             int value = conf_get_int(conf, key);
139             if (length == -1)
140                 sprintf(str, "%d", value);
141             else
142                 sprintf(str, "%g", (double)value / (double)(-length));
143             dlg_editbox_set(ctrl, dlg, str);
144         } else if (event == EVENT_VALCHANGE) {
145             char *str = dlg_editbox_get(ctrl, dlg);
146             if (length == -1)
147                 conf_set_int(conf, key, atoi(str));
148             else
149                 conf_set_int(conf, key, (int)((-length) * atof(str)));
150             sfree(str);
151         }
152     }
153 }
154 
conf_filesel_handler(union control * ctrl,dlgparam * dlg,void * data,int event)155 void conf_filesel_handler(union control *ctrl, dlgparam *dlg,
156                           void *data, int event)
157 {
158     int key = ctrl->fileselect.context.i;
159     Conf *conf = (Conf *)data;
160 
161     if (event == EVENT_REFRESH) {
162         dlg_filesel_set(
163             ctrl, dlg, conf_get_filename(conf, key));
164     } else if (event == EVENT_VALCHANGE) {
165         Filename *filename = dlg_filesel_get(ctrl, dlg);
166         conf_set_filename(conf, key, filename);
167         filename_free(filename);
168     }
169 }
170 
conf_fontsel_handler(union control * ctrl,dlgparam * dlg,void * data,int event)171 void conf_fontsel_handler(union control *ctrl, dlgparam *dlg,
172                           void *data, int event)
173 {
174     int key = ctrl->fontselect.context.i;
175     Conf *conf = (Conf *)data;
176 
177     if (event == EVENT_REFRESH) {
178         dlg_fontsel_set(
179             ctrl, dlg, conf_get_fontspec(conf, key));
180     } else if (event == EVENT_VALCHANGE) {
181         FontSpec *fontspec = dlg_fontsel_get(ctrl, dlg);
182         conf_set_fontspec(conf, key, fontspec);
183         fontspec_free(fontspec);
184     }
185 }
186 
config_host_handler(union control * ctrl,dlgparam * dlg,void * data,int event)187 static void config_host_handler(union control *ctrl, dlgparam *dlg,
188                                 void *data, int event)
189 {
190     Conf *conf = (Conf *)data;
191 
192     /*
193      * This function works just like the standard edit box handler,
194      * only it has to choose the control's label and text from two
195      * different places depending on the protocol.
196      */
197     if (event == EVENT_REFRESH) {
198         if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL) {
199             /*
200              * This label text is carefully chosen to contain an n,
201              * since that's the shortcut for the host name control.
202              */
203             dlg_label_change(ctrl, dlg, "Serial line");
204             dlg_editbox_set(ctrl, dlg, conf_get_str(conf, CONF_serline));
205         } else {
206             dlg_label_change(ctrl, dlg, HOST_BOX_TITLE);
207             dlg_editbox_set(ctrl, dlg, conf_get_str(conf, CONF_host));
208         }
209     } else if (event == EVENT_VALCHANGE) {
210         char *s = dlg_editbox_get(ctrl, dlg);
211         if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
212             conf_set_str(conf, CONF_serline, s);
213         else
214             conf_set_str(conf, CONF_host, s);
215         sfree(s);
216     }
217 }
218 
config_port_handler(union control * ctrl,dlgparam * dlg,void * data,int event)219 static void config_port_handler(union control *ctrl, dlgparam *dlg,
220                                 void *data, int event)
221 {
222     Conf *conf = (Conf *)data;
223     char buf[80];
224 
225     /*
226      * This function works similarly to the standard edit box handler,
227      * only it has to choose the control's label and text from two
228      * different places depending on the protocol.
229      */
230     if (event == EVENT_REFRESH) {
231         if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL) {
232             /*
233              * This label text is carefully chosen to contain a p,
234              * since that's the shortcut for the port control.
235              */
236             dlg_label_change(ctrl, dlg, "Speed");
237             sprintf(buf, "%d", conf_get_int(conf, CONF_serspeed));
238         } else {
239             dlg_label_change(ctrl, dlg, PORT_BOX_TITLE);
240             if (conf_get_int(conf, CONF_port) != 0)
241                 sprintf(buf, "%d", conf_get_int(conf, CONF_port));
242             else
243                 /* Display an (invalid) port of 0 as blank */
244                 buf[0] = '\0';
245         }
246         dlg_editbox_set(ctrl, dlg, buf);
247     } else if (event == EVENT_VALCHANGE) {
248         char *s = dlg_editbox_get(ctrl, dlg);
249         int i = atoi(s);
250         sfree(s);
251 
252         if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
253             conf_set_int(conf, CONF_serspeed, i);
254         else
255             conf_set_int(conf, CONF_port, i);
256     }
257 }
258 
259 struct hostport {
260     union control *host, *port, *protradio, *protlist;
261     bool mid_refresh;
262 };
263 
264 /*
265  * Shared handler for protocol radio-button and drop-list controls.
266  * Handles the interaction of those two controls, and also changes
267  * the setting of the port box to match the protocol if necessary,
268  * and refreshes both host and port boxes when switching to/from the
269  * serial backend.
270  */
config_protocols_handler(union control * ctrl,dlgparam * dlg,void * data,int event)271 static void config_protocols_handler(union control *ctrl, dlgparam *dlg,
272                                      void *data, int event)
273 {
274     Conf *conf = (Conf *)data;
275     int curproto = conf_get_int(conf, CONF_protocol);
276     struct hostport *hp = (struct hostport *)ctrl->generic.context.p;
277 
278     if (event == EVENT_REFRESH) {
279         /*
280          * Refresh the states of the controls from Conf.
281          *
282          * When refreshing these controls, we have to watch out for
283          * re-entrancy: because there are two controls involved, the
284          * refresh is not atomic, so the VALCHANGE and/or SELCHANGE
285          * callbacks resulting from our updates here might cause other
286          * settings here to change unwantedly. (E.g. setting the list
287          * selection shouldn't trigger the SELCHANGE side effect of
288          * selecting the Other radio button; setting the radio button
289          * to Other here shouldn't have the side effect of selecting
290          * whatever protocol is _currently_ selected in the list box,
291          * if we haven't selected the right one yet.)
292          */
293         hp->mid_refresh = true;
294 
295         if (ctrl == hp->protradio) {
296             /* Available buttons were set up when control was created.
297              * Just select one of them, possibly. */
298             for (int button = 0; button < ctrl->radio.nbuttons; button++)
299                 /* The final button is "Other:". If we reach that one, the
300                  * current protocol must be in the drop list, so we should
301                  * select the "Other:" button. */
302                 if (curproto == ctrl->radio.buttondata[button].i ||
303                     button == ctrl->radio.nbuttons-1) {
304                     dlg_radiobutton_set(ctrl, dlg, button);
305                     break;
306                 }
307         } else if (ctrl == hp->protlist) {
308             int curentry = -1;
309             dlg_update_start(ctrl, dlg);
310             dlg_listbox_clear(ctrl, dlg);
311             assert(n_ui_backends > 0 && n_ui_backends < PROTOCOL_LIMIT);
312             for (size_t i = n_ui_backends;
313                  i < PROTOCOL_LIMIT && backends[i]; i++) {
314                 dlg_listbox_addwithid(ctrl, dlg,
315                                       backends[i]->displayname,
316                                       backends[i]->protocol);
317                 if (backends[i]->protocol == curproto)
318                     curentry = i - n_ui_backends;
319             }
320             if (curentry > 0) {
321                 /*
322                  * The currently configured protocol is one of the
323                  * list-box ones, so select it in protlist.
324                  *
325                  * (The corresponding refresh event for protradio
326                  * should have selected the "Other:" radio button, to
327                  * keep things consistent.)
328                  */
329                 dlg_listbox_select(ctrl, dlg, curentry);
330             } else {
331                 /*
332                  * If the currently configured protocol is one of the
333                  * radio buttons, we must still ensure *something* is
334                  * selected in the list box. The sensible default is
335                  * the first list element, which be_*.c ought to have
336                  * arranged to be the 'runner-up' in protocol
337                  * popularity out of the ones relegated to the list
338                  * box.
339                  *
340                  * We don't make much effort to retain the state of
341                  * the list box when it doesn't correspond to an
342                  * actual protocol. So it's easy for this case to be
343                  * reached as a side effect of other actions, e.g.
344                  * loading a saved session that has a radio-button
345                  * protocol configured.
346                  */
347                 dlg_listbox_select(ctrl, dlg, 0);
348             }
349             dlg_update_done(ctrl, dlg);
350         }
351 
352         hp->mid_refresh = false;
353     } else if (!hp->mid_refresh) {
354         /*
355          * Potentially update Conf from the states of the controls.
356          */
357         int newproto = curproto;
358 
359         if (event == EVENT_VALCHANGE && ctrl == hp->protradio) {
360             int button = dlg_radiobutton_get(ctrl, dlg);
361             assert(button >= 0 && button < ctrl->radio.nbuttons);
362             if (ctrl->radio.buttondata[button].i == -1) {
363                 /*
364                  * The 'Other' radio button was selected, which means we
365                  * have to set CONF_protocol based on the currently
366                  * selected list box entry.
367                  *
368                  * (We conditionalise this on there _being_ a selected
369                  * list box entry. I hope the case where nothing is
370                  * selected can't actually come up except during
371                  * initialisation, and I also hope that hp->mid_session
372                  * will prevent that case from getting here. But as a
373                  * last-ditch fallback, this if statement should at least
374                  * guarantee that we don't pass a nonsense value to
375                  * dlg_listbox_getid.)
376                  */
377                 int i = dlg_listbox_index(hp->protlist, dlg);
378                 if (i >= 0)
379                     newproto = dlg_listbox_getid(hp->protlist, dlg, i);
380             } else {
381                 newproto = ctrl->radio.buttondata[button].i;
382             }
383         } else if (event == EVENT_SELCHANGE && ctrl == hp->protlist) {
384             int i = dlg_listbox_index(ctrl, dlg);
385             if (i >= 0) {
386                 newproto = dlg_listbox_getid(ctrl, dlg, i);
387                 /* Select the "Other" radio button, too */
388                 dlg_radiobutton_set(hp->protradio, dlg,
389                                     hp->protradio->radio.nbuttons-1);
390             }
391         }
392 
393         if (newproto != curproto) {
394             conf_set_int(conf, CONF_protocol, newproto);
395 
396             const struct BackendVtable *cvt = backend_vt_from_proto(curproto);
397             const struct BackendVtable *nvt = backend_vt_from_proto(newproto);
398             assert(cvt);
399             assert(nvt);
400             /*
401              * Iff the user hasn't changed the port from the old
402              * protocol's default, update it with the new protocol's
403              * default.
404              *
405              * (This includes a "default" of 0, implying that there is
406              * no sensible default for that protocol; in this case
407              * it's displayed as a blank.)
408              *
409              * This helps with the common case of tabbing through the
410              * controls in order and setting a non-default port before
411              * getting to the protocol; we want that non-default port
412              * to be preserved.
413              */
414             int port = conf_get_int(conf, CONF_port);
415             if (port == cvt->default_port)
416                 conf_set_int(conf, CONF_port, nvt->default_port);
417 
418             dlg_refresh(hp->host, dlg);
419             dlg_refresh(hp->port, dlg);
420         }
421     }
422 }
423 
loggingbuttons_handler(union control * ctrl,dlgparam * dlg,void * data,int event)424 static void loggingbuttons_handler(union control *ctrl, dlgparam *dlg,
425                                    void *data, int event)
426 {
427     int button;
428     Conf *conf = (Conf *)data;
429     /* This function works just like the standard radio-button handler,
430      * but it has to fall back to "no logging" in situations where the
431      * configured logging type isn't applicable.
432      */
433     if (event == EVENT_REFRESH) {
434         int logtype = conf_get_int(conf, CONF_logtype);
435 
436         for (button = 0; button < ctrl->radio.nbuttons; button++)
437             if (logtype == ctrl->radio.buttondata[button].i)
438                 break;
439 
440         /* We fell off the end, so we lack the configured logging type */
441         if (button == ctrl->radio.nbuttons) {
442             button = 0;
443             conf_set_int(conf, CONF_logtype, LGTYP_NONE);
444         }
445         dlg_radiobutton_set(ctrl, dlg, button);
446     } else if (event == EVENT_VALCHANGE) {
447         button = dlg_radiobutton_get(ctrl, dlg);
448         assert(button >= 0 && button < ctrl->radio.nbuttons);
449         conf_set_int(conf, CONF_logtype, ctrl->radio.buttondata[button].i);
450     }
451 }
452 
numeric_keypad_handler(union control * ctrl,dlgparam * dlg,void * data,int event)453 static void numeric_keypad_handler(union control *ctrl, dlgparam *dlg,
454                                    void *data, int event)
455 {
456     int button;
457     Conf *conf = (Conf *)data;
458     /*
459      * This function works much like the standard radio button
460      * handler, but it has to handle two fields in Conf.
461      */
462     if (event == EVENT_REFRESH) {
463         if (conf_get_bool(conf, CONF_nethack_keypad))
464             button = 2;
465         else if (conf_get_bool(conf, CONF_app_keypad))
466             button = 1;
467         else
468             button = 0;
469         assert(button < ctrl->radio.nbuttons);
470         dlg_radiobutton_set(ctrl, dlg, button);
471     } else if (event == EVENT_VALCHANGE) {
472         button = dlg_radiobutton_get(ctrl, dlg);
473         assert(button >= 0 && button < ctrl->radio.nbuttons);
474         if (button == 2) {
475             conf_set_bool(conf, CONF_app_keypad, false);
476             conf_set_bool(conf, CONF_nethack_keypad, true);
477         } else {
478             conf_set_bool(conf, CONF_app_keypad, (button != 0));
479             conf_set_bool(conf, CONF_nethack_keypad, false);
480         }
481     }
482 }
483 
cipherlist_handler(union control * ctrl,dlgparam * dlg,void * data,int event)484 static void cipherlist_handler(union control *ctrl, dlgparam *dlg,
485                                void *data, int event)
486 {
487     Conf *conf = (Conf *)data;
488     if (event == EVENT_REFRESH) {
489         int i;
490 
491         static const struct { const char *s; int c; } ciphers[] = {
492             { "ChaCha20 (SSH-2 only)",  CIPHER_CHACHA20 },
493             { "3DES",                   CIPHER_3DES },
494             { "Blowfish",               CIPHER_BLOWFISH },
495             { "DES",                    CIPHER_DES },
496             { "AES (SSH-2 only)",       CIPHER_AES },
497             { "Arcfour (SSH-2 only)",   CIPHER_ARCFOUR },
498             { "-- warn below here --",  CIPHER_WARN }
499         };
500 
501         /* Set up the "selected ciphers" box. */
502         /* (cipherlist assumed to contain all ciphers) */
503         dlg_update_start(ctrl, dlg);
504         dlg_listbox_clear(ctrl, dlg);
505         for (i = 0; i < CIPHER_MAX; i++) {
506             int c = conf_get_int_int(conf, CONF_ssh_cipherlist, i);
507             int j;
508             const char *cstr = NULL;
509             for (j = 0; j < (sizeof ciphers) / (sizeof ciphers[0]); j++) {
510                 if (ciphers[j].c == c) {
511                     cstr = ciphers[j].s;
512                     break;
513                 }
514             }
515             dlg_listbox_addwithid(ctrl, dlg, cstr, c);
516         }
517         dlg_update_done(ctrl, dlg);
518 
519     } else if (event == EVENT_VALCHANGE) {
520         int i;
521 
522         /* Update array to match the list box. */
523         for (i=0; i < CIPHER_MAX; i++)
524             conf_set_int_int(conf, CONF_ssh_cipherlist, i,
525                              dlg_listbox_getid(ctrl, dlg, i));
526     }
527 }
528 
529 #ifndef NO_GSSAPI
gsslist_handler(union control * ctrl,dlgparam * dlg,void * data,int event)530 static void gsslist_handler(union control *ctrl, dlgparam *dlg,
531                             void *data, int event)
532 {
533     Conf *conf = (Conf *)data;
534     if (event == EVENT_REFRESH) {
535         int i;
536 
537         dlg_update_start(ctrl, dlg);
538         dlg_listbox_clear(ctrl, dlg);
539         for (i = 0; i < ngsslibs; i++) {
540             int id = conf_get_int_int(conf, CONF_ssh_gsslist, i);
541             assert(id >= 0 && id < ngsslibs);
542             dlg_listbox_addwithid(ctrl, dlg, gsslibnames[id], id);
543         }
544         dlg_update_done(ctrl, dlg);
545 
546     } else if (event == EVENT_VALCHANGE) {
547         int i;
548 
549         /* Update array to match the list box. */
550         for (i=0; i < ngsslibs; i++)
551             conf_set_int_int(conf, CONF_ssh_gsslist, i,
552                              dlg_listbox_getid(ctrl, dlg, i));
553     }
554 }
555 #endif
556 
kexlist_handler(union control * ctrl,dlgparam * dlg,void * data,int event)557 static void kexlist_handler(union control *ctrl, dlgparam *dlg,
558                             void *data, int event)
559 {
560     Conf *conf = (Conf *)data;
561     if (event == EVENT_REFRESH) {
562         int i;
563 
564         static const struct { const char *s; int k; } kexes[] = {
565             { "Diffie-Hellman group 1",         KEX_DHGROUP1 },
566             { "Diffie-Hellman group 14",        KEX_DHGROUP14 },
567             { "Diffie-Hellman group exchange",  KEX_DHGEX },
568             { "RSA-based key exchange",         KEX_RSA },
569             { "ECDH key exchange",              KEX_ECDH },
570             { "-- warn below here --",          KEX_WARN }
571         };
572 
573         /* Set up the "kex preference" box. */
574         /* (kexlist assumed to contain all algorithms) */
575         dlg_update_start(ctrl, dlg);
576         dlg_listbox_clear(ctrl, dlg);
577         for (i = 0; i < KEX_MAX; i++) {
578             int k = conf_get_int_int(conf, CONF_ssh_kexlist, i);
579             int j;
580             const char *kstr = NULL;
581             for (j = 0; j < (sizeof kexes) / (sizeof kexes[0]); j++) {
582                 if (kexes[j].k == k) {
583                     kstr = kexes[j].s;
584                     break;
585                 }
586             }
587             dlg_listbox_addwithid(ctrl, dlg, kstr, k);
588         }
589         dlg_update_done(ctrl, dlg);
590 
591     } else if (event == EVENT_VALCHANGE) {
592         int i;
593 
594         /* Update array to match the list box. */
595         for (i=0; i < KEX_MAX; i++)
596             conf_set_int_int(conf, CONF_ssh_kexlist, i,
597                              dlg_listbox_getid(ctrl, dlg, i));
598     }
599 }
600 
hklist_handler(union control * ctrl,dlgparam * dlg,void * data,int event)601 static void hklist_handler(union control *ctrl, dlgparam *dlg,
602                             void *data, int event)
603 {
604     Conf *conf = (Conf *)data;
605     if (event == EVENT_REFRESH) {
606         int i;
607 
608         static const struct { const char *s; int k; } hks[] = {
609             { "Ed25519",               HK_ED25519 },
610             { "Ed448",                 HK_ED448 },
611             { "ECDSA",                 HK_ECDSA },
612             { "DSA",                   HK_DSA },
613             { "RSA",                   HK_RSA },
614             { "-- warn below here --", HK_WARN }
615         };
616 
617         /* Set up the "host key preference" box. */
618         /* (hklist assumed to contain all algorithms) */
619         dlg_update_start(ctrl, dlg);
620         dlg_listbox_clear(ctrl, dlg);
621         for (i = 0; i < HK_MAX; i++) {
622             int k = conf_get_int_int(conf, CONF_ssh_hklist, i);
623             int j;
624             const char *kstr = NULL;
625             for (j = 0; j < lenof(hks); j++) {
626                 if (hks[j].k == k) {
627                     kstr = hks[j].s;
628                     break;
629                 }
630             }
631             dlg_listbox_addwithid(ctrl, dlg, kstr, k);
632         }
633         dlg_update_done(ctrl, dlg);
634 
635     } else if (event == EVENT_VALCHANGE) {
636         int i;
637 
638         /* Update array to match the list box. */
639         for (i=0; i < HK_MAX; i++)
640             conf_set_int_int(conf, CONF_ssh_hklist, i,
641                              dlg_listbox_getid(ctrl, dlg, i));
642     }
643 }
644 
printerbox_handler(union control * ctrl,dlgparam * dlg,void * data,int event)645 static void printerbox_handler(union control *ctrl, dlgparam *dlg,
646                                void *data, int event)
647 {
648     Conf *conf = (Conf *)data;
649     if (event == EVENT_REFRESH) {
650         int nprinters, i;
651         printer_enum *pe;
652         const char *printer;
653 
654         dlg_update_start(ctrl, dlg);
655         /*
656          * Some backends may wish to disable the drop-down list on
657          * this edit box. Be prepared for this.
658          */
659         if (ctrl->editbox.has_list) {
660             dlg_listbox_clear(ctrl, dlg);
661             dlg_listbox_add(ctrl, dlg, PRINTER_DISABLED_STRING);
662             pe = printer_start_enum(&nprinters);
663             for (i = 0; i < nprinters; i++)
664                 dlg_listbox_add(ctrl, dlg, printer_get_name(pe, i));
665             printer_finish_enum(pe);
666         }
667         printer = conf_get_str(conf, CONF_printer);
668         if (!printer)
669             printer = PRINTER_DISABLED_STRING;
670         dlg_editbox_set(ctrl, dlg, printer);
671         dlg_update_done(ctrl, dlg);
672     } else if (event == EVENT_VALCHANGE) {
673         char *printer = dlg_editbox_get(ctrl, dlg);
674         if (!strcmp(printer, PRINTER_DISABLED_STRING))
675             printer[0] = '\0';
676         conf_set_str(conf, CONF_printer, printer);
677         sfree(printer);
678     }
679 }
680 
codepage_handler(union control * ctrl,dlgparam * dlg,void * data,int event)681 static void codepage_handler(union control *ctrl, dlgparam *dlg,
682                              void *data, int event)
683 {
684     Conf *conf = (Conf *)data;
685     if (event == EVENT_REFRESH) {
686         int i;
687         const char *cp, *thiscp;
688         dlg_update_start(ctrl, dlg);
689         thiscp = cp_name(decode_codepage(conf_get_str(conf,
690                                                       CONF_line_codepage)));
691         dlg_listbox_clear(ctrl, dlg);
692         for (i = 0; (cp = cp_enumerate(i)) != NULL; i++)
693             dlg_listbox_add(ctrl, dlg, cp);
694         dlg_editbox_set(ctrl, dlg, thiscp);
695         conf_set_str(conf, CONF_line_codepage, thiscp);
696         dlg_update_done(ctrl, dlg);
697     } else if (event == EVENT_VALCHANGE) {
698         char *codepage = dlg_editbox_get(ctrl, dlg);
699         conf_set_str(conf, CONF_line_codepage,
700                      cp_name(decode_codepage(codepage)));
701         sfree(codepage);
702     }
703 }
704 
sshbug_handler(union control * ctrl,dlgparam * dlg,void * data,int event)705 static void sshbug_handler(union control *ctrl, dlgparam *dlg,
706                            void *data, int event)
707 {
708     Conf *conf = (Conf *)data;
709     if (event == EVENT_REFRESH) {
710         /*
711          * We must fetch the previously configured value from the Conf
712          * before we start modifying the drop-down list, otherwise the
713          * spurious SELCHANGE we trigger in the process will overwrite
714          * the value we wanted to keep.
715          */
716         int oldconf = conf_get_int(conf, ctrl->listbox.context.i);
717         dlg_update_start(ctrl, dlg);
718         dlg_listbox_clear(ctrl, dlg);
719         dlg_listbox_addwithid(ctrl, dlg, "Auto", AUTO);
720         dlg_listbox_addwithid(ctrl, dlg, "Off", FORCE_OFF);
721         dlg_listbox_addwithid(ctrl, dlg, "On", FORCE_ON);
722         switch (oldconf) {
723           case AUTO:      dlg_listbox_select(ctrl, dlg, 0); break;
724           case FORCE_OFF: dlg_listbox_select(ctrl, dlg, 1); break;
725           case FORCE_ON:  dlg_listbox_select(ctrl, dlg, 2); break;
726         }
727         dlg_update_done(ctrl, dlg);
728     } else if (event == EVENT_SELCHANGE) {
729         int i = dlg_listbox_index(ctrl, dlg);
730         if (i < 0)
731             i = AUTO;
732         else
733             i = dlg_listbox_getid(ctrl, dlg, i);
734         conf_set_int(conf, ctrl->listbox.context.i, i);
735     }
736 }
737 
738 struct sessionsaver_data {
739     union control *editbox, *listbox, *loadbutton, *savebutton, *delbutton;
740     union control *okbutton, *cancelbutton;
741     struct sesslist sesslist;
742     bool midsession;
743     char *savedsession;     /* the current contents of ssd->editbox */
744 };
745 
sessionsaver_data_free(void * ssdv)746 static void sessionsaver_data_free(void *ssdv)
747 {
748     struct sessionsaver_data *ssd = (struct sessionsaver_data *)ssdv;
749     get_sesslist(&ssd->sesslist, false);
750     sfree(ssd->savedsession);
751     sfree(ssd);
752 }
753 
754 /*
755  * Helper function to load the session selected in the list box, if
756  * any, as this is done in more than one place below. Returns 0 for
757  * failure.
758  */
load_selected_session(struct sessionsaver_data * ssd,dlgparam * dlg,Conf * conf,bool * maybe_launch)759 static bool load_selected_session(
760     struct sessionsaver_data *ssd,
761     dlgparam *dlg, Conf *conf, bool *maybe_launch)
762 {
763     int i = dlg_listbox_index(ssd->listbox, dlg);
764     bool isdef;
765     if (i < 0) {
766         dlg_beep(dlg);
767         return false;
768     }
769     isdef = !strcmp(ssd->sesslist.sessions[i], "Default Settings");
770     load_settings(ssd->sesslist.sessions[i], conf);
771     sfree(ssd->savedsession);
772     ssd->savedsession = dupstr(isdef ? "" : ssd->sesslist.sessions[i]);
773     if (maybe_launch)
774         *maybe_launch = !isdef;
775     dlg_refresh(NULL, dlg);
776     /* Restore the selection, which might have been clobbered by
777      * changing the value of the edit box. */
778     dlg_listbox_select(ssd->listbox, dlg, i);
779     return true;
780 }
781 
sessionsaver_handler(union control * ctrl,dlgparam * dlg,void * data,int event)782 static void sessionsaver_handler(union control *ctrl, dlgparam *dlg,
783                                  void *data, int event)
784 {
785     Conf *conf = (Conf *)data;
786     struct sessionsaver_data *ssd =
787         (struct sessionsaver_data *)ctrl->generic.context.p;
788 
789     if (event == EVENT_REFRESH) {
790         if (ctrl == ssd->editbox) {
791             dlg_editbox_set(ctrl, dlg, ssd->savedsession);
792         } else if (ctrl == ssd->listbox) {
793             int i;
794             dlg_update_start(ctrl, dlg);
795             dlg_listbox_clear(ctrl, dlg);
796             for (i = 0; i < ssd->sesslist.nsessions; i++)
797                 dlg_listbox_add(ctrl, dlg, ssd->sesslist.sessions[i]);
798             dlg_update_done(ctrl, dlg);
799         }
800     } else if (event == EVENT_VALCHANGE) {
801         int top, bottom, halfway, i;
802         if (ctrl == ssd->editbox) {
803             sfree(ssd->savedsession);
804             ssd->savedsession = dlg_editbox_get(ctrl, dlg);
805             top = ssd->sesslist.nsessions;
806             bottom = -1;
807             while (top-bottom > 1) {
808                 halfway = (top+bottom)/2;
809                 i = strcmp(ssd->savedsession, ssd->sesslist.sessions[halfway]);
810                 if (i <= 0 ) {
811                     top = halfway;
812                 } else {
813                     bottom = halfway;
814                 }
815             }
816             if (top == ssd->sesslist.nsessions) {
817                 top -= 1;
818             }
819             dlg_listbox_select(ssd->listbox, dlg, top);
820         }
821     } else if (event == EVENT_ACTION) {
822         bool mbl = false;
823         if (!ssd->midsession &&
824             (ctrl == ssd->listbox ||
825              (ssd->loadbutton && ctrl == ssd->loadbutton))) {
826             /*
827              * The user has double-clicked a session, or hit Load.
828              * We must load the selected session, and then
829              * terminate the configuration dialog _if_ there was a
830              * double-click on the list box _and_ that session
831              * contains a hostname.
832              */
833             if (load_selected_session(ssd, dlg, conf, &mbl) &&
834                 (mbl && ctrl == ssd->listbox && conf_launchable(conf))) {
835                 dlg_end(dlg, 1);       /* it's all over, and succeeded */
836             }
837         } else if (ctrl == ssd->savebutton) {
838             bool isdef = !strcmp(ssd->savedsession, "Default Settings");
839             if (!ssd->savedsession[0]) {
840                 int i = dlg_listbox_index(ssd->listbox, dlg);
841                 if (i < 0) {
842                     dlg_beep(dlg);
843                     return;
844                 }
845                 isdef = !strcmp(ssd->sesslist.sessions[i], "Default Settings");
846                 sfree(ssd->savedsession);
847                 ssd->savedsession = dupstr(isdef ? "" :
848                                            ssd->sesslist.sessions[i]);
849             }
850             {
851                 char *errmsg = save_settings(ssd->savedsession, conf);
852                 if (errmsg) {
853                     dlg_error_msg(dlg, errmsg);
854                     sfree(errmsg);
855                 }
856             }
857             get_sesslist(&ssd->sesslist, false);
858             get_sesslist(&ssd->sesslist, true);
859             dlg_refresh(ssd->editbox, dlg);
860             dlg_refresh(ssd->listbox, dlg);
861         } else if (!ssd->midsession &&
862                    ssd->delbutton && ctrl == ssd->delbutton) {
863             int i = dlg_listbox_index(ssd->listbox, dlg);
864             if (i <= 0) {
865                 dlg_beep(dlg);
866             } else {
867                 del_settings(ssd->sesslist.sessions[i]);
868                 get_sesslist(&ssd->sesslist, false);
869                 get_sesslist(&ssd->sesslist, true);
870                 dlg_refresh(ssd->listbox, dlg);
871             }
872         } else if (ctrl == ssd->okbutton) {
873             if (ssd->midsession) {
874                 /* In a mid-session Change Settings, Apply is always OK. */
875                 dlg_end(dlg, 1);
876                 return;
877             }
878             /*
879              * Annoying special case. If the `Open' button is
880              * pressed while no host name is currently set, _and_
881              * the session list previously had the focus, _and_
882              * there was a session selected in that which had a
883              * valid host name in it, then load it and go.
884              */
885             if (dlg_last_focused(ctrl, dlg) == ssd->listbox &&
886                 !conf_launchable(conf) && dlg_is_visible(ssd->listbox, dlg)) {
887                 Conf *conf2 = conf_new();
888                 bool mbl = false;
889                 if (!load_selected_session(ssd, dlg, conf2, &mbl)) {
890                     dlg_beep(dlg);
891                     conf_free(conf2);
892                     return;
893                 }
894                 /* If at this point we have a valid session, go! */
895                 if (mbl && conf_launchable(conf2)) {
896                     conf_copy_into(conf, conf2);
897                     dlg_end(dlg, 1);
898                 } else
899                     dlg_beep(dlg);
900 
901                 conf_free(conf2);
902                 return;
903             }
904 
905             /*
906              * Otherwise, do the normal thing: if we have a valid
907              * session, get going.
908              */
909             if (conf_launchable(conf)) {
910                 dlg_end(dlg, 1);
911             } else
912                 dlg_beep(dlg);
913         } else if (ctrl == ssd->cancelbutton) {
914             dlg_end(dlg, 0);
915         }
916     }
917 }
918 
919 struct charclass_data {
920     union control *listbox, *editbox, *button;
921 };
922 
charclass_handler(union control * ctrl,dlgparam * dlg,void * data,int event)923 static void charclass_handler(union control *ctrl, dlgparam *dlg,
924                               void *data, int event)
925 {
926     Conf *conf = (Conf *)data;
927     struct charclass_data *ccd =
928         (struct charclass_data *)ctrl->generic.context.p;
929 
930     if (event == EVENT_REFRESH) {
931         if (ctrl == ccd->listbox) {
932             int i;
933             dlg_update_start(ctrl, dlg);
934             dlg_listbox_clear(ctrl, dlg);
935             for (i = 0; i < 128; i++) {
936                 char str[100];
937                 sprintf(str, "%d\t(0x%02X)\t%c\t%d", i, i,
938                         (i >= 0x21 && i != 0x7F) ? i : ' ',
939                         conf_get_int_int(conf, CONF_wordness, i));
940                 dlg_listbox_add(ctrl, dlg, str);
941             }
942             dlg_update_done(ctrl, dlg);
943         }
944     } else if (event == EVENT_ACTION) {
945         if (ctrl == ccd->button) {
946             char *str;
947             int i, n;
948             str = dlg_editbox_get(ccd->editbox, dlg);
949             n = atoi(str);
950             sfree(str);
951             for (i = 0; i < 128; i++) {
952                 if (dlg_listbox_issel(ccd->listbox, dlg, i))
953                     conf_set_int_int(conf, CONF_wordness, i, n);
954             }
955             dlg_refresh(ccd->listbox, dlg);
956         }
957     }
958 }
959 
960 struct colour_data {
961     union control *listbox, *redit, *gedit, *bedit, *button;
962 };
963 
964 /* Array of the user-visible colour names defined in the list macro in
965  * putty.h */
966 static const char *const colours[] = {
967     #define CONF_COLOUR_NAME_DECL(id,name) name,
968     CONF_COLOUR_LIST(CONF_COLOUR_NAME_DECL)
969     #undef CONF_COLOUR_NAME_DECL
970 };
971 
colour_handler(union control * ctrl,dlgparam * dlg,void * data,int event)972 static void colour_handler(union control *ctrl, dlgparam *dlg,
973                             void *data, int event)
974 {
975     Conf *conf = (Conf *)data;
976     struct colour_data *cd =
977         (struct colour_data *)ctrl->generic.context.p;
978     bool update = false, clear = false;
979     int r, g, b;
980 
981     if (event == EVENT_REFRESH) {
982         if (ctrl == cd->listbox) {
983             int i;
984             dlg_update_start(ctrl, dlg);
985             dlg_listbox_clear(ctrl, dlg);
986             for (i = 0; i < lenof(colours); i++)
987                 dlg_listbox_add(ctrl, dlg, colours[i]);
988             dlg_update_done(ctrl, dlg);
989             clear = true;
990             update = true;
991         }
992     } else if (event == EVENT_SELCHANGE) {
993         if (ctrl == cd->listbox) {
994             /* The user has selected a colour. Update the RGB text. */
995             int i = dlg_listbox_index(ctrl, dlg);
996             if (i < 0) {
997                 clear = true;
998             } else {
999                 clear = false;
1000                 r = conf_get_int_int(conf, CONF_colours, i*3+0);
1001                 g = conf_get_int_int(conf, CONF_colours, i*3+1);
1002                 b = conf_get_int_int(conf, CONF_colours, i*3+2);
1003             }
1004             update = true;
1005         }
1006     } else if (event == EVENT_VALCHANGE) {
1007         if (ctrl == cd->redit || ctrl == cd->gedit || ctrl == cd->bedit) {
1008             /* The user has changed the colour using the edit boxes. */
1009             char *str;
1010             int i, cval;
1011 
1012             str = dlg_editbox_get(ctrl, dlg);
1013             cval = atoi(str);
1014             sfree(str);
1015             if (cval > 255) cval = 255;
1016             if (cval < 0)   cval = 0;
1017 
1018             i = dlg_listbox_index(cd->listbox, dlg);
1019             if (i >= 0) {
1020                 if (ctrl == cd->redit)
1021                     conf_set_int_int(conf, CONF_colours, i*3+0, cval);
1022                 else if (ctrl == cd->gedit)
1023                     conf_set_int_int(conf, CONF_colours, i*3+1, cval);
1024                 else if (ctrl == cd->bedit)
1025                     conf_set_int_int(conf, CONF_colours, i*3+2, cval);
1026             }
1027         }
1028     } else if (event == EVENT_ACTION) {
1029         if (ctrl == cd->button) {
1030             int i = dlg_listbox_index(cd->listbox, dlg);
1031             if (i < 0) {
1032                 dlg_beep(dlg);
1033                 return;
1034             }
1035             /*
1036              * Start a colour selector, which will send us an
1037              * EVENT_CALLBACK when it's finished and allow us to
1038              * pick up the results.
1039              */
1040             dlg_coloursel_start(ctrl, dlg,
1041                                 conf_get_int_int(conf, CONF_colours, i*3+0),
1042                                 conf_get_int_int(conf, CONF_colours, i*3+1),
1043                                 conf_get_int_int(conf, CONF_colours, i*3+2));
1044         }
1045     } else if (event == EVENT_CALLBACK) {
1046         if (ctrl == cd->button) {
1047             int i = dlg_listbox_index(cd->listbox, dlg);
1048             /*
1049              * Collect the results of the colour selector. Will
1050              * return nonzero on success, or zero if the colour
1051              * selector did nothing (user hit Cancel, for example).
1052              */
1053             if (dlg_coloursel_results(ctrl, dlg, &r, &g, &b)) {
1054                 conf_set_int_int(conf, CONF_colours, i*3+0, r);
1055                 conf_set_int_int(conf, CONF_colours, i*3+1, g);
1056                 conf_set_int_int(conf, CONF_colours, i*3+2, b);
1057                 clear = false;
1058                 update = true;
1059             }
1060         }
1061     }
1062 
1063     if (update) {
1064         if (clear) {
1065             dlg_editbox_set(cd->redit, dlg, "");
1066             dlg_editbox_set(cd->gedit, dlg, "");
1067             dlg_editbox_set(cd->bedit, dlg, "");
1068         } else {
1069             char buf[40];
1070             sprintf(buf, "%d", r); dlg_editbox_set(cd->redit, dlg, buf);
1071             sprintf(buf, "%d", g); dlg_editbox_set(cd->gedit, dlg, buf);
1072             sprintf(buf, "%d", b); dlg_editbox_set(cd->bedit, dlg, buf);
1073         }
1074     }
1075 }
1076 
1077 struct ttymodes_data {
1078     union control *valradio, *valbox, *setbutton, *listbox;
1079 };
1080 
ttymodes_handler(union control * ctrl,dlgparam * dlg,void * data,int event)1081 static void ttymodes_handler(union control *ctrl, dlgparam *dlg,
1082                              void *data, int event)
1083 {
1084     Conf *conf = (Conf *)data;
1085     struct ttymodes_data *td =
1086         (struct ttymodes_data *)ctrl->generic.context.p;
1087 
1088     if (event == EVENT_REFRESH) {
1089         if (ctrl == td->listbox) {
1090             char *key, *val;
1091             dlg_update_start(ctrl, dlg);
1092             dlg_listbox_clear(ctrl, dlg);
1093             for (val = conf_get_str_strs(conf, CONF_ttymodes, NULL, &key);
1094                  val != NULL;
1095                  val = conf_get_str_strs(conf, CONF_ttymodes, key, &key)) {
1096                 char *disp = dupprintf("%s\t%s", key,
1097                                        (val[0] == 'A') ? "(auto)" :
1098                                        ((val[0] == 'N') ? "(don't send)"
1099                                                         : val+1));
1100                 dlg_listbox_add(ctrl, dlg, disp);
1101                 sfree(disp);
1102             }
1103             dlg_update_done(ctrl, dlg);
1104         } else if (ctrl == td->valradio) {
1105             dlg_radiobutton_set(ctrl, dlg, 0);
1106         }
1107     } else if (event == EVENT_SELCHANGE) {
1108         if (ctrl == td->listbox) {
1109             int ind = dlg_listbox_index(td->listbox, dlg);
1110             char *val;
1111             if (ind < 0) {
1112                 return; /* no item selected */
1113             }
1114             val = conf_get_str_str(conf, CONF_ttymodes,
1115                                    conf_get_str_nthstrkey(conf, CONF_ttymodes,
1116                                                           ind));
1117             assert(val != NULL);
1118             /* Do this first to defuse side-effects on radio buttons: */
1119             dlg_editbox_set(td->valbox, dlg, val+1);
1120             dlg_radiobutton_set(td->valradio, dlg,
1121                                 val[0] == 'A' ? 0 : (val[0] == 'N' ? 1 : 2));
1122         }
1123     } else if (event == EVENT_VALCHANGE) {
1124         if (ctrl == td->valbox) {
1125             /* If they're editing the text box, we assume they want its
1126              * value to be used. */
1127             dlg_radiobutton_set(td->valradio, dlg, 2);
1128         }
1129     } else if (event == EVENT_ACTION) {
1130         if (ctrl == td->setbutton) {
1131             int ind = dlg_listbox_index(td->listbox, dlg);
1132             const char *key;
1133             char *str, *val;
1134             char type;
1135 
1136             {
1137                 const char types[] = {'A', 'N', 'V'};
1138                 int button = dlg_radiobutton_get(td->valradio, dlg);
1139                 assert(button >= 0 && button < lenof(types));
1140                 type = types[button];
1141             }
1142 
1143             /* Construct new entry */
1144             if (ind >= 0) {
1145                 key = conf_get_str_nthstrkey(conf, CONF_ttymodes, ind);
1146                 str = (type == 'V' ? dlg_editbox_get(td->valbox, dlg)
1147                                    : dupstr(""));
1148                 val = dupprintf("%c%s", type, str);
1149                 sfree(str);
1150                 conf_set_str_str(conf, CONF_ttymodes, key, val);
1151                 sfree(val);
1152                 dlg_refresh(td->listbox, dlg);
1153                 dlg_listbox_select(td->listbox, dlg, ind);
1154             } else {
1155                 /* Not a multisel listbox, so this means nothing selected */
1156                 dlg_beep(dlg);
1157             }
1158         }
1159     }
1160 }
1161 
1162 struct environ_data {
1163     union control *varbox, *valbox, *addbutton, *rembutton, *listbox;
1164 };
1165 
environ_handler(union control * ctrl,dlgparam * dlg,void * data,int event)1166 static void environ_handler(union control *ctrl, dlgparam *dlg,
1167                             void *data, int event)
1168 {
1169     Conf *conf = (Conf *)data;
1170     struct environ_data *ed =
1171         (struct environ_data *)ctrl->generic.context.p;
1172 
1173     if (event == EVENT_REFRESH) {
1174         if (ctrl == ed->listbox) {
1175             char *key, *val;
1176             dlg_update_start(ctrl, dlg);
1177             dlg_listbox_clear(ctrl, dlg);
1178             for (val = conf_get_str_strs(conf, CONF_environmt, NULL, &key);
1179                  val != NULL;
1180                  val = conf_get_str_strs(conf, CONF_environmt, key, &key)) {
1181                 char *p = dupprintf("%s\t%s", key, val);
1182                 dlg_listbox_add(ctrl, dlg, p);
1183                 sfree(p);
1184             }
1185             dlg_update_done(ctrl, dlg);
1186         }
1187     } else if (event == EVENT_ACTION) {
1188         if (ctrl == ed->addbutton) {
1189             char *key, *val, *str;
1190             key = dlg_editbox_get(ed->varbox, dlg);
1191             if (!*key) {
1192                 sfree(key);
1193                 dlg_beep(dlg);
1194                 return;
1195             }
1196             val = dlg_editbox_get(ed->valbox, dlg);
1197             if (!*val) {
1198                 sfree(key);
1199                 sfree(val);
1200                 dlg_beep(dlg);
1201                 return;
1202             }
1203             conf_set_str_str(conf, CONF_environmt, key, val);
1204             str = dupcat(key, "\t", val);
1205             dlg_editbox_set(ed->varbox, dlg, "");
1206             dlg_editbox_set(ed->valbox, dlg, "");
1207             sfree(str);
1208             sfree(key);
1209             sfree(val);
1210             dlg_refresh(ed->listbox, dlg);
1211         } else if (ctrl == ed->rembutton) {
1212             int i = dlg_listbox_index(ed->listbox, dlg);
1213             if (i < 0) {
1214                 dlg_beep(dlg);
1215             } else {
1216                 char *key, *val;
1217 
1218                 key = conf_get_str_nthstrkey(conf, CONF_environmt, i);
1219                 if (key) {
1220                     /* Populate controls with the entry we're about to delete
1221                      * for ease of editing */
1222                     val = conf_get_str_str(conf, CONF_environmt, key);
1223                     dlg_editbox_set(ed->varbox, dlg, key);
1224                     dlg_editbox_set(ed->valbox, dlg, val);
1225                     /* And delete it */
1226                     conf_del_str_str(conf, CONF_environmt, key);
1227                 }
1228             }
1229             dlg_refresh(ed->listbox, dlg);
1230         }
1231     }
1232 }
1233 
1234 struct portfwd_data {
1235     union control *addbutton, *rembutton, *listbox;
1236     union control *sourcebox, *destbox, *direction;
1237 #ifndef NO_IPV6
1238     union control *addressfamily;
1239 #endif
1240 };
1241 
portfwd_handler(union control * ctrl,dlgparam * dlg,void * data,int event)1242 static void portfwd_handler(union control *ctrl, dlgparam *dlg,
1243                             void *data, int event)
1244 {
1245     Conf *conf = (Conf *)data;
1246     struct portfwd_data *pfd =
1247         (struct portfwd_data *)ctrl->generic.context.p;
1248 
1249     if (event == EVENT_REFRESH) {
1250         if (ctrl == pfd->listbox) {
1251             char *key, *val;
1252             dlg_update_start(ctrl, dlg);
1253             dlg_listbox_clear(ctrl, dlg);
1254             for (val = conf_get_str_strs(conf, CONF_portfwd, NULL, &key);
1255                  val != NULL;
1256                  val = conf_get_str_strs(conf, CONF_portfwd, key, &key)) {
1257                 char *p;
1258                 if (!strcmp(val, "D")) {
1259                     char *L;
1260                     /*
1261                      * A dynamic forwarding is stored as L12345=D or
1262                      * 6L12345=D (since it's mutually exclusive with
1263                      * L12345=anything else), but displayed as D12345
1264                      * to match the fiction that 'Local', 'Remote' and
1265                      * 'Dynamic' are three distinct modes and also to
1266                      * align with OpenSSH's command line option syntax
1267                      * that people will already be used to. So, for
1268                      * display purposes, find the L in the key string
1269                      * and turn it into a D.
1270                      */
1271                     p = dupprintf("%s\t", key);
1272                     L = strchr(p, 'L');
1273                     if (L) *L = 'D';
1274                 } else
1275                     p = dupprintf("%s\t%s", key, val);
1276                 dlg_listbox_add(ctrl, dlg, p);
1277                 sfree(p);
1278             }
1279             dlg_update_done(ctrl, dlg);
1280         } else if (ctrl == pfd->direction) {
1281             /*
1282              * Default is Local.
1283              */
1284             dlg_radiobutton_set(ctrl, dlg, 0);
1285 #ifndef NO_IPV6
1286         } else if (ctrl == pfd->addressfamily) {
1287             dlg_radiobutton_set(ctrl, dlg, 0);
1288 #endif
1289         }
1290     } else if (event == EVENT_ACTION) {
1291         if (ctrl == pfd->addbutton) {
1292             const char *family, *type;
1293             char *src, *key, *val;
1294             int whichbutton;
1295 
1296 #ifndef NO_IPV6
1297             whichbutton = dlg_radiobutton_get(pfd->addressfamily, dlg);
1298             if (whichbutton == 1)
1299                 family = "4";
1300             else if (whichbutton == 2)
1301                 family = "6";
1302             else
1303 #endif
1304                 family = "";
1305 
1306             whichbutton = dlg_radiobutton_get(pfd->direction, dlg);
1307             if (whichbutton == 0)
1308                 type = "L";
1309             else if (whichbutton == 1)
1310                 type = "R";
1311             else
1312                 type = "D";
1313 
1314             src = dlg_editbox_get(pfd->sourcebox, dlg);
1315             if (!*src) {
1316                 dlg_error_msg(dlg, "You need to specify a source port number");
1317                 sfree(src);
1318                 return;
1319             }
1320             if (*type != 'D') {
1321                 val = dlg_editbox_get(pfd->destbox, dlg);
1322                 if (!*val || !host_strchr(val, ':')) {
1323                     dlg_error_msg(dlg,
1324                                   "You need to specify a destination address\n"
1325                                   "in the form \"host.name:port\"");
1326                     sfree(src);
1327                     sfree(val);
1328                     return;
1329                 }
1330             } else {
1331                 type = "L";
1332                 val = dupstr("D");     /* special case */
1333             }
1334 
1335             key = dupcat(family, type, src);
1336             sfree(src);
1337 
1338             if (conf_get_str_str_opt(conf, CONF_portfwd, key)) {
1339                 dlg_error_msg(dlg, "Specified forwarding already exists");
1340             } else {
1341                 conf_set_str_str(conf, CONF_portfwd, key, val);
1342             }
1343 
1344             sfree(key);
1345             sfree(val);
1346             dlg_refresh(pfd->listbox, dlg);
1347         } else if (ctrl == pfd->rembutton) {
1348             int i = dlg_listbox_index(pfd->listbox, dlg);
1349             if (i < 0) {
1350                 dlg_beep(dlg);
1351             } else {
1352                 char *key, *p;
1353                 const char *val;
1354 
1355                 key = conf_get_str_nthstrkey(conf, CONF_portfwd, i);
1356                 if (key) {
1357                     static const char *const afs = "A46";
1358                     static const char *const dirs = "LRD";
1359                     const char *afp;
1360                     int dir;
1361 #ifndef NO_IPV6
1362                     int idx;
1363 #endif
1364 
1365                     /* Populate controls with the entry we're about to delete
1366                      * for ease of editing */
1367                     p = key;
1368 
1369                     afp = strchr(afs, *p);
1370 #ifndef NO_IPV6
1371                     idx = afp ? afp-afs : 0;
1372 #endif
1373                     if (afp)
1374                         p++;
1375 #ifndef NO_IPV6
1376                     dlg_radiobutton_set(pfd->addressfamily, dlg, idx);
1377 #endif
1378 
1379                     dir = *p;
1380 
1381                     val = conf_get_str_str(conf, CONF_portfwd, key);
1382                     if (!strcmp(val, "D")) {
1383                         dir = 'D';
1384                         val = "";
1385                     }
1386 
1387                     dlg_radiobutton_set(pfd->direction, dlg,
1388                                         strchr(dirs, dir) - dirs);
1389                     p++;
1390 
1391                     dlg_editbox_set(pfd->sourcebox, dlg, p);
1392                     dlg_editbox_set(pfd->destbox, dlg, val);
1393                     /* And delete it */
1394                     conf_del_str_str(conf, CONF_portfwd, key);
1395                 }
1396             }
1397             dlg_refresh(pfd->listbox, dlg);
1398         }
1399     }
1400 }
1401 
1402 struct manual_hostkey_data {
1403     union control *addbutton, *rembutton, *listbox, *keybox;
1404 };
1405 
manual_hostkey_handler(union control * ctrl,dlgparam * dlg,void * data,int event)1406 static void manual_hostkey_handler(union control *ctrl, dlgparam *dlg,
1407                                    void *data, int event)
1408 {
1409     Conf *conf = (Conf *)data;
1410     struct manual_hostkey_data *mh =
1411         (struct manual_hostkey_data *)ctrl->generic.context.p;
1412 
1413     if (event == EVENT_REFRESH) {
1414         if (ctrl == mh->listbox) {
1415             char *key, *val;
1416             dlg_update_start(ctrl, dlg);
1417             dlg_listbox_clear(ctrl, dlg);
1418             for (val = conf_get_str_strs(conf, CONF_ssh_manual_hostkeys,
1419                                          NULL, &key);
1420                  val != NULL;
1421                  val = conf_get_str_strs(conf, CONF_ssh_manual_hostkeys,
1422                                          key, &key)) {
1423                 dlg_listbox_add(ctrl, dlg, key);
1424             }
1425             dlg_update_done(ctrl, dlg);
1426         }
1427     } else if (event == EVENT_ACTION) {
1428         if (ctrl == mh->addbutton) {
1429             char *key;
1430 
1431             key = dlg_editbox_get(mh->keybox, dlg);
1432             if (!*key) {
1433                 dlg_error_msg(dlg, "You need to specify a host key or "
1434                               "fingerprint");
1435                 sfree(key);
1436                 return;
1437             }
1438 
1439             if (!validate_manual_hostkey(key)) {
1440                 dlg_error_msg(dlg, "Host key is not in a valid format");
1441             } else if (conf_get_str_str_opt(conf, CONF_ssh_manual_hostkeys,
1442                                             key)) {
1443                 dlg_error_msg(dlg, "Specified host key is already listed");
1444             } else {
1445                 conf_set_str_str(conf, CONF_ssh_manual_hostkeys, key, "");
1446             }
1447 
1448             sfree(key);
1449             dlg_refresh(mh->listbox, dlg);
1450         } else if (ctrl == mh->rembutton) {
1451             int i = dlg_listbox_index(mh->listbox, dlg);
1452             if (i < 0) {
1453                 dlg_beep(dlg);
1454             } else {
1455                 char *key;
1456 
1457                 key = conf_get_str_nthstrkey(conf, CONF_ssh_manual_hostkeys, i);
1458                 if (key) {
1459                     dlg_editbox_set(mh->keybox, dlg, key);
1460                     /* And delete it */
1461                     conf_del_str_str(conf, CONF_ssh_manual_hostkeys, key);
1462                 }
1463             }
1464             dlg_refresh(mh->listbox, dlg);
1465         }
1466     }
1467 }
1468 
clipboard_selector_handler(union control * ctrl,dlgparam * dlg,void * data,int event)1469 static void clipboard_selector_handler(union control *ctrl, dlgparam *dlg,
1470                                        void *data, int event)
1471 {
1472     Conf *conf = (Conf *)data;
1473     int setting = ctrl->generic.context.i;
1474 #ifdef NAMED_CLIPBOARDS
1475     int strsetting = ctrl->editbox.context2.i;
1476 #endif
1477 
1478     static const struct {
1479         const char *name;
1480         int id;
1481     } options[] = {
1482         {"No action", CLIPUI_NONE},
1483         {CLIPNAME_IMPLICIT, CLIPUI_IMPLICIT},
1484         {CLIPNAME_EXPLICIT, CLIPUI_EXPLICIT},
1485     };
1486 
1487     if (event == EVENT_REFRESH) {
1488         int i, val = conf_get_int(conf, setting);
1489 
1490         dlg_update_start(ctrl, dlg);
1491         dlg_listbox_clear(ctrl, dlg);
1492 
1493 #ifdef NAMED_CLIPBOARDS
1494         for (i = 0; i < lenof(options); i++)
1495             dlg_listbox_add(ctrl, dlg, options[i].name);
1496         if (val == CLIPUI_CUSTOM) {
1497             const char *sval = conf_get_str(conf, strsetting);
1498             for (i = 0; i < lenof(options); i++)
1499                 if (!strcmp(sval, options[i].name))
1500                     break;             /* needs escaping */
1501             if (i < lenof(options) || sval[0] == '=') {
1502                 char *escaped = dupcat("=", sval);
1503                 dlg_editbox_set(ctrl, dlg, escaped);
1504                 sfree(escaped);
1505             } else {
1506                 dlg_editbox_set(ctrl, dlg, sval);
1507             }
1508         } else {
1509             dlg_editbox_set(ctrl, dlg, options[0].name); /* fallback */
1510             for (i = 0; i < lenof(options); i++)
1511                 if (val == options[i].id)
1512                     dlg_editbox_set(ctrl, dlg, options[i].name);
1513         }
1514 #else
1515         for (i = 0; i < lenof(options); i++)
1516             dlg_listbox_addwithid(ctrl, dlg, options[i].name, options[i].id);
1517         dlg_listbox_select(ctrl, dlg, 0); /* fallback */
1518         for (i = 0; i < lenof(options); i++)
1519             if (val == options[i].id)
1520                 dlg_listbox_select(ctrl, dlg, i);
1521 #endif
1522         dlg_update_done(ctrl, dlg);
1523     } else if (event == EVENT_SELCHANGE
1524 #ifdef NAMED_CLIPBOARDS
1525                || event == EVENT_VALCHANGE
1526 #endif
1527         ) {
1528 #ifdef NAMED_CLIPBOARDS
1529         char *sval = dlg_editbox_get(ctrl, dlg);
1530         int i;
1531 
1532         for (i = 0; i < lenof(options); i++)
1533             if (!strcmp(sval, options[i].name)) {
1534                 conf_set_int(conf, setting, options[i].id);
1535                 conf_set_str(conf, strsetting, "");
1536                 break;
1537             }
1538         if (i == lenof(options)) {
1539             conf_set_int(conf, setting, CLIPUI_CUSTOM);
1540             if (sval[0] == '=')
1541                 sval++;
1542             conf_set_str(conf, strsetting, sval);
1543         }
1544 
1545         sfree(sval);
1546 #else
1547         int index = dlg_listbox_index(ctrl, dlg);
1548         if (index >= 0) {
1549             int val = dlg_listbox_getid(ctrl, dlg, index);
1550             conf_set_int(conf, setting, val);
1551         }
1552 #endif
1553     }
1554 }
1555 
clipboard_control(struct controlset * s,const char * label,char shortcut,int percentage,intorptr helpctx,int setting,int strsetting)1556 static void clipboard_control(struct controlset *s, const char *label,
1557                               char shortcut, int percentage, intorptr helpctx,
1558                               int setting, int strsetting)
1559 {
1560 #ifdef NAMED_CLIPBOARDS
1561     ctrl_combobox(s, label, shortcut, percentage, helpctx,
1562                   clipboard_selector_handler, I(setting), I(strsetting));
1563 #else
1564     /* strsetting isn't needed in this case */
1565     ctrl_droplist(s, label, shortcut, percentage, helpctx,
1566                   clipboard_selector_handler, I(setting));
1567 #endif
1568 }
1569 
serial_parity_handler(union control * ctrl,dlgparam * dlg,void * data,int event)1570 static void serial_parity_handler(union control *ctrl, dlgparam *dlg,
1571                                   void *data, int event)
1572 {
1573     static const struct {
1574         const char *name;
1575         int val;
1576     } parities[] = {
1577         {"None", SER_PAR_NONE},
1578         {"Odd", SER_PAR_ODD},
1579         {"Even", SER_PAR_EVEN},
1580         {"Mark", SER_PAR_MARK},
1581         {"Space", SER_PAR_SPACE},
1582     };
1583     int mask = ctrl->listbox.context.i;
1584     int i, j;
1585     Conf *conf = (Conf *)data;
1586 
1587     if (event == EVENT_REFRESH) {
1588         /* Fetching this once at the start of the function ensures we
1589          * remember what the right value is supposed to be when
1590          * operations below cause reentrant calls to this function. */
1591         int oldparity = conf_get_int(conf, CONF_serparity);
1592 
1593         dlg_update_start(ctrl, dlg);
1594         dlg_listbox_clear(ctrl, dlg);
1595         for (i = 0; i < lenof(parities); i++)  {
1596             if (mask & (1 << parities[i].val))
1597                 dlg_listbox_addwithid(ctrl, dlg, parities[i].name,
1598                                       parities[i].val);
1599         }
1600         for (i = j = 0; i < lenof(parities); i++) {
1601             if (mask & (1 << parities[i].val)) {
1602                 if (oldparity == parities[i].val) {
1603                     dlg_listbox_select(ctrl, dlg, j);
1604                     break;
1605                 }
1606                 j++;
1607             }
1608         }
1609         if (i == lenof(parities)) {    /* an unsupported setting was chosen */
1610             dlg_listbox_select(ctrl, dlg, 0);
1611             oldparity = SER_PAR_NONE;
1612         }
1613         dlg_update_done(ctrl, dlg);
1614         conf_set_int(conf, CONF_serparity, oldparity);    /* restore */
1615     } else if (event == EVENT_SELCHANGE) {
1616         int i = dlg_listbox_index(ctrl, dlg);
1617         if (i < 0)
1618             i = SER_PAR_NONE;
1619         else
1620             i = dlg_listbox_getid(ctrl, dlg, i);
1621         conf_set_int(conf, CONF_serparity, i);
1622     }
1623 }
1624 
serial_flow_handler(union control * ctrl,dlgparam * dlg,void * data,int event)1625 static void serial_flow_handler(union control *ctrl, dlgparam *dlg,
1626                                 void *data, int event)
1627 {
1628     static const struct {
1629         const char *name;
1630         int val;
1631     } flows[] = {
1632         {"None", SER_FLOW_NONE},
1633         {"XON/XOFF", SER_FLOW_XONXOFF},
1634         {"RTS/CTS", SER_FLOW_RTSCTS},
1635         {"DSR/DTR", SER_FLOW_DSRDTR},
1636     };
1637     int mask = ctrl->listbox.context.i;
1638     int i, j;
1639     Conf *conf = (Conf *)data;
1640 
1641     if (event == EVENT_REFRESH) {
1642         /* Fetching this once at the start of the function ensures we
1643          * remember what the right value is supposed to be when
1644          * operations below cause reentrant calls to this function. */
1645         int oldflow = conf_get_int(conf, CONF_serflow);
1646 
1647         dlg_update_start(ctrl, dlg);
1648         dlg_listbox_clear(ctrl, dlg);
1649         for (i = 0; i < lenof(flows); i++)  {
1650             if (mask & (1 << flows[i].val))
1651                 dlg_listbox_addwithid(ctrl, dlg, flows[i].name, flows[i].val);
1652         }
1653         for (i = j = 0; i < lenof(flows); i++) {
1654             if (mask & (1 << flows[i].val)) {
1655                 if (oldflow == flows[i].val) {
1656                     dlg_listbox_select(ctrl, dlg, j);
1657                     break;
1658                 }
1659                 j++;
1660             }
1661         }
1662         if (i == lenof(flows)) {       /* an unsupported setting was chosen */
1663             dlg_listbox_select(ctrl, dlg, 0);
1664             oldflow = SER_FLOW_NONE;
1665         }
1666         dlg_update_done(ctrl, dlg);
1667         conf_set_int(conf, CONF_serflow, oldflow);/* restore */
1668     } else if (event == EVENT_SELCHANGE) {
1669         int i = dlg_listbox_index(ctrl, dlg);
1670         if (i < 0)
1671             i = SER_FLOW_NONE;
1672         else
1673             i = dlg_listbox_getid(ctrl, dlg, i);
1674         conf_set_int(conf, CONF_serflow, i);
1675     }
1676 }
1677 
setup_config_box(struct controlbox * b,bool midsession,int protocol,int protcfginfo)1678 void setup_config_box(struct controlbox *b, bool midsession,
1679                       int protocol, int protcfginfo)
1680 {
1681     const struct BackendVtable *backvt;
1682     struct controlset *s;
1683     struct sessionsaver_data *ssd;
1684     struct charclass_data *ccd;
1685     struct colour_data *cd;
1686     struct ttymodes_data *td;
1687     struct environ_data *ed;
1688     struct portfwd_data *pfd;
1689     struct manual_hostkey_data *mh;
1690     union control *c;
1691     bool resize_forbidden = false;
1692     char *str;
1693 
1694     ssd = (struct sessionsaver_data *)
1695         ctrl_alloc_with_free(b, sizeof(struct sessionsaver_data),
1696                              sessionsaver_data_free);
1697     memset(ssd, 0, sizeof(*ssd));
1698     ssd->savedsession = dupstr("");
1699     ssd->midsession = midsession;
1700 
1701     /*
1702      * The standard panel that appears at the bottom of all panels:
1703      * Open, Cancel, Apply etc.
1704      */
1705     s = ctrl_getset(b, "", "", "");
1706     ctrl_columns(s, 5, 20, 20, 20, 20, 20);
1707     ssd->okbutton = ctrl_pushbutton(s,
1708                                     (midsession ? "Apply" : "Open"),
1709                                     (char)(midsession ? 'a' : 'o'),
1710                                     HELPCTX(no_help),
1711                                     sessionsaver_handler, P(ssd));
1712     ssd->okbutton->button.isdefault = true;
1713     ssd->okbutton->generic.column = 3;
1714     ssd->cancelbutton = ctrl_pushbutton(s, "Cancel", 'c', HELPCTX(no_help),
1715                                         sessionsaver_handler, P(ssd));
1716     ssd->cancelbutton->button.iscancel = true;
1717     ssd->cancelbutton->generic.column = 4;
1718     /* We carefully don't close the 5-column part, so that platform-
1719      * specific add-ons can put extra buttons alongside Open and Cancel. */
1720 
1721     /*
1722      * The Session panel.
1723      */
1724     str = dupprintf("Basic options for your %s session", appname);
1725     ctrl_settitle(b, "Session", str);
1726     sfree(str);
1727 
1728     if (!midsession) {
1729         struct hostport *hp = (struct hostport *)
1730             ctrl_alloc(b, sizeof(struct hostport));
1731         memset(hp, 0, sizeof(*hp));
1732 
1733         s = ctrl_getset(b, "Session", "hostport",
1734                         "Specify the destination you want to connect to");
1735         ctrl_columns(s, 2, 75, 25);
1736         c = ctrl_editbox(s, HOST_BOX_TITLE, 'n', 100,
1737                          HELPCTX(session_hostname),
1738                          config_host_handler, I(0), I(0));
1739         c->generic.column = 0;
1740         hp->host = c;
1741         c = ctrl_editbox(s, PORT_BOX_TITLE, 'p', 100,
1742                          HELPCTX(session_hostname),
1743                          config_port_handler, I(0), I(0));
1744         c->generic.column = 1;
1745         hp->port = c;
1746 
1747         ctrl_columns(s, 1, 100);
1748         c = ctrl_text(s, "Connection type:", HELPCTX(session_hostname));
1749         ctrl_columns(s, 2, 62, 38);
1750         c = ctrl_radiobuttons(s, NULL, NO_SHORTCUT, 3,
1751                               HELPCTX(session_hostname),
1752                               config_protocols_handler, P(hp), NULL);
1753         c->generic.column = 0;
1754         hp->protradio = c;
1755         c->radio.buttons = sresize(c->radio.buttons, PROTOCOL_LIMIT, char *);
1756         c->radio.shortcuts = sresize(c->radio.shortcuts, PROTOCOL_LIMIT, char);
1757         c->radio.buttondata = sresize(c->radio.buttondata, PROTOCOL_LIMIT,
1758                                       intorptr);
1759         assert(c->radio.nbuttons == 0);
1760         /* UI design assumes there exists at least one 'real' radio button */
1761         assert(n_ui_backends > 0 && n_ui_backends < PROTOCOL_LIMIT);
1762         for (size_t i = 0; i < n_ui_backends; i++) {
1763             assert(backends[i]);
1764             c->radio.buttons[c->radio.nbuttons] =
1765                 dupstr(backends[i]->displayname);
1766             c->radio.shortcuts[c->radio.nbuttons] =
1767                 (backends[i]->protocol == PROT_SSH ? 's' :
1768                  backends[i]->protocol == PROT_SERIAL ? 'r' :
1769                  backends[i]->protocol == PROT_RAW ? 'w' :  /* FIXME unused */
1770                  NO_SHORTCUT);
1771             c->radio.buttondata[c->radio.nbuttons] =
1772                 I(backends[i]->protocol);
1773             c->radio.nbuttons++;
1774         }
1775         /* UI design assumes there exists at least one droplist entry */
1776         assert(backends[c->radio.nbuttons]);
1777 
1778         c->radio.buttons[c->radio.nbuttons] = dupstr("Other:");
1779         c->radio.shortcuts[c->radio.nbuttons] = 't';
1780         c->radio.buttondata[c->radio.nbuttons] = I(-1);
1781         c->radio.nbuttons++;
1782 
1783         c = ctrl_droplist(s, NULL, NO_SHORTCUT, 100,
1784                           HELPCTX(session_hostname),
1785                           config_protocols_handler, P(hp));
1786         hp->protlist = c;
1787         /* droplist is populated in config_protocols_handler */
1788         c->generic.column = 1;
1789 
1790         /* Vertically centre the two protocol controls w.r.t. each other */
1791         hp->protlist->generic.align_next_to = hp->protradio;
1792 
1793         ctrl_columns(s, 1, 100);
1794     }
1795 
1796     /*
1797      * The Load/Save panel is available even in mid-session.
1798      */
1799     s = ctrl_getset(b, "Session", "savedsessions",
1800                     midsession ? "Save the current session settings" :
1801                     "Load, save or delete a stored session");
1802     ctrl_columns(s, 2, 75, 25);
1803     get_sesslist(&ssd->sesslist, true);
1804     ssd->editbox = ctrl_editbox(s, "Saved Sessions", 'e', 100,
1805                                 HELPCTX(session_saved),
1806                                 sessionsaver_handler, P(ssd), P(NULL));
1807     ssd->editbox->generic.column = 0;
1808     /* Reset columns so that the buttons are alongside the list, rather
1809      * than alongside that edit box. */
1810     ctrl_columns(s, 1, 100);
1811     ctrl_columns(s, 2, 75, 25);
1812     ssd->listbox = ctrl_listbox(s, NULL, NO_SHORTCUT,
1813                                 HELPCTX(session_saved),
1814                                 sessionsaver_handler, P(ssd));
1815     ssd->listbox->generic.column = 0;
1816     ssd->listbox->listbox.height = 7;
1817     if (!midsession) {
1818         ssd->loadbutton = ctrl_pushbutton(s, "Load", 'l',
1819                                           HELPCTX(session_saved),
1820                                           sessionsaver_handler, P(ssd));
1821         ssd->loadbutton->generic.column = 1;
1822     } else {
1823         /* We can't offer the Load button mid-session, as it would allow the
1824          * user to load and subsequently save settings they can't see. (And
1825          * also change otherwise immutable settings underfoot; that probably
1826          * shouldn't be a problem, but.) */
1827         ssd->loadbutton = NULL;
1828     }
1829     /* "Save" button is permitted mid-session. */
1830     ssd->savebutton = ctrl_pushbutton(s, "Save", 'v',
1831                                       HELPCTX(session_saved),
1832                                       sessionsaver_handler, P(ssd));
1833     ssd->savebutton->generic.column = 1;
1834     if (!midsession) {
1835         ssd->delbutton = ctrl_pushbutton(s, "Delete", 'd',
1836                                          HELPCTX(session_saved),
1837                                          sessionsaver_handler, P(ssd));
1838         ssd->delbutton->generic.column = 1;
1839     } else {
1840         /* Disable the Delete button mid-session too, for UI consistency. */
1841         ssd->delbutton = NULL;
1842     }
1843     ctrl_columns(s, 1, 100);
1844 
1845     s = ctrl_getset(b, "Session", "otheropts", NULL);
1846     ctrl_radiobuttons(s, "Close window on exit:", 'x', 4,
1847                       HELPCTX(session_coe),
1848                       conf_radiobutton_handler,
1849                       I(CONF_close_on_exit),
1850                       "Always", I(FORCE_ON),
1851                       "Never", I(FORCE_OFF),
1852                       "Only on clean exit", I(AUTO), NULL);
1853 
1854     /*
1855      * The Session/Logging panel.
1856      */
1857     ctrl_settitle(b, "Session/Logging", "Options controlling session logging");
1858 
1859     s = ctrl_getset(b, "Session/Logging", "main", NULL);
1860     /*
1861      * The logging buttons change depending on whether SSH packet
1862      * logging can sensibly be available.
1863      */
1864     {
1865         const char *sshlogname, *sshrawlogname;
1866         if ((midsession && protocol == PROT_SSH) ||
1867             (!midsession && backend_vt_from_proto(PROT_SSH))) {
1868             sshlogname = "SSH packets";
1869             sshrawlogname = "SSH packets and raw data";
1870         } else {
1871             sshlogname = NULL;         /* this will disable both buttons */
1872             sshrawlogname = NULL;      /* this will just placate optimisers */
1873         }
1874         ctrl_radiobuttons(s, "Session logging:", NO_SHORTCUT, 2,
1875                           HELPCTX(logging_main),
1876                           loggingbuttons_handler,
1877                           I(CONF_logtype),
1878                           "None", 't', I(LGTYP_NONE),
1879                           "Printable output", 'p', I(LGTYP_ASCII),
1880                           "All session output", 'l', I(LGTYP_DEBUG),
1881                           sshlogname, 's', I(LGTYP_PACKETS),
1882                           sshrawlogname, 'r', I(LGTYP_SSHRAW),
1883                           NULL);
1884     }
1885     ctrl_filesel(s, "Log file name:", 'f',
1886                  NULL, true, "Select session log file name",
1887                  HELPCTX(logging_filename),
1888                  conf_filesel_handler, I(CONF_logfilename));
1889     ctrl_text(s, "(Log file name can contain &Y, &M, &D for date,"
1890               " &T for time, &H for host name, and &P for port number)",
1891               HELPCTX(logging_filename));
1892     ctrl_radiobuttons(s, "What to do if the log file already exists:", 'e', 1,
1893                       HELPCTX(logging_exists),
1894                       conf_radiobutton_handler, I(CONF_logxfovr),
1895                       "Always overwrite it", I(LGXF_OVR),
1896                       "Always append to the end of it", I(LGXF_APN),
1897                       "Ask the user every time", I(LGXF_ASK), NULL);
1898     ctrl_checkbox(s, "Flush log file frequently", 'u',
1899                  HELPCTX(logging_flush),
1900                  conf_checkbox_handler, I(CONF_logflush));
1901     ctrl_checkbox(s, "Include header", 'i',
1902                  HELPCTX(logging_header),
1903                  conf_checkbox_handler, I(CONF_logheader));
1904 
1905     if ((midsession && protocol == PROT_SSH) ||
1906         (!midsession && backend_vt_from_proto(PROT_SSH))) {
1907         s = ctrl_getset(b, "Session/Logging", "ssh",
1908                         "Options specific to SSH packet logging");
1909         ctrl_checkbox(s, "Omit known password fields", 'k',
1910                       HELPCTX(logging_ssh_omit_password),
1911                       conf_checkbox_handler, I(CONF_logomitpass));
1912         ctrl_checkbox(s, "Omit session data", 'd',
1913                       HELPCTX(logging_ssh_omit_data),
1914                       conf_checkbox_handler, I(CONF_logomitdata));
1915     }
1916 
1917     /*
1918      * The Terminal panel.
1919      */
1920     ctrl_settitle(b, "Terminal", "Options controlling the terminal emulation");
1921 
1922     s = ctrl_getset(b, "Terminal", "general", "Set various terminal options");
1923     ctrl_checkbox(s, "Auto wrap mode initially on", 'w',
1924                   HELPCTX(terminal_autowrap),
1925                   conf_checkbox_handler, I(CONF_wrap_mode));
1926     ctrl_checkbox(s, "DEC Origin Mode initially on", 'd',
1927                   HELPCTX(terminal_decom),
1928                   conf_checkbox_handler, I(CONF_dec_om));
1929     ctrl_checkbox(s, "Implicit CR in every LF", 'r',
1930                   HELPCTX(terminal_lfhascr),
1931                   conf_checkbox_handler, I(CONF_lfhascr));
1932     ctrl_checkbox(s, "Implicit LF in every CR", 'f',
1933                   HELPCTX(terminal_crhaslf),
1934                   conf_checkbox_handler, I(CONF_crhaslf));
1935     ctrl_checkbox(s, "Use background colour to erase screen", 'e',
1936                   HELPCTX(terminal_bce),
1937                   conf_checkbox_handler, I(CONF_bce));
1938     ctrl_checkbox(s, "Enable blinking text", 'n',
1939                   HELPCTX(terminal_blink),
1940                   conf_checkbox_handler, I(CONF_blinktext));
1941     ctrl_editbox(s, "Answerback to ^E:", 's', 100,
1942                  HELPCTX(terminal_answerback),
1943                  conf_editbox_handler, I(CONF_answerback), I(1));
1944 
1945     s = ctrl_getset(b, "Terminal", "ldisc", "Line discipline options");
1946     ctrl_radiobuttons(s, "Local echo:", 'l', 3,
1947                       HELPCTX(terminal_localecho),
1948                       conf_radiobutton_handler,I(CONF_localecho),
1949                       "Auto", I(AUTO),
1950                       "Force on", I(FORCE_ON),
1951                       "Force off", I(FORCE_OFF), NULL);
1952     ctrl_radiobuttons(s, "Local line editing:", 't', 3,
1953                       HELPCTX(terminal_localedit),
1954                       conf_radiobutton_handler,I(CONF_localedit),
1955                       "Auto", I(AUTO),
1956                       "Force on", I(FORCE_ON),
1957                       "Force off", I(FORCE_OFF), NULL);
1958 
1959     s = ctrl_getset(b, "Terminal", "printing", "Remote-controlled printing");
1960     ctrl_combobox(s, "Printer to send ANSI printer output to:", 'p', 100,
1961                   HELPCTX(terminal_printing),
1962                   printerbox_handler, P(NULL), P(NULL));
1963 
1964     /*
1965      * The Terminal/Keyboard panel.
1966      */
1967     ctrl_settitle(b, "Terminal/Keyboard",
1968                   "Options controlling the effects of keys");
1969 
1970     s = ctrl_getset(b, "Terminal/Keyboard", "mappings",
1971                     "Change the sequences sent by:");
1972     ctrl_radiobuttons(s, "The Backspace key", 'b', 2,
1973                       HELPCTX(keyboard_backspace),
1974                       conf_radiobutton_bool_handler,
1975                       I(CONF_bksp_is_delete),
1976                       "Control-H", I(0), "Control-? (127)", I(1), NULL);
1977     ctrl_radiobuttons(s, "The Home and End keys", 'e', 2,
1978                       HELPCTX(keyboard_homeend),
1979                       conf_radiobutton_bool_handler,
1980                       I(CONF_rxvt_homeend),
1981                       "Standard", I(false), "rxvt", I(true), NULL);
1982     ctrl_radiobuttons(s, "The Function keys and keypad", 'f', 3,
1983                       HELPCTX(keyboard_funkeys),
1984                       conf_radiobutton_handler,
1985                       I(CONF_funky_type),
1986                       "ESC[n~", I(0), "Linux", I(1), "Xterm R6", I(2),
1987                       "VT400", I(3), "VT100+", I(4), "SCO", I(5), NULL);
1988 
1989     s = ctrl_getset(b, "Terminal/Keyboard", "appkeypad",
1990                     "Application keypad settings:");
1991     ctrl_radiobuttons(s, "Initial state of cursor keys:", 'r', 3,
1992                       HELPCTX(keyboard_appcursor),
1993                       conf_radiobutton_bool_handler,
1994                       I(CONF_app_cursor),
1995                       "Normal", I(0), "Application", I(1), NULL);
1996     ctrl_radiobuttons(s, "Initial state of numeric keypad:", 'n', 3,
1997                       HELPCTX(keyboard_appkeypad),
1998                       numeric_keypad_handler, P(NULL),
1999                       "Normal", I(0), "Application", I(1), "NetHack", I(2),
2000                       NULL);
2001 
2002     /*
2003      * The Terminal/Bell panel.
2004      */
2005     ctrl_settitle(b, "Terminal/Bell",
2006                   "Options controlling the terminal bell");
2007 
2008     s = ctrl_getset(b, "Terminal/Bell", "style", "Set the style of bell");
2009     ctrl_radiobuttons(s, "Action to happen when a bell occurs:", 'b', 1,
2010                       HELPCTX(bell_style),
2011                       conf_radiobutton_handler, I(CONF_beep),
2012                       "None (bell disabled)", I(BELL_DISABLED),
2013                       "Make default system alert sound", I(BELL_DEFAULT),
2014                       "Visual bell (flash window)", I(BELL_VISUAL), NULL);
2015 
2016     s = ctrl_getset(b, "Terminal/Bell", "overload",
2017                     "Control the bell overload behaviour");
2018     ctrl_checkbox(s, "Bell is temporarily disabled when over-used", 'd',
2019                   HELPCTX(bell_overload),
2020                   conf_checkbox_handler, I(CONF_bellovl));
2021     ctrl_editbox(s, "Over-use means this many bells...", 'm', 20,
2022                  HELPCTX(bell_overload),
2023                  conf_editbox_handler, I(CONF_bellovl_n), I(-1));
2024     ctrl_editbox(s, "... in this many seconds", 't', 20,
2025                  HELPCTX(bell_overload),
2026                  conf_editbox_handler, I(CONF_bellovl_t),
2027                  I(-TICKSPERSEC));
2028     ctrl_text(s, "The bell is re-enabled after a few seconds of silence.",
2029               HELPCTX(bell_overload));
2030     ctrl_editbox(s, "Seconds of silence required", 's', 20,
2031                  HELPCTX(bell_overload),
2032                  conf_editbox_handler, I(CONF_bellovl_s),
2033                  I(-TICKSPERSEC));
2034 
2035     /*
2036      * The Terminal/Features panel.
2037      */
2038     ctrl_settitle(b, "Terminal/Features",
2039                   "Enabling and disabling advanced terminal features");
2040 
2041     s = ctrl_getset(b, "Terminal/Features", "main", NULL);
2042     ctrl_checkbox(s, "Disable application cursor keys mode", 'u',
2043                   HELPCTX(features_application),
2044                   conf_checkbox_handler, I(CONF_no_applic_c));
2045     ctrl_checkbox(s, "Disable application keypad mode", 'k',
2046                   HELPCTX(features_application),
2047                   conf_checkbox_handler, I(CONF_no_applic_k));
2048     ctrl_checkbox(s, "Disable xterm-style mouse reporting", 'x',
2049                   HELPCTX(features_mouse),
2050                   conf_checkbox_handler, I(CONF_no_mouse_rep));
2051     ctrl_checkbox(s, "Disable remote-controlled terminal resizing", 's',
2052                   HELPCTX(features_resize),
2053                   conf_checkbox_handler,
2054                   I(CONF_no_remote_resize));
2055     ctrl_checkbox(s, "Disable switching to alternate terminal screen", 'w',
2056                   HELPCTX(features_altscreen),
2057                   conf_checkbox_handler, I(CONF_no_alt_screen));
2058     ctrl_checkbox(s, "Disable remote-controlled window title changing", 't',
2059                   HELPCTX(features_retitle),
2060                   conf_checkbox_handler,
2061                   I(CONF_no_remote_wintitle));
2062     ctrl_radiobuttons(s, "Response to remote title query (SECURITY):", 'q', 3,
2063                       HELPCTX(features_qtitle),
2064                       conf_radiobutton_handler,
2065                       I(CONF_remote_qtitle_action),
2066                       "None", I(TITLE_NONE),
2067                       "Empty string", I(TITLE_EMPTY),
2068                       "Window title", I(TITLE_REAL), NULL);
2069     ctrl_checkbox(s, "Disable remote-controlled clearing of scrollback", 'e',
2070                   HELPCTX(features_clearscroll),
2071                   conf_checkbox_handler,
2072                   I(CONF_no_remote_clearscroll));
2073     ctrl_checkbox(s, "Disable destructive backspace on server sending ^?",'b',
2074                   HELPCTX(features_dbackspace),
2075                   conf_checkbox_handler, I(CONF_no_dbackspace));
2076     ctrl_checkbox(s, "Disable remote-controlled character set configuration",
2077                   'r', HELPCTX(features_charset), conf_checkbox_handler,
2078                   I(CONF_no_remote_charset));
2079     ctrl_checkbox(s, "Disable Arabic text shaping",
2080                   'l', HELPCTX(features_arabicshaping), conf_checkbox_handler,
2081                   I(CONF_no_arabicshaping));
2082     ctrl_checkbox(s, "Disable bidirectional text display",
2083                   'd', HELPCTX(features_bidi), conf_checkbox_handler,
2084                   I(CONF_no_bidi));
2085 
2086     /*
2087      * The Window panel.
2088      */
2089     str = dupprintf("Options controlling %s's window", appname);
2090     ctrl_settitle(b, "Window", str);
2091     sfree(str);
2092 
2093     backvt = backend_vt_from_proto(protocol);
2094     if (backvt)
2095         resize_forbidden = (backvt->flags & BACKEND_RESIZE_FORBIDDEN);
2096 
2097     if (!resize_forbidden || !midsession) {
2098         s = ctrl_getset(b, "Window", "size", "Set the size of the window");
2099         ctrl_columns(s, 2, 50, 50);
2100         c = ctrl_editbox(s, "Columns", 'm', 100,
2101                          HELPCTX(window_size),
2102                          conf_editbox_handler, I(CONF_width), I(-1));
2103         c->generic.column = 0;
2104         c = ctrl_editbox(s, "Rows", 'r', 100,
2105                          HELPCTX(window_size),
2106                          conf_editbox_handler, I(CONF_height),I(-1));
2107         c->generic.column = 1;
2108         ctrl_columns(s, 1, 100);
2109     }
2110 
2111     s = ctrl_getset(b, "Window", "scrollback",
2112                     "Control the scrollback in the window");
2113     ctrl_editbox(s, "Lines of scrollback", 's', 50,
2114                  HELPCTX(window_scrollback),
2115                  conf_editbox_handler, I(CONF_savelines), I(-1));
2116     ctrl_checkbox(s, "Display scrollbar", 'd',
2117                   HELPCTX(window_scrollback),
2118                   conf_checkbox_handler, I(CONF_scrollbar));
2119     ctrl_checkbox(s, "Reset scrollback on keypress", 'k',
2120                   HELPCTX(window_scrollback),
2121                   conf_checkbox_handler, I(CONF_scroll_on_key));
2122     ctrl_checkbox(s, "Reset scrollback on display activity", 'p',
2123                   HELPCTX(window_scrollback),
2124                   conf_checkbox_handler, I(CONF_scroll_on_disp));
2125     ctrl_checkbox(s, "Push erased text into scrollback", 'e',
2126                   HELPCTX(window_erased),
2127                   conf_checkbox_handler,
2128                   I(CONF_erase_to_scrollback));
2129 
2130     /*
2131      * The Window/Appearance panel.
2132      */
2133     str = dupprintf("Configure the appearance of %s's window", appname);
2134     ctrl_settitle(b, "Window/Appearance", str);
2135     sfree(str);
2136 
2137     s = ctrl_getset(b, "Window/Appearance", "cursor",
2138                     "Adjust the use of the cursor");
2139     ctrl_radiobuttons(s, "Cursor appearance:", NO_SHORTCUT, 3,
2140                       HELPCTX(appearance_cursor),
2141                       conf_radiobutton_handler,
2142                       I(CONF_cursor_type),
2143                       "Block", 'l', I(0),
2144                       "Underline", 'u', I(1),
2145                       "Vertical line", 'v', I(2), NULL);
2146     ctrl_checkbox(s, "Cursor blinks", 'b',
2147                   HELPCTX(appearance_cursor),
2148                   conf_checkbox_handler, I(CONF_blink_cur));
2149 
2150     s = ctrl_getset(b, "Window/Appearance", "font",
2151                     "Font settings");
2152     ctrl_fontsel(s, "Font used in the terminal window", 'n',
2153                  HELPCTX(appearance_font),
2154                  conf_fontsel_handler, I(CONF_font));
2155 
2156     s = ctrl_getset(b, "Window/Appearance", "mouse",
2157                     "Adjust the use of the mouse pointer");
2158     ctrl_checkbox(s, "Hide mouse pointer when typing in window", 'p',
2159                   HELPCTX(appearance_hidemouse),
2160                   conf_checkbox_handler, I(CONF_hide_mouseptr));
2161 
2162     s = ctrl_getset(b, "Window/Appearance", "border",
2163                     "Adjust the window border");
2164     ctrl_editbox(s, "Gap between text and window edge:", 'e', 20,
2165                  HELPCTX(appearance_border),
2166                  conf_editbox_handler,
2167                  I(CONF_window_border), I(-1));
2168 
2169     /*
2170      * The Window/Behaviour panel.
2171      */
2172     str = dupprintf("Configure the behaviour of %s's window", appname);
2173     ctrl_settitle(b, "Window/Behaviour", str);
2174     sfree(str);
2175 
2176     s = ctrl_getset(b, "Window/Behaviour", "title",
2177                     "Adjust the behaviour of the window title");
2178     ctrl_editbox(s, "Window title:", 't', 100,
2179                  HELPCTX(appearance_title),
2180                  conf_editbox_handler, I(CONF_wintitle), I(1));
2181     ctrl_checkbox(s, "Separate window and icon titles", 'i',
2182                   HELPCTX(appearance_title),
2183                   conf_checkbox_handler,
2184                   I(CHECKBOX_INVERT | CONF_win_name_always));
2185 
2186     s = ctrl_getset(b, "Window/Behaviour", "main", NULL);
2187     ctrl_checkbox(s, "Warn before closing window", 'w',
2188                   HELPCTX(behaviour_closewarn),
2189                   conf_checkbox_handler, I(CONF_warn_on_close));
2190 
2191     /*
2192      * The Window/Translation panel.
2193      */
2194     ctrl_settitle(b, "Window/Translation",
2195                   "Options controlling character set translation");
2196 
2197     s = ctrl_getset(b, "Window/Translation", "trans",
2198                     "Character set translation");
2199     ctrl_combobox(s, "Remote character set:",
2200                   'r', 100, HELPCTX(translation_codepage),
2201                   codepage_handler, P(NULL), P(NULL));
2202 
2203     s = ctrl_getset(b, "Window/Translation", "tweaks", NULL);
2204     ctrl_checkbox(s, "Treat CJK ambiguous characters as wide", 'w',
2205                   HELPCTX(translation_cjk_ambig_wide),
2206                   conf_checkbox_handler, I(CONF_cjk_ambig_wide));
2207 
2208     str = dupprintf("Adjust how %s handles line drawing characters", appname);
2209     s = ctrl_getset(b, "Window/Translation", "linedraw", str);
2210     sfree(str);
2211     ctrl_radiobuttons(s, "Handling of line drawing characters:", NO_SHORTCUT,1,
2212                       HELPCTX(translation_linedraw),
2213                       conf_radiobutton_handler,
2214                       I(CONF_vtmode),
2215                       "Use Unicode line drawing code points",'u',I(VT_UNICODE),
2216                       "Poor man's line drawing (+, - and |)",'p',I(VT_POORMAN),
2217                       NULL);
2218     ctrl_checkbox(s, "Copy and paste line drawing characters as lqqqk",'d',
2219                   HELPCTX(selection_linedraw),
2220                   conf_checkbox_handler, I(CONF_rawcnp));
2221     ctrl_checkbox(s, "Enable VT100 line drawing even in UTF-8 mode",'8',
2222                   HELPCTX(translation_utf8linedraw),
2223                   conf_checkbox_handler, I(CONF_utf8linedraw));
2224 
2225     /*
2226      * The Window/Selection panel.
2227      */
2228     ctrl_settitle(b, "Window/Selection", "Options controlling copy and paste");
2229 
2230     s = ctrl_getset(b, "Window/Selection", "mouse",
2231                     "Control use of mouse");
2232     ctrl_checkbox(s, "Shift overrides application's use of mouse", 'p',
2233                   HELPCTX(selection_shiftdrag),
2234                   conf_checkbox_handler, I(CONF_mouse_override));
2235     ctrl_radiobuttons(s,
2236                       "Default selection mode (Alt+drag does the other one):",
2237                       NO_SHORTCUT, 2,
2238                       HELPCTX(selection_rect),
2239                       conf_radiobutton_bool_handler,
2240                       I(CONF_rect_select),
2241                       "Normal", 'n', I(false),
2242                       "Rectangular block", 'r', I(true), NULL);
2243 
2244     s = ctrl_getset(b, "Window/Selection", "clipboards",
2245                     "Assign copy/paste actions to clipboards");
2246     ctrl_checkbox(s, "Auto-copy selected text to "
2247                   CLIPNAME_EXPLICIT_OBJECT,
2248                   NO_SHORTCUT, HELPCTX(selection_autocopy),
2249                   conf_checkbox_handler, I(CONF_mouseautocopy));
2250     clipboard_control(s, "Mouse paste action:", NO_SHORTCUT, 60,
2251                       HELPCTX(selection_clipactions),
2252                       CONF_mousepaste, CONF_mousepaste_custom);
2253     clipboard_control(s, "{Ctrl,Shift} + Ins:", NO_SHORTCUT, 60,
2254                       HELPCTX(selection_clipactions),
2255                       CONF_ctrlshiftins, CONF_ctrlshiftins_custom);
2256     clipboard_control(s, "Ctrl + Shift + {C,V}:", NO_SHORTCUT, 60,
2257                       HELPCTX(selection_clipactions),
2258                       CONF_ctrlshiftcv, CONF_ctrlshiftcv_custom);
2259 
2260     s = ctrl_getset(b, "Window/Selection", "paste",
2261                     "Control pasting of text from clipboard to terminal");
2262     ctrl_checkbox(s, "Permit control characters in pasted text",
2263                   NO_SHORTCUT, HELPCTX(selection_pastectrl),
2264                   conf_checkbox_handler, I(CONF_paste_controls));
2265 
2266     /*
2267      * The Window/Selection/Copy panel.
2268      */
2269     ctrl_settitle(b, "Window/Selection/Copy",
2270                   "Options controlling copying from terminal to clipboard");
2271 
2272     s = ctrl_getset(b, "Window/Selection/Copy", "charclass",
2273                     "Classes of character that group together");
2274     ccd = (struct charclass_data *)
2275         ctrl_alloc(b, sizeof(struct charclass_data));
2276     ccd->listbox = ctrl_listbox(s, "Character classes:", 'e',
2277                                 HELPCTX(copy_charclasses),
2278                                 charclass_handler, P(ccd));
2279     ccd->listbox->listbox.multisel = 1;
2280     ccd->listbox->listbox.ncols = 4;
2281     ccd->listbox->listbox.percentages = snewn(4, int);
2282     ccd->listbox->listbox.percentages[0] = 15;
2283     ccd->listbox->listbox.percentages[1] = 25;
2284     ccd->listbox->listbox.percentages[2] = 20;
2285     ccd->listbox->listbox.percentages[3] = 40;
2286     ctrl_columns(s, 2, 67, 33);
2287     ccd->editbox = ctrl_editbox(s, "Set to class", 't', 50,
2288                                 HELPCTX(copy_charclasses),
2289                                 charclass_handler, P(ccd), P(NULL));
2290     ccd->editbox->generic.column = 0;
2291     ccd->button = ctrl_pushbutton(s, "Set", 's',
2292                                   HELPCTX(copy_charclasses),
2293                                   charclass_handler, P(ccd));
2294     ccd->button->generic.column = 1;
2295     ctrl_columns(s, 1, 100);
2296 
2297     /*
2298      * The Window/Colours panel.
2299      */
2300     ctrl_settitle(b, "Window/Colours", "Options controlling use of colours");
2301 
2302     s = ctrl_getset(b, "Window/Colours", "general",
2303                     "General options for colour usage");
2304     ctrl_checkbox(s, "Allow terminal to specify ANSI colours", 'i',
2305                   HELPCTX(colours_ansi),
2306                   conf_checkbox_handler, I(CONF_ansi_colour));
2307     ctrl_checkbox(s, "Allow terminal to use xterm 256-colour mode", '2',
2308                   HELPCTX(colours_xterm256), conf_checkbox_handler,
2309                   I(CONF_xterm_256_colour));
2310     ctrl_checkbox(s, "Allow terminal to use 24-bit colours", '4',
2311                   HELPCTX(colours_truecolour), conf_checkbox_handler,
2312                   I(CONF_true_colour));
2313     ctrl_radiobuttons(s, "Indicate bolded text by changing:", 'b', 3,
2314                       HELPCTX(colours_bold),
2315                       conf_radiobutton_handler, I(CONF_bold_style),
2316                       "The font", I(1),
2317                       "The colour", I(2),
2318                       "Both", I(3),
2319                       NULL);
2320 
2321     str = dupprintf("Adjust the precise colours %s displays", appname);
2322     s = ctrl_getset(b, "Window/Colours", "adjust", str);
2323     sfree(str);
2324     ctrl_text(s, "Select a colour from the list, and then click the"
2325               " Modify button to change its appearance.",
2326               HELPCTX(colours_config));
2327     ctrl_columns(s, 2, 67, 33);
2328     cd = (struct colour_data *)ctrl_alloc(b, sizeof(struct colour_data));
2329     cd->listbox = ctrl_listbox(s, "Select a colour to adjust:", 'u',
2330                                HELPCTX(colours_config), colour_handler, P(cd));
2331     cd->listbox->generic.column = 0;
2332     cd->listbox->listbox.height = 7;
2333     c = ctrl_text(s, "RGB value:", HELPCTX(colours_config));
2334     c->generic.column = 1;
2335     cd->redit = ctrl_editbox(s, "Red", 'r', 50, HELPCTX(colours_config),
2336                              colour_handler, P(cd), P(NULL));
2337     cd->redit->generic.column = 1;
2338     cd->gedit = ctrl_editbox(s, "Green", 'n', 50, HELPCTX(colours_config),
2339                              colour_handler, P(cd), P(NULL));
2340     cd->gedit->generic.column = 1;
2341     cd->bedit = ctrl_editbox(s, "Blue", 'e', 50, HELPCTX(colours_config),
2342                              colour_handler, P(cd), P(NULL));
2343     cd->bedit->generic.column = 1;
2344     cd->button = ctrl_pushbutton(s, "Modify", 'm', HELPCTX(colours_config),
2345                                  colour_handler, P(cd));
2346     cd->button->generic.column = 1;
2347     ctrl_columns(s, 1, 100);
2348 
2349     /*
2350      * The Connection panel. This doesn't show up if we're in a
2351      * non-network utility such as pterm. We tell this by being
2352      * passed a protocol < 0.
2353      */
2354     if (protocol >= 0) {
2355         ctrl_settitle(b, "Connection", "Options controlling the connection");
2356 
2357         s = ctrl_getset(b, "Connection", "keepalive",
2358                         "Sending of null packets to keep session active");
2359         ctrl_editbox(s, "Seconds between keepalives (0 to turn off)", 'k', 20,
2360                      HELPCTX(connection_keepalive),
2361                      conf_editbox_handler, I(CONF_ping_interval),
2362                      I(-1));
2363 
2364         if (!midsession) {
2365             s = ctrl_getset(b, "Connection", "tcp",
2366                             "Low-level TCP connection options");
2367             ctrl_checkbox(s, "Disable Nagle's algorithm (TCP_NODELAY option)",
2368                           'n', HELPCTX(connection_nodelay),
2369                           conf_checkbox_handler,
2370                           I(CONF_tcp_nodelay));
2371             ctrl_checkbox(s, "Enable TCP keepalives (SO_KEEPALIVE option)",
2372                           'p', HELPCTX(connection_tcpkeepalive),
2373                           conf_checkbox_handler,
2374                           I(CONF_tcp_keepalives));
2375 #ifndef NO_IPV6
2376             s = ctrl_getset(b, "Connection", "ipversion",
2377                           "Internet protocol version");
2378             ctrl_radiobuttons(s, NULL, NO_SHORTCUT, 3,
2379                           HELPCTX(connection_ipversion),
2380                           conf_radiobutton_handler,
2381                           I(CONF_addressfamily),
2382                           "Auto", 'u', I(ADDRTYPE_UNSPEC),
2383                           "IPv4", '4', I(ADDRTYPE_IPV4),
2384                           "IPv6", '6', I(ADDRTYPE_IPV6),
2385                           NULL);
2386 #endif
2387 
2388             {
2389                 const char *label = backend_vt_from_proto(PROT_SSH) ?
2390                     "Logical name of remote host (e.g. for SSH key lookup):" :
2391                     "Logical name of remote host:";
2392                 s = ctrl_getset(b, "Connection", "identity",
2393                                 "Logical name of remote host");
2394                 ctrl_editbox(s, label, 'm', 100,
2395                              HELPCTX(connection_loghost),
2396                              conf_editbox_handler, I(CONF_loghost), I(1));
2397             }
2398         }
2399 
2400         /*
2401          * A sub-panel Connection/Data, containing options that
2402          * decide on data to send to the server.
2403          */
2404         if (!midsession) {
2405             ctrl_settitle(b, "Connection/Data", "Data to send to the server");
2406 
2407             s = ctrl_getset(b, "Connection/Data", "login",
2408                             "Login details");
2409             ctrl_editbox(s, "Auto-login username", 'u', 50,
2410                          HELPCTX(connection_username),
2411                          conf_editbox_handler, I(CONF_username), I(1));
2412             {
2413                 /* We assume the local username is sufficiently stable
2414                  * to include on the dialog box. */
2415                 char *user = get_username();
2416                 char *userlabel = dupprintf("Use system username (%s)",
2417                                             user ? user : "");
2418                 sfree(user);
2419                 ctrl_radiobuttons(s, "When username is not specified:", 'n', 4,
2420                                   HELPCTX(connection_username_from_env),
2421                                   conf_radiobutton_bool_handler,
2422                                   I(CONF_username_from_env),
2423                                   "Prompt", I(false),
2424                                   userlabel, I(true),
2425                                   NULL);
2426                 sfree(userlabel);
2427             }
2428 
2429             s = ctrl_getset(b, "Connection/Data", "term",
2430                             "Terminal details");
2431             ctrl_editbox(s, "Terminal-type string", 't', 50,
2432                          HELPCTX(connection_termtype),
2433                          conf_editbox_handler, I(CONF_termtype), I(1));
2434             ctrl_editbox(s, "Terminal speeds", 's', 50,
2435                          HELPCTX(connection_termspeed),
2436                          conf_editbox_handler, I(CONF_termspeed), I(1));
2437 
2438             s = ctrl_getset(b, "Connection/Data", "env",
2439                             "Environment variables");
2440             ctrl_columns(s, 2, 80, 20);
2441             ed = (struct environ_data *)
2442                 ctrl_alloc(b, sizeof(struct environ_data));
2443             ed->varbox = ctrl_editbox(s, "Variable", 'v', 60,
2444                                       HELPCTX(telnet_environ),
2445                                       environ_handler, P(ed), P(NULL));
2446             ed->varbox->generic.column = 0;
2447             ed->valbox = ctrl_editbox(s, "Value", 'l', 60,
2448                                       HELPCTX(telnet_environ),
2449                                       environ_handler, P(ed), P(NULL));
2450             ed->valbox->generic.column = 0;
2451             ed->addbutton = ctrl_pushbutton(s, "Add", 'd',
2452                                             HELPCTX(telnet_environ),
2453                                             environ_handler, P(ed));
2454             ed->addbutton->generic.column = 1;
2455             ed->rembutton = ctrl_pushbutton(s, "Remove", 'r',
2456                                             HELPCTX(telnet_environ),
2457                                             environ_handler, P(ed));
2458             ed->rembutton->generic.column = 1;
2459             ctrl_columns(s, 1, 100);
2460             ed->listbox = ctrl_listbox(s, NULL, NO_SHORTCUT,
2461                                        HELPCTX(telnet_environ),
2462                                        environ_handler, P(ed));
2463             ed->listbox->listbox.height = 3;
2464             ed->listbox->listbox.ncols = 2;
2465             ed->listbox->listbox.percentages = snewn(2, int);
2466             ed->listbox->listbox.percentages[0] = 30;
2467             ed->listbox->listbox.percentages[1] = 70;
2468         }
2469 
2470     }
2471 
2472     if (!midsession) {
2473         /*
2474          * The Connection/Proxy panel.
2475          */
2476         ctrl_settitle(b, "Connection/Proxy",
2477                       "Options controlling proxy usage");
2478 
2479         s = ctrl_getset(b, "Connection/Proxy", "basics", NULL);
2480         ctrl_radiobuttons(s, "Proxy type:", 't', 3,
2481                           HELPCTX(proxy_type),
2482                           conf_radiobutton_handler,
2483                           I(CONF_proxy_type),
2484                           "None", I(PROXY_NONE),
2485                           "SOCKS 4", I(PROXY_SOCKS4),
2486                           "SOCKS 5", I(PROXY_SOCKS5),
2487                           "HTTP", I(PROXY_HTTP),
2488                           "Telnet", I(PROXY_TELNET),
2489                           NULL);
2490         ctrl_columns(s, 2, 80, 20);
2491         c = ctrl_editbox(s, "Proxy hostname", 'y', 100,
2492                          HELPCTX(proxy_main),
2493                          conf_editbox_handler,
2494                          I(CONF_proxy_host), I(1));
2495         c->generic.column = 0;
2496         c = ctrl_editbox(s, "Port", 'p', 100,
2497                          HELPCTX(proxy_main),
2498                          conf_editbox_handler,
2499                          I(CONF_proxy_port),
2500                          I(-1));
2501         c->generic.column = 1;
2502         ctrl_columns(s, 1, 100);
2503         ctrl_editbox(s, "Exclude Hosts/IPs", 'e', 100,
2504                      HELPCTX(proxy_exclude),
2505                      conf_editbox_handler,
2506                      I(CONF_proxy_exclude_list), I(1));
2507         ctrl_checkbox(s, "Consider proxying local host connections", 'x',
2508                       HELPCTX(proxy_exclude),
2509                       conf_checkbox_handler,
2510                       I(CONF_even_proxy_localhost));
2511         ctrl_radiobuttons(s, "Do DNS name lookup at proxy end:", 'd', 3,
2512                           HELPCTX(proxy_dns),
2513                           conf_radiobutton_handler,
2514                           I(CONF_proxy_dns),
2515                           "No", I(FORCE_OFF),
2516                           "Auto", I(AUTO),
2517                           "Yes", I(FORCE_ON), NULL);
2518         ctrl_editbox(s, "Username", 'u', 60,
2519                      HELPCTX(proxy_auth),
2520                      conf_editbox_handler,
2521                      I(CONF_proxy_username), I(1));
2522         c = ctrl_editbox(s, "Password", 'w', 60,
2523                          HELPCTX(proxy_auth),
2524                          conf_editbox_handler,
2525                          I(CONF_proxy_password), I(1));
2526         c->editbox.password = true;
2527         ctrl_editbox(s, "Telnet command", 'm', 100,
2528                      HELPCTX(proxy_command),
2529                      conf_editbox_handler,
2530                      I(CONF_proxy_telnet_command), I(1));
2531 
2532         ctrl_radiobuttons(s, "Print proxy diagnostics "
2533                           "in the terminal window", 'r', 5,
2534                           HELPCTX(proxy_logging),
2535                           conf_radiobutton_handler,
2536                           I(CONF_proxy_log_to_term),
2537                           "No", I(FORCE_OFF),
2538                           "Yes", I(FORCE_ON),
2539                           "Only until session starts", I(AUTO), NULL);
2540     }
2541 
2542     /*
2543      * Each per-protocol configuration GUI panel is conditionally
2544      * displayed. We don't display it if this binary doesn't contain a
2545      * backend for its protocol at all; we don't display it if we're
2546      * already in mid-session with a different protocol selected; and
2547      * even if we _do_ have this protocol selected, we don't display
2548      * the panel if the protocol doesn't permit any mid-session
2549      * reconfiguration anyway.
2550      */
2551 
2552 #define DISPLAY_RECONFIGURABLE_PROTOCOL(which_proto) \
2553     (backend_vt_from_proto(which_proto) && \
2554      (!midsession || protocol == (which_proto)))
2555 #define DISPLAY_NON_RECONFIGURABLE_PROTOCOL(which_proto) \
2556     (backend_vt_from_proto(which_proto) && !midsession)
2557 
2558     if (DISPLAY_RECONFIGURABLE_PROTOCOL(PROT_SSH) ||
2559         DISPLAY_RECONFIGURABLE_PROTOCOL(PROT_SSHCONN)) {
2560         /*
2561          * The Connection/SSH panel.
2562          */
2563         ctrl_settitle(b, "Connection/SSH",
2564                       "Options controlling SSH connections");
2565 
2566         /* SSH-1 or connection-sharing downstream */
2567         if (midsession && (protcfginfo == 1 || protcfginfo == -1)) {
2568             s = ctrl_getset(b, "Connection/SSH", "disclaimer", NULL);
2569             ctrl_text(s, "Nothing on this panel may be reconfigured in mid-"
2570                       "session; it is only here so that sub-panels of it can "
2571                       "exist without looking strange.", HELPCTX(no_help));
2572         }
2573 
2574         if (!midsession) {
2575 
2576             s = ctrl_getset(b, "Connection/SSH", "data",
2577                             "Data to send to the server");
2578             ctrl_editbox(s, "Remote command:", 'r', 100,
2579                          HELPCTX(ssh_command),
2580                          conf_editbox_handler, I(CONF_remote_cmd), I(1));
2581 
2582             s = ctrl_getset(b, "Connection/SSH", "protocol", "Protocol options");
2583             ctrl_checkbox(s, "Don't start a shell or command at all", 'n',
2584                           HELPCTX(ssh_noshell),
2585                           conf_checkbox_handler,
2586                           I(CONF_ssh_no_shell));
2587         }
2588 
2589         if (!midsession || !(protcfginfo == 1 || protcfginfo == -1)) {
2590             s = ctrl_getset(b, "Connection/SSH", "protocol", "Protocol options");
2591 
2592             ctrl_checkbox(s, "Enable compression", 'e',
2593                           HELPCTX(ssh_compress),
2594                           conf_checkbox_handler,
2595                           I(CONF_compression));
2596         }
2597 
2598         if (!midsession) {
2599             s = ctrl_getset(b, "Connection/SSH", "sharing", "Sharing an SSH connection between PuTTY tools");
2600 
2601             ctrl_checkbox(s, "Share SSH connections if possible", 's',
2602                           HELPCTX(ssh_share),
2603                           conf_checkbox_handler,
2604                           I(CONF_ssh_connection_sharing));
2605 
2606             ctrl_text(s, "Permitted roles in a shared connection:",
2607                       HELPCTX(ssh_share));
2608             ctrl_checkbox(s, "Upstream (connecting to the real server)", 'u',
2609                           HELPCTX(ssh_share),
2610                           conf_checkbox_handler,
2611                           I(CONF_ssh_connection_sharing_upstream));
2612             ctrl_checkbox(s, "Downstream (connecting to the upstream PuTTY)", 'd',
2613                           HELPCTX(ssh_share),
2614                           conf_checkbox_handler,
2615                           I(CONF_ssh_connection_sharing_downstream));
2616         }
2617 
2618         if (!midsession) {
2619             s = ctrl_getset(b, "Connection/SSH", "protocol", "Protocol options");
2620 
2621             ctrl_radiobuttons(s, "SSH protocol version:", NO_SHORTCUT, 2,
2622                               HELPCTX(ssh_protocol),
2623                               conf_radiobutton_handler,
2624                               I(CONF_sshprot),
2625                               "2", '2', I(3),
2626                               "1 (INSECURE)", '1', I(0), NULL);
2627         }
2628 
2629         /*
2630          * The Connection/SSH/Kex panel. (Owing to repeat key
2631          * exchange, much of this is meaningful in mid-session _if_
2632          * we're using SSH-2 and are not a connection-sharing
2633          * downstream, or haven't decided yet.)
2634          */
2635         if (protcfginfo != 1 && protcfginfo != -1) {
2636             ctrl_settitle(b, "Connection/SSH/Kex",
2637                           "Options controlling SSH key exchange");
2638 
2639             s = ctrl_getset(b, "Connection/SSH/Kex", "main",
2640                             "Key exchange algorithm options");
2641             c = ctrl_draglist(s, "Algorithm selection policy:", 's',
2642                               HELPCTX(ssh_kexlist),
2643                               kexlist_handler, P(NULL));
2644             c->listbox.height = KEX_MAX;
2645 #ifndef NO_GSSAPI
2646             ctrl_checkbox(s, "Attempt GSSAPI key exchange",
2647                           'k', HELPCTX(ssh_gssapi),
2648                           conf_checkbox_handler,
2649                           I(CONF_try_gssapi_kex));
2650 #endif
2651 
2652             s = ctrl_getset(b, "Connection/SSH/Kex", "repeat",
2653                             "Options controlling key re-exchange");
2654 
2655             ctrl_editbox(s, "Max minutes before rekey (0 for no limit)", 't', 20,
2656                          HELPCTX(ssh_kex_repeat),
2657                          conf_editbox_handler,
2658                          I(CONF_ssh_rekey_time),
2659                          I(-1));
2660 #ifndef NO_GSSAPI
2661             ctrl_editbox(s, "Minutes between GSS checks (0 for never)", NO_SHORTCUT, 20,
2662                          HELPCTX(ssh_kex_repeat),
2663                          conf_editbox_handler,
2664                          I(CONF_gssapirekey),
2665                          I(-1));
2666 #endif
2667             ctrl_editbox(s, "Max data before rekey (0 for no limit)", 'x', 20,
2668                          HELPCTX(ssh_kex_repeat),
2669                          conf_editbox_handler,
2670                          I(CONF_ssh_rekey_data),
2671                          I(16));
2672             ctrl_text(s, "(Use 1M for 1 megabyte, 1G for 1 gigabyte etc)",
2673                       HELPCTX(ssh_kex_repeat));
2674         }
2675 
2676         /*
2677          * The 'Connection/SSH/Host keys' panel.
2678          */
2679         if (protcfginfo != 1 && protcfginfo != -1) {
2680             ctrl_settitle(b, "Connection/SSH/Host keys",
2681                           "Options controlling SSH host keys");
2682 
2683             s = ctrl_getset(b, "Connection/SSH/Host keys", "main",
2684                             "Host key algorithm preference");
2685             c = ctrl_draglist(s, "Algorithm selection policy:", 's',
2686                               HELPCTX(ssh_hklist),
2687                               hklist_handler, P(NULL));
2688             c->listbox.height = 5;
2689 
2690             ctrl_checkbox(s, "Prefer algorithms for which a host key is known",
2691                           'p', HELPCTX(ssh_hk_known), conf_checkbox_handler,
2692                           I(CONF_ssh_prefer_known_hostkeys));
2693         }
2694 
2695         /*
2696          * Manual host key configuration is irrelevant mid-session,
2697          * as we enforce that the host key for rekeys is the
2698          * same as that used at the start of the session.
2699          */
2700         if (!midsession) {
2701             s = ctrl_getset(b, "Connection/SSH/Host keys", "hostkeys",
2702                             "Manually configure host keys for this connection");
2703 
2704             ctrl_columns(s, 2, 75, 25);
2705             c = ctrl_text(s, "Host keys or fingerprints to accept:",
2706                           HELPCTX(ssh_kex_manual_hostkeys));
2707             c->generic.column = 0;
2708             /* You want to select from the list, _then_ hit Remove. So
2709              * tab order should be that way round. */
2710             mh = (struct manual_hostkey_data *)
2711                 ctrl_alloc(b,sizeof(struct manual_hostkey_data));
2712             mh->rembutton = ctrl_pushbutton(s, "Remove", 'r',
2713                                             HELPCTX(ssh_kex_manual_hostkeys),
2714                                             manual_hostkey_handler, P(mh));
2715             mh->rembutton->generic.column = 1;
2716             mh->rembutton->generic.tabdelay = true;
2717             mh->listbox = ctrl_listbox(s, NULL, NO_SHORTCUT,
2718                                        HELPCTX(ssh_kex_manual_hostkeys),
2719                                        manual_hostkey_handler, P(mh));
2720             /* This list box can't be very tall, because there's not
2721              * much room in the pane on Windows at least. This makes
2722              * it become really unhelpful if a horizontal scrollbar
2723              * appears, so we suppress that. */
2724             mh->listbox->listbox.height = 2;
2725             mh->listbox->listbox.hscroll = false;
2726             ctrl_tabdelay(s, mh->rembutton);
2727             mh->keybox = ctrl_editbox(s, "Key", 'k', 80,
2728                                       HELPCTX(ssh_kex_manual_hostkeys),
2729                                       manual_hostkey_handler, P(mh), P(NULL));
2730             mh->keybox->generic.column = 0;
2731             mh->addbutton = ctrl_pushbutton(s, "Add key", 'y',
2732                                             HELPCTX(ssh_kex_manual_hostkeys),
2733                                             manual_hostkey_handler, P(mh));
2734             mh->addbutton->generic.column = 1;
2735             ctrl_columns(s, 1, 100);
2736         }
2737 
2738         if (!midsession || !(protcfginfo == 1 || protcfginfo == -1)) {
2739             /*
2740              * The Connection/SSH/Cipher panel.
2741              */
2742             ctrl_settitle(b, "Connection/SSH/Cipher",
2743                           "Options controlling SSH encryption");
2744 
2745             s = ctrl_getset(b, "Connection/SSH/Cipher",
2746                             "encryption", "Encryption options");
2747             c = ctrl_draglist(s, "Encryption cipher selection policy:", 's',
2748                               HELPCTX(ssh_ciphers),
2749                               cipherlist_handler, P(NULL));
2750             c->listbox.height = 6;
2751 
2752             ctrl_checkbox(s, "Enable legacy use of single-DES in SSH-2", 'i',
2753                           HELPCTX(ssh_ciphers),
2754                           conf_checkbox_handler,
2755                           I(CONF_ssh2_des_cbc));
2756         }
2757 
2758         if (!midsession) {
2759 
2760             /*
2761              * The Connection/SSH/Auth panel.
2762              */
2763             ctrl_settitle(b, "Connection/SSH/Auth",
2764                           "Options controlling SSH authentication");
2765 
2766             s = ctrl_getset(b, "Connection/SSH/Auth", "main", NULL);
2767             ctrl_checkbox(s, "Display pre-authentication banner (SSH-2 only)",
2768                           'd', HELPCTX(ssh_auth_banner),
2769                           conf_checkbox_handler,
2770                           I(CONF_ssh_show_banner));
2771             ctrl_checkbox(s, "Bypass authentication entirely (SSH-2 only)", 'b',
2772                           HELPCTX(ssh_auth_bypass),
2773                           conf_checkbox_handler,
2774                           I(CONF_ssh_no_userauth));
2775             ctrl_checkbox(s, "Disconnect if authentication succeeds trivially",
2776                           'n', HELPCTX(ssh_no_trivial_userauth),
2777                           conf_checkbox_handler,
2778                           I(CONF_ssh_no_trivial_userauth));
2779 
2780             s = ctrl_getset(b, "Connection/SSH/Auth", "methods",
2781                             "Authentication methods");
2782             ctrl_checkbox(s, "Attempt authentication using Pageant", 'p',
2783                           HELPCTX(ssh_auth_pageant),
2784                           conf_checkbox_handler,
2785                           I(CONF_tryagent));
2786             ctrl_checkbox(s, "Attempt TIS or CryptoCard auth (SSH-1)", 'm',
2787                           HELPCTX(ssh_auth_tis),
2788                           conf_checkbox_handler,
2789                           I(CONF_try_tis_auth));
2790             ctrl_checkbox(s, "Attempt \"keyboard-interactive\" auth (SSH-2)",
2791                           'i', HELPCTX(ssh_auth_ki),
2792                           conf_checkbox_handler,
2793                           I(CONF_try_ki_auth));
2794 
2795             s = ctrl_getset(b, "Connection/SSH/Auth", "params",
2796                             "Authentication parameters");
2797             ctrl_checkbox(s, "Allow agent forwarding", 'f',
2798                           HELPCTX(ssh_auth_agentfwd),
2799                           conf_checkbox_handler, I(CONF_agentfwd));
2800             ctrl_checkbox(s, "Allow attempted changes of username in SSH-2", NO_SHORTCUT,
2801                           HELPCTX(ssh_auth_changeuser),
2802                           conf_checkbox_handler,
2803                           I(CONF_change_username));
2804             ctrl_filesel(s, "Private key file for authentication:", 'k',
2805                          FILTER_KEY_FILES, false, "Select private key file",
2806                          HELPCTX(ssh_auth_privkey),
2807                          conf_filesel_handler, I(CONF_keyfile));
2808 
2809 #ifndef NO_GSSAPI
2810             /*
2811              * Connection/SSH/Auth/GSSAPI, which sadly won't fit on
2812              * the main Auth panel.
2813              */
2814             ctrl_settitle(b, "Connection/SSH/Auth/GSSAPI",
2815                           "Options controlling GSSAPI authentication");
2816             s = ctrl_getset(b, "Connection/SSH/Auth/GSSAPI", "gssapi", NULL);
2817 
2818             ctrl_checkbox(s, "Attempt GSSAPI authentication (SSH-2 only)",
2819                           't', HELPCTX(ssh_gssapi),
2820                           conf_checkbox_handler,
2821                           I(CONF_try_gssapi_auth));
2822 
2823             ctrl_checkbox(s, "Attempt GSSAPI key exchange (SSH-2 only)",
2824                           'k', HELPCTX(ssh_gssapi),
2825                           conf_checkbox_handler,
2826                           I(CONF_try_gssapi_kex));
2827 
2828             ctrl_checkbox(s, "Allow GSSAPI credential delegation", 'l',
2829                           HELPCTX(ssh_gssapi_delegation),
2830                           conf_checkbox_handler,
2831                           I(CONF_gssapifwd));
2832 
2833             /*
2834              * GSSAPI library selection.
2835              */
2836             if (ngsslibs > 1) {
2837                 c = ctrl_draglist(s, "Preference order for GSSAPI libraries:",
2838                                   'p', HELPCTX(ssh_gssapi_libraries),
2839                                   gsslist_handler, P(NULL));
2840                 c->listbox.height = ngsslibs;
2841 
2842                 /*
2843                  * I currently assume that if more than one GSS
2844                  * library option is available, then one of them is
2845                  * 'user-supplied' and so we should present the
2846                  * following file selector. This is at least half-
2847                  * reasonable, because if we're using statically
2848                  * linked GSSAPI then there will only be one option
2849                  * and no way to load from a user-supplied library,
2850                  * whereas if we're using dynamic libraries then
2851                  * there will almost certainly be some default
2852                  * option in addition to a user-supplied path. If
2853                  * anyone ever ports PuTTY to a system on which
2854                  * dynamic-library GSSAPI is available but there is
2855                  * absolutely no consensus on where to keep the
2856                  * libraries, there'll need to be a flag alongside
2857                  * ngsslibs to control whether the file selector is
2858                  * displayed.
2859                  */
2860 
2861                 ctrl_filesel(s, "User-supplied GSSAPI library path:", 's',
2862                              FILTER_DYNLIB_FILES, false, "Select library file",
2863                              HELPCTX(ssh_gssapi_libraries),
2864                              conf_filesel_handler,
2865                              I(CONF_ssh_gss_custom));
2866             }
2867 #endif
2868         }
2869 
2870         if (!midsession) {
2871             /*
2872              * The Connection/SSH/TTY panel.
2873              */
2874             ctrl_settitle(b, "Connection/SSH/TTY", "Remote terminal settings");
2875 
2876             s = ctrl_getset(b, "Connection/SSH/TTY", "sshtty", NULL);
2877             ctrl_checkbox(s, "Don't allocate a pseudo-terminal", 'p',
2878                           HELPCTX(ssh_nopty),
2879                           conf_checkbox_handler,
2880                           I(CONF_nopty));
2881 
2882             s = ctrl_getset(b, "Connection/SSH/TTY", "ttymodes",
2883                             "Terminal modes");
2884             td = (struct ttymodes_data *)
2885                 ctrl_alloc(b, sizeof(struct ttymodes_data));
2886             ctrl_text(s, "Terminal modes to send:", HELPCTX(ssh_ttymodes));
2887             td->listbox = ctrl_listbox(s, NULL, NO_SHORTCUT,
2888                                        HELPCTX(ssh_ttymodes),
2889                                        ttymodes_handler, P(td));
2890             td->listbox->listbox.height = 8;
2891             td->listbox->listbox.ncols = 2;
2892             td->listbox->listbox.percentages = snewn(2, int);
2893             td->listbox->listbox.percentages[0] = 40;
2894             td->listbox->listbox.percentages[1] = 60;
2895             ctrl_columns(s, 2, 75, 25);
2896             c = ctrl_text(s, "For selected mode, send:", HELPCTX(ssh_ttymodes));
2897             c->generic.column = 0;
2898             td->setbutton = ctrl_pushbutton(s, "Set", 's',
2899                                             HELPCTX(ssh_ttymodes),
2900                                             ttymodes_handler, P(td));
2901             td->setbutton->generic.column = 1;
2902             td->setbutton->generic.tabdelay = true;
2903             ctrl_columns(s, 1, 100);        /* column break */
2904             /* Bit of a hack to get the value radio buttons and
2905              * edit-box on the same row. */
2906             ctrl_columns(s, 2, 75, 25);
2907             td->valradio = ctrl_radiobuttons(s, NULL, NO_SHORTCUT, 3,
2908                                              HELPCTX(ssh_ttymodes),
2909                                              ttymodes_handler, P(td),
2910                                              "Auto", NO_SHORTCUT, P(NULL),
2911                                              "Nothing", NO_SHORTCUT, P(NULL),
2912                                              "This:", NO_SHORTCUT, P(NULL),
2913                                              NULL);
2914             td->valradio->generic.column = 0;
2915             td->valbox = ctrl_editbox(s, NULL, NO_SHORTCUT, 100,
2916                                       HELPCTX(ssh_ttymodes),
2917                                       ttymodes_handler, P(td), P(NULL));
2918             td->valbox->generic.column = 1;
2919             td->valbox->generic.align_next_to = td->valradio;
2920             ctrl_tabdelay(s, td->setbutton);
2921         }
2922 
2923         if (!midsession) {
2924             /*
2925              * The Connection/SSH/X11 panel.
2926              */
2927             ctrl_settitle(b, "Connection/SSH/X11",
2928                           "Options controlling SSH X11 forwarding");
2929 
2930             s = ctrl_getset(b, "Connection/SSH/X11", "x11", "X11 forwarding");
2931             ctrl_checkbox(s, "Enable X11 forwarding", 'e',
2932                           HELPCTX(ssh_tunnels_x11),
2933                           conf_checkbox_handler,I(CONF_x11_forward));
2934             ctrl_editbox(s, "X display location", 'x', 50,
2935                          HELPCTX(ssh_tunnels_x11),
2936                          conf_editbox_handler, I(CONF_x11_display), I(1));
2937             ctrl_radiobuttons(s, "Remote X11 authentication protocol", 'u', 2,
2938                               HELPCTX(ssh_tunnels_x11auth),
2939                               conf_radiobutton_handler,
2940                               I(CONF_x11_auth),
2941                               "MIT-Magic-Cookie-1", I(X11_MIT),
2942                               "XDM-Authorization-1", I(X11_XDM), NULL);
2943         }
2944 
2945         /*
2946          * The Tunnels panel _is_ still available in mid-session.
2947          */
2948         ctrl_settitle(b, "Connection/SSH/Tunnels",
2949                       "Options controlling SSH port forwarding");
2950 
2951         s = ctrl_getset(b, "Connection/SSH/Tunnels", "portfwd",
2952                         "Port forwarding");
2953         ctrl_checkbox(s, "Local ports accept connections from other hosts",'t',
2954                       HELPCTX(ssh_tunnels_portfwd_localhost),
2955                       conf_checkbox_handler,
2956                       I(CONF_lport_acceptall));
2957         ctrl_checkbox(s, "Remote ports do the same (SSH-2 only)", 'p',
2958                       HELPCTX(ssh_tunnels_portfwd_localhost),
2959                       conf_checkbox_handler,
2960                       I(CONF_rport_acceptall));
2961 
2962         ctrl_columns(s, 3, 55, 20, 25);
2963         c = ctrl_text(s, "Forwarded ports:", HELPCTX(ssh_tunnels_portfwd));
2964         c->generic.column = COLUMN_FIELD(0,2);
2965         /* You want to select from the list, _then_ hit Remove. So tab order
2966          * should be that way round. */
2967         pfd = (struct portfwd_data *)ctrl_alloc(b,sizeof(struct portfwd_data));
2968         pfd->rembutton = ctrl_pushbutton(s, "Remove", 'r',
2969                                          HELPCTX(ssh_tunnels_portfwd),
2970                                          portfwd_handler, P(pfd));
2971         pfd->rembutton->generic.column = 2;
2972         pfd->rembutton->generic.tabdelay = true;
2973         pfd->listbox = ctrl_listbox(s, NULL, NO_SHORTCUT,
2974                                     HELPCTX(ssh_tunnels_portfwd),
2975                                     portfwd_handler, P(pfd));
2976         pfd->listbox->listbox.height = 3;
2977         pfd->listbox->listbox.ncols = 2;
2978         pfd->listbox->listbox.percentages = snewn(2, int);
2979         pfd->listbox->listbox.percentages[0] = 20;
2980         pfd->listbox->listbox.percentages[1] = 80;
2981         ctrl_tabdelay(s, pfd->rembutton);
2982         ctrl_text(s, "Add new forwarded port:", HELPCTX(ssh_tunnels_portfwd));
2983         /* You want to enter source, destination and type, _then_ hit Add.
2984          * Again, we adjust the tab order to reflect this. */
2985         pfd->addbutton = ctrl_pushbutton(s, "Add", 'd',
2986                                          HELPCTX(ssh_tunnels_portfwd),
2987                                          portfwd_handler, P(pfd));
2988         pfd->addbutton->generic.column = 2;
2989         pfd->addbutton->generic.tabdelay = true;
2990         pfd->sourcebox = ctrl_editbox(s, "Source port", 's', 40,
2991                                       HELPCTX(ssh_tunnels_portfwd),
2992                                       portfwd_handler, P(pfd), P(NULL));
2993         pfd->sourcebox->generic.column = 0;
2994         pfd->destbox = ctrl_editbox(s, "Destination", 'i', 67,
2995                                     HELPCTX(ssh_tunnels_portfwd),
2996                                     portfwd_handler, P(pfd), P(NULL));
2997         pfd->direction = ctrl_radiobuttons(s, NULL, NO_SHORTCUT, 3,
2998                                            HELPCTX(ssh_tunnels_portfwd),
2999                                            portfwd_handler, P(pfd),
3000                                            "Local", 'l', P(NULL),
3001                                            "Remote", 'm', P(NULL),
3002                                            "Dynamic", 'y', P(NULL),
3003                                            NULL);
3004 #ifndef NO_IPV6
3005         pfd->addressfamily =
3006             ctrl_radiobuttons(s, NULL, NO_SHORTCUT, 3,
3007                               HELPCTX(ssh_tunnels_portfwd_ipversion),
3008                               portfwd_handler, P(pfd),
3009                               "Auto", 'u', I(ADDRTYPE_UNSPEC),
3010                               "IPv4", '4', I(ADDRTYPE_IPV4),
3011                               "IPv6", '6', I(ADDRTYPE_IPV6),
3012                               NULL);
3013 #endif
3014         ctrl_tabdelay(s, pfd->addbutton);
3015         ctrl_columns(s, 1, 100);
3016 
3017         if (!midsession) {
3018             /*
3019              * The Connection/SSH/Bugs panels.
3020              */
3021             ctrl_settitle(b, "Connection/SSH/Bugs",
3022                           "Workarounds for SSH server bugs");
3023 
3024             s = ctrl_getset(b, "Connection/SSH/Bugs", "main",
3025                             "Detection of known bugs in SSH servers");
3026             ctrl_droplist(s, "Chokes on SSH-2 ignore messages", '2', 20,
3027                           HELPCTX(ssh_bugs_ignore2),
3028                           sshbug_handler, I(CONF_sshbug_ignore2));
3029             ctrl_droplist(s, "Handles SSH-2 key re-exchange badly", 'k', 20,
3030                           HELPCTX(ssh_bugs_rekey2),
3031                           sshbug_handler, I(CONF_sshbug_rekey2));
3032             ctrl_droplist(s, "Chokes on PuTTY's SSH-2 'winadj' requests", 'j',
3033                           20, HELPCTX(ssh_bugs_winadj),
3034                           sshbug_handler, I(CONF_sshbug_winadj));
3035             ctrl_droplist(s, "Replies to requests on closed channels", 'q', 20,
3036                           HELPCTX(ssh_bugs_chanreq),
3037                           sshbug_handler, I(CONF_sshbug_chanreq));
3038             ctrl_droplist(s, "Ignores SSH-2 maximum packet size", 'x', 20,
3039                           HELPCTX(ssh_bugs_maxpkt2),
3040                           sshbug_handler, I(CONF_sshbug_maxpkt2));
3041 
3042             ctrl_settitle(b, "Connection/SSH/More bugs",
3043                           "Further workarounds for SSH server bugs");
3044 
3045             s = ctrl_getset(b, "Connection/SSH/More bugs", "main",
3046                             "Detection of known bugs in SSH servers");
3047             ctrl_droplist(s, "Requires padding on SSH-2 RSA signatures", 'p', 20,
3048                           HELPCTX(ssh_bugs_rsapad2),
3049                           sshbug_handler, I(CONF_sshbug_rsapad2));
3050             ctrl_droplist(s, "Only supports pre-RFC4419 SSH-2 DH GEX", 'd', 20,
3051                           HELPCTX(ssh_bugs_oldgex2),
3052                           sshbug_handler, I(CONF_sshbug_oldgex2));
3053             ctrl_droplist(s, "Miscomputes SSH-2 HMAC keys", 'm', 20,
3054                           HELPCTX(ssh_bugs_hmac2),
3055                           sshbug_handler, I(CONF_sshbug_hmac2));
3056             ctrl_droplist(s, "Misuses the session ID in SSH-2 PK auth", 'n', 20,
3057                           HELPCTX(ssh_bugs_pksessid2),
3058                           sshbug_handler, I(CONF_sshbug_pksessid2));
3059             ctrl_droplist(s, "Miscomputes SSH-2 encryption keys", 'e', 20,
3060                           HELPCTX(ssh_bugs_derivekey2),
3061                           sshbug_handler, I(CONF_sshbug_derivekey2));
3062             ctrl_droplist(s, "Chokes on SSH-1 ignore messages", 'i', 20,
3063                           HELPCTX(ssh_bugs_ignore1),
3064                           sshbug_handler, I(CONF_sshbug_ignore1));
3065             ctrl_droplist(s, "Refuses all SSH-1 password camouflage", 's', 20,
3066                           HELPCTX(ssh_bugs_plainpw1),
3067                           sshbug_handler, I(CONF_sshbug_plainpw1));
3068             ctrl_droplist(s, "Chokes on SSH-1 RSA authentication", 'r', 20,
3069                           HELPCTX(ssh_bugs_rsa1),
3070                           sshbug_handler, I(CONF_sshbug_rsa1));
3071         }
3072     }
3073 
3074     if (DISPLAY_RECONFIGURABLE_PROTOCOL(PROT_SERIAL)) {
3075         const BackendVtable *ser_vt = backend_vt_from_proto(PROT_SERIAL);
3076 
3077         /*
3078          * The Connection/Serial panel.
3079          */
3080         ctrl_settitle(b, "Connection/Serial",
3081                       "Options controlling local serial lines");
3082 
3083         if (!midsession) {
3084             /*
3085              * We don't permit switching to a different serial port in
3086              * midflight, although we do allow all other
3087              * reconfiguration.
3088              */
3089             s = ctrl_getset(b, "Connection/Serial", "serline",
3090                             "Select a serial line");
3091             ctrl_editbox(s, "Serial line to connect to", 'l', 40,
3092                          HELPCTX(serial_line),
3093                          conf_editbox_handler, I(CONF_serline), I(1));
3094         }
3095 
3096         s = ctrl_getset(b, "Connection/Serial", "sercfg", "Configure the serial line");
3097         ctrl_editbox(s, "Speed (baud)", 's', 40,
3098                      HELPCTX(serial_speed),
3099                      conf_editbox_handler, I(CONF_serspeed), I(-1));
3100         ctrl_editbox(s, "Data bits", 'b', 40,
3101                      HELPCTX(serial_databits),
3102                      conf_editbox_handler, I(CONF_serdatabits), I(-1));
3103         /*
3104          * Stop bits come in units of one half.
3105          */
3106         ctrl_editbox(s, "Stop bits", 't', 40,
3107                      HELPCTX(serial_stopbits),
3108                      conf_editbox_handler, I(CONF_serstopbits), I(-2));
3109         ctrl_droplist(s, "Parity", 'p', 40,
3110                       HELPCTX(serial_parity), serial_parity_handler,
3111                       I(ser_vt->serial_parity_mask));
3112         ctrl_droplist(s, "Flow control", 'f', 40,
3113                       HELPCTX(serial_flow), serial_flow_handler,
3114                       I(ser_vt->serial_flow_mask));
3115     }
3116 
3117     if (DISPLAY_RECONFIGURABLE_PROTOCOL(PROT_TELNET)) {
3118         /*
3119          * The Connection/Telnet panel.
3120          */
3121         ctrl_settitle(b, "Connection/Telnet",
3122                       "Options controlling Telnet connections");
3123 
3124         s = ctrl_getset(b, "Connection/Telnet", "protocol",
3125                         "Telnet protocol adjustments");
3126 
3127         if (!midsession) {
3128             ctrl_radiobuttons(s, "Handling of OLD_ENVIRON ambiguity:",
3129                               NO_SHORTCUT, 2,
3130                               HELPCTX(telnet_oldenviron),
3131                               conf_radiobutton_bool_handler,
3132                               I(CONF_rfc_environ),
3133                               "BSD (commonplace)", 'b', I(false),
3134                               "RFC 1408 (unusual)", 'f', I(true), NULL);
3135             ctrl_radiobuttons(s, "Telnet negotiation mode:", 't', 2,
3136                               HELPCTX(telnet_passive),
3137                               conf_radiobutton_bool_handler,
3138                               I(CONF_passive_telnet),
3139                               "Passive", I(true), "Active", I(false), NULL);
3140         }
3141         ctrl_checkbox(s, "Keyboard sends Telnet special commands", 'k',
3142                       HELPCTX(telnet_specialkeys),
3143                       conf_checkbox_handler,
3144                       I(CONF_telnet_keyboard));
3145         ctrl_checkbox(s, "Return key sends Telnet New Line instead of ^M",
3146                       'm', HELPCTX(telnet_newline),
3147                       conf_checkbox_handler,
3148                       I(CONF_telnet_newline));
3149     }
3150 
3151     if (DISPLAY_NON_RECONFIGURABLE_PROTOCOL(PROT_RLOGIN)) {
3152         /*
3153          * The Connection/Rlogin panel.
3154          */
3155         ctrl_settitle(b, "Connection/Rlogin",
3156                       "Options controlling Rlogin connections");
3157 
3158         s = ctrl_getset(b, "Connection/Rlogin", "data",
3159                         "Data to send to the server");
3160         ctrl_editbox(s, "Local username:", 'l', 50,
3161                      HELPCTX(rlogin_localuser),
3162                      conf_editbox_handler, I(CONF_localusername), I(1));
3163 
3164     }
3165 
3166     if (DISPLAY_NON_RECONFIGURABLE_PROTOCOL(PROT_SUPDUP)) {
3167         /*
3168          * The Connection/SUPDUP panel.
3169          */
3170         ctrl_settitle(b, "Connection/SUPDUP",
3171                       "Options controlling SUPDUP connections");
3172 
3173         s = ctrl_getset(b, "Connection/SUPDUP", "main", NULL);
3174 
3175         ctrl_editbox(s, "Location string", 'l', 70,
3176                      HELPCTX(supdup_location),
3177                      conf_editbox_handler, I(CONF_supdup_location),
3178                      I(1));
3179 
3180         ctrl_radiobuttons(s, "Extended ASCII Character set:", 'e', 4,
3181                           HELPCTX(supdup_ascii),
3182                           conf_radiobutton_handler,
3183                           I(CONF_supdup_ascii_set),
3184                           "None", I(SUPDUP_CHARSET_ASCII),
3185                           "ITS", I(SUPDUP_CHARSET_ITS),
3186                           "WAITS", I(SUPDUP_CHARSET_WAITS), NULL);
3187 
3188         ctrl_checkbox(s, "**MORE** processing", 'm',
3189                       HELPCTX(supdup_more),
3190                       conf_checkbox_handler,
3191                       I(CONF_supdup_more));
3192 
3193         ctrl_checkbox(s, "Terminal scrolling", 's',
3194                       HELPCTX(supdup_scroll),
3195                       conf_checkbox_handler,
3196                       I(CONF_supdup_scroll));
3197     }
3198 }
3199