OSDN Git Service

Enhance the security of routines for changing a master password.
[ffftp/ffftp.git] / registry.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 // VC 8.0(2005)以上でのみ rand_s を利用可能\r
31 #if 1400 <= _MSC_VER\r
32 //#define _CRT_RAND_S\r
33 #endif\r
34 \r
35 #define STRICT\r
36 // IPv6対応\r
37 #include <winsock2.h>\r
38 #include <windows.h>\r
39 #include <stdio.h>\r
40 #include <stdlib.h>\r
41 #include <time.h>\r
42 #include <string.h>\r
43 #include <windowsx.h>\r
44 #include <process.h>\r
45 \r
46 \r
47 #include "common.h"\r
48 #include "resource.h"\r
49 #include "sample.h"\r
50 #include "sha.h"\r
51 #include "aes.h"\r
52 \r
53 \r
54 /*===== プロトタイプ =====*/\r
55 \r
56 static void SaveStr(HKEY hKey, char *Key, char *Str, char *DefaultStr);\r
57 static void SaveIntNum(HKEY hKey, char *Key, int Num, int DefaultNum);\r
58 static void MakeFontData(LOGFONT Font, HFONT hFont, char *Buf);\r
59 static int RestoreFontData(char *Str, LOGFONT *Font);\r
60 \r
61 static void EncodePassword(char *Str, char *Buf);\r
62 static void EncodePasswordOriginal(char *Str, char *Buf);\r
63 static void EncodePassword2(char *Str, char *Buf, const char *Key);\r
64 static void EncodePassword3(char *Str, char *Buf, const char *Key);\r
65 \r
66 static void DecodePassword(char *Str, char *Buf);\r
67 static void DecodePasswordOriginal(char *Str, char *Buf);\r
68 static void DecodePassword2(char *Str, char *Buf, const char *Key);\r
69 static void DecodePassword3(char *Str, char *Buf, const char *Key);\r
70 static int CreateAesKey(unsigned char *AesKey, const char* Key);\r
71 \r
72 static void SetRegType(int Type);\r
73 static int OpenReg(char *Name, void **Handle);\r
74 static int CreateReg(char *Name, void **Handle);\r
75 static int CloseReg(void *Handle);\r
76 static int OpenSubKey(void *Parent, char *Name, void **Handle);\r
77 static int CreateSubKey(void *Parent, char *Name, void **Handle);\r
78 static int CloseSubKey(void *Handle);\r
79 static int DeleteSubKey(void *Handle, char *Name);\r
80 static int DeleteValue(void *Handle, char *Name);\r
81 static int ReadIntValueFromReg(void *Handle, char *Name, int *Value);\r
82 static int WriteIntValueToReg(void *Handle, char *Name, int Value);\r
83 static int ReadStringFromReg(void *Handle, char *Name, char *Str, DWORD Size);\r
84 static int WriteStringToReg(void *Handle, char *Name, char *Str);\r
85 static int ReadMultiStringFromReg(void *Handle, char *Name, char *Str, DWORD Size);\r
86 static int WriteMultiStringToReg(void *Handle, char *Name, char *Str);\r
87 static int ReadBinaryFromReg(void *Handle, char *Name, void *Bin, DWORD Size);\r
88 static int WriteBinaryToReg(void *Handle, char *Name, void *Bin, int Len);\r
89 // 暗号化通信対応\r
90 static int StrCatOut(char *Src, int Len, char *Dst);\r
91 static int StrReadIn(char *Src, int Max, char *Dst);\r
92 \r
93 int CheckPasswordValidity( char* Password, int length, const char* HashStr );\r
94 void CreatePasswordHash( char* Password, int length, char* HashStr );\r
95 void SetHashSalt( DWORD salt );\r
96 \r
97 DWORD GetRandamDWRODValue(void);\r
98 \r
99 /* 2010.01.30 genta 追加 */\r
100 static char SecretKey[FMAX_PATH+1];\r
101 static int SecretKeyLength;\r
102 static int IsMasterPasswordError = PASSWORD_OK;\r
103 \r
104 static int IsRndSourceInit = 0;\r
105 static ulong RndSource[9];\r
106 \r
107 // UTF-8対応\r
108 static int IniKanjiCode = KANJI_NOCNV;\r
109 \r
110 /*===== 外部参照 =====*/\r
111 \r
112 /* 設定値 */\r
113 extern int WinPosX;\r
114 extern int WinPosY;\r
115 extern int WinWidth;\r
116 extern int WinHeight;\r
117 extern int LocalWidth;\r
118 extern int TaskHeight;\r
119 extern int LocalTabWidth[4];\r
120 extern int RemoteTabWidth[6];\r
121 extern char UserMailAdrs[USER_MAIL_LEN+1];\r
122 extern char ViewerName[VIEWERS][FMAX_PATH+1];\r
123 extern HFONT ListFont;\r
124 extern LOGFONT ListLogFont;\r
125 extern int LocalFileSort;\r
126 extern int LocalDirSort;\r
127 extern int RemoteFileSort;\r
128 extern int RemoteDirSort;\r
129 extern int TransMode;\r
130 extern int ConnectOnStart;\r
131 extern int DebugConsole;\r
132 extern int SaveWinPos;\r
133 extern char AsciiExt[ASCII_EXT_LEN+1];\r
134 extern int RecvMode;\r
135 extern int SendMode;\r
136 extern int MoveMode;\r
137 extern int ListType;\r
138 extern int CacheEntry;\r
139 extern int CacheSave;\r
140 extern char DefaultLocalPath[FMAX_PATH+1];\r
141 extern int SaveTimeStamp;\r
142 extern int FindMode;\r
143 extern int DotFile;\r
144 extern int DclickOpen;\r
145 extern SOUNDFILE Sound[SOUND_TYPES];\r
146 extern int FnameCnv;\r
147 extern int ConnectAndSet;\r
148 extern int TimeOut;\r
149 extern int RmEOF;\r
150 extern int RegType;\r
151 extern char FwallHost[HOST_ADRS_LEN+1];\r
152 extern char FwallUser[USER_NAME_LEN+1];\r
153 extern char FwallPass[PASSWORD_LEN+1];\r
154 extern int FwallPort;\r
155 extern int FwallType;\r
156 extern int FwallDefault;\r
157 extern int FwallSecurity;\r
158 extern int FwallResolv;\r
159 extern int FwallLower;\r
160 extern int FwallDelimiter;\r
161 extern int PasvDefault;\r
162 extern char MirrorNoTrn[MIRROR_LEN+1];\r
163 extern char MirrorNoDel[MIRROR_LEN+1];\r
164 extern int MirrorFnameCnv;\r
165 //extern int MirrorFolderCnv;\r
166 extern int RasClose;\r
167 extern int RasCloseNotify;\r
168 extern int FileHist;\r
169 extern char DefAttrList[DEFATTRLIST_LEN+1];\r
170 extern SIZE HostDlgSize;\r
171 extern SIZE BmarkDlgSize;\r
172 extern SIZE MirrorDlgSize;\r
173 extern int Sizing;\r
174 extern int SortSave;\r
175 extern char TmpPath[FMAX_PATH+1];\r
176 extern int QuickAnonymous;\r
177 extern int PassToHist;\r
178 extern int VaxSemicolon;\r
179 extern int SendQuit;\r
180 extern int NoRasControl;\r
181 extern int SuppressSave;\r
182 \r
183 extern int UpExistMode;\r
184 extern int ExistMode;\r
185 extern int DispIgnoreHide;\r
186 extern int DispDrives;\r
187 extern int MirUpDelNotify;\r
188 extern int MirDownDelNotify;\r
189 \r
190 extern int FolderAttr;\r
191 extern int FolderAttrNum;\r
192 \r
193 // 暗号化通信対応\r
194 extern BYTE CertificateCacheHash[MAX_CERT_CACHE_HASH][20];\r
195 extern BYTE SSLRootCAFileHash[20];\r
196 // ファイルアイコン表示対応\r
197 extern int DispFileIcon;\r
198 // ディレクトリ自動作成\r
199 extern int MakeAllDir;\r
200 \r
201 /*----- マスタパスワードの設定 ----------------------------------------------\r
202 *\r
203 *       Parameter\r
204 *               const char* Password : マスターパスワード\r
205 *\r
206 *       Return Value\r
207 *               なし\r
208 *----------------------------------------------------------------------------*/\r
209 void SetMasterPassword( const char* Password )\r
210 {\r
211         ZeroMemory( SecretKey, MAX_PASSWORD_LEN + 12 );\r
212         if( Password != NULL ){\r
213                 strncpy( SecretKey, Password, MAX_PASSWORD_LEN );\r
214         }\r
215         else {\r
216                 strcpy( SecretKey, DEFAULT_PASSWORD );\r
217         }\r
218         SecretKeyLength = strlen( SecretKey );\r
219         \r
220         /* 未検証なので,初期状態はOKにする (強制再設定→保存にを可能にする)*/\r
221         IsMasterPasswordError = PASSWORD_OK;\r
222 }\r
223 \r
224 // セキュリティ強化\r
225 void GetMasterPassword(char* Password)\r
226 {\r
227         strcpy(Password, SecretKey);\r
228 }\r
229 \r
230 /*----- マスタパスワードの状態取得 ----------------------------------------------\r
231 *\r
232 *       Parameter\r
233 *               なし\r
234 *\r
235 *       Return Value\r
236 *               PASSWORD_OK : OK\r
237 *               PASSWORD_UNMATCH : パスワード不一致\r
238 *               BAD_PASSWORD_HASH : パスワード確認失敗\r
239 *----------------------------------------------------------------------------*/\r
240 int GetMasterPasswordStatus(void)\r
241 {\r
242         return IsMasterPasswordError;\r
243 }\r
244 \r
245 /*----- レジストリ/INIファイルのマスターパスワードの検証を行う ------------\r
246 *\r
247 *       Parameter\r
248 *               なし\r
249 *\r
250 *       Return Value\r
251 *               \r
252 *----------------------------------------------------------------------------*/\r
253 \r
254 int ValidateMasterPassword(void)\r
255 {\r
256         void *hKey3;\r
257         int i;\r
258 \r
259         SetRegType(REGTYPE_INI);\r
260         if((i = OpenReg("FFFTP", &hKey3)) != FFFTP_SUCCESS)\r
261         {\r
262                 if(AskForceIni() == NO)\r
263                 {\r
264                         SetRegType(REGTYPE_REG);\r
265                         i = OpenReg("FFFTP", &hKey3);\r
266                 }\r
267         }\r
268         if(i == FFFTP_SUCCESS){\r
269                 char checkbuf[48];\r
270                 int salt = 0;\r
271 \r
272                 if( ReadIntValueFromReg(hKey3, "CredentialSalt", &salt)){\r
273                         SetHashSalt( salt );\r
274                 }\r
275                 if( ReadStringFromReg(hKey3, "CredentialCheck", checkbuf, sizeof( checkbuf )) == FFFTP_SUCCESS ){\r
276                         switch( CheckPasswordValidity( SecretKey, SecretKeyLength, checkbuf ) ){\r
277                         case 0: /* not match */\r
278                                 IsMasterPasswordError = PASSWORD_UNMATCH;\r
279                                 break;\r
280                         case 1: /* match */\r
281                                 IsMasterPasswordError = PASSWORD_OK;\r
282                                 break;\r
283                         case 2: /* invalid hash */\r
284                         default:\r
285                                 IsMasterPasswordError = BAD_PASSWORD_HASH;\r
286                                 break;\r
287                         }\r
288                 }\r
289                 CloseReg(hKey3);\r
290                 return YES;\r
291         }\r
292         return NO;\r
293 }\r
294 \r
295 /*----- レジストリ/INIファイルに設定値を保存 ---------------------------------\r
296 *\r
297 *       Parameter\r
298 *               なし\r
299 *\r
300 *       Return Value\r
301 *               なし\r
302 *----------------------------------------------------------------------------*/\r
303 \r
304 void SaveRegistry(void)\r
305 {\r
306         void *hKey3;\r
307         void *hKey4;\r
308         void *hKey5;\r
309         // 暗号化通信対応\r
310 //      char Str[FMAX_PATH+1];\r
311         char Str[PRIVATE_KEY_LEN*4+1];\r
312         char Buf[FMAX_PATH+1];\r
313         int i;\r
314         int n;\r
315         HOSTDATA DefaultHost;\r
316         HOSTDATA Host;\r
317         HISTORYDATA Hist;\r
318         HISTORYDATA DefaultHist;\r
319         \r
320         if( GetMasterPasswordStatus() == PASSWORD_UNMATCH ){\r
321                 /* 2010.01.30 genta: マスターパスワードが不一致の場合は不用意に上書きしない */\r
322                 return;\r
323         }\r
324 \r
325         SetRegType(RegType);\r
326         if(CreateReg("FFFTP", &hKey3) == FFFTP_SUCCESS)\r
327         {\r
328                 char buf[48];\r
329                 int salt = GetTickCount();\r
330         \r
331                 WriteIntValueToReg(hKey3, "Version", VER_NUM);\r
332                 WriteIntValueToReg(hKey3, "CredentialSalt", salt);\r
333                 \r
334                 SetHashSalt( salt );\r
335                 /* save password hash */\r
336                 CreatePasswordHash( SecretKey, SecretKeyLength, buf );\r
337                 WriteStringToReg(hKey3, "CredentialCheck", buf);\r
338 \r
339                 if(CreateSubKey(hKey3, "Options", &hKey4) == FFFTP_SUCCESS)\r
340                 {\r
341                         WriteIntValueToReg(hKey4, "NoSave", SuppressSave);\r
342 \r
343                         if(SuppressSave != YES)\r
344                         {\r
345                                 WriteIntValueToReg(hKey4, "WinPosX", WinPosX);\r
346                                 WriteIntValueToReg(hKey4, "WinPosY", WinPosY);\r
347                                 WriteIntValueToReg(hKey4, "WinWidth", WinWidth);\r
348                                 WriteIntValueToReg(hKey4, "WinHeight", WinHeight);\r
349                                 WriteIntValueToReg(hKey4, "LocalWidth", LocalWidth);\r
350                                 WriteIntValueToReg(hKey4, "TaskHeight", TaskHeight);\r
351                                 WriteBinaryToReg(hKey4, "LocalColm", LocalTabWidth, sizeof(LocalTabWidth));\r
352                                 WriteBinaryToReg(hKey4, "RemoteColm", RemoteTabWidth, sizeof(RemoteTabWidth));\r
353                                 WriteIntValueToReg(hKey4, "SwCmd", Sizing);\r
354 \r
355                                 WriteStringToReg(hKey4, "UserMail", UserMailAdrs);\r
356                                 WriteStringToReg(hKey4, "Viewer", ViewerName[0]);\r
357                                 WriteStringToReg(hKey4, "Viewer2", ViewerName[1]);\r
358                                 WriteStringToReg(hKey4, "Viewer3", ViewerName[2]);\r
359 \r
360                                 WriteIntValueToReg(hKey4, "TrType", TransMode);\r
361                                 WriteIntValueToReg(hKey4, "Recv", RecvMode);\r
362                                 WriteIntValueToReg(hKey4, "Send", SendMode);\r
363                                 WriteIntValueToReg(hKey4, "Move", MoveMode);\r
364                                 WriteStringToReg(hKey4, "Path", DefaultLocalPath);\r
365                                 WriteIntValueToReg(hKey4, "Time", SaveTimeStamp);\r
366                                 WriteIntValueToReg(hKey4, "EOF", RmEOF);\r
367                                 WriteIntValueToReg(hKey4, "Scolon", VaxSemicolon);\r
368 \r
369                                 WriteIntValueToReg(hKey4, "RecvEx", ExistMode);\r
370                                 WriteIntValueToReg(hKey4, "SendEx", UpExistMode);\r
371 \r
372                                 WriteIntValueToReg(hKey4, "LFsort", LocalFileSort);\r
373                                 WriteIntValueToReg(hKey4, "LDsort", LocalDirSort);\r
374                                 WriteIntValueToReg(hKey4, "RFsort", RemoteFileSort);\r
375                                 WriteIntValueToReg(hKey4, "RDsort", RemoteDirSort);\r
376                                 WriteIntValueToReg(hKey4, "SortSave", SortSave);\r
377 \r
378                                 WriteIntValueToReg(hKey4, "ListType", ListType);\r
379                                 WriteIntValueToReg(hKey4, "Cache", CacheEntry);\r
380                                 WriteIntValueToReg(hKey4, "CacheSave", CacheSave);\r
381                                 WriteIntValueToReg(hKey4, "DotFile", DotFile);\r
382                                 WriteIntValueToReg(hKey4, "Dclick", DclickOpen);\r
383 \r
384                                 WriteIntValueToReg(hKey4, "ConS", ConnectOnStart);\r
385                                 WriteIntValueToReg(hKey4, "OldDlg", ConnectAndSet);\r
386                                 WriteIntValueToReg(hKey4, "RasClose", RasClose);\r
387                                 WriteIntValueToReg(hKey4, "RasNotify", RasCloseNotify);\r
388                                 WriteIntValueToReg(hKey4, "Qanony", QuickAnonymous);\r
389                                 WriteIntValueToReg(hKey4, "PassHist", PassToHist);\r
390                                 WriteIntValueToReg(hKey4, "SendQuit", SendQuit);\r
391                                 WriteIntValueToReg(hKey4, "NoRas", NoRasControl);\r
392 \r
393                                 WriteIntValueToReg(hKey4, "Debug", DebugConsole);\r
394                                 WriteIntValueToReg(hKey4, "WinPos", SaveWinPos);\r
395                                 WriteIntValueToReg(hKey4, "RegExp", FindMode);\r
396                                 WriteIntValueToReg(hKey4, "Reg", RegType);\r
397 \r
398                                 WriteMultiStringToReg(hKey4, "AsciiFile", AsciiExt);\r
399                                 WriteIntValueToReg(hKey4, "LowUp", FnameCnv);\r
400                                 WriteIntValueToReg(hKey4, "Tout", TimeOut);\r
401 \r
402                                 WriteMultiStringToReg(hKey4, "NoTrn", MirrorNoTrn);\r
403                                 WriteMultiStringToReg(hKey4, "NoDel", MirrorNoDel);\r
404                                 WriteIntValueToReg(hKey4, "MirFile", MirrorFnameCnv);\r
405                                 WriteIntValueToReg(hKey4, "MirUNot", MirUpDelNotify);\r
406                                 WriteIntValueToReg(hKey4, "MirDNot", MirDownDelNotify);\r
407 \r
408                                 MakeFontData(ListLogFont, ListFont, Str);\r
409                                 WriteStringToReg(hKey4, "ListFont", Str);\r
410                                 WriteIntValueToReg(hKey4, "ListHide", DispIgnoreHide);\r
411                                 WriteIntValueToReg(hKey4, "ListDrv", DispDrives);\r
412 \r
413                                 WriteStringToReg(hKey4, "FwallHost", FwallHost);\r
414                                 WriteStringToReg(hKey4, "FwallUser", FwallUser);\r
415                                 EncodePassword(FwallPass, Str);\r
416                                 WriteStringToReg(hKey4, "FwallPass", Str);\r
417                                 WriteIntValueToReg(hKey4, "FwallPort", FwallPort);\r
418                                 WriteIntValueToReg(hKey4, "FwallType", FwallType);\r
419                                 WriteIntValueToReg(hKey4, "FwallDef", FwallDefault);\r
420                                 WriteIntValueToReg(hKey4, "FwallSec", FwallSecurity);\r
421                                 WriteIntValueToReg(hKey4, "PasvDef", PasvDefault);\r
422                                 WriteIntValueToReg(hKey4, "FwallRes", FwallResolv);\r
423                                 WriteIntValueToReg(hKey4, "FwallLow", FwallLower);\r
424                                 WriteIntValueToReg(hKey4, "FwallDel", FwallDelimiter);\r
425 \r
426                                 WriteIntValueToReg(hKey4, "SndConSw", Sound[SND_CONNECT].On);\r
427                                 WriteIntValueToReg(hKey4, "SndTrnSw", Sound[SND_TRANS].On);\r
428                                 WriteIntValueToReg(hKey4, "SndErrSw", Sound[SND_ERROR].On);\r
429                                 WriteStringToReg(hKey4, "SndCon", Sound[SND_CONNECT].Fname);\r
430                                 WriteStringToReg(hKey4, "SndTrn", Sound[SND_TRANS].Fname);\r
431                                 WriteStringToReg(hKey4, "SndErr", Sound[SND_ERROR].Fname);\r
432 \r
433                                 WriteMultiStringToReg(hKey4, "DefAttr", DefAttrList);\r
434 \r
435                                 // 環境依存の不具合対策\r
436 //                              GetTempPath(FMAX_PATH, Str);\r
437                                 GetAppTempPath(Str);\r
438                                 SetYenTail(Str);\r
439                                 SaveStr(hKey4, "Tmp", TmpPath, Str);\r
440 \r
441                                 WriteBinaryToReg(hKey4, "Hdlg", &HostDlgSize, sizeof(SIZE));\r
442                                 WriteBinaryToReg(hKey4, "Bdlg", &BmarkDlgSize, sizeof(SIZE));\r
443                                 WriteBinaryToReg(hKey4, "Mdlg", &MirrorDlgSize, sizeof(SIZE));\r
444 \r
445                                 WriteIntValueToReg(hKey4, "FAttrSw", FolderAttr);\r
446                                 WriteIntValueToReg(hKey4, "FAttr", FolderAttrNum);\r
447 \r
448                                 WriteIntValueToReg(hKey4, "HistNum", FileHist);\r
449 \r
450                                 /* Ver1.54a以前の形式のヒストリデータは削除 */\r
451                                 DeleteValue(hKey4, "Hist");\r
452 \r
453                                 /* ヒストリの設定を保存 */\r
454                                 CopyDefaultHistory(&DefaultHist);\r
455                                 n = 0;\r
456                                 for(i = AskHistoryNum(); i > 0; i--)\r
457                                 {\r
458                                         if(GetHistoryByNum(i-1, &Hist) == FFFTP_SUCCESS)\r
459                                         {\r
460                                                 sprintf(Str, "History%d", n);\r
461                                                 if(CreateSubKey(hKey4, Str, &hKey5) == FFFTP_SUCCESS)\r
462                                                 {\r
463                                                         SaveStr(hKey5, "HostAdrs", Hist.HostAdrs, DefaultHist.HostAdrs);\r
464                                                         SaveStr(hKey5, "UserName", Hist.UserName, DefaultHist.UserName);\r
465                                                         SaveStr(hKey5, "Account", Hist.Account, DefaultHist.Account);\r
466                                                         SaveStr(hKey5, "LocalDir", Hist.LocalInitDir, NULL);\r
467                                                         SaveStr(hKey5, "RemoteDir", Hist.RemoteInitDir, DefaultHist.RemoteInitDir);\r
468                                                         SaveStr(hKey5, "Chmod", Hist.ChmodCmd, DefaultHist.ChmodCmd);\r
469                                                         SaveStr(hKey5, "Nlst", Hist.LsName, DefaultHist.LsName);\r
470                                                         SaveStr(hKey5, "Init", Hist.InitCmd, DefaultHist.InitCmd);\r
471                                                         EncodePassword(Hist.PassWord, Str);\r
472                                                         SaveStr(hKey5, "Password", Str, DefaultHist.PassWord);\r
473                                                         SaveIntNum(hKey5, "Port", Hist.Port, DefaultHist.Port);\r
474                                                         SaveIntNum(hKey5, "Kanji", Hist.KanjiCode, DefaultHist.KanjiCode);\r
475                                                         SaveIntNum(hKey5, "KanaCnv", Hist.KanaCnv, DefaultHist.KanaCnv);\r
476                                                         SaveIntNum(hKey5, "NameKanji", Hist.NameKanjiCode, DefaultHist.NameKanjiCode);\r
477                                                         SaveIntNum(hKey5, "NameKana", Hist.NameKanaCnv, DefaultHist.NameKanaCnv);\r
478                                                         SaveIntNum(hKey5, "Pasv", Hist.Pasv, DefaultHist.Pasv);\r
479                                                         SaveIntNum(hKey5, "Fwall", Hist.FireWall, DefaultHist.FireWall);\r
480                                                         SaveIntNum(hKey5, "List", Hist.ListCmdOnly, DefaultHist.ListCmdOnly);\r
481                                                         SaveIntNum(hKey5, "NLST-R", Hist.UseNLST_R, DefaultHist.UseNLST_R);\r
482                                                         SaveIntNum(hKey5, "Tzone", Hist.TimeZone, DefaultHist.TimeZone);\r
483                                                         SaveIntNum(hKey5, "Type", Hist.HostType, DefaultHist.HostType);\r
484                                                         SaveIntNum(hKey5, "Sync", Hist.SyncMove, DefaultHist.SyncMove);\r
485                                                         SaveIntNum(hKey5, "Fpath", Hist.NoFullPath, DefaultHist.NoFullPath);\r
486                                                         WriteBinaryToReg(hKey5, "Sort", &Hist.Sort, sizeof(Hist.Sort));\r
487                                                         SaveIntNum(hKey5, "Secu", Hist.Security, DefaultHist.Security);\r
488                                                         WriteIntValueToReg(hKey5, "TrType", Hist.Type);\r
489                                                         SaveIntNum(hKey5, "Dial", Hist.Dialup, DefaultHist.Dialup);\r
490                                                         SaveIntNum(hKey5, "UseIt", Hist.DialupAlways, DefaultHist.DialupAlways);\r
491                                                         SaveIntNum(hKey5, "Notify", Hist.DialupNotify, DefaultHist.DialupNotify);\r
492                                                         SaveStr(hKey5, "DialTo", Hist.DialEntry, DefaultHist.DialEntry);\r
493                                                         // 暗号化通信対応\r
494                                                         SaveIntNum(hKey5, "NoEncryption", Hist.UseNoEncryption, DefaultHist.UseNoEncryption);\r
495                                                         SaveIntNum(hKey5, "FTPES", Hist.UseFTPES, DefaultHist.UseFTPES);\r
496                                                         SaveIntNum(hKey5, "FTPIS", Hist.UseFTPIS, DefaultHist.UseFTPIS);\r
497                                                         SaveIntNum(hKey5, "SFTP", Hist.UseSFTP, DefaultHist.UseSFTP);\r
498                                                         EncodePassword(Hist.PrivateKey, Str);\r
499                                                         SaveStr(hKey5, "PKey", Str, DefaultHist.PrivateKey);\r
500                                                         // 同時接続対応\r
501                                                         SaveIntNum(hKey5, "ThreadCount", Hist.MaxThreadCount, DefaultHist.MaxThreadCount);\r
502                                                         SaveIntNum(hKey5, "ReuseCmdSkt", Hist.ReuseCmdSkt, DefaultHist.ReuseCmdSkt);\r
503                                                         // MLSD対応\r
504                                                         SaveIntNum(hKey5, "MLSD", Hist.UseMLSD, DefaultHist.UseMLSD);\r
505                                                         // IPv6対応\r
506                                                         SaveIntNum(hKey5, "NetType", Hist.NetType, DefaultHist.NetType);\r
507                                                         // 自動切断対策\r
508                                                         SaveIntNum(hKey5, "Noop", Hist.NoopInterval, DefaultHist.NoopInterval);\r
509                                                         // 再転送対応\r
510                                                         SaveIntNum(hKey5, "ErrMode", Hist.TransferErrorMode, DefaultHist.TransferErrorMode);\r
511                                                         SaveIntNum(hKey5, "ErrNotify", Hist.TransferErrorNotify, DefaultHist.TransferErrorNotify);\r
512 \r
513                                                         CloseSubKey(hKey5);\r
514                                                         n++;\r
515                                                 }\r
516                                         }\r
517                                 }\r
518                                 WriteIntValueToReg(hKey4, "SavedHist", n);\r
519 \r
520                                 /* 余分なヒストリがあったら削除 */\r
521                                 for(; n < 999; n++)\r
522                                 {\r
523                                         sprintf(Str, "History%d", n);\r
524                                         if(DeleteSubKey(hKey4, Str) != FFFTP_SUCCESS)\r
525                                                 break;\r
526                                 }\r
527 \r
528                                 /* ホストの設定を保存 */\r
529                                 CopyDefaultHost(&DefaultHost);\r
530                                 i = 0;\r
531                                 while(CopyHostFromList(i, &Host) == FFFTP_SUCCESS)\r
532                                 {\r
533                                         sprintf(Str, "Host%d", i);\r
534                                         if(CreateSubKey(hKey4, Str, &hKey5) == FFFTP_SUCCESS)\r
535                                         {\r
536 //                                              SaveIntNum(hKey5, "Set", Host.Level, DefaultHost.Level);\r
537                                                 WriteIntValueToReg(hKey5, "Set", Host.Level);\r
538                                                 SaveStr(hKey5, "HostName", Host.HostName, DefaultHost.HostName);\r
539                                                 if((Host.Level & SET_LEVEL_GROUP) == 0)\r
540                                                 {\r
541                                                         SaveStr(hKey5, "HostAdrs", Host.HostAdrs, DefaultHost.HostAdrs);\r
542                                                         SaveStr(hKey5, "UserName", Host.UserName, DefaultHost.UserName);\r
543                                                         SaveStr(hKey5, "Account", Host.Account, DefaultHost.Account);\r
544                                                         SaveStr(hKey5, "LocalDir", Host.LocalInitDir, NULL);\r
545                                                         SaveStr(hKey5, "RemoteDir", Host.RemoteInitDir, DefaultHost.RemoteInitDir);\r
546                                                         SaveStr(hKey5, "Chmod", Host.ChmodCmd, DefaultHost.ChmodCmd);\r
547                                                         SaveStr(hKey5, "Nlst", Host.LsName, DefaultHost.LsName);\r
548                                                         SaveStr(hKey5, "Init", Host.InitCmd, DefaultHost.InitCmd);\r
549 \r
550                                                         if(Host.Anonymous == NO)\r
551                                                                 EncodePassword(Host.PassWord, Str);\r
552                                                         else\r
553                                                                 strcpy(Str, DefaultHost.PassWord);\r
554                                                         SaveStr(hKey5, "Password", Str, DefaultHost.PassWord);\r
555 \r
556                                                         SaveIntNum(hKey5, "Port", Host.Port, DefaultHost.Port);\r
557                                                         SaveIntNum(hKey5, "Anonymous", Host.Anonymous, DefaultHost.Anonymous);\r
558                                                         SaveIntNum(hKey5, "Kanji", Host.KanjiCode, DefaultHost.KanjiCode);\r
559                                                         SaveIntNum(hKey5, "KanaCnv", Host.KanaCnv, DefaultHost.KanaCnv);\r
560                                                         SaveIntNum(hKey5, "NameKanji", Host.NameKanjiCode, DefaultHost.NameKanjiCode);\r
561                                                         SaveIntNum(hKey5, "NameKana", Host.NameKanaCnv, DefaultHost.NameKanaCnv);\r
562                                                         SaveIntNum(hKey5, "Pasv", Host.Pasv, DefaultHost.Pasv);\r
563                                                         SaveIntNum(hKey5, "Fwall", Host.FireWall, DefaultHost.FireWall);\r
564                                                         SaveIntNum(hKey5, "List", Host.ListCmdOnly, DefaultHost.ListCmdOnly);\r
565                                                         SaveIntNum(hKey5, "NLST-R", Host.UseNLST_R, DefaultHost.UseNLST_R);\r
566                                                         SaveIntNum(hKey5, "Last", Host.LastDir, DefaultHost.LastDir);\r
567                                                         SaveIntNum(hKey5, "Tzone", Host.TimeZone, DefaultHost.TimeZone);\r
568                                                         SaveIntNum(hKey5, "Type", Host.HostType, DefaultHost.HostType);\r
569                                                         SaveIntNum(hKey5, "Sync", Host.SyncMove, DefaultHost.SyncMove);\r
570                                                         SaveIntNum(hKey5, "Fpath", Host.NoFullPath, DefaultHost.NoFullPath);\r
571                                                         WriteBinaryToReg(hKey5, "Sort", &Host.Sort, sizeof(Host.Sort));\r
572                                                         SaveIntNum(hKey5, "Secu", Host.Security, DefaultHost.Security);\r
573 \r
574                                                         WriteMultiStringToReg(hKey5, "Bmarks", Host.BookMark);\r
575 \r
576                                                         SaveIntNum(hKey5, "Dial", Host.Dialup, DefaultHost.Dialup);\r
577                                                         SaveIntNum(hKey5, "UseIt", Host.DialupAlways, DefaultHost.DialupAlways);\r
578                                                         SaveIntNum(hKey5, "Notify", Host.DialupNotify, DefaultHost.DialupNotify);\r
579                                                         SaveStr(hKey5, "DialTo", Host.DialEntry, DefaultHost.DialEntry);\r
580                                                         // 暗号化通信対応\r
581                                                         SaveIntNum(hKey5, "NoEncryption", Host.UseNoEncryption, DefaultHost.UseNoEncryption);\r
582                                                         SaveIntNum(hKey5, "FTPES", Host.UseFTPES, DefaultHost.UseFTPES);\r
583                                                         SaveIntNum(hKey5, "FTPIS", Host.UseFTPIS, DefaultHost.UseFTPIS);\r
584                                                         SaveIntNum(hKey5, "SFTP", Host.UseSFTP, DefaultHost.UseSFTP);\r
585                                                         EncodePassword(Host.PrivateKey, Str);\r
586                                                         SaveStr(hKey5, "PKey", Str, DefaultHost.PrivateKey);\r
587                                                         // 同時接続対応\r
588                                                         SaveIntNum(hKey5, "ThreadCount", Host.MaxThreadCount, DefaultHost.MaxThreadCount);\r
589                                                         SaveIntNum(hKey5, "ReuseCmdSkt", Host.ReuseCmdSkt, DefaultHost.ReuseCmdSkt);\r
590                                                         // MLSD対応\r
591                                                         SaveIntNum(hKey5, "MLSD", Host.UseMLSD, DefaultHost.UseMLSD);\r
592                                                         // IPv6対応\r
593                                                         SaveIntNum(hKey5, "NetType", Host.NetType, DefaultHost.NetType);\r
594                                                         // 自動切断対策\r
595                                                         SaveIntNum(hKey5, "Noop", Host.NoopInterval, DefaultHost.NoopInterval);\r
596                                                         // 再転送対応\r
597                                                         SaveIntNum(hKey5, "ErrMode", Host.TransferErrorMode, DefaultHost.TransferErrorMode);\r
598                                                         SaveIntNum(hKey5, "ErrNotify", Host.TransferErrorNotify, DefaultHost.TransferErrorNotify);\r
599                                                 }\r
600                                                 CloseSubKey(hKey5);\r
601                                         }\r
602                                         i++;\r
603                                 }\r
604                                 WriteIntValueToReg(hKey4, "SetNum", i);\r
605 \r
606                                 /* 余分なホストの設定があったら削除 */\r
607                                 for(; i < 998; i++)\r
608                                 {\r
609                                         sprintf(Str, "Host%d", i);\r
610                                         if(DeleteSubKey(hKey4, Str) != FFFTP_SUCCESS)\r
611                                                 break;\r
612                                 }\r
613 \r
614                                 if((i = AskCurrentHost()) == HOSTNUM_NOENTRY)\r
615                                         i = 0;\r
616                                 WriteIntValueToReg(hKey4, "CurSet", i);\r
617 \r
618                                 // 暗号化通信対応\r
619                                 WriteBinaryToReg(hKey4, "CertCacheHash", &CertificateCacheHash, sizeof(CertificateCacheHash));\r
620                                 strcpy(Buf, "");\r
621                                 StrCatOut((char*)&SSLRootCAFileHash, sizeof(SSLRootCAFileHash), Buf);\r
622                                 EncodePassword(Buf, Str);\r
623                                 WriteStringToReg(hKey4, "RootCertHash", Str);\r
624                                 // ファイルアイコン表示対応\r
625                                 WriteIntValueToReg(hKey4, "ListIcon", DispFileIcon);\r
626                                 // ディレクトリ自動作成\r
627                                 WriteIntValueToReg(hKey4, "MakeDir", MakeAllDir);\r
628                         }\r
629                         CloseSubKey(hKey4);\r
630                 }\r
631                 CloseReg(hKey3);\r
632         }\r
633         return;\r
634 }\r
635 \r
636 /*----- レジストリ/INIファイルから設定値を呼び出す ---------------------------\r
637 *\r
638 *       この関数を複数回呼び出すと,ホスト設定が追加される.\r
639 *\r
640 *       Parameter\r
641 *               なし\r
642 *\r
643 *       Return Value\r
644 *               YES: 読み出し成功\r
645 *               NO:  読み出し失敗(設定無し)\r
646 *----------------------------------------------------------------------------*/\r
647 \r
648 int LoadRegistry(void)\r
649 {\r
650         void *hKey3;\r
651         void *hKey4;\r
652         void *hKey5;\r
653         int i;\r
654         int Sets;\r
655         // 暗号化通信対応\r
656 //      char Str[256];  /* ASCII_EXT_LENより大きい事 */\r
657         char Str[PRIVATE_KEY_LEN*4+1];\r
658         char Buf[FMAX_PATH+1];\r
659         char *Pos;\r
660         char *Pos2;\r
661         HOSTDATA Host;\r
662         HISTORYDATA Hist;\r
663         int Sts;\r
664         int Version;\r
665 \r
666         Sts = NO;\r
667 \r
668         SetRegType(REGTYPE_INI);\r
669         if((i = OpenReg("FFFTP", &hKey3)) != FFFTP_SUCCESS)\r
670         {\r
671                 if(AskForceIni() == NO)\r
672                 {\r
673                         SetRegType(REGTYPE_REG);\r
674                         i = OpenReg("FFFTP", &hKey3);\r
675                 }\r
676         }\r
677 \r
678         if(i == FFFTP_SUCCESS)\r
679         {\r
680 //              char checkbuf[48];\r
681                 int salt = 0;\r
682                 Sts = YES;\r
683 \r
684                 ReadIntValueFromReg(hKey3, "Version", &Version);\r
685                 // UTF-8対応\r
686                 if(Version < 1980)\r
687                         IniKanjiCode = KANJI_SJIS;\r
688 \r
689                 if(OpenSubKey(hKey3, "Options", &hKey4) == FFFTP_SUCCESS)\r
690                 {\r
691                         ReadIntValueFromReg(hKey4, "WinPosX", &WinPosX);\r
692                         ReadIntValueFromReg(hKey4, "WinPosY", &WinPosY);\r
693                         ReadIntValueFromReg(hKey4, "WinWidth", &WinWidth);\r
694                         ReadIntValueFromReg(hKey4, "WinHeight", &WinHeight);\r
695                         ReadIntValueFromReg(hKey4, "LocalWidth", &LocalWidth);\r
696                         /* ↓旧バージョンのバグ対策 */\r
697                         LocalWidth = max1(0, LocalWidth);\r
698                         ReadIntValueFromReg(hKey4, "TaskHeight", &TaskHeight);\r
699                         /* ↓旧バージョンのバグ対策 */\r
700                         TaskHeight = max1(0, TaskHeight);\r
701                         ReadBinaryFromReg(hKey4, "LocalColm", &LocalTabWidth, sizeof(LocalTabWidth));\r
702                         ReadBinaryFromReg(hKey4, "RemoteColm", &RemoteTabWidth, sizeof(RemoteTabWidth));\r
703                         ReadIntValueFromReg(hKey4, "SwCmd", &Sizing);\r
704 \r
705                         ReadStringFromReg(hKey4, "UserMail", UserMailAdrs, USER_MAIL_LEN+1);\r
706                         ReadStringFromReg(hKey4, "Viewer", ViewerName[0], FMAX_PATH+1);\r
707                         ReadStringFromReg(hKey4, "Viewer2", ViewerName[1], FMAX_PATH+1);\r
708                         ReadStringFromReg(hKey4, "Viewer3", ViewerName[2], FMAX_PATH+1);\r
709 \r
710                         ReadIntValueFromReg(hKey4, "TrType", &TransMode);\r
711                         ReadIntValueFromReg(hKey4, "Recv", &RecvMode);\r
712                         ReadIntValueFromReg(hKey4, "Send", &SendMode);\r
713                         ReadIntValueFromReg(hKey4, "Move", &MoveMode);\r
714                         ReadStringFromReg(hKey4, "Path", DefaultLocalPath, FMAX_PATH+1);\r
715                         ReadIntValueFromReg(hKey4, "Time", &SaveTimeStamp);\r
716                         ReadIntValueFromReg(hKey4, "EOF", &RmEOF);\r
717                         ReadIntValueFromReg(hKey4, "Scolon", &VaxSemicolon);\r
718 \r
719                         ReadIntValueFromReg(hKey4, "RecvEx", &ExistMode);\r
720                         ReadIntValueFromReg(hKey4, "SendEx", &UpExistMode);\r
721 \r
722                         ReadIntValueFromReg(hKey4, "LFsort", &LocalFileSort);\r
723                         ReadIntValueFromReg(hKey4, "LDsort", &LocalDirSort);\r
724                         ReadIntValueFromReg(hKey4, "RFsort", &RemoteFileSort);\r
725                         ReadIntValueFromReg(hKey4, "RDsort", &RemoteDirSort);\r
726                         ReadIntValueFromReg(hKey4, "SortSave", &SortSave);\r
727 \r
728                         ReadIntValueFromReg(hKey4, "ListType", &ListType);\r
729                         ReadIntValueFromReg(hKey4, "Cache", &CacheEntry);\r
730                         ReadIntValueFromReg(hKey4, "CacheSave", &CacheSave);\r
731                         ReadIntValueFromReg(hKey4, "DotFile", &DotFile);\r
732                         ReadIntValueFromReg(hKey4, "Dclick", &DclickOpen);\r
733 \r
734                         ReadIntValueFromReg(hKey4, "ConS", &ConnectOnStart);\r
735                         ReadIntValueFromReg(hKey4, "OldDlg", &ConnectAndSet);\r
736                         ReadIntValueFromReg(hKey4, "RasClose", &RasClose);\r
737                         ReadIntValueFromReg(hKey4, "RasNotify", &RasCloseNotify);\r
738                         ReadIntValueFromReg(hKey4, "Qanony", &QuickAnonymous);\r
739                         ReadIntValueFromReg(hKey4, "PassHist", &PassToHist);\r
740                         ReadIntValueFromReg(hKey4, "SendQuit", &SendQuit);\r
741                         ReadIntValueFromReg(hKey4, "NoRas", &NoRasControl);\r
742 \r
743                         ReadIntValueFromReg(hKey4, "Debug", &DebugConsole);\r
744                         ReadIntValueFromReg(hKey4, "WinPos", &SaveWinPos);\r
745                         ReadIntValueFromReg(hKey4, "RegExp", &FindMode);\r
746                         ReadIntValueFromReg(hKey4, "Reg", &RegType);\r
747 \r
748                         if(ReadMultiStringFromReg(hKey4, "AsciiFile", AsciiExt, ASCII_EXT_LEN+1) == FFFTP_FAIL)\r
749                         {\r
750                                 /* 旧ASCIIモードの拡張子の設定を新しいものに変換 */\r
751                                 ReadStringFromReg(hKey4, "Ascii", Str, ASCII_EXT_LEN+1);\r
752                                 memset(AsciiExt, NUL, ASCII_EXT_LEN+1);\r
753                                 Pos = Str;\r
754                                 while(*Pos != NUL)\r
755                                 {\r
756                                         if((Pos2 = strchr(Pos, ';')) == NULL)\r
757                                                 Pos2 = strchr(Pos, NUL);\r
758                                         if((Pos2 - Pos) > 0)\r
759                                         {\r
760                                                 if((StrMultiLen(AsciiExt) + (Pos2 - Pos) + 2) >= ASCII_EXT_LEN)\r
761                                                         break;\r
762                                                 strcpy(AsciiExt + StrMultiLen(AsciiExt), "*.");\r
763                                                 strncpy(AsciiExt + StrMultiLen(AsciiExt) - 1, Pos, (Pos2 - Pos));\r
764                                         }\r
765                                         Pos = Pos2;\r
766                                         if(*Pos == ';')\r
767                                                 Pos++;\r
768                                 }\r
769                         }\r
770 \r
771                         ReadIntValueFromReg(hKey4, "LowUp", &FnameCnv);\r
772                         ReadIntValueFromReg(hKey4, "Tout", &TimeOut);\r
773 \r
774                         ReadMultiStringFromReg(hKey4, "NoTrn", MirrorNoTrn, MIRROR_LEN+1);\r
775                         ReadMultiStringFromReg(hKey4, "NoDel", MirrorNoDel, MIRROR_LEN+1);\r
776                         ReadIntValueFromReg(hKey4, "MirFile", &MirrorFnameCnv);\r
777                         ReadIntValueFromReg(hKey4, "MirUNot", &MirUpDelNotify);\r
778                         ReadIntValueFromReg(hKey4, "MirDNot", &MirDownDelNotify);\r
779 \r
780                         if(ReadStringFromReg(hKey4, "ListFont", Str, 256) == FFFTP_SUCCESS)\r
781                         {\r
782                                 if(RestoreFontData(Str, &ListLogFont) == FFFTP_SUCCESS)\r
783                                         ListFont = CreateFontIndirect(&ListLogFont);\r
784                         }\r
785                         ReadIntValueFromReg(hKey4, "ListHide", &DispIgnoreHide);\r
786                         ReadIntValueFromReg(hKey4, "ListDrv", &DispDrives);\r
787 \r
788                         ReadStringFromReg(hKey4, "FwallHost", FwallHost, HOST_ADRS_LEN+1);\r
789                         ReadStringFromReg(hKey4, "FwallUser", FwallUser, USER_NAME_LEN+1);\r
790                         ReadStringFromReg(hKey4, "FwallPass", Str, 255);\r
791                         DecodePassword(Str, FwallPass);\r
792                         ReadIntValueFromReg(hKey4, "FwallPort", &FwallPort);\r
793                         ReadIntValueFromReg(hKey4, "FwallType", &FwallType);\r
794                         ReadIntValueFromReg(hKey4, "FwallDef", &FwallDefault);\r
795                         ReadIntValueFromReg(hKey4, "FwallSec", &FwallSecurity);\r
796                         ReadIntValueFromReg(hKey4, "PasvDef", &PasvDefault);\r
797                         ReadIntValueFromReg(hKey4, "FwallRes", &FwallResolv);\r
798                         ReadIntValueFromReg(hKey4, "FwallLow", &FwallLower);\r
799                         ReadIntValueFromReg(hKey4, "FwallDel", &FwallDelimiter);\r
800 \r
801                         ReadIntValueFromReg(hKey4, "SndConSw", &Sound[SND_CONNECT].On);\r
802                         ReadIntValueFromReg(hKey4, "SndTrnSw", &Sound[SND_TRANS].On);\r
803                         ReadIntValueFromReg(hKey4, "SndErrSw", &Sound[SND_ERROR].On);\r
804                         ReadStringFromReg(hKey4, "SndCon", Sound[SND_CONNECT].Fname, FMAX_PATH+1);\r
805                         ReadStringFromReg(hKey4, "SndTrn", Sound[SND_TRANS].Fname, FMAX_PATH+1);\r
806                         ReadStringFromReg(hKey4, "SndErr", Sound[SND_ERROR].Fname, FMAX_PATH+1);\r
807 \r
808                         ReadMultiStringFromReg(hKey4, "DefAttr", DefAttrList, DEFATTRLIST_LEN+1);\r
809 \r
810                         ReadStringFromReg(hKey4, "Tmp", TmpPath, FMAX_PATH+1);\r
811 \r
812                         ReadBinaryFromReg(hKey4, "Hdlg", &HostDlgSize, sizeof(SIZE));\r
813                         ReadBinaryFromReg(hKey4, "Bdlg", &BmarkDlgSize, sizeof(SIZE));\r
814                         ReadBinaryFromReg(hKey4, "Mdlg", &MirrorDlgSize, sizeof(SIZE));\r
815 \r
816                         ReadIntValueFromReg(hKey4, "FAttrSw", &FolderAttr);\r
817                         ReadIntValueFromReg(hKey4, "FAttr", &FolderAttrNum);\r
818 \r
819                         ReadIntValueFromReg(hKey4, "NoSave", &SuppressSave);\r
820 \r
821                         ReadIntValueFromReg(hKey4, "HistNum", &FileHist);\r
822 //                      ReadMultiStringFromReg(hKey4, "Hist", Hist, (FMAX_PATH+1)*HISTORY_MAX+1);\r
823 \r
824                         /* ヒストリの設定を読み込む */\r
825                         Sets = 0;\r
826                         ReadIntValueFromReg(hKey4, "SavedHist", &Sets);\r
827 \r
828                         for(i = 0; i < Sets; i++)\r
829                         {\r
830                                 sprintf(Str, "History%d", i);\r
831                                 if(OpenSubKey(hKey4, Str, &hKey5) == FFFTP_SUCCESS)\r
832                                 {\r
833                                         CopyDefaultHistory(&Hist);\r
834 \r
835                                         ReadStringFromReg(hKey5, "HostAdrs", Hist.HostAdrs, HOST_ADRS_LEN+1);\r
836                                         ReadStringFromReg(hKey5, "UserName", Hist.UserName, USER_NAME_LEN+1);\r
837                                         ReadStringFromReg(hKey5, "Account", Hist.Account, ACCOUNT_LEN+1);\r
838                                         ReadStringFromReg(hKey5, "LocalDir", Hist.LocalInitDir, INIT_DIR_LEN+1);\r
839                                         ReadStringFromReg(hKey5, "RemoteDir", Hist.RemoteInitDir, INIT_DIR_LEN+1);\r
840                                         ReadStringFromReg(hKey5, "Chmod", Hist.ChmodCmd, CHMOD_CMD_LEN+1);\r
841                                         ReadStringFromReg(hKey5, "Nlst", Hist.LsName, NLST_NAME_LEN+1);\r
842                                         ReadStringFromReg(hKey5, "Init", Hist.InitCmd, INITCMD_LEN+1);\r
843                                         ReadIntValueFromReg(hKey5, "Port", &Hist.Port);\r
844                                         ReadIntValueFromReg(hKey5, "Kanji", &Hist.KanjiCode);\r
845                                         ReadIntValueFromReg(hKey5, "KanaCnv", &Hist.KanaCnv);\r
846                                         ReadIntValueFromReg(hKey5, "NameKanji", &Hist.NameKanjiCode);\r
847                                         ReadIntValueFromReg(hKey5, "NameKana", &Hist.NameKanaCnv);\r
848                                         ReadIntValueFromReg(hKey5, "Pasv", &Hist.Pasv);\r
849                                         ReadIntValueFromReg(hKey5, "Fwall", &Hist.FireWall);\r
850                                         ReadIntValueFromReg(hKey5, "List", &Hist.ListCmdOnly);\r
851                                         ReadIntValueFromReg(hKey5, "NLST-R", &Hist.UseNLST_R);\r
852                                         ReadIntValueFromReg(hKey5, "Tzone", &Hist.TimeZone);\r
853                                         ReadIntValueFromReg(hKey5, "Type", &Hist.HostType);\r
854                                         ReadIntValueFromReg(hKey5, "Sync", &Hist.SyncMove);\r
855                                         ReadIntValueFromReg(hKey5, "Fpath", &Hist.NoFullPath);\r
856                                         ReadBinaryFromReg(hKey5, "Sort", &Hist.Sort, sizeof(Hist.Sort));\r
857                                         ReadIntValueFromReg(hKey5, "Secu", &Hist.Security);\r
858                                         ReadIntValueFromReg(hKey5, "TrType", &Hist.Type);\r
859                                         strcpy(Str, "");\r
860                                         ReadStringFromReg(hKey5, "Password", Str, 255);\r
861                                         DecodePassword(Str, Hist.PassWord);\r
862                                         ReadIntValueFromReg(hKey5, "Dial", &Hist.Dialup);\r
863                                         ReadIntValueFromReg(hKey5, "UseIt", &Hist.DialupAlways);\r
864                                         ReadIntValueFromReg(hKey5, "Notify", &Hist.DialupNotify);\r
865                                         ReadStringFromReg(hKey5, "DialTo", Hist.DialEntry, RAS_NAME_LEN+1);\r
866                                         // 暗号化通信対応\r
867                                         ReadIntValueFromReg(hKey5, "NoEncryption", &Hist.UseNoEncryption);\r
868                                         ReadIntValueFromReg(hKey5, "FTPES", &Hist.UseFTPES);\r
869                                         ReadIntValueFromReg(hKey5, "FTPIS", &Hist.UseFTPIS);\r
870                                         ReadIntValueFromReg(hKey5, "SFTP", &Hist.UseSFTP);\r
871                                         strcpy(Str, "");\r
872                                         ReadStringFromReg(hKey5, "PKey", Str, PRIVATE_KEY_LEN*4+1);\r
873                                         DecodePassword(Str, Hist.PrivateKey);\r
874                                         // 同時接続対応\r
875                                         ReadIntValueFromReg(hKey5, "ThreadCount", &Hist.MaxThreadCount);\r
876                                         ReadIntValueFromReg(hKey5, "ReuseCmdSkt", &Hist.ReuseCmdSkt);\r
877                                         // MLSD対応\r
878                                         ReadIntValueFromReg(hKey5, "MLSD", &Hist.UseMLSD);\r
879                                         // IPv6対応\r
880                                         ReadIntValueFromReg(hKey5, "NetType", &Hist.NetType);\r
881                                         // 自動切断対策\r
882                                         ReadIntValueFromReg(hKey5, "Noop", &Hist.NoopInterval);\r
883                                         // 再転送対応\r
884                                         ReadIntValueFromReg(hKey5, "ErrMode", &Hist.TransferErrorMode);\r
885                                         ReadIntValueFromReg(hKey5, "ErrNotify", &Hist.TransferErrorNotify);\r
886 \r
887                                         CloseSubKey(hKey5);\r
888                                         AddHistoryToHistory(&Hist);\r
889                                 }\r
890                         }\r
891 \r
892                         /* ホストの設定を読み込む */\r
893                         Sets = 0;\r
894                         ReadIntValueFromReg(hKey4, "SetNum", &Sets);\r
895 \r
896                         for(i = 0; i < Sets; i++)\r
897                         {\r
898                                 sprintf(Str, "Host%d", i);\r
899                                 if(OpenSubKey(hKey4, Str, &hKey5) == FFFTP_SUCCESS)\r
900                                 {\r
901                                         CopyDefaultHost(&Host);\r
902                                         /* 下位互換性のため */\r
903                                         // SourceForge.JPによるフォーク\r
904 //                                      if(Version < VER_NUM)\r
905                                         if(Version < 1921)\r
906                                         {\r
907                                                 Host.Pasv = NO;\r
908                                                 Host.ListCmdOnly = NO;\r
909                                         }\r
910                                         // 1.97b以前はデフォルトでShift_JIS\r
911                                         if(Version < 1980)\r
912                                                 Host.NameKanjiCode = KANJI_SJIS;\r
913                                         ReadIntValueFromReg(hKey5, "Set", &Host.Level);\r
914 \r
915                                         ReadStringFromReg(hKey5, "HostName", Host.HostName, HOST_NAME_LEN+1);\r
916                                         ReadStringFromReg(hKey5, "HostAdrs", Host.HostAdrs, HOST_ADRS_LEN+1);\r
917                                         ReadStringFromReg(hKey5, "UserName", Host.UserName, USER_NAME_LEN+1);\r
918                                         ReadStringFromReg(hKey5, "Account", Host.Account, ACCOUNT_LEN+1);\r
919                                         ReadStringFromReg(hKey5, "LocalDir", Host.LocalInitDir, INIT_DIR_LEN+1);\r
920                                         ReadStringFromReg(hKey5, "RemoteDir", Host.RemoteInitDir, INIT_DIR_LEN+1);\r
921                                         ReadStringFromReg(hKey5, "Chmod", Host.ChmodCmd, CHMOD_CMD_LEN+1);\r
922                                         ReadStringFromReg(hKey5, "Nlst", Host.LsName, NLST_NAME_LEN+1);\r
923                                         ReadStringFromReg(hKey5, "Init", Host.InitCmd, INITCMD_LEN+1);\r
924                                         ReadIntValueFromReg(hKey5, "Port", &Host.Port);\r
925                                         ReadIntValueFromReg(hKey5, "Anonymous", &Host.Anonymous);\r
926                                         ReadIntValueFromReg(hKey5, "Kanji", &Host.KanjiCode);\r
927                                         // 1.98b以前のUTF-8はBOMあり\r
928                                         if(Version < 1983)\r
929                                         {\r
930                                                 if(Host.KanjiCode == KANJI_UTF8N)\r
931                                                         Host.KanjiCode = KANJI_UTF8BOM;\r
932                                         }\r
933                                         ReadIntValueFromReg(hKey5, "KanaCnv", &Host.KanaCnv);\r
934                                         ReadIntValueFromReg(hKey5, "NameKanji", &Host.NameKanjiCode);\r
935                                         ReadIntValueFromReg(hKey5, "NameKana", &Host.NameKanaCnv);\r
936                                         ReadIntValueFromReg(hKey5, "Pasv", &Host.Pasv);\r
937                                         ReadIntValueFromReg(hKey5, "Fwall", &Host.FireWall);\r
938                                         ReadIntValueFromReg(hKey5, "List", &Host.ListCmdOnly);\r
939                                         ReadIntValueFromReg(hKey5, "NLST-R", &Host.UseNLST_R);\r
940                                         ReadIntValueFromReg(hKey5, "Last", &Host.LastDir);\r
941                                         ReadIntValueFromReg(hKey5, "Tzone", &Host.TimeZone);\r
942                                         ReadIntValueFromReg(hKey5, "Type", &Host.HostType);\r
943                                         ReadIntValueFromReg(hKey5, "Sync", &Host.SyncMove);\r
944                                         ReadIntValueFromReg(hKey5, "Fpath", &Host.NoFullPath);\r
945                                         ReadBinaryFromReg(hKey5, "Sort", &Host.Sort, sizeof(Host.Sort));\r
946                                         ReadIntValueFromReg(hKey5, "Secu", &Host.Security);\r
947                                         if(Host.Anonymous != YES)\r
948                                         {\r
949                                                 strcpy(Str, "");\r
950                                                 ReadStringFromReg(hKey5, "Password", Str, 255);\r
951                                                 DecodePassword(Str, Host.PassWord);\r
952                                         }\r
953                                         else\r
954                                                 strcpy(Host.PassWord, UserMailAdrs);\r
955 \r
956                                         ReadMultiStringFromReg(hKey5, "Bmarks", Host.BookMark, BOOKMARK_SIZE);\r
957 \r
958                                         ReadIntValueFromReg(hKey5, "Dial", &Host.Dialup);\r
959                                         ReadIntValueFromReg(hKey5, "UseIt", &Host.DialupAlways);\r
960                                         ReadIntValueFromReg(hKey5, "Notify", &Host.DialupNotify);\r
961                                         ReadStringFromReg(hKey5, "DialTo", Host.DialEntry, RAS_NAME_LEN+1);\r
962                                         // 暗号化通信対応\r
963                                         ReadIntValueFromReg(hKey5, "NoEncryption", &Host.UseNoEncryption);\r
964                                         ReadIntValueFromReg(hKey5, "FTPES", &Host.UseFTPES);\r
965                                         ReadIntValueFromReg(hKey5, "FTPIS", &Host.UseFTPIS);\r
966                                         ReadIntValueFromReg(hKey5, "SFTP", &Host.UseSFTP);\r
967                                         strcpy(Str, "");\r
968                                         ReadStringFromReg(hKey5, "PKey", Str, PRIVATE_KEY_LEN*4+1);\r
969                                         DecodePassword(Str, Host.PrivateKey);\r
970                                         // 同時接続対応\r
971                                         ReadIntValueFromReg(hKey5, "ThreadCount", &Host.MaxThreadCount);\r
972                                         ReadIntValueFromReg(hKey5, "ReuseCmdSkt", &Host.ReuseCmdSkt);\r
973                                         // 1.98d以前で同時接続数が1より大きい場合はソケットの再利用なし\r
974                                         if(Version < 1985)\r
975                                         {\r
976                                                 if(Host.MaxThreadCount > 1)\r
977                                                         Host.ReuseCmdSkt = NO;\r
978                                         }\r
979                                         // MLSD対応\r
980                                         ReadIntValueFromReg(hKey5, "MLSD", &Host.UseMLSD);\r
981                                         // IPv6対応\r
982                                         ReadIntValueFromReg(hKey5, "NetType", &Host.NetType);\r
983                                         // 自動切断対策\r
984                                         ReadIntValueFromReg(hKey5, "Noop", &Host.NoopInterval);\r
985                                         // 再転送対応\r
986                                         ReadIntValueFromReg(hKey5, "ErrMode", &Host.TransferErrorMode);\r
987                                         ReadIntValueFromReg(hKey5, "ErrNotify", &Host.TransferErrorNotify);\r
988 \r
989                                         CloseSubKey(hKey5);\r
990 \r
991                                         AddHostToList(&Host, -1, Host.Level);\r
992                                 }\r
993                         }\r
994 \r
995                         ReadIntValueFromReg(hKey4, "CurSet", &Sets);\r
996                         SetCurrentHost(Sets);\r
997 \r
998                         // 暗号化通信対応\r
999                         ReadBinaryFromReg(hKey4, "CertCacheHash", &CertificateCacheHash, sizeof(CertificateCacheHash));\r
1000                         ReadStringFromReg(hKey4, "RootCertHash", Str, PRIVATE_KEY_LEN*4+1);\r
1001                         DecodePassword(Str, Buf);\r
1002                         StrReadIn(Buf, sizeof(SSLRootCAFileHash), (char*)&SSLRootCAFileHash);\r
1003                         // ファイルアイコン表示対応\r
1004                         ReadIntValueFromReg(hKey4, "ListIcon", &DispFileIcon);\r
1005                         // ディレクトリ自動作成\r
1006                         ReadIntValueFromReg(hKey4, "MakeDir", &MakeAllDir);\r
1007 \r
1008                         CloseSubKey(hKey4);\r
1009                 }\r
1010                 CloseReg(hKey3);\r
1011         }\r
1012         else\r
1013         {\r
1014                 /*===== 最初の起動時(設定が無い) =====*/\r
1015 \r
1016 #if 0\r
1017                 strcpy(UserMailAdrs, "");\r
1018                 strcpy(Str, "");\r
1019                 if(InputDialogBox(mailadrs_dlg, HWND_DESKTOP, NULL, Str, USER_MAIL_LEN+1, &i, IDH_HELP_TOPIC_0000001) == YES)\r
1020                         strcpy(UserMailAdrs, Str);\r
1021 \r
1022                 for(i = 0; i < SAMPLE_HOSTS; i++)\r
1023                 {\r
1024                         CopyDefaultHost(&Host);\r
1025                         Host.Level = Sample[i].Level;\r
1026                         strcpy(Host.PassWord, UserMailAdrs);\r
1027                         strcpy(Host.HostName, Sample[i].HostName);\r
1028                         strcpy(Host.HostAdrs, Sample[i].HostAdrs);\r
1029                         strcpy(Host.UserName, "anonymous");\r
1030                         AddHostToList(&Host, -1, Host.Level);\r
1031                 }\r
1032 #endif\r
1033         }\r
1034         return(Sts);\r
1035 }\r
1036 \r
1037 \r
1038 /*----- 隠しドライブ情報を取得 ------------------------------------------------\r
1039 *\r
1040 *       Parameter\r
1041 *               なし\r
1042 *\r
1043 *       Return Value\r
1044 *               DWORD \r
1045 *                       YES/NO=設定無し\r
1046 *----------------------------------------------------------------------------*/\r
1047 \r
1048 DWORD LoadHideDriveListRegistry(void)\r
1049 {\r
1050         HKEY hKey1;\r
1051         HKEY hKey2;\r
1052         HKEY hKey3;\r
1053         HKEY hKey4;\r
1054         HKEY hKey5;\r
1055         HKEY hKey6;\r
1056         DWORD Size;\r
1057         DWORD Type;\r
1058         DWORD Ret;\r
1059 \r
1060         Ret = 0;\r
1061         if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hKey1) == ERROR_SUCCESS)\r
1062         {\r
1063                 if(RegOpenKeyEx(hKey1, "Microsoft", 0, KEY_READ, &hKey2) == ERROR_SUCCESS)\r
1064                 {\r
1065                         if(RegOpenKeyEx(hKey2, "Windows", 0, KEY_READ, &hKey3) == ERROR_SUCCESS)\r
1066                         {\r
1067                                 if(RegOpenKeyEx(hKey3, "CurrentVersion", 0, KEY_READ, &hKey4) == ERROR_SUCCESS)\r
1068                                 {\r
1069                                         if(RegOpenKeyEx(hKey4, "Policies", 0, KEY_READ, &hKey5) == ERROR_SUCCESS)\r
1070                                         {\r
1071                                                 if(RegOpenKeyEx(hKey5, "Explorer", 0, KEY_READ, &hKey6) == ERROR_SUCCESS)\r
1072                                                 {\r
1073                                                         Size = sizeof(DWORD);\r
1074                                                         RegQueryValueEx(hKey6, "NoDrives", NULL, &Type, (BYTE *)&Ret, &Size);\r
1075                                                         RegCloseKey(hKey6);\r
1076                                                 }\r
1077                                                 RegCloseKey(hKey5);\r
1078                                         }\r
1079                                         RegCloseKey(hKey4);\r
1080                                 }\r
1081                                 RegCloseKey(hKey3);\r
1082                         }\r
1083                         RegCloseKey(hKey2);\r
1084                 }\r
1085                 RegCloseKey(hKey1);\r
1086         }\r
1087         return(Ret);\r
1088 }\r
1089 \r
1090 \r
1091 /*----- レジストリの設定値をクリア --------------------------------------------\r
1092 *\r
1093 *       Parameter\r
1094 *               なし\r
1095 *\r
1096 *       Return Value\r
1097 *               なし\r
1098 *----------------------------------------------------------------------------*/\r
1099 \r
1100 void ClearRegistry(void)\r
1101 {\r
1102         HKEY hKey2;\r
1103         HKEY hKey3;\r
1104         HKEY hKey4;\r
1105         DWORD Dispos;\r
1106         char Str[20];\r
1107         int i;\r
1108 \r
1109         if(RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Sota", 0, "", REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, NULL, &hKey2, &Dispos) == ERROR_SUCCESS)\r
1110         {\r
1111                 if(RegCreateKeyEx(hKey2, "FFFTP", 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey3, &Dispos) == ERROR_SUCCESS)\r
1112                 {\r
1113                         if(RegCreateKeyEx(hKey3, "Options", 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey4, &Dispos) == ERROR_SUCCESS)\r
1114                         {\r
1115                                 for(i = 0; ; i++)\r
1116                                 {\r
1117                                         sprintf(Str, "Host%d", i);\r
1118                                         if(RegDeleteKey(hKey4, Str) != ERROR_SUCCESS)\r
1119                                                 break;\r
1120                                 }\r
1121                                 for(i = 0; ; i++)\r
1122                                 {\r
1123                                         sprintf(Str, "History%d", i);\r
1124                                         if(RegDeleteKey(hKey4, Str) != ERROR_SUCCESS)\r
1125                                                 break;\r
1126                                 }\r
1127                                 RegCloseKey(hKey4);\r
1128                         }\r
1129                         RegDeleteKey(hKey3, "Options");\r
1130                         RegCloseKey(hKey3);\r
1131                 }\r
1132                 RegDeleteKey(hKey2, "FFFTP");\r
1133                 RegCloseKey(hKey2);\r
1134         }\r
1135         return;\r
1136 }\r
1137 \r
1138 \r
1139 // ポータブル版判定\r
1140 void ClearIni(void)\r
1141 {\r
1142 //      FILE *Strm;\r
1143 //      if((Strm = fopen(AskIniFilePath(), "rt")) != NULL)\r
1144 //      {\r
1145 //              fclose(Strm);\r
1146 //              MoveFileToTrashCan(AskIniFilePath());\r
1147 //      }\r
1148         DeleteFile(AskIniFilePath());\r
1149         return;\r
1150 }\r
1151 \r
1152 \r
1153 /*----- 設定をファイルに保存 --------------------------------------------------\r
1154 *\r
1155 *       Parameter\r
1156 *               なし\r
1157 *\r
1158 *       Return Value\r
1159 *               なし\r
1160 *----------------------------------------------------------------------------*/\r
1161 \r
1162 void SaveSettingsToFile(void)\r
1163 {\r
1164         char Tmp[FMAX_PATH*2];\r
1165         char Fname[FMAX_PATH+1];\r
1166         // 任意のコードが実行されるバグ修正\r
1167         char CurDir[FMAX_PATH+1];\r
1168         char SysDir[FMAX_PATH+1];\r
1169 \r
1170         if(RegType == REGTYPE_REG)\r
1171         {\r
1172                 strcpy(Fname, "FFFTP.reg");\r
1173                 if(SelectFile(GetMainHwnd(), Fname, MSGJPN286, MSGJPN287, "reg", OFN_EXTENSIONDIFFERENT | OFN_OVERWRITEPROMPT, 1) == TRUE)\r
1174                 {\r
1175                         sprintf(Tmp, "/e \x22%s\x22 HKEY_CURRENT_USER\\Software\\sota\\FFFTP", Fname);\r
1176                         // 任意のコードが実行されるバグ修正\r
1177 //                      if(ShellExecute(NULL, "open", "regedit", Tmp, ".", SW_SHOW) <= (HINSTANCE)32)\r
1178 //                      {\r
1179 //                              MessageBox(NULL, MSGJPN285, "FFFTP", MB_OK);\r
1180 //                      }\r
1181                         if(GetCurrentDirectory(FMAX_PATH, CurDir) > 0)\r
1182                         {\r
1183                                 if(GetSystemDirectory(SysDir, FMAX_PATH) > 0)\r
1184                                 {\r
1185                                         if(SetCurrentDirectory(SysDir))\r
1186                                         {\r
1187                                                 if(ShellExecute(NULL, "open", "regedit", Tmp, NULL, SW_SHOW) <= (HINSTANCE)32)\r
1188                                                 {\r
1189                                                         MessageBox(NULL, MSGJPN285, "FFFTP", MB_OK);\r
1190                                                 }\r
1191                                                 SetCurrentDirectory(CurDir);\r
1192                                         }\r
1193                                 }\r
1194                         }\r
1195                 }\r
1196         }\r
1197         else\r
1198         {\r
1199                 strcpy(Fname, "FFFTP-Backup.ini");\r
1200                 if(SelectFile(GetMainHwnd(), Fname, MSGJPN286, MSGJPN288, "ini", OFN_EXTENSIONDIFFERENT | OFN_OVERWRITEPROMPT, 1) == TRUE)\r
1201                 {\r
1202                         CopyFile(AskIniFilePath(), Fname, FALSE);\r
1203                 }\r
1204         }\r
1205         return;\r
1206 }\r
1207 \r
1208 \r
1209 /*----- 設定をファイルから復元 ------------------------------------------------\r
1210 *\r
1211 *       Parameter\r
1212 *               なし\r
1213 *\r
1214 *       Return Value\r
1215 *               int     ロードしたかどうか (YES/NO)\r
1216 *----------------------------------------------------------------------------*/\r
1217 \r
1218 int LoadSettingsFromFile(void)\r
1219 {\r
1220         int Ret;\r
1221         char Tmp[FMAX_PATH*2];\r
1222         char Fname[FMAX_PATH+1];\r
1223         // 任意のコードが実行されるバグ修正\r
1224         char CurDir[FMAX_PATH+1];\r
1225         char SysDir[FMAX_PATH+1];\r
1226 \r
1227         Ret = NO;\r
1228         strcpy(Fname, "");\r
1229         if(SelectFile(GetMainHwnd(), Fname, MSGJPN291, MSGJPN290, "", OFN_FILEMUSTEXIST, 0) == TRUE)\r
1230         {\r
1231                 if((strlen(Fname) >= 5) && (_stricmp(&Fname[strlen(Fname)-4], ".reg") == 0))\r
1232                 {\r
1233                         sprintf(Tmp, "\x22%s\x22", Fname);\r
1234                         // 任意のコードが実行されるバグ修正\r
1235 //                      if(ShellExecute(NULL, "open", "regedit", Tmp, ".", SW_SHOW) <= (HINSTANCE)32)\r
1236 //                      {\r
1237 //                              MessageBox(NULL, MSGJPN285, "FFFTP", MB_OK);\r
1238 //                      }\r
1239 //                      else\r
1240 //                      {\r
1241 //                              Ret = YES;\r
1242 //                              /* レジストリエディタが終了するのを待つ */\r
1243 //                              WaitForSingleObject(Info.hProcess, INFINITE);\r
1244 //                      }\r
1245                         if(GetCurrentDirectory(FMAX_PATH, CurDir) > 0)\r
1246                         {\r
1247                                 if(GetSystemDirectory(SysDir, FMAX_PATH) > 0)\r
1248                                 {\r
1249                                         if(SetCurrentDirectory(SysDir))\r
1250                                         {\r
1251                                                 if(ShellExecute(NULL, "open", "regedit", Tmp, NULL, SW_SHOW) <= (HINSTANCE)32)\r
1252                                                 {\r
1253                                                         MessageBox(NULL, MSGJPN285, "FFFTP", MB_OK);\r
1254                                                 }\r
1255                                                 else\r
1256                                                 {\r
1257                                                         Ret = YES;\r
1258                                                         /* レジストリエディタが終了するのを待つ */\r
1259 //                                                      WaitForSingleObject(Info.hProcess, INFINITE);\r
1260                                                 }\r
1261                                                 SetCurrentDirectory(CurDir);\r
1262                                         }\r
1263                                 }\r
1264                         }\r
1265                 }\r
1266                 else if((strlen(Fname) >= 5) && (_stricmp(&Fname[strlen(Fname)-4], ".ini") == 0))\r
1267                 {\r
1268                         CopyFile(Fname, AskIniFilePath(), FALSE);\r
1269                         Ret = YES;\r
1270                 }\r
1271                 else\r
1272                         MessageBox(NULL, MSGJPN293, "FFFTP", MB_OK);\r
1273         }\r
1274         return(Ret);\r
1275 }\r
1276 \r
1277 \r
1278 \r
1279 \r
1280 /*----- レジストリ/INIファイルに文字列をセーブ --------------------------------\r
1281 *\r
1282 *       Parameter\r
1283 *               HKEY hKey : レジストリキー\r
1284 *               char *Key : キー名\r
1285 *               char *Str : セーブする文字列\r
1286 *               char *DefaultStr : デフォルトの文字列\r
1287 *\r
1288 *       Return Value\r
1289 *               なし\r
1290 *\r
1291 *       Note\r
1292 *               文字列がデフォルトの文字列と同じならセーブしない\r
1293 *----------------------------------------------------------------------------*/\r
1294 \r
1295 static void SaveStr(HKEY hKey, char *Key, char *Str, char *DefaultStr)\r
1296 {\r
1297         if((DefaultStr != NULL) && (strcmp(Str, DefaultStr) == 0))\r
1298                 DeleteValue(hKey, Key);\r
1299         else\r
1300                 WriteStringToReg(hKey, Key, Str);\r
1301 \r
1302         return;\r
1303 }\r
1304 \r
1305 \r
1306 /*----- レジストリ/INIファイルに数値(INT)をセーブ -----------------------------\r
1307 *\r
1308 *       Parameter\r
1309 *               HKEY hKey : レジストリキー\r
1310 *               char *Key : キー名\r
1311 *               int Num : セーブする値\r
1312 *               int DefaultNum : デフォルトの値\r
1313 *\r
1314 *       Return Value\r
1315 *               なし\r
1316 *\r
1317 *       Note\r
1318 *               数値がデフォルトの値と同じならセーブしない\r
1319 *----------------------------------------------------------------------------*/\r
1320 \r
1321 static void SaveIntNum(HKEY hKey, char *Key, int Num, int DefaultNum)\r
1322 {\r
1323         if(Num == DefaultNum)\r
1324                 DeleteValue(hKey, Key);\r
1325         else\r
1326                 WriteIntValueToReg(hKey, Key, Num);\r
1327 \r
1328         return;\r
1329 }\r
1330 \r
1331 \r
1332 /*----- LOGFONTデータを文字列に変換する ---------------------------------------\r
1333 *\r
1334 *       Parameter\r
1335 *               LOGFONT Font : フォントデータ\r
1336 *               HFONT hFont : フォントのハンドル\r
1337 *                       NULL = デフォルトのフォント\r
1338 *               char *Buf : バッファ\r
1339 *\r
1340 *       Return Value\r
1341 *               なし\r
1342 *----------------------------------------------------------------------------*/\r
1343 \r
1344 static void MakeFontData(LOGFONT Font, HFONT hFont, char *Buf)\r
1345 {\r
1346         *Buf = NUL;\r
1347         if(hFont != NULL)\r
1348                 sprintf(Buf, "%d %d %d %d %d %d %d %d %d %d %d %d %d %s",\r
1349                         Font.lfHeight, Font.lfWidth, Font.lfEscapement, Font.lfOrientation,\r
1350                         Font.lfWeight, Font.lfItalic, Font.lfUnderline, Font.lfStrikeOut,\r
1351                         Font.lfCharSet, Font.lfOutPrecision, Font.lfClipPrecision,\r
1352                         Font.lfQuality, Font.lfPitchAndFamily, Font.lfFaceName);\r
1353         return;\r
1354 }\r
1355 \r
1356 \r
1357 /*----- 文字列をLOGFONTデータに変換する ---------------------------------------\r
1358 *\r
1359 *       Parameter\r
1360 *               char *Str : 文字列\r
1361 *               LOGFONT *Font : フォントデータ\r
1362 *\r
1363 *       Return Value\r
1364 *               int ステータス\r
1365 *                       FFFTP_SUCCESS/FFFTP_FAIL=変換できない\r
1366 *----------------------------------------------------------------------------*/\r
1367 \r
1368 static int RestoreFontData(char *Str, LOGFONT *Font)\r
1369 {\r
1370         int i;\r
1371         int Sts;\r
1372 \r
1373         Sts = FFFTP_FAIL;\r
1374         if(sscanf(Str, "%d %d %d %d %d %d %d %d %d %d %d %d %d",\r
1375                         &(Font->lfHeight), &(Font->lfWidth), &(Font->lfEscapement), &(Font->lfOrientation),\r
1376                         &(Font->lfWeight), &(Font->lfItalic), &(Font->lfUnderline), &(Font->lfStrikeOut),\r
1377                         &(Font->lfCharSet), &(Font->lfOutPrecision), &(Font->lfClipPrecision),\r
1378                         &(Font->lfQuality), &(Font->lfPitchAndFamily)) == 13)\r
1379         {\r
1380                 for(i = 13; i > 0; i--)\r
1381                 {\r
1382                         if((Str = strchr(Str, ' ')) == NULL)\r
1383                                 break;\r
1384                         Str++;\r
1385                 }\r
1386                 if(i == 0)\r
1387                 {\r
1388                         strcpy(Font->lfFaceName, Str);\r
1389                         Sts = FFFTP_SUCCESS;\r
1390                 }\r
1391         }\r
1392 \r
1393         if(Sts == FFFTP_FAIL)\r
1394                 memset(Font, NUL, sizeof(LOGFONT));\r
1395 \r
1396         return(Sts);\r
1397 }\r
1398 \r
1399 /*----- パスワードを暗号化する ------------------------------------------------\r
1400 *\r
1401 *       Parameter\r
1402 *               char *Str : パスワード\r
1403 *               char *Buf : 暗号化したパスワードを格納するバッファ\r
1404 *\r
1405 *       Return Value\r
1406 *               なし\r
1407 *----------------------------------------------------------------------------*/\r
1408 static void EncodePassword(char *Str, char *Buf)\r
1409 {\r
1410         EncodePassword3( Str, Buf, SecretKey );\r
1411 }\r
1412 \r
1413 /*----- パスワードを暗号化する(オリジナルアルゴリズム)  ------------------\r
1414 *\r
1415 *       Parameter\r
1416 *               char *Str : パスワード\r
1417 *               char *Buf : 暗号化したパスワードを格納するバッファ\r
1418 *\r
1419 *       Return Value\r
1420 *               なし\r
1421 *----------------------------------------------------------------------------*/\r
1422 \r
1423 static void EncodePasswordOriginal(char *Str, char *Buf)\r
1424 {\r
1425         unsigned char *Get;\r
1426         unsigned char *Put;\r
1427         int Rnd;\r
1428         int Ch;\r
1429 \r
1430         srand((unsigned)time(NULL));\r
1431 \r
1432         Get = (unsigned char *)Str;\r
1433         Put = (unsigned char *)Buf;\r
1434         \r
1435         if( *Get == NUL ){\r
1436                 *Put = NUL;\r
1437                 return;\r
1438         }\r
1439 \r
1440         /* 識別子を先頭に置く */\r
1441         Put[0] = '0';\r
1442         Put[1] = 'A';\r
1443         Put += 2;\r
1444 \r
1445         while(*Get != NUL)\r
1446         {\r
1447                 Rnd = rand() % 3;\r
1448                 Ch = ((int)*Get++) << Rnd;\r
1449                 Ch = (unsigned char)Ch | (unsigned char)(Ch >> 8);\r
1450                 *Put++ = 0x40 | ((Rnd & 0x3) << 4) | (Ch & 0xF);\r
1451                 *Put++ = 0x40 | ((Ch >> 4) & 0xF);\r
1452                 if((*(Put-2) & 0x1) != 0)\r
1453                         *Put++ = (rand() % 62) + 0x40;\r
1454         }\r
1455         *Put = NUL;\r
1456         return;\r
1457 }\r
1458 \r
1459 /*----- パスワードを暗号化する(オリジナルアルゴリズム^Key)  ----------------\r
1460 *\r
1461 *       Parameter\r
1462 *               char *Str : パスワード\r
1463 *               char *Buf : 暗号化したパスワードを格納するバッファ\r
1464 *               const char *Key : 暗号化キー\r
1465 *\r
1466 *       Return Value\r
1467 *               なし\r
1468 *----------------------------------------------------------------------------*/\r
1469 \r
1470 static void EncodePassword2(char *Str, char *Buf, const char* Key)\r
1471 {\r
1472         unsigned char *Get;\r
1473         unsigned char *Put;\r
1474         int Rnd;\r
1475         int Ch;\r
1476 \r
1477         /* 2010.01.31 genta Key */\r
1478         unsigned char *KeyHead = (unsigned char *)Key;\r
1479         unsigned char *KeyEnd = KeyHead + strlen(KeyHead);\r
1480         unsigned char *KeyCurrent = KeyHead;\r
1481 \r
1482         srand((unsigned)time(NULL));\r
1483 \r
1484         Get = (unsigned char *)Str;\r
1485         Put = (unsigned char *)Buf;\r
1486         \r
1487         if( *Get == NUL ){\r
1488                 *Put = NUL;\r
1489                 return;\r
1490         }\r
1491 \r
1492         /* 識別子を先頭に置く */\r
1493         Put[0] = '0';\r
1494         Put[1] = 'B';\r
1495         Put += 2;\r
1496 \r
1497         while(*Get != NUL)\r
1498         {\r
1499                 Rnd = rand() % 3;\r
1500                 Ch = ((int)(*Get++ ^ *KeyCurrent)) << Rnd;\r
1501                 Ch = (unsigned char)Ch | (unsigned char)(Ch >> 8);\r
1502                 *Put++ = 0x40 | ((Rnd & 0x3) << 4) | (Ch & 0xF);\r
1503                 *Put++ = 0x40 | ((Ch >> 4) & 0xF);\r
1504                 if((*(Put-2) & 0x1) != 0)\r
1505                         *Put++ = (rand() % 62) + 0x40;\r
1506 \r
1507                 /* 2010.01.31 genta Key */\r
1508                 if( ++KeyCurrent == KeyEnd ){\r
1509                         KeyCurrent = KeyHead;\r
1510                 }\r
1511         }\r
1512         *Put = NUL;\r
1513         return;\r
1514 }\r
1515 \r
1516 /*----- パスワードを暗号化する(AES) ------------------------------------------\r
1517 *\r
1518 *       Parameter\r
1519 *               char *Str : パスワード\r
1520 *               char *Buf : 暗号化したパスワードを格納するバッファ\r
1521 *               const char *Key : 暗号化キー\r
1522 *\r
1523 *       Return Value\r
1524 *               なし\r
1525 *----------------------------------------------------------------------------*/\r
1526 \r
1527 static void EncodePassword3(char *Str, char *Buf, const char *Key)\r
1528 {\r
1529         unsigned char *Put;\r
1530         unsigned char *AesEncBuf;\r
1531         unsigned char *StrPadBuf;\r
1532         size_t StrLen;\r
1533         size_t StrPadLen;\r
1534         size_t StrPadIndex;\r
1535         size_t IvIndex;\r
1536         size_t EncBufIndex;\r
1537         DWORD RandValue;\r
1538         int RandByteCount;\r
1539         unsigned char AesKey[32];\r
1540         unsigned char AesCbcIv[AES_BLOCK_SIZE];\r
1541         aes_encrypt_ctx Ctx;\r
1542 \r
1543         Buf[0] = NUL;\r
1544 \r
1545         Put = (unsigned char *)Buf;\r
1546         StrLen = strlen(Str);\r
1547         StrPadLen = ((StrLen + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;\r
1548 \r
1549         /* 最低長を32文字とする */\r
1550 //      StrPadLen = min1(StrPadLen, AES_BLOCK_SIZE * 2);\r
1551         StrPadLen = max1(StrPadLen, AES_BLOCK_SIZE * 2);\r
1552 \r
1553         if((StrPadBuf = malloc(StrPadLen)) != NULL)\r
1554         {\r
1555                 if((AesEncBuf = malloc(StrPadLen)) != NULL)\r
1556                 {\r
1557                         BOOL PutState;\r
1558 \r
1559                         PutState = FALSE;\r
1560                         strncpy(StrPadBuf, Str, StrPadLen);\r
1561                         \r
1562                         /* PAD部分を乱数で埋める StrPad[StrLen](が有効な場合) は NUL */\r
1563                         for(StrPadIndex = StrLen + 1; StrPadIndex < StrPadLen;)\r
1564                         {\r
1565                                 RandValue = GetRandamDWRODValue();\r
1566                                 for(RandByteCount = 0; RandByteCount < 4; RandByteCount++)\r
1567                                 {\r
1568                                         if(StrPadIndex < StrPadLen)\r
1569                                         {\r
1570                                                 StrPadBuf[StrPadIndex++] = (unsigned char)(RandValue & 0xff);\r
1571                                                 RandValue >>= 8;\r
1572                                         }\r
1573                                 }\r
1574                         }\r
1575                         \r
1576                         // IVの初期化\r
1577                         for(IvIndex = 0; IvIndex < AES_BLOCK_SIZE;)\r
1578                         {\r
1579                                 RandValue = GetRandamDWRODValue();\r
1580                                 for(RandByteCount = 0; RandByteCount < 4; RandByteCount++)\r
1581                                 {\r
1582                                         if(IvIndex < AES_BLOCK_SIZE)\r
1583                                         {\r
1584                                                 AesCbcIv[IvIndex++] = (unsigned char)(RandValue & 0xff);\r
1585                                                 RandValue >>= 8;\r
1586                                         }\r
1587                                 }\r
1588                         }\r
1589                         Put[0] = '0';\r
1590                         Put[1] = 'C';\r
1591                         Put += 2;\r
1592                         for(IvIndex = 0; IvIndex < AES_BLOCK_SIZE; IvIndex++)\r
1593                         {\r
1594                                 sprintf(Put, "%02x", AesCbcIv[IvIndex]);\r
1595                                 Put += 2;\r
1596                         }\r
1597                         *Put++ = ':';\r
1598 \r
1599                         if(CreateAesKey(AesKey, Key) == FFFTP_SUCCESS)\r
1600                         {\r
1601                                 aes_encrypt_key(AesKey, 32, &Ctx);\r
1602 \r
1603                                 if(aes_cbc_encrypt(StrPadBuf, AesEncBuf, StrPadLen, AesCbcIv, &Ctx) == EXIT_SUCCESS)\r
1604                                 {\r
1605                                         for(EncBufIndex = 0; EncBufIndex < StrPadLen; EncBufIndex++)\r
1606                                         {\r
1607                                                 sprintf(Put, "%02x", AesEncBuf[EncBufIndex]);\r
1608                                                 Put += 2;\r
1609                                         }\r
1610                                         *Put = NUL;\r
1611                                         PutState = TRUE;\r
1612                                 }\r
1613                         }\r
1614                         if(FALSE == PutState)\r
1615                         {\r
1616                                 Buf[0] = NUL;\r
1617                         }\r
1618                         free(StrPadBuf);\r
1619                 }\r
1620                 free(AesEncBuf);\r
1621         }\r
1622         return;\r
1623 }\r
1624 \r
1625 \r
1626 /*----- パスワードの暗号化を解く ----------------------------------------------\r
1627 *\r
1628 *       Parameter\r
1629 *               char *Str : 暗号化したパスワード\r
1630 *               char *Buf : パスワードを格納するバッファ\r
1631 *\r
1632 *       Return Value\r
1633 *               なし\r
1634 *----------------------------------------------------------------------------*/\r
1635 \r
1636 static void DecodePassword(char *Str, char *Buf)\r
1637 {\r
1638         unsigned char *Get;\r
1639         unsigned char *Put;\r
1640 \r
1641         Get = (unsigned char *)Str;\r
1642         Put = (unsigned char *)Buf;\r
1643         \r
1644         if( *Get == NUL ){\r
1645                 *Put = NUL;\r
1646         }\r
1647         else if( 0x40 <= *Get && *Get < 0x80 ){\r
1648                 /* Original algorithm */\r
1649                 DecodePasswordOriginal( Str, Buf );\r
1650         }\r
1651         else if( strncmp( Get, "0A", 2 ) == 0 ){\r
1652                 DecodePasswordOriginal( Str + 2, Buf );\r
1653         }\r
1654         else if( strncmp( Get, "0B", 2 ) == 0 ){\r
1655                 DecodePassword2( Str + 2, Buf, SecretKey );\r
1656         }\r
1657         else if( strncmp( Get, "0C", 2 ) == 0 ){\r
1658                 DecodePassword3( Str + 2, Buf, SecretKey );\r
1659         }\r
1660         else {\r
1661                 //      unknown encoding\r
1662                 *Put = NUL;\r
1663                 return;\r
1664         }\r
1665 }\r
1666 \r
1667 /*----- パスワードの暗号化を解く(オリジナルアルゴリズム) -------------------\r
1668 *\r
1669 *       Parameter\r
1670 *               char *Str : 暗号化したパスワード\r
1671 *               char *Buf : パスワードを格納するバッファ\r
1672 *\r
1673 *       Return Value\r
1674 *               なし\r
1675 *----------------------------------------------------------------------------*/\r
1676 static void DecodePasswordOriginal(char *Str, char *Buf)\r
1677 {\r
1678         unsigned char *Get;\r
1679         unsigned char *Put;\r
1680         int Rnd;\r
1681         int Ch;\r
1682 \r
1683         Get = (unsigned char *)Str;\r
1684         Put = (unsigned char *)Buf;\r
1685 \r
1686         while(*Get != NUL)\r
1687         {\r
1688                 Rnd = ((unsigned int)*Get >> 4) & 0x3;\r
1689                 Ch = (*Get & 0xF) | ((*(Get+1) & 0xF) << 4);\r
1690                 Ch <<= 8;\r
1691                 if((*Get & 0x1) != 0)\r
1692                         Get++;\r
1693                 Get += 2;\r
1694                 Ch >>= Rnd;\r
1695                 Ch = (Ch & 0xFF) | ((Ch >> 8) & 0xFF);\r
1696                 *Put++ = Ch;\r
1697         }\r
1698         *Put = NUL;\r
1699         return;\r
1700 }\r
1701 \r
1702 /*----- パスワードの暗号化を解く(オリジナルアルゴリズム^Key) -------------------\r
1703 *\r
1704 *       Parameter\r
1705 *               char *Str : 暗号化したパスワード\r
1706 *               char *Buf : パスワードを格納するバッファ\r
1707 *               const char *Key : 暗号化キー\r
1708 *\r
1709 *       Return Value\r
1710 *               なし\r
1711 *----------------------------------------------------------------------------*/\r
1712 static void DecodePassword2(char *Str, char *Buf, const char* Key)\r
1713 {\r
1714         int Rnd;\r
1715         int Ch;\r
1716         unsigned char *Get = (unsigned char *)Str;\r
1717         unsigned char *Put = (unsigned char *)Buf;\r
1718 \r
1719         /* 2010.01.31 genta Key */\r
1720         unsigned char *KeyHead = (unsigned char *)Key;\r
1721         unsigned char *KeyEnd = KeyHead + strlen(KeyHead);\r
1722         unsigned char *KeyCurrent = KeyHead;\r
1723 \r
1724         while(*Get != NUL)\r
1725         {\r
1726                 Rnd = ((unsigned int)*Get >> 4) & 0x3;\r
1727                 Ch = (*Get & 0xF) | ((*(Get+1) & 0xF) << 4);\r
1728                 Ch <<= 8;\r
1729                 if((*Get & 0x1) != 0)\r
1730                         Get++;\r
1731                 Get += 2;\r
1732                 Ch >>= Rnd;\r
1733                 Ch = (Ch & 0xFF) | ((Ch >> 8) & 0xFF);\r
1734                 *Put++ = Ch ^ *KeyCurrent;\r
1735                 \r
1736                 /* 2010.01.31 genta Key */\r
1737                 if( ++KeyCurrent == KeyEnd ){\r
1738                         KeyCurrent = KeyHead;\r
1739                 }\r
1740         }\r
1741         *Put = NUL;\r
1742         return;\r
1743 }\r
1744 \r
1745 /*----- パスワードの暗号化を解く(AES) ---------------------------------------\r
1746 *\r
1747 *       Parameter\r
1748 *               char *Str : 暗号化したパスワード\r
1749 *               char *Buf : パスワードを格納するバッファ\r
1750 *               const char *Key : 暗号化キー\r
1751 *\r
1752 *       Return Value\r
1753 *               なし\r
1754 *----------------------------------------------------------------------------*/\r
1755 \r
1756 static void DecodePassword3(char *Str, char *Buf, const char *Key)\r
1757 {\r
1758         char *Get;\r
1759         unsigned char *EncBuf;\r
1760         size_t StrLen;\r
1761         size_t IvIndex;\r
1762         size_t EncBufIndex;\r
1763         size_t EncBufLen;\r
1764         unsigned char AesKey[32];\r
1765         unsigned char AesCbcIv[AES_BLOCK_SIZE];\r
1766         aes_decrypt_ctx Ctx;\r
1767 \r
1768         Buf[0] = NUL;\r
1769 \r
1770         Get = Str;\r
1771         StrLen = strlen(Str);\r
1772 \r
1773         if(AES_BLOCK_SIZE * 2 + 1 < StrLen)\r
1774         {\r
1775 \r
1776                 EncBufLen = (StrLen - 1 ) / 2 - AES_BLOCK_SIZE;\r
1777                 if((EncBuf = malloc(EncBufLen)) != NULL)\r
1778                 {\r
1779                         for(IvIndex = 0; IvIndex < AES_BLOCK_SIZE; IvIndex++)\r
1780                         {\r
1781                                 AesCbcIv[IvIndex]  = hex2bin(*Get++) << 4;\r
1782                                 AesCbcIv[IvIndex] |= hex2bin(*Get++);\r
1783                         }\r
1784 \r
1785                         if(*Get++ == ':')\r
1786                         {\r
1787                                 if(CreateAesKey(AesKey, Key) == FFFTP_SUCCESS)\r
1788                                 {\r
1789                                         aes_decrypt_key(AesKey, 32, &Ctx);\r
1790 \r
1791                                         for(EncBufIndex = 0; EncBufIndex < EncBufLen; EncBufIndex++)\r
1792                                         {\r
1793                                                 EncBuf[EncBufIndex]  = hex2bin(*Get++) << 4;\r
1794                                                 EncBuf[EncBufIndex] |= hex2bin(*Get++);\r
1795                                         }\r
1796                                         if(aes_cbc_decrypt(EncBuf, Buf, EncBufLen, AesCbcIv, &Ctx) == EXIT_SUCCESS)\r
1797                                         {\r
1798                                                 Buf[EncBufLen] = NUL;\r
1799                                         }\r
1800                                 }\r
1801                         }\r
1802                         free(EncBuf);\r
1803                 }\r
1804         }\r
1805         return;\r
1806 }\r
1807 \r
1808 /*----- AES用固定長キーを作成 ----------------------------------------------\r
1809 *\r
1810 *       Parameter\r
1811 *               unsigned char *AesKey : AES暗号鍵\r
1812 *               const char *Key : 暗号化キー\r
1813 *\r
1814 *       Return Value\r
1815 *               int ステータス (FFFTP_SUCCESS/FFFTP_FAIL)\r
1816 *       Note\r
1817 *               SHA-1をもちいて32Byte鍵を生成する\r
1818 *----------------------------------------------------------------------------*/\r
1819 \r
1820 static int CreateAesKey(unsigned char *AesKey, const char* Key)\r
1821 {\r
1822         char* HashKey;\r
1823         uint32 HashKeyLen;\r
1824         uint32 results[10];\r
1825         int ByteOffset;\r
1826         int KeyIndex;\r
1827         int ResIndex;\r
1828 \r
1829         HashKeyLen = strlen(Key) + 16;\r
1830         if((HashKey = malloc(HashKeyLen + 1)) == NULL){\r
1831                 return (FFFTP_FAIL);\r
1832         }\r
1833 \r
1834         strcpy(HashKey, Key);\r
1835         strcat(HashKey, ">g^r=@N7=//z<[`:");\r
1836     sha_memory((uchar *)HashKey, HashKeyLen, results);\r
1837 \r
1838         strcpy(HashKey, Key);\r
1839         strcat(HashKey, "VG77dO1#EyC]$|C@");\r
1840     sha_memory((uchar *)HashKey, HashKeyLen, results + 5);\r
1841 \r
1842         KeyIndex = 0;\r
1843         ResIndex = 0;\r
1844         while(ResIndex < 8){\r
1845                 for(ByteOffset = 0; ByteOffset < 4; ByteOffset++){\r
1846                         AesKey[KeyIndex++] = (results[ResIndex] >> ByteOffset * 8) & 0xff;\r
1847                 }\r
1848                 ResIndex++;\r
1849         }\r
1850         free(HashKey);\r
1851 \r
1852         return (FFFTP_SUCCESS);\r
1853 }\r
1854 \r
1855 \r
1856 /*===== レジストリとINIファイルのアクセス処理 ============*/\r
1857 \r
1858 \r
1859 /*===== INIファイル用のレジストリデータ =====*/\r
1860 \r
1861 typedef struct regdatatbl {\r
1862         char KeyName[80+1];                     /* キー名 */\r
1863         char ValTbl[REG_SECT_MAX];      /* 値のテーブル */\r
1864         int ValLen;                                     /* 値データのバイト数 */\r
1865         int Mode;                                       /* キーのモード */\r
1866         struct regdatatbl *Next;\r
1867 } REGDATATBL;\r
1868 \r
1869 /*===== プロトタイプ =====*/\r
1870 \r
1871 static BOOL WriteOutRegToFile(REGDATATBL *Pos);\r
1872 static int ReadInReg(char *Name, REGDATATBL **Handle);\r
1873 // 暗号化通信対応\r
1874 //static int StrCatOut(char *Src, int Len, char *Dst);\r
1875 //static int StrReadIn(char *Src, int Max, char *Dst);\r
1876 static char *ScanValue(void *Handle, char *Name);\r
1877 \r
1878 \r
1879 /*===== ローカルなワーク =====*/\r
1880 \r
1881 static int TmpRegType;\r
1882 \r
1883 \r
1884 \r
1885 /*----- レジストリのタイプを設定する ------------------------------------------\r
1886 *\r
1887 *       Parameter\r
1888 *               int Type : タイプ (REGTYPE_xxx)\r
1889 *\r
1890 *       Return Value\r
1891 *               int ステータス\r
1892 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1893 *----------------------------------------------------------------------------*/\r
1894 \r
1895 static void SetRegType(int Type)\r
1896 {\r
1897         TmpRegType = Type;\r
1898         return;\r
1899 }\r
1900 \r
1901 \r
1902 /*----- レジストリ/INIファイルをオープンする(読み込み)-----------------------\r
1903 *\r
1904 *       Parameter\r
1905 *               char *Name : レジストリ名\r
1906 *               void **Handle : ハンドルを返すワーク\r
1907 *\r
1908 *       Return Value\r
1909 *               int ステータス\r
1910 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1911 *----------------------------------------------------------------------------*/\r
1912 \r
1913 static int OpenReg(char *Name, void **Handle)\r
1914 {\r
1915         int Sts;\r
1916         char Tmp[FMAX_PATH+1];\r
1917 \r
1918         Sts = FFFTP_FAIL;\r
1919         if(TmpRegType == REGTYPE_REG)\r
1920         {\r
1921                 strcpy(Tmp, "Software\\Sota\\");\r
1922                 strcat(Tmp, Name);\r
1923                 if(RegOpenKeyEx(HKEY_CURRENT_USER, Tmp, 0, KEY_READ, (HKEY *)Handle) == ERROR_SUCCESS)\r
1924                         Sts = FFFTP_SUCCESS;\r
1925         }\r
1926         else\r
1927         {\r
1928                 if((Sts = ReadInReg(Name, (REGDATATBL **)Handle)) == FFFTP_SUCCESS)\r
1929                         ((REGDATATBL *)(*Handle))->Mode = 0;\r
1930         }\r
1931         return(Sts);\r
1932 }\r
1933 \r
1934 \r
1935 /*----- レジストリ/INIファイルを作成する(書き込み)---------------------------\r
1936 *\r
1937 *       Parameter\r
1938 *               char *Name : レジストリ名\r
1939 *               void **Handle : ハンドルを返すワーク\r
1940 *\r
1941 *       Return Value\r
1942 *               int ステータス\r
1943 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1944 *----------------------------------------------------------------------------*/\r
1945 \r
1946 static int CreateReg(char *Name, void **Handle)\r
1947 {\r
1948         int Sts;\r
1949         char Tmp[FMAX_PATH+1];\r
1950         DWORD Dispos;\r
1951 \r
1952         Sts = FFFTP_FAIL;\r
1953         if(TmpRegType == REGTYPE_REG)\r
1954         {\r
1955                 strcpy(Tmp, "Software\\Sota\\");\r
1956                 strcat(Tmp, Name);\r
1957                 if(RegCreateKeyEx(HKEY_CURRENT_USER, Tmp, 0, "", REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY | KEY_SET_VALUE, NULL, (HKEY *)Handle, &Dispos) == ERROR_SUCCESS)\r
1958                         Sts = FFFTP_SUCCESS;\r
1959         }\r
1960         else\r
1961         {\r
1962                 if((*Handle = malloc(sizeof(REGDATATBL))) != NULL)\r
1963                 {\r
1964                         strcpy(((REGDATATBL *)(*Handle))->KeyName, Name);\r
1965                         ((REGDATATBL *)(*Handle))->ValLen = 0;\r
1966                         ((REGDATATBL *)(*Handle))->Next = NULL;\r
1967                         ((REGDATATBL *)(*Handle))->Mode = 1;\r
1968                         Sts = FFFTP_SUCCESS;\r
1969                 }\r
1970         }\r
1971         return(Sts);\r
1972 }\r
1973 \r
1974 \r
1975 /*----- レジストリ/INIファイルをクローズする ----------------------------------\r
1976 *\r
1977 *       Parameter\r
1978 *               void *Handle : ハンドル\r
1979 *\r
1980 *       Return Value\r
1981 *               int ステータス\r
1982 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1983 *----------------------------------------------------------------------------*/\r
1984 \r
1985 static int CloseReg(void *Handle)\r
1986 {\r
1987         REGDATATBL *Pos;\r
1988         REGDATATBL *Next;\r
1989         // ポータブル版判定\r
1990 //      FILE *Strm;\r
1991 \r
1992         if(TmpRegType == REGTYPE_REG)\r
1993         {\r
1994                 RegCloseKey(Handle);\r
1995 \r
1996                 /* INIファイルを削除 */\r
1997                 // ポータブル版判定\r
1998 //              if((Strm = fopen(AskIniFilePath(), "rt")) != NULL)\r
1999 //              {\r
2000 //                      fclose(Strm);\r
2001 //                      MoveFileToTrashCan(AskIniFilePath());\r
2002 //              }\r
2003         }\r
2004         else\r
2005         {\r
2006                 if(((REGDATATBL *)Handle)->Mode == 1)\r
2007                 {\r
2008                         if(WriteOutRegToFile(Handle) == TRUE)\r
2009                         {\r
2010 //                              /* レジストリをクリア */\r
2011 //                              ClearRegistry();\r
2012                         }\r
2013                 }\r
2014                 /* テーブルを削除 */\r
2015                 Pos = Handle;\r
2016                 while(Pos != NULL)\r
2017                 {\r
2018                         Next = Pos->Next;\r
2019                         free(Pos);\r
2020                         Pos = Next;\r
2021                 }\r
2022         }\r
2023         return(FFFTP_SUCCESS);\r
2024 }\r
2025 \r
2026 \r
2027 /*----- レジストリ情報をINIファイルに書き込む ---------------------------------\r
2028 *\r
2029 *       Parameter\r
2030 *               REGDATATBL *Pos : レジストリデータ\r
2031 *\r
2032 *       Return Value\r
2033 *               なし\r
2034 *----------------------------------------------------------------------------*/\r
2035 \r
2036 static BOOL WriteOutRegToFile(REGDATATBL *Pos)\r
2037 {\r
2038         FILE *Strm;\r
2039         char *Disp;\r
2040         BOOL Ret;\r
2041 \r
2042         Ret = FALSE;\r
2043         if((Strm = fopen(AskIniFilePath(), "wt")) != NULL)\r
2044         {\r
2045                 fprintf(Strm, MSGJPN239);\r
2046                 while(Pos != NULL)\r
2047                 {\r
2048                         fprintf(Strm, "\n[%s]\n", Pos->KeyName);\r
2049 \r
2050                         Disp = Pos->ValTbl;\r
2051                         while(Disp < (Pos->ValTbl + Pos->ValLen))\r
2052                         {\r
2053                                 fprintf(Strm, "%s\n", Disp);\r
2054                                 Disp = Disp + strlen(Disp) + 1;\r
2055                         }\r
2056                         Pos = Pos->Next;\r
2057                 }\r
2058                 fclose(Strm);\r
2059                 Ret = TRUE;\r
2060         }\r
2061         else\r
2062                 MessageBox(NULL, MSGJPN240, "FFFTP", MB_OK);\r
2063 \r
2064         return(Ret);\r
2065 }\r
2066 \r
2067 \r
2068 /*----- INIファイルからレジストリ情報を読み込む -------------------------------\r
2069 *\r
2070 *       Parameter\r
2071 *               char *Name : 名前\r
2072 *               void *Handle : ハンドル\r
2073 *\r
2074 *       Return Value\r
2075 *               int ステータス\r
2076 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2077 *----------------------------------------------------------------------------*/\r
2078 \r
2079 static int ReadInReg(char *Name, REGDATATBL **Handle)\r
2080 {\r
2081         FILE *Strm;\r
2082         char *Buf;\r
2083         char *Tmp;\r
2084         char *Data;\r
2085         REGDATATBL *New;\r
2086         REGDATATBL *Pos;\r
2087         int Sts;\r
2088 \r
2089         Sts = FFFTP_FAIL;\r
2090         *Handle = NULL;\r
2091 \r
2092         if((Strm = fopen(AskIniFilePath(), "rt")) != NULL)\r
2093         {\r
2094                 if((Buf = malloc(REG_SECT_MAX)) != NULL)\r
2095                 {\r
2096                         while(fgets(Buf, REG_SECT_MAX, Strm) != NULL)\r
2097                         {\r
2098                                 if(*Buf != '#')\r
2099                                 {\r
2100                                         if((Tmp = strchr(Buf, '\n')) != NULL)\r
2101                                                 *Tmp = NUL;\r
2102 \r
2103                                         if(*Buf == '[')\r
2104                                         {\r
2105                                                 if((New = malloc(sizeof(REGDATATBL))) != NULL)\r
2106                                                 {\r
2107                                                         if((Tmp = strchr(Buf, ']')) != NULL)\r
2108                                                                 *Tmp = NUL;\r
2109                                                         strcpy(New->KeyName, Buf+1);\r
2110                                                         New->ValLen = 0;\r
2111                                                         New->Next = NULL;\r
2112                                                         Data = New->ValTbl;\r
2113                                                 }\r
2114                                                 if(*Handle == NULL)\r
2115                                                         *Handle = New;\r
2116                                                 else\r
2117                                                 {\r
2118                                                         Pos = *Handle;\r
2119                                                         while(Pos->Next != NULL)\r
2120                                                                 Pos = Pos->Next;\r
2121                                                         Pos->Next = New;\r
2122                                                 }\r
2123                                         }\r
2124                                         else if(strlen(Buf) > 0)\r
2125                                         {\r
2126                                                 strcpy(Data, Buf);\r
2127                                                 Data += strlen(Buf) + 1;\r
2128                                                 New->ValLen += strlen(Buf) + 1;\r
2129                                         }\r
2130                                 }\r
2131                         }\r
2132                         Sts = FFFTP_SUCCESS;\r
2133                         free(Buf);\r
2134                 }\r
2135                 fclose(Strm);\r
2136         }\r
2137         return(Sts);\r
2138 }\r
2139 \r
2140 \r
2141 /*----- サブキーをオープンする ------------------------------------------------\r
2142 *\r
2143 *       Parameter\r
2144 *               void *Parent : 親のハンドル\r
2145 *               char *Name : 名前\r
2146 *               void **Handle : ハンドルを返すワーク\r
2147 *\r
2148 *       Return Value\r
2149 *               int ステータス\r
2150 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2151 *----------------------------------------------------------------------------*/\r
2152 \r
2153 static int OpenSubKey(void *Parent, char *Name, void **Handle)\r
2154 {\r
2155         int Sts;\r
2156         char Key[80];\r
2157         REGDATATBL *Pos;\r
2158 \r
2159         Sts = FFFTP_FAIL;\r
2160         if(TmpRegType == REGTYPE_REG)\r
2161         {\r
2162                 if(RegOpenKeyEx(Parent, Name, 0, KEY_READ, (HKEY *)Handle) == ERROR_SUCCESS)\r
2163                         Sts = FFFTP_SUCCESS;\r
2164         }\r
2165         else\r
2166         {\r
2167                 strcpy(Key, ((REGDATATBL *)Parent)->KeyName);\r
2168                 strcat(Key, "\\");\r
2169                 strcat(Key, Name);\r
2170                 Pos = Parent;\r
2171                 while(Pos != NULL)\r
2172                 {\r
2173                         if(strcmp(Pos->KeyName, Key) == 0)\r
2174                         {\r
2175                                 *Handle = Pos;\r
2176                                 Sts = FFFTP_SUCCESS;\r
2177                                 break;\r
2178                         }\r
2179                         Pos = Pos->Next;\r
2180                 }\r
2181         }\r
2182         return(Sts);\r
2183 }\r
2184 \r
2185 \r
2186 /*----- サブキーを作成する ----------------------------------------------------\r
2187 *\r
2188 *       Parameter\r
2189 *               void *Parent : 親のハンドル\r
2190 *               char *Name : 名前\r
2191 *               void **Handle : ハンドルを返すワーク\r
2192 *\r
2193 *       Return Value\r
2194 *               int ステータス\r
2195 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2196 *----------------------------------------------------------------------------*/\r
2197 \r
2198 static int CreateSubKey(void *Parent, char *Name, void **Handle)\r
2199 {\r
2200         int Sts;\r
2201         DWORD Dispos;\r
2202         REGDATATBL *Pos;\r
2203 \r
2204         Sts = FFFTP_FAIL;\r
2205         if(TmpRegType == REGTYPE_REG)\r
2206         {\r
2207                 if(RegCreateKeyEx(Parent, Name, 0, "", REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, (HKEY *)Handle, &Dispos) == ERROR_SUCCESS)\r
2208                         Sts = FFFTP_SUCCESS;\r
2209         }\r
2210         else\r
2211         {\r
2212                 if((*Handle = malloc(sizeof(REGDATATBL))) != NULL)\r
2213                 {\r
2214                         strcpy(((REGDATATBL *)(*Handle))->KeyName, ((REGDATATBL *)Parent)->KeyName);\r
2215                         strcat(((REGDATATBL *)(*Handle))->KeyName, "\\");\r
2216                         strcat(((REGDATATBL *)(*Handle))->KeyName, Name);\r
2217 \r
2218                         ((REGDATATBL *)(*Handle))->ValLen = 0;\r
2219                         ((REGDATATBL *)(*Handle))->Next = NULL;\r
2220 \r
2221                         Pos = (REGDATATBL *)Parent;\r
2222                         while(Pos->Next != NULL)\r
2223                                 Pos = Pos->Next;\r
2224                         Pos->Next = *Handle;\r
2225                         Sts = FFFTP_SUCCESS;\r
2226                 }\r
2227         }\r
2228         return(Sts);\r
2229 }\r
2230 \r
2231 \r
2232 /*----- サブキーをクローズする ------------------------------------------------\r
2233 *\r
2234 *       Parameter\r
2235 *               void *Handle : ハンドル\r
2236 *\r
2237 *       Return Value\r
2238 *               int ステータス\r
2239 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2240 *----------------------------------------------------------------------------*/\r
2241 \r
2242 static int CloseSubKey(void *Handle)\r
2243 {\r
2244         if(TmpRegType == REGTYPE_REG)\r
2245                 RegCloseKey(Handle);\r
2246         else\r
2247         {\r
2248                 /* Nothing */\r
2249         }\r
2250         return(FFFTP_SUCCESS);\r
2251 }\r
2252 \r
2253 \r
2254 /*----- サブキーを削除する ----------------------------------------------------\r
2255 *\r
2256 *       Parameter\r
2257 *               void *Handle : ハンドル\r
2258 *               char *Name : 名前\r
2259 *\r
2260 *       Return Value\r
2261 *               int ステータス\r
2262 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2263 *----------------------------------------------------------------------------*/\r
2264 \r
2265 static int DeleteSubKey(void *Handle, char *Name)\r
2266 {\r
2267         int Sts;\r
2268 \r
2269         Sts = FFFTP_FAIL;\r
2270         if(TmpRegType == REGTYPE_REG)\r
2271         {\r
2272                 if(RegDeleteKey(Handle, Name) == ERROR_SUCCESS)\r
2273                         Sts = FFFTP_SUCCESS;\r
2274         }\r
2275         else\r
2276         {\r
2277                 Sts = FFFTP_FAIL;\r
2278         }\r
2279         return(Sts);\r
2280 }\r
2281 \r
2282 \r
2283 /*----- 値を削除する ----------------------------------------------------------\r
2284 *\r
2285 *       Parameter\r
2286 *               void *Handle : ハンドル\r
2287 *               char *Name : 名前\r
2288 *\r
2289 *       Return Value\r
2290 *               int ステータス\r
2291 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2292 *----------------------------------------------------------------------------*/\r
2293 \r
2294 static int DeleteValue(void *Handle, char *Name)\r
2295 {\r
2296         int Sts;\r
2297 \r
2298         Sts = FFFTP_FAIL;\r
2299         if(TmpRegType == REGTYPE_REG)\r
2300         {\r
2301                 if(RegDeleteValue(Handle, Name) == ERROR_SUCCESS)\r
2302                         Sts = FFFTP_SUCCESS;\r
2303         }\r
2304         else\r
2305         {\r
2306                 Sts = FFFTP_FAIL;\r
2307         }\r
2308         return(Sts);\r
2309 }\r
2310 \r
2311 \r
2312 /*----- INT値を読み込む -------------------------------------------------------\r
2313 *\r
2314 *       Parameter\r
2315 *               void *Handle : ハンドル\r
2316 *               char *Name : 名前\r
2317 *               int *Value : INT値を返すワーク\r
2318 *\r
2319 *       Return Value\r
2320 *               int ステータス\r
2321 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2322 *----------------------------------------------------------------------------*/\r
2323 \r
2324 static int ReadIntValueFromReg(void *Handle, char *Name, int *Value)\r
2325 {\r
2326         int Sts;\r
2327         DWORD Size;\r
2328         char *Pos;\r
2329 \r
2330         Sts = FFFTP_FAIL;\r
2331         if(TmpRegType == REGTYPE_REG)\r
2332         {\r
2333                 Size = sizeof(int);\r
2334                 if(RegQueryValueEx(Handle, Name, NULL, NULL, (BYTE *)Value, &Size) == ERROR_SUCCESS)\r
2335                         Sts = FFFTP_SUCCESS;\r
2336         }\r
2337         else\r
2338         {\r
2339                 if((Pos = ScanValue(Handle, Name)) != NULL)\r
2340                 {\r
2341                         *Value = atoi(Pos);\r
2342                         Sts = FFFTP_SUCCESS;\r
2343                 }\r
2344         }\r
2345         return(Sts);\r
2346 }\r
2347 \r
2348 \r
2349 /*----- INT値を書き込む -------------------------------------------------------\r
2350 *\r
2351 *       Parameter\r
2352 *               void *Handle : ハンドル\r
2353 *               char *Name : 名前\r
2354 *               int Value : INT値\r
2355 *\r
2356 *       Return Value\r
2357 *               int ステータス\r
2358 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2359 *----------------------------------------------------------------------------*/\r
2360 \r
2361 static int WriteIntValueToReg(void *Handle, char *Name, int Value)\r
2362 {\r
2363         REGDATATBL *Pos;\r
2364         char *Data;\r
2365         char Tmp[20];\r
2366 \r
2367         if(TmpRegType == REGTYPE_REG)\r
2368                 RegSetValueEx(Handle, Name, 0, REG_DWORD, (CONST BYTE *)&Value, sizeof(int));\r
2369         else\r
2370         {\r
2371                 Pos = (REGDATATBL *)Handle;\r
2372                 Data = Pos->ValTbl + Pos->ValLen;\r
2373                 strcpy(Data, Name);\r
2374                 strcat(Data, "=");\r
2375                 sprintf(Tmp, "%d", Value);\r
2376                 strcat(Data, Tmp);\r
2377                 Pos->ValLen += strlen(Data) + 1;\r
2378         }\r
2379         return(FFFTP_SUCCESS);\r
2380 }\r
2381 \r
2382 \r
2383 /*----- 文字列を読み込む ------------------------------------------------------\r
2384 *\r
2385 *       Parameter\r
2386 *               void *Handle : ハンドル\r
2387 *               char *Name : 名前\r
2388 *               char *Str : 文字列を返すワーク\r
2389 *               DWORD Size : 最大サイズ\r
2390 *\r
2391 *       Return Value\r
2392 *               int ステータス\r
2393 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2394 *----------------------------------------------------------------------------*/\r
2395 \r
2396 static int ReadStringFromReg(void *Handle, char *Name, char *Str, DWORD Size)\r
2397 {\r
2398         int Sts;\r
2399         char *Pos;\r
2400         // UTF-8対応\r
2401         DWORD TempSize;\r
2402         char* pa0;\r
2403         wchar_t* pw0;\r
2404 \r
2405         Sts = FFFTP_FAIL;\r
2406         if(TmpRegType == REGTYPE_REG)\r
2407         {\r
2408                 if(RegQueryValueEx(Handle, Name, NULL, NULL, (BYTE *)Str, &Size) == ERROR_SUCCESS)\r
2409                 {\r
2410                         if(*(Str + Size - 1) != NUL)\r
2411                                 *(Str + Size) = NUL;\r
2412                         Sts = FFFTP_SUCCESS;\r
2413                 }\r
2414         }\r
2415         else\r
2416         {\r
2417                 if((Pos = ScanValue(Handle, Name)) != NULL)\r
2418                 {\r
2419                         // UTF-8対応\r
2420 //                      Size = min1(Size-1, strlen(Pos));\r
2421 //                      Size = StrReadIn(Pos, Size, Str);\r
2422 //                      *(Str + Size) = NUL;\r
2423 //                      Sts = FFFTP_SUCCESS;\r
2424                         switch(IniKanjiCode)\r
2425                         {\r
2426                         case KANJI_NOCNV:\r
2427                                 TempSize = min1(Size-1, strlen(Pos));\r
2428                                 TempSize = StrReadIn(Pos, TempSize, Str);\r
2429                                 *(Str + TempSize) = NUL;\r
2430                                 Sts = FFFTP_SUCCESS;\r
2431                                 if(!CheckStringM(Str))\r
2432                                         break;\r
2433                                 // UTF-8ではない可能性がある\r
2434                                 // Shift_JISとみなす\r
2435                         case KANJI_SJIS:\r
2436                                 if(pa0 = AllocateStringA(Size * 4))\r
2437                                 {\r
2438                                         if(pw0 = AllocateStringW(Size * 4 * 4))\r
2439                                         {\r
2440                                                 TempSize = min1((Size * 4) - 1, strlen(Pos));\r
2441                                                 TempSize = StrReadIn(Pos, TempSize, pa0);\r
2442                                                 *(pa0 + TempSize) = NUL;\r
2443                                                 AtoW(pw0, Size * 4 * 4, pa0, -1);\r
2444                                                 WtoM(Str, Size, pw0, -1);\r
2445                                                 TerminateStringM(Str, Size);\r
2446                                                 Sts = FFFTP_SUCCESS;\r
2447                                                 FreeDuplicatedString(pw0);\r
2448                                         }\r
2449                                         FreeDuplicatedString(pa0);\r
2450                                 }\r
2451                                 break;\r
2452                         }\r
2453                 }\r
2454         }\r
2455         return(Sts);\r
2456 }\r
2457 \r
2458 \r
2459 /*----- 文字列を書き込む ------------------------------------------------------\r
2460 *\r
2461 *       Parameter\r
2462 *               void *Handle : ハンドル\r
2463 *               char *Name : 名前\r
2464 *               char *Str :文字列\r
2465 *\r
2466 *       Return Value\r
2467 *               int ステータス\r
2468 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2469 *----------------------------------------------------------------------------*/\r
2470 \r
2471 static int WriteStringToReg(void *Handle, char *Name, char *Str)\r
2472 {\r
2473         REGDATATBL *Pos;\r
2474         char *Data;\r
2475 \r
2476         if(TmpRegType == REGTYPE_REG)\r
2477                 RegSetValueEx(Handle, Name, 0, REG_SZ, (CONST BYTE *)Str, strlen(Str)+1);\r
2478         else\r
2479         {\r
2480                 Pos = (REGDATATBL *)Handle;\r
2481                 Data = Pos->ValTbl + Pos->ValLen;\r
2482                 strcpy(Data, Name);\r
2483                 strcat(Data, "=");\r
2484                 Pos->ValLen += strlen(Data);\r
2485                 Data = Pos->ValTbl + Pos->ValLen;\r
2486                 Pos->ValLen += StrCatOut(Str, strlen(Str), Data) + 1;\r
2487         }\r
2488         return(FFFTP_SUCCESS);\r
2489 }\r
2490 \r
2491 \r
2492 /*----- マルチ文字列を読み込む ------------------------------------------------\r
2493 *\r
2494 *       Parameter\r
2495 *               void *Handle : ハンドル\r
2496 *               char *Name : 名前\r
2497 *               char *Str : 文字列を返すワーク\r
2498 *               DWORD Size : 最大サイズ\r
2499 *\r
2500 *       Return Value\r
2501 *               int ステータス\r
2502 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2503 *----------------------------------------------------------------------------*/\r
2504 \r
2505 static int ReadMultiStringFromReg(void *Handle, char *Name, char *Str, DWORD Size)\r
2506 {\r
2507         int Sts;\r
2508         char *Pos;\r
2509         // UTF-8対応\r
2510         DWORD TempSize;\r
2511         char* pa0;\r
2512         wchar_t* pw0;\r
2513 \r
2514         Sts = FFFTP_FAIL;\r
2515         if(TmpRegType == REGTYPE_REG)\r
2516         {\r
2517                 if(RegQueryValueEx(Handle, Name, NULL, NULL, (BYTE *)Str, &Size) == ERROR_SUCCESS)\r
2518                 {\r
2519                         if(*(Str + Size - 1) != NUL)\r
2520                                 *(Str + Size) = NUL;\r
2521                         Sts = FFFTP_SUCCESS;\r
2522                 }\r
2523         }\r
2524         else\r
2525         {\r
2526                 if((Pos = ScanValue(Handle, Name)) != NULL)\r
2527                 {\r
2528                         // UTF-8対応\r
2529 //                      Size = min1(Size-1, strlen(Pos));\r
2530 //                      Size = StrReadIn(Pos, Size, Str);\r
2531 //                      *(Str + Size) = NUL;\r
2532 //                      Sts = FFFTP_SUCCESS;\r
2533                         switch(IniKanjiCode)\r
2534                         {\r
2535                         case KANJI_NOCNV:\r
2536                                 TempSize = min1(Size - 2, strlen(Pos));\r
2537                                 TempSize = StrReadIn(Pos, TempSize, Str);\r
2538                                 *(Str + TempSize) = NUL;\r
2539                                 *(Str + TempSize + 1) = NUL;\r
2540                                 Sts = FFFTP_SUCCESS;\r
2541                                 if(!CheckMultiStringM(Str))\r
2542                                         break;\r
2543                                 // UTF-8ではない可能性がある\r
2544                                 // Shift_JISとみなす\r
2545                         case KANJI_SJIS:\r
2546                                 if(pa0 = AllocateStringA(Size * 4))\r
2547                                 {\r
2548                                         if(pw0 = AllocateStringW(Size * 4 * 4))\r
2549                                         {\r
2550                                                 TempSize = min1((Size * 4) - 2, strlen(Pos));\r
2551                                                 TempSize = StrReadIn(Pos, TempSize, pa0);\r
2552                                                 *(pa0 + TempSize) = NUL;\r
2553                                                 *(pa0 + TempSize + 1) = NUL;\r
2554                                                 AtoWMultiString(pw0, Size * 4 * 4, pa0);\r
2555                                                 WtoMMultiString(Str, Size, pw0);\r
2556                                                 TerminateStringM(Str, Size);\r
2557                                                 TerminateStringM(Str, Size - 1);\r
2558                                                 Sts = FFFTP_SUCCESS;\r
2559                                                 FreeDuplicatedString(pw0);\r
2560                                         }\r
2561                                         FreeDuplicatedString(pa0);\r
2562                                 }\r
2563                                 break;\r
2564                         }\r
2565                 }\r
2566         }\r
2567         return(Sts);\r
2568 }\r
2569 \r
2570 \r
2571 /*----- マルチ文字列を書き込む ------------------------------------------------\r
2572 *\r
2573 *       Parameter\r
2574 *               void *Handle : ハンドル\r
2575 *               char *Name : 名前\r
2576 *               char *Str : 文字列\r
2577 *\r
2578 *       Return Value\r
2579 *               int ステータス\r
2580 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2581 *----------------------------------------------------------------------------*/\r
2582 \r
2583 static int WriteMultiStringToReg(void *Handle, char *Name, char *Str)\r
2584 {\r
2585         REGDATATBL *Pos;\r
2586         char *Data;\r
2587 \r
2588         if(TmpRegType == REGTYPE_REG)\r
2589                 RegSetValueEx(Handle, Name, 0, REG_MULTI_SZ, (CONST BYTE *)Str, StrMultiLen(Str)+1);\r
2590         else\r
2591         {\r
2592                 Pos = (REGDATATBL *)Handle;\r
2593                 Data = Pos->ValTbl + Pos->ValLen;\r
2594                 strcpy(Data, Name);\r
2595                 strcat(Data, "=");\r
2596                 Pos->ValLen += strlen(Data);\r
2597                 Data = Pos->ValTbl + Pos->ValLen;\r
2598                 Pos->ValLen += StrCatOut(Str, StrMultiLen(Str), Data) + 1;\r
2599         }\r
2600         return(FFFTP_SUCCESS);\r
2601 }\r
2602 \r
2603 \r
2604 /*----- バイナリを読み込む-----------------------------------------------------\r
2605 *\r
2606 *       Parameter\r
2607 *               void *Handle : ハンドル\r
2608 *               char *Name : 名前\r
2609 *               void *Bin : バイナリを返すワーク\r
2610 *               DWORD Size : 最大サイズ\r
2611 *\r
2612 *       Return Value\r
2613 *               int ステータス\r
2614 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2615 *----------------------------------------------------------------------------*/\r
2616 \r
2617 static int ReadBinaryFromReg(void *Handle, char *Name, void *Bin, DWORD Size)\r
2618 {\r
2619         int Sts;\r
2620         char *Pos;\r
2621 \r
2622         Sts = FFFTP_FAIL;\r
2623         if(TmpRegType == REGTYPE_REG)\r
2624         {\r
2625                 if(RegQueryValueEx(Handle, Name, NULL, NULL, (BYTE *)Bin, &Size) == ERROR_SUCCESS)\r
2626                         Sts = FFFTP_SUCCESS;\r
2627         }\r
2628         else\r
2629         {\r
2630                 if((Pos = ScanValue(Handle, Name)) != NULL)\r
2631                 {\r
2632                         Size = min1(Size, strlen(Pos));\r
2633                         Size = StrReadIn(Pos, Size, Bin);\r
2634                         Sts = FFFTP_SUCCESS;\r
2635                 }\r
2636         }\r
2637         return(Sts);\r
2638 }\r
2639 \r
2640 \r
2641 /*----- バイナリを書き込む ----------------------------------------------------\r
2642 *\r
2643 *       Parameter\r
2644 *               void *Handle : ハンドル\r
2645 *               char *Name : 名前\r
2646 *               void *Bin : バイナリ\r
2647 *               int Len : 長さ\r
2648 *\r
2649 *       Return Value\r
2650 *               int ステータス\r
2651 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2652 *----------------------------------------------------------------------------*/\r
2653 \r
2654 static int WriteBinaryToReg(void *Handle, char *Name, void *Bin, int Len)\r
2655 {\r
2656         REGDATATBL *Pos;\r
2657         char *Data;\r
2658 \r
2659         if(TmpRegType == REGTYPE_REG)\r
2660                 RegSetValueEx(Handle, Name, 0, REG_BINARY, (CONST BYTE *)Bin, Len);\r
2661         else\r
2662         {\r
2663                 Pos = (REGDATATBL *)Handle;\r
2664                 Data = Pos->ValTbl + Pos->ValLen;\r
2665                 strcpy(Data, Name);\r
2666                 strcat(Data, "=");\r
2667                 Pos->ValLen += strlen(Data);\r
2668                 Data = Pos->ValTbl + Pos->ValLen;\r
2669                 Pos->ValLen += StrCatOut(Bin, Len, Data) + 1;\r
2670         }\r
2671         return(FFFTP_SUCCESS);\r
2672 }\r
2673 \r
2674 \r
2675 /*----- 文字列をバッファに追加書き込みする ------------------------------------\r
2676 *\r
2677 *       Parameter\r
2678 *               char *Src : 文字列\r
2679 *               int len : 文字列の長さ\r
2680 *               char *Dst : 書き込みするバッファ\r
2681 *\r
2682 *       Return Value\r
2683 *               int 追加したバイト数\r
2684 *----------------------------------------------------------------------------*/\r
2685 \r
2686 static int StrCatOut(char *Src, int Len, char *Dst)\r
2687 {\r
2688         int Count;\r
2689 \r
2690         Dst += strlen(Dst);\r
2691         Count = 0;\r
2692         for(; Len > 0; Len--)\r
2693         {\r
2694                 if(*Src == '\\')\r
2695                 {\r
2696                         *Dst++ = '\\';\r
2697                         *Dst++ = '\\';\r
2698                         Count += 2;\r
2699                 }\r
2700                 else if((*Src >= 0x20) && (*Src <= 0x7E))\r
2701                 {\r
2702                         *Dst++ = *Src;\r
2703                         Count++;\r
2704                 }\r
2705                 else\r
2706                 {\r
2707                         sprintf(Dst, "\\%02X", *(unsigned char *)Src);\r
2708                         Dst += 3;\r
2709                         Count += 3;\r
2710                 }\r
2711                 Src++;\r
2712         }\r
2713         *Dst = NUL;\r
2714         return(Count);\r
2715 }\r
2716 \r
2717 \r
2718 /*----- 文字列をバッファに読み込む --------------------------------------------\r
2719 *\r
2720 *       Parameter\r
2721 *               char *Src : 文字列\r
2722 *               int Max : 最大サイズ\r
2723 *               char *Dst : 書き込みするバッファ\r
2724 *\r
2725 *       Return Value\r
2726 *               int 読み込んだバイト数\r
2727 *----------------------------------------------------------------------------*/\r
2728 \r
2729 static int StrReadIn(char *Src, int Max, char *Dst)\r
2730 {\r
2731         int Count;\r
2732         int Tmp;\r
2733 \r
2734         Count = 0;\r
2735         while(*Src != NUL)\r
2736         {\r
2737                 if(Count >= Max)\r
2738                         break;\r
2739 \r
2740                 if(*Src == '\\')\r
2741                 {\r
2742                         Src++;\r
2743                         if(*Src == '\\')\r
2744                                 *Dst = '\\';\r
2745                         else\r
2746                         {\r
2747                                 sscanf(Src, "%02x", &Tmp);\r
2748                                 *Dst = Tmp;\r
2749                                 Src++;\r
2750                         }\r
2751                 }\r
2752                 else\r
2753                         *Dst = *Src;\r
2754 \r
2755                 Count++;\r
2756                 Dst++;\r
2757                 Src++;\r
2758         }\r
2759         return(Count);\r
2760 }\r
2761 \r
2762 \r
2763 /*----- 値を検索する ----------------------------------------------------------\r
2764 *\r
2765 *       Parameter\r
2766 *               char *Handle : ハンドル\r
2767 *               char *Name : 名前\r
2768 *\r
2769 *       Return Value\r
2770 *               char *値データの先頭\r
2771 *                       NULL=指定の名前の値が見つからない\r
2772 *----------------------------------------------------------------------------*/\r
2773 \r
2774 static char *ScanValue(void *Handle, char *Name)\r
2775 {\r
2776         REGDATATBL *Cur;\r
2777         char *Pos;\r
2778         char *Ret;\r
2779 \r
2780         Ret = NULL;\r
2781         Cur = Handle;\r
2782         Pos = Cur->ValTbl;\r
2783         while(Pos < (Cur->ValTbl + Cur->ValLen))\r
2784         {\r
2785                 if((strncmp(Name, Pos, strlen(Name)) == 0) &&\r
2786                    (*(Pos + strlen(Name)) == '='))\r
2787                 {\r
2788                         Ret = Pos + strlen(Name) + 1;\r
2789                         break;\r
2790                 }\r
2791                 Pos += strlen(Pos) + 1;\r
2792         }\r
2793         return(Ret);\r
2794 }\r
2795 \r
2796 \r
2797 /*----------- パスワードの妥当性を確認する ------------------------------------\r
2798 *\r
2799 *       Parameter\r
2800 *               char *Password: パスワード文字列\r
2801 *               char *HashStr: SHA-1ハッシュ文字列\r
2802 *\r
2803 *       Return Value\r
2804 *               int 0 不一致\r
2805 *                       1: 一致\r
2806 *                       2: 異常\r
2807 *----------------------------------------------------------------------------*/\r
2808 int CheckPasswordValidity( char* Password, int length, const char* HashStr )\r
2809 {\r
2810         ulong hash1[5];\r
2811         ulong hash2[5];\r
2812         \r
2813         int i, j;\r
2814         \r
2815         const char* p = HashStr;\r
2816         \r
2817         /* 空文字列は一致したことにする */\r
2818         if( HashStr[0] == NUL ) return 1;\r
2819 \r
2820         /* Hashをチェックするする*/\r
2821         if( strlen(HashStr) != 40 )     return 2;\r
2822 \r
2823         /* Hashをデコードする*/\r
2824         for( i = 0; i < 5; i++ ){\r
2825                 ulong decode = 0;\r
2826                 for( j = 0; j < 8; j++ ){\r
2827                         if( *p < 0x40 || 0x40 + 15 < *p ){\r
2828                                 return 2;\r
2829                         }\r
2830                         decode = (decode << 4 ) + (*p - 0x40);\r
2831                         ++p;\r
2832                 }\r
2833                 hash1[i] = decode;\r
2834         }\r
2835         \r
2836         /* Password をハッシュする */\r
2837         sha_memory( Password, length, hash2 );\r
2838         \r