Пример #1
0
        public bool CreateEntry(string username, string password, string url, string submithost, string realm, string groupUuid)
        {
            string baseUrl = url;

            // index bigger than https:// <-- this slash
            if (baseUrl.LastIndexOf("/") > 9)
            {
                baseUrl = baseUrl.Substring(0, baseUrl.LastIndexOf("/") + 1);
            }

            var uri = new Uri(url);

            PwEntry entry = new PwEntry(true, true);

            entry.Strings.Set(PwDefs.TitleField, new ProtectedString(false, uri.Host));
            entry.Strings.Set(PwDefs.UserNameField, new ProtectedString(false, username));
            entry.Strings.Set(PwDefs.PasswordField, new ProtectedString(true, password));
            entry.Strings.Set(PwDefs.UrlField, new ProtectedString(true, baseUrl));

            if ((submithost != null && uri.Host != submithost) || realm != null)
            {
                var config = new EntryConfig();
                if (submithost != null)
                {
                    config.Allow.Add(submithost);
                }
                if (realm != null)
                {
                    config.Realm = realm;
                }

                var serializer = _ext.NewJsonSerializer();
                var writer     = new StringWriter();
                serializer.Serialize(writer, config);
                entry.Strings.Set(KeePassNatMsgExt.KeePassNatMsgName, new ProtectedString(false, writer.ToString()));
            }

            PwGroup group = null;

            if (!string.IsNullOrEmpty(groupUuid))
            {
                var db = _ext.GetConnectionDatabase();
                if (db.RootGroup != null)
                {
                    var uuid = new PwUuid(MemUtil.HexStringToByteArray(groupUuid));
                    group = db.RootGroup.FindGroup(uuid, true);
                }
            }

            if (group == null)
            {
                group = _ext.GetPasswordsGroup();
            }

            group.AddEntry(entry, true);
            _ext.UpdateUI(group);

            return(true);
        }
Пример #2
0
        public bool CreateEntry(string username, string password, string url, string submithost, string realm)
        {
            var root  = _host.Database.RootGroup;
            var group = root.FindCreateGroup(KeePassNatMsgExt.KeePassNatMsgGroupName, false);

            if (group == null)
            {
                group = new PwGroup(true, true, KeePassNatMsgExt.KeePassNatMsgGroupName, PwIcon.WorldComputer);
                root.AddGroup(group, true);
                _ext.UpdateUI(null);
            }

            string baseUrl = url;

            // index bigger than https:// <-- this slash
            if (baseUrl.LastIndexOf("/") > 9)
            {
                baseUrl = baseUrl.Substring(0, baseUrl.LastIndexOf("/") + 1);
            }

            var uri = new Uri(url);

            PwEntry entry = new PwEntry(true, true);

            entry.Strings.Set(PwDefs.TitleField, new ProtectedString(false, uri.Host));
            entry.Strings.Set(PwDefs.UserNameField, new ProtectedString(false, username));
            entry.Strings.Set(PwDefs.PasswordField, new ProtectedString(true, password));
            entry.Strings.Set(PwDefs.UrlField, new ProtectedString(true, baseUrl));

            if ((submithost != null && uri.Host != submithost) || realm != null)
            {
                var config = new EntryConfig();
                if (submithost != null)
                {
                    config.Allow.Add(submithost);
                }
                if (realm != null)
                {
                    config.Realm = realm;
                }

                var serializer = _ext.NewJsonSerializer();
                var writer     = new StringWriter();
                serializer.Serialize(writer, config);
                entry.Strings.Set(KeePassNatMsgExt.KeePassNatMsgName, new ProtectedString(false, writer.ToString()));
            }

            group.AddEntry(entry, true);
            _ext.UpdateUI(group);

            return(true);
        }
Пример #3
0
        internal Response GetLoginsHandler(Request req)
        {
            if (!req.TryDecrypt())
            {
                return(new ErrorResponse(req, ErrorType.CannotDecryptMessage));
            }

            var msg       = req.Message;
            var id        = msg.GetString("id");
            var url       = msg.GetString("url");
            var submitUrl = msg.GetString("submitUrl");

            Uri hostUri;
            Uri submitUri;

            if (!string.IsNullOrEmpty(url))
            {
                hostUri = new Uri(url);
            }
            else
            {
                return(new ErrorResponse(req, ErrorType.NoUrlProvided));
            }

            if (!string.IsNullOrEmpty(submitUrl))
            {
                submitUri = new Uri(submitUrl);
            }
            else
            {
                submitUri = hostUri;
            }

            var resp = req.GetResponse();

            resp.Message.Add("id", id);

            var items = FindMatchingEntries(url, submitUrl, null);

            if (items.ToList().Count > 0)
            {
                Func <PwEntry, bool> filter = delegate(PwEntry e)
                {
                    var c = _ext.GetEntryConfig(e);

                    var title    = e.Strings.ReadSafe(PwDefs.TitleField);
                    var entryUrl = e.Strings.ReadSafe(PwDefs.UrlField);
                    if (c != null)
                    {
                        return(title != hostUri.Host && entryUrl != hostUri.Host && !c.Allow.Contains(hostUri.Host) || (submitUri.Host != null && !c.Allow.Contains(submitUri.Host) && submitUri.Host != title && submitUri.Host != entryUrl));
                    }
                    return(title != hostUri.Host && entryUrl != hostUri.Host || (submitUri.Host != null && title != submitUri.Host && entryUrl != submitUri.Host));
                };

                var configOpt  = new ConfigOpt(_host.CustomConfig);
                var config     = _ext.GetConfigEntry(true);
                var autoAllowS = config.Strings.ReadSafe("Auto Allow");
                var autoAllow  = autoAllowS != null && autoAllowS.Trim() != "";
                autoAllow = autoAllow || configOpt.AlwaysAllowAccess;
                var needPrompting = from e in items where filter(e.entry) select e;

                if (needPrompting.ToList().Count > 0 && !autoAllow)
                {
                    var win = _host.MainWindow;

                    using (var f = new AccessControlForm())
                    {
                        win.Invoke((MethodInvoker) delegate
                        {
                            f.Icon    = win.Icon;
                            f.Plugin  = _ext;
                            f.Entries = (from e in items where filter(e.entry) select e.entry).ToList();
                            //f.Entries = needPrompting.ToList();
                            f.Host  = submitUri.Host != null ? submitUri.Host : hostUri.Host;
                            f.Load += delegate { f.Activate(); };
                            f.ShowDialog(win);
                            if (f.Remember && (f.Allowed || f.Denied))
                            {
                                foreach (var e in needPrompting)
                                {
                                    var c = _ext.GetEntryConfig(e.entry);
                                    if (c == null)
                                    {
                                        c = new EntryConfig();
                                    }
                                    var set = f.Allowed ? c.Allow : c.Deny;
                                    set.Add(hostUri.Host);
                                    if (submitUri.Host != null && submitUri.Host != hostUri.Host)
                                    {
                                        set.Add(submitUri.Host);
                                    }
                                    _ext.SetEntryConfig(e.entry, c);
                                }
                            }
                            if (!f.Allowed)
                            {
                                items = items.Except(needPrompting);
                            }
                        });
                    }
                }

                foreach (var entryDatabase in items)
                {
                    string entryUrl = String.Copy(entryDatabase.entry.Strings.ReadSafe(PwDefs.UrlField));
                    if (String.IsNullOrEmpty(entryUrl))
                    {
                        entryUrl = entryDatabase.entry.Strings.ReadSafe(PwDefs.TitleField);
                    }

                    entryUrl = entryUrl.ToLower();

                    entryDatabase.entry.UsageCount = (ulong)LevenshteinDistance(submitUrl.ToLower(), entryUrl);
                }

                var itemsList = items.ToList();

                if (configOpt.SpecificMatchingOnly)
                {
                    itemsList = (from e in itemsList
                                 orderby e.entry.UsageCount ascending
                                 select e).ToList();

                    ulong lowestDistance = itemsList.Count > 0 ?
                                           itemsList[0].entry.UsageCount :
                                           0;

                    itemsList = (from e in itemsList
                                 where e.entry.UsageCount == lowestDistance
                                 orderby e.entry.UsageCount
                                 select e).ToList();
                }

                if (configOpt.SortResultByUsername)
                {
                    var items2 = from e in itemsList orderby e.entry.UsageCount ascending, _ext.GetUserPass(e)[0] ascending select e;
                    itemsList = items2.ToList();
                }
                else
                {
                    var items2 = from e in itemsList orderby e.entry.UsageCount ascending, e.entry.Strings.ReadSafe(PwDefs.TitleField) ascending select e;
                    itemsList = items2.ToList();
                }

                var entries = new JArray(itemsList.Select(item =>
                {
                    var up        = _ext.GetUserPass(item);
                    JArray fldArr = null;
                    var fields    = GetFields(configOpt, item);
                    if (fields != null)
                    {
                        fldArr = new JArray(fields.Select(f => new JObject {
                            { f.Key, f.Value }
                        }));
                    }
                    return(new JObject {
                        { "name", item.entry.Strings.ReadSafe(PwDefs.TitleField) },
                        { "login", up[0] },
                        { "password", up[1] },
                        { "uuid", item.entry.Uuid.ToHexString() },
                        { "stringFields", fldArr }
                    });
                }));

                resp.Message.Add("count", itemsList.Count);
                resp.Message.Add("entries", entries);

                if (itemsList.Count > 0)
                {
                    var names = (from e in itemsList select e.entry.Strings.ReadSafe(PwDefs.TitleField)).Distinct();
                    var n     = String.Join("\n    ", names);

                    if (configOpt.ReceiveCredentialNotification)
                    {
                        _ext.ShowNotification(String.Format("{0}: {1} is receiving credentials for:\n    {2}", req.GetString("id"), hostUri.Host, n));
                    }
                }

                return(resp);
            }

            resp.Message.Add("count", 0);
            resp.Message.Add("entries", new JArray());

            return(resp);
        }