Пример #1
0
        private static bool NeedClipboardWorkaround()
        {
            try
            {
                string strHandle = (NativeLib.RunConsoleApp(AppXDoTool,
                                                            "getactivewindow") ?? string.Empty).Trim();
                if (strHandle.Length == 0)
                {
                    Debug.Assert(false); return(false);
                }

                // IntPtr h = new IntPtr(long.Parse(strHandle));
                long.Parse(strHandle);                 // Validate

                // Detection of own windows based on Form.Handle
                // comparisons doesn't work reliably (Mono's handles
                // are usually off by 1)
                // Predicate<IntPtr> fOwnWindow = g_fOwnWindow;
                // if(fOwnWindow != null)
                // {
                //	if(fOwnWindow(h)) return true;
                // }
                // else { Debug.Assert(false); }

                string strWmClass = (NativeLib.RunConsoleApp("xprop",
                                                             "-id " + strHandle + " WM_CLASS") ?? string.Empty);

                if (strWmClass.IndexOf("\"" + PwDefs.ResClass + "\"",
                                       StrUtil.CaseIgnoreCmp) >= 0)
                {
                    return(true);
                }
                if (strWmClass.IndexOf("\"Remmina\"",
                                       StrUtil.CaseIgnoreCmp) >= 0)
                {
                    return(true);
                }
            }
            catch (ThreadAbortException) { throw; }
            catch (Exception) { Debug.Assert(false); }

            return(false);
        }
Пример #2
0
        private static void FixClipThread()
        {
            try
            {
#if !KeePassUAP
                const string      strXSel = "xsel";
                const AppRunFlags rfW     = AppRunFlags.WaitForExit;

                string strLast = null;
                while (true)
                {
                    string str = NativeLib.RunConsoleApp(strXSel,
                                                         "--output --clipboard");
                    if (str == null)
                    {
                        return;                                 // 'xsel' not installed
                    }
                    if (str != strLast)
                    {
                        if (NeedClipboardWorkaround())
                        {
                            NativeLib.RunConsoleApp(strXSel,
                                                    "--input --clipboard", str, rfW);
                        }

                        strLast = str;
                    }

                    Thread.Sleep(250);
                }
#endif
            }
            catch (ThreadAbortException)
            {
                try { Thread.ResetAbort(); }
                catch (Exception) { Debug.Assert(false); }
            }
            catch (Exception) { Debug.Assert(false); }
            finally { m_thFixClip = null; }
        }
Пример #3
0
        /// <summary>
        /// Transform a key.
        /// </summary>
        /// <param name="pBuf256">Source and destination buffer.</param>
        /// <param name="pKey256">Key to use in the transformation.</param>
        /// <param name="uRounds">Number of transformation rounds.</param>
        /// <returns>Returns <c>true</c>, if the key was transformed successfully.</returns>
        public static bool TransformKey256(byte[] pBuf256, byte[] pKey256, UInt64 uRounds)
        {
            if (m_bAllowNative == false)
            {
                return(false);
            }

            KeyValuePair <IntPtr, IntPtr> kvp = NativeLib.PrepareArrays(pBuf256, pKey256);
            bool bResult = false;

            try
            {
                bResult = NativeLib.TransformKey(kvp.Key, kvp.Value, uRounds);
            }
            catch (Exception) { bResult = false; }

            if (bResult)
            {
                NativeLib.GetBuffers256(kvp, pBuf256, pKey256);
            }

            NativeLib.FreeArrays(kvp);
            return(bResult);
        }
Пример #4
0
        public static string MakeRelativePath(string strBaseFile, string strTargetFile)
        {
            if (strBaseFile == null)
            {
                throw new ArgumentNullException("strBasePath");
            }
            if (strTargetFile == null)
            {
                throw new ArgumentNullException("strTargetPath");
            }
            if (strBaseFile.Length == 0)
            {
                return(strTargetFile);
            }
            if (strTargetFile.Length == 0)
            {
                return(string.Empty);
            }

            // Test whether on different Windows drives
            if ((strBaseFile.Length >= 3) && (strTargetFile.Length >= 3))
            {
                if ((strBaseFile[1] == ':') && (strTargetFile[1] == ':') &&
                    (strBaseFile[2] == '\\') && (strTargetFile[2] == '\\') &&
                    (strBaseFile[0] != strTargetFile[0]))
                {
                    return(strTargetFile);
                }
            }

#if (!KeePassLibSD && !KeePassUAP)
            if (NativeLib.IsUnix())
            {
#endif
            bool bBaseUnc   = IsUncPath(strBaseFile);
            bool bTargetUnc = IsUncPath(strTargetFile);
            if ((!bBaseUnc && bTargetUnc) || (bBaseUnc && !bTargetUnc))
            {
                return(strTargetFile);
            }

            string strBase   = GetShortestAbsolutePath(strBaseFile);
            string strTarget = GetShortestAbsolutePath(strTargetFile);
            string[] vBase   = strBase.Split(m_vDirSeps);
            string[] vTarget = strTarget.Split(m_vDirSeps);

            int i = 0;
            while ((i < (vBase.Length - 1)) && (i < (vTarget.Length - 1)) &&
                   (vBase[i] == vTarget[i]))
            {
                ++i;
            }

            StringBuilder sbRel = new StringBuilder();
            for (int j = i; j < (vBase.Length - 1); ++j)
            {
                if (sbRel.Length > 0)
                {
                    sbRel.Append(UrlUtil.LocalDirSepChar);
                }
                sbRel.Append("..");
            }
            for (int k = i; k < vTarget.Length; ++k)
            {
                if (sbRel.Length > 0)
                {
                    sbRel.Append(UrlUtil.LocalDirSepChar);
                }
                sbRel.Append(vTarget[k]);
            }

            return(sbRel.ToString());

#if (!KeePassLibSD && !KeePassUAP)
        }

        try                 // Windows
        {
            const int     nMaxPath = NativeMethods.MAX_PATH * 2;
            StringBuilder sb       = new StringBuilder(nMaxPath + 2);
            if (NativeMethods.PathRelativePathTo(sb, strBaseFile, 0,
                                                 strTargetFile, 0) == false)
            {
                return(strTargetFile);
            }

            string str = sb.ToString();
            while (str.StartsWith(".\\"))
            {
                str = str.Substring(2, str.Length - 2);
            }

            return(str);
        }
        catch (Exception) { Debug.Assert(false); }
        return(strTargetFile);
#endif
        }
Пример #5
0
        public static string GetShortestAbsolutePath(string strPath)
        {
            if (strPath == null)
            {
                throw new ArgumentNullException("strPath");
            }
            if (strPath.Length == 0)
            {
                return(string.Empty);
            }

            // Path.GetFullPath is incompatible with UNC paths traversing over
            // different server shares (which are created by PathRelativePathTo);
            // we need to build the absolute path on our own...
            if (IsUncPath(strPath))
            {
                char   chSep = strPath[0];
                char[] vSep  = ((chSep == '/') ? (new char[] { '/' }) :
                                (new char[] { '\\', '/' }));

                List <string> l = new List <string>();
#if !KeePassLibSD
                string[] v = strPath.Split(vSep, StringSplitOptions.None);
#else
                string[] v = strPath.Split(vSep);
#endif
                Debug.Assert((v.Length >= 3) && (v[0].Length == 0) &&
                             (v[1].Length == 0));

                foreach (string strPart in v)
                {
                    if (strPart.Equals("."))
                    {
                        continue;
                    }
                    else if (strPart.Equals(".."))
                    {
                        if (l.Count > 0)
                        {
                            l.RemoveAt(l.Count - 1);
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                    }
                    else
                    {
                        l.Add(strPart);                      // Do not ignore zero length parts
                    }
                }

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < l.Count; ++i)
                {
                    // Don't test length of sb, might be 0 due to initial UNC seps
                    if (i > 0)
                    {
                        sb.Append(chSep);
                    }

                    sb.Append(l[i]);
                }

                return(sb.ToString());
            }

            string str;
            try { str = Path.GetFullPath(strPath); }
            catch (Exception) { Debug.Assert(false); return(strPath); }

            Debug.Assert((str.IndexOf("\\..\\") < 0) || NativeLib.IsUnix());
            foreach (char ch in UrlUtil.DirSepChars)
            {
                string strSep = new string(ch, 1);
                str = str.Replace(strSep + "." + strSep, strSep);
            }

            return(str);
        }
Пример #6
0
        private static void FixClipThread()
        {
            try
            {
#if !KeePassUAP
                const string      strXSel  = "xsel";
                const string      strXSelR = "--output --clipboard";
                const string      strXSelW = "--input --clipboard --nodetach";
                const AppRunFlags rfW      = AppRunFlags.WaitForExit;
                const int         msDelay  = 250;

                // XSel is required
                string strTest = NativeLib.RunConsoleApp(strXSel, strXSelR);
                if (strTest == null)
                {
                    return;                                 // XSel not installed
                }
                // Without XDoTool, the workaround would be applied to
                // all applications, which may corrupt the clipboard
                // when it doesn't contain simple text only;
                // https://sourceforge.net/p/keepass/bugs/1603/#a113
                strTest = (NativeLib.RunConsoleApp(AppXDoTool,
                                                   "help") ?? string.Empty).Trim();
                if (strTest.Length == 0)
                {
                    return;
                }

                Thread.Sleep(msDelay);

                string strLast = null;
                while (true)
                {
                    string str = NativeLib.RunConsoleApp(strXSel, strXSelR);
                    if (str == null)
                    {
                        Debug.Assert(false);
                    }
                    else if (str != strLast)
                    {
                        if (NeedClipboardWorkaround())
                        {
                            // Use --nodetach to prevent clipboard corruption;
                            // https://sourceforge.net/p/keepass/bugs/1603/
                            NativeLib.RunConsoleApp(strXSel, strXSelW, str, rfW);
                        }

                        strLast = str;
                    }

                    Thread.Sleep(msDelay);
                }
#endif
            }
            catch (ThreadAbortException)
            {
                try { Thread.ResetAbort(); }
                catch (Exception) { Debug.Assert(false); }
            }
            catch (Exception) { Debug.Assert(false); }
            finally { g_thFixClip = null; }
        }