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

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.5 ! root        4:  Governed by the TrueCrypt License 2.6 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:        
1.1.1.5 ! root       96:        virtual BOOL STDMETHODCALLTYPE IsPagingFileActive (BOOL checkNonWindowsPartitionsOnly)
1.1.1.4   root       97:        {
1.1.1.5 ! root       98:                return BaseCom::IsPagingFileActive (checkNonWindowsPartitionsOnly);
1.1.1.4   root       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.5 ! root      129:        SetProcessShutdownParameters (0x100, 0);
        !           130: 
1.1.1.3   root      131:        TrueCryptFactory<TrueCryptMainCom> factory (GetCurrentThreadId ());
1.1       root      132:        DWORD cookie;
                    133: 
                    134:        if (IsUacSupported ())
                    135:                UacElevated = TRUE;
                    136: 
1.1.1.3   root      137:        if (CoRegisterClassObject (CLSID_TrueCryptMainCom, (LPUNKNOWN) &factory,
1.1       root      138:                CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie) != S_OK)
                    139:                return FALSE;
                    140: 
                    141:        MSG msg;
1.1.1.2   root      142:        while (int r = GetMessage (&msg, NULL, 0, 0))
1.1       root      143:        {
1.1.1.2   root      144:                if (r == -1)
                    145:                        return FALSE;
                    146: 
1.1       root      147:                TranslateMessage (&msg);
                    148:                DispatchMessage (&msg);
                    149: 
                    150:                if (msg.message == WM_APP
                    151:                        && ObjectCount < 1
                    152:                        && !factory.IsServerLocked ())
                    153:                        break;
                    154:        }
                    155:        CoRevokeClassObject (cookie);
                    156: 
                    157:        return TRUE;
                    158: }
                    159: 
                    160: 
1.1.1.3   root      161: static BOOL ComGetInstance (HWND hWnd, ITrueCryptMainCom **tcServer)
1.1       root      162: {
1.1.1.3   root      163:        return ComGetInstanceBase (hWnd, CLSID_TrueCryptMainCom, IID_ITrueCryptMainCom, (void **) tcServer);
                    164: }
                    165: 
                    166: 
                    167: ITrueCryptMainCom *GetElevatedInstance (HWND parent)
                    168: {
                    169:        ITrueCryptMainCom *instance;
                    170: 
                    171:        if (!ComGetInstance (parent, &instance))
                    172:                throw UserAbort (SRC_POS);
                    173: 
                    174:        return instance;
1.1       root      175: }
                    176: 
                    177: 
                    178: extern "C" int UacBackupVolumeHeader (HWND hwndDlg, BOOL bRequireConfirmation, char *lpszVolume)
                    179: {
1.1.1.3   root      180:        CComPtr<ITrueCryptMainCom> tc;
1.1       root      181:        int r;
                    182: 
                    183:        CoInitialize (NULL);
                    184: 
                    185:        if (ComGetInstance (hwndDlg, &tc))
                    186:                r = tc->BackupVolumeHeader ((LONG_PTR) hwndDlg, bRequireConfirmation, CComBSTR (lpszVolume));
                    187:        else
                    188:                r = -1;
                    189: 
                    190:        CoUninitialize ();
                    191: 
                    192:        return r;
                    193: }
                    194: 
                    195: 
                    196: extern "C" int UacRestoreVolumeHeader (HWND hwndDlg, char *lpszVolume)
                    197: {
1.1.1.3   root      198:        CComPtr<ITrueCryptMainCom> tc;
1.1       root      199:        int r;
                    200: 
                    201:        CoInitialize (NULL);
                    202: 
                    203:        if (ComGetInstance (hwndDlg, &tc))
                    204:                r = tc->RestoreVolumeHeader ((LONG_PTR) hwndDlg, CComBSTR (lpszVolume));
                    205:        else
                    206:                r = -1;
                    207: 
                    208:        CoUninitialize ();
                    209: 
                    210:        return r;
                    211: }
                    212: 
                    213: 
                    214: extern "C" int UacChangePwd (char *lpszVolume, Password *oldPassword, Password *newPassword, int pkcs5, HWND hwndDlg)
                    215: {
1.1.1.3   root      216:        CComPtr<ITrueCryptMainCom> tc;
1.1       root      217:        int r;
                    218: 
                    219:        if (ComGetInstance (hwndDlg, &tc))
                    220:        {
                    221:                WaitCursor ();
                    222:                r = tc->ChangePassword (CComBSTR (lpszVolume), oldPassword, newPassword, 0, (LONG_PTR) hwndDlg);
                    223:                NormalCursor ();
                    224:        }
                    225:        else
                    226:                r = -1;
                    227: 
                    228:        return r;
                    229: }

unix.superglobalmegacorp.com

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