private bool Replace(string path2)
        {
            bool res = false;

            if (File.Exists(path2) && CompareFiles(path, path2))
            {
                string errmsg1 = ERROR_MESSAGE_DELIMITER + "Replace(" + Path.GetDirectoryName(path2) + "):";
                DigitalSignatureVerifier.WinVerifyTrustResult sigcheck = DigitalSignatureVerifier.WinVerifyTrust(path2);
                if (sigcheck == DigitalSignatureVerifier.WinVerifyTrustResult.Success)
                {
                    try
                    {
                        File.Copy(path2, path, true);
                        res = true;
                    }
                    catch (Exception ex)
                    {
                        opError.Append(errmsg1 + ex.Message);
                    }
                }
                else
                {
                    opError.Append(errmsg1 + sigcheck.ToString());
                }
            }
            return(res);
        }
        public OperationResult Check(ref DigitalSignatureVerifier.WinVerifyTrustResult sigcheck)
        {
            OperationResult res = OperationResult.NoOperation;

            opError.Clear();
            if (!string.IsNullOrEmpty(path))
            {
                if (sigcheck == DigitalSignatureVerifier.WinVerifyTrustResult.FunctionCallFailed)
                {
                    sigcheck = DigitalSignatureVerifier.WinVerifyTrust(path);
                }

                bool b1 = sigcheck == DigitalSignatureVerifier.WinVerifyTrustResult.ASN1BadTag;
                bool b2 = (File.GetAttributes(path) & FileAttributes.Compressed) == FileAttributes.Compressed;
                if (b1 || b2)
                {
                    var fs  = File.GetAccessControl(path, AccessControlSections.Owner);
                    var sid = fs.GetOwner(typeof(SecurityIdentifier));
                    if (TrustedInstallerAccountSid.Equals(sid.Value, StringComparison.OrdinalIgnoreCase))
                    {
                        res = (SetAccess(true)) ? OperationResult.AccessChanged : OperationResult.AccessFailed;
                    }
                    if (res != OperationResult.AccessFailed)
                    {
                        OperationResult res2 = OperationResult.NoOperation;
                        if (b2)
                        {
                            res2 = (Compress(false)) ? OperationResult.UncompressSuccess : OperationResult.UncompressFailed;
                        }
                        if (b1)
                        {
                            res2 = (Replace()) ? OperationResult.ReplaceSuccess : OperationResult.ReplaceFailed;
                        }
                        if ((res == OperationResult.AccessChanged) && SetAccess(false))
                        {
                            res = OperationResult.NoOperation;
                        }
                        if (res == OperationResult.NoOperation)
                        {
                            res = res2;
                            if (res != OperationResult.ReplaceFailed)
                            {
                                sigcheck = DigitalSignatureVerifier.WinVerifyTrust(path);
                                if ((sigcheck != DigitalSignatureVerifier.WinVerifyTrustResult.Success) && (sigcheck != DigitalSignatureVerifier.WinVerifyTrustResult.SubjectFormUnknown))
                                {
                                    res = OperationResult.ReplaceFailed;
                                }
                            }
                        }
                        else if ((res == OperationResult.AccessChanged) && (res2 == OperationResult.ReplaceFailed))
                        {
                            res = OperationResult.AccessChangedReplaceFailed;
                        }
                    }
                }
            }
            return(res);
        }
Пример #3
0
        private XmlElement ProcessDirectory(DirectoryInfo sDirectoryInfo, int depth)
        {
            string sName = sDirectoryInfo.FullName;// (bTopLevel) ? sDirectoryInfo.FullName : sDirectoryInfo.Name;

            if (m_bLowerCaseNames)
            {
                sName = sName.ToLower();
            }
            XmlElement xDir = m_xDocument.CreateElement(XML_ELEMENT_DIRECTORY);

            xDir.SetAttribute(XML_ELEMENT_ATTRIBUTE_NAME, sName);
            if (!workControl.NeedStop)
            {
                string     sBrowseErr = null;
                FileInfo[] fl;
                try
                {
                    fl = (string.IsNullOrEmpty(m_sSearchPattern)) ? sDirectoryInfo.GetFiles() : sDirectoryInfo.GetFiles(m_sSearchPattern);
                }
                catch (Exception e)
                {
                    fl         = null;
                    sBrowseErr = e.Message;
                }
                if (fl != null)
                {
                    xDir.SetAttribute(XML_ELEMENT_ATTRIBUTE_CREATIONTIME, (m_bTimeInTicks) ? sDirectoryInfo.CreationTimeUtc.Ticks.ToString() : sDirectoryInfo.CreationTimeUtc.ToString());
                    xDir.SetAttribute(XML_ELEMENT_ATTRIBUTE_LASTWRITETIME, (m_bTimeInTicks) ? sDirectoryInfo.LastWriteTimeUtc.Ticks.ToString() : sDirectoryInfo.LastWriteTimeUtc.ToString());
                    xDir.SetAttribute(XML_ELEMENT_ATTRIBUTE_ATTRIBUTES, sDirectoryInfo.Attributes.ToString());
                    xDir.SetAttribute(XML_ELEMENT_ATTRIBUTE_FILES, fl.Length.ToString());
                    foreach (FileInfo f in fl)
                    {
                        if (workControl.NeedStop)
                        {
                            break;
                        }
                        sName = f.Name;
                        if (m_bLowerCaseNames)
                        {
                            sName = sName.ToLower();
                        }
                        XmlElement xFile = m_xDocument.CreateElement(XML_ELEMENT_FILE);
                        xFile.SetAttribute(XML_ELEMENT_ATTRIBUTE_NAME, sName);
                        xFile.SetAttribute(XML_ELEMENT_ATTRIBUTE_CREATIONTIME, (m_bTimeInTicks) ? f.CreationTimeUtc.Ticks.ToString() : f.CreationTimeUtc.ToString());
                        xFile.SetAttribute(XML_ELEMENT_ATTRIBUTE_LASTWRITETIME, (m_bTimeInTicks) ? f.LastWriteTimeUtc.Ticks.ToString() : f.LastWriteTimeUtc.ToString());
                        xFile.SetAttribute(XML_ELEMENT_ATTRIBUTE_ATTRIBUTES, f.Attributes.ToString());
                        xFile.SetAttribute(XML_ELEMENT_ATTRIBUTE_FILELENGTH, f.Length.ToString());

                        bool bCompany = string.IsNullOrWhiteSpace(m_sExcludedCompany);
                        if (!bCompany)
                        {
                            try
                            {
                                FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(f.FullName);
                                bCompany = !m_sExcludedCompany.Equals(myFileVersionInfo.CompanyName, StringComparison.CurrentCultureIgnoreCase);
                            }
                            catch
                            {
                                bCompany = true;
                            }
                        }
                        string sfcert = "0";
                        DigitalSignatureVerifier.WinVerifyTrustResult wvtr = DigitalSignatureVerifier.WinVerifyTrustResult.FileError;
                        if (f.Length > 0)
                        {
                            X509Certificate sig = null;
                            try
                            {
                                sig = X509Certificate.CreateFromSignedFile(f.FullName);
                            }
                            catch { }
                            if (sig != null)
                            {
                                sfcert = sig.GetCertHashString();
                            }

                            wvtr = DigitalSignatureVerifier.WinVerifyTrust(f.FullName);
                        }
                        xFile.SetAttribute(XML_ELEMENT_ATTRIBUTE_FILESIG, sfcert);
                        CompressedFile.OperationResult opres = CompressedFile.OperationResult.NoOperation;
                        sfcert = opres.ToString();
                        if (m_bTryToFixCorruptedFiles)
                        {
                            var compressedFile = new CompressedFile(f.FullName, m_sDriverFolder);
                            opres  = compressedFile.Check(ref wvtr);
                            sfcert = opres.ToString();
                            string oerr = compressedFile.OperationError;
                            if (!string.IsNullOrWhiteSpace(oerr))
                            {
                                sfcert += CompressedFile.ERROR_MESSAGE_DELIMITER + oerr;
                            }
                        }
                        xFile.SetAttribute(XML_ELEMENT_ATTRIBUTE_FILESIGVERIFIED, wvtr.ToString());
                        if (opres != CompressedFile.OperationResult.NoOperation)
                        {
                            xFile.SetAttribute(XML_ELEMENT_ATTRIBUTE_OPRESULT, sfcert);
                        }
                        if ((opres != CompressedFile.OperationResult.NoOperation) || ((!m_bOnlyCorrupted || (wvtr != DigitalSignatureVerifier.WinVerifyTrustResult.Success) && (wvtr != DigitalSignatureVerifier.WinVerifyTrustResult.FileError) && (wvtr != DigitalSignatureVerifier.WinVerifyTrustResult.SubjectFormUnknown) && (wvtr != DigitalSignatureVerifier.WinVerifyTrustResult.NoSignature)) && bCompany))
                        {
                            xDir.AppendChild(xFile);
                        }
                    }
                    if (!workControl.NeedStop)
                    {
                        DirectoryInfo[] ds = sDirectoryInfo.GetDirectories();
                        int             c  = ds.Length;
                        xDir.SetAttribute(XML_ELEMENT_ATTRIBUTE_DIRECTORIES, c.ToString());
                        if ((depth != 0) && (c > 0))
                        {
                            depth--;
                            foreach (DirectoryInfo d in ds)
                            {
                                XmlElement d1 = ProcessDirectory(d, depth);
                                if (!m_bExcludeEmptyDirs || d1.HasChildNodes)
                                {
                                    xDir.AppendChild(d1);
                                }
                            }
                        }
                    }
                }
                else if (!string.IsNullOrEmpty(sBrowseErr))
                {
                    xDir.SetAttribute(XML_ELEMENT_ATTRIBUTE_ERROR, sBrowseErr);
                }
            }
            return(xDir);
        }