Example #1
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            string path = txtFilePath.Text;

            if (path == String.Empty)
            {
                MessageBox.Show("Please choose file location.", "Error");
                return;
            }
            DiskImage diskImage;

            try
            {
                diskImage = DiskImage.GetDiskImage(path);
            }
            catch (IOException ex)
            {
                MessageBox.Show("Can't open disk image: " + ex.Message, "Error");
                return;
            }
            catch (InvalidDataException ex)
            {
                MessageBox.Show("Invalid disk image: " + ex.Message, "Error");
                return;
            }
            catch (NotImplementedException)
            {
                MessageBox.Show("Unsupported Disk Image Format", "Error");
                return;
            }

            diskImage.IsReadOnly = chkReadOnly.Checked;
            bool isLocked = false;

            try
            {
                isLocked = diskImage.ExclusiveLock();
            }
            catch (IOException)
            {
            }
            if (!isLocked)
            {
                MessageBox.Show("Cannot lock the disk image for exclusive access.", "Error");
                return;
            }
            m_diskImage       = diskImage;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
Example #2
0
        public static void SelectCommand(string[] args)
        {
            if (args.Length == 1)
            {
                HelpSelect();
                return;
            }

            switch (args[1].ToLower())
            {
            case "disk":
            {
                if (args.Length == 3)
                {
                    int diskIndex = Conversion.ToInt32(args[2], -1);
                    if (diskIndex >= 0)
                    {
                        PhysicalDisk disk = null;
                        try
                        {
                            disk = new PhysicalDisk(diskIndex);
                        }
                        catch
                        {
                            Console.WriteLine();
                            Console.WriteLine("Error: Invalid disk number");
                        }

                        if (disk != null)
                        {
                            m_selectedDisk = disk;
                        }
                    }
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("Error: Invalid number of arguments");
                }
                break;
            }

            case "vdisk":
            {
                if (args.Length == 3)
                {
                    KeyValuePairList <string, string> parameters = ParseParameters(args, 2);
                    if (parameters.ContainsKey("file"))
                    {
                        string path = parameters.ValueOf("file");
                        if (new FileInfo(path).Exists)
                        {
                            try
                            {
                                m_selectedDisk = DiskImage.GetDiskImage(path);
                            }
                            catch (InvalidDataException)
                            {
                                Console.WriteLine("Invalid virtual disk format");
                            }
                            catch (NotImplementedException)
                            {
                                Console.WriteLine("Unsupported virtual disk format");
                            }
                            catch (IOException ex)
                            {
                                Console.WriteLine("Cannot read file: " + ex.Message);
                            }
                        }
                        else
                        {
                            Console.WriteLine("File not found: \"{0}\"", path);
                        }
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine("Error: Invalid argument");
                    }
                }
                else
                {
                    Console.WriteLine("Error: Invalid number of arguments");
                }
                break;
            }

            case "partition":
            {
                if (m_selectedDisk != null)
                {
                    if (args.Length == 3)
                    {
                        int partitionIndex          = Conversion.ToInt32(args[2], -1);
                        List <Partition> partitions = BasicDiskHelper.GetPartitions(m_selectedDisk);
                        if (partitionIndex >= 0 && partitionIndex < partitions.Count)
                        {
                            m_selectedVolume = partitions[partitionIndex];
                        }
                        else
                        {
                            Console.WriteLine("Error: Invalid partition number");
                        }
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine("Error: Partition number was not specified");
                    }
                }
                else
                {
                    Console.WriteLine("No disk has been selected");
                }
                break;
            }

            case "volume":
            {
                if (args.Length == 3)
                {
                    List <Volume> volumes;
                    try
                    {
                        volumes = WindowsVolumeHelper.GetVolumes();
                    }
                    catch
                    {
                        volumes = new List <Volume>();
                    }

                    int volumeIndex = Conversion.ToInt32(args[2], -1);
                    if (volumeIndex >= 0 && volumeIndex < volumes.Count)
                    {
                        m_selectedVolume = volumes[volumeIndex];
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine("Error: Invalid volume number");
                    }
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("Error: Volume number was not specified");
                }
                break;
            }

            default:
                HelpSelect();
                break;
            }
        }
Example #3
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

            this.Text           += " v" + version.ToString(3);
            m_server.OnLogEntry += Program.OnLogEntry;

            List <IPAddress> localIPs = GetHostIPAddresses();
            KeyValuePairList <string, IPAddress> list = new KeyValuePairList <string, IPAddress>();

            list.Add("Any", IPAddress.Any);
            foreach (IPAddress address in localIPs)
            {
                list.Add(address.ToString(), address);
            }
            comboIPAddress.DataSource    = list;
            comboIPAddress.DisplayMember = "Key";
            comboIPAddress.ValueMember   = "Value";
            lblStatus.Text = "Author: Tal Aloni ([email protected])";
#if Win32
            if (!SecurityHelper.IsAdministrator())
            {
                lblStatus.Text = "Some features require administrator privileges and have been disabled";
            }
#endif
            //test
            string[] args = Environment.GetCommandLineArgs();
            if (args.Length > 1)
            {
                //to do

                /*
                 * if (args[0].Equals("DiskImage", StringComparison.OrdinalIgnoreCase)) { m_disks.Add(DiskImage.GetDiskImage(args[2], false)); }
                 */
            }
            if (System.IO.File.Exists("config.xml") && args.Length <= 1)
            {
                XmlDocument XmlDocObj = new XmlDocument();
                XmlDocObj.Load("config.xml");
                XmlNode node            = XmlDocObj.SelectSingleNode("//target");
                string  targetname      = "";
                string  targetpath      = "";
                string  targettype      = "";
                string  targetsize      = "";
                string  targetdiskindex = "";
                if (node.Attributes["name"] != null)
                {
                    targetname = node.Attributes["name"].Value;
                }
                if (node.Attributes["path"] != null)
                {
                    targetpath = node.Attributes["path"].Value;
                }
                if (node.Attributes["class"] != null)
                {
                    targettype = node.Attributes["class"].Value;
                }
                if (node.Attributes["size"] != null)
                {
                    targetsize = node.Attributes["size"].Value;
                }
                if (node.Attributes["index"] != null)
                {
                    targetdiskindex = node.Attributes["index"].Value;
                }
                if (targetname != "")
                {
                    //
                    List <Disk> m_disks = new List <Disk>();
                    if (targettype.Equals("RAMDisk", StringComparison.OrdinalIgnoreCase))
                    {
                        m_disks.Add(new RAMDisk(int.Parse(targetsize) * 1024 * 1024));
                    }
                    if (targettype.Equals("DiskImage", StringComparison.OrdinalIgnoreCase))
                    {
                        m_disks.Add(DiskImage.GetDiskImage(targetpath, false));
                    }
                    if (targettype.Equals("createDiskImage", StringComparison.OrdinalIgnoreCase))
                    {
                        m_disks.Add(VirtualHardDisk.CreateFixedDisk(targetpath, long.Parse(targetsize) * 1024 * 1024));
                    }
                    if (targettype.Equals("createRawDiskImage", StringComparison.OrdinalIgnoreCase))
                    {
                        m_disks.Add(RawDiskImage.Create(targetpath, long.Parse(targetsize) * 1024 * 1024));
                    }
                    if (targettype.Equals("PhysicalDisk", StringComparison.OrdinalIgnoreCase))
                    {
                        m_disks.Add(new PhysicalDisk(int.Parse(targetdiskindex)));
                    }
                    ISCSITarget target = new ISCSITarget(targetname, m_disks);
                    ((SCSI.VirtualSCSITarget)target.SCSITarget).OnLogEntry += Program.OnLogEntry;
                    target.OnAuthorizationRequest += new EventHandler <AuthorizationRequestArgs>(ISCSITarget_OnAuthorizationRequest);
                    target.OnSessionTermination   += new EventHandler <SessionTerminationArgs>(ISCSITarget_OnSessionTermination);
                    m_targets.Add(target);
                    //
                    try
                    {
                        m_server.AddTarget(target);
                    }
                    catch (ArgumentException ex)
                    {
                        MessageBox.Show(ex.Message, "Error");
                        return;
                    }
                    listTargets.Items.Add(target.TargetName);
                }
            }
        }
Example #4
0
        private void AdvanceToNextTab()
        {
            switch (tabControl1.SelectedIndex)
            {
            case 0:
            {
                if (txtVolumeFile.Text != String.Empty)
                {
                    try
                    {
                        m_password = GetPassword();
                    }
                    catch (IOException)
                    {
                        MessageBox.Show("Cannot access one or more keyfiles", "Error");
                        return;
                    }

                    try
                    {
                        m_disk = DiskImage.GetDiskImage(txtVolumeFile.Text);
                    }
                    catch (IOException)
                    {
                        MessageBox.Show("Cannot access the selected file", "Error");
                        return;
                    }
                    catch (NotImplementedException)
                    {
                        MessageBox.Show("Disk image format is not supported", "Error");
                        return;
                    }

                    if (DiskAccessLibrary.LogicalDiskManager.DynamicDisk.IsDynamicDisk(m_disk))
                    {
                        MessageBox.Show("Dynamic disks are not supported", "Error");
                        return;
                    }

                    try
                    {
                        m_partition = VolumeSelectionHelper.GetLastPartition(m_disk);
                    }
                    catch (IOException)
                    {
                        MessageBox.Show("Cannot access the disk", "Error");
                        return;
                    }
                    if (m_partition == null)
                    {
                        MessageBox.Show("Partition table does not contain a valid partition", "Error");
                        return;
                    }

                    try
                    {
                        m_volume = new TrueCryptVolume(m_partition, m_password);
                    }
                    catch (InvalidDataException)
                    {
                        MessageBox.Show("Invalid TrueCrypt volume or incorrect password", "Error");
                        return;
                    }
                    catch (NotSupportedException)
                    {
                        MessageBox.Show("Unsupported TrueCrypt volume format version", "Error");
                        return;
                    }

                    ListDiskDetails();
                    tabControl1.SelectedTab = tabControl1.TabPages[1];
                    btnBack.Enabled         = true;
                }
                break;
            }

            case 1:
            {
                if (!m_isSupportedFileSystem)
                {
                    MessageBox.Show("File system is not supported", "Unsupported Operation");
                }
                else if (m_volume.IsHiddenVolume)
                {
                    MessageBox.Show("Resizing an hidden volume is not supported", "Unsupported Operation");
                }
                else if (!(m_disk is VirtualHardDisk || m_disk is VirtualMachineDisk) && (m_partition is MBRPartition || m_partition is GPTPartition))
                {
                    MessageBox.Show("Resizing a volume on a raw disk image is not supported, use VHD", "Unsupported Operation");
                }
                else
                {
                    UpdateFreeSpace();
                    tabControl1.SelectedTab = tabControl1.TabPages[2];
                }
                break;
            }

            case 2:
            {
                m_additionalNumberOfBytes  = Conversion.ToInt64(txtSizeToAdd.Text, 0);
                m_additionalNumberOfBytes *= 1024 * 1024;
                if (radioGB.Checked)
                {
                    m_additionalNumberOfBytes *= 1024;
                }

                if (m_additionalNumberOfBytes <= 0)
                {
                    MessageBox.Show("Invalid size", "Error");
                    return;
                }

                string driveName          = Path.GetPathRoot(txtVolumeFile.Text);
                long   availableFreeSpace = (long)Win32Utils.GetFreeDiskSpace(driveName);

                if (availableFreeSpace < m_additionalNumberOfBytes)
                {
                    MessageBox.Show("Not enough free space", "Error");
                    return;
                }

                tabControl1.SelectedTab = tabControl1.TabPages[3];
                btnNext.Text            = "Resize";
                break;
            }

            case 3:
            {
                // We'll check free disk space again just to be sure
                string driveName          = Path.GetPathRoot(txtVolumeFile.Text);
                long   availableFreeSpace = (long)Win32Utils.GetFreeDiskSpace(driveName);

                if (availableFreeSpace < m_additionalNumberOfBytes)
                {
                    MessageBox.Show("Not enough free space", "Error");
                    return;
                }

                btnBack.Enabled = false;
                btnNext.Enabled = false;
                chkFillWithRandomData.Enabled = false;
                ResizeVolume();
                break;
            }

            case 4:
            {
                this.Close();
                break;
            }
            }
        }
Example #5
0
        private static void ReadConfig()
        {
            if (!File.Exists(CONFIG_FILE))
            {
                Console.WriteLine($"{CONFIG_FILE} not found, starting in interactive mode");
                return;
            }

            var config = JsonConvert.DeserializeObject <Config>(File.ReadAllText(CONFIG_FILE));

            if (config.Targets == null || config.Targets.Length == 0)
            {
                Console.WriteLine($"No targets configured, starting in interactive mode");
                return;
            }

            var targets = new List <ISCSITarget>();

            foreach (var targetConfig in config.Targets)
            {
                Console.WriteLine("Adding target '{0}'", targetConfig.Name);
                var disks = new List <Disk>();

                foreach (var diskConfig in targetConfig.Disks)
                {
                    Disk disk;
                    var  parameters = diskConfig.Parameters as JObject;
                    switch (diskConfig.Kind)
                    {
                    case DiskKind.Raw:
                        Console.WriteLine("  Adding Raw disk image '{0}'. Filename: '{1}'",
                                          diskConfig.Name, parameters["File"].Value <string>());
                        disk = DiskImage.GetDiskImage(parameters["File"].Value <string>());
                        break;

                    case DiskKind.External:
                        Console.WriteLine("  Adding External disk '{0}'", diskConfig.Name);
                        var type    = Type.GetType(parameters["Type"].Value <string>());
                        var extDisk = Activator.CreateInstance(type) as ExternalDisk;
                        extDisk.SetParameters(parameters);
                        disk = extDisk;
                        break;

                    default:
                        throw new NotImplementedException();
                    }
                    if (disk is DiskImage)
                    {
                        bool isLocked = ((DiskImage)disk).ExclusiveLock();
                        if (!isLocked)
                        {
                            Console.WriteLine("Error: Cannot lock the disk image for exclusive access");
                            return;
                        }
                    }
                    disks.Add(disk);
                }
                targets.Add(new ISCSITarget(targetConfig.Name, disks));
            }
            m_server                 = new ISCSIServer(targets, config.Port, config.Logging?.File);
            ISCSIServer.LogLevel     = config.Logging.Level;
            ISCSIServer.LogToConsole = config.Logging.LogToConsole;
            m_server.Start();
            Console.WriteLine("Server started, listening on port {0}", config.Port);
        }