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

1.1       root        1: /*
1.1.1.3   root        2:  Copyright (c) 2007-2008 TrueCrypt Foundation. All rights reserved.
1.1       root        3: 
1.1.1.4 ! root        4:  Governed by the TrueCrypt License 2.5 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.4 ! root       95:        
        !            96:        virtual BOOL STDMETHODCALLTYPE IsPagingFileActive ()
        !            97:        {
        !            98:                return BaseCom::IsPagingFileActive ();
        !            99:        }
1.1       root      100: 
1.1.1.3   root      101:        virtual DWORD STDMETHODCALLTYPE ReadWriteFile (BOOL write, BOOL device, BSTR filePath, BSTR *bufferBstr, unsigned __int64 offset, unsigned __int32 size, DWORD *sizeDone)
                    102:        {
                    103:                return BaseCom::ReadWriteFile (write, device, filePath, bufferBstr, offset, size, sizeDone);
                    104:        }
                    105: 
1.1.1.4 ! root      106:        virtual DWORD STDMETHODCALLTYPE RegisterFilterDriver (BOOL registerDriver, BOOL volumeClass)
1.1.1.3   root      107:        {
1.1.1.4 ! root      108:                return BaseCom::RegisterFilterDriver (registerDriver, volumeClass);
1.1.1.3   root      109:        }
                    110: 
                    111:        virtual DWORD STDMETHODCALLTYPE SetDriverServiceStartType (DWORD startType)
                    112:        {
                    113:                return BaseCom::SetDriverServiceStartType (startType);
                    114:        }
                    115: 
1.1.1.4 ! root      116:        virtual DWORD STDMETHODCALLTYPE WriteLocalMachineRegistryDwordValue (BSTR keyPath, BSTR valueName, DWORD value)
        !           117:        {
        !           118:                return BaseCom::WriteLocalMachineRegistryDwordValue (keyPath, valueName, value);
        !           119:        }
        !           120: 
1.1       root      121: protected:
                    122:        DWORD MessageThreadId;
                    123:        LONG RefCount;
                    124: };
                    125: 
                    126: 
                    127: extern "C" BOOL ComServerMain ()
                    128: {
1.1.1.3   root      129:        TrueCryptFactory<TrueCryptMainCom> factory (GetCurrentThreadId ());
1.1       root      130:        DWORD cookie;
                    131: 
                    132:        if (IsUacSupported ())
                    133:                UacElevated = TRUE;
                    134: 
1.1.1.3   root      135:        if (CoRegisterClassObject (CLSID_TrueCryptMainCom, (LPUNKNOWN) &factory,
1.1       root      136:                CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie) != S_OK)
                    137:                return FALSE;
                    138: 
                    139:        MSG msg;
1.1.1.2   root      140:        while (int r = GetMessage (&msg, NULL, 0, 0))
1.1       root      141:        {
1.1.1.2   root      142:                if (r == -1)
                    143:                        return FALSE;
                    144: 
1.1       root      145:                TranslateMessage (&msg);
                    146:                DispatchMessage (&msg);
                    147: 
                    148:                if (msg.message == WM_APP
                    149:                        && ObjectCount < 1
                    150:                        && !factory.IsServerLocked ())
                    151:                        break;
                    152:        }
                    153:        CoRevokeClassObject (cookie);
                    154: 
                    155:        return TRUE;
                    156: }
                    157: 
                    158: 
1.1.1.3   root      159: static BOOL ComGetInstance (HWND hWnd, ITrueCryptMainCom **tcServer)
1.1       root      160: {
1.1.1.3   root      161:        return ComGetInstanceBase (hWnd, CLSID_TrueCryptMainCom, IID_ITrueCryptMainCom, (void **) tcServer);
                    162: }
                    163: 
                    164: 
                    165: ITrueCryptMainCom *GetElevatedInstance (HWND parent)
                    166: {
                    167:        ITrueCryptMainCom *instance;
                    168: 
                    169:        if (!ComGetInstance (parent, &instance))
                    170:                throw UserAbort (SRC_POS);
                    171: 
                    172:        return instance;
1.1       root      173: }
                    174: 
                    175: 
                    176: extern "C" int UacBackupVolumeHeader (HWND hwndDlg, BOOL bRequireConfirmation, char *lpszVolume)
                    177: {
1.1.1.3   root      178:        CComPtr<ITrueCryptMainCom> tc;
1.1       root      179:        int r;
                    180: 
                    181:        CoInitialize (NULL);
                    182: 
                    183:        if (ComGetInstance (hwndDlg, &tc))
                    184:                r = tc->BackupVolumeHeader ((LONG_PTR) hwndDlg, bRequireConfirmation, CComBSTR (lpszVolume));
                    185:        else
                    186:                r = -1;
                    187: 
                    188:        CoUninitialize ();
                    189: 
                    190:        return r;
                    191: }
                    192: 
                    193: 
                    194: extern "C" int UacRestoreVolumeHeader (HWND hwndDlg, char *lpszVolume)
                    195: {
1.1.1.3   root      196:        CComPtr<ITrueCryptMainCom> tc;
1.1       root      197:        int r;
                    198: 
                    199:        CoInitialize (NULL);
                    200: 
                    201:        if (ComGetInstance (hwndDlg, &tc))
                    202:                r = tc->RestoreVolumeHeader ((LONG_PTR) hwndDlg, CComBSTR (lpszVolume));
                    203:        else
                    204:                r = -1;
                    205: 
                    206:        CoUninitialize ();
                    207: 
                    208:        return r;
                    209: }
                    210: 
                    211: 
                    212: extern "C" int UacChangePwd (char *lpszVolume, Password *oldPassword, Password *newPassword, int pkcs5, HWND hwndDlg)
                    213: {
1.1.1.3   root      214:        CComPtr<ITrueCryptMainCom> tc;
1.1       root      215:        int r;
                    216: 
                    217:        if (ComGetInstance (hwndDlg, &tc))
                    218:        {
                    219:                WaitCursor ();
                    220:                r = tc->ChangePassword (CComBSTR (lpszVolume), oldPassword, newPassword, 0, (LONG_PTR) hwndDlg);
                    221:                NormalCursor ();
                    222:        }
                    223:        else
                    224:                r = -1;
                    225: 
                    226:        return r;
                    227: }

unix.superglobalmegacorp.com

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