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); }
protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); IOConnection.ConfigureWebRequest(request, m_ioc); return(request); }
// 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.UtcNow); lfi = new LockFileInfo(Convert.ToBase64String(pbID), strTime, #if KeePassUAP EnvironmentExt.UserName, EnvironmentExt.MachineName, EnvironmentExt.UserDomainName); #elif ModernKeePassLib || KeePassLibSD string.Empty, string.Empty, string.Empty); #else Environment.UserName, Environment.MachineName, Environment.UserDomainName); #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); }
public static byte[] ReadFile(IOConnectionInfo ioc) { try { using (Stream s = IOConnection.OpenRead(ioc)) { return(MemUtil.Read(s)); } } catch (Exception) { Debug.Assert(false); } return(null); }
public bool CanProbablyAccess() { #if ModernKeePassLib if (IsLocalFile()) { return(Bytes != null); } #else return(IOConnection.FileExists(this, false)); // Raises event #endif return(true); }
public Stream OpenWrite() { if (m_iocBase == null) { Debug.Assert(false); throw new ObjectDisposedException(null); } if (!m_bTransacted) { m_bMadeUnhidden |= UrlUtil.UnhideFile(m_iocTemp.Path); } return(IOConnection.OpenWrite(m_iocTemp)); }
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); } }
public static LockFileInfo Load(IOConnectionInfo iocLockFile) { Stream s = null; try { s = IOConnection.OpenRead(iocLockFile); if (s == null) { return(null); } string str = null; using (StreamReader sr = new StreamReader(s, StrUtil.Utf8)) { str = sr.ReadToEnd(); } 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); }
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 ModernKeePassLib 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; }
private void Dispose(bool bDisposing) { m_iocBase = null; if (!bDisposing) { return; } try { foreach (IOConnectionInfo ioc in m_lToDelete) { if (IOConnection.FileExists(ioc, false)) { IOConnection.DeleteFile(ioc); } } m_lToDelete.Clear(); } catch (Exception) { Debug.Assert(false); } }
protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing && MonoWorkarounds.IsRequired(10163) && m_bWrite && !m_bDisposed) { 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); } } m_bDisposed = true; }
public void Load(string strFilePath, KdbxFormat fmt, IStatusLogger slLogger) { IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFilePath); #endif Load(IOConnection.OpenRead(ioc), fmt, slLogger); }
private void CommitWriteTransaction() { if (g_bExtraSafe) { if (!IOConnection.FileExists(m_iocTemp)) { throw new FileNotFoundException(m_iocTemp.Path + MessageService.NewLine + KLRes.FileSaveFailed); } } bool bMadeUnhidden = UrlUtil.UnhideFile(m_iocBase.Path); #if !ModernKeePassLib // 'All' includes 'Audit' (SACL), which requires SeSecurityPrivilege, // which we usually don't have and therefore get an exception; // trying to set 'Owner' or 'Group' can result in an // UnauthorizedAccessException; thus we restore 'Access' (DACL) only const AccessControlSections acs = AccessControlSections.Access; #endif bool bEfsEncrypted = false; byte[] pbSec = null; DateTime?otCreation = null; bool bBaseExists = IOConnection.FileExists(m_iocBase); if (bBaseExists && m_iocBase.IsLocalFile()) { // FileAttributes faBase = FileAttributes.Normal; try { #if !ModernKeePassLib FileAttributes faBase = File.GetAttributes(m_iocBase.Path); bEfsEncrypted = ((long)(faBase & FileAttributes.Encrypted) != 0); try { if (bEfsEncrypted) { File.Decrypt(m_iocBase.Path); } } // For TxF catch (Exception) { Debug.Assert(false); } #endif #if ModernKeePassLib otCreation = m_iocBase.StorageFile.DateCreated.UtcDateTime; #else otCreation = File.GetCreationTimeUtc(m_iocBase.Path); #endif #if !ModernKeePassLib // May throw with Mono FileSecurity sec = File.GetAccessControl(m_iocBase.Path, acs); if (sec != null) { pbSec = sec.GetSecurityDescriptorBinaryForm(); } #endif } catch (Exception) { Debug.Assert(NativeLib.IsUnix()); } // if((long)(faBase & FileAttributes.ReadOnly) != 0) // throw new UnauthorizedAccessException(); } if (!TxfMove()) { if (bBaseExists) { IOConnection.DeleteFile(m_iocBase); } IOConnection.RenameFile(m_iocTemp, m_iocBase); } else { Debug.Assert(pbSec != null); } // TxF success => NTFS => has ACL try { // If File.GetCreationTimeUtc fails, it may return a // date with year 1601, and Unix times start in 1970, // so testing for 1971 should ensure validity; // https://msdn.microsoft.com/en-us/library/system.io.file.getcreationtimeutc.aspx #if !ModernKeePassLib if (otCreation.HasValue && (otCreation.Value.Year >= 1971)) { File.SetCreationTimeUtc(m_iocBase.Path, otCreation.Value); } #endif #if !ModernKeePassLib if (bEfsEncrypted) { try { File.Encrypt(m_iocBase.Path); } catch (Exception) { Debug.Assert(false); } } // File.SetAccessControl(m_iocBase.Path, secPrev); // Directly calling File.SetAccessControl with the previous // FileSecurity object does not work; the binary form // indirection is required; // https://sourceforge.net/p/keepass/bugs/1738/ // https://msdn.microsoft.com/en-us/library/system.io.file.setaccesscontrol.aspx if ((pbSec != null) && (pbSec.Length != 0)) { FileSecurity sec = new FileSecurity(); sec.SetSecurityDescriptorBinaryForm(pbSec, acs); File.SetAccessControl(m_iocBase.Path, sec); } #endif } catch (Exception) { Debug.Assert(false); } if (bMadeUnhidden) { UrlUtil.HideFile(m_iocBase.Path, true); } }
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); } }
public void Load(StorageFile file, KdbxFormat fmt, IStatusLogger slLogger) { IOConnectionInfo ioc = IOConnectionInfo.FromFile(file); Load(IOConnection.OpenRead(ioc), fmt, slLogger); }