VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Core/Unix/Solaris/CoreSolaris.cpp
blob: 9101e4f80411619603b918bddd804c78b62bd5f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 Derived from source code of TrueCrypt 7.1a, which is
 Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
 by the TrueCrypt License 3.0.

 Modifications and additions to the original source code (contained in this file) 
 and all other portions of this file are Copyright (c) 2013-2016 IDRIX
 and are governed by the Apache License 2.0 the full text of which is
 contained in the file License.txt included in VeraCrypt binary and source
 code distribution packages.
*/

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/mntent.h>
#include <sys/mnttab.h>
#include "CoreSolaris.h"
#include "Core/Unix/CoreServiceProxy.h"

namespace VeraCrypt
{
	CoreSolaris::CoreSolaris ()
	{
	}

	CoreSolaris::~CoreSolaris ()
	{
	}

	DevicePath CoreSolaris::AttachFileToLoopDevice (const FilePath &filePath, bool readOnly) const
	{
		list <string> args;
		args.push_back ("-a");
		args.push_back (filePath);

		return StringConverter::Trim (Process::Execute ("lofiadm", args));
	}

	void CoreSolaris::DetachLoopDevice (const DevicePath &devicePath) const
	{
		list <string> args;
		args.push_back ("-d");
		args.push_back (devicePath);

		for (int t = 0; true; t++)
		{
			try
			{
				Process::Execute ("lofiadm", args);
				break;
			}
			catch (ExecutedProcessFailed&)
			{
				if (t > 5)
					throw;
				Thread::Sleep (200);
			}
		}
	}

	HostDeviceList CoreSolaris::GetHostDevices (bool pathListOnly) const
	{
		HostDeviceList devices;

		foreach_ref (const FilePath &devPath, Directory::GetFilePaths ("/dev/rdsk", false))
		{
			string drivePath = devPath;
			if (drivePath.rfind ("p0") == drivePath.size() - 2)
			{
				make_shared_auto (HostDevice, device);
				device->Path = drivePath;

				try
				{
					device->Size = GetDeviceSize (device->Path);
				}
				catch (...)
				{
					device->Size = 0;
				}
				
				if (device->Size == 0)
					continue;

				device->MountPoint = GetDeviceMountPoint (device->Path);
				device->SystemNumber = 0;

				devices.push_back (device);

				for (int partNumber = 1; partNumber <= 32; partNumber++)
				{
					stringstream partPath;
					partPath << drivePath.substr (0, drivePath.size() - 1) << partNumber;

					if (FilesystemPath (partPath.str()).IsBlockDevice() || FilesystemPath (partPath.str()).IsCharacterDevice())
					{
						make_shared_auto (HostDevice, partition);
						partition->Path = partPath.str();

						try 
						{	        
							partition->Size = GetDeviceSize (partition->Path);
						}
						catch (...)
						{
							partition->Size = 0;
						}

						if (partition->Size == 0)
							continue;

						partition->MountPoint = GetDeviceMountPoint (partition->Path);
						partition->SystemNumber = 0;

						device->Partitions.push_back (partition);
					}
				}
			}
		}

		return devices;
	}

	MountedFilesystemList CoreSolaris::GetMountedFilesystems (const DevicePath &devicePath, const DirectoryPath &mountPoint) const
	{
		MountedFilesystemList mountedFilesystems;

		FILE *mtab = fopen ("/etc/mnttab", "r");
		throw_sys_sub_if (!mtab, "/etc/mnttab");
		finally_do_arg (FILE *, mtab, { fclose (finally_arg); });

		int getmntentResult;
		struct mnttab entry;
		while ((getmntentResult = getmntent (mtab, &entry)) == 0)
		{
			make_shared_auto (MountedFilesystem, mf);

			if (entry.mnt_special)
				mf->Device = DevicePath (entry.mnt_special);
			else
				continue;

			if (entry.mnt_mountp)
				mf->MountPoint = DirectoryPath (entry.mnt_mountp);

			if (entry.mnt_fstype)
				mf->Type = entry.mnt_fstype;

			if ((devicePath.IsEmpty() || devicePath == mf->Device) && (mountPoint.IsEmpty() || mountPoint == mf->MountPoint))
				mountedFilesystems.push_back (mf);
		}

		throw_sys_if (getmntentResult > 0);

		return mountedFilesystems;
	}

	void CoreSolaris::MountFilesystem (const DevicePath &devicePath, const DirectoryPath &mountPoint, const string &filesystemType, bool readOnly, const string &systemMountOptions) const
	{
		try
		{
			// Try to mount FAT by default as mount is unable to probe filesystem type on Solaris
			CoreUnix::MountFilesystem (devicePath, mountPoint, filesystemType.empty() ? "pcfs" : filesystemType, readOnly, systemMountOptions);
		}
		catch (ExecutedProcessFailed&)
		{
			if (!filesystemType.empty())
				throw;

			CoreUnix::MountFilesystem (devicePath, mountPoint, filesystemType, readOnly, systemMountOptions);
		}
	}

	auto_ptr <CoreBase> Core (new CoreServiceProxy <CoreSolaris>);
	auto_ptr <CoreBase> CoreDirect (new CoreSolaris);
}