コード例 #1
0
        /// <summary>
        /// Finds all entries with a given group and tag (or multiple)
        /// </summary>
        /// <param name="sourceDb">Database to search for the entries.</param>
        /// <param name="groups">Groups to search for (multiple separated by ,).</param>
        /// <param name="tags">Tag to search for (multiple separated by ,).</param>
        /// <returns>A PwObjectList with all metching entries.</returns>
        private static PwObjectList <PwEntry> FindEntriesByGroupAndTag(PwDatabase sourceDb, string groups, string tags)
        {
            PwObjectList <PwEntry> entries = new PwObjectList <PwEntry>();

            // Tag and group export
            foreach (string group in groups.Split(',').Select(x => x.Trim()))
            {
                PwGroup groupToExport = sourceDb.RootGroup.GetFlatGroupList().FirstOrDefault(g => g.Name == group);

                if (groupToExport == null)
                {
                    throw new ArgumentException("No group with the name of the Group-Setting found.");
                }

                foreach (string tag in tags.Split(',').Select(x => x.Trim()))
                {
                    PwObjectList <PwEntry> tagEntries = new PwObjectList <PwEntry>();
                    groupToExport.FindEntriesByTag(tag, tagEntries, true);
                    // Prevent duplicated entries
                    IEnumerable <PwUuid> existingUuids = entries.Select(x => x.Uuid);
                    List <PwEntry>       entriesToAdd  = tagEntries.Where(x => !existingUuids.Contains(x.Uuid)).ToList();
                    entries.Add(entriesToAdd);
                }
            }

            return(entries);
        }
コード例 #2
0
        public async void CheckAll()
        {
            bulkCheck = true;

            var progressDisplay = new ProgressDisplay();

            progressDisplay.Show();

            var allEntries = new PwObjectList <PwEntry>();

            Host.Database.RootGroup.SearchEntries(SearchParameters.None, allEntries);

            var entries = allEntries.Select(x =>
                                            new { password = x, hash = GetPasswordHash(x) });
            int index = 0;

            foreach (var entry in entries)
            {
                await Task.Run(() => PasswordCheckWorker(entry.password, entry.hash));;
                TouchEntry(entry.password);

                progressDisplay.progressBar.Value = (index++) * 100 / allEntries.Count();

                if (progressDisplay.UserTerminated)
                {
                    progressDisplay.Close();
                    break;
                }
            }

            UpdateUI();

            progressDisplay.Close();
        }
コード例 #3
0
        private static void SelectEntriesAndDelete(PwObjectList <PwEntry> masterList, PwObjectList <PwEntry> slaveList, PwDatabase targetDb)
        {
            //Find entries in slaveList not in masterList to delete
            IEnumerable <PwUuid> masterUuid = masterList.Select(u => u.Uuid);
            var toDelete = slaveList.Where(e => !masterUuid.Contains(e.Uuid)).ToList();

            DeleteEntries(toDelete, targetDb);
        }
コード例 #4
0
        public IEnumerable <EntryEntity> Search(string groupId, string text)
        {
            var pwGroup       = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(groupId), true);
            var searchResults = new PwObjectList <PwEntry>();

            pwGroup.SearchEntries(new SearchParameters
            {
                ComparisonMode = StringComparison.OrdinalIgnoreCase,
                SearchInTitles = true,
                //SearchInUserNames = true,
                SearchString = text
            }, searchResults);
            return(searchResults.Select(e => _mapper.Map <EntryEntity>(e)));
        }
コード例 #5
0
        /// <summary>
        /// Finds all entries with a given tag (or multiple)
        /// </summary>
        /// <param name="sourceDb">Database to search for the entries.</param>
        /// <param name="tags">Tag to search for (multiple separated by ,).</param>
        /// <returns>A PwObjectList with all metching entries.</returns>
        private static PwObjectList <PwEntry> FindEntriesByTag(PwDatabase sourceDb, string tags)
        {
            PwObjectList <PwEntry> entries = new PwObjectList <PwEntry>();

            foreach (string tag in tags.Split(',').Select(x => x.Trim()))
            {
                PwObjectList <PwEntry> tagEntries = new PwObjectList <PwEntry>();
                sourceDb.RootGroup.FindEntriesByTag(tag, tagEntries, true);
                // Prevent duplicated entries
                IEnumerable <PwUuid> existingUuids = entries.Select(x => x.Uuid);
                List <PwEntry>       entriesToAdd  = tagEntries.Where(x => !existingUuids.Contains(x.Uuid)).ToList();
                entries.Add(entriesToAdd);
            }

            return(entries);
        }
コード例 #6
0
        private static void CopyEntriesAndGroups(PwDatabase sourceDb, Settings settings, PwObjectList <PwEntry> entries,
                                                 PwDatabase targetDatabase)
        {
            //If OverrideEntireGroup is set to true
            if (!settings.OverrideTargetDatabase && !settings.FlatExport &&
                settings.OverrideEntireGroup && !string.IsNullOrEmpty(settings.Group))
            {
                //Delete every entry in target database' groups to override them
                IEnumerable <PwGroup> groupsToDelete = entries.Select(x => x.ParentGroup).Distinct();
                DeleteTargetGroupsInDatabase(groupsToDelete, targetDatabase);
            }

            foreach (PwEntry entry in entries)
            {
                // Get or create the target group in the target database (including hierarchy)
                PwGroup targetGroup = settings.FlatExport
                    ? targetDatabase.RootGroup
                    : CreateTargetGroupInDatebase(entry, targetDatabase, sourceDb);

                PwEntry peNew = null;
                if (!settings.OverrideTargetDatabase)
                {
                    peNew = targetGroup.FindEntry(entry.Uuid, bSearchRecursive: false);

                    // Check if the target entry is newer than the source entry
                    if (settings.OverrideEntryOnlyNewer && peNew != null &&
                        peNew.LastModificationTime > entry.LastModificationTime)
                    {
                        // Yes -> skip this entry
                        continue;
                    }
                }

                // Was no existing entry in the target database found?
                if (peNew == null)
                {
                    // Create a new entry
                    peNew      = new PwEntry(false, false);
                    peNew.Uuid = entry.Uuid;

                    // Add entry to the target group in the new database
                    targetGroup.AddEntry(peNew, true);
                }

                // Clone entry properties if ExportUserAndPassOnly is false
                if (!settings.ExportUserAndPassOnly)
                {
                    peNew.AssignProperties(entry, false, true, true);
                    peNew.Strings.Set(PwDefs.UrlField,
                                      FieldHelper.GetFieldWRef(entry, sourceDb, PwDefs.UrlField));
                    peNew.Strings.Set(PwDefs.NotesField,
                                      FieldHelper.GetFieldWRef(entry, sourceDb, PwDefs.NotesField));
                }

                // Copy/override some supported fields with ref resolving values
                peNew.Strings.Set(PwDefs.TitleField,
                                  FieldHelper.GetFieldWRef(entry, sourceDb, PwDefs.TitleField));
                peNew.Strings.Set(PwDefs.UserNameField,
                                  FieldHelper.GetFieldWRef(entry, sourceDb, PwDefs.UserNameField));
                peNew.Strings.Set(PwDefs.PasswordField,
                                  FieldHelper.GetFieldWRef(entry, sourceDb, PwDefs.PasswordField));

                // Handle custom icon
                HandleCustomIcon(targetDatabase, sourceDb, entry);
            }
        }
コード例 #7
0
        private static PwObjectList <PwEntry> GetMatching(PwDatabase sourceDb, Settings settings)
        {
            PwObjectList <PwEntry> entries = new PwObjectList <PwEntry>();

            if (!string.IsNullOrEmpty(settings.Tag) && string.IsNullOrEmpty(settings.Group))
            {
                // Tag only export
                // Support multiple tag (Tag1,Tag2)
                foreach (string tag in settings.Tag.Split(','))
                {
                    PwObjectList <PwEntry> tagEntries = new PwObjectList <PwEntry>();
                    sourceDb.RootGroup.FindEntriesByTag(tag, tagEntries, true);
                    // Prevent duplicated entries
                    IEnumerable <PwUuid> existingUuids = entries.Select(x => x.Uuid);
                    List <PwEntry>       entriesToAdd  = tagEntries.Where(x => !existingUuids.Contains(x.Uuid)).ToList();
                    entries.Add(entriesToAdd);
                }
            }
            else if (string.IsNullOrEmpty(settings.Tag) && !string.IsNullOrEmpty(settings.Group))
            {
                // Support multiple group (Group1,Group2)
                foreach (string group in settings.Group.Split(','))
                {
                    // Tag and group export
                    PwGroup groupToExport = sourceDb.RootGroup.GetFlatGroupList().FirstOrDefault(g => g.Name == group);

                    if (groupToExport == null)
                    {
                        throw new ArgumentException("No group with the name of the Group-Setting found.");
                    }

                    PwObjectList <PwEntry> groupEntries = groupToExport.GetEntries(true);
                    // Prevent duplicated entries
                    IEnumerable <PwUuid> existingUuids = entries.Select(x => x.Uuid);
                    List <PwEntry>       entriesToAdd  = groupEntries.Where(x => !existingUuids.Contains(x.Uuid)).ToList();
                    entries.Add(entriesToAdd);
                }
            }
            else if (!string.IsNullOrEmpty(settings.Tag) && !string.IsNullOrEmpty(settings.Group))
            {
                // Tag and group export
                foreach (string group in settings.Group.Split(','))
                {
                    PwGroup groupToExport = sourceDb.RootGroup.GetFlatGroupList().FirstOrDefault(g => g.Name == group);

                    if (groupToExport == null)
                    {
                        throw new ArgumentException("No group with the name of the Group-Setting found.");
                    }

                    foreach (string tag in settings.Tag.Split(','))
                    {
                        PwObjectList <PwEntry> tagEntries = new PwObjectList <PwEntry>();
                        groupToExport.FindEntriesByTag(tag, tagEntries, true);
                        // Prevent duplicated entries
                        IEnumerable <PwUuid> existingUuids = entries.Select(x => x.Uuid);
                        List <PwEntry>       entriesToAdd  = tagEntries.Where(x => !existingUuids.Contains(x.Uuid)).ToList();
                        entries.Add(entriesToAdd);
                    }
                }
            }
            else
            {
                throw new ArgumentException("At least one of Tag or ExportFolderName must be set.");
            }

            return(entries);
        }
コード例 #8
0
        private IEnumerable <PwEntryDatabase> FindMatchingEntries(string url, string realm)
        {
            var listResult = new List <PwEntryDatabase>();
            var hostUri    = new Uri(url);

            var formHost       = hostUri.Host;
            var searchHost     = hostUri.Host;
            var origSearchHost = hostUri.Host;

            List <PwDatabase> listDatabases = new List <PwDatabase>();

            var configOpt = new ConfigOpt(_host.CustomConfig);

            if (configOpt.SearchInAllOpenedDatabases)
            {
                foreach (PwDocument doc in _host.MainWindow.DocumentManager.Documents)
                {
                    if (doc.Database.IsOpen)
                    {
                        listDatabases.Add(doc.Database);
                    }
                }
            }
            else
            {
                listDatabases.Add(_host.Database);
            }

            var parms      = MakeSearchParameters(configOpt.HideExpired);
            var searchUrls = configOpt.SearchUrls;
            int listCount  = 0;

            foreach (PwDatabase db in listDatabases)
            {
                searchHost = origSearchHost;
                //get all possible entries for given host-name
                while (listResult.Count == listCount && (origSearchHost == searchHost || searchHost.IndexOf(".") != -1))
                {
                    parms.SearchString = string.Format("^{0}$|/{0}/?", searchHost);
                    var listEntries = new PwObjectList <PwEntry>();
                    db.RootGroup.SearchEntries(parms, listEntries);
                    listResult.AddRange(listEntries.Select(x => new PwEntryDatabase(x, db)));
                    if (searchUrls)
                    {
                        AddURLCandidates(db, listResult, parms.RespectEntrySearchingDisabled);
                    }
                    searchHost = searchHost.Substring(searchHost.IndexOf(".") + 1);

                    //searchHost contains no dot --> prevent possible infinite loop
                    if (searchHost == origSearchHost)
                    {
                        break;
                    }
                }
                listCount = listResult.Count;
            }

            var filter = new GFunc <PwEntry, bool>((PwEntry e) =>
            {
                var title    = e.Strings.ReadSafe(PwDefs.TitleField);
                var entryUrl = e.Strings.ReadSafe(PwDefs.UrlField);
                var c        = _ext.GetEntryConfig(e);
                if (c != null)
                {
                    if (c.Allow.Contains(formHost))
                    {
                        return(true);
                    }
                    if (c.Deny.Contains(formHost))
                    {
                        return(false);
                    }
                    if (!string.IsNullOrEmpty(realm) && c.Realm != realm)
                    {
                        return(false);
                    }
                }

                if (IsValidUrl(entryUrl, formHost))
                {
                    return(true);
                }

                if (IsValidUrl(title, formHost))
                {
                    return(true);
                }

                if (searchUrls)
                {
                    foreach (var sf in e.Strings.Where(s => s.Key.StartsWith("URL", StringComparison.InvariantCultureIgnoreCase)))
                    {
                        var sfv = e.Strings.ReadSafe(sf.Key);

                        if (sf.Key.IndexOf("regex", StringComparison.OrdinalIgnoreCase) >= 0 &&
                            System.Text.RegularExpressions.Regex.IsMatch(formHost, sfv))
                        {
                            return(true);
                        }

                        if (IsValidUrl(sfv, formHost))
                        {
                            return(true);
                        }
                    }
                }

                return(formHost.Contains(title) || (!string.IsNullOrEmpty(entryUrl) && formHost.Contains(entryUrl)));
            });

            var filterSchemes = new GFunc <PwEntry, bool>((PwEntry e) =>
            {
                var title    = e.Strings.ReadSafe(PwDefs.TitleField);
                var entryUrl = e.Strings.ReadSafe(PwDefs.UrlField);
                Uri entryUri;
                Uri titleUri;

                if (entryUrl != null && Uri.TryCreate(entryUrl, UriKind.Absolute, out entryUri) && entryUri.Scheme == hostUri.Scheme)
                {
                    return(true);
                }

                if (Uri.TryCreate(title, UriKind.Absolute, out titleUri) && titleUri.Scheme == hostUri.Scheme)
                {
                    return(true);
                }

                return(false);
            });

            var result = listResult.Where(e => filter(e.entry));

            if (configOpt.MatchSchemes)
            {
                result = result.Where(e => filterSchemes(e.entry));
            }

            if (configOpt.HideExpired)
            {
                result = result.Where(x => !(x.entry.Expires && x.entry.ExpiryTime <= DateTime.UtcNow));
            }

            return(result);
        }
コード例 #9
0
        private ResponseData FindCredentials(string url)
        {
            var notFound = new ResponseData
            {
                Entries = new ResponseEntry[0]
            };

            if (_pHost == null)
            {
                return(notFound);
            }

            if (_plugin.CryptoKey == null)
            {
                return(notFound);
            }

            if (!_pHost.Database.IsOpen)
            {
                return(notFound);
            }

            var cleanedSite = new UrlStripper(url);


            var sp = SearchParameters.None;

            sp.SearchString   = url;
            sp.SearchInUrls   = true;
            sp.SearchInTitles = true;
            sp.ExcludeExpired = true;

            var tmpResults = new PwObjectList <PwEntry>();

            _pHost.Database.RootGroup.SearchEntries(sp, tmpResults);

            while (cleanedSite.CanStrip)
            {
                var tmpRes = new PwObjectList <PwEntry>();
                sp.SearchString = cleanedSite.Strip();
                _pHost.Database.RootGroup.SearchEntries(sp, tmpRes);
                tmpResults.Add(tmpRes);
            }

            var results = new PwObjectList <PwEntry>();

            results.Add(tmpResults.Distinct().OrderBy(e => e.Strings.ReadSafe(PwDefs.TitleField)).ToList());

            if (results.UCount == 0)
            {
                return(notFound);
            }

            return(new ResponseData
            {
                Entries = results.Select(e => new ResponseEntry
                {
                    EntryName = e.Strings.ReadSafe(PwDefs.TitleField),
                    Username = e.Strings.ReadSafe(PwDefs.UserNameField),
                    Password = e.Strings.ReadSafe(PwDefs.PasswordField)
                }).ToArray()
            });
        }