Annotation of truecrypt/mount/maincom.cpp, revision 1.1.1.8

1.1       root        1: /*
1.1.1.6   root        2:  Copyright (c) 2007-2009 TrueCrypt Foundation. All rights reserved.
1.1       root        3: 
1.1.1.8 ! root        4:  Governed by the TrueCrypt License 2.8 the full text of which is contained
1.1       root        5:  in the file License.txt included in TrueCrypt binary and source code
                      6:  distribution packages.
                      7: */
                      8: 
                      9: #include <atlcomcli.h>
                     10: #include <atlconv.h>
                     11: #include <windows.h>
                     12: #include "BaseCom.h"
1.1.1.3   root       13: #include "BootEncryption.h"
1.1       root       14: #include "Dlgcode.h"
                     15: #include "MainCom.h"
                     16: #include "MainCom_h.h"
                     17: #include "MainCom_i.c"
1.1.1.4   root       18: #include "Mount.h"
1.1       root       19: #include "Password.h"
                     20: 
1.1.1.3   root       21: using namespace TrueCrypt;
                     22: 
1.1       root       23: static volatile LONG ObjectCount = 0;
                     24: 
1.1.1.3   root       25: class TrueCryptMainCom : public ITrueCryptMainCom
1.1       root       26: {
                     27: 
                     28: public:
1.1.1.3   root       29:        TrueCryptMainCom (DWORD messageThreadId) : RefCount (0), MessageThreadId (messageThreadId)
1.1       root       30:        {
                     31:                InterlockedIncrement (&ObjectCount);
                     32:        }
                     33: 
1.1.1.3   root       34:        ~TrueCryptMainCom ()
1.1       root       35:        {
                     36:                if (InterlockedDecrement (&ObjectCount) == 0)
                     37:                        PostThreadMessage (MessageThreadId, WM_APP, 0, 0);
                     38:        }
                     39: 
                     40:        virtual ULONG STDMETHODCALLTYPE AddRef ()
                     41:        {
                     42:                return InterlockedIncrement (&RefCount);
                     43:        }
                     44: 
                     45:        virtual ULONG STDMETHODCALLTYPE Release ()
                     46:        {
                     47:                if (!InterlockedDecrement (&RefCount))
                     48:                {
                     49:                        delete this;
                     50:                        return 0;
                     51:                }
                     52: 
                     53:                return RefCount;
                     54:        }
                     55: 
                     56:        virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID riid, void **ppvObject)
                     57:        {
1.1.1.3   root       58:                if (riid == IID_IUnknown || riid == IID_ITrueCryptMainCom)
1.1       root       59:                        *ppvObject = this;
                     60:                else
                     61:                {
                     62:                        *ppvObject = NULL;
                     63:                        return E_NOINTERFACE;
                     64:                }
                     65: 
                     66:                AddRef ();
                     67:                return S_OK;
                     68:        }
                     69:        
                     70:        virtual int STDMETHODCALLTYPE BackupVolumeHeader (LONG_PTR hwndDlg, BOOL bRequireConfirmation, BSTR lpszVolume)
                     71:        {
                     72:                USES_CONVERSION;
1.1.1.4   root       73:                MainDlg = (HWND) hwndDlg;
1.1       root       74:                return ::BackupVolumeHeader ((HWND) hwndDlg, bRequireConfirmation, CW2A (lpszVolume));
                     75:        }
                     76: 
                     77:        virtual int STDMETHODCALLTYPE RestoreVolumeHeader (LONG_PTR hwndDlg, BSTR lpszVolume)
                     78:        {
                     79:                USES_CONVERSION;
1.1.1.4   root       80:                MainDlg = (HWND) hwndDlg;
1.1       root       81:                return ::RestoreVolumeHeader ((HWND) hwndDlg, CW2A (lpszVolume));
                     82:        }
                     83: 
1.1.1.3   root       84:        virtual DWORD STDMETHODCALLTYPE CallDriver (DWORD ioctl, BSTR input, BSTR *output)
                     85:        {
                     86:                return BaseCom::CallDriver (ioctl, input, output);
                     87:        }
                     88: 
1.1       root       89:        virtual int STDMETHODCALLTYPE ChangePassword (BSTR volumePath, Password *oldPassword, Password *newPassword, int pkcs5, LONG_PTR hWnd)
                     90:        {
                     91:                USES_CONVERSION;
1.1.1.4   root       92:                MainDlg = (HWND) hWnd;
1.1       root       93:                return ::ChangePwd (CW2A (volumePath), oldPassword, newPassword, pkcs5, (HWND) hWnd);
                     94:        }
1.1.1.8 ! root       95: 
        !            96:        virtual DWORD STDMETHODCALLTYPE CopyFile (BSTR sourceFile, BSTR destinationFile)
        !            97:        {
        !            98:                return BaseCom::CopyFile (sourceFile, destinationFile);
        !            99:        }
        !           100: 
        !           101:        virtual DWORD STDMETHODCALLTYPE DeleteFile (BSTR file)
        !           102:        {
        !           103:                return BaseCom::DeleteFile (file);
        !           104:        }
        !           105: 
1.1.1.5   root      106:        virtual BOOL STDMETHODCALLTYPE IsPagingFileActive (BOOL checkNonWindowsPartitionsOnly)
1.1.1.4   root      107:        {
1.1.1.5   root      108:                return BaseCom::IsPagingFileActive (checkNonWindowsPartitionsOnly);
1.1.1.4   root      109:        }
1.1       root      110: 
1.1.1.3   root      111:        virtual DWORD STDMETHODCALLTYPE ReadWriteFile (BOOL write, BOOL device, BSTR filePath, BSTR *bufferBstr, unsigned __int64 offset, unsigned __int32 size, DWORD *sizeDone)
                    112:        {
                    113:                return BaseCom::ReadWriteFile (write, device, filePath, bufferBstr, offset, size, sizeDone);
                    114:        }
                    115: 
1.1.1.4   root      116:        virtual DWORD STDMETHODCALLTYPE RegisterFilterDriver (BOOL registerDriver, BOOL volumeClass)
1.1.1.3   root      117:        {
1.1.1.4   root      118:                return BaseCom::RegisterFilterDriver (registerDriver, volumeClass);
1.1.1.3   root      119:        }
                    120: 
1.1.1.8 ! root      121:        virtual DWORD STDMETHODCALLTYPE RegisterSystemFavoritesService (BOOL registerService)
        !           122:        {
        !           123:                return BaseCom::RegisterSystemFavoritesService (registerService);
        !           124:        }
        !           125: 
1.1.1.3   root      126:        virtual DWORD STDMETHODCALLTYPE SetDriverServiceStartType (DWORD startType)
                    127:        {
                    128:                return BaseCom::SetDriverServiceStartType (startType);
                    129:        }
                    130: 
1.1.1.4   root      131:        virtual DWORD STDMETHODCALLTYPE WriteLocalMachineRegistryDwordValue (BSTR keyPath, BSTR valueName, DWORD value)
                    132:        {
                    133:                return BaseCom::WriteLocalMachineRegistryDwordValue (keyPath, valueName, value);
                    134:        }
                    135: 
1.1       root      136: protected:
                    137:        DWORD MessageThreadId;
                    138:        LONG RefCount;
                    139: };
                    140: 
                    141: 
                    142: extern "C" BOOL ComServerMain ()
                    143: {
1.1.1.5   root      144:        SetProcessShutdownParameters (0x100, 0);
                    145: 
1.1.1.3   root      146:        TrueCryptFactory<TrueCryptMainCom> factory (GetCurrentThreadId ());
1.1       root      147:        DWORD cookie;
                    148: 
                    149:        if (IsUacSupported ())
                    150:                UacElevated = TRUE;
                    151: 
1.1.1.3   root      152:        if (CoRegisterClassObject (CLSID_TrueCryptMainCom, (LPUNKNOWN) &factory,
1.1       root      153:                CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie) != S_OK)
                    154:                return FALSE;
                    155: 
                    156:        MSG msg;
1.1.1.2   root      157:        while (int r = GetMessage (&msg, NULL, 0, 0))
1.1       root      158:        {
1.1.1.2   root      159:                if (r == -1)
                    160:                        return FALSE;
                    161: 
1.1       root      162:                TranslateMessage (&msg);
                    163:                DispatchMessage (&msg);
                    164: 
                    165:                if (msg.message == WM_APP
                    166:                        && ObjectCount < 1
                    167:                        && !factory.IsServerLocked ())
                    168:                        break;
                    169:        }
                    170:        CoRevokeClassObject (cookie);
                    171: 
                    172:        return TRUE;
                    173: }
                    174: 
                    175: 
1.1.1.3   root      176: static BOOL ComGetInstance (HWND hWnd, ITrueCryptMainCom **tcServer)
1.1       root      177: {
1.1.1.3   root      178:        return ComGetInstanceBase (hWnd, CLSID_TrueCryptMainCom, IID_ITrueCryptMainCom, (void **) tcServer);
                    179: }
                    180: 
                    181: 
                    182: ITrueCryptMainCom *GetElevatedInstance (HWND parent)
                    183: {
                    184:        ITrueCryptMainCom *instance;
                    185: 
                    186:        if (!ComGetInstance (parent, &instance))
                    187:                throw UserAbort (SRC_POS);
                    188: 
                    189:        return instance;
1.1       root      190: }
                    191: 
                    192: 
                    193: extern "C" int UacBackupVolumeHeader (HWND hwndDlg, BOOL bRequireConfirmation, char *lpszVolume)
                    194: {
1.1.1.3   root      195:        CComPtr<ITrueCryptMainCom> tc;
1.1       root      196:        int r;
                    197: 
                    198:        CoInitialize (NULL);
                    199: 
                    200:        if (ComGetInstance (hwndDlg, &tc))
                    201:                r = tc->BackupVolumeHeader ((LONG_PTR) hwndDlg, bRequireConfirmation, CComBSTR (lpszVolume));
                    202:        else
                    203:                r = -1;
                    204: 
                    205:        CoUninitialize ();
                    206: 
                    207:        return r;
                    208: }
                    209: 
                    210: 
                    211: extern "C" int UacRestoreVolumeHeader (HWND hwndDlg, char *lpszVolume)
                    212: {
1.1.1.3   root      213:        CComPtr<ITrueCryptMainCom> tc;
1.1       root      214:        int r;
                    215: 
                    216:        CoInitialize (NULL);
                    217: 
                    218:        if (ComGetInstance (hwndDlg, &tc))
                    219:                r = tc->RestoreVolumeHeader ((LONG_PTR) hwndDlg, CComBSTR (lpszVolume));
                    220:        else
                    221:                r = -1;
                    222: 
                    223:        CoUninitialize ();
                    224: 
                    225:        return r;
                    226: }
                    227: 
                    228: 
                    229: extern "C" int UacChangePwd (char *lpszVolume, Password *oldPassword, Password *newPassword, int pkcs5, HWND hwndDlg)
                    230: {
1.1.1.3   root      231:        CComPtr<ITrueCryptMainCom> tc;
1.1       root      232:        int r;
                    233: 
                    234:        if (ComGetInstance (hwndDlg, &tc))
                    235:        {
                    236:                WaitCursor ();
1.1.1.6   root      237:                r = tc->ChangePassword (CComBSTR (lpszVolume), oldPassword, newPassword, pkcs5, (LONG_PTR) hwndDlg);
1.1       root      238:                NormalCursor ();
                    239:        }
                    240:        else
                    241:                r = -1;
                    242: 
                    243:        return r;
                    244: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.