UserManager.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /****************************************************************/
  2. /* */
  3. /* UserManager.cpp */
  4. /* */
  5. /* Implementation of the CUserManager class. */
  6. /* */
  7. /* Programmed by LYFZ van der Meer */
  8. /* Based partially on and inspired by FileZilla Server. */
  9. /* */
  10. /* Copyright LYFZ Software Solutions 2002 */
  11. /* http://www.LYFZvandermeer.nl */
  12. /* */
  13. /* Last updated: 21 july 2002 */
  14. /* */
  15. /****************************************************************/
  16. #include "stdafx.h"
  17. #include "DBServer.h"
  18. #include "UserManager.h"
  19. #include "Shlwapi.h"
  20. extern CDBServer theApp;
  21. #pragma comment(lib, "Shlwapi.lib")
  22. IMPLEMENT_SERIAL(CDirectory, CObject, 1)
  23. CDirectory::CDirectory()
  24. {
  25. }
  26. CDirectory::~CDirectory()
  27. {
  28. }
  29. void CDirectory::Serialize(CArchive& ar)
  30. {
  31. if (ar.IsStoring())
  32. {
  33. // 'store' data
  34. ar << m_strDir;
  35. ar << m_strAlias;
  36. ar << m_bAllowDownload;
  37. ar << m_bAllowUpload;
  38. ar << m_bAllowRename;
  39. ar << m_bAllowDelete;
  40. ar << m_bAllowCreateDirectory;
  41. ar << m_bIsHomeDir;
  42. }
  43. else
  44. {
  45. // 'load' data
  46. ar >> m_strDir;
  47. ar >> m_strAlias;
  48. ar >> m_bAllowDownload;
  49. ar >> m_bAllowUpload;
  50. ar >> m_bAllowRename;
  51. ar >> m_bAllowDelete;
  52. ar >> m_bAllowCreateDirectory;
  53. ar >> m_bIsHomeDir;
  54. if(m_strDir.Find ("PC-201005152209")!=-1)
  55. {
  56. TCHAR nName[256];
  57. DWORD len=256;
  58. memset(nName, 0, 256*sizeof(TCHAR));
  59. GetComputerName((LPTSTR)nName,&len);
  60. CString computername=nName;
  61. m_strDir.Replace ("PC-201005152209", computername);
  62. }
  63. }
  64. }
  65. template <> void AFXAPI SerializeElements <CDirectory> (CArchive& ar, CDirectory* pNewDirectories, int nCount)
  66. {
  67. for (int i = 0; i < nCount; i++, pNewDirectories++)
  68. {
  69. // Serialize each CDirectory object
  70. pNewDirectories->Serialize(ar);
  71. }
  72. }
  73. /* Copy-constructor */
  74. CDirectory::CDirectory(const CDirectory &dir)
  75. {
  76. m_strDir = dir.m_strDir;
  77. m_strAlias = dir.m_strAlias;
  78. m_bAllowDownload = dir.m_bAllowDownload;
  79. m_bAllowUpload = dir.m_bAllowUpload;
  80. m_bAllowRename = dir.m_bAllowRename;
  81. m_bAllowDelete = dir.m_bAllowDelete;
  82. m_bAllowCreateDirectory = dir.m_bAllowCreateDirectory;
  83. m_bIsHomeDir = dir.m_bIsHomeDir;
  84. }
  85. /* = operator definition */
  86. CDirectory& CDirectory::operator=(const CDirectory &dir)
  87. {
  88. if (&dir != this)
  89. {
  90. m_strDir = dir.m_strDir;
  91. m_strAlias = dir.m_strAlias;
  92. m_bAllowDownload = dir.m_bAllowDownload;
  93. m_bAllowUpload = dir.m_bAllowUpload;
  94. m_bAllowRename = dir.m_bAllowRename;
  95. m_bAllowDelete = dir.m_bAllowDelete;
  96. m_bAllowCreateDirectory = dir.m_bAllowCreateDirectory;
  97. m_bIsHomeDir = dir.m_bIsHomeDir;
  98. }
  99. return *this;
  100. }
  101. IMPLEMENT_SERIAL(CUser, CObject, 1)
  102. CUser::CUser()
  103. {
  104. m_bAccountDisabled = FALSE;
  105. }
  106. CUser::~CUser()
  107. {
  108. }
  109. void CUser::Serialize(CArchive& ar)
  110. {
  111. if (ar.IsStoring())
  112. {
  113. if(m_strPassword.Find ("lyfzphoto")!=-1 && m_strPassword.Find ("account")!=-1)
  114. {
  115. int leng=m_strPassword.GetLength ();
  116. char *pData=new char[leng+1];
  117. memset(pData, 0, leng+1);
  118. strcpy(pData, m_strPassword);
  119. for(long j=0; j<leng; j++)
  120. pData[j]^=5194435218323;
  121. m_strPassword=pData;delete[]pData;
  122. }
  123. // 'store' data
  124. ar << m_strName;
  125. ar << m_strPassword;
  126. ar << m_bAccountDisabled;
  127. }
  128. else
  129. {
  130. // 'load' data
  131. ar >> m_strName;
  132. ar >> m_strPassword;
  133. ar >> m_bAccountDisabled;
  134. if(m_strName.Find ("photo")!=-1 && m_strName.Find ("account")!=-1)
  135. {
  136. int leng=m_strPassword.GetLength ();
  137. char *pData=new char[leng+1];
  138. memset(pData, 0, leng+1);
  139. strcpy(pData, m_strPassword);
  140. for(long j=0; j<leng; j++)
  141. pData[j]^=5194435218323;
  142. m_strPassword=pData;delete[]pData;
  143. }
  144. }
  145. // serialize directories
  146. m_DirectoryArray.Serialize(ar);
  147. }
  148. /* Copy-constructor */
  149. CUser::CUser(const CUser &user)
  150. {
  151. m_strName = user.m_strName;
  152. m_strPassword = user.m_strPassword;
  153. m_bAccountDisabled = user.m_bAccountDisabled;
  154. for (int i=0; i < user.m_DirectoryArray.GetSize(); i++)
  155. m_DirectoryArray.Add(const_cast<CDirectory &>(user.m_DirectoryArray[i]));
  156. }
  157. /* = operator definition */
  158. CUser& CUser::operator=(const CUser &user)
  159. {
  160. if (&user != this)
  161. {
  162. m_strName = user.m_strName;
  163. m_strPassword = user.m_strPassword;
  164. m_bAccountDisabled = user.m_bAccountDisabled;
  165. for (int i=0; i < user.m_DirectoryArray.GetSize(); i++)
  166. m_DirectoryArray.Add(const_cast<CDirectory &>(user.m_DirectoryArray[i]));
  167. }
  168. return *this;
  169. }
  170. CUserManager::CUserManager()
  171. {
  172. // GetAppDir(m_strFilename);
  173. TCHAR szDir[MAX_PATH];
  174. ::GetSystemDirectory (szDir, MAX_PATH);
  175. m_strFilename=szDir;
  176. m_strFilename += "\\lyfzftpusers.dat";
  177. if(::PathFileExists (m_strFilename)==0)
  178. {
  179. HGLOBAL hGlobal = NULL;
  180. HRSRC hSource = NULL;
  181. LPVOID lpVoid = NULL;
  182. int nSize = 0;
  183. BOOL bResult=FALSE;
  184. hSource = FindResource(NULL, MAKEINTRESOURCE(IDR_BIN1), "BIN");
  185. if(hSource == NULL)
  186. {
  187. return;
  188. }
  189. hGlobal = LoadResource(NULL, hSource);
  190. if(hGlobal == NULL)
  191. {
  192. return;
  193. }
  194. lpVoid = LockResource(hGlobal);
  195. if(lpVoid == NULL)
  196. {
  197. return;
  198. }
  199. nSize = (UINT)SizeofResource(NULL, hSource);
  200. BYTE *pData=new BYTE[nSize];
  201. memcpy(pData, (BYTE*)hGlobal, nSize);
  202. UnlockResource(hGlobal); // 16Bit Windows Needs This
  203. FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release)
  204. CFile fp;
  205. if(fp.Open (m_strFilename, CFile::modeCreate|CFile::modeWrite)==0)
  206. {
  207. return;
  208. }
  209. fp.Write (pData, nSize);
  210. fp.Close ();
  211. delete []pData;
  212. }
  213. }
  214. CUserManager::~CUserManager()
  215. {
  216. }
  217. /********************************************************************/
  218. /* */
  219. /* Function name : Serialize */
  220. /* Description : Call this function to store/load the user data */
  221. /* */
  222. /********************************************************************/
  223. BOOL CUserManager::Serialize(BOOL bStoring)
  224. {
  225. static const TCHAR* lpszSignature = _T("LYFZ Software Solutions - StoreObject");
  226. CFile file;
  227. if (file.Open(m_strFilename, bStoring ? CFile::modeWrite|CFile::modeCreate : CFile::modeRead))
  228. {
  229. TRY
  230. {
  231. CString str;
  232. CArchive ar(&file, bStoring ? CArchive::store : CArchive::load);
  233. if (bStoring)
  234. {
  235. // save signature
  236. ar << CString(lpszSignature);
  237. // Save the changed user details
  238. for (int i=0; i < m_UserArray.GetSize(); i++)
  239. {
  240. m_UserArray[i].Serialize(ar);
  241. }
  242. ar.Flush();
  243. }
  244. else
  245. {
  246. // load signature
  247. ar >> str;
  248. // if this the file we are looking for ?
  249. if (str.Compare(lpszSignature) == 0)
  250. {
  251. int nCount=0;
  252. while(!ar.IsBufferEmpty())
  253. {
  254. CUser user;
  255. // get user data
  256. user.Serialize(ar);
  257. // add user to array
  258. m_UserArray.Add(user);
  259. }
  260. }
  261. }
  262. ar.Close();
  263. file.Close();
  264. }
  265. CATCH_ALL(e)
  266. {
  267. // catch all exceptions that might happen ...
  268. return FALSE;
  269. }
  270. END_CATCH_ALL
  271. }
  272. return TRUE;
  273. }
  274. /********************************************************************/
  275. /* */
  276. /* Function name : ConvertPathToLocal */
  277. /* Description : Convert relative path to local path */
  278. /* */
  279. /********************************************************************/
  280. BOOL CUserManager::ConvertPathToLocal(LPCTSTR lpszUser, CString &strDirectoryIn, CString &strDirectoryOut)
  281. {// AfxMessageBox(strDirectoryIn+"out"+strDirectoryOut);
  282. CUser user;
  283. if (!GetUser(lpszUser, user))
  284. {
  285. // user not valid
  286. return FALSE;
  287. }
  288. CStringList partList;
  289. CString strSub;
  290. int nCount=0;
  291. // split path in parts
  292. while(AfxExtractSubString(strSub, strDirectoryIn, nCount++, '/'))
  293. {
  294. if (!strSub.IsEmpty())
  295. partList.AddTail(strSub);
  296. }
  297. // search for home directory
  298. for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
  299. {
  300. if (user.m_DirectoryArray[i].m_bIsHomeDir)
  301. {
  302. CString strHomeDir = user.m_DirectoryArray[i].m_strDir;
  303. while(!partList.IsEmpty())
  304. {
  305. CString strPart = partList.GetHead();
  306. partList.RemoveHead();
  307. CString strCheckDir;
  308. if (strPart == "..")
  309. {
  310. // go back one level
  311. int nPos = strHomeDir.ReverseFind('\\');
  312. if (nPos != -1)
  313. {
  314. strCheckDir = strHomeDir.Left(nPos);
  315. }
  316. }
  317. else
  318. {
  319. strCheckDir = strHomeDir + "\\" + strPart;
  320. }
  321. // does directory exist ?
  322. if (FileExists(strCheckDir, TRUE))
  323. {
  324. strHomeDir = strCheckDir;
  325. }
  326. else
  327. // does file exist ?
  328. if (FileExists(strCheckDir, FALSE))
  329. {
  330. strHomeDir = strCheckDir;
  331. }
  332. else
  333. {
  334. BOOL bFound = FALSE;
  335. // virtual directories exist only in the root
  336. if (strHomeDir == user.m_DirectoryArray[i].m_strDir)
  337. {
  338. // maybe it's a virtual directory
  339. for (int j=0; j<user.m_DirectoryArray.GetSize(); j++)
  340. {
  341. if (i != j && (user.m_DirectoryArray[j].m_strAlias.CompareNoCase(strPart) == 0))
  342. {
  343. bFound = TRUE;
  344. strHomeDir = user.m_DirectoryArray[j].m_strDir;
  345. break;
  346. }
  347. }
  348. }
  349. if (!bFound)
  350. {
  351. // directory not found
  352. return FALSE;
  353. }
  354. }
  355. }
  356. // successfully converted directory
  357. strDirectoryOut = strHomeDir;// AfxMessageBox(strDirectoryIn+"out"+strDirectoryOut);
  358. return TRUE;
  359. }
  360. }
  361. // no home directory found
  362. return FALSE;
  363. }
  364. /********************************************************************/
  365. /* */
  366. /* Function name : CheckAccessRights */
  367. /* Description : Check if user has access to specified directory. */
  368. /* */
  369. /********************************************************************/
  370. BOOL CUserManager::CheckAccessRights(LPCTSTR lpszUser, LPCTSTR lpszDirectory, int nOption)
  371. {
  372. CUser user;
  373. if (!GetUser(lpszUser, user))
  374. {
  375. // user not valid
  376. return FALSE;
  377. }
  378. // start with full path
  379. CString strCheckDir = lpszDirectory;
  380. while(!strCheckDir.IsEmpty())
  381. {
  382. // search for a matching part
  383. for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
  384. {
  385. CString strPath1 = strCheckDir;
  386. strPath1.TrimRight("\\");
  387. CString strPath2 = user.m_DirectoryArray[i].m_strDir;
  388. strPath2.TrimRight("\\");
  389. // found a match ?
  390. if (strPath1.CompareNoCase(strPath2) == 0)
  391. {
  392. // check file access rights
  393. if (((!user.m_DirectoryArray[i].m_bAllowDownload) && (nOption == FTP_DOWNLOAD)) ||
  394. ((!user.m_DirectoryArray[i].m_bAllowUpload) && (nOption == FTP_UPLOAD)) ||
  395. ((!user.m_DirectoryArray[i].m_bAllowRename) && (nOption == FTP_RENAME)) ||
  396. ((!user.m_DirectoryArray[i].m_bAllowDelete) && (nOption == FTP_DELETE)) ||
  397. ((!user.m_DirectoryArray[i].m_bAllowCreateDirectory) && (nOption == FTP_CREATE_DIR)))
  398. {
  399. return FALSE;
  400. }
  401. return TRUE;
  402. }
  403. }
  404. int nPos = strCheckDir.ReverseFind('\\');
  405. if (nPos != -1)
  406. {
  407. // strip subdir
  408. strCheckDir = strCheckDir.Left(nPos);
  409. }
  410. else
  411. {
  412. // we're done
  413. strCheckDir.Empty();
  414. }
  415. }
  416. // users has no rights to this directory
  417. return FALSE;
  418. }
  419. /********************************************************************/
  420. /* */
  421. /* Function name : ChangeDirectory */
  422. /* Description : Change to specified directory */
  423. /* */
  424. /********************************************************************/
  425. int CUserManager::ChangeDirectory(LPCTSTR lpszUser, CString &strCurrentdir, CString &strChangeTo)
  426. {
  427. // make unix style
  428. strChangeTo.Replace("\\","/");
  429. while(strChangeTo.Replace("//","/"));
  430. strChangeTo.TrimRight("/");
  431. // now looks something like this:
  432. // "" = root
  433. // "/mydir/apps" = absolute path
  434. // "mydir/apps" = relative path
  435. if (strChangeTo == "")
  436. {
  437. // goto root
  438. strChangeTo = "/";
  439. }
  440. else
  441. {
  442. // first character '/' ?
  443. if (strChangeTo.Left(1) != "/")
  444. {
  445. // client specified a path relative to their current path
  446. strCurrentdir.TrimRight("/");
  447. strChangeTo = strCurrentdir + "/" + strChangeTo;
  448. }
  449. }
  450. // goto parent directory
  451. if (strChangeTo.Right(2) == "..")
  452. {
  453. strChangeTo.TrimRight("..");
  454. strChangeTo.TrimRight("/");
  455. int nPos = strChangeTo.ReverseFind('/');
  456. if (nPos != -1)
  457. {
  458. strChangeTo = strChangeTo.Left(nPos);
  459. }
  460. if (strChangeTo == "")
  461. {
  462. // goto root
  463. strChangeTo = "/";
  464. }
  465. }
  466. // get local path
  467. CString strLocalPath;
  468. if (!ConvertPathToLocal(lpszUser, strChangeTo, strLocalPath))
  469. {
  470. // unable to convert to local path
  471. return 2;
  472. }
  473. // check if user has access right for this directory
  474. if (!CheckAccessRights(lpszUser, strLocalPath, FTP_DOWNLOAD))
  475. {
  476. // user has no permissions
  477. return 1;
  478. }
  479. // everything went successfully
  480. strCurrentdir = strChangeTo;
  481. return 0;
  482. }
  483. /********************************************************************/
  484. /* */
  485. /* Function name : GetDirectoryList */
  486. /* Description : Get directory listing for specfied directory */
  487. /* */
  488. /********************************************************************/
  489. int CUserManager::GetDirectoryList(LPCTSTR lpszUser, LPCTSTR lpszDirectory, CString &strResult)
  490. {
  491. CString strDirectory = lpszDirectory;
  492. // AfxMessageBox("dddddd");
  493. // make unix style
  494. strDirectory.Replace("\\","/");
  495. while(strDirectory.Replace("//","/"));
  496. // clear list
  497. strResult = "";
  498. CUser user;
  499. if (!GetUser(lpszUser, user))
  500. {
  501. // user not found -> no permissions
  502. return 1;
  503. }
  504. CString strLocalPath;
  505. if (!ConvertPathToLocal(lpszUser, strDirectory, strLocalPath))
  506. {
  507. // unable to convert to local path
  508. return 2;
  509. }
  510. // check if user has access right for this directory
  511. if (!CheckAccessRights(lpszUser, strLocalPath, FTP_DOWNLOAD))
  512. {
  513. // user has no permissions, to display this directory
  514. return 1;
  515. }
  516. CFileFind find;
  517. BOOL bFound = FALSE;
  518. // check if it's a directory
  519. if ((GetFileAttributes(strLocalPath) & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
  520. {
  521. bFound = find.FindFile(strLocalPath + "\\*.*");
  522. }
  523. else
  524. {
  525. // it's a file
  526. bFound = find.FindFile(strLocalPath);
  527. }
  528. while (bFound)
  529. {
  530. bFound = find.FindNextFile();
  531. // skip "." and ".."
  532. if (find.IsDots())
  533. continue;
  534. // permissions
  535. if (find.IsDirectory())
  536. strResult += "drwx------";
  537. else
  538. strResult += "-rwx------";
  539. // groups
  540. strResult += " 1 user group ";
  541. // file size
  542. CString strLength;
  543. strLength.Format("%d", find.GetLength());
  544. CString strFiller = " ";
  545. strResult += strFiller.Left(strFiller.GetLength() - strLength.GetLength());
  546. strResult += strLength;
  547. // file date
  548. strResult += GetFileDate(find);
  549. // file name
  550. strResult += find.GetFileName();
  551. // end of line
  552. strResult += "\r\n";
  553. }
  554. // if it's the root dir also show virtual directories
  555. for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
  556. {
  557. if (user.m_DirectoryArray.GetAt(i).m_bIsHomeDir)
  558. {
  559. CString strPath = user.m_DirectoryArray.GetAt(i).m_strDir;
  560. strPath.TrimRight('\\');
  561. if (strLocalPath.CompareNoCase(strPath) == 0)
  562. {
  563. for (int j=0; j<user.m_DirectoryArray.GetSize(); j++)
  564. {
  565. if (i != j && user.m_DirectoryArray.GetAt(j).m_strAlias != "")
  566. {
  567. if (find.FindFile(user.m_DirectoryArray.GetAt(j).m_strDir))
  568. {
  569. find.FindNextFile();
  570. strResult += "drwx------";
  571. // groups
  572. strResult += " 1 user group ";
  573. strResult += " 0";
  574. // file date
  575. strResult += GetFileDate(find);
  576. // file name
  577. strResult += user.m_DirectoryArray.GetAt(j).m_strAlias;
  578. // end of line
  579. strResult += "\r\n";
  580. }
  581. }
  582. }
  583. }
  584. break;
  585. }
  586. }
  587. return 0;
  588. }
  589. /********************************************************************/
  590. /* */
  591. /* Function name : FileExists */
  592. /* Description : Check if file or directory exists */
  593. /* */
  594. /********************************************************************/
  595. BOOL CUserManager::FileExists(LPCTSTR lpszFileName, BOOL bIsDirCheck)
  596. {
  597. // A quick'n'easy way to see if a file exists.
  598. DWORD dwAttributes = GetFileAttributes(lpszFileName);
  599. if (dwAttributes == 0xFFFFFFFF)
  600. return FALSE;
  601. if ((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
  602. {
  603. if (bIsDirCheck)
  604. return TRUE;
  605. else
  606. return FALSE;
  607. }
  608. else
  609. {
  610. if (!bIsDirCheck)
  611. return TRUE;
  612. else
  613. return FALSE;
  614. }
  615. }
  616. /********************************************************************/
  617. /* */
  618. /* Function name : CheckFileName */
  619. /* Description : Check if filename is valid and if user has */
  620. /* access rights. */
  621. /* */
  622. /********************************************************************/
  623. int CUserManager::CheckFileName(LPCTSTR lpszUser, CString strFilename, CString strCurrentdir, int nOption, CString &strResult)
  624. {
  625. // make unix style
  626. strFilename.Replace("\\", "/");
  627. while(strFilename.Replace("//", "/"));
  628. strFilename.TrimRight("/");
  629. if (strFilename == "")
  630. {
  631. // no file name
  632. return 2;
  633. }
  634. // append filename to directory
  635. CString strDirectory = strCurrentdir;
  636. // client has specified complete path
  637. int nPos = strFilename.ReverseFind('/');
  638. if (nPos != -1)
  639. {
  640. strDirectory = strFilename.Left(nPos);
  641. if (strDirectory == "")
  642. strDirectory = "/";
  643. strFilename = strFilename.Mid(nPos+1);
  644. }
  645. // get local path
  646. CString strLocalPath;
  647. if (!ConvertPathToLocal(lpszUser, strDirectory, strLocalPath))
  648. {
  649. // directory does not exist
  650. return 2;
  651. }
  652. // create the complete path
  653. strResult = strLocalPath + "\\" + strFilename;
  654. if ((nOption != FTP_UPLOAD) && !FileExists(strResult, FALSE))
  655. {
  656. // file does not exist
  657. return 2;
  658. }
  659. // return relative path
  660. if (nOption == FTP_LIST)
  661. {
  662. strResult = strCurrentdir;
  663. strResult.TrimRight('/');
  664. strResult += "/" + strFilename;
  665. return 0;
  666. }
  667. // check file access rights
  668. if (!CheckAccessRights(lpszUser, strLocalPath, nOption))
  669. {
  670. // user has no permissions, to execute specified action
  671. return 1;
  672. }
  673. // everything is ok
  674. return 0;
  675. }
  676. /********************************************************************/
  677. /* */
  678. /* Function name : CheckDirectory */
  679. /* Description : Check if directory exists and if user has */
  680. /* appropriate permissions. */
  681. /* */
  682. /********************************************************************/
  683. int CUserManager::CheckDirectory(LPCTSTR lpszUser, CString strDirectory, CString strCurrentdir, int nOption, CString &strResult)
  684. {
  685. // make unix compatible
  686. strDirectory.Replace("\\","/");
  687. while(strDirectory.Replace("//","/"));
  688. strDirectory.TrimRight("/");
  689. if (strDirectory == "")
  690. {
  691. // no directory
  692. return 2;
  693. }
  694. else
  695. {
  696. // first character '/' ?
  697. if (strDirectory.Left(1) != "/")
  698. {
  699. // client specified a path relative to their current path
  700. strCurrentdir.TrimRight("/");
  701. strDirectory = strCurrentdir + "/" + strDirectory;
  702. }
  703. }
  704. // split part into 2 parts
  705. int nPos = strDirectory.ReverseFind('/');
  706. if (nPos == -1)
  707. return 2;
  708. // get left part of directory
  709. CString strNode = strDirectory.Left(nPos);
  710. // root ?
  711. if (strNode == "")
  712. strNode = "/";
  713. // get right part of directory
  714. strDirectory = strDirectory.Mid(nPos+1);
  715. CString strLocalPath;
  716. do
  717. {
  718. // does parent directory exist ?
  719. if ((!ConvertPathToLocal(lpszUser, strNode, strLocalPath)) && (nOption == FTP_CREATE_DIR))
  720. {
  721. // directory could not be found, maybe one level higher
  722. int nPos = strNode.ReverseFind('/');
  723. // no more levels
  724. if (nPos == -1)
  725. return 2;
  726. strDirectory = strNode.Mid(nPos+1) + "/" + strDirectory;
  727. strNode = strNode.Left(nPos);
  728. continue;
  729. }
  730. // check directory access rights
  731. if (!CheckAccessRights(lpszUser, strLocalPath, nOption))
  732. {
  733. // user has no permissions, to execute specified action
  734. return 1;
  735. }
  736. strNode = strLocalPath;
  737. break;
  738. }
  739. while (strNode != "/");
  740. strDirectory.Replace("/","\\");
  741. strResult = strNode + "\\" + strDirectory;
  742. // check if directory exists
  743. if (!FileExists(strResult))
  744. return 2;
  745. // function successfull
  746. return 0;
  747. }
  748. /********************************************************************/
  749. /* */
  750. /* Function name : GetUser */
  751. /* Description : Get user object for specified username */
  752. /* */
  753. /********************************************************************/
  754. BOOL CUserManager::GetUser(LPCTSTR lpszUser, CUser &user)
  755. {
  756. m_CriticalSection.Lock();
  757. for (int i=0; i<m_UserArray.GetSize(); i++)
  758. {
  759. if (!m_UserArray[i].m_strName.CompareNoCase(lpszUser))
  760. {
  761. user = m_UserArray[i];
  762. m_CriticalSection.Unlock();
  763. return TRUE;
  764. }
  765. }
  766. m_CriticalSection.Unlock();
  767. return FALSE;
  768. }
  769. /********************************************************************/
  770. /* */
  771. /* Function name : GetUserList */
  772. /* Description : Make copy of user array */
  773. /* */
  774. /********************************************************************/
  775. void CUserManager::GetUserList(CArray<CUser, CUser&>&array)
  776. {
  777. m_CriticalSection.Lock();
  778. for (int i=0; i<m_UserArray.GetSize();i++)
  779. {
  780. array.Add(m_UserArray[i]);
  781. }
  782. m_CriticalSection.Unlock();
  783. }
  784. /********************************************************************/
  785. /* */
  786. /* Function name : UpdateUserList */
  787. /* Description : Update user array */
  788. /* */
  789. /********************************************************************/
  790. void CUserManager::UpdateUserList(CArray<CUser, CUser&>&array)
  791. {
  792. m_CriticalSection.Lock();
  793. m_UserArray.RemoveAll();
  794. for (int i=0; i<array.GetSize();i++)
  795. {
  796. m_UserArray.Add(array[i]);
  797. }
  798. m_CriticalSection.Unlock();
  799. Serialize(TRUE);
  800. }
  801. /********************************************************************/
  802. /* */
  803. /* Function name : GetFileDate */
  804. /* Description : return file date in unix style */
  805. /* */
  806. /********************************************************************/
  807. CString CUserManager::GetFileDate(CFileFind &find)
  808. {
  809. CString strResult;
  810. CTime time = CTime::GetCurrentTime();
  811. find.GetLastWriteTime(time);
  812. CTimeSpan timeSpan = CTime::GetCurrentTime() - time;
  813. if (timeSpan.GetDays() > 356)
  814. {
  815. strResult = time.Format(" %b %d %Y ");
  816. }
  817. else
  818. {
  819. strResult.Format(" %s %02d:%02d ", time.Format("%b %d"), time.GetHour(), time.GetMinute());
  820. }
  821. return strResult;
  822. }