-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
connect.cpp
1507 lines (1317 loc) · 49.7 KB
/
connect.cpp
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
/*=============================================================================
*
* ホストへの接続/切断
*
===============================================================================
/ Copyright (C) 1997-2007 Sota. All rights reserved.
/
/ Redistribution and use in source and binary forms, with or without
/ modification, are permitted provided that the following conditions
/ are met:
/
/ 1. Redistributions of source code must retain the above copyright
/ notice, this list of conditions and the following disclaimer.
/ 2. Redistributions in binary form must reproduce the above copyright
/ notice, this list of conditions and the following disclaimer in the
/ documentation and/or other materials provided with the distribution.
/
/ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
/ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
/ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
/ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
/ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
/ USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
/ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
/ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
/ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/============================================================================*/
#include "common.h"
/*===== プロトタイプ =====*/
static int SendInitCommand(std::shared_ptr<SocketContext> Socket, std::wstring_view Cmd, int *CancelCheckWork);
static void AskUseFireWall(std::wstring_view Host, int *Fire, int *Pasv, int *List);
static void SaveCurrentSetToHistory(void);
static int ReConnectSkt(std::shared_ptr<SocketContext>& Skt);
static std::shared_ptr<SocketContext> DoConnect(HOSTDATA* HostData, std::wstring const& Host, std::wstring& User, std::wstring& Pass, std::wstring& Acct, int Port, int Fwall, int SavePass, int Security, int *CancelCheckWork);
static std::wstring CheckOneTimePassword(std::wstring&& pass, std::wstring const& reply, int Type);
/*===== 外部参照 =====*/
extern std::wstring TitleHostName;
static int Anonymous;
static int TryConnect = NO;
static std::shared_ptr<SocketContext> CmdCtrlSocket;
static std::shared_ptr<SocketContext> TrnCtrlSocket;
static HOSTDATA CurHost;
#if defined(HAVE_TANDEM)
static int Oss = NO; /* OSS ファイルシステムへアクセスしている場合は YES */
#endif
// 接続しているホストを返す
// TODO: GetConnectingHost()との違いがよくわからない
HOSTDATA const& GetCurHost() noexcept {
return CurHost;
}
// UNC文字列を分解する
// "\"は全て"/"に置き換える
static bool SplitUNCpath(std::wstring&& unc, std::wstring& Host, std::wstring& Path, std::wstring& File, std::wstring& User, std::wstring& Pass, int& Port) {
static boost::wregex re{ LR"(^(?:([^:@]*)(?::([^@]*))?@)?(?:\[([^\]]*)\]|([^:/]*))(?::([0-9]*))?(.*?)([^/]*)$)" };
unc = ReplaceAll(std::move(unc), L'\\', L'/');
if (auto const pos = unc.find(L"//"sv); pos != std::wstring::npos)
unc.erase(0, pos + 2);
if (boost::wsmatch m; boost::regex_search(unc, m, re)) {
User = m[1];
Pass = m[2].matched ? m[2] : L""s;
Host = m[m[3].matched ? 3 : 4];
Port = m[5].matched ? std::stoi(m[5]) : IPPORT_FTP;
Path = m[6];
File = m[7];
return true;
}
Host = Path = File = User = Pass = L""s;
Port = IPPORT_FTP;
return false;
}
/*----- ホスト一覧を使ってホストへ接続 ----------------------------------------
*
* Parameter
* int Type : ダイアログのタイプ (DLG_TYPE_xxx)
* int Num : 接続するホスト番号(0~, -1=ダイアログを出す)
* Return Value
* なし
*----------------------------------------------------------------------------*/
void ConnectProc(int Type, int Num)
{
SaveBookMark();
SaveCurrentSetToHost();
if((Num >= 0) || (SelectHost(Type) == YES))
{
if(Num >= 0)
SetCurrentHost(Num);
/* 接続中なら切断する */
if (CmdCtrlSocket)
DisconnectProc();
Notice(IDS_SEPARATOR);
InitPWDcommand();
CopyHostFromList(AskCurrentHost(), &CurHost);
// UTF-8対応
CurHost.CurNameKanjiCode = CurHost.NameKanjiCode;
if (ConnectRas(CurHost.Dialup != NO, CurHost.DialupAlways != NO, CurHost.DialupNotify != NO, CurHost.DialEntry)) {
SetHostKanaCnvImm(CurHost.KanaCnv);
SetHostKanjiCodeImm(CurHost.KanjiCode);
SetSyncMoveMode(CurHost.SyncMove);
if (AskSaveSortToHost() == YES && CurHost.Sort != HostSort::NotSaved()) {
SetSortTypeImm(CurHost.Sort);
ReSortDispList(WIN_LOCAL, &CancelFlg);
}
int const Save = empty(CurHost.PassWord) ? NO : YES;
DisableUserOpe();
CmdCtrlSocket = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, Save, CurHost.Security, &CancelFlg);
TrnCtrlSocket = CmdCtrlSocket;
if (CmdCtrlSocket) {
// 暗号化通信対応
switch(CurHost.CryptMode)
{
case CRYPT_NONE:
if(CurHost.UseFTPIS != NO || CurHost.UseSFTP != NO)
{
if(Dialog(GetFtpInst(), savecrypt_dlg, GetMainHwnd()))
SetHostEncryption(AskCurrentHost(), CurHost.UseNoEncryption, CurHost.UseFTPES, NO, NO);
}
break;
case CRYPT_FTPES:
if(CurHost.UseNoEncryption != NO || CurHost.UseFTPIS != NO || CurHost.UseSFTP != NO)
{
if(Dialog(GetFtpInst(), savecrypt_dlg, GetMainHwnd()))
SetHostEncryption(AskCurrentHost(), NO, CurHost.UseFTPES, NO, NO);
}
break;
case CRYPT_FTPIS:
if(CurHost.UseNoEncryption != NO || CurHost.UseFTPES != NO || CurHost.UseSFTP != NO)
{
if(Dialog(GetFtpInst(), savecrypt_dlg, GetMainHwnd()))
SetHostEncryption(AskCurrentHost(), NO, NO, CurHost.UseFTPIS, NO);
}
break;
case CRYPT_SFTP:
if(CurHost.UseNoEncryption != NO || CurHost.UseFTPES != NO || CurHost.UseFTPIS != NO)
{
if(Dialog(GetFtpInst(), savecrypt_dlg, GetMainHwnd()))
SetHostEncryption(AskCurrentHost(), NO, NO, NO, CurHost.UseSFTP);
}
break;
}
TitleHostName = CurHost.HostName;
DispWindowTitle();
UpdateStatusBar();
Sound::Connected.Play();
SendInitCommand(CmdCtrlSocket, CurHost.InitCmd, &CancelFlg);
if (!empty(CurHost.LocalInitDir)) {
DoLocalCWD(CurHost.LocalInitDir);
GetLocalDirForWnd();
}
InitTransCurDir();
DoCWD(CurHost.RemoteInitDir, YES, YES, YES);
LoadBookMark();
GetRemoteDirForWnd(CACHE_NORMAL, &CancelFlg);
}
else
Sound::Error.Play();
EnableUserOpe();
}
else
Notice(IDS_MSGJPN001);
}
return;
}
// ホスト名を入力してホストへ接続
void QuickConnectProc() {
struct QuickCon {
using result_t = bool;
std::wstring hostname;
std::wstring username;
std::wstring password;
bool firewall = false;
bool passive = false;
INT_PTR OnInit(HWND hDlg) noexcept {
SendDlgItemMessageW(hDlg, QHOST_HOST, CB_LIMITTEXT, FMAX_PATH, 0);
SetText(hDlg, QHOST_HOST, L"");
SendDlgItemMessageW(hDlg, QHOST_USER, EM_LIMITTEXT, USER_NAME_LEN, 0);
if (QuickAnonymous == YES) {
SetText(hDlg, QHOST_USER, L"anonymous");
SetText(hDlg, QHOST_PASS, UserMailAdrs);
} else {
SetText(hDlg, QHOST_USER, L"");
SetText(hDlg, QHOST_PASS, L"");
}
SendDlgItemMessageW(hDlg, QHOST_PASS, EM_LIMITTEXT, PASSWORD_LEN, 0);
SendDlgItemMessageW(hDlg, QHOST_FWALL, BM_SETCHECK, FwallDefault, 0);
SendDlgItemMessageW(hDlg, QHOST_PASV, BM_SETCHECK, PasvDefault, 0);
for (auto const& Tmp : GetHistories())
SendDlgItemMessageW(hDlg, QHOST_HOST, CB_ADDSTRING, 0, (LPARAM)Tmp.HostAdrs.c_str());
return TRUE;
}
void OnCommand(HWND hDlg, WORD id) {
switch (id) {
case IDOK:
hostname = GetText(hDlg, QHOST_HOST);
username = GetText(hDlg, QHOST_USER);
password = GetText(hDlg, QHOST_PASS);
firewall = SendDlgItemMessageW(hDlg, QHOST_FWALL, BM_GETCHECK, 0, 0) == BST_CHECKED;
passive = SendDlgItemMessageW(hDlg, QHOST_PASV, BM_GETCHECK, 0, 0) == BST_CHECKED;
EndDialog(hDlg, true);
break;
case IDCANCEL:
EndDialog(hDlg, false);
break;
}
}
};
SaveBookMark();
SaveCurrentSetToHost();
if (QuickCon qc; Dialog(GetFtpInst(), hostname_dlg, GetMainHwnd(), qc)) {
/* 接続中なら切断する */
if (CmdCtrlSocket)
DisconnectProc();
Notice(IDS_SEPARATOR);
InitPWDcommand();
CopyDefaultHost(&CurHost);
// UTF-8対応
CurHost.CurNameKanjiCode = CurHost.NameKanjiCode;
std::wstring File;
if (SplitUNCpath(std::move(qc.hostname), CurHost.HostAdrs, CurHost.RemoteInitDir, File, CurHost.UserName, CurHost.PassWord, CurHost.Port)) {
if (empty(CurHost.UserName)) {
CurHost.UserName = qc.username;
CurHost.PassWord = qc.password;
}
SetCurrentHost(HOSTNUM_NOENTRY);
AskUseFireWall(CurHost.HostAdrs, &CurHost.FireWall, &CurHost.Pasv, &CurHost.ListCmdOnly);
CurHost.FireWall = qc.firewall ? 1 : 0;
CurHost.Pasv = qc.passive ? 1 : 0;
SetHostKanaCnvImm(CurHost.KanaCnv);
SetHostKanjiCodeImm(CurHost.KanjiCode);
SetSyncMoveMode(CurHost.SyncMove);
DisableUserOpe();
CmdCtrlSocket = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security, &CancelFlg);
TrnCtrlSocket = CmdCtrlSocket;
if (CmdCtrlSocket) {
TitleHostName = CurHost.HostAdrs;
DispWindowTitle();
UpdateStatusBar();
Sound::Connected.Play();
InitTransCurDir();
DoCWD(CurHost.RemoteInitDir, YES, YES, YES);
GetRemoteDirForWnd(CACHE_NORMAL, &CancelFlg);
EnableUserOpe();
if (!empty(File))
DirectDownloadProc(File);
} else {
Sound::Error.Play();
EnableUserOpe();
}
}
}
}
/*----- 指定したホスト名でホストへ接続 ----------------------------------------
*
* Parameter
* char *unc : UNC文字列
* int Kanji : ホストの漢字コード (KANJI_xxx)
* int Kana : 半角かな→全角変換モード (YES/NO)
* int Fkanji : ファイル名の漢字コード (KANJI_xxx)
* int TrMode : 転送モード (TYPE_xx)
*
* Return Value
* なし
*----------------------------------------------------------------------------*/
void DirectConnectProc(std::wstring&& unc, int Kanji, int Kana, int Fkanji, int TrMode)
{
std::wstring Host;
std::wstring Path;
std::wstring File;
std::wstring User;
std::wstring Pass;
int Port;
SaveBookMark();
SaveCurrentSetToHost();
/* 接続中なら切断する */
if (CmdCtrlSocket)
DisconnectProc();
Notice(IDS_SEPARATOR);
InitPWDcommand();
if (SplitUNCpath(std::move(unc), Host, Path, File, User, Pass, Port)) {
if (empty(User)) {
User = L"anonymous"s;
Pass = UserMailAdrs;
}
CopyDefaultHost(&CurHost);
SetCurrentHost(HOSTNUM_NOENTRY);
CurHost.HostAdrs = std::move(Host);
CurHost.UserName = std::move(User);
CurHost.PassWord = std::move(Pass);
CurHost.RemoteInitDir = std::move(Path);
AskUseFireWall(CurHost.HostAdrs, &CurHost.FireWall, &CurHost.Pasv, &CurHost.ListCmdOnly);
CurHost.Port = Port;
CurHost.KanjiCode = Kanji;
CurHost.KanaCnv = Kana;
CurHost.NameKanjiCode = Fkanji;
CurHost.KanaCnv = YES; /* とりあえず */
// UTF-8対応
CurHost.CurNameKanjiCode = CurHost.NameKanjiCode;
SetHostKanaCnvImm(CurHost.KanaCnv);
SetHostKanjiCodeImm(CurHost.KanjiCode);
SetSyncMoveMode(CurHost.SyncMove);
if(TrMode != TYPE_DEFAULT)
{
SetTransferTypeImm(TrMode);
DispTransferType();
}
DisableUserOpe();
CmdCtrlSocket = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security, &CancelFlg);
TrnCtrlSocket = CmdCtrlSocket;
if (CmdCtrlSocket) {
TitleHostName = CurHost.HostAdrs;
DispWindowTitle();
UpdateStatusBar();
Sound::Connected.Play();
InitTransCurDir();
DoCWD(CurHost.RemoteInitDir, YES, YES, YES);
GetRemoteDirForWnd(CACHE_NORMAL, &CancelFlg);
EnableUserOpe();
if (!empty(File))
DirectDownloadProc(File);
else
ResetAutoExitFlg();
}
else
{
Sound::Error.Play();
EnableUserOpe();
}
}
return;
}
/*----- ホストのヒストリで指定されたホストへ接続 ------------------------------
*
* Parameter
* int MenuCmd : 取り出すヒストリに割り当てられたメニューコマンド
*
* Return Value
* なし
*----------------------------------------------------------------------------*/
void HistoryConnectProc(int MenuCmd)
{
if (auto history = GetHistoryByCmd(MenuCmd)) {
SaveBookMark();
SaveCurrentSetToHost();
/* 接続中なら切断する */
if (CmdCtrlSocket)
DisconnectProc();
Notice(IDS_SEPARATOR);
InitPWDcommand();
CurHost = *history;
// UTF-8対応
CurHost.CurNameKanjiCode = CurHost.NameKanjiCode;
if (ConnectRas(CurHost.Dialup != NO, CurHost.DialupAlways != NO, CurHost.DialupNotify != NO, CurHost.DialEntry)) {
SetCurrentHost(HOSTNUM_NOENTRY);
SetHostKanaCnvImm(CurHost.KanaCnv);
SetHostKanjiCodeImm(CurHost.KanjiCode);
SetSyncMoveMode(CurHost.SyncMove);
SetSortTypeImm(CurHost.Sort);
ReSortDispList(WIN_LOCAL, &CancelFlg);
SetTransferTypeImm(history->Type);
DispTransferType();
DisableUserOpe();
CmdCtrlSocket = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security, &CancelFlg);
TrnCtrlSocket = CmdCtrlSocket;
if (CmdCtrlSocket) {
TitleHostName = CurHost.HostAdrs;
DispWindowTitle();
UpdateStatusBar();
Sound::Connected.Play();
SendInitCommand(CmdCtrlSocket, CurHost.InitCmd, &CancelFlg);
DoLocalCWD(CurHost.LocalInitDir);
GetLocalDirForWnd();
InitTransCurDir();
DoCWD(CurHost.RemoteInitDir, YES, YES, YES);
GetRemoteDirForWnd(CACHE_NORMAL, &CancelFlg);
}
else
Sound::Error.Play();
EnableUserOpe();
}
else
Notice(IDS_MSGJPN002);
}
else
Sound::Error.Play();
return;
}
// ホストの初期化コマンドを送る
// 初期化コマンドは以下のようなフォーマットであること
// cmd1\0
// cmd1\r\ncmd2\r\n\0
static int SendInitCommand(std::shared_ptr<SocketContext> Socket, std::wstring_view Cmd, int* CancelCheckWork) {
static boost::wregex re{ LR"([^\r\n]+)" };
for (boost::regex_iterator<std::wstring_view::const_iterator> it{ begin(Cmd), end(Cmd), re }, end; it != end; ++it)
DoQUOTE(Socket, sv((*it)[0]), CancelCheckWork);
return 0;
}
/*----- 指定のホストはFireWallを使う設定かどうかを返す ------------------------
*
* Parameter
* char *Hots : ホスト名
* int *Fire : FireWallを使うかどうかを返すワーク
* int *Pasv : PASVモードを返すワーク
* int *List : LISTコマンドのみ使用フラグ
*
* Return Value
* なし
*----------------------------------------------------------------------------*/
static void AskUseFireWall(std::wstring_view Host, int *Fire, int *Pasv, int *List)
{
int i;
HOSTDATA Tmp;
*Fire = FwallDefault;
*Pasv = PasvDefault;
// NLSTを送ってしまうバグ修正(ただしNLSTを使うべきホストへクイック接続できなくなる)
// *List = NO;
i = 0;
while(CopyHostFromList(i, &Tmp) == FFFTP_SUCCESS)
{
if (Host == Tmp.HostAdrs) {
*Fire = Tmp.FireWall;
*Pasv = Tmp.Pasv;
*List = Tmp.ListCmdOnly;
break;
}
i++;
}
return;
}
// 接続しているホストのホストタイプを返す
int AskHostType() {
#if defined(HAVE_TANDEM)
/* OSS ファイルシステムは UNIX ファイルシステムと同じでいいので AUTO を返す
ただし、Guardian ファイルシステムに戻ったときにおかしくならないように
CurHost.HostType 変数は更新しない */
if (CurHost.HostType == HTYPE_TANDEM && Oss == YES)
return HTYPE_AUTO;
#endif
return GetConnectingHost().HostType;
}
// 接続しているホストでフルパスでファイルアクセスしないかどうかを返す
int AskNoFullPathMode() noexcept {
if(CurHost.HostType == HTYPE_VMS)
return(YES);
else
return(CurHost.NoFullPath);
}
// 現在の設定をホストの設定にセットする
// カレントディレクトリ、ソート方法をホストの設定にセットする
void SaveCurrentSetToHost() {
if (TrnCtrlSocket && AskCurrentHost() != HOSTNUM_NOENTRY) {
if (GetConnectingHost().LastDir == YES)
SetHostDir(AskCurrentHost(), AskLocalCurDir().native(), AskRemoteCurDir());
SetHostSort(AskCurrentHost(), AskSortType());
}
}
/*----- 現在の設定をヒストリにセットする --------------------------------------
*
* Parameter
* なし
*
* Return Value
* なし
*----------------------------------------------------------------------------*/
static void SaveCurrentSetToHistory(void)
{
CurHost.LocalInitDir = AskLocalCurDir();
CurHost.RemoteInitDir = AskRemoteCurDir();
CurHost.Sort = AskSortType();
CurHost.KanjiCode = AskHostKanjiCode();
CurHost.KanaCnv = AskHostKanaCnv();
CurHost.SyncMove = AskSyncMoveMode();
AddHostToHistory(CurHost);
SetAllHistoryToMenu();
return;
}
// コマンドコントロールソケットの再接続
int ReConnectCmdSkt() {
int result;
if (AskShareProh() == YES && AskTransferNow() == YES) {
SktShareProh();
result = FFFTP_SUCCESS;
} else {
if (CmdCtrlSocket == TrnCtrlSocket)
TrnCtrlSocket.reset();
result = ReConnectSkt(CmdCtrlSocket);
}
if (!TrnCtrlSocket)
TrnCtrlSocket = CmdCtrlSocket;
return result;
}
// 転送コントロールソケットの再接続
int ReConnectTrnSkt(std::shared_ptr<SocketContext>& Skt, int *CancelCheckWork) {
// char Path[FMAX_PATH+1];
int Sts;
// 暗号化通信対応
HOSTDATA HostData;
Sts = FFFTP_FAIL;
Notice(IDS_MSGJPN003);
// DisableUserOpe();
HostData = CurHost;
if(HostData.CryptMode != CRYPT_NONE)
HostData.UseNoEncryption = NO;
if(HostData.CryptMode != CRYPT_FTPES)
HostData.UseFTPES = NO;
if(HostData.CryptMode != CRYPT_FTPIS)
HostData.UseFTPIS = NO;
if(HostData.CryptMode != CRYPT_SFTP)
HostData.UseSFTP = NO;
// UTF-8対応
HostData.CurNameKanjiCode = HostData.NameKanjiCode;
// 同時接続対応
HostData.NoDisplayUI = YES;
if (Skt = DoConnect(&HostData, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security, CancelCheckWork)) {
SendInitCommand(Skt, CurHost.InitCmd, CancelCheckWork);
Sts = FFFTP_SUCCESS;
}
else
Sound::Error.Play();
// EnableUserOpe();
return(Sts);
}
// 回線の再接続
static int ReConnectSkt(std::shared_ptr<SocketContext>& Skt) {
int Sts;
Sts = FFFTP_FAIL;
Notice(IDS_MSGJPN003);
DisableUserOpe();
if (Skt = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security, &CancelFlg)) {
SendInitCommand(Skt, CurHost.InitCmd, &CancelFlg);
DoCWD(AskRemoteCurDir(), YES, YES, YES);
Sts = FFFTP_SUCCESS;
}
else
Sound::Error.Play();
EnableUserOpe();
return(Sts);
}
// コマンドコントロールソケットを返す
std::shared_ptr<SocketContext> AskCmdCtrlSkt() noexcept {
return CmdCtrlSocket;
}
// 転送コントロールソケットを返す
std::shared_ptr<SocketContext> AskTrnCtrlSkt() noexcept {
return TrnCtrlSocket;
}
/*----- コマンド/転送コントロールソケットの共有を解除 ------------------------
*
* Parameter
* なし
*
* Return Value
* なし
*----------------------------------------------------------------------------*/
void SktShareProh(void)
{
if(CmdCtrlSocket == TrnCtrlSocket)
{
if(CurHost.ReuseCmdSkt == YES)
{
CurHost.ReuseCmdSkt = NO;
CmdCtrlSocket.reset();
ReConnectSkt(CmdCtrlSocket);
}
}
return;
}
// コマンド/転送コントロールソケットの共有が解除されているかチェック
// YES=共有解除/NO=共有
int AskShareProh() noexcept {
return CmdCtrlSocket == TrnCtrlSocket || !TrnCtrlSocket ? NO : YES;
}
/*----- ホストから切断 --------------------------------------------------------
*
* Parameter
* なし
*
* Return Value
* なし
*----------------------------------------------------------------------------*/
void DisconnectProc(void)
{
AbortAllTransfer();
if (CmdCtrlSocket && CmdCtrlSocket != TrnCtrlSocket) {
DoQUIT(CmdCtrlSocket, &CancelFlg);
CmdCtrlSocket.reset();
}
if (TrnCtrlSocket) {
DoQUIT(TrnCtrlSocket, &CancelFlg);
TrnCtrlSocket.reset();
SaveCurrentSetToHistory();
EraseRemoteDirForWnd();
Notice(IDS_MSGJPN004);
}
DispWindowTitle();
UpdateStatusBar();
MakeButtonsFocus();
ClearBookMark();
return;
}
void DisconnectSet() noexcept {
CmdCtrlSocket.reset();
TrnCtrlSocket.reset();
}
// ホストに接続中かどうかを返す
int AskConnecting() noexcept {
return TrnCtrlSocket ? YES : NO;
}
#if defined(HAVE_TANDEM)
/*----- OSS ファイルシステムにアクセスしているかどうかのフラグを変更する ------
*
* Parameter
* int ステータス (YES/NO)
*
* Return Value
* int ステータス (YES/NO)
*----------------------------------------------------------------------------*/
int SetOSS(int wkOss)
{
if(Oss != wkOss)
CurHost.InitCmd = wkOss == YES ? L"OSS"sv : L"GUARDIAN"sv;
Oss = wkOss;
return(Oss);
}
// OSS ファイルシステムにアクセスしているかどうかを返す
int AskOSS() noexcept {
return(Oss);
}
#endif /* HAVE_TANDEM */
/*----- ホストへ接続する ------------------------------------------------------
*
* Parameter
* char *Host : ホスト名
* char *User : ユーザ名
* char *Pass : パスワード
* char *Acct : アカウント
* int Port : ポート
* int Fwall : FireWallを使うかどうか (YES/NO)
* int SavePass : パスワードを再入力した時に保存するかどうか (YES/NO)
* int Security : セキュリティ (SECURITY_xxx, MDx)
*
* Return Value
* SOCKET ソケット
*
* Note
* ホスト名、ユーザ名、パスワードが指定されていなかったときは、接続に使用
* したものをコピーしてかえす
* char *Host : ホスト名
* char *User : ユーザ名
* char *Pass : パスワード
* char *Acct : アカウント
*
* FireWallは次のように動作する
* TYPE1 Connect fire → USER user(f) → PASS pass(f) → SITE host → USER user(h) → PASS pass(h) → ACCT acct
* TYPE2 Connect fire → USER user(f) → PASS pass(f) → USER user(h)@host → PASS pass(h) → ACCT acct
* TYPE3 Connect fire → USER user(h)@host → PASS pass(h) → ACCT acct
* TYPE4 Connect fire → OPEN host → USER user(h) → PASS pass(h) → ACCT acct
* TYPE5 SOCKS4
* none Connect host → USER user(h) → PASS pass(h) → ACCT acct
*----------------------------------------------------------------------------*/
// 暗号化通信対応
static std::shared_ptr<SocketContext> DoConnectCrypt(int CryptMode, HOSTDATA* HostData, std::wstring const& Host, std::wstring& User, std::wstring& Pass, std::wstring& Acct, int Port, int Fwall, int SavePass, int Security, int *CancelCheckWork)
{
int Sts;
int Flg;
int Anony;
std::shared_ptr<SocketContext> ContSock;
int Continue;
int ReInPass;
constexpr std::wstring_view SiteTbl[] = { L"SITE"sv, L"site"sv, L"OPEN"sv, L"open"sv };
struct linger LingerOpt;
struct tcp_keepalive KeepAlive;
DWORD dwTmp;
if(CryptMode == CRYPT_NONE || CryptMode == CRYPT_FTPES || CryptMode == CRYPT_FTPIS)
{
if(Fwall == YES)
Fwall = FwallType;
else
Fwall = FWALL_NONE;
TryConnect = YES;
// 暗号化通信対応
// CancelFlg = NO;
#if 0
// WSASetBlockingHook(BlkHookFnc);
#endif
auto [tempHost, tempPort] = FWALL_FU_FP_SITE <= Fwall && Fwall <= FWALL_OPEN || Fwall == FWALL_SIDEWINDER || Fwall == FWALL_FU_FP ? std::tuple{ FwallHost, FwallPort } : std::tuple{ Host, Port };
if (!empty(tempHost)) {
if (ContSock = connectsock(Host, std::move(tempHost), tempPort, CancelCheckWork)) {
// バッファを無効
#ifdef DISABLE_CONTROL_NETWORK_BUFFERS
constexpr int BufferSize = 0;
setsockopt(ContSock->handle, SOL_SOCKET, SO_SNDBUF, (char*)&BufferSize, sizeof(int));
setsockopt(ContSock->handle, SOL_SOCKET, SO_RCVBUF, (char*)&BufferSize, sizeof(int));
#endif
if(CryptMode == CRYPT_FTPIS)
{
if (ContSock->AttachSSL(CancelCheckWork)) {
while((Sts = std::get<0>(ContSock->ReadReply(CancelCheckWork)) / 100) == FTP_PRELIM)
;
}
else
Sts = FTP_ERROR;
}
else
{
while((Sts = std::get<0>(ContSock->ReadReply(CancelCheckWork)) / 100) == FTP_PRELIM)
;
}
if(Sts == FTP_COMPLETE)
{
Flg = 1;
if(setsockopt(ContSock->handle, SOL_SOCKET, SO_OOBINLINE, (LPSTR)&Flg, sizeof(Flg)) == SOCKET_ERROR)
WSAError(L"setsockopt(SOL_SOCKET, SO_OOBINLINE)"sv);
// データ転送用ソケットのTCP遅延転送が無効されているので念のため
if(setsockopt(ContSock->handle, IPPROTO_TCP, TCP_NODELAY, (LPSTR)&Flg, sizeof(Flg)) == SOCKET_ERROR)
WSAError(L"setsockopt(IPPROTO_TCP, TCP_NODELAY)"sv);
//#pragma aaa
Flg = 1;
if(setsockopt(ContSock->handle, SOL_SOCKET, SO_KEEPALIVE, (LPSTR)&Flg, sizeof(Flg)) == SOCKET_ERROR)
WSAError(L"setsockopt(SOL_SOCKET, SO_KEEPALIVE)"sv);
// 切断対策
if(TimeOut > 0)
{
KeepAlive.onoff = 1;
KeepAlive.keepalivetime = TimeOut * 1000;
KeepAlive.keepaliveinterval = 1000;
if(WSAIoctl(ContSock->handle, SIO_KEEPALIVE_VALS, &KeepAlive, sizeof(struct tcp_keepalive), NULL, 0, &dwTmp, NULL, NULL) == SOCKET_ERROR)
WSAError(L"WSAIoctl(SIO_KEEPALIVE_VALS)"sv);
}
LingerOpt.l_onoff = 1;
LingerOpt.l_linger = 90;
if(setsockopt(ContSock->handle, SOL_SOCKET, SO_LINGER, (LPSTR)&LingerOpt, sizeof(LingerOpt)) == SOCKET_ERROR)
WSAError(L"setsockopt(SOL_SOCKET, SO_LINGER)"sv);
///////
/*===== 認証を行なう =====*/
Sts = FTP_COMPLETE;
if((Fwall == FWALL_FU_FP_SITE) ||
(Fwall == FWALL_FU_FP_USER) ||
(Fwall == FWALL_FU_FP))
{
if (auto [code, text] = Command(ContSock, CancelCheckWork, L"USER {}"sv, FwallUser); (Sts = code / 100) == FTP_CONTINUE) {
auto const pass = CheckOneTimePassword(std::wstring{ FwallPass }, text, FwallSecurity);
Sts = std::get<0>(Command(ContSock, CancelCheckWork, L"PASS {}"sv, pass)) / 100;
}
}
else if(Fwall == FWALL_SIDEWINDER)
{
Sts = std::get<0>(Command(ContSock, CancelCheckWork, L"USER {}:{}{}{}"sv, FwallUser, FwallPass, (wchar_t)FwallDelimiter, Host)) / 100;
}
if((Sts != FTP_COMPLETE) && (Sts != FTP_CONTINUE))
{
Notice(IDS_MSGJPN006);
ContSock.reset();
}
else
{
if((Fwall == FWALL_FU_FP_SITE) || (Fwall == FWALL_OPEN))
{
int const index = (Fwall == FWALL_OPEN ? 2 : 0) | (FwallLower == YES ? 1 : 0);
auto const format = Port == IPPORT_FTP ? L"{} {}"sv : L"{} {} {}"sv;
Sts = std::get<0>(Command(ContSock, CancelCheckWork, format, SiteTbl[index], Host)) / 100;
}
if((Sts != FTP_COMPLETE) && (Sts != FTP_CONTINUE))
{
Notice(IDS_MSGJPN007, Host);
ContSock.reset();
}
else
{
Anony = NO;
if (empty(User) && HostData->NoDisplayUI == NO)
InputDialog(username_dlg, GetMainHwnd(), 0, User, USER_NAME_LEN + 1, &Anony);
if (!empty(User)) {
if(Anony == YES)
{
User = L"anonymous"s;
Pass = UserMailAdrs;
}
auto const user = Fwall != FWALL_FU_FP_USER && Fwall != FWALL_USER ? User : Port == IPPORT_FTP ? std::format(L"{}{}{}"sv, User, (wchar_t)FwallDelimiter, Host) : std::format(L"{}{}{} {}"sv, User, (wchar_t)FwallDelimiter, Host, Port);
// FTPES対応
if (CryptMode == CRYPT_FTPES) {
if ((Sts = std::get<0>(Command(ContSock, CancelCheckWork, L"AUTH TLS"sv))) != 234 && (Sts = std::get<0>(Command(ContSock, CancelCheckWork, L"AUTH SSL"sv))) != 234)
Sts = FTP_ERROR;
else if (!ContSock->AttachSSL(CancelCheckWork))
Sts = FTP_ERROR;
else if ((Sts = std::get<0>(Command(ContSock, CancelCheckWork, L"PBSZ 0"sv))) != 200)
Sts = FTP_ERROR;
else if ((Sts = std::get<0>(Command(ContSock, CancelCheckWork, L"PROT P"sv))) != 200)
Sts = FTP_ERROR;
}
// FTPIS対応
if (CryptMode == CRYPT_FTPIS) {
// "PBSZ 0"と"PROT P"は黙示的に設定されているはずだが念のため
if ((Sts = std::get<0>(Command(ContSock, CancelCheckWork, L"PBSZ 0"sv))) == 200)
Sts = std::get<0>(Command(ContSock, CancelCheckWork, L"PROT P"sv));
}
ReInPass = NO;
do
{
// FTPES対応
if(Sts == FTP_ERROR)
break;
Continue = NO;
if (auto [code, text] = Command(ContSock, CancelCheckWork, L"USER {}"sv, user); (Sts = code / 100) == FTP_CONTINUE)
{
if (empty(Pass) && HostData->NoDisplayUI == NO)
InputDialog(passwd_dlg, GetMainHwnd(), 0, Pass, PASSWORD_LEN + 1);
if (!empty(Pass)) {
auto pass = CheckOneTimePassword(std::wstring{ Pass }, text, Security);
/* パスワードがスペース1個の時はパスワードの実体なしとする */
if (pass == L" "sv)
pass = L""s;
Sts = std::get<0>(Command(ContSock, CancelCheckWork, L"PASS {}"sv, pass)) / 100;
if(Sts == FTP_ERROR)
{
if (HostData->NoDisplayUI == NO && InputDialog(re_passwd_dlg, GetMainHwnd(), 0, Pass, PASSWORD_LEN + 1))
Continue = YES;
else {
Pass.clear();
Debug(L"No password specified."sv);
}
ReInPass = YES;
}
else if(Sts == FTP_CONTINUE)
{
if (empty(Acct) && HostData->NoDisplayUI == NO)
InputDialog(account_dlg, GetMainHwnd(), 0, Acct, ACCOUNT_LEN + 1);
if (!empty(Acct))
Sts = std::get<0>(Command(ContSock, CancelCheckWork, L"ACCT {}", Acct)) / 100;
else
Debug(L"No account specified."sv);
}
}
else
{
Sts = FTP_ERROR;
Debug(L"No password specified."sv);
}
}
// FTPES対応
if(Continue == YES)
Sts = FTP_COMPLETE;
}
while(Continue == YES);
}
else
{
Sts = FTP_ERROR;
Debug(L"No user name specified."sv);