Example #1
0
		public TaskReencrypt(IProtectAttachment attachment)
		{
			if (attachment == null)
                throw new ArgumentNullException();

            Attachment = attachment;
		}
 public void RemoveAttachment(IProtectAttachment attachment)
 {
     lock (_lock)
     {
         Aggregate(attachment, false);
     }
 }
	    internal void ReEncrypt(IProtectAttachment attachment)
		{
			bool cancel;
            if (_encryptionManager.ReencryptAttachments(new[] {(Attachment) attachment}, out cancel) && cancel == false)
            {
                attachment.ReEncrypt = false;
            }
		}
 public void AddAttachment(IProtectAttachment attachment)
 {
     // The lock is required, if you open an msg file with a lot of attachments AddAttachment will be called
     // and the Aggregate function is still processing the previous attachment.
     lock (_lock)
     {
         Aggregate(attachment, true);
     }
 }
Example #5
0
        public static List<string> Expand(IProtectAttachment attachment, string password, CancellationToken token, LocalCopyOfFileManager lcfm)
        {
            var files = new List<string>();
            string destination = lcfm.ManagedFolder;
            using (var fs = File.OpenRead(attachment.FileName))
            {
                var zf = new ZipFile(fs);
                if (!string.IsNullOrEmpty(password))
                {
                    zf.Password = password; // AES encrypted entries are handled automatically
                }

                int i = 0;
                foreach (ZipEntry zipEntry in zf)
                {
                    i += 1;

                    if (token.IsCancellationRequested)
                        break;

                    if (!zipEntry.IsFile)
                    {
                        continue; // Ignore directories
                    }

                    // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
                    var entryFileName = zipEntry.Name;
                    // Optionally match entrynames against a selection list here to skip as desired.
                    // The unpacked length is available in the zipEntry.Size property.

                    var fullPath = Path.Combine(Path.Combine(destination, i.ToString()) , entryFileName);
                    var directory = Path.GetDirectoryName(fullPath);
                    if (!string.IsNullOrEmpty(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }

                    var buffer = new byte[4096]; // 4K is optimum
                    using (var zipStream = zf.GetInputStream(zipEntry))
                    {
                        // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
                        // of the file, but does not waste memory.
                        // The "using" will close the stream even if an exception occurs.
                        using (FileStream streamWriter = File.Create(fullPath))
                        {
                            StreamUtils.Copy(zipStream, streamWriter, buffer);
                        }
                    }

                    files.Add(fullPath);
                }

                zf.IsStreamOwner = true; // Makes close also shut the underlying stream
                zf.Close(); // Ensure we release resources
            }
            return files;
        }
Example #6
0
        internal void RemoveAttachment(IProtectAttachment protectAttachment)
        {
            if (pnlContent.Controls.ContainsKey(protectAttachment.Id))
            {
                var control = pnlContent.Controls[protectAttachment.Id] as AttachmentRiskControl;
                if (control != null)
                {
                    cbPerformAction.CheckedChanged -= control.PerformActionCheckChanged;
                    control.MetadataTypeCheckChanged -= OnMetadataTypeCheckChanged;

                }
                pnlContent.Controls.RemoveByKey(protectAttachment.Id);
            }
        }
        public void UnPack(IProtectAttachment attachment, CancellationToken token, string password = "")
        {
            if (attachment.FileType == FileType.ZIP)
            {
                if (attachment.HasPassword && password == "")
                    return;

                UnPackZipFile(attachment, token, password);
            }
            else
            {
                UnPackMessageFile(attachment, token);
            }
        }
        public AttachmentTileControl(IProtectAttachment attachment, bool showCheckBox)
        {
            InitializeComponent();
            _attachment = attachment;
            lblName.Text = attachment.Name;
            pbShellIcon.Image = ShellIcon.GetSmallIcon(attachment.FileName).ToBitmap();

            if (showCheckBox)
            {
                cbAttachmentSelected.CheckedChanged += OnCheckedChanged;
                _attachment.PropertyChanged += OnPropertyChanged;
            }

            cbAttachmentSelected.Visible = showCheckBox;
        }
Example #9
0
        private void Execute(IProtectAttachment attachment, IContentEncryptionUi ui)
        {
            try
            {
                using (var applicationController = new ApplicationControllerWrapper())
                {
                    attachment.Status = PropertyNames.Processing;
                    if (_marshaller.AttachmentEncryption.Decrypt(attachment, ui) == 0)
                    {
                        if (attachment.FileType == FileType.PDFDocument)
                        {
                            Pdf.Security.Remove(attachment.FileName, attachment.OpenPassword);
                        }

                        if (!attachment.IsCollection)
                            attachment.Status = PropertyNames.Decrypted;

                        var action = new DiscoveryAction(_marshaller, StatusUpdate, new List<IProtectAttachment> { attachment }, attachment.IsCollection);
                        action.DiscoveryCompleted();

                        RaiseEvent(StatusUpdate, new ActionEventArgs(PropertyNames.Decrypted) { Attachment = attachment });
                    }
                    else
                    {
                        attachment.Status = PropertyNames.DecryptionCancelled;
                    }
                }
                attachment.Status = PropertyNames.IsProcessed;
            }
            catch (OperationCanceledException)
            {
                //ignore
            }
            catch (Exception e)
            {
                Logger.LogError(e);

                attachment.LastError = e;
                attachment.Status = PropertyNames.Exception;
                _encounteredException = true;
                RaiseEvent(StatusUpdate,
                           new ActionEventArgs(PropertyNames.Exception)
                           {
                               Exception = e,
                               Attachment = attachment
                           });
            }
        }
        private static bool IsContainedInCollection(IEnumerable<IProtectAttachment> attachments,
                                                    IProtectAttachment attachmentToFind)
        {
            foreach (var attachment in attachments)
            {
                if (attachment.Id == attachmentToFind.Id)
                {
                    return true;
                }

                if (attachment.Children.Any() && IsContainedInCollection(attachment.Children, attachmentToFind))
                {
                    return true;
                }
            }

            return false;
        }
Example #11
0
        public static void RePack(IProtectAttachment attachment, string password, CancellationToken token)
        {
            using (var stream = new ZipOutputStream(File.Create(attachment.FileName)))
            {
                stream.SetLevel(9); // 0-9, 9 being the highest compression

                if (!string.IsNullOrEmpty(password))
                {
                    stream.Password = password;
                }

                var buffer = new byte[4096];

                foreach (var child in attachment.Children)
                {
                    if (token.IsCancellationRequested)
                        return;

                    var entry = new ZipEntry(GetFileNamePreservingFolderStructure(child))
                                    {
                                        IsUnicodeText = true,
                                        DateTime = DateTime.Now
                                    };

                    stream.PutNextEntry(entry);

                    using (var fs = File.OpenRead(child.FileName))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0,
                            buffer.Length);

                            stream.Write(buffer, 0, sourceBytes);

                        } while (sourceBytes > 0);
                    }
                }
                stream.Finish();
                stream.Close();
            }
        }
        private void UnPackZipFile(IProtectAttachment protectAttachment, CancellationToken token, string password)
        {
            if (protectAttachment.FileType != FileType.ZIP) 
                return;

            var lcfm = new LocalCopyOfFileManager();
            var files = ZipFilePackager.Expand(protectAttachment, password, token, lcfm);
            foreach (var file in files)
            {
                if (token.IsCancellationRequested)
                    return;

                ProtectAttachment attachment;
                var fileType = FileTypeBridge.GetSupportedFileType(file);
                switch (fileType)
                {
                    case FileType.Unknown:
                        {
                            attachment = new ProtectAttachment(new SimpleFile(file, Path.GetFileName(file)), lcfm);
                            break;
                        }
                    case FileType.Email:
                    case FileType.ZIP:
                    case FileType.OutlookMessageFile:
                        {
                            attachment = new ProtectAttachment(FcsFileFactory.Create(file, Path.GetFileName(file)), lcfm);
                            UnPack(attachment, token);
                            break;
                        }
                    default:
                        {
                            attachment = new ProtectAttachment(FcsFileFactory.Create(file, Path.GetFileName(file)), lcfm);
                            break;
                        }
                }

                attachment.Directory = GetDirectory(attachment.FileName, lcfm);

                protectAttachment.Children.Add(attachment);
            }
        }
	    public int Decrypt(IProtectAttachment attachment, IContentEncryptionUi ui)
	    {
	        try
	        {
	            var at = (Attachment) attachment;
	            var result = (int) _encryptionManager.DecryptAttachment(ref at, ui);
	            if (result == 0)
	            {
	                attachment.ReEncrypt = true;
	            }
	            return result;
	        }
	        catch (Exception)
	        {
	            if (!string.IsNullOrEmpty(_encryptionManager.LastErrorText))
	            {
	                throw new Exception(_encryptionManager.LastErrorText);
	            }
                throw;
	        }
	    }
        public void SavePasswordToCache(IProtectAttachment attachment, string password)
	    {
            _encryptionManager.SavePasswordToCache((Attachment)attachment, password);
	    }
		public void UpdatePasswordCache(IProtectAttachment attachment)
		{
			_encryptionManager.UpdatePasswordCache((Attachment)attachment);
		}
Example #16
0
 public static bool UnSupported(IProtectAttachment attachment)
 {
     return !FileTypeHelper.IsSupported(attachment);
 }
Example #17
0
 public static bool SupportCleaning(IProtectAttachment attachment)
 {
     return FileTypeHelper.SupportsCleaning(attachment);
 }
Example #18
0
 public void GenerateRiskReport(IProtectAttachment attachment)
 {
     _tasks.Add(new NamedTask(() => new TaskRiskReport(attachment).Execute(_marshaller.CancellationToken), TaskNames.GenerateRiskReport));
 }
Example #19
0
        public void AddAttachment(IProtectAttachment attachment)
        {
            try
            {
                if (InvokeRequired)
                {
                    Invoke(new MethodInvoker(() => AddAttachment(attachment)));
                    return;
                }

                if (_cleanFilesControl.pnlContent.Controls.ContainsKey(attachment.Id))
                {
                    _cleanFilesControl.pnlContent.Controls.RemoveByKey(attachment.Id);
                }

                var rc = new AttachmentRiskControl(attachment, PropertyChanged) { Dock = DockStyle.Top, Name = attachment.Id };

                // Do not touch the following code, there is a race condition whereby the control's handle is not created by the
                // time the statusupdate events start firing. This ensures that it is created immediately.
                var handle = rc.Handle;
                if (handle == IntPtr.Zero)
                {
                    Logger.LogError("Attachment risk control's handle is invalid.");
                }
                rc.DecryptAttachmentClicked += OnDecryptAttachmentClicked;
                rc.ViewRiskReportClicked += (sender, args) => _presenter.ViewRiskReport(args.Attachment);
                rc.RemoveEncryptedAttachment += (sender, args) => RemoveAttachment(args.Attachment, false);

                _cleanFilesControl.AddAttachmentRiskControl(rc);

                _convertToPdfControl.AddAttachment(attachment);
            }
            catch (ObjectDisposedException e)
            {
                Logger.LogError(e);
            }
        }
Example #20
0
        internal void AddAttachment(IProtectAttachment attachment)
        {
            if (!pnlAttachments.Controls.ContainsKey(attachment.Id))
            {
                if (DynamicAccordianHelper.CanPdf(new List<IProtectAttachment>() {attachment}))
                {
                    var rc = new AttachmentTileControl(attachment, true);
                    rc.Name = attachment.Id;
                    rc.Dock = DockStyle.Top;
                    rc.Visible = true;
                    rc.IsChecked = cbPerformAction.Checked;
                    rc.AttachmentSelectionChanged += OnAttachmentSelectionChanged;
                    rc.AttachmentNameChanged += OnAttachmentNameChanged;

                    pnlAttachments.Controls.Add(rc);
                }
            }
        }
 public ProtectAttachmentEventArgs(IProtectAttachment attachment)
 {
     Attachment = attachment;
 }
Example #22
0
 internal void AddAttachment(ObservableCollection<IProtectAttachment> attachments, IProtectAttachment protectAttachment)
 {
     _tasks.Add(new NamedTask(() =>
     {
         var taskToRemove = _tasks.OfType<NamedRemoveTask>().FirstOrDefault(t => t.IsForAttachment(protectAttachment));
         if (taskToRemove == null)
         {
             attachments.Add(protectAttachment);
         }
         else
         {
             taskToRemove.Skip = true;
         }
     }, TaskNames.AddAttachment));
 }
Example #23
0
		public void DecryptAttachment(IProtectAttachment attachment, IContentEncryptionUi ui)
		{
            _tasks.Add(new NamedTask(() => new DecryptionAction(_marshaller, StatusUpdate, attachment, ui), TaskNames.DecryptAttachment));
		}
Example #24
0
 public static bool PasswordProtected(IProtectAttachment attachment)
 {
     return attachment.HasPassword;
 }
Example #25
0
 public TaskExtract(IProtectAttachment attachment)
 {
     _attachment = attachment;
 }
Example #26
0
        // ReSharper disable once UnusedParameter.Local
        private void VerifyFilesAreOfType(IProtectAttachment attachment, FileType fileType)
        {
            using (var packager = new MailAttachmentPackager())
            {
                packager.UnPack(attachment, new CancellationToken());
            }


            var flattenedList = TestUtils.Flatten(attachment.Children, new List<FileType>() { FileType.Email });

            foreach (var item in flattenedList)
            {
                Assert.IsTrue(item.FileType == fileType);
            }
        }
Example #27
0
        internal void RemoveAttachment(IProtectAttachment attachment)
        {
            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(() => RemoveAttachment(attachment)));
                return;
            }

            if (pnlAttachments.Controls.ContainsKey(attachment.Id))
            {
                pnlAttachments.Controls.RemoveByKey(attachment.Id);
            }
        }
Example #28
0
 private bool IsSendLinkHtmlFile(IProtectAttachment attachment)
 {
     return Path.GetFileNameWithoutExtension(attachment.Name) == OptionApi.GetString("SendLinkContentFileName");
 }
Example #29
0
        private void RemoveAttachment(IProtectAttachment protectAttachment, bool removeChildren)
        {
            try
            {
                if (InvokeRequired)
                {
                    Invoke(new MethodInvoker(() => RemoveAttachment(protectAttachment, removeChildren)));
                    return;
                }

                if (removeChildren && protectAttachment.Children.Any())
                {
                    foreach (var child in protectAttachment.Children)
                    {
                        RemoveAttachment(child, removeChildren);
                    }
                }

                _cleanFilesControl.RemoveAttachment(protectAttachment);

                _convertToPdfControl.RemoveAttachment(protectAttachment);
            }
            catch (ObjectDisposedException e)
            {
                Logger.LogError(e);
            }
        }
Example #30
0
        private SendLinkInfo GetSendLinkInfo(IProtectAttachment attachment, string link, long folderId)
        {
            string displayName = attachment.Name;
            string ext = Path.GetExtension(attachment.FileName);

            string proposedFileName = GetProposedFileName(displayName, ext);
            File.Copy(attachment.FileName, proposedFileName, true);

            return new SendLinkInfo { FilePath = proposedFileName, DisplayName = displayName, Link = link, FolderId = folderId };
        }