コード例 #1
0
        private static void RaiseIOAccessPreEvent(IOConnectionInfo ioc,
                                                  IOConnectionInfo ioc2, IOAccessType t)
        {
            if (ioc == null)
            {
                Debug.Assert(false); return;
            }
            // ioc2 may be null

            if (IOConnection.IOAccessPre != null)
            {
                IOConnectionInfo  ioc2Lcl = ((ioc2 != null) ? ioc2.CloneDeep() : null);
                IOAccessEventArgs e       = new IOAccessEventArgs(ioc.CloneDeep(), ioc2Lcl, t);
                IOConnection.IOAccessPre(null, e);
            }
        }
コード例 #2
0
ファイル: FileLock.cs プロジェクト: newlifes01/ModernKeePass
        public FileLock(IOConnectionInfo iocBaseFile)
        {
            if (iocBaseFile == null)
            {
                throw new ArgumentNullException("strBaseFile");
            }

            m_iocLockFile       = iocBaseFile.CloneDeep();
            m_iocLockFile.Path += LockFileExt;

            LockFileInfo lfiEx = LockFileInfo.Load(m_iocLockFile);

            if (lfiEx != null)
            {
                m_iocLockFile = null;                 // Otherwise Dispose deletes the existing one
                throw new FileLockException(iocBaseFile.GetDisplayName(),
                                            lfiEx.GetOwner());
            }

            LockFileInfo.Create(m_iocLockFile);
        }
コード例 #3
0
        public FileTransactionEx(IOConnectionInfo iocBaseFile, bool bTransacted)
        {
            if (iocBaseFile == null)
            {
                throw new ArgumentNullException("iocBaseFile");
            }

            m_bTransacted = bTransacted;

            m_iocBase = iocBaseFile.CloneDeep();
            if (m_iocBase.IsLocalFile())
            {
                m_iocBase.Path = UrlUtil.GetShortestAbsolutePath(m_iocBase.Path);
            }

            string strPath = m_iocBase.Path;

#if !ModernKeePassLib
            if (m_iocBase.IsLocalFile())
            {
                try
                {
                    if (File.Exists(strPath))
                    {
                        // Symbolic links are realized via reparse points;
                        // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365503.aspx
                        // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365680.aspx
                        // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365006.aspx
                        // Performing a file transaction on a symbolic link
                        // would delete/replace the symbolic link instead of
                        // writing to its target
                        FileAttributes fa = File.GetAttributes(strPath);
                        if ((long)(fa & FileAttributes.ReparsePoint) != 0)
                        {
                            m_bTransacted = false;
                        }
                    }
                    else
                    {
                        // If the base and the temporary file are in different
                        // folders and the base file doesn't exist (i.e. we can't
                        // backup the ACL), a transaction would cause the new file
                        // to have the default ACL of the temporary folder instead
                        // of the one of the base folder; therefore, we don't use
                        // a transaction when the base file doesn't exist (this
                        // also results in other applications monitoring the folder
                        // to see one file creation only)
                        m_bTransacted = false;
                    }
                }
                catch (Exception) { Debug.Assert(false); }
            }
#endif

#if !ModernKeePassLib
            // Prevent transactions for FTP URLs under .NET 4.0 in order to
            // avoid/workaround .NET bug 621450:
            // https://connect.microsoft.com/VisualStudio/feedback/details/621450/problem-renaming-file-on-ftp-server-using-ftpwebrequest-in-net-framework-4-0-vs2010-only
            if (strPath.StartsWith("ftp:", StrUtil.CaseIgnoreCmp) &&
                (Environment.Version.Major >= 4) && !NativeLib.IsUnix())
            {
                m_bTransacted = false;
            }
#endif

            foreach (KeyValuePair <string, bool> kvp in g_dEnabled)
            {
                if (strPath.StartsWith(kvp.Key, StrUtil.CaseIgnoreCmp))
                {
                    m_bTransacted = kvp.Value;
                    break;
                }
            }

            if (m_bTransacted)
            {
                m_iocTemp       = m_iocBase.CloneDeep();
                m_iocTemp.Path += StrTempSuffix;

                TxfPrepare();                 // Adjusts m_iocTemp
            }
            else
            {
                m_iocTemp = m_iocBase;
            }
        }
コード例 #4
0
        internal static void ConfigureWebRequest(WebRequest request,
                                                 IOConnectionInfo ioc)
        {
            if (request == null)
            {
                Debug.Assert(false); return;
            }                                                                // No throw

            IocProperties p = ((ioc != null) ? ioc.Properties : null);

            if (p == null)
            {
                Debug.Assert(false); p = new IocProperties();
            }

            IHasIocProperties ihpReq = (request as IHasIocProperties);

            if (ihpReq != null)
            {
                IocProperties pEx = ihpReq.IOConnectionProperties;
                if (pEx != null)
                {
                    p.CopyTo(pEx);
                }
                else
                {
                    ihpReq.IOConnectionProperties = p.CloneDeep();
                }
            }

            if (IsHttpWebRequest(request))
            {
                // WebDAV support
#if !KeePassUAP
                request.PreAuthenticate = true;                 // Also auth GET
#endif
                if (string.Equals(request.Method, WebRequestMethods.Http.Post,
                                  StrUtil.CaseIgnoreCmp))
                {
                    request.Method = WebRequestMethods.Http.Put;
                }

#if !KeePassUAP
                HttpWebRequest hwr = (request as HttpWebRequest);
                if (hwr != null)
                {
                    string strUA = p.Get(IocKnownProperties.UserAgent);
                    if (!string.IsNullOrEmpty(strUA))
                    {
                        hwr.UserAgent = strUA;
                    }
                }
                else
                {
                    Debug.Assert(false);
                }
#endif
            }
#if !KeePassUAP
            else if (IsFtpWebRequest(request))
            {
                FtpWebRequest fwr = (request as FtpWebRequest);
                if (fwr != null)
                {
                    bool?obPassive = p.GetBool(IocKnownProperties.Passive);
                    if (obPassive.HasValue)
                    {
                        fwr.UsePassive = obPassive.Value;
                    }
                }
                else
                {
                    Debug.Assert(false);
                }
            }
#endif

#if !KeePassUAP
            // Not implemented and ignored in Mono < 2.10
            try
            {
                request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
            }
            catch (NotImplementedException) { }
            catch (Exception) { Debug.Assert(false); }
#endif

            try
            {
                IWebProxy prx;
                if (GetWebProxy(out prx))
                {
                    request.Proxy = prx;
                }
            }
            catch (Exception) { Debug.Assert(false); }

#if !KeePassUAP
            long?olTimeout = p.GetLong(IocKnownProperties.Timeout);
            if (olTimeout.HasValue && (olTimeout.Value >= 0))
            {
                request.Timeout = (int)Math.Min(olTimeout.Value, (long)int.MaxValue);
            }

            bool?ob = p.GetBool(IocKnownProperties.PreAuth);
            if (ob.HasValue)
            {
                request.PreAuthenticate = ob.Value;
            }
#endif

            if (IOConnection.IOWebRequestPre != null)
            {
                IOWebRequestEventArgs e = new IOWebRequestEventArgs(request,
                                                                    ((ioc != null) ? ioc.CloneDeep() : null));
                IOConnection.IOWebRequestPre(null, e);
            }
        }