"Fossies" - the Fresh Open Source Software Archive

Member "src/Main/UserPreferences.cpp" (10 Oct 2018, 8397 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "UserPreferences.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #include "System.h"
   14 #include "Main/Application.h"
   15 #include "UserPreferences.h"
   16 #include "Xml.h"
   17 
   18 namespace VeraCrypt
   19 {
   20     void UserPreferences::SetValue (const wxString &cfgText, bool &cfgVar)
   21     {
   22         if (cfgText == L"0")
   23             cfgVar = false;
   24         else if (cfgText == L"1")
   25             cfgVar = true;
   26     }
   27 
   28     void UserPreferences::SetValue (const wxString &cfgText, int &cfgVar)
   29     {
   30         if (cfgText.empty())
   31             cfgVar = 0;
   32         else
   33             cfgVar = StringConverter::ToUInt32 (wstring (cfgText));
   34     }
   35 
   36     void UserPreferences::SetValue (const wxString &cfgText, uint64 &cfgVar)
   37     {
   38         if (cfgText.empty())
   39             cfgVar = 0;
   40         else
   41             cfgVar = StringConverter::ToUInt64 (wstring (cfgText));
   42     }
   43 
   44     void UserPreferences::SetValue (const wxString &cfgText, wstring &cfgVar)
   45     {
   46         cfgVar = cfgText;
   47     }
   48 
   49     void UserPreferences::SetValue (const wxString &cfgText, wxString &cfgVar)
   50     {
   51         cfgVar = cfgText;
   52     }
   53 
   54     void UserPreferences::SetValue (const wxString &cfgText, FilesystemPath &cfgVar)
   55     {
   56         cfgVar = wstring (cfgText);
   57     }
   58 
   59     void UserPreferences::Load()
   60     {
   61         // Preferences
   62         FilePath cfgPath = Application::GetConfigFilePath (GetPreferencesFileName());
   63         if (cfgPath.IsFile())
   64         {
   65             map <wxString, wxString> configMap;
   66             foreach (XmlNode node, XmlParser (cfgPath).GetNodes (L"config"))
   67             {
   68                 configMap[node.Attributes[L"key"]] = node.InnerText;
   69             }
   70 
   71 #define TC_CONFIG_SET(NAME) SetValue (configMap[L###NAME], NAME)
   72 
   73             TC_CONFIG_SET (BackgroundTaskEnabled);
   74             TC_CONFIG_SET (BackgroundTaskMenuDismountItemsEnabled);
   75             TC_CONFIG_SET (BackgroundTaskMenuMountItemsEnabled);
   76             TC_CONFIG_SET (BackgroundTaskMenuOpenItemsEnabled);
   77             TC_CONFIG_SET (BeepAfterHotkeyMountDismount);
   78             SetValue (configMap[L"CachePasswords"], DefaultMountOptions.CachePassword);
   79             TC_CONFIG_SET (CloseBackgroundTaskOnNoVolumes);
   80             TC_CONFIG_SET (CloseExplorerWindowsOnDismount);
   81             TC_CONFIG_SET (CloseSecurityTokenSessionsAfterMount);
   82             TC_CONFIG_SET (DisableKernelEncryptionModeWarning);
   83             TC_CONFIG_SET (DismountOnInactivity);
   84             TC_CONFIG_SET (DismountOnLogOff);
   85             TC_CONFIG_SET (DismountOnPowerSaving);
   86             TC_CONFIG_SET (DismountOnScreenSaver);
   87             TC_CONFIG_SET (DisplayMessageAfterHotkeyDismount);
   88             TC_CONFIG_SET (BackgroundTaskEnabled);
   89             SetValue (configMap[L"FilesystemOptions"], DefaultMountOptions.FilesystemOptions);
   90             TC_CONFIG_SET (ForceAutoDismount);
   91             TC_CONFIG_SET (LastSelectedSlotNumber);
   92             TC_CONFIG_SET (MaxVolumeIdleTime);
   93             TC_CONFIG_SET (MountDevicesOnLogon);
   94             TC_CONFIG_SET (MountFavoritesOnLogon);
   95 
   96             bool readOnly;
   97             SetValue (configMap[L"MountVolumesReadOnly"], readOnly);
   98             DefaultMountOptions.Protection = readOnly ? VolumeProtection::ReadOnly : VolumeProtection::None;
   99 
  100             SetValue (configMap[L"MountVolumesRemovable"], DefaultMountOptions.Removable);
  101             SetValue (configMap[L"NoHardwareCrypto"], DefaultMountOptions.NoHardwareCrypto);
  102             SetValue (configMap[L"NoKernelCrypto"], DefaultMountOptions.NoKernelCrypto);
  103             TC_CONFIG_SET (OpenExplorerWindowAfterMount);
  104             SetValue (configMap[L"PreserveTimestamps"], DefaultMountOptions.PreserveTimestamps);
  105             TC_CONFIG_SET (SaveHistory);
  106             SetValue (configMap[L"SecurityTokenLibrary"], SecurityTokenModule);
  107             TC_CONFIG_SET (StartOnLogon);
  108             TC_CONFIG_SET (UseKeyfiles);
  109             TC_CONFIG_SET (WipeCacheOnAutoDismount);
  110             TC_CONFIG_SET (WipeCacheOnClose);
  111 
  112             SetValue (configMap[L"DefaultTrueCryptMode"], DefaultMountOptions.TrueCryptMode);
  113 
  114             wstring defaultPrf;
  115             SetValue (configMap[L"DefaultPRF"], defaultPrf);
  116 
  117             shared_ptr <Pkcs5Kdf> savedKdf;
  118             try
  119             {
  120                 if (defaultPrf != L"autodetection")
  121                     savedKdf = Pkcs5Kdf::GetAlgorithm (defaultPrf, DefaultMountOptions.TrueCryptMode);
  122             }
  123             catch (ParameterIncorrect&)
  124             {
  125             }
  126 
  127             DefaultMountOptions.Kdf = savedKdf;
  128             DefaultMountOptions.ProtectionKdf = savedKdf;
  129         }
  130 
  131         // Default keyfiles
  132         cfgPath = Application::GetConfigFilePath (GetDefaultKeyfilesFileName());
  133         if (cfgPath.IsFile())
  134         {
  135             foreach (const XmlNode &node, XmlParser (cfgPath).GetNodes (L"keyfile"))
  136             {
  137                 DefaultKeyfiles.push_back (make_shared <Keyfile> ((wstring) node.InnerText));
  138             }
  139         }
  140 
  141 #ifdef TC_WINDOWS
  142         // Hotkeys
  143         Hotkeys = Hotkey::LoadList();
  144 #endif
  145     }
  146 
  147     void UserPreferences::Save() const
  148     {
  149         // Preferences
  150         class ConfigXmlFormatter
  151         {
  152         public:
  153             void AddEntry (const wchar_t *key, const wxString &value)
  154             {
  155                 if (!value.empty())
  156                 {
  157                     XmlNode config (L"config");
  158                     config.Attributes[L"key"] = key;
  159                     config.InnerText = value;
  160                     XmlConfig.InnerNodes.push_back (config);
  161                 }
  162             }
  163 
  164             void AddEntry (const wchar_t *key, bool value)
  165             {
  166                 AddEntry (key, wxString (value ? L"1" : L"0"));
  167             }
  168 
  169             void AddEntry (const wchar_t *key, int value)
  170             {
  171                 wstringstream s;
  172                 s << value;
  173                 AddEntry (key, s.str());
  174             }
  175 
  176             void AddEntry (const wchar_t *key, uint64 value)
  177             {
  178                 wstringstream s;
  179                 s << value;
  180                 AddEntry (key, s.str());
  181             }
  182 
  183             XmlNode XmlConfig;
  184         };
  185 
  186         ConfigXmlFormatter formatter;
  187         formatter.XmlConfig.Name = L"configuration";
  188 
  189 #define TC_CONFIG_ADD(NAME) formatter.AddEntry (L###NAME, NAME)
  190 
  191         TC_CONFIG_ADD (BackgroundTaskEnabled);
  192         TC_CONFIG_ADD (BackgroundTaskMenuDismountItemsEnabled);
  193         TC_CONFIG_ADD (BackgroundTaskMenuMountItemsEnabled);
  194         TC_CONFIG_ADD (BackgroundTaskMenuOpenItemsEnabled);
  195         TC_CONFIG_ADD (BeepAfterHotkeyMountDismount);
  196         formatter.AddEntry (L"CachePasswords", DefaultMountOptions.CachePassword);
  197         TC_CONFIG_ADD (CloseBackgroundTaskOnNoVolumes);
  198         TC_CONFIG_ADD (CloseExplorerWindowsOnDismount);
  199         TC_CONFIG_ADD (CloseSecurityTokenSessionsAfterMount);
  200         TC_CONFIG_ADD (DisableKernelEncryptionModeWarning);
  201         TC_CONFIG_ADD (DismountOnInactivity);
  202         TC_CONFIG_ADD (DismountOnLogOff);
  203         TC_CONFIG_ADD (DismountOnPowerSaving);
  204         TC_CONFIG_ADD (DismountOnScreenSaver);
  205         TC_CONFIG_ADD (DisplayMessageAfterHotkeyDismount);
  206         TC_CONFIG_ADD (BackgroundTaskEnabled);
  207         formatter.AddEntry (L"FilesystemOptions", DefaultMountOptions.FilesystemOptions);
  208         TC_CONFIG_ADD (ForceAutoDismount);
  209         TC_CONFIG_ADD (LastSelectedSlotNumber);
  210         TC_CONFIG_ADD (MaxVolumeIdleTime);
  211         TC_CONFIG_ADD (MountDevicesOnLogon);
  212         TC_CONFIG_ADD (MountFavoritesOnLogon);
  213         formatter.AddEntry (L"MountVolumesReadOnly", DefaultMountOptions.Protection == VolumeProtection::ReadOnly);
  214         formatter.AddEntry (L"MountVolumesRemovable", DefaultMountOptions.Removable);
  215         formatter.AddEntry (L"NoHardwareCrypto", DefaultMountOptions.NoHardwareCrypto);
  216         formatter.AddEntry (L"NoKernelCrypto", DefaultMountOptions.NoKernelCrypto);
  217         TC_CONFIG_ADD (OpenExplorerWindowAfterMount);
  218         formatter.AddEntry (L"PreserveTimestamps", DefaultMountOptions.PreserveTimestamps);
  219         TC_CONFIG_ADD (SaveHistory);
  220         formatter.AddEntry (L"SecurityTokenLibrary", wstring (SecurityTokenModule));
  221         TC_CONFIG_ADD (StartOnLogon);
  222         TC_CONFIG_ADD (UseKeyfiles);
  223         TC_CONFIG_ADD (WipeCacheOnAutoDismount);
  224         TC_CONFIG_ADD (WipeCacheOnClose);
  225 
  226         formatter.AddEntry (L"DefaultTrueCryptMode", DefaultMountOptions.TrueCryptMode);
  227 
  228         wstring defaultPrf = L"autodetection";
  229         if (DefaultMountOptions.Kdf)
  230             defaultPrf = DefaultMountOptions.Kdf->GetName ();
  231         formatter.AddEntry (L"DefaultPRF", defaultPrf);
  232 
  233         XmlWriter writer (Application::GetConfigFilePath (GetPreferencesFileName(), true));
  234         writer.WriteNode (formatter.XmlConfig);
  235         writer.Close();
  236 
  237         // Default keyfiles
  238         FilePath keyfilesCfgPath = Application::GetConfigFilePath (GetDefaultKeyfilesFileName(), true);
  239 
  240         if (DefaultKeyfiles.empty())
  241         {
  242             if (keyfilesCfgPath.IsFile())
  243                 keyfilesCfgPath.Delete();
  244         }
  245         else
  246         {
  247             XmlNode keyfilesXml (L"defaultkeyfiles");
  248 
  249             foreach_ref (const Keyfile &keyfile, DefaultKeyfiles)
  250             {
  251                 keyfilesXml.InnerNodes.push_back (XmlNode (L"keyfile", wxString (wstring(FilesystemPath (keyfile)))));
  252             }
  253 
  254             XmlWriter keyfileWriter (keyfilesCfgPath);
  255             keyfileWriter.WriteNode (keyfilesXml);
  256             keyfileWriter.Close();
  257         }
  258 
  259 #ifdef TC_WINDOWS
  260         // Hotkeys
  261         Hotkey::SaveList (Hotkeys);
  262 #endif
  263     }
  264 }