forked from cyd01/KiTTY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kitty_puttytray.c
1977 lines (1723 loc) · 54.6 KB
/
kitty_puttytray.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
// Saved sessions enumeration.
struct enumsettings {
HKEY key;
int i;
int fromFile;
HANDLE hFile;
};
// Random seed functions enumeration
enum { DEL, OPEN_R, OPEN_W };
// PUTTY Tray / PuTTY File - global storage type
static int storagetype = 0; // 0 = registry, 1 = file
// PUTTY Tray / PuTTY File - extra variables / structs for file
static char seedpath[2 * MAX_PATH + 10] = "\0";
static char sesspath[2 * MAX_PATH] = "\0";
static char sshkpath[2 * MAX_PATH] = "\0";
static char oldpath[2 * MAX_PATH] = "\0";
static char sessionsuffix[16] = "\0";
static char keysuffix[16] = "\0";
/* JK: structures for handling settings in memory as linked list */
struct setItem {
char* key;
char* value;
struct setItem* next;
};
struct setPack {
unsigned int fromFile;
void* handle;
char* fileBuf;
};
// Forward declarations for helper functions
void mungestr(const char *in, char *out);
void unmungestr(const char *in, char *out, int outlen);
static void registry_recursive_remove(HKEY key);
// Forward declarations for file functions
void *file_open_settings_w(const char *sessionname, char **errmsg);
void file_write_setting_s(void *handle, const char *key, const char *value);
void file_write_setting_i(void *handle, const char *key, int value);
void file_write_setting_filename(void *handle, const char *key, Filename value);
void file_write_setting_fontspec(void *handle, const char *key, FontSpec font);
void file_close_settings_w(void *handle);
void *file_open_settings_r(const char *sessionname);
char *file_read_setting_s(void *handle, const char *key, char *buffer, int buflen);
int file_read_setting_i(void *handle, const char *key, int defvalue);
int file_read_setting_filename(void *handle, const char *key, Filename *value);
int file_read_setting_fontspec(void *handle, const char *key, FontSpec *font);
void file_close_settings_r(void *handle);
void file_del_settings(const char *sessionname);
void *file_enum_settings_start();
char *file_enum_settings_next(void *handle, char *buffer, int buflen);
void file_enum_settings_finish(void *handle);
int file_verify_host_key(const char *hostname, int port, const char *keytype, const char *key);
void file_store_host_key(const char *hostname, int port, const char *keytype, const char *key);
// Forward declarations for registry functions
void *reg_open_settings_w(const char *sessionname, char **errmsg);
void reg_write_setting_s(void *handle, const char *key, const char *value);
void reg_write_setting_i(void *handle, const char *key, int value);
void reg_write_setting_filename(void *handle, const char *key, Filename value);
void reg_write_setting_fontspec(void *handle, const char *key, FontSpec font);
void reg_close_settings_w(void *handle);
void *reg_open_settings_r(const char *sessionname);
char *reg_read_setting_s(void *handle, const char *key, char *buffer, int buflen);
int reg_read_setting_i(void *handle, const char *key, int defvalue);
int reg_read_setting_filename(void *handle, const char *key, Filename *value);
int reg_read_setting_fontspec(void *handle, const char *key, FontSpec *font);
void reg_close_settings_r(void *handle);
void reg_del_settings(const char *sessionname);
void *reg_enum_settings_start();
char *reg_enum_settings_next(void *handle, char *buffer, int buflen);
void reg_enum_settings_finish(void *handle);
int reg_verify_host_key(const char *hostname, int port, const char *keytype, const char *key);
void reg_store_host_key(const char *hostname, int port, const char *keytype, const char *key);
/*
* Sets storage type
*/
void set_storagetype(int new_storagetype)
{
storagetype = new_storagetype;
}
/*
* Write a saved session. The caller is expected to call
* open_setting_w() to get a `void *' handle, then pass that to a
* number of calls to write_setting_s() and write_setting_i(), and
* then close it using close_settings_w(). At the end of this call
* sequence the settings should have been written to the PuTTY
* persistent storage area.
*
* A given key will be written at most once while saving a session.
* Keys may be up to 255 characters long. String values have no length
* limit.
*
* Any returned error message must be freed after use.
*
* STORAGETYPE SWITCHER
*/
void *open_settings_w(const char *sessionname, char **errmsg)
{
if (storagetype == 1) {
return file_open_settings_w(sessionname, errmsg);
} else {
return reg_open_settings_w(sessionname, errmsg);
}
}
/*
* STORAGETYPE SWITCHER
*/
void write_setting_s(void *handle, const char *key, const char *value)
{
if (storagetype == 1) {
file_write_setting_s(handle, key, value);
} else {
reg_write_setting_s(handle, key, value);
}
}
/*
* STORAGETYPE SWITCHER
*/
void write_setting_i(void *handle, const char *key, int value)
{
if (storagetype == 1) {
file_write_setting_i(handle, key, value);
} else {
reg_write_setting_i(handle, key, value);
}
}
/*
* STORAGETYPE SWITCHER
*/
void write_setting_filename(void *handle, const char *name, Filename result)
{
if (storagetype == 1) {
file_write_setting_filename(handle, name, result);
} else {
reg_write_setting_filename(handle, name, result);
}
}
/*
* STORAGETYPE SWITCHER
*/
void write_setting_fontspec(void *handle, const char *name, FontSpec font)
{
if (storagetype == 1) {
file_write_setting_fontspec(handle, name, font);
} else {
reg_write_setting_fontspec(handle, name, font);
}
}
/*
* STORAGETYPE SWITCHER
*/
void close_settings_w(void *handle)
{
if (storagetype == 1) {
file_close_settings_w(handle);
} else {
reg_close_settings_w(handle);
}
}
/*
* Read a saved session. The caller is expected to call
* open_setting_r() to get a `void *' handle, then pass that to a
* number of calls to read_setting_s() and read_setting_i(), and
* then close it using close_settings_r().
*
* read_setting_s() writes into the provided buffer and returns a
* pointer to the same buffer.
*
* If a particular string setting is not present in the session,
* read_setting_s() can return NULL, in which case the caller
* should invent a sensible default. If an integer setting is not
* present, read_setting_i() returns its provided default.
*
* read_setting_filename() and read_setting_fontspec() each read into
* the provided buffer, and return zero if they failed to.
*
* STORAGETYPE SWITCHER
*/
void *open_settings_r(const char *sessionname)
{
if (storagetype == 1) {
return file_open_settings_r(sessionname);
} else {
return reg_open_settings_r(sessionname);
}
}
/*
* STORAGETYPE SWITCHER
*/
char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
{
if (storagetype == 1) {
return file_read_setting_s(handle, key, buffer, buflen);
} else {
return reg_read_setting_s(handle, key, buffer, buflen);
}
}
/*
* STORAGETYPE SWITCHER
*/
int read_setting_i(void *handle, const char *key, int defvalue)
{
if (storagetype == 1) {
return file_read_setting_i(handle, key, defvalue);
} else {
return reg_read_setting_i(handle, key, defvalue);
}
}
/*
* STORAGETYPE SWITCHER
*/
int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
{
if (storagetype == 1) {
return file_read_setting_fontspec(handle, name, result);
} else {
return reg_read_setting_fontspec(handle, name, result);
}
}
/*
* STORAGETYPE SWITCHER
*/
int read_setting_filename(void *handle, const char *name, Filename *result)
{
if (storagetype == 1) {
return file_read_setting_filename(handle, name, result);
} else {
return reg_read_setting_filename(handle, name, result);
}
}
/*
* STORAGETYPE SWITCHER
*/
void close_settings_r(void *handle)
{
if (storagetype == 1) {
return file_close_settings_r(handle);
} else {
return reg_close_settings_r(handle);
}
}
/*
* Delete a whole saved session.
*
* STORAGETYPE SWITCHER
*/
void del_settings(const char *sessionname)
{
if (storagetype == 1) {
file_del_settings(sessionname);
} else {
reg_del_settings(sessionname);
}
}
/*
* STORAGETYPE SWITCHER
*/
void *enum_settings_start(int new_storagetype)
{
storagetype = new_storagetype;
if (storagetype == 1) {
return file_enum_settings_start();
} else {
return reg_enum_settings_start();
}
}
/*
* STORAGETYPE SWITCHER
*/
char *enum_settings_next(void *handle, char *buffer, int buflen)
{
if (storagetype == 1) {
return file_enum_settings_next(handle, buffer, buflen);
} else {
return reg_enum_settings_next(handle, buffer, buflen);
}
}
/*
* STORAGETYPE SWITCHER
*/
void enum_settings_finish(void *handle)
{
if (storagetype == 1) {
file_enum_settings_finish(handle);
} else {
reg_enum_settings_finish(handle);
}
}
/* ----------------------------------------------------------------------
* Functions to access PuTTY's host key database.
*/
/*
* Helper for hostkey functions (not part of storage.h)
* NO HACK: PuttyTray / PuTTY File - This is an original function (not patched)
*/
static void hostkey_regname(char *buffer, const char *hostname, int port, const char *keytype)
{
int len;
strcpy(buffer, keytype);
strcat(buffer, "@");
len = strlen(buffer);
len += sprintf(buffer + len, "%d:", port);
mungestr(hostname, buffer + strlen(buffer));
}
/*
* See if a host key matches the database entry. Return values can
* be 0 (entry matches database), 1 (entry is absent in database),
* or 2 (entry exists in database and is different).
*
* STORAGETYPE SWITCHER
*/
int verify_host_key(const char *hostname, int port, const char *keytype, const char *key)
{
if (storagetype == 1) {
return file_verify_host_key(hostname, port, keytype, key);
} else {
return reg_verify_host_key(hostname, port, keytype, key);
}
}
/*
* Write a host key into the database, overwriting any previous
* entry that might have been there.
*
* STORAGETYPE SWITCHER
*/
/*void store_host_key(const char *hostname, int port, const char *keytype, const char *key)
{
if (storagetype == 1) {
file_store_host_key(hostname, port, keytype, key);
} else {
reg_store_host_key(hostname, port, keytype, key);
}
}
*/
/* ----------------------------------------------------------------------
* Functions to access PuTTY's random number seed file.
*/
/*
* HELPER FOR RANDOM SEED FUNCTIONS (not part of storage.h)
* Open (or delete) the random seed file.
*
* NO HACK: PuttyTray / PuTTY File - This is an original function (not patched)
*/
static int try_random_seed(char const *path, int action, HANDLE *ret)
{
if (action == DEL) {
remove(path);
*ret = INVALID_HANDLE_VALUE;
return FALSE; /* so we'll do the next ones too */
}
*ret = CreateFile(path,
action == OPEN_W ? GENERIC_WRITE : GENERIC_READ,
action == OPEN_W ? 0 : (FILE_SHARE_READ |
FILE_SHARE_WRITE),
NULL,
action == OPEN_W ? CREATE_ALWAYS : OPEN_EXISTING,
action == OPEN_W ? FILE_ATTRIBUTE_NORMAL : 0,
NULL);
return (*ret != INVALID_HANDLE_VALUE);
}
/*
* HELPER FOR RANDOM SEED FUNCTIONS (not part of storage.h)
*
* PARTLY HACKED: PuttyTray / PuTTY File - This is an original function (only first lines patched)
*/
static HANDLE access_random_seed(int action)
{
HKEY rkey;
DWORD type, size;
HANDLE rethandle;
char seedpath[2 * MAX_PATH + 10] = "\0";
/* PuttyTray / PuTTY File - HACK STARTS HERE */
if (seedpath != '\0') {
/* JK: In PuTTY 0.58 this won't ever happen - this function was called only if (!seedpath[0])
* This changed in PuTTY 0.59 - read the long comment below
*/
return;
}
/* PuttyTray / PuTTY File - HACK ENDS HERE */
/*
* Iterate over a selection of possible random seed paths until
* we find one that works.
*
* We do this iteration separately for reading and writing,
* meaning that we will automatically migrate random seed files
* if a better location becomes available (by reading from the
* best location in which we actually find one, and then
* writing to the best location in which we can _create_ one).
*/
/*
* First, try the location specified by the user in the
* Registry, if any.
*/
size = sizeof(seedpath);
if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) ==
ERROR_SUCCESS) {
int ret = RegQueryValueEx(rkey, "RandSeedFile",
0, &type, seedpath, &size);
if (ret != ERROR_SUCCESS || type != REG_SZ)
seedpath[0] = '\0';
RegCloseKey(rkey);
if (*seedpath && try_random_seed(seedpath, action, &rethandle))
return rethandle;
}
/*
* Next, try the user's local Application Data directory,
* followed by their non-local one. This is found using the
* SHGetFolderPath function, which won't be present on all
* versions of Windows.
*/
if (!tried_shgetfolderpath) {
/* This is likely only to bear fruit on systems with IE5+
* installed, or WinMe/2K+. There is some faffing with
* SHFOLDER.DLL we could do to try to find an equivalent
* on older versions of Windows if we cared enough.
* However, the invocation below requires IE5+ anyway,
* so stuff that. */
shell32_module = LoadLibrary("SHELL32.DLL");
if (shell32_module) {
p_SHGetFolderPath = (p_SHGetFolderPath_t)
GetProcAddress(shell32_module, "SHGetFolderPathA");
}
}
if (p_SHGetFolderPath) {
if (SUCCEEDED(p_SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA,
NULL, SHGFP_TYPE_CURRENT, seedpath))) {
strcat(seedpath, "\\PUTTY.RND");
if (try_random_seed(seedpath, action, &rethandle))
return rethandle;
}
if (SUCCEEDED(p_SHGetFolderPath(NULL, CSIDL_APPDATA,
NULL, SHGFP_TYPE_CURRENT, seedpath))) {
strcat(seedpath, "\\PUTTY.RND");
if (try_random_seed(seedpath, action, &rethandle))
return rethandle;
}
}
/*
* Failing that, try %HOMEDRIVE%%HOMEPATH% as a guess at the
* user's home directory.
*/
{
int len, ret;
len =
GetEnvironmentVariable("HOMEDRIVE", seedpath,
sizeof(seedpath));
ret =
GetEnvironmentVariable("HOMEPATH", seedpath + len,
sizeof(seedpath) - len);
if (ret != 0) {
strcat(seedpath, "\\PUTTY.RND");
if (try_random_seed(seedpath, action, &rethandle))
return rethandle;
}
}
/*
* And finally, fall back to C:\WINDOWS.
*/
GetWindowsDirectory(seedpath, sizeof(seedpath));
strcat(seedpath, "\\PUTTY.RND");
if (try_random_seed(seedpath, action, &rethandle))
return rethandle;
/*
* If even that failed, give up.
*/
return INVALID_HANDLE_VALUE;
}
/*
* Read PuTTY's random seed file and pass its contents to a noise
* consumer function.
*
* NO HACK: PuttyTray / PuTTY File - This is an original function (not patched)
*/
void read_random_seed(noise_consumer_t consumer)
{
HANDLE seedf = access_random_seed(OPEN_R);
if (seedf != INVALID_HANDLE_VALUE) {
while (1) {
char buf[1024];
DWORD len;
if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len)
consumer(buf, len);
else
break;
}
CloseHandle(seedf);
}
}
/*
* Write PuTTY's random seed file from a given chunk of noise.
*
* NO HACK: PuttyTray / PuTTY File - This is an original function (not patched)
*/
void write_random_seed(void *data, int len)
{
HANDLE seedf = access_random_seed(OPEN_W);
if (seedf != INVALID_HANDLE_VALUE) {
DWORD lenwritten;
WriteFile(seedf, data, len, &lenwritten, NULL);
CloseHandle(seedf);
}
}
/* ----------------------------------------------------------------------
* Cleanup function: remove all of PuTTY's persistent state.
*
* NO HACK: PuttyTray / PuTTY File - This is an original function (not patched)
*/
void cleanup_all(void)
{
HKEY key;
int ret;
char name[MAX_PATH + 1];
/* ------------------------------------------------------------
* Wipe out the random seed file, in all of its possible
* locations.
*/
access_random_seed(DEL);
/* ------------------------------------------------------------
* Destroy all registry information associated with PuTTY.
*/
/*
* Open the main PuTTY registry key and remove everything in it.
*/
if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) ==
ERROR_SUCCESS) {
registry_recursive_remove(key);
RegCloseKey(key);
}
/*
* Now open the parent key and remove the PuTTY main key. Once
* we've done that, see if the parent key has any other
* children.
*/
if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_PARENT,
&key) == ERROR_SUCCESS) {
RegDeleteKey(key, PUTTY_REG_PARENT_CHILD);
ret = RegEnumKey(key, 0, name, sizeof(name));
RegCloseKey(key);
/*
* If the parent key had no other children, we must delete
* it in its turn. That means opening the _grandparent_
* key.
*/
if (ret != ERROR_SUCCESS) {
if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_GPARENT,
&key) == ERROR_SUCCESS) {
RegDeleteKey(key, PUTTY_REG_GPARENT_CHILD);
RegCloseKey(key);
}
}
}
/*
* Now we're done.
*/
}
/* ----------------------------------------------------------------------
* PUTTY FILE HELPERS (not part of storage.h)
*/
/* JK: my generic function for simplyfing error reporting */
DWORD errorShow(const char* pcErrText, const char* pcErrParam) {
HWND hwRodic;
DWORD erChyba;
char pcBuf[16];
char* pcHlaska = snewn(strlen(pcErrParam) + strlen(pcErrText) + 31, char);
erChyba = GetLastError();
ltoa(erChyba, pcBuf, 10);
strcpy(pcHlaska, "Error: ");
strcat(pcHlaska, pcErrText);
strcat(pcHlaska, "\n");
if (pcErrParam) {
strcat(pcHlaska, pcErrParam);
strcat(pcHlaska, "\n");
}
strcat(pcHlaska, "Error code: ");
strcat(pcHlaska, pcBuf);
/* JK: get parent-window and show */
hwRodic = GetActiveWindow();
if (hwRodic != NULL) { hwRodic = GetLastActivePopup(hwRodic);}
if (MessageBox(hwRodic, pcHlaska, "Error", MB_OK|MB_APPLMODAL|MB_ICONEXCLAMATION) == 0) {
/* JK: this is really bad -> just ignore */
return 0;
}
sfree(pcHlaska);
return erChyba;
};
/* JK: pack string for use as filename - pack < > : " / \ | */
static void packstr(const char *in, char *out) {
while (*in) {
if (*in == '<' || *in == '>' || *in == ':' || *in == '"' ||
*in == '/' || *in == '|') {
*out++ = '%';
*out++ = hex[((unsigned char) *in) >> 4];
*out++ = hex[((unsigned char) *in) & 15];
} else
*out++ = *in;
in++;
}
*out = '\0';
return;
}
/*
* JK: create directory if specified as dir1\dir2\dir3 and dir1|2 doesn't exists
* handle if part of path already exists
*/
int createPath(char* dir) {
char *p;
p = strrchr(dir, '\\');
if (p == NULL) {
/* what if it already exists */
if (!SetCurrentDirectory(dir)) {
CreateDirectory(dir, NULL);
return SetCurrentDirectory(dir);
}
return 1;
}
*p = '\0';
createPath(dir);
*p = '\\';
++p;
/* what if it already exists */
if (!SetCurrentDirectory(dir)) {
CreateDirectory(p, NULL);
return SetCurrentDirectory(p);
}
return 1;
}
/*
* JK: join path pcMain.pcSuf solving extra cases to pcDest
* expecting - pcMain as path from WinAPI ::GetCurrentDirectory()/GetModuleFileName()
* - pcSuf as user input path from config (at least MAX_PATH long)
*/
char* joinPath(char* pcDest, char* pcMain, char* pcSuf) {
char* pcBuf = snewn(MAX_PATH+1, char);
/* at first ExpandEnvironmentStrings */
if (0 == ExpandEnvironmentStrings(pcSuf, pcBuf, MAX_PATH)) {
/* JK: failure -> revert back - but it ussualy won't work, so report error to user! */
errorShow("Unable to ExpandEnvironmentStrings for session path", pcSuf);
strncpy(pcBuf, pcSuf, strlen(pcSuf));
}
/* now ExpandEnvironmentStringsForUser - only on win2000Pro and above */
/* It's much more tricky than I've expected, so it's ToDo */
/*
static HMODULE userenv_module = NULL;
typedef BOOL (WINAPI *p_ExpandESforUser_t) (HANDLE, LPCTSTR, LPTSTR, DWORD);
static p_ExpandESforUser_t p_ExpandESforUser = NULL;
HMODULE userenv_module = LoadLibrary("USERENV.DLL");
if (userenv_module) {
p_ExpandESforUser = (p_ExpandESforUser_t) GetProcAddress(shell32_module, "ExpandEnvironmentStringsForUserA");
if (p_ExpandESforUser) {
TOKEN_IMPERSONATE
if (0 == (p_ExpandESforUser(NULL, pcSuf, pcBuf, MAX_PATH))) {
/* JK: failure -> revert back - but it ussualy won't work, so report error to user! *//*
errorShow("Unable to ExpandEnvironmentStringsForUser for session path", pcBuf);
strncpy(pcSuf, pcBuf, strlen(pcSuf));
}
}
}*/
/* expand done, resutl in pcBuf */
if ((*pcBuf == '/') || (*pcBuf == '\\')) {
/* everything ok */
strcpy(pcDest, pcMain);
strcat(pcDest, pcBuf);
}
else {
if (*(pcBuf+1) == ':') {
/* absolute path */
strcpy(pcDest, pcBuf);
}
else {
/* some weird relative path - add '\' */
strcpy(pcDest, pcMain);
strcat(pcDest, "\\");
strcat(pcDest, pcBuf);
}
}
sfree(pcBuf);
return pcDest;
}
/*
* JK: init path variables from config or otherwise
* as of 1.5 GetModuleFileName solves our currentDirectory problem
*/
int loadPath() {
char *fileCont = NULL;
DWORD fileSize;
DWORD bytesRead;
char *p = NULL;
char *p2 = NULL;
HANDLE hFile;
char* puttypath = snewn( (MAX_PATH*2), char);
/* JK: save path/curdir */
GetCurrentDirectory( (MAX_PATH*2), oldpath);
/* JK: get where putty.exe is */
if (GetModuleFileName(NULL, puttypath, (MAX_PATH*2)) != 0)
{
p = strrchr(puttypath, '\\');
if (p)
{
*p = '\0';
}
SetCurrentDirectory(puttypath);
}
else GetCurrentDirectory( (MAX_PATH*2), puttypath);
/* JK: set default values - if there is a config file, it will be overwitten */
strcpy(sesspath, puttypath);
strcat(sesspath, "\\sessions");
strcpy(sshkpath, puttypath);
strcat(sshkpath, "\\sshhostkeys");
strcpy(seedpath, puttypath);
strcat(seedpath, "\\putty.rnd");
hFile = CreateFile("putty.conf",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
/* JK: now we can pre-clean-up */
SetCurrentDirectory(oldpath);
if (hFile != INVALID_HANDLE_VALUE) {
fileSize = GetFileSize(hFile, NULL);
fileCont = snewn(fileSize+16, char);
if (!ReadFile(hFile, fileCont, fileSize, &bytesRead, NULL)) {
errorShow("Unable to read configuration file, falling back to defaults", NULL);
/* JK: default values are already there and clean-up at end */
}
else {
/* JK: parse conf file to path variables */
*(fileCont+fileSize) = '\0';
p = fileCont;
while (p) {
if (*p == ';') { /* JK: comment -> skip line */
p = strchr(p, '\n');
++p;
continue;
}
p2 = strchr(p, '=');
if (!p2) break;
*p2 = '\0';
++p2;
if (!strcmp(p, "sessions")) {
p = strchr(p2, '\n');
*p = '\0';
joinPath(sesspath, puttypath, p2);
p2 = sesspath+strlen(sesspath)-1;
while ((*p2 == ' ')||(*p2 == '\n')||(*p2 == '\r')||(*p2 == '\t')) --p2;
*(p2+1) = '\0';
}
else if (!strcmp(p, "sshhostkeys")) {
p = strchr(p2, '\n');
*p = '\0';
joinPath(sshkpath, puttypath, p2);
p2 = sshkpath+strlen(sshkpath)-1;
while ((*p2 == ' ')||(*p2 == '\n')||(*p2 == '\r')||(*p2 == '\t')) --p2;
*(p2+1) = '\0';
}
else if (!strcmp(p, "seedfile")) {
p = strchr(p2, '\n');
*p = '\0';
joinPath(seedpath, puttypath, p2);
p2 = seedpath+strlen(seedpath)-1;
while ((*p2 == ' ')||(*p2 == '\n')||(*p2 == '\r')||(*p2 == '\t')) --p2;
*(p2+1) = '\0';
}
else if (!strcmp(p, "sessionsuffix")) {
p = strchr(p2, '\n');
*p = '\0';
strcpy(sessionsuffix, p2);
p2 = sessionsuffix+strlen(sessionsuffix)-1;
while ((*p2 == ' ')||(*p2 == '\n')||(*p2 == '\r')||(*p2 == '\t')) --p2;
*(p2+1) = '\0';
}
else if (!strcmp(p, "keysuffix")) {
p = strchr(p2, '\n');
*p = '\0';
strcpy(keysuffix, p2);
p2 = keysuffix+strlen(keysuffix)-1;
while ((*p2 == ' ')||(*p2 == '\n')||(*p2 == '\r')||(*p2 == '\t')) --p2;
*(p2+1) = '\0';
}
++p;
}
}
CloseHandle(hFile);
sfree(fileCont);
}
/* else - INVALID_HANDLE {
* JK: unable to read conf file - probably doesn't exists
* we won't create one, user wants putty light, just fall back to defaults
* and defaults are already there
}*/
sfree(puttypath);
return 1;
}
/* ----------------------------------------------------------------------
* OTHER HELPERS (not part of storage.h)
*
* NO HACK: PuttyTray / PuTTY File - these are original functions (not patched)
*/
void mungestr(const char *in, char *out)
{
int candot = 0;
while (*in) {
if (*in == ' ' || *in == '\\' || *in == '*' || *in == '?' ||
*in == '%' || *in < ' ' || *in > '~' || (*in == '.'
&& !candot)) {
*out++ = '%';
*out++ = hex[((unsigned char) *in) >> 4];
*out++ = hex[((unsigned char) *in) & 15];
} else
*out++ = *in;
in++;
candot = 1;
}
*out = '\0';
return;
}
void unmungestr(const char *in, char *out, int outlen)
{
while (*in) {
if (*in == '%' && in[1] && in[2]) {
int i, j;
i = in[1] - '0';
i -= (i > 9 ? 7 : 0);
j = in[2] - '0';
j -= (j > 9 ? 7 : 0);
*out++ = (i << 4) + j;
if (!--outlen)
return;
in += 3;
} else {
*out++ = *in++;
if (!--outlen)
return;
}
}
*out = '\0';
return;
}
/*
* Recursively delete a registry key and everything under it.
*/
static void registry_recursive_remove(HKEY key)
{
DWORD i;
char name[MAX_PATH + 1];
HKEY subkey;
i = 0;
while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) {
if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) {
registry_recursive_remove(subkey);
RegCloseKey(subkey);
}
RegDeleteKey(key, name);
}
}
/* ---------------------------------------------------------------------------------------------------------
* ---------------------------------------------------------------------------------------------------------
* FILE FUNCTIONS
* ---------------------------------------------------------------------------------------------------------
* -------------------------------------------------------------------------------------------------------*/
void *file_open_settings_w(const char *sessionname, char **errmsg)
{
char *p;
struct setPack* sp;
*errmsg = NULL;
if (!sessionname || !*sessionname) {
sessionname = "Default Settings";
}
/* JK: if sessionname contains [registry] -> cut it off */
/*if ( *(sessionname+strlen(sessionname)-1) == ']') {
p = strrchr(sessionname, '[');
*(p-1) = '\0';
}*/
p = snewn(3 * strlen(sessionname) + 1, char);
mungestr(sessionname, p);
sp = snew( struct setPack );
sp->fromFile = 0;
sp->handle = NULL;
/* JK: secure pack for filename */
sp->fileBuf = snewn(3 * strlen(p) + 1 + 16, char);
packstr(p, sp->fileBuf);
strcat(sp->fileBuf, sessionsuffix);
sfree(p);
return sp;
}
void file_write_setting_s(void *handle, const char *key, const char *value)
{
struct setItem *st;
if (handle) {
/* JK: counting max lenght of keys/values */
((struct setPack*) handle)->fromFile = max(((struct setPack*) handle)->fromFile, strlen(key)+1);