Exemplo n.º 1
0
        private static bool GetString(PwEntry pe, string strName, SprContext ctx,
                                      bool bTrim, out string strValue)
        {
            if ((pe == null) || (strName == null))
            {
                Debug.Assert(false);
                strValue = string.Empty;
                return(false);
            }

            string str = pe.Strings.ReadSafe(strName);

            if (ctx != null)
            {
                str = SprEngine.Compile(str, ctx);
            }
            if (bTrim)
            {
                str = str.Trim();
            }

            strValue = str;
            return(str.Length != 0);
        }
Exemplo n.º 2
0
        private static List <AutoExecItem> GetAutoExecItems(PwDatabase pd)
        {
            List <AutoExecItem> l = new List <AutoExecItem>();

            if (pd == null)
            {
                Debug.Assert(false); return(l);
            }
            if (!pd.IsOpen)
            {
                return(l);
            }

            PwGroup pgRoot = pd.RootGroup;

            if (pgRoot == null)
            {
                Debug.Assert(false); return(l);
            }

            List <PwEntry> lAutoEntries = new List <PwEntry>();

            AddAutoExecEntries(lAutoEntries, pgRoot);

            long lPriStd = 0;

            foreach (PwEntry pe in lAutoEntries)
            {
                string str = pe.Strings.ReadSafe(PwDefs.UrlField);
                if (str.Length == 0)
                {
                    continue;
                }

                AutoExecItem a = new AutoExecItem(pe, pd);
                l.Add(a);

                SprContext ctx = new SprContext(pe, pd, SprCompileFlags.All);

                if (pe.Expires && (pe.ExpiryTime <= DateTime.UtcNow))
                {
                    a.Enabled = false;
                }

                bool?ob = GetBoolEx(pe, "Enabled", ctx);
                if (ob.HasValue)
                {
                    a.Enabled = ob.Value;
                }

                ob = GetBoolEx(pe, "Visible", ctx);
                if (ob.HasValue)
                {
                    a.Visible = ob.Value;
                }

                long lItemPri = lPriStd;
                if (GetString(pe, "Priority", ctx, true, out str))
                {
                    long.TryParse(str, out lItemPri);
                }
                a.Priority = lItemPri;

                ++lPriStd;
            }

            l.Sort(KeeAutoExecExt.PrioritySort);
            return(l);
        }
Exemplo n.º 3
0
        private void AutoOpenEntryPriv(AutoExecItem a, bool bManual)
        {
            PwEntry    pe        = a.Entry;
            PwDatabase pdContext = a.Database;

            SprContext ctxNoEsc = new SprContext(pe, pdContext, SprCompileFlags.All);
            SprContext ctxEsc   = new SprContext(pe, pdContext, SprCompileFlags.All,
                                                 false, true);

            string strDb;

            if (!GetString(pe, PwDefs.UrlField, ctxEsc, true, out strDb))
            {
                return;
            }

            IOConnectionInfo ioc = IOConnectionInfo.FromPath(strDb);

            //TODO

            /*if (ioc.IsLocalFile() && !UrlUtil.IsAbsolutePath(strDb))
             *  ioc = IOConnectionInfo.FromPath(UrlUtil.MakeAbsolutePath(
             *      WinUtil.GetExecutable(), strDb));*/
            if (ioc.Path.Length == 0)
            {
                return;
            }

            string strIocUserName;

            if (GetString(pe, "IocUserName", ctxNoEsc, true, out strIocUserName))
            {
                ioc.UserName = strIocUserName;
            }

            string strIocPassword;

            if (GetString(pe, "IocPassword", ctxNoEsc, true, out strIocPassword))
            {
                ioc.Password = strIocPassword;
            }

            if ((strIocUserName.Length != 0) && (strIocPassword.Length != 0))
            {
                ioc.IsComplete = true;
            }

            string str;

            if (GetString(pe, "IocTimeout", ctxNoEsc, true, out str))
            {
                long l;
                if (long.TryParse(str, out l))
                {
                    ioc.Properties.SetLong(IocKnownProperties.Timeout, l);
                }
            }

            bool?ob = GetBoolEx(pe, "IocPreAuth", ctxNoEsc);

            if (ob.HasValue)
            {
                ioc.Properties.SetBool(IocKnownProperties.PreAuth, ob.Value);
            }

            if (GetString(pe, "IocUserAgent", ctxNoEsc, true, out str))
            {
                ioc.Properties.Set(IocKnownProperties.UserAgent, str);
            }

            ob = GetBoolEx(pe, "IocExpect100Continue", ctxNoEsc);
            if (ob.HasValue)
            {
                ioc.Properties.SetBool(IocKnownProperties.Expect100Continue, ob.Value);
            }

            ob = GetBoolEx(pe, "IocPassive", ctxNoEsc);
            if (ob.HasValue)
            {
                ioc.Properties.SetBool(IocKnownProperties.Passive, ob.Value);
            }

            ob = GetBoolEx(pe, "SkipIfNotExists", ctxNoEsc);
            if (!ob.HasValue) // Backw. compat.
            {
                ob = GetBoolEx(pe, "Skip if not exists", ctxNoEsc);
            }
            if (ob.HasValue && ob.Value)
            {
                if (!IOConnection.FileExists(ioc))
                {
                    return;
                }
            }

            CompositeKey ck = new CompositeKey();

            if (GetString(pe, PwDefs.PasswordField, ctxNoEsc, false, out str))
            {
                ck.AddUserKey(new KcpPassword(str));
            }

            if (GetString(pe, PwDefs.UserNameField, ctxNoEsc, false, out str))
            {
                string           strAbs = str;
                IOConnectionInfo iocKey = IOConnectionInfo.FromPath(strAbs);
                if (iocKey.IsLocalFile() && !UrlUtil.IsAbsolutePath(strAbs))
                {
                    //TODO
                    /*      strAbs = UrlUtil.MakeAbsolutePath(WinUtil.GetExecutable(), strAbs);*/
                }


                ob = GetBoolEx(pe, "SkipIfKeyFileNotExists", ctxNoEsc);
                if (ob.HasValue && ob.Value)
                {
                    IOConnectionInfo iocKeyAbs = IOConnectionInfo.FromPath(strAbs);
                    if (!IOConnection.FileExists(iocKeyAbs))
                    {
                        return;
                    }
                }

                try { ck.AddUserKey(new KcpKeyFile(strAbs)); }
                catch (InvalidOperationException)
                {
                    //TODO
                    throw new Exception("TODO");
                    //throw new Exception(strAbs + MessageService.NewParagraph + KPRes.KeyFileError);
                }
                catch (Exception) { throw; }
            }
            else // Try getting key file from attachments
            {
                ProtectedBinary pBin = pe.Binaries.Get("KeyFile.bin");
                if (pBin != null)
                {
                    ck.AddUserKey(new KcpKeyFile(IOConnectionInfo.FromPath(
                                                     StrUtil.DataToDataUri(pBin.ReadData(), null))));
                }
            }

            if (GetString(pe, "KeyProvider", ctxNoEsc, true, out str))
            {
                /*TODO KeyProvider kp = m_host.KeyProviderPool.Get(str);
                 * if (kp == null)
                 *  throw new Exception(@"Unknown key provider: '" + str + @"'!");
                 *
                 * KeyProviderQueryContext ctxKP = new KeyProviderQueryContext(
                 *  ioc, false, false);
                 *
                 * bool bPerformHash = !kp.DirectKey;
                 * byte[] pbProvKey = kp.GetKey(ctxKP);
                 * if ((pbProvKey != null) && (pbProvKey.Length != 0))
                 * {
                 *  ck.AddUserKey(new KcpCustomKey(str, pbProvKey, bPerformHash));
                 *  MemUtil.ZeroByteArray(pbProvKey);
                 * }
                 * else return; // Provider has shown error message*/
                throw new Exception("KeyProvider not supported");
            }

            ob = GetBoolEx(pe, "UserAccount", ctxNoEsc);
            if (ob.HasValue && ob.Value)
            {
                ck.AddUserKey(new KcpUserAccount());
            }

            if (ck.UserKeyCount == 0)
            {
                return;
            }

            GetString(pe, "Focus", ctxNoEsc, true, out str);
            bool bRestoreFocus = str.Equals("Restore", StrUtil.CaseIgnoreCmp);

            /*TODO
             * PwDatabase pdPrev = m_host.MainWindow.ActiveDatabase;
             *
             * m_host.MainWindow.OpenDatabase(ioc, ck, true);
             *
             * if (bRestoreFocus && (pdPrev != null) && !bManual)
             * {
             *  PwDocument docPrev = m_host.MainWindow.DocumentManager.FindDocument(
             *      pdPrev);
             *  if (docPrev != null) m_host.MainWindow.MakeDocumentActive(docPrev);
             *  else { Debug.Assert(false); }
             * }*/
        }
Exemplo n.º 4
0
        public static bool TryGetDatabaseIoc(AutoExecItem a, out IOConnectionInfo ioc)
        {
            PwEntry    pe        = a.Entry;
            PwDatabase pdContext = a.Database;

            SprContext ctxNoEsc = new SprContext(pe, pdContext, SprCompileFlags.All);
            SprContext ctxEsc   = new SprContext(pe, pdContext, SprCompileFlags.All,
                                                 false, true);

            ioc = null;

            string strDb;

            if (!GetString(pe, PwDefs.UrlField, ctxEsc, true, out strDb))
            {
                return(false);
            }

            ioc = IOConnectionInfo.FromPath(strDb);
            //TODO

            /*if (ioc.IsLocalFile() && !UrlUtil.IsAbsolutePath(strDb))
             *  ioc = IOConnectionInfo.FromPath(UrlUtil.MakeAbsolutePath(
             *      WinUtil.GetExecutable(), strDb));*/
            if (ioc.Path.Length == 0)
            {
                return(false);
            }

            string strIocUserName;

            if (GetString(pe, "IocUserName", ctxNoEsc, true, out strIocUserName))
            {
                ioc.UserName = strIocUserName;
            }

            string strIocPassword;

            if (GetString(pe, "IocPassword", ctxNoEsc, true, out strIocPassword))
            {
                ioc.Password = strIocPassword;
            }

            if ((strIocUserName.Length != 0) && (strIocPassword.Length != 0))
            {
                ioc.IsComplete = true;
            }

            string str;

            if (GetString(pe, "IocTimeout", ctxNoEsc, true, out str))
            {
                long l;
                if (long.TryParse(str, out l))
                {
                    ioc.Properties.SetLong(IocKnownProperties.Timeout, l);
                }
            }

            bool?ob = GetBoolEx(pe, "IocPreAuth", ctxNoEsc);

            if (ob.HasValue)
            {
                ioc.Properties.SetBool(IocKnownProperties.PreAuth, ob.Value);
            }

            if (GetString(pe, "IocUserAgent", ctxNoEsc, true, out str))
            {
                ioc.Properties.Set(IocKnownProperties.UserAgent, str);
            }

            ob = GetBoolEx(pe, "IocExpect100Continue", ctxNoEsc);
            if (ob.HasValue)
            {
                ioc.Properties.SetBool(IocKnownProperties.Expect100Continue, ob.Value);
            }

            ob = GetBoolEx(pe, "IocPassive", ctxNoEsc);
            if (ob.HasValue)
            {
                ioc.Properties.SetBool(IocKnownProperties.Passive, ob.Value);
            }
            return(true);
        }
Exemplo n.º 5
0
        public static bool AutoOpenEntry(Activity activity, AutoExecItem item, bool bManual,
                                         ActivityLaunchMode launchMode)
        {
            string           str;
            PwEntry          pe       = item.Entry;
            SprContext       ctxNoEsc = new SprContext(pe, item.Database, SprCompileFlags.All);
            IOConnectionInfo ioc;

            if (!TryGetDatabaseIoc(item, out ioc))
            {
                return(false);
            }

            var ob = GetBoolEx(pe, "SkipIfNotExists", ctxNoEsc);

            if (!ob.HasValue) // Backw. compat.
            {
                ob = GetBoolEx(pe, "Skip if not exists", ctxNoEsc);
            }
            if (ob.HasValue && ob.Value)
            {
                if (!CheckFileExsts(ioc))
                {
                    return(false);
                }
            }

            CompositeKey ck = new CompositeKey();

            if (GetString(pe, PwDefs.PasswordField, ctxNoEsc, false, out str))
            {
                ck.AddUserKey(new KcpPassword(str));
            }

            if (GetString(pe, PwDefs.UserNameField, ctxNoEsc, false, out str))
            {
                string           strAbs = str;
                IOConnectionInfo iocKey = IOConnectionInfo.FromPath(strAbs);
                if (iocKey.IsLocalFile() && !UrlUtil.IsAbsolutePath(strAbs))
                {
                    //local relative paths not supported on Android
                    return(false);
                }


                ob = GetBoolEx(pe, "SkipIfKeyFileNotExists", ctxNoEsc);
                if (ob.HasValue && ob.Value)
                {
                    IOConnectionInfo iocKeyAbs = IOConnectionInfo.FromPath(strAbs);
                    if (!CheckFileExsts(iocKeyAbs))
                    {
                        return(false);
                    }
                }

                try { ck.AddUserKey(new KcpKeyFile(strAbs)); }
                catch (InvalidOperationException)
                {
                    Toast.MakeText(Application.Context, Resource.String.error_adding_keyfile, ToastLength.Long).Show();
                    return(false);
                }
                catch (Exception) { throw; }
            }
            else // Try getting key file from attachments
            {
                ProtectedBinary pBin = pe.Binaries.Get("KeyFile.bin");
                if (pBin != null)
                {
                    ck.AddUserKey(new KcpKeyFile(IOConnectionInfo.FromPath(
                                                     StrUtil.DataToDataUri(pBin.ReadData(), null))));
                }
            }

            GetString(pe, "Focus", ctxNoEsc, true, out str);
            bool bRestoreFocus = str.Equals("Restore", StrUtil.CaseIgnoreCmp);

            PasswordActivity.Launch(activity, ioc, ck, launchMode, !
                                    bRestoreFocus);

            App.Kp2a.RegisterChildDatabase(ioc);

            return(true);
        }
Exemplo n.º 6
0
            public override View GetView(int position, View convertView, ViewGroup parent)
            {
                if (cursorInflater == null)
                {
                    cursorInflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
                }

                View view;

                if (convertView == null)
                {
                    // if it's not recycled, initialize some attributes

                    view = cursorInflater.Inflate(Resource.Layout.child_db_config_row, parent, false);


                    view.FindViewById <Button>(Resource.Id.child_db_enable_on_this_device).Click += (sender, args) =>
                    {
                        View sending_view = (View)sender;
                        _context.OnEnable(_displayedChildDatabases[GetClickedPos(sending_view)]);
                    };

                    view.FindViewById <Button>(Resource.Id.child_db_disable_on_this_device).Click += (sender, args) =>
                    {
                        View sending_view = (View)sender;
                        _context.OnDisable(_displayedChildDatabases[GetClickedPos(sending_view)]);
                    };


                    view.FindViewById <Button>(Resource.Id.child_db_edit).Click += (sender, args) =>
                    {
                        View sending_view = (View)sender;
                        _context.OnEdit(_displayedChildDatabases[GetClickedPos(sending_view)]);
                    };


                    view.FindViewById <Button>(Resource.Id.child_db_open).Click += (sender, args) =>
                    {
                        View sending_view = (View)sender;
                        _context.OnOpen(_displayedChildDatabases[GetClickedPos(sending_view)]);
                    };


                    view.FindViewById <Button>(Resource.Id.child_db_enable_a_copy_for_this_device).Click += (sender, args) =>
                    {
                        View sending_view = (View)sender;
                        _context.OnEnableCopy(_displayedChildDatabases[GetClickedPos(sending_view)]);
                    };
                }
                else
                {
                    view = convertView;
                }


                var iv           = view.FindViewById <ImageView>(Resource.Id.child_db_icon);
                var autoExecItem = _displayedChildDatabases[position];
                var pw           = autoExecItem.Entry;

                SprContext ctx = new SprContext(pw, App.Kp2a.FindDatabaseForElement(pw).KpDatabase, SprCompileFlags.All);

                string deviceId = KeeAutoExecExt.ThisDeviceId;

                view.FindViewById <TextView>(Resource.Id.child_db_title).Text =
                    SprEngine.Compile(pw.Strings.GetSafe(PwDefs.TitleField).ReadString(), ctx);

                view.FindViewById <TextView>(Resource.Id.child_db_url).Text =
                    _context.GetString(Resource.String.entry_url) + ": " + SprEngine.Compile(pw.Strings.GetSafe(PwDefs.UrlField).ReadString(), ctx);

                bool deviceEnabledExplict;
                bool deviceEnabled = KeeAutoExecExt.IsDeviceEnabled(autoExecItem, deviceId, out deviceEnabledExplict);

                deviceEnabled &= deviceEnabledExplict;


                if (!autoExecItem.Enabled)
                {
                    view.FindViewById <TextView>(Resource.Id.child_db_enabled_here).Text =
                        _context.GetString(Resource.String.plugin_disabled);
                }
                else
                {
                    view.FindViewById <TextView>(Resource.Id.child_db_enabled_here).Text =
                        _context.GetString(Resource.String.child_db_enabled_on_this_device) + ": " +
                        (!deviceEnabledExplict ?
                         _context.GetString(Resource.String.unspecified)
                            :
                         ((autoExecItem.Enabled && deviceEnabled)
                                ? _context.GetString(Resource.String.yes)
                                : _context.GetString(Resource.String.no)));
                }

                view.FindViewById(Resource.Id.child_db_enable_on_this_device).Visibility  = !deviceEnabled && autoExecItem.Enabled ? ViewStates.Visible : ViewStates.Gone;
                view.FindViewById(Resource.Id.child_db_disable_on_this_device).Visibility = (deviceEnabled || !deviceEnabledExplict) && autoExecItem.Enabled ? ViewStates.Visible : ViewStates.Gone;
                view.FindViewById(Resource.Id.child_db_enable_a_copy_for_this_device_container).Visibility = !deviceEnabled && autoExecItem.Enabled ? ViewStates.Visible : ViewStates.Gone;
                view.FindViewById(Resource.Id.child_db_edit).Visibility = deviceEnabledExplict || !autoExecItem.Enabled  ? ViewStates.Visible : ViewStates.Gone;
                IOConnectionInfo ioc;

                if ((KeeAutoExecExt.TryGetDatabaseIoc(autoExecItem, out ioc)) && App.Kp2a.TryGetDatabase(ioc) == null)
                {
                    view.FindViewById(Resource.Id.child_db_open).Visibility = ViewStates.Visible;
                }
                else
                {
                    view.FindViewById(Resource.Id.child_db_open).Visibility = ViewStates.Gone;
                }


                Database db = App.Kp2a.FindDatabaseForElement(pw);

                bool isExpired = pw.Expires && pw.ExpiryTime < DateTime.Now;

                if (isExpired)
                {
                    db.DrawableFactory.AssignDrawableTo(iv, _context, db.KpDatabase, PwIcon.Expired, PwUuid.Zero, false);
                }
                else
                {
                    db.DrawableFactory.AssignDrawableTo(iv, _context, db.KpDatabase, pw.IconId, pw.CustomIconUuid, false);
                }



                view.Tag = position.ToString();

                return(view);
            }