Exemple #1
0
        public static byte[] ReadFile(IOConnectionInfo ioc)
        {
            Stream       sIn = null;
            MemoryStream ms  = null;

            try
            {
                sIn = IOConnection.OpenRead(ioc);
                if (sIn == null)
                {
                    return(null);
                }

                ms = new MemoryStream();
                MemUtil.CopyStream(sIn, ms);

                return(ms.ToArray());
            }
            catch (Exception) { }
            finally
            {
                if (sIn != null)
                {
                    sIn.Dispose();
                }
                if (ms != null)
                {
                    ms.Dispose();
                }
            }

            return(null);
        }
Exemple #2
0
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);

            IOConnection.ConfigureWebRequest(request);
            return(request);
        }
Exemple #3
0
        public override void Close()
        {
            base.Close();

            if (MonoWorkarounds.IsRequired(10163) && m_bWrite)
            {
                try
                {
                    Stream s = this.BaseStream;
                    Type   t = s.GetType();
                    if (t.Name == "WebConnectionStream")
                    {
                        PropertyInfo pi = t.GetProperty("Request",
                                                        BindingFlags.Instance | BindingFlags.NonPublic);
                        if (pi != null)
                        {
                            WebRequest wr = (pi.GetValue(s, null) as WebRequest);
                            if (wr != null)
                            {
                                IOConnection.DisposeResponse(wr.GetResponse(), false);
                            }
                            else
                            {
                                Debug.Assert(false);
                            }
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                    }
                }
                catch (Exception) { Debug.Assert(false); }
            }
        }
Exemple #4
0
        private void CommitWriteTransaction()
        {
            bool bMadeUnhidden = UrlUtil.UnhideFile(m_iocBase.Path);

#if (!KeePass2PCL && !KeePassLibSD && !KeePassRT)
            FileSecurity bkSecurity    = null;
            bool         bEfsEncrypted = false;
#endif

            if (IOConnection.FileExists(m_iocBase))
            {
#if (!KeePass2PCL && !KeePassLibSD && !KeePassRT)
                if (m_iocBase.IsLocalFile())
                {
                    try
                    {
                        FileAttributes faBase = File.GetAttributes(m_iocBase.Path);
                        bEfsEncrypted = ((long)(faBase & FileAttributes.Encrypted) != 0);

                        DateTime tCreation = File.GetCreationTime(m_iocBase.Path);
                        bkSecurity = File.GetAccessControl(m_iocBase.Path);

                        File.SetCreationTime(m_iocTemp.Path, tCreation);
                    }
                    catch (Exception) { Debug.Assert(false); }
                }
#endif

                IOConnection.DeleteFile(m_iocBase);
            }

            IOConnection.RenameFile(m_iocTemp, m_iocBase);

#if (!KeePass2PCL && !KeePassLibSD && !KeePassRT)
            if (m_iocBase.IsLocalFile())
            {
                try
                {
                    if (bEfsEncrypted)
                    {
                        try { File.Encrypt(m_iocBase.Path); }
                        catch (Exception) { Debug.Assert(false); }
                    }

                    if (bkSecurity != null)
                    {
                        File.SetAccessControl(m_iocBase.Path, bkSecurity);
                    }
                }
                catch (Exception) { Debug.Assert(false); }
            }
#endif

            if (bMadeUnhidden)
            {
                UrlUtil.HideFile(m_iocBase.Path, true);                           // Hide again
            }
        }
Exemple #5
0
            // Throws on error
            public static LockFileInfo Create(IOConnectionInfo iocLockFile)
            {
                LockFileInfo lfi;
                Stream       s = null;

                try
                {
                    byte[] pbID    = CryptoRandom.Instance.GetRandomBytes(16);
                    string strTime = TimeUtil.SerializeUtc(DateTime.Now);

#if (!KeePass2PCL && !KeePassLibSD && !KeePassRT)
                    lfi = new LockFileInfo(Convert.ToBase64String(pbID), strTime,
                                           Environment.UserName, Environment.MachineName,
                                           Environment.UserDomainName);
#else
                    lfi = new LockFileInfo(Convert.ToBase64String(pbID), strTime,
                                           string.Empty, string.Empty, string.Empty);
#endif

                    StringBuilder sb = new StringBuilder();
#if !KeePassLibSD
                    sb.AppendLine(LockFileHeader);
                    sb.AppendLine(lfi.ID);
                    sb.AppendLine(strTime);
                    sb.AppendLine(lfi.UserName);
                    sb.AppendLine(lfi.Machine);
                    sb.AppendLine(lfi.Domain);
#else
                    sb.Append(LockFileHeader + MessageService.NewLine);
                    sb.Append(lfi.ID + MessageService.NewLine);
                    sb.Append(strTime + MessageService.NewLine);
                    sb.Append(lfi.UserName + MessageService.NewLine);
                    sb.Append(lfi.Machine + MessageService.NewLine);
                    sb.Append(lfi.Domain + MessageService.NewLine);
#endif

                    byte[] pbFile = StrUtil.Utf8.GetBytes(sb.ToString());

                    s = IOConnection.OpenWrite(iocLockFile);
                    if (s == null)
                    {
                        throw new IOException(iocLockFile.GetDisplayName());
                    }
                    s.Write(pbFile, 0, pbFile.Length);
                }
                finally { if (s != null)
                          {
                              s.Dispose();
                          }
                }

                return(lfi);
            }
Exemple #6
0
        public Stream OpenWrite()
        {
            if (!m_bTransacted)
            {
                m_bMadeUnhidden = UrlUtil.UnhideFile(m_iocTemp.Path);
            }
            else             // m_bTransacted
            {
                try { IOConnection.DeleteFile(m_iocTemp); }
                catch (Exception) { }
            }

            return(IOConnection.OpenWrite(m_iocTemp));
        }
Exemple #7
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);
            }
        }
Exemple #8
0
        private void Dispose(bool bDisposing)
        {
            if (m_iocLockFile == null)
            {
                return;
            }

            bool bFileDeleted = false;

            for (int r = 0; r < 5; ++r)
            {
                // if(!OwnLockFile()) { bFileDeleted = true; break; }

                try
                {
                    IOConnection.DeleteFile(m_iocLockFile);
                    bFileDeleted = true;
                }
                catch (Exception) { Debug.Assert(false); }

                if (bFileDeleted)
                {
                    break;
                }

#if KeePass2PCL
                if (bDisposing)
                {
                    Task.Delay(50).Wait();
                }
#else
                if (bDisposing)
                {
                    Thread.Sleep(50);
                }
#endif
            }

            if (bDisposing && !bFileDeleted)
            {
                IOConnection.DeleteFile(m_iocLockFile);                 // Possibly with exception
            }
            m_iocLockFile = null;
        }
Exemple #9
0
            public static LockFileInfo Load(IOConnectionInfo iocLockFile)
            {
                Stream s = null;

                try
                {
                    s = IOConnection.OpenRead(iocLockFile);
                    if (s == null)
                    {
                        return(null);
                    }
                    StreamReader sr  = new StreamReader(s, StrUtil.Utf8);
                    string       str = sr.ReadToEnd();
                    sr.Dispose();
                    if (str == null)
                    {
                        Debug.Assert(false); return(null);
                    }

                    str = StrUtil.NormalizeNewLines(str, false);
                    string[] v = str.Split('\n');
                    if ((v == null) || (v.Length < 6))
                    {
                        Debug.Assert(false); return(null);
                    }

                    if (!v[0].StartsWith(LockFileHeader))
                    {
                        Debug.Assert(false); return(null);
                    }
                    return(new LockFileInfo(v[1], v[2], v[3], v[4], v[5]));
                }
                catch (FileNotFoundException) { }
                catch (Exception) { Debug.Assert(false); }
                finally { if (s != null)
                          {
                              s.Dispose();
                          }
                }

                return(null);
            }
Exemple #10
0
        /// <summary>
        /// Load a KDB file from a file.
        /// </summary>
        /// <param name="strFilePath">File to load.</param>
        /// <param name="kdbFormat">Format specifier.</param>
        /// <param name="slLogger">Status logger (optional).</param>
        public void Load(string strFilePath, KdbxFormat kdbFormat, IStatusLogger slLogger)
        {
            IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFilePath);

            Load(IOConnection.OpenRead(ioc), kdbFormat, slLogger);
        }