forked from Yasushi/putty
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.c
3363 lines (3092 loc) · 136 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* config.c - the platform-independent parts of the PuTTY
* configuration box.
*/
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include "putty.h"
#include "dialog.h"
#include "storage.h"
#include "tree234.h"
#define PRINTER_DISABLED_STRING "None (printing disabled)"
#define HOST_BOX_TITLE "Host Name (or IP address)"
#define PORT_BOX_TITLE "Port"
void conf_radiobutton_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
int button;
Conf *conf = (Conf *)data;
/*
* For a standard radio button set, the context parameter gives
* the primary key (CONF_foo), and the extra data per button
* gives the value the target field should take if that button
* is the one selected.
*/
if (event == EVENT_REFRESH) {
int val = conf_get_int(conf, ctrl->context.i);
for (button = 0; button < ctrl->radio.nbuttons; button++)
if (val == ctrl->radio.buttondata[button].i)
break;
/* We expected that `break' to happen, in all circumstances. */
assert(button < ctrl->radio.nbuttons);
dlg_radiobutton_set(ctrl, dlg, button);
} else if (event == EVENT_VALCHANGE) {
button = dlg_radiobutton_get(ctrl, dlg);
assert(button >= 0 && button < ctrl->radio.nbuttons);
conf_set_int(conf, ctrl->context.i,
ctrl->radio.buttondata[button].i);
}
}
void conf_radiobutton_bool_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
int button;
Conf *conf = (Conf *)data;
/*
* Same as conf_radiobutton_handler, but using conf_set_bool in
* place of conf_set_int, because it's dealing with a bool-typed
* config option.
*/
if (event == EVENT_REFRESH) {
int val = conf_get_bool(conf, ctrl->context.i);
for (button = 0; button < ctrl->radio.nbuttons; button++)
if (val == ctrl->radio.buttondata[button].i)
break;
/* We expected that `break' to happen, in all circumstances. */
assert(button < ctrl->radio.nbuttons);
dlg_radiobutton_set(ctrl, dlg, button);
} else if (event == EVENT_VALCHANGE) {
button = dlg_radiobutton_get(ctrl, dlg);
assert(button >= 0 && button < ctrl->radio.nbuttons);
conf_set_bool(conf, ctrl->context.i,
ctrl->radio.buttondata[button].i);
}
}
#define CHECKBOX_INVERT (1<<30)
void conf_checkbox_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
int key;
bool invert;
Conf *conf = (Conf *)data;
/*
* For a standard checkbox, the context parameter gives the
* primary key (CONF_foo), optionally ORed with CHECKBOX_INVERT.
*/
key = ctrl->context.i;
if (key & CHECKBOX_INVERT) {
key &= ~CHECKBOX_INVERT;
invert = true;
} else
invert = false;
/*
* C lacks a logical XOR, so the following code uses the idiom
* (!a ^ !b) to obtain the logical XOR of a and b. (That is, 1
* iff exactly one of a and b is nonzero, otherwise 0.)
*/
if (event == EVENT_REFRESH) {
bool val = conf_get_bool(conf, key);
dlg_checkbox_set(ctrl, dlg, (!val ^ !invert));
} else if (event == EVENT_VALCHANGE) {
conf_set_bool(conf, key, !dlg_checkbox_get(ctrl,dlg) ^ !invert);
}
}
const struct conf_editbox_handler_type conf_editbox_str = {.type = EDIT_STR};
const struct conf_editbox_handler_type conf_editbox_int = {.type = EDIT_INT};
void conf_editbox_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
/*
* The standard edit-box handler expects the main `context' field
* to contain the primary key. The secondary `context2' field is a
* pointer to the struct conf_editbox_handler_type defined in
* putty.h.
*/
int key = ctrl->context.i;
const struct conf_editbox_handler_type *type = ctrl->context2.cp;
Conf *conf = (Conf *)data;
if (type->type == EDIT_STR) {
if (event == EVENT_REFRESH) {
bool utf8;
char *field = conf_get_str_ambi(conf, key, &utf8);
if (utf8)
dlg_editbox_set_utf8(ctrl, dlg, field);
else
dlg_editbox_set(ctrl, dlg, field);
} else if (event == EVENT_VALCHANGE) {
char *field = dlg_editbox_get_utf8(ctrl, dlg);
if (!conf_try_set_utf8(conf, key, field)) {
sfree(field);
field = dlg_editbox_get(ctrl, dlg);
conf_set_str(conf, key, field);
}
sfree(field);
}
} else {
if (event == EVENT_REFRESH) {
char str[80];
int value = conf_get_int(conf, key);
if (type->type == EDIT_INT)
sprintf(str, "%d", value);
else
sprintf(str, "%g", (double)value / type->denominator);
dlg_editbox_set(ctrl, dlg, str);
} else if (event == EVENT_VALCHANGE) {
char *str = dlg_editbox_get(ctrl, dlg);
if (type->type == EDIT_INT)
conf_set_int(conf, key, atoi(str));
else
conf_set_int(conf, key, (int)round(type->denominator * atof(str)));
sfree(str);
}
}
}
void conf_filesel_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
int key = ctrl->context.i;
Conf *conf = (Conf *)data;
if (event == EVENT_REFRESH) {
dlg_filesel_set(
ctrl, dlg, conf_get_filename(conf, key));
} else if (event == EVENT_VALCHANGE) {
Filename *filename = dlg_filesel_get(ctrl, dlg);
conf_set_filename(conf, key, filename);
filename_free(filename);
}
}
void conf_fontsel_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
int key = ctrl->context.i;
Conf *conf = (Conf *)data;
if (event == EVENT_REFRESH) {
dlg_fontsel_set(
ctrl, dlg, conf_get_fontspec(conf, key));
} else if (event == EVENT_VALCHANGE) {
FontSpec *fontspec = dlg_fontsel_get(ctrl, dlg);
conf_set_fontspec(conf, key, fontspec);
fontspec_free(fontspec);
}
}
static void config_host_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
Conf *conf = (Conf *)data;
/*
* This function works just like the standard edit box handler,
* only it has to choose the control's label and text from two
* different places depending on the protocol.
*/
if (event == EVENT_REFRESH) {
if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL) {
/*
* This label text is carefully chosen to contain an n,
* since that's the shortcut for the host name control.
*/
dlg_label_change(ctrl, dlg, "Serial line");
dlg_editbox_set(ctrl, dlg, conf_get_str(conf, CONF_serline));
} else {
dlg_label_change(ctrl, dlg, HOST_BOX_TITLE);
dlg_editbox_set(ctrl, dlg, conf_get_str(conf, CONF_host));
}
} else if (event == EVENT_VALCHANGE) {
char *s = dlg_editbox_get(ctrl, dlg);
if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
conf_set_str(conf, CONF_serline, s);
else
conf_set_str(conf, CONF_host, s);
sfree(s);
}
}
static void config_port_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
Conf *conf = (Conf *)data;
char buf[80];
/*
* This function works similarly to the standard edit box handler,
* only it has to choose the control's label and text from two
* different places depending on the protocol.
*/
if (event == EVENT_REFRESH) {
if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL) {
/*
* This label text is carefully chosen to contain a p,
* since that's the shortcut for the port control.
*/
dlg_label_change(ctrl, dlg, "Speed");
sprintf(buf, "%d", conf_get_int(conf, CONF_serspeed));
} else {
dlg_label_change(ctrl, dlg, PORT_BOX_TITLE);
if (conf_get_int(conf, CONF_port) != 0)
sprintf(buf, "%d", conf_get_int(conf, CONF_port));
else
/* Display an (invalid) port of 0 as blank */
buf[0] = '\0';
}
dlg_editbox_set(ctrl, dlg, buf);
} else if (event == EVENT_VALCHANGE) {
char *s = dlg_editbox_get(ctrl, dlg);
int i = atoi(s);
sfree(s);
if (conf_get_int(conf, CONF_protocol) == PROT_SERIAL)
conf_set_int(conf, CONF_serspeed, i);
else
conf_set_int(conf, CONF_port, i);
}
}
struct hostport {
dlgcontrol *host, *port, *protradio, *protlist;
bool mid_refresh;
};
/*
* Shared handler for protocol radio-button and drop-list controls.
* Handles the interaction of those two controls, and also changes
* the setting of the port box to match the protocol if necessary,
* and refreshes both host and port boxes when switching to/from the
* serial backend.
*/
static void config_protocols_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
Conf *conf = (Conf *)data;
int curproto = conf_get_int(conf, CONF_protocol);
struct hostport *hp = (struct hostport *)ctrl->context.p;
if (event == EVENT_REFRESH) {
/*
* Refresh the states of the controls from Conf.
*
* When refreshing these controls, we have to watch out for
* re-entrancy: because there are two controls involved, the
* refresh is not atomic, so the VALCHANGE and/or SELCHANGE
* callbacks resulting from our updates here might cause other
* settings here to change unwantedly. (E.g. setting the list
* selection shouldn't trigger the SELCHANGE side effect of
* selecting the Other radio button; setting the radio button
* to Other here shouldn't have the side effect of selecting
* whatever protocol is _currently_ selected in the list box,
* if we haven't selected the right one yet.)
*/
hp->mid_refresh = true;
if (ctrl == hp->protradio) {
/* Available buttons were set up when control was created.
* Just select one of them, possibly. */
for (int button = 0; button < ctrl->radio.nbuttons; button++)
/* The final button is "Other:". If we reach that one, the
* current protocol must be in the drop list, so we should
* select the "Other:" button. */
if (curproto == ctrl->radio.buttondata[button].i ||
button == ctrl->radio.nbuttons-1) {
dlg_radiobutton_set(ctrl, dlg, button);
break;
}
} else if (ctrl == hp->protlist) {
int curentry = -1;
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
assert(n_ui_backends > 0 && n_ui_backends < PROTOCOL_LIMIT);
for (size_t i = n_ui_backends;
i < PROTOCOL_LIMIT && backends[i]; i++) {
dlg_listbox_addwithid(ctrl, dlg,
backends[i]->displayname_tc,
backends[i]->protocol);
if (backends[i]->protocol == curproto)
curentry = i - n_ui_backends;
}
if (curentry > 0) {
/*
* The currently configured protocol is one of the
* list-box ones, so select it in protlist.
*
* (The corresponding refresh event for protradio
* should have selected the "Other:" radio button, to
* keep things consistent.)
*/
dlg_listbox_select(ctrl, dlg, curentry);
} else {
/*
* If the currently configured protocol is one of the
* radio buttons, we must still ensure *something* is
* selected in the list box. The sensible default is
* the first list element, which be_*.c ought to have
* arranged to be the 'runner-up' in protocol
* popularity out of the ones relegated to the list
* box.
*
* We don't make much effort to retain the state of
* the list box when it doesn't correspond to an
* actual protocol. So it's easy for this case to be
* reached as a side effect of other actions, e.g.
* loading a saved session that has a radio-button
* protocol configured.
*/
dlg_listbox_select(ctrl, dlg, 0);
}
dlg_update_done(ctrl, dlg);
}
hp->mid_refresh = false;
} else if (!hp->mid_refresh) {
/*
* Potentially update Conf from the states of the controls.
*/
int newproto = curproto;
if (event == EVENT_VALCHANGE && ctrl == hp->protradio) {
int button = dlg_radiobutton_get(ctrl, dlg);
assert(button >= 0 && button < ctrl->radio.nbuttons);
if (ctrl->radio.buttondata[button].i == -1) {
/*
* The 'Other' radio button was selected, which means we
* have to set CONF_protocol based on the currently
* selected list box entry.
*
* (We conditionalise this on there _being_ a selected
* list box entry. I hope the case where nothing is
* selected can't actually come up except during
* initialisation, and I also hope that hp->mid_session
* will prevent that case from getting here. But as a
* last-ditch fallback, this if statement should at least
* guarantee that we don't pass a nonsense value to
* dlg_listbox_getid.)
*/
int i = dlg_listbox_index(hp->protlist, dlg);
if (i >= 0)
newproto = dlg_listbox_getid(hp->protlist, dlg, i);
} else {
newproto = ctrl->radio.buttondata[button].i;
}
} else if (event == EVENT_SELCHANGE && ctrl == hp->protlist) {
int i = dlg_listbox_index(ctrl, dlg);
if (i >= 0) {
newproto = dlg_listbox_getid(ctrl, dlg, i);
/* Select the "Other" radio button, too */
dlg_radiobutton_set(hp->protradio, dlg,
hp->protradio->radio.nbuttons-1);
}
}
if (newproto != curproto) {
conf_set_int(conf, CONF_protocol, newproto);
const struct BackendVtable *cvt = backend_vt_from_proto(curproto);
const struct BackendVtable *nvt = backend_vt_from_proto(newproto);
assert(cvt);
assert(nvt);
/*
* Iff the user hasn't changed the port from the old
* protocol's default, update it with the new protocol's
* default.
*
* (This includes a "default" of 0, implying that there is
* no sensible default for that protocol; in this case
* it's displayed as a blank.)
*
* This helps with the common case of tabbing through the
* controls in order and setting a non-default port before
* getting to the protocol; we want that non-default port
* to be preserved.
*/
int port = conf_get_int(conf, CONF_port);
if (port == cvt->default_port)
conf_set_int(conf, CONF_port, nvt->default_port);
dlg_refresh(hp->host, dlg);
dlg_refresh(hp->port, dlg);
}
}
}
static void loggingbuttons_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
int button;
Conf *conf = (Conf *)data;
/* This function works just like the standard radio-button handler,
* but it has to fall back to "no logging" in situations where the
* configured logging type isn't applicable.
*/
if (event == EVENT_REFRESH) {
int logtype = conf_get_int(conf, CONF_logtype);
for (button = 0; button < ctrl->radio.nbuttons; button++)
if (logtype == ctrl->radio.buttondata[button].i)
break;
/* We fell off the end, so we lack the configured logging type */
if (button == ctrl->radio.nbuttons) {
button = 0;
conf_set_int(conf, CONF_logtype, LGTYP_NONE);
}
dlg_radiobutton_set(ctrl, dlg, button);
} else if (event == EVENT_VALCHANGE) {
button = dlg_radiobutton_get(ctrl, dlg);
assert(button >= 0 && button < ctrl->radio.nbuttons);
conf_set_int(conf, CONF_logtype, ctrl->radio.buttondata[button].i);
}
}
static void numeric_keypad_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
int button;
Conf *conf = (Conf *)data;
/*
* This function works much like the standard radio button
* handler, but it has to handle two fields in Conf.
*/
if (event == EVENT_REFRESH) {
if (conf_get_bool(conf, CONF_nethack_keypad))
button = 2;
else if (conf_get_bool(conf, CONF_app_keypad))
button = 1;
else
button = 0;
assert(button < ctrl->radio.nbuttons);
dlg_radiobutton_set(ctrl, dlg, button);
} else if (event == EVENT_VALCHANGE) {
button = dlg_radiobutton_get(ctrl, dlg);
assert(button >= 0 && button < ctrl->radio.nbuttons);
if (button == 2) {
conf_set_bool(conf, CONF_app_keypad, false);
conf_set_bool(conf, CONF_nethack_keypad, true);
} else {
conf_set_bool(conf, CONF_app_keypad, (button != 0));
conf_set_bool(conf, CONF_nethack_keypad, false);
}
}
}
static void cipherlist_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
Conf *conf = (Conf *)data;
if (event == EVENT_REFRESH) {
int i;
static const struct { const char *s; int c; } ciphers[] = {
{ "ChaCha20 (SSH-2 only)", CIPHER_CHACHA20 },
{ "AES-GCM (SSH-2 only)", CIPHER_AESGCM },
{ "3DES", CIPHER_3DES },
{ "Blowfish", CIPHER_BLOWFISH },
{ "DES", CIPHER_DES },
{ "AES (SSH-2 only)", CIPHER_AES },
{ "Arcfour (SSH-2 only)", CIPHER_ARCFOUR },
{ "-- warn below here --", CIPHER_WARN }
};
/* Set up the "selected ciphers" box. */
/* (cipherlist assumed to contain all ciphers) */
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
for (i = 0; i < CIPHER_MAX; i++) {
int c = conf_get_int_int(conf, CONF_ssh_cipherlist, i);
int j;
const char *cstr = NULL;
for (j = 0; j < (sizeof ciphers) / (sizeof ciphers[0]); j++) {
if (ciphers[j].c == c) {
cstr = ciphers[j].s;
break;
}
}
dlg_listbox_addwithid(ctrl, dlg, cstr, c);
}
dlg_update_done(ctrl, dlg);
} else if (event == EVENT_VALCHANGE) {
int i;
/* Update array to match the list box. */
for (i=0; i < CIPHER_MAX; i++)
conf_set_int_int(conf, CONF_ssh_cipherlist, i,
dlg_listbox_getid(ctrl, dlg, i));
}
}
#ifndef NO_GSSAPI
static void gsslist_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
Conf *conf = (Conf *)data;
if (event == EVENT_REFRESH) {
int i;
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
for (i = 0; i < ngsslibs; i++) {
int id = conf_get_int_int(conf, CONF_ssh_gsslist, i);
assert(id >= 0 && id < ngsslibs);
dlg_listbox_addwithid(ctrl, dlg, gsslibnames[id], id);
}
dlg_update_done(ctrl, dlg);
} else if (event == EVENT_VALCHANGE) {
int i;
/* Update array to match the list box. */
for (i=0; i < ngsslibs; i++)
conf_set_int_int(conf, CONF_ssh_gsslist, i,
dlg_listbox_getid(ctrl, dlg, i));
}
}
#endif
static void kexlist_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
Conf *conf = (Conf *)data;
if (event == EVENT_REFRESH) {
int i;
static const struct { const char *s; int k; } kexes[] = {
{ "Diffie-Hellman group 1 (1024-bit)", KEX_DHGROUP1 },
{ "Diffie-Hellman group 14 (2048-bit)", KEX_DHGROUP14 },
{ "Diffie-Hellman group 15 (3072-bit)", KEX_DHGROUP15 },
{ "Diffie-Hellman group 16 (4096-bit)", KEX_DHGROUP16 },
{ "Diffie-Hellman group 17 (6144-bit)", KEX_DHGROUP17 },
{ "Diffie-Hellman group 18 (8192-bit)", KEX_DHGROUP18 },
{ "Diffie-Hellman group exchange", KEX_DHGEX },
{ "RSA-based key exchange", KEX_RSA },
{ "ECDH key exchange", KEX_ECDH },
{ "NTRU Prime / Curve25519 hybrid kex", KEX_NTRU_HYBRID },
{ "-- warn below here --", KEX_WARN }
};
/* Set up the "kex preference" box. */
/* (kexlist assumed to contain all algorithms) */
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
for (i = 0; i < KEX_MAX; i++) {
int k = conf_get_int_int(conf, CONF_ssh_kexlist, i);
int j;
const char *kstr = NULL;
for (j = 0; j < (sizeof kexes) / (sizeof kexes[0]); j++) {
if (kexes[j].k == k) {
kstr = kexes[j].s;
break;
}
}
dlg_listbox_addwithid(ctrl, dlg, kstr, k);
}
dlg_update_done(ctrl, dlg);
} else if (event == EVENT_VALCHANGE) {
int i;
/* Update array to match the list box. */
for (i=0; i < KEX_MAX; i++)
conf_set_int_int(conf, CONF_ssh_kexlist, i,
dlg_listbox_getid(ctrl, dlg, i));
}
}
static void hklist_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
Conf *conf = (Conf *)data;
if (event == EVENT_REFRESH) {
int i;
static const struct { const char *s; int k; } hks[] = {
{ "Ed25519", HK_ED25519 },
{ "Ed448", HK_ED448 },
{ "ECDSA", HK_ECDSA },
{ "DSA", HK_DSA },
{ "RSA", HK_RSA },
{ "-- warn below here --", HK_WARN }
};
/* Set up the "host key preference" box. */
/* (hklist assumed to contain all algorithms) */
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
for (i = 0; i < HK_MAX; i++) {
int k = conf_get_int_int(conf, CONF_ssh_hklist, i);
int j;
const char *kstr = NULL;
for (j = 0; j < lenof(hks); j++) {
if (hks[j].k == k) {
kstr = hks[j].s;
break;
}
}
dlg_listbox_addwithid(ctrl, dlg, kstr, k);
}
dlg_update_done(ctrl, dlg);
} else if (event == EVENT_VALCHANGE) {
int i;
/* Update array to match the list box. */
for (i=0; i < HK_MAX; i++)
conf_set_int_int(conf, CONF_ssh_hklist, i,
dlg_listbox_getid(ctrl, dlg, i));
}
}
static void printerbox_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
Conf *conf = (Conf *)data;
if (event == EVENT_REFRESH) {
int nprinters, i;
printer_enum *pe;
const char *printer;
dlg_update_start(ctrl, dlg);
/*
* Some backends may wish to disable the drop-down list on
* this edit box. Be prepared for this.
*/
if (ctrl->editbox.has_list) {
dlg_listbox_clear(ctrl, dlg);
dlg_listbox_add(ctrl, dlg, PRINTER_DISABLED_STRING);
pe = printer_start_enum(&nprinters);
for (i = 0; i < nprinters; i++)
dlg_listbox_add(ctrl, dlg, printer_get_name(pe, i));
printer_finish_enum(pe);
}
printer = conf_get_str(conf, CONF_printer);
if (!printer)
printer = PRINTER_DISABLED_STRING;
dlg_editbox_set(ctrl, dlg, printer);
dlg_update_done(ctrl, dlg);
} else if (event == EVENT_VALCHANGE) {
char *printer = dlg_editbox_get(ctrl, dlg);
if (!strcmp(printer, PRINTER_DISABLED_STRING))
printer[0] = '\0';
conf_set_str(conf, CONF_printer, printer);
sfree(printer);
}
}
static void codepage_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
Conf *conf = (Conf *)data;
if (event == EVENT_REFRESH) {
int i;
const char *cp, *thiscp;
dlg_update_start(ctrl, dlg);
thiscp = conf_get_str(conf, CONF_line_codepage);
if (decode_codepage (thiscp) != CP_UTF8 || iso2022_init_test (thiscp))
thiscp = cp_name(decode_codepage(thiscp));
dlg_listbox_clear(ctrl, dlg);
for (i = 0; (cp = cp_enumerate(i)) != NULL; i++)
dlg_listbox_add(ctrl, dlg, cp);
dlg_editbox_set(ctrl, dlg, thiscp);
conf_set_str(conf, CONF_line_codepage, thiscp);
dlg_update_done(ctrl, dlg);
} else if (event == EVENT_VALCHANGE) {
char *codepage = dlg_editbox_get(ctrl, dlg);
conf_set_str(conf, CONF_line_codepage, codepage);
if (decode_codepage (codepage) != CP_UTF8 || iso2022_init_test (codepage))
conf_set_str(conf, CONF_line_codepage,
cp_name(decode_codepage(codepage)));
sfree(codepage);
}
}
static void sshbug_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
Conf *conf = (Conf *)data;
if (event == EVENT_REFRESH) {
/*
* We must fetch the previously configured value from the Conf
* before we start modifying the drop-down list, otherwise the
* spurious SELCHANGE we trigger in the process will overwrite
* the value we wanted to keep.
*/
int oldconf = conf_get_int(conf, ctrl->context.i);
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
dlg_listbox_addwithid(ctrl, dlg, "Auto", AUTO);
dlg_listbox_addwithid(ctrl, dlg, "Off", FORCE_OFF);
dlg_listbox_addwithid(ctrl, dlg, "On", FORCE_ON);
switch (oldconf) {
case AUTO: dlg_listbox_select(ctrl, dlg, 0); break;
case FORCE_OFF: dlg_listbox_select(ctrl, dlg, 1); break;
case FORCE_ON: dlg_listbox_select(ctrl, dlg, 2); break;
}
dlg_update_done(ctrl, dlg);
} else if (event == EVENT_SELCHANGE) {
int i = dlg_listbox_index(ctrl, dlg);
if (i < 0)
i = AUTO;
else
i = dlg_listbox_getid(ctrl, dlg, i);
conf_set_int(conf, ctrl->context.i, i);
}
}
static void sshbug_handler_manual_only(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
/*
* This is just like sshbug_handler, except that there's no 'Auto'
* option. Used for bug workaround flags that can't be
* autodetected, and have to be manually enabled if they're to be
* used at all.
*/
Conf *conf = (Conf *)data;
if (event == EVENT_REFRESH) {
int oldconf = conf_get_int(conf, ctrl->context.i);
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
dlg_listbox_addwithid(ctrl, dlg, "Off", FORCE_OFF);
dlg_listbox_addwithid(ctrl, dlg, "On", FORCE_ON);
switch (oldconf) {
case FORCE_OFF: dlg_listbox_select(ctrl, dlg, 0); break;
case FORCE_ON: dlg_listbox_select(ctrl, dlg, 1); break;
}
dlg_update_done(ctrl, dlg);
} else if (event == EVENT_SELCHANGE) {
int i = dlg_listbox_index(ctrl, dlg);
if (i < 0)
i = FORCE_OFF;
else
i = dlg_listbox_getid(ctrl, dlg, i);
conf_set_int(conf, ctrl->context.i, i);
}
}
struct sessionsaver_data {
dlgcontrol *editbox, *listbox, *loadbutton, *savebutton, *delbutton;
dlgcontrol *okbutton, *cancelbutton;
struct sesslist sesslist;
bool midsession;
char *savedsession; /* the current contents of ssd->editbox */
};
static void sessionsaver_data_free(void *ssdv)
{
struct sessionsaver_data *ssd = (struct sessionsaver_data *)ssdv;
get_sesslist(&ssd->sesslist, false);
sfree(ssd->savedsession);
sfree(ssd);
}
/*
* Helper function to load the session selected in the list box, if
* any, as this is done in more than one place below. Returns 0 for
* failure.
*/
static bool load_selected_session(
struct sessionsaver_data *ssd,
dlgparam *dlg, Conf *conf, bool *maybe_launch)
{
int i = dlg_listbox_index(ssd->listbox, dlg);
bool isdef;
if (i < 0) {
dlg_beep(dlg);
return false;
}
isdef = !strcmp(ssd->sesslist.sessions[i], "Default Settings");
load_settings(ssd->sesslist.sessions[i], conf);
sfree(ssd->savedsession);
ssd->savedsession = dupstr(isdef ? "" : ssd->sesslist.sessions[i]);
if (maybe_launch)
*maybe_launch = !isdef;
dlg_refresh(NULL, dlg);
/* Restore the selection, which might have been clobbered by
* changing the value of the edit box. */
dlg_listbox_select(ssd->listbox, dlg, i);
return true;
}
static void sessionsaver_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
Conf *conf = (Conf *)data;
struct sessionsaver_data *ssd =
(struct sessionsaver_data *)ctrl->context.p;
if (event == EVENT_REFRESH) {
if (ctrl == ssd->editbox) {
dlg_editbox_set(ctrl, dlg, ssd->savedsession);
} else if (ctrl == ssd->listbox) {
int i;
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
for (i = 0; i < ssd->sesslist.nsessions; i++) {
/* hack: append dummy string so that session names won't be translated */
char *item_text = i ? dupcat(ssd->sesslist.sessions[i], " ", NULL) : dupstr(ssd->sesslist.sessions[i]);
dlg_listbox_add(ctrl, dlg, item_text);
sfree(item_text);
}
dlg_update_done(ctrl, dlg);
}
} else if (event == EVENT_VALCHANGE) {
int top, bottom, halfway, i;
if (ctrl == ssd->editbox) {
sfree(ssd->savedsession);
ssd->savedsession = dlg_editbox_get(ctrl, dlg);
top = ssd->sesslist.nsessions;
bottom = -1;
while (top-bottom > 1) {
halfway = (top+bottom)/2;
i = strcmp(ssd->savedsession, ssd->sesslist.sessions[halfway]);
if (i <= 0 ) {
top = halfway;
} else {
bottom = halfway;
}
}
if (top == ssd->sesslist.nsessions) {
top -= 1;
}
dlg_listbox_select(ssd->listbox, dlg, top);
}
} else if (event == EVENT_ACTION) {
bool mbl = false;
if (!ssd->midsession &&
(ctrl == ssd->listbox ||
(ssd->loadbutton && ctrl == ssd->loadbutton))) {
/*
* The user has double-clicked a session, or hit Load.
* We must load the selected session, and then
* terminate the configuration dialog _if_ there was a
* double-click on the list box _and_ that session
* contains a hostname.
*/
if (load_selected_session(ssd, dlg, conf, &mbl) &&
(mbl && ctrl == ssd->listbox && conf_launchable(conf))) {
dlg_end(dlg, 1); /* it's all over, and succeeded */
}
} else if (ctrl == ssd->savebutton) {
bool isdef = !strcmp(ssd->savedsession, "Default Settings");
if (!ssd->savedsession[0]) {
int i = dlg_listbox_index(ssd->listbox, dlg);
if (i < 0) {
dlg_beep(dlg);
return;
}
isdef = !strcmp(ssd->sesslist.sessions[i], "Default Settings");
sfree(ssd->savedsession);
ssd->savedsession = dupstr(isdef ? "" :
ssd->sesslist.sessions[i]);
}
{
char *errmsg = save_settings(ssd->savedsession, conf);
if (errmsg) {
dlg_error_msg(dlg, errmsg);
sfree(errmsg);
}
}
get_sesslist(&ssd->sesslist, false);
get_sesslist(&ssd->sesslist, true);
dlg_refresh(ssd->editbox, dlg);
dlg_refresh(ssd->listbox, dlg);
} else if (!ssd->midsession &&
ssd->delbutton && ctrl == ssd->delbutton) {
int i = dlg_listbox_index(ssd->listbox, dlg);
if (i <= 0) {
dlg_beep(dlg);
} else {
del_settings(ssd->sesslist.sessions[i]);
get_sesslist(&ssd->sesslist, false);
get_sesslist(&ssd->sesslist, true);
dlg_refresh(ssd->listbox, dlg);
}
} else if (ctrl == ssd->okbutton) {
if (ssd->midsession) {
/* In a mid-session Change Settings, Apply is always OK. */
dlg_end(dlg, 1);
return;
}
/*
* Annoying special case. If the `Open' button is
* pressed while no host name is currently set, _and_
* the session list previously had the focus, _and_
* there was a session selected in that which had a
* valid host name in it, then load it and go.
*/
if (dlg_last_focused(ctrl, dlg) == ssd->listbox &&
!conf_launchable(conf) && dlg_is_visible(ssd->listbox, dlg)) {
Conf *conf2 = conf_new();
bool mbl = false;
if (!load_selected_session(ssd, dlg, conf2, &mbl)) {
dlg_beep(dlg);
conf_free(conf2);
return;
}
/* If at this point we have a valid session, go! */
if (mbl && conf_launchable(conf2)) {
conf_copy_into(conf, conf2);
dlg_end(dlg, 1);
} else
dlg_beep(dlg);
conf_free(conf2);
return;
}
/*
* Otherwise, do the normal thing: if we have a valid
* session, get going.
*/
if (conf_launchable(conf)) {
dlg_end(dlg, 1);
} else
dlg_beep(dlg);
} else if (ctrl == ssd->cancelbutton) {
dlg_end(dlg, 0);
}
}
}
struct charclass_data {
dlgcontrol *listbox, *editbox, *button;
};
static void charclass_handler(dlgcontrol *ctrl, dlgparam *dlg,
void *data, int event)
{
Conf *conf = (Conf *)data;
struct charclass_data *ccd =
(struct charclass_data *)ctrl->context.p;
if (event == EVENT_REFRESH) {
if (ctrl == ccd->listbox) {
int i;
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
for (i = 0; i < 128; i++) {
char str[100];
sprintf(str, "%d\t(0x%02X)\t%c\t%d", i, i,
(i >= 0x21 && i != 0x7F) ? i : ' ',
conf_get_int_int(conf, CONF_wordness, i));
dlg_listbox_add(ctrl, dlg, str);
}
dlg_update_done(ctrl, dlg);
}
} else if (event == EVENT_ACTION) {
if (ctrl == ccd->button) {
char *str;
int i, n;
str = dlg_editbox_get(ccd->editbox, dlg);
n = atoi(str);
sfree(str);