OSDN Git Service

Fix bugs of asyncronous socket control.
[ffftp/ffftp.git] / connect.c
1 /*=============================================================================\r
2 *\r
3 *                                                               ホストへの接続/切断\r
4 *\r
5 ===============================================================================\r
6 / Copyright (C) 1997-2007 Sota. All rights reserved.\r
7 /\r
8 / Redistribution and use in source and binary forms, with or without \r
9 / modification, are permitted provided that the following conditions \r
10 / are met:\r
11 /\r
12 /  1. Redistributions of source code must retain the above copyright \r
13 /     notice, this list of conditions and the following disclaimer.\r
14 /  2. Redistributions in binary form must reproduce the above copyright \r
15 /     notice, this list of conditions and the following disclaimer in the \r
16 /     documentation and/or other materials provided with the distribution.\r
17 /\r
18 / THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR \r
19 / IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES \r
20 / OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. \r
21 / IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, \r
22 / INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, \r
23 / BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
24 / USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
25 / ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
26 / (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF \r
27 / THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28 /============================================================================*/\r
29 \r
30 #define STRICT\r
31 #include <stdio.h>\r
32 #include <stddef.h>\r
33 #include <stdlib.h>\r
34 #include <stdarg.h>\r
35 #include <string.h>\r
36 #include <mbstring.h>\r
37 #include <time.h>\r
38 #include <winsock.h>\r
39 #include <windowsx.h>\r
40 #include <commctrl.h>\r
41 \r
42 #include "common.h"\r
43 #include "resource.h"\r
44 \r
45 #include <htmlhelp.h>\r
46 #include "helpid.h"\r
47 \r
48 // UTF-8対応\r
49 #undef __MBSWRAPPER_H__\r
50 #include "mbswrapper.h"\r
51 \r
52 \r
53 /*===== プロトタイプ =====*/\r
54 \r
55 static BOOL CALLBACK QuickConDialogCallBack(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam);\r
56 static int SendInitCommand(char *Cmd);\r
57 static void AskUseFireWall(char *Host, int *Fire, int *Pasv, int *List);\r
58 static void SaveCurrentSetToHistory(void);\r
59 static int ReConnectSkt(SOCKET *Skt);\r
60 // 暗号化通信対応\r
61 //static SOCKET DoConnect(char *Host, char *User, char *Pass, char *Acct, int Port, int Fwall, int SavePass, int Security);\r
62 static SOCKET DoConnectCrypt(int CryptMode, HOSTDATA* HostData, char *Host, char *User, char *Pass, char *Acct, int Port, int Fwall, int SavePass, int Security);\r
63 static SOCKET DoConnect(HOSTDATA* HostData, char *Host, char *User, char *Pass, char *Acct, int Port, int Fwall, int SavePass, int Security);\r
64 static int CheckOneTimePassword(char *Pass, char *Reply, int Type);\r
65 static BOOL CALLBACK BlkHookFnc(void);\r
66 static int Socks5MakeCmdPacket(SOCKS5REQUEST *Packet, char Cmd, int ValidIP, ulong IP, char *Host, ushort Port);\r
67 static int SocksSendCmd(SOCKET Socket, void *Data, int Size, int *CancelCheckWork);\r
68 static int Socks5GetCmdReply(SOCKET Socket, SOCKS5REPLY *Packet);\r
69 static int Socks4GetCmdReply(SOCKET Socket, SOCKS4REPLY *Packet);\r
70 static int Socks5SelMethod(SOCKET Socket, int *CancelCheckWork);\r
71 \r
72 /*===== 外部参照 =====*/\r
73 \r
74 extern char FilterStr[FILTER_EXT_LEN+1];\r
75 extern char TitleHostName[HOST_ADRS_LEN+1];\r
76 extern int CancelFlg;\r
77 \r
78 /* 設定値 */\r
79 extern char UserMailAdrs[USER_MAIL_LEN+1];\r
80 extern char FwallHost[HOST_ADRS_LEN+1];\r
81 extern char FwallUser[USER_NAME_LEN+1];\r
82 extern char FwallPass[PASSWORD_LEN+1];\r
83 extern int FwallPort;\r
84 extern int FwallType;\r
85 extern int FwallDefault;\r
86 extern int FwallSecurity;\r
87 extern int FwallResolv;\r
88 extern int FwallLower;\r
89 extern int FwallDelimiter;\r
90 extern int PasvDefault;\r
91 extern int QuickAnonymous;\r
92 \r
93 /*===== ローカルなワーク =====*/\r
94 \r
95 static int Anonymous;\r
96 static int TryConnect = NO;\r
97 static SOCKET CmdCtrlSocket = INVALID_SOCKET;\r
98 static SOCKET TrnCtrlSocket = INVALID_SOCKET;\r
99 static HOSTDATA CurHost;\r
100 \r
101 /* 接続中の接続先、SOCKSサーバのアドレス情報を保存しておく */\r
102 /* この情報はlistenソケットを取得する際に用いる */\r
103 static struct sockaddr_in SocksSockAddr;        /* SOCKSサーバのアドレス情報 */\r
104 static struct sockaddr_in CurSockAddr;          /* 接続先ホストのアドレス情報 */\r
105 \r
106 static int UseIPadrs;\r
107 static char DomainName[HOST_ADRS_LEN+1];\r
108 \r
109 \r
110 \r
111 \r
112 /*----- ホスト一覧を使ってホストへ接続 ----------------------------------------\r
113 *\r
114 *       Parameter\r
115 *               int Type : ダイアログのタイプ (DLG_TYPE_xxx)\r
116 *               int Num : 接続するホスト番号(0~, -1=ダイアログを出す)\r
117 \r
118 *       Return Value\r
119 *               なし\r
120 *----------------------------------------------------------------------------*/\r
121 \r
122 void ConnectProc(int Type, int Num)\r
123 {\r
124         int Save;\r
125         int LFSort;\r
126         int LDSort;\r
127         int RFSort;\r
128         int RDSort;\r
129 \r
130         SaveBookMark();\r
131         SaveCurrentSetToHost();\r
132 \r
133         if((Num >= 0) || (SelectHost(Type) == YES))\r
134         {\r
135                 if(Num >= 0)\r
136                         SetCurrentHost(Num);\r
137 \r
138                 /* 接続中なら切断する */\r
139                 if(CmdCtrlSocket != INVALID_SOCKET)\r
140                         DisconnectProc();\r
141 \r
142                 SetTaskMsg("----------------------------");\r
143 \r
144                 InitPWDcommand();\r
145                 CopyHostFromList(AskCurrentHost(), &CurHost);\r
146 \r
147                 if(ConnectRas(CurHost.Dialup, CurHost.DialupAlways, CurHost.DialupNotify, CurHost.DialEntry) == FFFTP_SUCCESS)\r
148                 {\r
149                         SetHostKanaCnvImm(CurHost.KanaCnv);\r
150                         SetHostKanjiCodeImm(CurHost.KanjiCode);\r
151                         SetSyncMoveMode(CurHost.SyncMove);\r
152 \r
153                         if((AskSaveSortToHost() == YES) && (CurHost.Sort != SORT_NOTSAVED))\r
154                         {\r
155                                 DecomposeSortType(CurHost.Sort, &LFSort, &LDSort, &RFSort, &RDSort);\r
156                                 SetSortTypeImm(LFSort, LDSort, RFSort, RDSort);\r
157                                 ReSortDispList(WIN_LOCAL, &CancelFlg);\r
158                         }\r
159 \r
160                         Save = NO;\r
161                         if(strlen(CurHost.PassWord) > 0)\r
162                                 Save = YES;\r
163 \r
164                         DisableUserOpe();\r
165                         // 暗号化通信対応\r
166 //                      CmdCtrlSocket = DoConnect(CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, Save, CurHost.Security);\r
167                         CmdCtrlSocket = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, Save, CurHost.Security);\r
168                         TrnCtrlSocket = CmdCtrlSocket;\r
169 \r
170                         if(CmdCtrlSocket != INVALID_SOCKET)\r
171                         {\r
172                                 strcpy(TitleHostName, CurHost.HostName);\r
173                                 DispWindowTitle();\r
174                                 SoundPlay(SND_CONNECT);\r
175 \r
176                                 SendInitCommand(CurHost.InitCmd);\r
177 \r
178                                 if(strlen(CurHost.LocalInitDir) > 0)\r
179                                 {\r
180                                         DoLocalCWD(CurHost.LocalInitDir);\r
181                                         GetLocalDirForWnd();\r
182                                 }\r
183                                 InitTransCurDir();\r
184                                 DoCWD(CurHost.RemoteInitDir, YES, YES, YES);\r
185 \r
186                                 LoadBookMark();\r
187                                 GetRemoteDirForWnd(CACHE_NORMAL, &CancelFlg);\r
188                         }\r
189                         else\r
190                                 SoundPlay(SND_ERROR);\r
191 \r
192                         EnableUserOpe();\r
193                 }\r
194                 else\r
195                         SetTaskMsg(MSGJPN001);\r
196         }\r
197         return;\r
198 }\r
199 \r
200 \r
201 /*----- ホスト名を入力してホストへ接続 ----------------------------------------\r
202 *\r
203 *       Parameter\r
204 *               なし\r
205 *\r
206 *       Return Value\r
207 *               なし\r
208 *----------------------------------------------------------------------------*/\r
209 \r
210 void QuickConnectProc(void)\r
211 {\r
212         char Tmp[FMAX_PATH+1 + USER_NAME_LEN+1 + PASSWORD_LEN+1 + 2];\r
213         char File[FMAX_PATH+1];\r
214 \r
215         SaveBookMark();\r
216         SaveCurrentSetToHost();\r
217 \r
218         if(DialogBoxParam(GetFtpInst(), MAKEINTRESOURCE(hostname_dlg), GetMainHwnd(), QuickConDialogCallBack, (LPARAM)Tmp) == YES)\r
219         {\r
220                 /* 接続中なら切断する */\r
221                 if(CmdCtrlSocket != INVALID_SOCKET)\r
222                         DisconnectProc();\r
223 \r
224                 SetTaskMsg("----------------------------");\r
225 \r
226                 InitPWDcommand();\r
227                 CopyDefaultHost(&CurHost);\r
228                 if(SplitUNCpath(Tmp, CurHost.HostAdrs, CurHost.RemoteInitDir, File, CurHost.UserName, CurHost.PassWord, &CurHost.Port) == FFFTP_SUCCESS)\r
229                 {\r
230                         if(strlen(CurHost.UserName) == 0)\r
231                         {\r
232                                 strcpy(CurHost.UserName, Tmp + FMAX_PATH+1);\r
233                                 strcpy(CurHost.PassWord, Tmp + FMAX_PATH+1 + USER_NAME_LEN+1);\r
234                         }\r
235 \r
236                         SetCurrentHost(HOSTNUM_NOENTRY);\r
237                         AskUseFireWall(CurHost.HostAdrs, &CurHost.FireWall, &CurHost.Pasv, &CurHost.ListCmdOnly);\r
238                         CurHost.FireWall = (int)Tmp[FMAX_PATH+1 + USER_NAME_LEN+1 + PASSWORD_LEN+1];\r
239                         CurHost.Pasv = (int)Tmp[FMAX_PATH+1 + USER_NAME_LEN+1 + PASSWORD_LEN+1 + 1];\r
240 \r
241                         SetHostKanaCnvImm(CurHost.KanaCnv);\r
242                         SetHostKanjiCodeImm(CurHost.KanjiCode);\r
243                         SetSyncMoveMode(CurHost.SyncMove);\r
244 \r
245                         DisableUserOpe();\r
246                         // 暗号化通信対応\r
247 //                      CmdCtrlSocket = DoConnect(CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security);\r
248                         CmdCtrlSocket = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security);\r
249                         TrnCtrlSocket = CmdCtrlSocket;\r
250 \r
251                         if(CmdCtrlSocket != INVALID_SOCKET)\r
252                         {\r
253                                 strcpy(TitleHostName, CurHost.HostAdrs);\r
254                                 DispWindowTitle();\r
255                                 SoundPlay(SND_CONNECT);\r
256 \r
257                                 InitTransCurDir();\r
258                                 DoCWD(CurHost.RemoteInitDir, YES, YES, YES);\r
259 \r
260                                 GetRemoteDirForWnd(CACHE_NORMAL, &CancelFlg);\r
261                                 EnableUserOpe();\r
262 \r
263                                 if(strlen(File) > 0)\r
264                                         DirectDownLoadProc(File);\r
265                         }\r
266                         else\r
267                         {\r
268                                 SoundPlay(SND_ERROR);\r
269                                 EnableUserOpe();\r
270                         }\r
271                 }\r
272         }\r
273         return;\r
274 }\r
275 \r
276 \r
277 /*----- クイック接続ダイアログのコールバック ----------------------------------\r
278 *\r
279 *       Parameter\r
280 *               HWND hDlg : ウインドウハンドル\r
281 *               UINT message : メッセージ番号\r
282 *               WPARAM wParam : メッセージの WPARAM 引数\r
283 *               LPARAM lParam : メッセージの LPARAM 引数\r
284 *\r
285 *       Return Value\r
286 *               BOOL TRUE/FALSE\r
287 *----------------------------------------------------------------------------*/\r
288 \r
289 static BOOL CALLBACK QuickConDialogCallBack(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)\r
290 {\r
291         static char *Buf;\r
292         int i;\r
293         HISTORYDATA Tmp;\r
294 \r
295 //char Str[HOST_ADRS_LEN+USER_NAME_LEN+INIT_DIR_LEN+5+1];\r
296 \r
297         switch (iMessage)\r
298         {\r
299                 case WM_INITDIALOG :\r
300                         SendDlgItemMessage(hDlg, QHOST_HOST, CB_LIMITTEXT, FMAX_PATH, 0);\r
301                         SendDlgItemMessage(hDlg, QHOST_HOST, WM_SETTEXT, 0, (LPARAM)"");\r
302                         SendDlgItemMessage(hDlg, QHOST_USER, EM_LIMITTEXT, USER_NAME_LEN, 0);\r
303                         if(QuickAnonymous == YES)\r
304                         {\r
305                                 SendDlgItemMessage(hDlg, QHOST_USER, WM_SETTEXT, 0, (LPARAM)"anonymous");\r
306                                 SendDlgItemMessage(hDlg, QHOST_PASS, WM_SETTEXT, 0, (LPARAM)UserMailAdrs);\r
307                         }\r
308                         else\r
309                         {\r
310                                 SendDlgItemMessage(hDlg, QHOST_USER, WM_SETTEXT, 0, (LPARAM)"");\r
311                                 SendDlgItemMessage(hDlg, QHOST_PASS, WM_SETTEXT, 0, (LPARAM)"");\r
312                         }\r
313                         SendDlgItemMessage(hDlg, QHOST_PASS, EM_LIMITTEXT, PASSWORD_LEN, 0);\r
314                         SendDlgItemMessage(hDlg, QHOST_FWALL, BM_SETCHECK, FwallDefault, 0);\r
315                         SendDlgItemMessage(hDlg, QHOST_PASV, BM_SETCHECK, PasvDefault, 0);\r
316                         for(i = 0; i < HISTORY_MAX; i++)\r
317                         {\r
318                                 if(GetHistoryByNum(i, &Tmp) == FFFTP_SUCCESS)\r
319                                 {\r
320 //sprintf(Str, "%s (%s) %s", Tmp.HostAdrs, Tmp.UserName, Tmp.RemoteInitDir);\r
321 //SendDlgItemMessage(hDlg, QHOST_HOST, CB_ADDSTRING, 0, (LPARAM)Str);\r
322                                         SendDlgItemMessage(hDlg, QHOST_HOST, CB_ADDSTRING, 0, (LPARAM)Tmp.HostAdrs);\r
323                                 }\r
324                         }\r
325                         Buf = (char *)lParam;\r
326                         return(TRUE);\r
327 \r
328                 case WM_COMMAND :\r
329                         switch(GET_WM_COMMAND_ID(wParam, lParam))\r
330                         {\r
331                                 case IDOK :\r
332                                         SendDlgItemMessage(hDlg, QHOST_HOST, WM_GETTEXT, FMAX_PATH+1, (LPARAM)Buf);\r
333                                         SendDlgItemMessage(hDlg, QHOST_USER, WM_GETTEXT, USER_NAME_LEN+1, (LPARAM)Buf + FMAX_PATH+1);\r
334                                         SendDlgItemMessage(hDlg, QHOST_PASS, WM_GETTEXT, PASSWORD_LEN+1, (LPARAM)Buf + FMAX_PATH+1 + USER_NAME_LEN+1);\r
335                                         *(Buf + FMAX_PATH+1 + USER_NAME_LEN+1 + PASSWORD_LEN+1) = (char)SendDlgItemMessage(hDlg, QHOST_FWALL, BM_GETCHECK, 0, 0);\r
336                                         *(Buf + FMAX_PATH+1 + USER_NAME_LEN+1 + PASSWORD_LEN+1+1) = (char)SendDlgItemMessage(hDlg, QHOST_PASV, BM_GETCHECK, 0, 0);\r
337                                         EndDialog(hDlg, YES);\r
338                                         break;\r
339 \r
340                                 case IDCANCEL :\r
341                                         EndDialog(hDlg, NO);\r
342                                         break;\r
343 \r
344 //                              case QHOST_HOST :\r
345 //                                      if(HIWORD(wParam) == CBN_EDITCHANGE)\r
346 //                                              DoPrintf("EDIT");\r
347 //                                      break;\r
348                         }\r
349             return(TRUE);\r
350         }\r
351         return(FALSE);\r
352 }\r
353 \r
354 \r
355 /*----- 指定したホスト名でホストへ接続 ----------------------------------------\r
356 *\r
357 *       Parameter\r
358 *               char *unc : UNC文字列\r
359 *               int Kanji : ホストの漢字コード (KANJI_xxx)\r
360 *               int Kana : 半角かな→全角変換モード (YES/NO)\r
361 *               int Fkanji : ファイル名の漢字コード (KANJI_xxx)\r
362 *               int TrMode : 転送モード (TYPE_xx)\r
363 *\r
364 *       Return Value\r
365 *               なし\r
366 *----------------------------------------------------------------------------*/\r
367 \r
368 void DirectConnectProc(char *unc, int Kanji, int Kana, int Fkanji, int TrMode)\r
369 {\r
370         char Host[HOST_ADRS_LEN+1];\r
371         char Path[FMAX_PATH+1];\r
372         char File[FMAX_PATH+1];\r
373         char User[USER_NAME_LEN+1];\r
374         char Pass[PASSWORD_LEN+1];\r
375         int Port;\r
376 \r
377         SaveBookMark();\r
378         SaveCurrentSetToHost();\r
379 \r
380         /* 接続中なら切断する */\r
381         if(CmdCtrlSocket != INVALID_SOCKET)\r
382                 DisconnectProc();\r
383 \r
384         SetTaskMsg("----------------------------");\r
385 \r
386         InitPWDcommand();\r
387         if(SplitUNCpath(unc, Host, Path, File, User, Pass, &Port) == FFFTP_SUCCESS)\r
388         {\r
389                 if(strlen(User) == 0)\r
390                 {\r
391                         strcpy(User, "anonymous");\r
392                         strcpy(Pass, UserMailAdrs);\r
393                 }\r
394 \r
395                 CopyDefaultHost(&CurHost);\r
396 \r
397                 SetCurrentHost(HOSTNUM_NOENTRY);\r
398                 strcpy(CurHost.HostAdrs, Host);\r
399                 strcpy(CurHost.UserName, User);\r
400                 strcpy(CurHost.PassWord, Pass);\r
401                 strcpy(CurHost.RemoteInitDir, Path);\r
402                 AskUseFireWall(CurHost.HostAdrs, &CurHost.FireWall, &CurHost.Pasv, &CurHost.ListCmdOnly);\r
403                 CurHost.Port = Port;\r
404                 CurHost.KanjiCode = Kanji;\r
405                 CurHost.KanaCnv = Kana;\r
406                 CurHost.NameKanjiCode = Fkanji;\r
407                 CurHost.KanaCnv = YES;                  /* とりあえず */\r
408 \r
409                 SetHostKanaCnvImm(CurHost.KanaCnv);\r
410                 SetHostKanjiCodeImm(CurHost.KanjiCode);\r
411                 SetSyncMoveMode(CurHost.SyncMove);\r
412 \r
413                 if(TrMode != TYPE_DEFAULT)\r
414                 {\r
415                         SetTransferTypeImm(TrMode);\r
416                         DispTransferType();\r
417                 }\r
418 \r
419                 DisableUserOpe();\r
420                 // 暗号化通信対応\r
421 //              CmdCtrlSocket = DoConnect(CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security);\r
422                 CmdCtrlSocket = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security);\r
423                 TrnCtrlSocket = CmdCtrlSocket;\r
424 \r
425                 if(CmdCtrlSocket != INVALID_SOCKET)\r
426                 {\r
427                         strcpy(TitleHostName, CurHost.HostAdrs);\r
428                         DispWindowTitle();\r
429                         SoundPlay(SND_CONNECT);\r
430 \r
431                         InitTransCurDir();\r
432                         DoCWD(CurHost.RemoteInitDir, YES, YES, YES);\r
433 \r
434                         GetRemoteDirForWnd(CACHE_NORMAL, &CancelFlg);\r
435                         EnableUserOpe();\r
436 \r
437                         if(strlen(File) > 0)\r
438                                 DirectDownLoadProc(File);\r
439                         else\r
440                                 ResetAutoExitFlg();\r
441                 }\r
442                 else\r
443                 {\r
444                         SoundPlay(SND_ERROR);\r
445                         EnableUserOpe();\r
446                 }\r
447         }\r
448         return;\r
449 }\r
450 \r
451 \r
452 /*----- ホストのヒストリで指定されたホストへ接続 ------------------------------\r
453 *\r
454 *       Parameter\r
455 *               int MenuCmd : 取り出すヒストリに割り当てられたメニューコマンド\r
456 *\r
457 *       Return Value\r
458 *               なし\r
459 *----------------------------------------------------------------------------*/\r
460 \r
461 void HistoryConnectProc(int MenuCmd)\r
462 {\r
463         HISTORYDATA Hist;\r
464         int LFSort;\r
465         int LDSort;\r
466         int RFSort;\r
467         int RDSort;\r
468 \r
469         if(GetHistoryByCmd(MenuCmd, &Hist) == FFFTP_SUCCESS)\r
470         {\r
471                 SaveBookMark();\r
472                 SaveCurrentSetToHost();\r
473 \r
474                 /* 接続中なら切断する */\r
475                 if(CmdCtrlSocket != INVALID_SOCKET)\r
476                         DisconnectProc();\r
477 \r
478                 SetTaskMsg("----------------------------");\r
479 \r
480                 InitPWDcommand();\r
481                 CopyHistoryToHost(&Hist, &CurHost);\r
482 \r
483                 if(ConnectRas(CurHost.Dialup, CurHost.DialupAlways, CurHost.DialupNotify, CurHost.DialEntry) == FFFTP_SUCCESS)\r
484                 {\r
485                         SetCurrentHost(HOSTNUM_NOENTRY);\r
486                         SetHostKanaCnvImm(CurHost.KanaCnv);\r
487                         SetHostKanjiCodeImm(CurHost.KanjiCode);\r
488                         SetSyncMoveMode(CurHost.SyncMove);\r
489 \r
490                         DecomposeSortType(CurHost.Sort, &LFSort, &LDSort, &RFSort, &RDSort);\r
491                         SetSortTypeImm(LFSort, LDSort, RFSort, RDSort);\r
492                         ReSortDispList(WIN_LOCAL, &CancelFlg);\r
493 \r
494                         SetTransferTypeImm(Hist.Type);\r
495                         DispTransferType();\r
496 \r
497                         DisableUserOpe();\r
498                         // 暗号化通信対応\r
499 //                      CmdCtrlSocket = DoConnect(CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security);\r
500                         CmdCtrlSocket = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security);\r
501                         TrnCtrlSocket = CmdCtrlSocket;\r
502 \r
503                         if(CmdCtrlSocket != INVALID_SOCKET)\r
504                         {\r
505                                 strcpy(TitleHostName, CurHost.HostAdrs);\r
506                                 DispWindowTitle();\r
507                                 SoundPlay(SND_CONNECT);\r
508 \r
509                                 SendInitCommand(CurHost.InitCmd);\r
510 \r
511                                 DoLocalCWD(CurHost.LocalInitDir);\r
512                                 GetLocalDirForWnd();\r
513 \r
514                                 InitTransCurDir();\r
515                                 DoCWD(CurHost.RemoteInitDir, YES, YES, YES);\r
516 \r
517                                 GetRemoteDirForWnd(CACHE_NORMAL, &CancelFlg);\r
518                         }\r
519                         else\r
520                                 SoundPlay(SND_ERROR);\r
521 \r
522                         EnableUserOpe();\r
523                 }\r
524                 else\r
525                         SetTaskMsg(MSGJPN002);\r
526         }\r
527         else\r
528                 SoundPlay(SND_ERROR);\r
529 \r
530         return;\r
531 }\r
532 \r
533 \r
534 /*----- ホストの初期化コマンドを送る ------------------------------------------\r
535 *\r
536 *       Parameter\r
537 *               int Cmd : 初期化コマンドス\r
538 *\r
539 *       Return Value\r
540 *               なし\r
541 *\r
542 *       NOte\r
543 *               初期化コマンドは以下のようなフォーマットであること\r
544 *                       cmd1\0\r
545 *                       cmd1\r\ncmd2\r\n\0\r
546 *----------------------------------------------------------------------------*/\r
547 \r
548 static int SendInitCommand(char *Cmd)\r
549 {\r
550         char Tmp[INITCMD_LEN+1];\r
551         char *Pos;\r
552 \r
553         while(strlen(Cmd) > 0)\r
554         {\r
555                 strcpy(Tmp, Cmd);\r
556                 if((Pos = strchr(Tmp, '\r')) != NULL)\r
557                         *Pos = NUL;\r
558                 if(strlen(Tmp) > 0)\r
559                         DoQUOTE(Tmp);\r
560 \r
561                 if((Cmd = strchr(Cmd, '\n')) != NULL)\r
562                         Cmd++;\r
563                 else\r
564                         break;\r
565         }\r
566         return(0);\r
567 }\r
568 \r
569 \r
570 /*----- 指定のホストはFireWallを使う設定かどうかを返す ------------------------\r
571 *\r
572 *       Parameter\r
573 *               char *Hots : ホスト名\r
574 *               int *Fire : FireWallを使うかどうかを返すワーク\r
575 *               int *Pasv : PASVモードを返すワーク\r
576 *               int *List : LISTコマンドのみ使用フラグ\r
577 *\r
578 *       Return Value\r
579 *               なし\r
580 *----------------------------------------------------------------------------*/\r
581 \r
582 static void AskUseFireWall(char *Host, int *Fire, int *Pasv, int *List)\r
583 {\r
584         int i;\r
585         HOSTDATA Tmp;\r
586 \r
587         *Fire = FwallDefault;\r
588         *Pasv = PasvDefault;\r
589         // NLSTを送ってしまうバグ修正(ただしNLSTを使うべきホストへクイック接続できなくなる)\r
590 //      *List = NO;\r
591 \r
592         i = 0;\r
593         while(CopyHostFromList(i, &Tmp) == FFFTP_SUCCESS)\r
594         {\r
595                 if(strcmp(Host, Tmp.HostAdrs) == 0)\r
596                 {\r
597                         *Fire = Tmp.FireWall;\r
598                         *Pasv = Tmp.Pasv;\r
599                         *List = Tmp.ListCmdOnly;\r
600                         break;\r
601                 }\r
602                 i++;\r
603         }\r
604         return;\r
605 }\r
606 \r
607 \r
608 /*----- 接続しているホストのアドレスを返す ------------------------------------\r
609 *\r
610 *       Parameter\r
611 *               なし\r
612 *\r
613 *       Return Value\r
614 *               char *ホストのアドレス\r
615 *----------------------------------------------------------------------------*/\r
616 \r
617 char *AskHostAdrs(void)\r
618 {\r
619         return(CurHost.HostAdrs);\r
620 }\r
621 \r
622 \r
623 /*----- 接続しているホストのポートを返す --------------------------------------\r
624 *\r
625 *       Parameter\r
626 *               なし\r
627 *\r
628 *       Return Value\r
629 *               int ホストのポート\r
630 *----------------------------------------------------------------------------*/\r
631 \r
632 int AskHostPort(void)\r
633 {\r
634         return(CurHost.Port);\r
635 }\r
636 \r
637 /*----- 接続しているホストのファイル名の漢字コードを返す ----------------------\r
638 *\r
639 *       Parameter\r
640 *               なし\r
641 *\r
642 *       Return Value\r
643 *               int 漢字コード (KANJI_xxx)\r
644 *----------------------------------------------------------------------------*/\r
645 \r
646 int AskHostNameKanji(void)\r
647 {\r
648         if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
649                 CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
650 \r
651         return(CurHost.NameKanjiCode);\r
652 }\r
653 \r
654 \r
655 /*----- 接続しているホストのファイル名の半角カナ変換フラグを返す --------------\r
656 *\r
657 *       Parameter\r
658 *               なし\r
659 *\r
660 *       Return Value\r
661 *               int 半角カナを全角に変換するかどうか (YES/NO)\r
662 *----------------------------------------------------------------------------*/\r
663 \r
664 int AskHostNameKana(void)\r
665 {\r
666         if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
667                 CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
668 \r
669         return(CurHost.NameKanaCnv);\r
670 }\r
671 \r
672 \r
673 /*----- 接続しているホストのLISTコマンドモードを返す --------------------------\r
674 *\r
675 *       Parameter\r
676 *               なし\r
677 *\r
678 *       Return Value\r
679 *               int LISTコマンドモード (YES/NO)\r
680 *----------------------------------------------------------------------------*/\r
681 \r
682 int AskListCmdMode(void)\r
683 {\r
684         if(CurHost.HostType == HTYPE_VMS)\r
685                 return(YES);\r
686         else\r
687         {\r
688                 if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
689                         CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
690                 return(CurHost.ListCmdOnly);\r
691         }\r
692 }\r
693 \r
694 \r
695 /*----- 接続しているホストでNLST -Rを使うかどうかを返す ------------------------\r
696 *\r
697 *       Parameter\r
698 *               なし\r
699 *\r
700 *       Return Value\r
701 *               int NLST -Rを使うかどうか (YES/NO)\r
702 *----------------------------------------------------------------------------*/\r
703 \r
704 int AskUseNLST_R(void)\r
705 {\r
706         if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
707                 CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
708 \r
709         return(CurHost.UseNLST_R);\r
710 }\r
711 \r
712 \r
713 /*----- 接続しているホストのChmodコマンドを返す -------------------------------\r
714 *\r
715 *       Parameter\r
716 *               なし\r
717 *\r
718 *       Return Value\r
719 *               char *Chmodコマンド\r
720 *----------------------------------------------------------------------------*/\r
721 \r
722 char *AskHostChmodCmd(void)\r
723 {\r
724         if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
725                 CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
726 \r
727         return(CurHost.ChmodCmd);\r
728 }\r
729 \r
730 \r
731 /*----- 接続しているホストのタイムゾーンを返す --------------------------------\r
732 *\r
733 *       Parameter\r
734 *               なし\r
735 *\r
736 *       Return Value\r
737 *               int タイムゾーン\r
738 *----------------------------------------------------------------------------*/\r
739 \r
740 int AskHostTimeZone(void)\r
741 {\r
742         if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
743                 CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
744 \r
745         return(CurHost.TimeZone);\r
746 }\r
747 \r
748 \r
749 /*----- 接続しているホストのPASVモードを返す ----------------------------------\r
750 *\r
751 *       Parameter\r
752 *               なし\r
753 *\r
754 *       Return Value\r
755 *               int PASVモードかどうか (YES/NO)\r
756 *----------------------------------------------------------------------------*/\r
757 \r
758 int AskPasvMode(void)\r
759 {\r
760         return(CurHost.Pasv);\r
761 }\r
762 \r
763 \r
764 /*----- 接続しているホストのLNSTファイル名を返す ------------------------------\r
765 *\r
766 *       Parameter\r
767 *               なし\r
768 *\r
769 *       Return Value\r
770 *               char *ファイル名/オプション\r
771 *----------------------------------------------------------------------------*/\r
772 \r
773 char *AskHostLsName(void)\r
774 {\r
775         if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
776                 CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
777 \r
778         return(CurHost.LsName);\r
779 }\r
780 \r
781 \r
782 /*----- 接続しているホストのホストタイプを返す --------------------------------\r
783 *\r
784 *       Parameter\r
785 *               なし\r
786 *\r
787 *       Return Value\r
788 *               char *ファイル名/オプション\r
789 *----------------------------------------------------------------------------*/\r
790 \r
791 int AskHostType(void)\r
792 {\r
793         if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
794                 CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
795 \r
796         return(CurHost.HostType);\r
797 }\r
798 \r
799 \r
800 /*----- 接続しているホストはFireWallを使うホストかどうかを返す ----------------\r
801 *\r
802 *       Parameter\r
803 *               なし\r
804 *\r
805 *       Return Value\r
806 *               int FireWallを使うかどうか (YES/NO)\r
807 *----------------------------------------------------------------------------*/\r
808 \r
809 int AskHostFireWall(void)\r
810 {\r
811         return(CurHost.FireWall);\r
812 }\r
813 \r
814 \r
815 /*----- 接続しているホストでフルパスでファイルアクセスしないかどうかを返す ----\r
816 *\r
817 *       Parameter\r
818 *               なし\r
819 *\r
820 *       Return Value\r
821 *               int フルパスでアクセスしない (YES=フルパス禁止/NO)\r
822 *----------------------------------------------------------------------------*/\r
823 \r
824 int AskNoFullPathMode(void)\r
825 {\r
826         if(CurHost.HostType == HTYPE_VMS)\r
827                 return(YES);\r
828         else\r
829                 return(CurHost.NoFullPath);\r
830 }\r
831 \r
832 \r
833 /*----- 接続しているユーザ名を返す --------------------------------------------\r
834 *\r
835 *       Parameter\r
836 *               なし\r
837 *\r
838 *       Return Value\r
839 *               char *ユーザ名\r
840 *----------------------------------------------------------------------------*/\r
841 \r
842 char *AskHostUserName(void)\r
843 {\r
844         return(CurHost.UserName);\r
845 }\r
846 \r
847 \r
848 /*----- 現在の設定をホストの設定にセットする ----------------------------------\r
849 *\r
850 *       Parameter\r
851 *               なし\r
852 *\r
853 *       Return Value\r
854 *               なし\r
855 *\r
856 *       Note\r
857 *               カレントディレクトリ、ソート方法をホストの設定にセットする\r
858 *----------------------------------------------------------------------------*/\r
859 \r
860 void SaveCurrentSetToHost(void)\r
861 {\r
862         int Host;\r
863         char LocDir[FMAX_PATH+1];\r
864         char HostDir[FMAX_PATH+1];\r
865 \r
866         if(TrnCtrlSocket != INVALID_SOCKET)\r
867         {\r
868                 if((Host = AskCurrentHost()) != HOSTNUM_NOENTRY)\r
869                 {\r
870                         CopyHostFromListInConnect(Host, &CurHost);\r
871                         if(CurHost.LastDir == YES)\r
872                         {\r
873                                 AskLocalCurDir(LocDir, FMAX_PATH);\r
874                                 AskRemoteCurDir(HostDir, FMAX_PATH);\r
875                                 SetHostDir(AskCurrentHost(), LocDir, HostDir);\r
876                         }\r
877                         SetHostSort(AskCurrentHost(), AskSortType(ITEM_LFILE), AskSortType(ITEM_LDIR), AskSortType(ITEM_RFILE), AskSortType(ITEM_RDIR));\r
878                 }\r
879         }\r
880         return;\r
881 }\r
882 \r
883 \r
884 /*----- 現在の設定をヒストリにセットする --------------------------------------\r
885 *\r
886 *       Parameter\r
887 *               なし\r
888 *\r
889 *       Return Value\r
890 *               なし\r
891 *----------------------------------------------------------------------------*/\r
892 \r
893 static void SaveCurrentSetToHistory(void)\r
894 {\r
895         char LocDir[FMAX_PATH+1];\r
896         char HostDir[FMAX_PATH+1];\r
897 \r
898         AskLocalCurDir(LocDir, FMAX_PATH);\r
899         AskRemoteCurDir(HostDir, FMAX_PATH);\r
900         strcpy(CurHost.LocalInitDir, LocDir);\r
901         strcpy(CurHost.RemoteInitDir, HostDir);\r
902 \r
903         CurHost.Sort = AskSortType(ITEM_LFILE) * 0x1000000 | AskSortType(ITEM_LDIR) * 0x10000 | AskSortType(ITEM_RFILE) * 0x100 | AskSortType(ITEM_RDIR);\r
904 \r
905         CurHost.KanjiCode = AskHostKanjiCode();\r
906         CurHost.KanaCnv = AskHostKanaCnv();\r
907 \r
908         CurHost.SyncMove = AskSyncMoveMode();\r
909 \r
910         AddHostToHistory(&CurHost, AskTransferType());\r
911         SetAllHistoryToMenu();\r
912 \r
913         return;\r
914 }\r
915 \r
916 \r
917 /*----- コマンドコントロールソケットの再接続 ----------------------------------\r
918 *\r
919 *       Parameter\r
920 *               なし\r
921 *\r
922 *       Return Value\r
923 *               int ステータス\r
924 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
925 *----------------------------------------------------------------------------*/\r
926 \r
927 int ReConnectCmdSkt(void)\r
928 {\r
929         int Sts;\r
930         SOCKET s;\r
931 \r
932 \r
933         // 同時接続対応\r
934 //      if(CmdCtrlSocket != TrnCtrlSocket)\r
935 //              do_closesocket(TrnCtrlSocket);\r
936 //      TrnCtrlSocket = INVALID_SOCKET;\r
937         s = TrnCtrlSocket;\r
938         TrnCtrlSocket = INVALID_SOCKET;\r
939         if(CmdCtrlSocket != s && s != INVALID_SOCKET)\r
940                 do_closesocket(s);\r
941 \r
942         // 同時接続対応\r
943 //      Sts = ReConnectSkt(&CmdCtrlSocket);\r
944         s = CmdCtrlSocket;\r
945         CmdCtrlSocket = INVALID_SOCKET;\r
946         Sts = ReConnectSkt(&s);\r
947         CmdCtrlSocket = s;\r
948 \r
949         TrnCtrlSocket = CmdCtrlSocket;\r
950 \r
951         return(Sts);\r
952 }\r
953 \r
954 \r
955 /*----- 転送コントロールソケットの再接続 --------------------------------------\r
956 *\r
957 *       Parameter\r
958 *               なし\r
959 *\r
960 *       Return Value\r
961 *               int ステータス\r
962 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
963 *----------------------------------------------------------------------------*/\r
964 \r
965 //int ReConnectTrnSkt(void)\r
966 //{\r
967 //      return(ReConnectSkt(&TrnCtrlSocket));\r
968 //}\r
969 // 同時接続対応\r
970 int ReConnectTrnSkt(SOCKET *Skt)\r
971 {\r
972 //      char Path[FMAX_PATH+1];\r
973         int Sts;\r
974         // 暗号化通信対応\r
975         HOSTDATA HostData;\r
976 \r
977         Sts = FFFTP_FAIL;\r
978 \r
979         SetTaskMsg(MSGJPN003);\r
980 \r
981 //      DisableUserOpe();\r
982         /* 現在のソケットは切断 */\r
983         if(*Skt != INVALID_SOCKET)\r
984                 do_closesocket(*Skt);\r
985         /* 再接続 */\r
986         // 暗号化通信対応\r
987         HostData = CurHost;\r
988         if(HostData.CryptMode != CRYPT_NONE)\r
989                 HostData.UseNoEncryption = NO;\r
990         if(HostData.CryptMode != CRYPT_FTPES)\r
991                 HostData.UseFTPES = NO;\r
992         if(HostData.CryptMode != CRYPT_FTPIS)\r
993                 HostData.UseFTPIS = NO;\r
994         if(HostData.CryptMode != CRYPT_SFTP)\r
995                 HostData.UseSFTP = NO;\r
996 //      if((*Skt = DoConnect(CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security)) != INVALID_SOCKET)\r
997         if((*Skt = DoConnect(&HostData, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security)) != INVALID_SOCKET)\r
998         {\r
999 //              AskRemoteCurDir(Path, FMAX_PATH);\r
1000 //              DoCWD(Path, YES, YES, YES);\r
1001                 Sts = FFFTP_SUCCESS;\r
1002         }\r
1003         else\r
1004                 SoundPlay(SND_ERROR);\r
1005 \r
1006 //      EnableUserOpe();\r
1007         return(Sts);\r
1008 }\r
1009 \r
1010 \r
1011 /*----- 回線の再接続 ----------------------------------------------------------\r
1012 *\r
1013 *       Parameter\r
1014 *               SOCKET *Skt : 接続したソケットを返すワーク\r
1015 *\r
1016 *       Return Value\r
1017 *               int ステータス\r
1018 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1019 *----------------------------------------------------------------------------*/\r
1020 \r
1021 static int ReConnectSkt(SOCKET *Skt)\r
1022 {\r
1023         char Path[FMAX_PATH+1];\r
1024         int Sts;\r
1025 \r
1026         Sts = FFFTP_FAIL;\r
1027 \r
1028         SetTaskMsg(MSGJPN003);\r
1029 \r
1030         DisableUserOpe();\r
1031         /* 現在のソケットは切断 */\r
1032         if(*Skt != INVALID_SOCKET)\r
1033                 do_closesocket(*Skt);\r
1034         /* 再接続 */\r
1035         // 暗号化通信対応\r
1036 //      if((*Skt = DoConnect(CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security)) != INVALID_SOCKET)\r
1037         if((*Skt = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security)) != INVALID_SOCKET)\r
1038         {\r
1039                 AskRemoteCurDir(Path, FMAX_PATH);\r
1040                 DoCWD(Path, YES, YES, YES);\r
1041                 Sts = FFFTP_SUCCESS;\r
1042         }\r
1043         else\r
1044                 SoundPlay(SND_ERROR);\r
1045 \r
1046         EnableUserOpe();\r
1047         return(Sts);\r
1048 }\r
1049 \r
1050 \r
1051 /*----- コマンドコントロールソケットを返す ------------------------------------\r
1052 *\r
1053 *       Parameter\r
1054 *               なし\r
1055 *\r
1056 *       Return Value\r
1057 *               SOCKET コマンドコントロールソケット\r
1058 *----------------------------------------------------------------------------*/\r
1059 \r
1060 SOCKET AskCmdCtrlSkt(void)\r
1061 {\r
1062         return(CmdCtrlSocket);\r
1063 }\r
1064 \r
1065 \r
1066 /*----- 転送コントロールソケットを返す ----------------------------------------\r
1067 *\r
1068 *       Parameter\r
1069 *               なし\r
1070 *\r
1071 *       Return Value\r
1072 *               SOCKET 転送コントロールソケット\r
1073 *----------------------------------------------------------------------------*/\r
1074 \r
1075 SOCKET AskTrnCtrlSkt(void)\r
1076 {\r
1077         return(TrnCtrlSocket);\r
1078 }\r
1079 \r
1080 \r
1081 /*----- コマンド/転送コントロールソケットの共有を解除 ------------------------\r
1082 *\r
1083 *       Parameter\r
1084 *               なし\r
1085 *\r
1086 *       Return Value\r
1087 *               なし\r
1088 *----------------------------------------------------------------------------*/\r
1089 \r
1090 void SktShareProh(void)\r
1091 {\r
1092         if(CmdCtrlSocket == TrnCtrlSocket)\r
1093         {\r
1094 \r
1095 //SetTaskMsg("############### SktShareProh");\r
1096 \r
1097                 // 同時接続対応\r
1098                 // 転送スレッドがソケットを各自で用意\r
1099                 // TrnCtrlSocketはメインスレッド以外から使用されない\r
1100 //              CmdCtrlSocket = INVALID_SOCKET;\r
1101 //              ReConnectSkt(&CmdCtrlSocket);\r
1102         }\r
1103         return;\r
1104 }\r
1105 \r
1106 \r
1107 /*----- コマンド/転送コントロールソケットの共有が解除されているかチェック ----\r
1108 *\r
1109 *       Parameter\r
1110 *               なし\r
1111 *\r
1112 *       Return Value\r
1113 *               int ステータス\r
1114 *                       YES=共有解除/NO=共有\r
1115 *----------------------------------------------------------------------------*/\r
1116 \r
1117 int AskShareProh(void)\r
1118 {\r
1119         int Sts;\r
1120 \r
1121         Sts = YES;\r
1122 //      if(CmdCtrlSocket == TrnCtrlSocket)\r
1123 //              Sts = NO;\r
1124 \r
1125         return(Sts);\r
1126 }\r
1127 \r
1128 \r
1129 /*----- ホストから切断 --------------------------------------------------------\r
1130 *\r
1131 *       Parameter\r
1132 *               なし\r
1133 *\r
1134 *       Return Value\r
1135 *               なし\r
1136 *----------------------------------------------------------------------------*/\r
1137 \r
1138 void DisconnectProc(void)\r
1139 {\r
1140 \r
1141 //SetTaskMsg("############### Disconnect Cmd=%x, Trn=%x", CmdCtrlSocket,TrnCtrlSocket);\r
1142 \r
1143         if((CmdCtrlSocket != INVALID_SOCKET) && (CmdCtrlSocket != TrnCtrlSocket))\r
1144         {\r
1145                 DoQUIT(CmdCtrlSocket);\r
1146                 DoClose(CmdCtrlSocket);\r
1147         }\r
1148 \r
1149         if(TrnCtrlSocket != INVALID_SOCKET)\r
1150         {\r
1151                 DoQUIT(TrnCtrlSocket);\r
1152                 DoClose(TrnCtrlSocket);\r
1153 \r
1154                 SaveCurrentSetToHistory();\r
1155 \r
1156                 EraseRemoteDirForWnd();\r
1157                 SetTaskMsg(MSGJPN004);\r
1158         }\r
1159 \r
1160         TrnCtrlSocket = INVALID_SOCKET;\r
1161         CmdCtrlSocket = INVALID_SOCKET;\r
1162 \r
1163         DispWindowTitle();\r
1164         MakeButtonsFocus();\r
1165         ClearBookMark();\r
1166 \r
1167         return;\r
1168 }\r
1169 \r
1170 \r
1171 /*----- ソケットが強制切断されたときの処理 ------------------------------------\r
1172 *\r
1173 *       Parameter\r
1174 *               なし\r
1175 *\r
1176 *       Return Value\r
1177 *               なし\r
1178 *----------------------------------------------------------------------------*/\r
1179 \r
1180 void DisconnectSet(void)\r
1181 {\r
1182         CmdCtrlSocket = INVALID_SOCKET;\r
1183         TrnCtrlSocket = INVALID_SOCKET;\r
1184 \r
1185         EraseRemoteDirForWnd();\r
1186         DispWindowTitle();\r
1187         MakeButtonsFocus();\r
1188         SetTaskMsg(MSGJPN005);\r
1189         return;\r
1190 }\r
1191 \r
1192 \r
1193 /*----- ホストに接続中かどうかを返す ------------------------------------------\r
1194 *\r
1195 *       Parameter\r
1196 *               なし\r
1197 *\r
1198 *       Return Value\r
1199 *               int ステータス (YES/NO)\r
1200 *----------------------------------------------------------------------------*/\r
1201 \r
1202 int AskConnecting(void)\r
1203 {\r
1204         int Sts;\r
1205 \r
1206         Sts = NO;\r
1207         if(TrnCtrlSocket != INVALID_SOCKET)\r
1208                 Sts = YES;\r
1209 \r
1210         return(Sts);\r
1211 }\r
1212 \r
1213 \r
1214 /*----- ホストへ接続する ------------------------------------------------------\r
1215 *\r
1216 *       Parameter\r
1217 *               char *Host : ホスト名\r
1218 *               char *User : ユーザ名\r
1219 *               char *Pass : パスワード\r
1220 *               char *Acct : アカウント\r
1221 *               int Port : ポート\r
1222 *               int Fwall : FireWallを使うかどうか (YES/NO)\r
1223 *               int SavePass : パスワードを再入力した時に保存するかどうか (YES/NO)\r
1224 *               int Security : セキュリティ (SECURITY_xxx, MDx)\r
1225 *\r
1226 *       Return Value\r
1227 *               SOCKET ソケット\r
1228 *\r
1229 *       Note\r
1230 *               ホスト名、ユーザ名、パスワードが指定されていなかったときは、接続に使用\r
1231 *               したものをコピーしてかえす\r
1232 *                       char *Host : ホスト名\r
1233 *                       char *User : ユーザ名\r
1234 *                       char *Pass : パスワード\r
1235 *                       char *Acct : アカウント\r
1236 *\r
1237 *               FireWallは次のように動作する\r
1238 *                       TYPE1   Connect fire → USER user(f) → PASS pass(f) → SITE host → USER user(h) →      PASS pass(h) → ACCT acct\r
1239 *                       TYPE2   Connect fire → USER user(f) → PASS pass(f) →              USER user(h)@host → PASS pass(h) → ACCT acct\r
1240 *                       TYPE3   Connect fire →                                              USER user(h)@host → PASS pass(h) → ACCT acct\r
1241 *                       TYPE4   Connect fire →                                 OPEN host → USER user(h) →      PASS pass(h) → ACCT acct\r
1242 *                       TYPE5   SOCKS4\r
1243 *                       none    Connect host →                                              USER user(h) →      PASS pass(h) → ACCT acct\r
1244 *----------------------------------------------------------------------------*/\r
1245 \r
1246 // 暗号化通信対応\r
1247 static SOCKET DoConnectCrypt(int CryptMode, HOSTDATA* HostData, char *Host, char *User, char *Pass, char *Acct, int Port, int Fwall, int SavePass, int Security)\r
1248 {\r
1249         int Sts;\r
1250         int Flg;\r
1251         int Anony;\r
1252         SOCKET ContSock;\r
1253         char Buf[1024];\r
1254         char Reply[1024];\r
1255         int Continue;\r
1256         int ReInPass;\r
1257         char *Tmp;\r
1258         int HostPort;\r
1259         static const char *SiteTbl[4] = { "SITE", "site", "OPEN", "open" };\r
1260         char TmpBuf[ONELINE_BUF_SIZE];\r
1261         struct linger LingerOpt;\r
1262 \r
1263         // 暗号化通信対応\r
1264         ContSock = INVALID_SOCKET;\r
1265 \r
1266         if(CryptMode == CRYPT_NONE || CryptMode == CRYPT_FTPES || CryptMode == CRYPT_FTPIS)\r
1267         {\r
1268                 if(Fwall == YES)\r
1269                         Fwall = FwallType;\r
1270                 else\r
1271                         Fwall = FWALL_NONE;\r
1272 \r
1273                 TryConnect = YES;\r
1274                 // 暗号化通信対応\r
1275 //              CancelFlg = NO;\r
1276 #if 0\r
1277 //              WSASetBlockingHook(BlkHookFnc);\r
1278 #endif\r
1279 \r
1280                 ContSock = INVALID_SOCKET;\r
1281 \r
1282                 HostPort = Port;\r
1283                 Tmp = Host;\r
1284                 if(((Fwall >= FWALL_FU_FP_SITE) && (Fwall <= FWALL_OPEN)) ||\r
1285                    (Fwall == FWALL_SIDEWINDER) ||\r
1286                    (Fwall == FWALL_FU_FP))\r
1287                 {\r
1288                         Tmp = FwallHost;\r
1289                         Port = FwallPort;\r
1290                 }\r
1291 \r
1292                 if(strlen(Tmp) != 0)\r
1293                 {\r
1294                         if((ContSock = connectsock(Tmp, Port, "", &CancelFlg)) != INVALID_SOCKET)\r
1295                         {\r
1296                                 // バッファを無効\r
1297 #ifdef DISABLE_CONTROL_NETWORK_BUFFERS\r
1298                                 int BufferSize = 0;\r
1299                                 setsockopt(ContSock, SOL_SOCKET, SO_SNDBUF, (char*)&BufferSize, sizeof(int));\r
1300                                 setsockopt(ContSock, SOL_SOCKET, SO_RCVBUF, (char*)&BufferSize, sizeof(int));\r
1301 #endif\r
1302                                 // FTPIS対応\r
1303 //                              while((Sts = ReadReplyMessage(ContSock, Buf, 1024, &CancelFlg, TmpBuf) / 100) == FTP_PRELIM)\r
1304 //                                      ;\r
1305                                 if(CryptMode == CRYPT_FTPIS)\r
1306                                 {\r
1307                                         if(AttachSSL(ContSock, INVALID_SOCKET))\r
1308                                         {\r
1309                                                 while((Sts = ReadReplyMessage(ContSock, Buf, 1024, &CancelFlg, TmpBuf) / 100) == FTP_PRELIM)\r
1310                                                         ;\r
1311                                                 if((Sts = command(ContSock, Reply, &CancelFlg, "PBSZ 0")) == 200)\r
1312                                                 {\r
1313                                                         if((Sts = command(ContSock, Reply, &CancelFlg, "PROT P")) == 200)\r
1314                                                         {\r
1315                                                         }\r
1316                                                         else\r
1317                                                                 Sts = FTP_ERROR;\r
1318                                                 }\r
1319                                                 else\r
1320                                                         Sts = FTP_ERROR;\r
1321                                         }\r
1322                                         else\r
1323                                                 Sts = FTP_ERROR;\r
1324                                 }\r
1325                                 else\r
1326                                 {\r
1327                                         while((Sts = ReadReplyMessage(ContSock, Buf, 1024, &CancelFlg, TmpBuf) / 100) == FTP_PRELIM)\r
1328                                                 ;\r
1329                                 }\r
1330 \r
1331                                 if(Sts == FTP_COMPLETE)\r
1332                                 {\r
1333                                         Flg = 1;\r
1334                                         if(setsockopt(ContSock, SOL_SOCKET, SO_OOBINLINE, (LPSTR)&Flg, sizeof(Flg)) == SOCKET_ERROR)\r
1335                                                 ReportWSError("setsockopt", WSAGetLastError());\r
1336                                         // データ転送用ソケットのTCP遅延転送が無効されているので念のため\r
1337                                         if(setsockopt(ContSock, IPPROTO_TCP, TCP_NODELAY, (LPSTR)&Flg, sizeof(Flg)) == SOCKET_ERROR)\r
1338                                                 ReportWSError("setsockopt", WSAGetLastError());\r
1339 //#pragma aaa\r
1340                                         Flg = 1;\r
1341                                         if(setsockopt(ContSock, SOL_SOCKET, SO_KEEPALIVE, (LPSTR)&Flg, sizeof(Flg)) == SOCKET_ERROR)\r
1342                                                 ReportWSError("setsockopt", WSAGetLastError());\r
1343                                         LingerOpt.l_onoff = 1;\r
1344                                         LingerOpt.l_linger = 90;\r
1345                                         if(setsockopt(ContSock, SOL_SOCKET, SO_LINGER, (LPSTR)&LingerOpt, sizeof(LingerOpt)) == SOCKET_ERROR)\r
1346                                                 ReportWSError("setsockopt", WSAGetLastError());\r
1347 ///////\r
1348 \r
1349 \r
1350                                         /*===== 認証を行なう =====*/\r
1351 \r
1352                                         Sts = FTP_COMPLETE;\r
1353                                         if((Fwall == FWALL_FU_FP_SITE) ||\r
1354                                            (Fwall == FWALL_FU_FP_USER) ||\r
1355                                            (Fwall == FWALL_FU_FP))\r
1356                                         {\r
1357                                                 if((Sts = command(ContSock, Reply, &CancelFlg, "USER %s", FwallUser) / 100) == FTP_CONTINUE)\r
1358                                                 {\r
1359                                                         CheckOneTimePassword(FwallPass, Reply, FwallSecurity);\r
1360                                                         Sts = command(ContSock, NULL, &CancelFlg, "PASS %s", Reply) / 100;\r
1361                                                 }\r
1362                                         }\r
1363                                         else if(Fwall == FWALL_SIDEWINDER)\r
1364                                         {\r
1365                                                 Sts = command(ContSock, Reply, &CancelFlg, "USER %s:%s%c%s", FwallUser, FwallPass, FwallDelimiter, Host) / 100;\r
1366                                         }\r
1367                                         if((Sts != FTP_COMPLETE) && (Sts != FTP_CONTINUE))\r
1368                                         {\r
1369                                                 SetTaskMsg(MSGJPN006);\r
1370                                                 DoClose(ContSock);\r
1371                                                 ContSock = INVALID_SOCKET;\r
1372                                         }\r
1373                                         else\r
1374                                         {\r
1375                                                 if((Fwall == FWALL_FU_FP_SITE) || (Fwall == FWALL_OPEN))\r
1376                                                 {\r
1377                                                         Flg = 0;\r
1378                                                         if(Fwall == FWALL_OPEN)\r
1379                                                                 Flg = 2;\r
1380                                                         if(FwallLower == YES)\r
1381                                                                 Flg++;\r
1382 \r
1383                                                         if(HostPort == PORT_NOR)\r
1384                                                                 Sts = command(ContSock, NULL, &CancelFlg, "%s %s", SiteTbl[Flg], Host) / 100;\r
1385                                                         else\r
1386                                                                 Sts = command(ContSock, NULL, &CancelFlg, "%s %s %d", SiteTbl[Flg], Host, HostPort) / 100;\r
1387                                                 }\r
1388 \r
1389                                                 if((Sts != FTP_COMPLETE) && (Sts != FTP_CONTINUE))\r
1390                                                 {\r
1391                                                         SetTaskMsg(MSGJPN007, Host);\r
1392                                                         DoClose(ContSock);\r
1393                                                         ContSock = INVALID_SOCKET;\r
1394                                                 }\r
1395                                                 else\r
1396                                                 {\r
1397                                                         Anony = NO;\r
1398                                                         if((strlen(User) != 0) || \r
1399                                                            (InputDialogBox(username_dlg, GetMainHwnd(), NULL, User, USER_NAME_LEN+1, &Anony, IDH_HELP_TOPIC_0000001) == YES))\r
1400                                                         {\r
1401                                                                 if(Anony == YES)\r
1402                                                                 {\r
1403                                                                         strcpy(User, "anonymous");\r
1404                                                                         strcpy(Pass, UserMailAdrs);\r
1405                                                                 }\r
1406 \r
1407                                                                 if((Fwall == FWALL_FU_FP_USER) || (Fwall == FWALL_USER))\r
1408                                                                 {\r
1409                                                                         if(HostPort == PORT_NOR)\r
1410                                                                                 sprintf(Buf, "%s%c%s", User, FwallDelimiter, Host);\r
1411                                                                         else\r
1412                                                                                 sprintf(Buf, "%s%c%s %d", User, FwallDelimiter, Host, HostPort);\r
1413                                                                 }\r
1414                                                                 else\r
1415                                                                         strcpy(Buf, User);\r
1416 \r
1417                                                                 // FTPES対応\r
1418                                                                 if(CryptMode == CRYPT_FTPES)\r
1419                                                                 {\r
1420                                                                         if(IsOpenSSLLoaded() && (Sts = command(ContSock, Reply, &CancelFlg, "AUTH TLS")) == 234)\r
1421                                                                         {\r
1422                                                                                 if(AttachSSL(ContSock, INVALID_SOCKET))\r
1423                                                                                 {\r
1424                                                                                         if((Sts = command(ContSock, Reply, &CancelFlg, "PBSZ 0")) == 200)\r
1425                                                                                         {\r
1426                                                                                                 if((Sts = command(ContSock, Reply, &CancelFlg, "PROT P")) == 200)\r
1427                                                                                                 {\r
1428                                                                                                 }\r
1429                                                                                                 else\r
1430                                                                                                         Sts = FTP_ERROR;\r
1431                                                                                         }\r
1432                                                                                         else\r
1433                                                                                                 Sts = FTP_ERROR;\r
1434                                                                                 }\r
1435                                                                                 else\r
1436                                                                                         Sts = FTP_ERROR;\r
1437                                                                         }\r
1438                                                                         else\r
1439                                                                                 Sts = FTP_ERROR;\r
1440                                                                 }\r
1441 \r
1442                                                                 ReInPass = NO;\r
1443                                                                 do\r
1444                                                                 {\r
1445                                                                         // FTPES対応\r
1446                                                                         if(Sts == FTP_ERROR)\r
1447                                                                                 break;\r
1448                                                                         Continue = NO;\r
1449                                                                         if((Sts = command(ContSock, Reply, &CancelFlg, "USER %s", Buf) / 100) == FTP_CONTINUE)\r
1450                                                                         {\r
1451                                                                                 if((strlen(Pass) != 0) || \r
1452                                                                                    (InputDialogBox(passwd_dlg, GetMainHwnd(), NULL, Pass, PASSWORD_LEN+1, &Anony, IDH_HELP_TOPIC_0000001) == YES))\r
1453                                                                                 {\r
1454                                                                                         CheckOneTimePassword(Pass, Reply, Security);\r
1455 \r
1456                                                                                         /* パスワードがスペース1個の時はパスワードの実体なしとする */\r
1457                                                                                         if(strcmp(Reply, " ") == 0)\r
1458                                                                                                 strcpy(Reply, "");\r
1459 \r
1460                                                                                         Sts = command(ContSock, NULL, &CancelFlg, "PASS %s", Reply) / 100;\r
1461                                                                                         if(Sts == FTP_ERROR)\r
1462                                                                                         {\r
1463                                                                                                 strcpy(Pass, "");\r
1464                                                                                                 if(InputDialogBox(re_passwd_dlg, GetMainHwnd(), NULL, Pass, PASSWORD_LEN+1, &Anony, IDH_HELP_TOPIC_0000001) == YES)\r
1465                                                                                                         Continue = YES;\r
1466                                                                                                 else\r
1467                                                                                                         DoPrintf("No password specified.");\r
1468                                                                                                 ReInPass = YES;\r
1469                                                                                         }\r
1470                                                                                         else if(Sts == FTP_CONTINUE)\r
1471                                                                                         {\r
1472                                                                                                 if((strlen(Acct) != 0) || \r
1473                                                                                                    (InputDialogBox(account_dlg, GetMainHwnd(), NULL, Acct, ACCOUNT_LEN+1, &Anony, IDH_HELP_TOPIC_0000001) == YES))\r
1474                                                                                                 {\r
1475                                                                                                         Sts = command(ContSock, NULL, &CancelFlg, "ACCT %s", Acct) / 100;\r
1476                                                                                                 }\r
1477                                                                                                 else\r
1478                                                                                                         DoPrintf("No account specified");\r
1479                                                                                         }\r
1480                                                                                 }\r
1481                                                                                 else\r
1482                                                                                 {\r
1483                                                                                         Sts = FTP_ERROR;\r
1484                                                                                         DoPrintf("No password specified.");\r
1485                                                                                 }\r
1486                                                                         }\r
1487                                                                 }\r
1488                                                                 while(Continue == YES);\r
1489                                                         }\r
1490                                                         else\r
1491                                                         {\r
1492                                                                 Sts = FTP_ERROR;\r
1493                                                                 DoPrintf("No user name specified");\r
1494                                                         }\r
1495 \r
1496                                                         if(Sts != FTP_COMPLETE)\r
1497                                                         {\r
1498                                                                 SetTaskMsg(MSGJPN008, Host);\r
1499                                                                 DoClose(ContSock);\r
1500                                                                 ContSock = INVALID_SOCKET;\r
1501                                                         }\r
1502                                                         else if((SavePass == YES) && (ReInPass == YES))\r
1503                                                         {\r
1504                                                                 if(DialogBox(GetFtpInst(), MAKEINTRESOURCE(savepass_dlg), GetMainHwnd(), ExeEscDialogProc) == YES)\r
1505                                                                         SetHostPassword(AskCurrentHost(), Pass);\r
1506                                                         }\r
1507                                                 }\r
1508                                         }\r
1509                                 }\r
1510                                 else\r
1511                                 {\r
1512 //#pragma aaa\r
1513                                         SetTaskMsg(MSGJPN009/*"接続できません(1) %x", ContSock*/);\r
1514                                         DoClose(ContSock);\r
1515                                         ContSock = INVALID_SOCKET;\r
1516                                 }\r
1517                         }\r
1518                 }\r
1519                 else\r
1520                 {\r
1521 \r
1522                         if(((Fwall >= FWALL_FU_FP_SITE) && (Fwall <= FWALL_OPEN)) ||\r
1523                            (Fwall == FWALL_FU_FP))\r
1524                                 SetTaskMsg(MSGJPN010);\r
1525                         else\r
1526                                 SetTaskMsg(MSGJPN011);\r
1527                 }\r
1528 \r
1529 #if 0\r
1530 //              WSAUnhookBlockingHook();\r
1531 #endif\r
1532                 TryConnect = NO;\r
1533 \r
1534                 // FEAT対応\r
1535                 // ホストの機能を確認\r
1536                 if(ContSock != INVALID_SOCKET)\r
1537                 {\r
1538                         if((Sts = command(ContSock, Reply, &CancelFlg, "FEAT")) == 211)\r
1539                         {\r
1540                                 // 改行文字はReadReplyMessageで消去されるため区切り文字に空白を使用\r
1541                                 // UTF-8対応\r
1542                                 if(strstr(Reply, " UTF8 "))\r
1543                                         HostData->Feature |= FEATURE_UTF8;\r
1544                                 // MLST対応\r
1545                                 if(strstr(Reply, " MLST ") || strstr(Reply, " MLSD "))\r
1546                                         HostData->Feature |= FEATURE_MLSD;\r
1547                                 // IPv6対応\r
1548                                 if(strstr(Reply, " EPRT ") || strstr(Reply, " EPSV "))\r
1549                                         HostData->Feature |= FEATURE_EPRT | FEATURE_EPSV;\r
1550                         }\r
1551                         // UTF-8対応\r
1552                         if(HostData->NameKanjiCode == KANJI_AUTO && (HostData->Feature & FEATURE_UTF8))\r
1553                         {\r
1554                                 if((Sts = command(ContSock, Reply, &CancelFlg, "OPTS UTF8 ON")) == 200)\r
1555                                 {\r
1556                                 }\r
1557                         }\r
1558                 }\r
1559         }\r
1560         else if(CryptMode == CRYPT_SFTP)\r
1561         {\r
1562         }\r
1563 \r
1564         return(ContSock);\r
1565 }\r
1566 \r
1567 static SOCKET DoConnect(HOSTDATA* HostData, char *Host, char *User, char *Pass, char *Acct, int Port, int Fwall, int SavePass, int Security)\r
1568 {\r
1569         SOCKET ContSock;\r
1570         ContSock = INVALID_SOCKET;\r
1571         CancelFlg = NO;\r
1572         if(CancelFlg == NO && ContSock == INVALID_SOCKET && HostData->UseSFTP == YES)\r
1573         {\r
1574                 SetTaskMsg(MSGJPN317);\r
1575                 if((ContSock = DoConnectCrypt(CRYPT_SFTP, HostData, Host, User, Pass, Acct, Port, Fwall, SavePass, Security)) != INVALID_SOCKET)\r
1576                         HostData->CryptMode = CRYPT_SFTP;\r
1577         }\r
1578 //      if(CancelFlg == NO && ContSock == INVALID_SOCKET && HostData->UseFTPIS == YES)\r
1579 //      {\r
1580 //              SetTaskMsg(MSGJPN316);\r
1581 //              if((ContSock = DoConnectCrypt(CRYPT_FTPIS, HostData, Host, User, Pass, Acct, Port, Fwall, SavePass, Security)) != INVALID_SOCKET)\r
1582 //                      HostData->CryptMode = CRYPT_FTPIS;\r
1583 //      }\r
1584         if(CancelFlg == NO && ContSock == INVALID_SOCKET && HostData->UseFTPES == YES)\r
1585         {\r
1586                 SetTaskMsg(MSGJPN315);\r
1587                 if((ContSock = DoConnectCrypt(CRYPT_FTPES, HostData, Host, User, Pass, Acct, Port, Fwall, SavePass, Security)) != INVALID_SOCKET)\r
1588                         HostData->CryptMode = CRYPT_FTPES;\r
1589         }\r
1590         if(CancelFlg == NO && ContSock == INVALID_SOCKET && HostData->UseNoEncryption == YES)\r
1591         {\r
1592                 SetTaskMsg(MSGJPN314);\r
1593                 if((ContSock = DoConnectCrypt(CRYPT_NONE, HostData, Host, User, Pass, Acct, Port, Fwall, SavePass, Security)) != INVALID_SOCKET)\r
1594                         HostData->CryptMode = CRYPT_NONE;\r
1595         }\r
1596         return ContSock;\r
1597 }\r
1598 \r
1599 \r
1600 /*----- ワンタイムパスワードのチェック ----------------------------------------\r
1601 *\r
1602 *       Parameter\r
1603 *               chat *Pass : パスワード/パスフレーズ\r
1604 *               char *Reply : USERコマンドを送ったあとのリプライ文字列\r
1605 *                                               /PASSコマンドで送るパスワードを返すバッファ\r
1606 *               int Type : タイプ (SECURITY_xxx, MDx)\r
1607 *\r
1608 *       Return Value\r
1609 *               int ステータス\r
1610 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1611 *\r
1612 *       Note\r
1613 *               ワンタイムパスワードでない時はPassをそのままReplyにコピー\r
1614 *----------------------------------------------------------------------------*/\r
1615 \r
1616 static int CheckOneTimePassword(char *Pass, char *Reply, int Type)\r
1617 {\r
1618         int Sts;\r
1619         char *Pos;\r
1620         int Seq;\r
1621         char Seed[MAX_SEED_LEN+1];\r
1622         int i;\r
1623 \r
1624         Sts = FFFTP_SUCCESS;\r
1625         Pos = NULL;\r
1626 \r
1627         if(Type == SECURITY_AUTO)\r
1628         {\r
1629                 if((Pos = stristr(Reply, "otp-md5")) != NULL)\r
1630                 {\r
1631                         Type = MD5;\r
1632                         SetTaskMsg(MSGJPN012);\r
1633                 }\r
1634                 else if((Pos = stristr(Reply, "otp-sha1")) != NULL)\r
1635                 {\r
1636                         Type = SHA1;\r
1637                         SetTaskMsg(MSGJPN013);\r
1638                 }\r
1639                 else if(((Pos = stristr(Reply, "otp-md4")) != NULL) || ((Pos = stristr(Reply, "s/key")) != NULL))\r
1640                 {\r
1641                         Type = MD4;\r
1642                         SetTaskMsg(MSGJPN014);\r
1643                 }\r
1644         }\r
1645         else\r
1646                 Pos = GetNextField(Reply);\r
1647 \r
1648         if((Type == MD4) || (Type == MD5) || (Type == SHA1))\r
1649         {\r
1650                 /* シーケンス番号を見つけるループ */\r
1651                 DoPrintf("Analize OTP");\r
1652                 DoPrintf("%s", Pos);\r
1653                 Sts = FFFTP_FAIL;\r
1654                 while((Pos = GetNextField(Pos)) != NULL)\r
1655                 {\r
1656                         if(IsDigit(*Pos))\r
1657                         {\r
1658                                 Seq = atoi(Pos);\r
1659                                 DoPrintf("Sequence=%d", Seq);\r
1660 \r
1661                                 /* Seed */\r
1662                                 if((Pos = GetNextField(Pos)) != NULL)\r
1663                                 {\r
1664                                         if(GetOneField(Pos, Seed, MAX_SEED_LEN) == FFFTP_SUCCESS)\r
1665                                         {\r
1666                                                 /* Seedは英数字のみ有効とする */\r
1667                                                 for(i = strlen(Seed)-1; i >= 0; i--)\r
1668                                                 {\r
1669                                                         if((IsAlpha(Seed[i]) == 0) && (IsDigit(Seed[i]) == 0))\r
1670                                                                 Seed[i] = NUL;\r
1671                                                 }\r
1672                                                 if(strlen(Seed) > 0)\r
1673                                                 {\r
1674                                                         DoPrintf("Seed=%s", Seed);\r
1675                                                         Make6WordPass(Seq, Seed, Pass, Type, Reply);\r
1676                                                         DoPrintf("Response=%s", Reply);\r
1677 \r
1678                                                         /* シーケンス番号のチェックと警告 */\r
1679                                                         if(Seq <= 10)\r
1680                                                                 DialogBox(GetFtpInst(), MAKEINTRESOURCE(otp_notify_dlg), GetMainHwnd(), ExeEscDialogProc);\r
1681 \r
1682                                                         Sts = FFFTP_SUCCESS;\r
1683                                                 }\r
1684                                         }\r
1685                                 }\r
1686                                 break;\r
1687                         }\r
1688                 }\r
1689 \r
1690                 if(Sts == FFFTP_FAIL)\r
1691                         SetTaskMsg(MSGJPN015);\r
1692         }\r
1693         else\r
1694         {\r
1695                 strcpy(Reply, Pass);\r
1696                 DoPrintf("No OTP used.");\r
1697         }\r
1698         return(Sts);\r
1699 }\r
1700 \r
1701 \r
1702 \r
1703 \r
1704 \r
1705 \r
1706 \r
1707 \r
1708 \r
1709 \r
1710 \r
1711 \r
1712 \r
1713 \r
1714 /*----- ソケットを接続する ----------------------------------------------------\r
1715 *\r
1716 *       Parameter\r
1717 *               char *host : ホスト名\r
1718 *               int port : ポート番号\r
1719 *               char *PreMsg : メッセージの前半部分\r
1720 *\r
1721 *       Return Value\r
1722 *               SOCKET ソケット\r
1723 *----------------------------------------------------------------------------*/\r
1724 \r
1725 SOCKET connectsock(char *host, int port, char *PreMsg, int *CancelCheckWork)\r
1726 {\r
1727         struct sockaddr_in saSockAddr;\r
1728         char HostEntry[MAXGETHOSTSTRUCT];\r
1729         struct hostent *pHostEntry;\r
1730         SOCKET sSocket;\r
1731         int Len;\r
1732         int Fwall;\r
1733         SOCKS4CMD Socks4Cmd;\r
1734         SOCKS4REPLY Socks4Reply;\r
1735         SOCKS5REQUEST Socks5Cmd;\r
1736         SOCKS5REPLY Socks5Reply;\r
1737 \r
1738         //////////////////////////////\r
1739         // ホスト名解決と接続の準備\r
1740         //////////////////////////////\r
1741 \r
1742         Fwall = FWALL_NONE;\r
1743         if(AskHostFireWall() == YES)\r
1744                 Fwall = FwallType;\r
1745 \r
1746         sSocket = INVALID_SOCKET;\r
1747 \r
1748         UseIPadrs = YES;\r
1749         strcpy(DomainName, host);\r
1750         memset(&CurSockAddr, 0, sizeof(CurSockAddr));\r
1751         CurSockAddr.sin_port = htons((u_short)port);\r
1752         CurSockAddr.sin_family = AF_INET;\r
1753         if((CurSockAddr.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)\r
1754         {\r
1755                 // ホスト名が指定された\r
1756                 // ホスト名からアドレスを求める\r
1757                 if(((Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER)) &&\r
1758                    (FwallResolv == YES))\r
1759                 {\r
1760                         // ホスト名解決はSOCKSサーバに任せる\r
1761                         pHostEntry = NULL;\r
1762                 }\r
1763                 else\r
1764                 {\r
1765                         // アドレスを取得\r
1766                         SetTaskMsg(MSGJPN016, DomainName);\r
1767                         pHostEntry = do_gethostbyname(host, HostEntry, MAXGETHOSTSTRUCT, CancelCheckWork);\r
1768                 }\r
1769 \r
1770                 if(pHostEntry != NULL)\r
1771                 {\r
1772                         memcpy((char *)&CurSockAddr.sin_addr, pHostEntry->h_addr, pHostEntry->h_length);\r
1773                         SetTaskMsg(MSGJPN017, PreMsg, DomainName, inet_ntoa(CurSockAddr.sin_addr), ntohs(CurSockAddr.sin_port));\r
1774                 }\r
1775                 else\r
1776                 {\r
1777                         if((Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER))\r
1778                         {\r
1779                                 UseIPadrs = NO;\r
1780                                 SetTaskMsg(MSGJPN018, PreMsg, DomainName, ntohs(CurSockAddr.sin_port));\r
1781                         }\r
1782                         else\r
1783                         {\r
1784                                 SetTaskMsg(MSGJPN019, host);\r
1785                                 return(INVALID_SOCKET);\r
1786                         }\r
1787                 }\r
1788         }\r
1789         else\r
1790                 SetTaskMsg(MSGJPN020, PreMsg, inet_ntoa(CurSockAddr.sin_addr), ntohs(CurSockAddr.sin_port));\r
1791 \r
1792         if((Fwall == FWALL_SOCKS4) || (Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER))\r
1793         {\r
1794                 // SOCKSを使う\r
1795                 // SOCKSに接続する準備\r
1796                 if(Fwall == FWALL_SOCKS4)\r
1797                 {\r
1798                         Socks4Cmd.Ver = SOCKS4_VER;\r
1799                         Socks4Cmd.Cmd = SOCKS4_CMD_CONNECT;\r
1800                         Socks4Cmd.Port = CurSockAddr.sin_port;\r
1801                         Socks4Cmd.AdrsInt = CurSockAddr.sin_addr.s_addr;\r
1802                         strcpy(Socks4Cmd.UserID, FwallUser);\r
1803                         Len = offsetof(SOCKS4CMD, UserID) + strlen(FwallUser) + 1;\r
1804                 }\r
1805                 else\r
1806                 {\r
1807                         Len = Socks5MakeCmdPacket(&Socks5Cmd, SOCKS5_CMD_CONNECT, UseIPadrs, CurSockAddr.sin_addr.s_addr, DomainName, CurSockAddr.sin_port);\r
1808                 }\r
1809 \r
1810                 memset(&SocksSockAddr, 0, sizeof(SocksSockAddr));\r
1811                 if((SocksSockAddr.sin_addr.s_addr = inet_addr(FwallHost)) == INADDR_NONE)\r
1812                 {\r
1813                         if((pHostEntry = do_gethostbyname(FwallHost, HostEntry, MAXGETHOSTSTRUCT, CancelCheckWork)) != NULL)\r
1814                                 memcpy((char *)&SocksSockAddr.sin_addr, pHostEntry->h_addr, pHostEntry->h_length);\r
1815                         else\r
1816                         {\r
1817                                 SetTaskMsg(MSGJPN021, FwallHost);\r
1818                                 return INVALID_SOCKET;\r
1819                         }\r
1820                 }\r
1821                 SocksSockAddr.sin_port = htons((u_short)FwallPort);\r
1822                 SocksSockAddr.sin_family = AF_INET;\r
1823                 SetTaskMsg(MSGJPN022, inet_ntoa(SocksSockAddr.sin_addr), ntohs(SocksSockAddr.sin_port));\r
1824                 // connectで接続する先はSOCKSサーバ\r
1825                 memcpy(&saSockAddr, &SocksSockAddr, sizeof(SocksSockAddr));\r
1826         }\r
1827         else\r
1828         {\r
1829                 // connectで接続するのは接続先のホスト\r
1830                 memcpy(&saSockAddr, &CurSockAddr, sizeof(CurSockAddr));\r
1831         }\r
1832 \r
1833         /////////////\r
1834         // 接続実行\r
1835         /////////////\r
1836 \r
1837         if((sSocket = do_socket(AF_INET, SOCK_STREAM, TCP_PORT)) != INVALID_SOCKET)\r
1838         {\r
1839                 if(do_connect(sSocket, (struct sockaddr *)&saSockAddr, sizeof(saSockAddr), CancelCheckWork) != SOCKET_ERROR)\r
1840                 {\r
1841                         if(Fwall == FWALL_SOCKS4)\r
1842                         {\r
1843                                 Socks4Reply.Result = -1;\r
1844                                 if((SocksSendCmd(sSocket, &Socks4Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
1845                                    (Socks4GetCmdReply(sSocket, &Socks4Reply) != FFFTP_SUCCESS) || \r
1846                                    (Socks4Reply.Result != SOCKS4_RES_OK))\r
1847                                 {\r
1848                                         SetTaskMsg(MSGJPN023, Socks4Reply.Result);\r
1849                                         DoClose(sSocket);\r
1850                                         sSocket = INVALID_SOCKET;\r
1851                                 }\r
1852                         }\r
1853                         else if((Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER))\r
1854                         {\r
1855                                 if(Socks5SelMethod(sSocket, CancelCheckWork) == FFFTP_FAIL)\r
1856                                 {\r
1857                                         DoClose(sSocket);\r
1858                                         sSocket = INVALID_SOCKET;\r
1859                                 }\r
1860 \r
1861                                 Socks5Reply.Result = -1;\r
1862                                 if((SocksSendCmd(sSocket, &Socks5Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
1863                                    (Socks5GetCmdReply(sSocket, &Socks5Reply) != FFFTP_SUCCESS) || \r
1864                                    (Socks5Reply.Result != SOCKS5_RES_OK))\r
1865                                 {\r
1866                                         SetTaskMsg(MSGJPN024, Socks5Reply.Result);\r
1867                                         DoClose(sSocket);\r
1868                                         sSocket = INVALID_SOCKET;\r
1869                                 }\r
1870 \r
1871                         }\r
1872 \r
1873                         if(sSocket != INVALID_SOCKET)\r
1874                                 SetTaskMsg(MSGJPN025);\r
1875                 }\r
1876                 else\r
1877                 {\r
1878 //#pragma aaa\r
1879                         SetTaskMsg(MSGJPN026/*"接続できません(2) %x", sSocket*/);\r
1880                         DoClose(sSocket);\r
1881                         sSocket = INVALID_SOCKET;\r
1882                 }\r
1883         }\r
1884         else\r
1885                 SetTaskMsg(MSGJPN027);\r
1886 \r
1887         return(sSocket);\r
1888 }\r
1889 \r
1890 \r
1891 /*----- リッスンソケットを取得 ------------------------------------------------\r
1892 *\r
1893 *       Parameter\r
1894 *               SOCKET ctrl_skt : コントロールソケット\r
1895 *\r
1896 *       Return Value\r
1897 *               SOCKET リッスンソケット\r
1898 *----------------------------------------------------------------------------*/\r
1899 \r
1900 SOCKET GetFTPListenSocket(SOCKET ctrl_skt, int *CancelCheckWork)\r
1901 {\r
1902     SOCKET listen_skt;\r
1903     int iLength;\r
1904     char *a,*p;\r
1905         struct sockaddr_in saCtrlAddr;\r
1906         struct sockaddr_in saTmpAddr;\r
1907         SOCKS4CMD Socks4Cmd;\r
1908         SOCKS4REPLY Socks4Reply;\r
1909         SOCKS5REQUEST Socks5Cmd;\r
1910         SOCKS5REPLY Socks5Reply;\r
1911 \r
1912         int Len;\r
1913         int Fwall;\r
1914 \r
1915         Fwall = FWALL_NONE;\r
1916         if(AskHostFireWall() == YES)\r
1917                 Fwall = FwallType;\r
1918 \r
1919         if((listen_skt = do_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != INVALID_SOCKET)\r
1920         {\r
1921                 if(Fwall == FWALL_SOCKS4)\r
1922                 {\r
1923                         /*===== SOCKS4を使う =====*/\r
1924                         DoPrintf("Use SOCKS4 BIND");\r
1925                         if(do_connect(listen_skt, (struct sockaddr *)&SocksSockAddr, sizeof(SocksSockAddr), CancelCheckWork) != SOCKET_ERROR)\r
1926                         {\r
1927                                 Socks4Cmd.Ver = SOCKS4_VER;\r
1928                                 Socks4Cmd.Cmd = SOCKS4_CMD_BIND;\r
1929                                 Socks4Cmd.Port = CurSockAddr.sin_port;\r
1930                                 Socks4Cmd.AdrsInt = CurSockAddr.sin_addr.s_addr;\r
1931                                 strcpy(Socks4Cmd.UserID, FwallUser);\r
1932                                 Len = offsetof(SOCKS4CMD, UserID) + strlen(FwallUser) + 1;\r
1933 \r
1934                                 Socks4Reply.Result = -1;\r
1935                                 if((SocksSendCmd(listen_skt, &Socks4Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
1936                                    (Socks4GetCmdReply(listen_skt, &Socks4Reply) != FFFTP_SUCCESS) || \r
1937                                    (Socks4Reply.Result != SOCKS4_RES_OK))\r
1938                                 {\r
1939                                         SetTaskMsg(MSGJPN028, Socks4Reply.Result);\r
1940                                         DoClose(listen_skt);\r
1941                                         listen_skt = INVALID_SOCKET;\r
1942                                 }\r
1943 \r
1944                                 if(Socks4Reply.AdrsInt == 0)\r
1945                                         Socks4Reply.AdrsInt = SocksSockAddr.sin_addr.s_addr;\r
1946 \r
1947                                 a = (char *)&Socks4Reply.AdrsInt;\r
1948                                 p = (char *)&Socks4Reply.Port;\r
1949                         }\r
1950                 }\r
1951                 else if((Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER))\r
1952                 {\r
1953                         /*===== SOCKS5を使う =====*/\r
1954                         DoPrintf("Use SOCKS5 BIND");\r
1955                         if(do_connect(listen_skt, (struct sockaddr *)&SocksSockAddr, sizeof(SocksSockAddr), CancelCheckWork) != SOCKET_ERROR)\r
1956                         {\r
1957                                 if(Socks5SelMethod(listen_skt, CancelCheckWork) == FFFTP_FAIL)\r
1958                                 {\r
1959                                         DoClose(listen_skt);\r
1960                                         listen_skt = INVALID_SOCKET;\r
1961                                         return(listen_skt);\r
1962                                 }\r
1963 \r
1964                                 Len = Socks5MakeCmdPacket(&Socks5Cmd, SOCKS5_CMD_BIND, UseIPadrs, CurSockAddr.sin_addr.s_addr, DomainName, CurSockAddr.sin_port);\r
1965 \r
1966                                 Socks5Reply.Result = -1;\r
1967                                 if((SocksSendCmd(listen_skt, &Socks5Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
1968                                    (Socks5GetCmdReply(listen_skt, &Socks5Reply) != FFFTP_SUCCESS) || \r
1969                                    (Socks5Reply.Result != SOCKS5_RES_OK))\r
1970                                 {\r
1971                                         SetTaskMsg(MSGJPN029, Socks5Reply.Result);\r
1972                                         DoClose(listen_skt);\r
1973                                         listen_skt = INVALID_SOCKET;\r
1974                                 }\r
1975 \r
1976                                 if(Socks5Reply.AdrsInt == 0)\r
1977                                         Socks5Reply.AdrsInt = SocksSockAddr.sin_addr.s_addr;\r
1978 \r
1979                                 a = (char *)&Socks5Reply.AdrsInt;\r
1980                                 p = (char *)&Socks5Reply.Port;\r
1981                         }\r
1982                 }\r
1983                 else\r
1984                 {\r
1985                         /*===== SOCKSを使わない =====*/\r
1986                         DoPrintf("Use normal BIND");\r
1987                         saCtrlAddr.sin_port = htons(0);\r
1988                         saCtrlAddr.sin_family = AF_INET;\r
1989                         saCtrlAddr.sin_addr.s_addr = 0;\r
1990 \r
1991                         if(bind(listen_skt, (struct sockaddr *)&saCtrlAddr, sizeof(struct sockaddr)) != SOCKET_ERROR)\r
1992                         {\r
1993                                 iLength = sizeof(saCtrlAddr);\r
1994                                 if(getsockname(listen_skt, (struct sockaddr *)&saCtrlAddr, &iLength) != SOCKET_ERROR)\r
1995                                 {\r
1996                                         if(do_listen(listen_skt, 1) == 0)\r
1997                                         {\r
1998                                                 iLength = sizeof(saTmpAddr);\r
1999                                                 if(getsockname(ctrl_skt, (struct sockaddr *)&saTmpAddr, &iLength) == SOCKET_ERROR)\r
2000                                                         ReportWSError("getsockname", WSAGetLastError());\r
2001 \r
2002                                                 a = (char *)&saTmpAddr.sin_addr;\r
2003                                                 p = (char *)&saCtrlAddr.sin_port;\r
2004                                         }\r
2005                                         else\r
2006                                         {\r
2007                                                 ReportWSError("listen", WSAGetLastError());\r
2008                                                 do_closesocket(listen_skt);\r
2009                                                 listen_skt = INVALID_SOCKET;\r
2010                                         }\r
2011                                 }\r
2012                                 else\r
2013                                 {\r
2014                                         ReportWSError("getsockname", WSAGetLastError());\r
2015                                         do_closesocket(listen_skt);\r
2016                                         listen_skt = INVALID_SOCKET;\r
2017                                 }\r
2018                         }\r
2019                         else\r
2020                         {\r
2021                                 ReportWSError("bind", WSAGetLastError());\r
2022                                 do_closesocket(listen_skt);\r
2023                                 listen_skt = INVALID_SOCKET;\r
2024                         }\r
2025 \r
2026                         if(listen_skt == INVALID_SOCKET)\r
2027                                 SetTaskMsg(MSGJPN030);\r
2028                 }\r
2029         }\r
2030         else\r
2031                 ReportWSError("socket create", WSAGetLastError());\r
2032 \r
2033         if(listen_skt != INVALID_SOCKET)\r
2034         {\r
2035 #define  UC(b)  (((int)b)&0xff)\r
2036                 if((command(ctrl_skt,NULL, &CancelFlg, "PORT %d,%d,%d,%d,%d,%d",\r
2037                                 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),\r
2038                                 UC(p[0]), UC(p[1])) / 100) != FTP_COMPLETE)\r
2039                 {\r
2040                         SetTaskMsg(MSGJPN031);\r
2041                         do_closesocket(listen_skt);\r
2042                         listen_skt = INVALID_SOCKET;\r
2043                 }\r
2044 //              else\r
2045 //                      DoPrintf("Skt=%u : listener %s port %u",listen_skt,inet_ntoa(saCtrlAddr.sin_addr),ntohs(saCtrlAddr.sin_port));\r
2046         }\r
2047 \r
2048         return(listen_skt);\r
2049 }\r
2050 \r
2051 \r
2052 /*----- ホストへ接続処理中かどうかを返す---------------------------------------\r
2053 *\r
2054 *       Parameter\r
2055 *               なし\r
2056 *\r
2057 *       Return Value\r
2058 *               int ステータス\r
2059 *                       YES/NO\r
2060 *----------------------------------------------------------------------------*/\r
2061 \r
2062 int AskTryingConnect(void)\r
2063 {\r
2064         return(TryConnect);\r
2065 }\r
2066 \r
2067 \r
2068 #if 0\r
2069 ///*----- ブロッキングコールのフックコールバック --------------------------------\r
2070 //*\r
2071 //*     Parameter\r
2072 //*             なし\r
2073 //*\r
2074 //*     Return Value\r
2075 //*             BOOL FALSE\r
2076 //*----------------------------------------------------------------------------*/\r
2077 //\r
2078 //static BOOL CALLBACK BlkHookFnc(void)\r
2079 //{\r
2080 //      BackgrndMessageProc();\r
2081 //\r
2082 //      if(CancelFlg == YES)\r
2083 //      {\r
2084 //              SetTaskMsg(MSGJPN032);\r
2085 //              WSACancelBlockingCall();\r
2086 //              CancelFlg = NO;\r
2087 //      }\r
2088 //      return(FALSE);\r
2089 //}\r
2090 #endif\r
2091 \r
2092 \r
2093 \r
2094 /*----- SOCKS5のコマンドパケットを作成する ------------------------------------\r
2095 *\r
2096 *       Parameter\r
2097 *               SOCKS5REQUEST *Packet : パケットを作成するワーク\r
2098 *               char Cmd : コマンド\r
2099 *               int ValidIP : IPアドレスを使うかどうか(YES/NO)\r
2100 *               ulong IP : IPアドレス\r
2101 *               char *Host : ホスト名\r
2102 *               ushort Port : ポート\r
2103 *\r
2104 *       Return Value\r
2105 *               int コマンドパケットの長さ\r
2106 *----------------------------------------------------------------------------*/\r
2107 \r
2108 static int Socks5MakeCmdPacket(SOCKS5REQUEST *Packet, char Cmd, int ValidIP, ulong IP, char *Host, ushort Port)\r
2109 {\r
2110         uchar *Pos;\r
2111         int Len;\r
2112         int TotalLen;\r
2113 \r
2114         Pos = (uchar *)Packet;\r
2115         Pos += SOCKS5REQUEST_SIZE;\r
2116         TotalLen = SOCKS5REQUEST_SIZE + 2;      /* +2はポートの分 */\r
2117 \r
2118         Packet->Ver = SOCKS5_VER;\r
2119         Packet->Cmd = Cmd;\r
2120         Packet->Rsv = 0;\r
2121         if(ValidIP == YES)\r
2122         {\r
2123                 /* IPアドレスを指定 */\r
2124                 Packet->Type = SOCKS5_ADRS_IPV4;\r
2125                 *((ulong *)Pos) = IP;\r
2126                 Pos += 4;\r
2127                 TotalLen += 4;\r
2128         }\r
2129         else\r
2130         {\r
2131                 /* ホスト名を指定 */\r
2132                 Packet->Type = SOCKS5_ADRS_NAME;\r
2133                 Len = strlen(Host);\r
2134                 *Pos++ = Len;\r
2135                 strcpy(Pos, Host);\r
2136                 Pos += Len;\r
2137                 TotalLen += Len + 1;\r
2138         }\r
2139         *((ushort *)Pos) = Port;\r
2140 \r
2141         return(TotalLen);\r
2142 }\r
2143 \r
2144 \r
2145 /*----- SOCKSのコマンドを送る -------------------------------------------------\r
2146 *\r
2147 *       Parameter\r
2148 *               SOCKET Socket : ソケット\r
2149 *               void *Data : 送るデータ\r
2150 *               int Size : サイズ\r
2151 *\r
2152 *       Return Value\r
2153 *               int ステータス (FFFTP_SUCCESS/FFFTP_FAIL)\r
2154 *----------------------------------------------------------------------------*/\r
2155 \r
2156 static int SocksSendCmd(SOCKET Socket, void *Data, int Size, int *CancelCheckWork)\r
2157 {\r
2158         int Ret;\r
2159 \r
2160         Ret = SendData(Socket, (char *)Data, Size, 0, CancelCheckWork);\r
2161 \r
2162         if(Ret != FFFTP_SUCCESS)\r
2163                 SetTaskMsg(MSGJPN033, *((short *)Data));\r
2164 \r
2165         return(Ret);\r
2166 }\r
2167 \r
2168 \r
2169 /*----- SOCKS5のコマンドに対するリプライパケットを受信する --------------------\r
2170 *\r
2171 *       Parameter\r
2172 *               SOCKET Socket : ソケット\r
2173 *               SOCKS5REPLY *Packet : パケット\r
2174 *\r
2175 *       Return Value\r
2176 *               int ステータス (FFFTP_SUCCESS/FFFTP_FAIL)\r
2177 *----------------------------------------------------------------------------*/\r
2178 \r
2179 static int Socks5GetCmdReply(SOCKET Socket, SOCKS5REPLY *Packet)\r
2180 {\r
2181         uchar *Pos;\r
2182         int Len;\r
2183         int Ret;\r
2184 \r
2185         Pos = (uchar *)Packet;\r
2186         Pos += SOCKS5REPLY_SIZE;\r
2187 \r
2188         if((Ret = ReadNchar(Socket, (char *)Packet, SOCKS5REPLY_SIZE, &CancelFlg)) == FFFTP_SUCCESS)\r
2189         {\r
2190                 if(Packet->Type == SOCKS5_ADRS_IPV4)\r
2191                         Len = 4 + 2;\r
2192                 else if(Packet->Type == SOCKS5_ADRS_IPV6)\r
2193                         Len = 6 + 2;\r
2194                 else\r
2195                 {\r
2196                         if((Ret = ReadNchar(Socket, (char *)Pos, 1, &CancelFlg)) == FFFTP_SUCCESS)\r
2197                         {\r
2198                                 Len = *Pos + 2;\r
2199                                 Pos++;\r
2200                         }\r
2201                 }\r
2202 \r
2203                 if(Ret == FFFTP_SUCCESS)\r
2204                         Ret = ReadNchar(Socket, (char *)Pos, Len, &CancelFlg);\r
2205         }\r
2206 \r
2207         if(Ret != FFFTP_SUCCESS)\r
2208                 SetTaskMsg(MSGJPN034);\r
2209 \r
2210         return(Ret);\r
2211 }\r
2212 \r
2213 \r
2214 /*----- SOCKS4のコマンドに対するリプライパケットを受信する --------------------\r
2215 *\r
2216 *       Parameter\r
2217 *               SOCKET Socket : ソケット\r
2218 *               SOCKS5REPLY *Packet : パケット\r
2219 *\r
2220 *       Return Value\r
2221 *               int ステータス (FFFTP_SUCCESS/FFFTP_FAIL)\r
2222 *----------------------------------------------------------------------------*/\r
2223 \r
2224 static int Socks4GetCmdReply(SOCKET Socket, SOCKS4REPLY *Packet)\r
2225 {\r
2226         int Ret;\r
2227 \r
2228         Ret = ReadNchar(Socket, (char *)Packet, SOCKS4REPLY_SIZE, &CancelFlg);\r
2229 \r
2230         if(Ret != FFFTP_SUCCESS)\r
2231                 DoPrintf(MSGJPN035);\r
2232 \r
2233         return(Ret);\r
2234 }\r
2235 \r
2236 \r
2237 /*----- SOCKS5の認証を行う ----------------------------------------------------\r
2238 *\r
2239 *       Parameter\r
2240 *               SOCKET Socket : ソケット\r
2241 *\r
2242 *       Return Value\r
2243 *               int ステータス (FFFTP_SUCCESS/FFFTP_FAIL)\r
2244 *----------------------------------------------------------------------------*/\r
2245 \r
2246 static int Socks5SelMethod(SOCKET Socket, int *CancelCheckWork)\r
2247 {\r
2248         int Ret;\r
2249         SOCKS5METHODREQUEST Socks5Method;\r
2250         SOCKS5METHODREPLY Socks5MethodReply;\r
2251         SOCKS5USERPASSSTATUS Socks5Status;\r
2252         char Buf[USER_NAME_LEN + PASSWORD_LEN + 4];\r
2253         int Len;\r
2254         int Len2;\r
2255 \r
2256         Ret = FFFTP_SUCCESS;\r
2257         Socks5Method.Ver = SOCKS5_VER;\r
2258         Socks5Method.Num = 1;\r
2259         if(FwallType == FWALL_SOCKS5_NOAUTH)\r
2260                 Socks5Method.Methods[0] = SOCKS5_AUTH_NONE;\r
2261         else\r
2262                 Socks5Method.Methods[0] = SOCKS5_AUTH_USER;\r
2263 \r
2264         if((SocksSendCmd(Socket, &Socks5Method, SOCKS5METHODREQUEST_SIZE, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2265            (ReadNchar(Socket, (char *)&Socks5MethodReply, SOCKS5METHODREPLY_SIZE, &CancelFlg) != FFFTP_SUCCESS) ||\r
2266            (Socks5MethodReply.Method == (uchar)0xFF))\r
2267         {\r
2268                 SetTaskMsg(MSGJPN036);\r
2269                 Ret = FFFTP_FAIL;\r
2270         }\r
2271         else if(Socks5MethodReply.Method == SOCKS5_AUTH_USER)\r
2272         {\r
2273                 DoPrintf("SOCKS5 User/Pass Authentication");\r
2274                 Buf[0] = SOCKS5_USERAUTH_VER;\r
2275                 Len = strlen(FwallUser);\r
2276                 Len2 = strlen(FwallPass);\r
2277                 Buf[1] = Len;\r
2278                 strcpy(Buf+2, FwallUser);\r
2279                 Buf[2 + Len] = Len2;\r
2280                 strcpy(Buf+3+Len, FwallPass);\r
2281 \r
2282                 if((SocksSendCmd(Socket, &Buf, Len+Len2+3, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2283                    (ReadNchar(Socket, (char *)&Socks5Status, SOCKS5USERPASSSTATUS_SIZE, &CancelFlg) != FFFTP_SUCCESS) ||\r
2284                    (Socks5Status.Status != 0))\r
2285                 {\r
2286                         SetTaskMsg(MSGJPN037);\r
2287                         Ret = FFFTP_FAIL;\r
2288                 }\r
2289         }\r
2290         else\r
2291                 DoPrintf("SOCKS5 No Authentication");\r
2292 \r
2293         return(Ret);\r
2294 }\r
2295 \r
2296 \r
2297 /*----- SOCKSのBINDの第2リプライメッセージを受け取る -------------------------\r
2298 *\r
2299 *       Parameter\r
2300 *               SOCKET Socket : ソケット\r
2301 *               SOCKET *Data : データソケットを返すワーク\r
2302 *\r
2303 *       Return Value\r
2304 *               int ステータス (FFFTP_SUCCESS/FFFTP_FAIL)\r
2305 *----------------------------------------------------------------------------*/\r
2306 \r
2307 int SocksGet2ndBindReply(SOCKET Socket, SOCKET *Data)\r
2308 {\r
2309         int Ret;\r
2310         char Buf[300];\r
2311 \r
2312         Ret = FFFTP_FAIL;\r
2313         if((AskHostFireWall() == YES) && (FwallType == FWALL_SOCKS4))\r
2314         {\r
2315                 Socks4GetCmdReply(Socket, (SOCKS4REPLY *)Buf);\r
2316                 *Data = Socket;\r
2317                 Ret = FFFTP_SUCCESS;\r
2318         }\r
2319         else if((AskHostFireWall() == YES) &&\r
2320                         ((FwallType == FWALL_SOCKS5_NOAUTH) || (FwallType == FWALL_SOCKS5_USER)))\r
2321         {\r
2322                 Socks5GetCmdReply(Socket, (SOCKS5REPLY *)Buf);\r
2323                 *Data = Socket;\r
2324                 Ret = FFFTP_SUCCESS;\r
2325         }\r
2326         return(Ret);\r
2327 }\r
2328 \r
2329 \r
2330 \r
2331 // 暗号化通信対応\r
2332 int AskCryptMode(void)\r
2333 {\r
2334         return(CurHost.CryptMode);\r
2335 }\r
2336 \r
2337 int AskUseNoEncryption(void)\r
2338 {\r
2339         return(CurHost.UseNoEncryption);\r
2340 }\r
2341 \r
2342 int AskUseFTPES(void)\r
2343 {\r
2344         return(CurHost.UseFTPES);\r
2345 }\r
2346 \r
2347 int AskUseFTPIS(void)\r
2348 {\r
2349         return(CurHost.UseFTPIS);\r
2350 }\r
2351 \r
2352 int AskUseSFTP(void)\r
2353 {\r
2354         return(CurHost.UseSFTP);\r
2355 }\r
2356 \r
2357 char *AskPrivateKey(void)\r
2358 {\r
2359         return(CurHost.PrivateKey);\r
2360 }\r
2361 \r
2362 // 同時接続対応\r
2363 int AskMaxThreadCount(void)\r
2364 {\r
2365         return(CurHost.MaxThreadCount);\r
2366 }\r
2367 \r
2368 // FEAT対応\r
2369 int AskHostFeature(void)\r
2370 {\r
2371         return(CurHost.Feature);\r
2372 }\r
2373 \r
2374 // MLSD対応\r
2375 int AskUseMLSD(void)\r
2376 {\r
2377         return(CurHost.UseMLSD);\r
2378 }\r
2379 \r
2380 // IPv6対応\r
2381 int AskUseIPv6(void)\r
2382 {\r
2383         return(CurHost.UseIPv6);\r
2384 }\r
2385 \r