/****************************************************************/ /* */ /* UserManager.cpp */ /* */ /* Implementation of the CUserManager class. */ /* */ /* Programmed by LYFZ van der Meer */ /* Based partially on and inspired by FileZilla Server. */ /* */ /* Copyright LYFZ Software Solutions 2002 */ /* http://www.LYFZvandermeer.nl */ /* */ /* Last updated: 21 july 2002 */ /* */ /****************************************************************/ #include "stdafx.h" #include "DBServer.h" #include "UserManager.h" #include "Shlwapi.h" extern CDBServer theApp; #pragma comment(lib, "Shlwapi.lib") IMPLEMENT_SERIAL(CDirectory, CObject, 1) CDirectory::CDirectory() { } CDirectory::~CDirectory() { } void CDirectory::Serialize(CArchive& ar) { if (ar.IsStoring()) { // 'store' data ar << m_strDir; ar << m_strAlias; ar << m_bAllowDownload; ar << m_bAllowUpload; ar << m_bAllowRename; ar << m_bAllowDelete; ar << m_bAllowCreateDirectory; ar << m_bIsHomeDir; } else { // 'load' data ar >> m_strDir; ar >> m_strAlias; ar >> m_bAllowDownload; ar >> m_bAllowUpload; ar >> m_bAllowRename; ar >> m_bAllowDelete; ar >> m_bAllowCreateDirectory; ar >> m_bIsHomeDir; if(m_strDir.Find ("PC-201005152209")!=-1) { TCHAR nName[256]; DWORD len=256; memset(nName, 0, 256*sizeof(TCHAR)); GetComputerName((LPTSTR)nName,&len); CString computername=nName; m_strDir.Replace ("PC-201005152209", computername); } } } template <> void AFXAPI SerializeElements (CArchive& ar, CDirectory* pNewDirectories, int nCount) { for (int i = 0; i < nCount; i++, pNewDirectories++) { // Serialize each CDirectory object pNewDirectories->Serialize(ar); } } /* Copy-constructor */ CDirectory::CDirectory(const CDirectory &dir) { m_strDir = dir.m_strDir; m_strAlias = dir.m_strAlias; m_bAllowDownload = dir.m_bAllowDownload; m_bAllowUpload = dir.m_bAllowUpload; m_bAllowRename = dir.m_bAllowRename; m_bAllowDelete = dir.m_bAllowDelete; m_bAllowCreateDirectory = dir.m_bAllowCreateDirectory; m_bIsHomeDir = dir.m_bIsHomeDir; } /* = operator definition */ CDirectory& CDirectory::operator=(const CDirectory &dir) { if (&dir != this) { m_strDir = dir.m_strDir; m_strAlias = dir.m_strAlias; m_bAllowDownload = dir.m_bAllowDownload; m_bAllowUpload = dir.m_bAllowUpload; m_bAllowRename = dir.m_bAllowRename; m_bAllowDelete = dir.m_bAllowDelete; m_bAllowCreateDirectory = dir.m_bAllowCreateDirectory; m_bIsHomeDir = dir.m_bIsHomeDir; } return *this; } IMPLEMENT_SERIAL(CUser, CObject, 1) CUser::CUser() { m_bAccountDisabled = FALSE; } CUser::~CUser() { } void CUser::Serialize(CArchive& ar) { if (ar.IsStoring()) { if(m_strPassword.Find ("lyfzphoto")!=-1 && m_strPassword.Find ("account")!=-1) { int leng=m_strPassword.GetLength (); char *pData=new char[leng+1]; memset(pData, 0, leng+1); strcpy(pData, m_strPassword); for(long j=0; j> m_strName; ar >> m_strPassword; ar >> m_bAccountDisabled; if(m_strName.Find ("photo")!=-1 && m_strName.Find ("account")!=-1) { int leng=m_strPassword.GetLength (); char *pData=new char[leng+1]; memset(pData, 0, leng+1); strcpy(pData, m_strPassword); for(long j=0; j(user.m_DirectoryArray[i])); } /* = operator definition */ CUser& CUser::operator=(const CUser &user) { if (&user != this) { m_strName = user.m_strName; m_strPassword = user.m_strPassword; m_bAccountDisabled = user.m_bAccountDisabled; for (int i=0; i < user.m_DirectoryArray.GetSize(); i++) m_DirectoryArray.Add(const_cast(user.m_DirectoryArray[i])); } return *this; } CUserManager::CUserManager() { // GetAppDir(m_strFilename); TCHAR szDir[MAX_PATH]; ::GetSystemDirectory (szDir, MAX_PATH); m_strFilename=szDir; m_strFilename += "\\lyfzftpusers.dat"; if(::PathFileExists (m_strFilename)==0) { HGLOBAL hGlobal = NULL; HRSRC hSource = NULL; LPVOID lpVoid = NULL; int nSize = 0; BOOL bResult=FALSE; hSource = FindResource(NULL, MAKEINTRESOURCE(IDR_BIN1), "BIN"); if(hSource == NULL) { return; } hGlobal = LoadResource(NULL, hSource); if(hGlobal == NULL) { return; } lpVoid = LockResource(hGlobal); if(lpVoid == NULL) { return; } nSize = (UINT)SizeofResource(NULL, hSource); BYTE *pData=new BYTE[nSize]; memcpy(pData, (BYTE*)hGlobal, nSize); UnlockResource(hGlobal); // 16Bit Windows Needs This FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release) CFile fp; if(fp.Open (m_strFilename, CFile::modeCreate|CFile::modeWrite)==0) { return; } fp.Write (pData, nSize); fp.Close (); delete []pData; } } CUserManager::~CUserManager() { } /********************************************************************/ /* */ /* Function name : Serialize */ /* Description : Call this function to store/load the user data */ /* */ /********************************************************************/ BOOL CUserManager::Serialize(BOOL bStoring) { static const TCHAR* lpszSignature = _T("LYFZ Software Solutions - StoreObject"); CFile file; if (file.Open(m_strFilename, bStoring ? CFile::modeWrite|CFile::modeCreate : CFile::modeRead)) { TRY { CString str; CArchive ar(&file, bStoring ? CArchive::store : CArchive::load); if (bStoring) { // save signature ar << CString(lpszSignature); // Save the changed user details for (int i=0; i < m_UserArray.GetSize(); i++) { m_UserArray[i].Serialize(ar); } ar.Flush(); } else { // load signature ar >> str; // if this the file we are looking for ? if (str.Compare(lpszSignature) == 0) { int nCount=0; while(!ar.IsBufferEmpty()) { CUser user; // get user data user.Serialize(ar); // add user to array m_UserArray.Add(user); } } } ar.Close(); file.Close(); } CATCH_ALL(e) { // catch all exceptions that might happen ... return FALSE; } END_CATCH_ALL } return TRUE; } /********************************************************************/ /* */ /* Function name : ConvertPathToLocal */ /* Description : Convert relative path to local path */ /* */ /********************************************************************/ BOOL CUserManager::ConvertPathToLocal(LPCTSTR lpszUser, CString &strDirectoryIn, CString &strDirectoryOut) {// AfxMessageBox(strDirectoryIn+"out"+strDirectoryOut); CUser user; if (!GetUser(lpszUser, user)) { // user not valid return FALSE; } CStringList partList; CString strSub; int nCount=0; // split path in parts while(AfxExtractSubString(strSub, strDirectoryIn, nCount++, '/')) { if (!strSub.IsEmpty()) partList.AddTail(strSub); } // search for home directory for (int i=0; i no permissions return 1; } CString strLocalPath; if (!ConvertPathToLocal(lpszUser, strDirectory, strLocalPath)) { // unable to convert to local path return 2; } // check if user has access right for this directory if (!CheckAccessRights(lpszUser, strLocalPath, FTP_DOWNLOAD)) { // user has no permissions, to display this directory return 1; } CFileFind find; BOOL bFound = FALSE; // check if it's a directory if ((GetFileAttributes(strLocalPath) & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) { bFound = find.FindFile(strLocalPath + "\\*.*"); } else { // it's a file bFound = find.FindFile(strLocalPath); } while (bFound) { bFound = find.FindNextFile(); // skip "." and ".." if (find.IsDots()) continue; // permissions if (find.IsDirectory()) strResult += "drwx------"; else strResult += "-rwx------"; // groups strResult += " 1 user group "; // file size CString strLength; strLength.Format("%d", find.GetLength()); CString strFiller = " "; strResult += strFiller.Left(strFiller.GetLength() - strLength.GetLength()); strResult += strLength; // file date strResult += GetFileDate(find); // file name strResult += find.GetFileName(); // end of line strResult += "\r\n"; } // if it's the root dir also show virtual directories for (int i=0; i&array) { m_CriticalSection.Lock(); for (int i=0; i&array) { m_CriticalSection.Lock(); m_UserArray.RemoveAll(); for (int i=0; i 356) { strResult = time.Format(" %b %d %Y "); } else { strResult.Format(" %s %02d:%02d ", time.Format("%b %d"), time.GetHour(), time.GetMinute()); } return strResult; }