Ejemplo n.º 1
0
        public static void DeleteFile(IOConnectionInfo ioc)
        {
            if (ioc.IsLocalFile())
            {
                File.Delete(ioc.Path); return;
            }

#if !KeePassLibSD
            WebRequest req = CreateWebRequest(ioc);
            if (req != null)
            {
                if (req is HttpWebRequest)
                {
                    req.Method = "DELETE";
                }
                else if (req is FtpWebRequest)
                {
                    req.Method = WebRequestMethods.Ftp.DeleteFile;
                }
                else if (req is FileWebRequest)
                {
                    File.Delete(UrlUtil.FileUrlToPath(ioc.Path));
                    return;
                }
                else
                {
                    req.Method = WrmDeleteFile;
                }

                req.GetResponse();
            }
#endif
        }
Ejemplo n.º 2
0
        string GetLocalConfigLocation()
        {
            string strBaseDirName = PwDefs.ShortProductName;

            if (!string.IsNullOrEmpty(KeePass.App.Configuration.AppConfigSerializer.BaseName))
            {
                strBaseDirName = KeePass.App.Configuration.AppConfigSerializer.BaseName;
            }

            string strUserDir;

            try
            {
                strUserDir = Environment.GetFolderPath(
                    Environment.SpecialFolder.ApplicationData);
            }
            catch (Exception)
            {
                strUserDir = UrlUtil.GetFileDirectory(UrlUtil.FileUrlToPath(
                                                          Assembly.GetExecutingAssembly().GetName().CodeBase), true, false);
            }
            strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false);

            return(strUserDir + strBaseDirName + Path.DirectorySeparatorChar);
        }
Ejemplo n.º 3
0
        public static void DeleteFile(IOConnectionInfo ioc)
        {
            RaiseIOAccessPreEvent(ioc, IOAccessType.Delete);

            if (ioc.IsLocalFile())
            {
                File.Delete(ioc.Path); return;
            }

#if !KeePassLibSD
            WebRequest req = CreateWebRequest(ioc);
            if (req != null)
            {
                if (IsHttpWebRequest(req))
                {
                    req.Method = "DELETE";
                }
                else if (IsFtpWebRequest(req))
                {
                    req.Method = WebRequestMethods.Ftp.DeleteFile;
                }
                else if (IsFileWebRequest(req))
                {
                    File.Delete(UrlUtil.FileUrlToPath(ioc.Path));
                    return;
                }
                else
                {
                    req.Method = WrmDeleteFile;
                }

                DisposeResponse(req.GetResponse(), true);
            }
#endif
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Rename/move a file. For local file system and WebDAV, the
        /// specified file is moved, i.e. the file destination can be
        /// in a different directory/path. In contrast, for FTP the
        /// file is renamed, i.e. its destination must be in the same
        /// directory/path.
        /// </summary>
        /// <param name="iocFrom">Source file path.</param>
        /// <param name="iocTo">Target file path.</param>
        public static void RenameFile(IOConnectionInfo iocFrom, IOConnectionInfo iocTo)
        {
            RaiseIOAccessPreEvent(iocFrom, iocTo, IOAccessType.Move);

            if (iocFrom.IsLocalFile())
            {
                File.Move(iocFrom.Path, iocTo.Path); return;
            }

#if (!KeePassLibSD && !KeePassRT)
            WebRequest req = CreateWebRequest(iocFrom);
            if (req != null)
            {
                if (req is HttpWebRequest)
                {
                    req.Method = "MOVE";
                    req.Headers.Set("Destination", iocTo.Path);                     // Full URL supported
                }
                else if (req is FtpWebRequest)
                {
                    req.Method = WebRequestMethods.Ftp.Rename;
                    string strTo = UrlUtil.GetFileName(iocTo.Path);

                    // We're affected by .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
                    // Prepending "./", "%2E/" or "Dummy/../" doesn't work.

                    ((FtpWebRequest)req).RenameTo = strTo;
                }
                else if (req is FileWebRequest)
                {
                    File.Move(UrlUtil.FileUrlToPath(iocFrom.Path),
                              UrlUtil.FileUrlToPath(iocTo.Path));
                    return;
                }
                else
                {
                    req.Method = WrmMoveFile;
                    req.Headers.Set(WrhMoveFileTo, iocTo.Path);
                }

                DisposeResponse(req.GetResponse(), true);
            }
#endif

            // using(Stream sIn = IOConnection.OpenRead(iocFrom))
            // {
            //	using(Stream sOut = IOConnection.OpenWrite(iocTo))
            //	{
            //		MemUtil.CopyStream(sIn, sOut);
            //		sOut.Close();
            //	}
            //
            //	sIn.Close();
            // }
            // DeleteFile(iocFrom);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Rename/move a file. For local file system and WebDAV, the
        /// specified file is moved, i.e. the file destination can be
        /// in a different directory/path. In contrast, for FTP the
        /// file is renamed, i.e. its destination must be in the same
        /// directory/path.
        /// </summary>
        /// <param name="iocFrom">Source file path.</param>
        /// <param name="iocTo">Target file path.</param>
        public static void RenameFile(IOConnectionInfo iocFrom, IOConnectionInfo iocTo)
        {
            if (iocFrom.IsLocalFile())
            {
                File.Move(iocFrom.Path, iocTo.Path);
                return;
            }

#if !KeePassLibSD
            WebRequest req = CreateWebRequest(iocFrom);
            if (req != null)
            {
                if (req is HttpWebRequest)
                {
                    req.Method = "MOVE";
                    req.Headers.Set("Destination", iocTo.Path); // Full URL supported
                }
                else if (req is FtpWebRequest)
                {
                    req.Method = WebRequestMethods.Ftp.Rename;
                    ((FtpWebRequest)req).RenameTo = UrlUtil.GetFileName(iocTo.Path);
                }
                else if (req is FileWebRequest)
                {
                    File.Move(UrlUtil.FileUrlToPath(iocFrom.Path),
                              UrlUtil.FileUrlToPath(iocTo.Path));
                    return;
                }
                else
                {
                    req.Method = WrmMoveFile;
                    req.Headers.Set(WrhMoveFileTo, iocTo.Path);
                }

                DisposeResponse(req.GetResponse(), true);
            }
#endif

            // using(Stream sIn = IOConnection.OpenRead(iocFrom))
            // {
            //	using(Stream sOut = IOConnection.OpenWrite(iocTo))
            //	{
            //		MemUtil.CopyStream(sIn, sOut);
            //		sOut.Close();
            //	}
            //
            //	sIn.Close();
            // }
            // DeleteFile(iocFrom);
        }
Ejemplo n.º 6
0
        public static string GetExecutable()
        {
            string strExePath = null;

            try { strExePath = Assembly.GetExecutingAssembly().Location; }
            catch (Exception) { }

            if ((strExePath == null) || (strExePath.Length == 0))
            {
                strExePath = Assembly.GetExecutingAssembly().GetName().CodeBase;
                strExePath = UrlUtil.FileUrlToPath(strExePath);
            }

            return(strExePath);
        }
Ejemplo n.º 7
0
        public static string GetExecutable()
        {
            if (m_strExePath != null)
            {
                return(m_strExePath);
            }

            try { m_strExePath = Assembly.GetExecutingAssembly().Location; }
            catch (Exception) { }

            if (string.IsNullOrEmpty(m_strExePath))
            {
                m_strExePath = Assembly.GetExecutingAssembly().GetName().CodeBase;
                m_strExePath = UrlUtil.FileUrlToPath(m_strExePath);
            }

            return(m_strExePath);
        }
Ejemplo n.º 8
0
        public static void DeleteFile(IOConnectionInfo ioc)
        {
            RaiseIOAccessPreEvent(ioc, IOAccessType.Delete);

#if KeePass2PCL
            if (ioc.IsLocalFile())
            {
                var file = FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result;
                file.DeleteAsync().RunSynchronously();
                return;
            }
#else
            if (ioc.IsLocalFile())
            {
                File.Delete(ioc.Path); return;
            }
#endif

#if (!KeePass2PCL && !KeePassLibSD && !KeePassRT)
            WebRequest req = CreateWebRequest(ioc);
            if (req != null)
            {
                if (req is HttpWebRequest)
                {
                    req.Method = "DELETE";
                }
                else if (req is FtpWebRequest)
                {
                    req.Method = WebRequestMethods.Ftp.DeleteFile;
                }
                else if (req is FileWebRequest)
                {
                    File.Delete(UrlUtil.FileUrlToPath(ioc.Path));
                    return;
                }
                else
                {
                    req.Method = WrmDeleteFile;
                }

                DisposeResponse(req.GetResponse(), true);
            }
#endif
        }
Ejemplo n.º 9
0
        public static void DeleteFile(IOConnectionInfo ioc)
        {
            RaiseIOAccessPreEvent(ioc, IOAccessType.Delete);

            //in case a user entered a directory instead of a filename, make sure we're never
            //deleting their whole WebDAV/FTP content
            if (ioc.Path.EndsWith("/"))
            {
                throw new IOException("Delete file does not expect directory URIs.");
            }

            if (ioc.IsLocalFile())
            {
                File.Delete(ioc.Path); return;
            }

#if (!KeePassLibSD && !KeePassRT)
            RepeatWithDigestOnFail(ioc, (WebRequest req) => {
                if (req != null)
                {
                    if (req is HttpWebRequest)
                    {
                        req.Method = "DELETE";
                    }
                    else if (req is FtpWebRequest)
                    {
                        req.Method = WebRequestMethods.Ftp.DeleteFile;
                    }
                    else if (req is FileWebRequest)
                    {
                        File.Delete(UrlUtil.FileUrlToPath(ioc.Path));
                        return;
                    }
                    else
                    {
                        req.Method = WrmDeleteFile;
                    }

                    DisposeResponse(req.GetResponse(), true);
                }
            });
#endif
        }
Ejemplo n.º 10
0
        public static string GetExecutable()
        {
            string str = m_strExePath;

            if (str != null)
            {
                return(str);
            }

            try { str = Assembly.GetExecutingAssembly().Location; }
            catch (Exception) { }

            if (string.IsNullOrEmpty(str))
            {
                str = Assembly.GetExecutingAssembly().GetName().CodeBase;
                str = UrlUtil.FileUrlToPath(str);
            }

            m_strExePath = str;
            return(str);
        }
Ejemplo n.º 11
0
        private static void GetConfigPaths()
        {
            if (m_strGlobalConfigFile == null)
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                Debug.Assert(asm != null); if (asm == null)
                {
                    return;
                }

#if !KeePassLibSD
                string strFile = null;

                try { strFile = asm.Location; }
                catch (Exception) { }

                if (string.IsNullOrEmpty(strFile))
                {
                    strFile = UrlUtil.FileUrlToPath(asm.GetName().CodeBase);
                }
#else
                string strFile = UrlUtil.FileUrlToPath(asm.GetName().CodeBase);
#endif
                Debug.Assert(strFile != null); if (strFile == null)
                {
                    return;
                }

                if (string.IsNullOrEmpty(m_strBaseName))
                {
                    // Remove assembly extension
                    if (strFile.EndsWith(".exe", StrUtil.CaseIgnoreCmp))
                    {
                        strFile = strFile.Substring(0, strFile.Length - 4);
                    }
                    else if (strFile.EndsWith(".dll", StrUtil.CaseIgnoreCmp))
                    {
                        strFile = strFile.Substring(0, strFile.Length - 4);
                    }
                }
                else                 // Base name != null
                {
                    strFile = UrlUtil.GetFileDirectory(strFile, true, false) + m_strBaseName;
                }

                m_strGlobalConfigFile   = strFile + ".config.xml";
                m_strEnforcedConfigFile = strFile + ".config.enforced.xml";
            }

            if (m_strUserConfigFile == null)
            {
                string strBaseDirName = PwDefs.ShortProductName;
                if ((m_strBaseName != null) && (m_strBaseName.Length > 0))
                {
                    strBaseDirName = m_strBaseName;
                }

                string strUserDir;
                try
                {
                    strUserDir = Environment.GetFolderPath(
                        Environment.SpecialFolder.ApplicationData);
                }
                catch (Exception)
                {
                    strUserDir = UrlUtil.GetFileDirectory(UrlUtil.FileUrlToPath(
                                                              Assembly.GetExecutingAssembly().GetName().CodeBase), true, false);
                }
                strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false);

                string strUserDirLocal;
                try
                {
                    strUserDirLocal = Environment.GetFolderPath(
                        Environment.SpecialFolder.LocalApplicationData);
                }
                catch (Exception) { strUserDirLocal = strUserDir; }
                strUserDirLocal = UrlUtil.EnsureTerminatingSeparator(strUserDirLocal, false);

                m_strCreateDir      = strUserDir + strBaseDirName;
                m_strCreateDirLocal = strUserDirLocal + strBaseDirName;
                m_strUserConfigFile = m_strCreateDir + Path.DirectorySeparatorChar +
                                      strBaseDirName + ".config.xml";
            }

            string strLocalOvr = Program.CommandLineArgs[
                AppDefs.CommandLineOptions.ConfigPathLocal];
            if (!string.IsNullOrEmpty(strLocalOvr))
            {
                string strWD = UrlUtil.EnsureTerminatingSeparator(
                    WinUtil.GetWorkingDirectory(), false);
                m_strUserConfigFile = UrlUtil.MakeAbsolutePath(strWD +
                                                               "Sentinel.txt", strLocalOvr);
            }

            Debug.Assert(!string.IsNullOrEmpty(m_strCreateDir));
        }
Ejemplo n.º 12
0
        private static void GetConfigPaths()
        {
            if (m_strGlobalConfigFile == null)
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                Debug.Assert(asm != null); if (asm == null)
                {
                    return;
                }

#if !KeePassLibSD
                string strFile = null;

                try { strFile = asm.Location; }
                catch (Exception) { }

                if ((strFile == null) || (strFile.Length == 0))
                {
                    strFile = UrlUtil.FileUrlToPath(asm.GetName().CodeBase);
                }
#else
                string strFile = UrlUtil.FileUrlToPath(asm.GetName().CodeBase);
#endif
                Debug.Assert(strFile != null); if (strFile == null)
                {
                    return;
                }

                if ((m_strBaseName == null) || (m_strBaseName.Length == 0))
                {
                    // Remove assembly extension
                    if (strFile.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
                    {
                        strFile = strFile.Substring(0, strFile.Length - 4);
                    }
                    else if (strFile.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
                    {
                        strFile = strFile.Substring(0, strFile.Length - 4);
                    }
                }
                else                 // Base name != null
                {
                    strFile = UrlUtil.GetFileDirectory(strFile, true) + m_strBaseName;
                }

                m_strGlobalConfigFile   = strFile + ".config.xml";
                m_strEnforcedConfigFile = strFile + ".config.enforced.xml";
            }

            if (m_strUserConfigFile == null)
            {
                string strBaseDirName = PwDefs.ShortProductName;
                if ((m_strBaseName != null) && (m_strBaseName.Length > 0))
                {
                    strBaseDirName = m_strBaseName;
                }

                string strUserDir;
                try
                {
                    strUserDir = Environment.GetFolderPath(
                        Environment.SpecialFolder.ApplicationData);
                }
                catch (Exception)
                {
                    strUserDir = UrlUtil.GetFileDirectory(UrlUtil.FileUrlToPath(
                                                              Assembly.GetExecutingAssembly().GetName().CodeBase), true);
                }

                if ((!strUserDir.EndsWith(new string(Path.DirectorySeparatorChar, 1))) &&
                    (!strUserDir.EndsWith("\\")) && (!strUserDir.EndsWith("/")))
                {
                    strUserDir += new string(Path.DirectorySeparatorChar, 1);
                }

                m_strCreateDir      = strUserDir + strBaseDirName;
                m_strUserConfigFile = m_strCreateDir + Path.DirectorySeparatorChar +
                                      strBaseDirName + ".config.xml";
            }

            Debug.Assert(m_strCreateDir != null); Debug.Assert(m_strCreateDir.Length > 0);
        }
Ejemplo n.º 13
0
        private static void GetConfigPaths()
        {
            if (g_strGlobalConfigFile == null)
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                if (asm == null)
                {
                    Debug.Assert(false); return;
                }

#if !KeePassLibSD
                string strFile = null;

                try { strFile = asm.Location; }
                catch (Exception) { }

                if (string.IsNullOrEmpty(strFile))
                {
                    strFile = UrlUtil.FileUrlToPath(asm.GetName().CodeBase);
                }
#else
                string strFile = UrlUtil.FileUrlToPath(asm.GetName().CodeBase);
#endif
                if (string.IsNullOrEmpty(strFile))
                {
                    Debug.Assert(false); strFile = string.Empty;
                }

                if (!string.IsNullOrEmpty(g_strBaseName))
                {
                    strFile = UrlUtil.GetFileDirectory(strFile, true, false) + g_strBaseName;
                }
                else
                {
                    if (strFile.EndsWith(".exe", StrUtil.CaseIgnoreCmp) ||
                        strFile.EndsWith(".dll", StrUtil.CaseIgnoreCmp))
                    {
                        strFile = strFile.Substring(0, strFile.Length - 4);
                    }
                }

                g_strGlobalConfigFile   = strFile + g_strFileSuffix;
                g_strEnforcedConfigFile = strFile + g_strFileEnfSuffix;
            }

            if (g_strUserConfigFile == null)
            {
                string strBaseName = (!string.IsNullOrEmpty(g_strBaseName) ?
                                      g_strBaseName : PwDefs.ShortProductName);

                string strUserDir;
                try
                {
                    strUserDir = Environment.GetFolderPath(
                        Environment.SpecialFolder.ApplicationData);
                }
                catch (Exception)
                {
                    strUserDir = UrlUtil.GetFileDirectory(UrlUtil.FileUrlToPath(
                                                              Assembly.GetExecutingAssembly().GetName().CodeBase), true, false);
                }
                strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false);

                string strUserDirLocal;
                try
                {
                    strUserDirLocal = Environment.GetFolderPath(
                        Environment.SpecialFolder.LocalApplicationData);
                }
                catch (Exception) { strUserDirLocal = strUserDir; }
                strUserDirLocal = UrlUtil.EnsureTerminatingSeparator(strUserDirLocal, false);

                g_strAppDataDir      = strUserDir + strBaseName;
                g_strAppDataLocalDir = strUserDirLocal + strBaseName;
                g_strUserConfigFile  = UrlUtil.EnsureTerminatingSeparator(
                    g_strAppDataDir, false) + strBaseName + g_strFileSuffix;

                string strLocalOvr = Program.CommandLineArgs[
                    AppDefs.CommandLineOptions.ConfigPathLocal];
                if (!string.IsNullOrEmpty(strLocalOvr))
                {
                    string strWD = UrlUtil.EnsureTerminatingSeparator(
                        WinUtil.GetWorkingDirectory(), false);
                    g_strUserConfigFile = UrlUtil.MakeAbsolutePath(strWD +
                                                                   "Sentinel.txt", strLocalOvr);
                    // Do not change g_strAppDataDir, as it's returned from
                    // the AppDataDirectory property
                }

                Debug.Assert(!string.IsNullOrEmpty(g_strAppDataDir));
            }
        }