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

1.1       root        1: /*
                      2:  Copyright (c) TrueCrypt Foundation. All rights reserved.
                      3: 
1.1.1.2 ! root        4:  Covered by the TrueCrypt License 2.3 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 <strsafe.h>
                     12: #include <windows.h>
                     13: #include "BaseCom.h"
                     14: #include "Dlgcode.h"
                     15: #include "MainCom.h"
                     16: #include "MainCom_h.h"
                     17: #include "MainCom_i.c"
                     18: #include "Password.h"
                     19: 
                     20: static volatile LONG ObjectCount = 0;
                     21: 
                     22: class TrueCrypt : public ITrueCrypt
                     23: {
                     24: 
                     25: public:
                     26:        TrueCrypt (DWORD messageThreadId) : RefCount (0), MessageThreadId (messageThreadId)
                     27:        {
                     28:                InterlockedIncrement (&ObjectCount);
                     29:        }
                     30: 
                     31:        ~TrueCrypt ()
                     32:        {
                     33:                if (InterlockedDecrement (&ObjectCount) == 0)
                     34:                        PostThreadMessage (MessageThreadId, WM_APP, 0, 0);
                     35:        }
                     36: 
                     37:        virtual ULONG STDMETHODCALLTYPE AddRef ()
                     38:        {
                     39:                return InterlockedIncrement (&RefCount);
                     40:        }
                     41: 
                     42:        virtual ULONG STDMETHODCALLTYPE Release ()
                     43:        {
                     44:                if (!InterlockedDecrement (&RefCount))
                     45:                {
                     46:                        delete this;
                     47:                        return 0;
                     48:                }
                     49: 
                     50:                return RefCount;
                     51:        }
                     52: 
                     53:        virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID riid, void **ppvObject)
                     54:        {
                     55:                if (riid == IID_IUnknown || riid == IID_ITrueCrypt)
                     56:                        *ppvObject = this;
                     57:                else
                     58:                {
                     59:                        *ppvObject = NULL;
                     60:                        return E_NOINTERFACE;
                     61:                }
                     62: 
                     63:                AddRef ();
                     64:                return S_OK;
                     65:        }
                     66:        
                     67:        virtual int STDMETHODCALLTYPE BackupVolumeHeader (LONG_PTR hwndDlg, BOOL bRequireConfirmation, BSTR lpszVolume)
                     68:        {
                     69:                USES_CONVERSION;
                     70:                return ::BackupVolumeHeader ((HWND) hwndDlg, bRequireConfirmation, CW2A (lpszVolume));
                     71:        }
                     72: 
                     73:        virtual int STDMETHODCALLTYPE RestoreVolumeHeader (LONG_PTR hwndDlg, BSTR lpszVolume)
                     74:        {
                     75:                USES_CONVERSION;
                     76:                return ::RestoreVolumeHeader ((HWND) hwndDlg, CW2A (lpszVolume));
                     77:        }
                     78: 
                     79:        virtual int STDMETHODCALLTYPE ChangePassword (BSTR volumePath, Password *oldPassword, Password *newPassword, int pkcs5, LONG_PTR hWnd)
                     80:        {
                     81:                USES_CONVERSION;
                     82:                return ::ChangePwd (CW2A (volumePath), oldPassword, newPassword, pkcs5, (HWND) hWnd);
                     83:        }
                     84: 
                     85: protected:
                     86:        DWORD MessageThreadId;
                     87:        LONG RefCount;
                     88: };
                     89: 
                     90: 
                     91: extern "C" BOOL ComServerMain ()
                     92: {
                     93:        TrueCryptFactory<TrueCrypt> factory (GetCurrentThreadId ());
                     94:        DWORD cookie;
                     95: 
                     96:        if (IsUacSupported ())
                     97:                UacElevated = TRUE;
                     98: 
                     99:        if (CoRegisterClassObject (CLSID_TrueCrypt, (LPUNKNOWN) &factory,
                    100:                CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie) != S_OK)
                    101:                return FALSE;
                    102: 
                    103:        MSG msg;
1.1.1.2 ! root      104:        while (int r = GetMessage (&msg, NULL, 0, 0))
1.1       root      105:        {
1.1.1.2 ! root      106:                if (r == -1)
        !           107:                        return FALSE;
        !           108: 
1.1       root      109:                TranslateMessage (&msg);
                    110:                DispatchMessage (&msg);
                    111: 
                    112:                if (msg.message == WM_APP
                    113:                        && ObjectCount < 1
                    114:                        && !factory.IsServerLocked ())
                    115:                        break;
                    116:        }
                    117:        CoRevokeClassObject (cookie);
                    118: 
                    119:        return TRUE;
                    120: }
                    121: 
                    122: 
                    123: static BOOL ComGetInstance (HWND hWnd, ITrueCrypt **tcServer)
                    124: {
                    125:        return ComGetInstanceBase (hWnd, CLSID_TrueCrypt, IID_ITrueCrypt, (void **) tcServer);
                    126: }
                    127: 
                    128: 
                    129: extern "C" int UacBackupVolumeHeader (HWND hwndDlg, BOOL bRequireConfirmation, char *lpszVolume)
                    130: {
                    131:        CComPtr<ITrueCrypt> tc;
                    132:        int r;
                    133: 
                    134:        CoInitialize (NULL);
                    135: 
                    136:        if (ComGetInstance (hwndDlg, &tc))
                    137:                r = tc->BackupVolumeHeader ((LONG_PTR) hwndDlg, bRequireConfirmation, CComBSTR (lpszVolume));
                    138:        else
                    139:                r = -1;
                    140: 
                    141:        CoUninitialize ();
                    142: 
                    143:        return r;
                    144: }
                    145: 
                    146: 
                    147: extern "C" int UacRestoreVolumeHeader (HWND hwndDlg, char *lpszVolume)
                    148: {
                    149:        CComPtr<ITrueCrypt> tc;
                    150:        int r;
                    151: 
                    152:        CoInitialize (NULL);
                    153: 
                    154:        if (ComGetInstance (hwndDlg, &tc))
                    155:                r = tc->RestoreVolumeHeader ((LONG_PTR) hwndDlg, CComBSTR (lpszVolume));
                    156:        else
                    157:                r = -1;
                    158: 
                    159:        CoUninitialize ();
                    160: 
                    161:        return r;
                    162: }
                    163: 
                    164: 
                    165: extern "C" int UacChangePwd (char *lpszVolume, Password *oldPassword, Password *newPassword, int pkcs5, HWND hwndDlg)
                    166: {
                    167:        CComPtr<ITrueCrypt> tc;
                    168:        int r;
                    169: 
                    170:        if (ComGetInstance (hwndDlg, &tc))
                    171:        {
                    172:                WaitCursor ();
                    173:                r = tc->ChangePassword (CComBSTR (lpszVolume), oldPassword, newPassword, 0, (LONG_PTR) hwndDlg);
                    174:                NormalCursor ();
                    175:        }
                    176:        else
                    177:                r = -1;
                    178: 
                    179:        return r;
                    180: }

unix.superglobalmegacorp.com

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