"Fossies" - the Fresh Open Source Software Archive

Member "src/Core/Unix/MacOSX/CoreMacOSX.cpp" (10 Oct 2018, 5796 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 "CoreMacOSX.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.18_Source_vs_1.19_Source.

    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 <fstream>
   14 #include <stdio.h>
   15 #include <unistd.h>
   16 #include <sys/param.h>
   17 #include <sys/ucred.h>
   18 #include <sys/mount.h>
   19 #include <sys/sysctl.h>
   20 #include <sys/types.h>
   21 #include <sys/wait.h>
   22 #include <sys/stat.h>
   23 #include "CoreMacOSX.h"
   24 #include "Driver/Fuse/FuseService.h"
   25 #include "Core/Unix/CoreServiceProxy.h"
   26 
   27 namespace VeraCrypt
   28 {
   29     CoreMacOSX::CoreMacOSX ()
   30     {
   31     }
   32 
   33     CoreMacOSX::~CoreMacOSX ()
   34     {
   35     }
   36 
   37     shared_ptr <VolumeInfo> CoreMacOSX::DismountVolume (shared_ptr <VolumeInfo> mountedVolume, bool ignoreOpenFiles, bool syncVolumeInfo)
   38     {
   39         if (!mountedVolume->VirtualDevice.IsEmpty() && mountedVolume->VirtualDevice.IsBlockDevice())
   40         {
   41             list <string> args;
   42             args.push_back ("detach");
   43             args.push_back (mountedVolume->VirtualDevice);
   44 
   45             if (ignoreOpenFiles)
   46                 args.push_back ("-force");
   47 
   48             try
   49             {
   50                 Process::Execute ("hdiutil", args);
   51             }
   52             catch (ExecutedProcessFailed &e)
   53             {
   54                 if (!ignoreOpenFiles)
   55                 {
   56                     string err = e.GetErrorOutput();
   57 
   58                     if (err.find ("couldn't unmount") != string::npos
   59                         || err.find ("busy") != string::npos
   60                         || err.find ("49153") != string::npos)
   61                     {
   62                         throw MountedVolumeInUse (SRC_POS);
   63                     }
   64                 }
   65 
   66                 throw;
   67             }
   68         }
   69 
   70         if (syncVolumeInfo || mountedVolume->Protection == VolumeProtection::HiddenVolumeReadOnly)
   71         {
   72             sync();
   73             VolumeInfoList ml = GetMountedVolumes (mountedVolume->Path);
   74 
   75             if (ml.size() > 0)
   76                 mountedVolume = ml.front();
   77         }
   78 
   79         list <string> args;
   80         args.push_back ("--");
   81         args.push_back (mountedVolume->AuxMountPoint);
   82 
   83         for (int t = 0; true; t++)
   84         {
   85             try
   86             {
   87                 Process::Execute ("umount", args);
   88                 break;
   89             }
   90             catch (ExecutedProcessFailed&)
   91             {
   92                 if (t > 10)
   93                     throw;
   94                 Thread::Sleep (200);
   95             }
   96         }
   97 
   98         try
   99         {
  100             mountedVolume->AuxMountPoint.Delete();
  101         }
  102         catch (...) { }
  103 
  104         return mountedVolume;
  105     }
  106 
  107     void CoreMacOSX::CheckFilesystem (shared_ptr <VolumeInfo> mountedVolume, bool repair) const
  108     {
  109         list <string> args;
  110         args.push_back ("/Applications/Utilities/Disk Utility.app");
  111         Process::Execute ("open", args);
  112     }
  113 
  114     void CoreMacOSX::MountAuxVolumeImage (const DirectoryPath &auxMountPoint, const MountOptions &options) const
  115     {
  116         // Check FUSE version
  117         char fuseVersionString[MAXHOSTNAMELEN + 1] = { 0 };
  118         size_t fuseVersionStringLength = MAXHOSTNAMELEN;
  119         int status;
  120 
  121         if ((status = sysctlbyname ("osxfuse.version.number", fuseVersionString, &fuseVersionStringLength, NULL, 0)) != 0)
  122         {
  123             fuseVersionStringLength = MAXHOSTNAMELEN;
  124             if ((status = sysctlbyname ("vfs.generic.osxfuse.version.number", fuseVersionString, &fuseVersionStringLength, NULL, 0)) != 0)
  125             {
  126                 throw HigherFuseVersionRequired (SRC_POS);
  127             }
  128         }
  129 
  130         // look for OSXFuse dynamic library
  131         struct stat sb;
  132         if (0 != stat("/usr/local/lib/libosxfuse_i64.2.dylib", &sb))
  133         {
  134             throw HigherFuseVersionRequired (SRC_POS);
  135         }
  136 
  137         vector <string> fuseVersion = StringConverter::Split (string (fuseVersionString), ".");
  138         if (fuseVersion.size() < 2)
  139             throw HigherFuseVersionRequired (SRC_POS);
  140 
  141         uint32 fuseVersionMajor = StringConverter::ToUInt32 (fuseVersion[0]);
  142         uint32 fuseVersionMinor = StringConverter::ToUInt32 (fuseVersion[1]);
  143 
  144         if (fuseVersionMajor < 2 || (fuseVersionMajor == 2 && fuseVersionMinor < 5))
  145             throw HigherFuseVersionRequired (SRC_POS);
  146 
  147         // Mount volume image
  148         string volImage = string (auxMountPoint) + FuseService::GetVolumeImagePath();
  149 
  150         list <string> args;
  151         args.push_back ("attach");
  152         args.push_back (volImage);
  153         args.push_back ("-plist");
  154         args.push_back ("-noautofsck");
  155         args.push_back ("-imagekey");
  156         args.push_back ("diskimage-class=CRawDiskImage");
  157 
  158         if (!options.NoFilesystem && options.MountPoint && !options.MountPoint->IsEmpty())
  159         {
  160             args.push_back ("-mount");
  161             args.push_back ("required");
  162 
  163             // Let the system specify mount point except when the user specified a non-default one
  164             if (string (*options.MountPoint).find (GetDefaultMountPointPrefix()) != 0)
  165             {
  166                 args.push_back ("-mountpoint");
  167                 args.push_back (*options.MountPoint);
  168             }
  169         }
  170         else
  171             args.push_back ("-nomount");
  172 
  173         if (options.Protection == VolumeProtection::ReadOnly)
  174             args.push_back ("-readonly");
  175 
  176         string xml;
  177 
  178         while (true)
  179         {
  180             try
  181             {
  182                 xml = Process::Execute ("hdiutil", args);
  183                 break;
  184             }
  185             catch (ExecutedProcessFailed &e)
  186             {
  187                 if (e.GetErrorOutput().find ("noautofsck") != string::npos)
  188                 {
  189                     args.remove ("-noautofsck");
  190                     continue;
  191                 }
  192 
  193                 throw;
  194             }
  195         }
  196 
  197         size_t p = xml.find ("<key>dev-entry</key>");
  198         if (p == string::npos)
  199             throw ParameterIncorrect (SRC_POS);
  200 
  201         p = xml.find ("<string>", p);
  202         if (p == string::npos)
  203             throw ParameterIncorrect (SRC_POS);
  204         p += 8;
  205 
  206         size_t e = xml.find ("</string>", p);
  207         if (e == string::npos)
  208             throw ParameterIncorrect (SRC_POS);
  209 
  210         DevicePath virtualDev = StringConverter::Trim (xml.substr (p, e - p));
  211 
  212         try
  213         {
  214             FuseService::SendAuxDeviceInfo (auxMountPoint, virtualDev);
  215         }
  216         catch (...)
  217         {
  218             try
  219             {
  220                 list <string> args;
  221                 args.push_back ("detach");
  222                 args.push_back (volImage);
  223                 args.push_back ("-force");
  224 
  225                 Process::Execute ("hdiutil", args);
  226             }
  227             catch (ExecutedProcessFailed&) { }
  228             throw;
  229         }
  230     }
  231 
  232     auto_ptr <CoreBase> Core (new CoreServiceProxy <CoreMacOSX>);
  233     auto_ptr <CoreBase> CoreDirect (new CoreMacOSX);
  234 }