public MatchAccuracyMethod SelectsCorrectMatchAccuracyMethod( string urlSearch, MatchAccuracyMethod entryMam, MatchAccuracyMethod defaultMam, string[] overrideURLs = null, MatchAccuracyMethod[] overrideMethods = null) { var pwe = new PwEntry(true, true); var conf = new EntryConfig(entryMam); pwe.SetKPRPCConfig(conf); var urlSummary = URLSummary.FromURL(urlSearch); var dbConf = new DatabaseConfig() { DefaultMatchAccuracy = defaultMam }; if (overrideURLs != null) { for (int i = 0; i < overrideURLs.Length; i++) { dbConf.MatchedURLAccuracyOverrides.Add(overrideURLs[i], overrideMethods[i]); } } return(pwe.GetMatchAccuracyMethod(urlSummary, dbConf)); }
public MatchAccuracyEnum CalculatesCorrectMatchAccuracyScore(string urlEntry, string urlSearch, MatchAccuracyMethod entryMam) { var pwe = new PwEntry(true, true); pwe.Strings.Set("URL", new ProtectedString(false, urlEntry)); var conf = new EntryConfig(entryMam); pwe.SetKPRPCConfig(conf); var urlSummary = URLSummary.FromURL(urlSearch); return((MatchAccuracyEnum)KeePassRPCService.BestMatchAccuracyForAnyURL(pwe, conf, urlSearch, urlSummary, entryMam)); }
private void ConvertKPRPCSeperateStringsToJSON(PwEntry pwe, PwDatabase db) { //Sanity check to protect against duplicate updates if (pwe.Strings.Exists("KPRPC JSON")) return; ArrayList formFieldList = new ArrayList(); ArrayList URLs = new ArrayList(); //bool usernameFound = false; bool passwordFound = false; bool alwaysAutoFill = false; bool neverAutoFill = false; bool alwaysAutoSubmit = false; bool neverAutoSubmit = false; int priority = 0; string usernameName = ""; string usernameValue = ""; foreach (System.Collections.Generic.KeyValuePair <string, KeePassLib.Security.ProtectedString> pwestring in pwe.Strings) { string pweKey = pwestring.Key; string pweValue = pwestring.Value.ReadString(); if ((pweKey.StartsWith("Form field ") || pweKey.StartsWith("KPRPC Form field ")) && pweKey.EndsWith(" type") && pweKey.Length > 16) { string fieldName = ""; if (pweKey.StartsWith("Form field ")) fieldName = pweKey.Substring(11).Substring(0, pweKey.Length - 11 - 5); else fieldName = pweKey.Substring(17).Substring(0, pweKey.Length - 17 - 5); string fieldId = ""; int fieldPage = 1; if (pwe.Strings.Exists("Form field " + fieldName + " page")) { try { fieldPage = int.Parse(GetPwEntryString(pwe, "Form field " + fieldName + " page", db)); } catch (Exception) { fieldPage = 1; } } else if (pwe.Strings.Exists("KPRPC Form field " + fieldName + " page")) { try { fieldPage = int.Parse(GetPwEntryString(pwe, "KPRPC Form field " + fieldName + " page", db)); } catch (Exception) { fieldPage = 1; } } if (pwe.Strings.Exists("Form field " + fieldName + " id")) fieldId = GetPwEntryString(pwe, "Form field " + fieldName + " id", db); else if (pwe.Strings.Exists("KPRPC Form field " + fieldName + " id")) fieldId = GetPwEntryString(pwe, "KPRPC Form field " + fieldName + " id", db); //Not going to backfill missing passwords and usernames but we do need to convert from old value placeholder to new one if (pweValue == "password") { if (pwe.Strings.Exists("Form field " + fieldName + " value")) formFieldList.Add(new FormField(fieldName, fieldName, GetPwEntryString(pwe, "Form field " + fieldName + " value", db), FormFieldType.FFTpassword, fieldId, fieldPage)); else if (pwe.Strings.Exists("KPRPC Form field " + fieldName + " value")) formFieldList.Add(new FormField(fieldName, fieldName, GetPwEntryString(pwe, "KPRPC Form field " + fieldName + " value", db), FormFieldType.FFTpassword, fieldId, fieldPage)); else if (!passwordFound) // it's the default password { formFieldList.Add(new FormField(fieldName, "KeePass password", "{PASSWORD}", FormFieldType.FFTpassword, fieldId, fieldPage)); passwordFound = true; } } else if (pweValue == "username") { string displayUser = "******"; //if (usernameFound) // displayUser = fieldName; formFieldList.Add(new FormField(fieldName, displayUser, "{USERNAME}", FormFieldType.FFTusername, fieldId, fieldPage)); usernameName = fieldName; usernameValue = GetPwEntryString(pwe, "UserName", db); //usernameFound = true; } else if (pweValue == "text") { formFieldList.Add(new FormField(fieldName, fieldName, GetFormFieldValue(pwe, fieldName, db), FormFieldType.FFTtext, fieldId, fieldPage)); } else if (pweValue == "radio") { formFieldList.Add(new FormField(fieldName, fieldName, GetFormFieldValue(pwe, fieldName, db), FormFieldType.FFTradio, fieldId, fieldPage)); } else if (pweValue == "select") { formFieldList.Add(new FormField(fieldName, fieldName, GetFormFieldValue(pwe, fieldName, db), FormFieldType.FFTselect, fieldId, fieldPage)); } else if (pweValue == "checkbox") { formFieldList.Add(new FormField(fieldName, fieldName, GetFormFieldValue(pwe, fieldName, db), FormFieldType.FFTcheckbox, fieldId, fieldPage)); } } else if (pweKey == "Alternative URLs" || pweKey == "KPRPC Alternative URLs") { string[] urlsArray = pweValue.Split(new char[] { ' ' }); foreach (string altURL in urlsArray) if (!string.IsNullOrEmpty(altURL)) URLs.Add(altURL); } } if (pwe.Strings.Exists("KeeFox Always Auto Fill") || pwe.Strings.Exists("KPRPC Always Auto Fill")) alwaysAutoFill = true; if (pwe.Strings.Exists("KeeFox Always Auto Submit") || pwe.Strings.Exists("KPRPC Always Auto Submit")) alwaysAutoSubmit = true; if (pwe.Strings.Exists("KeeFox Never Auto Fill") || pwe.Strings.Exists("KPRPC Never Auto Fill")) neverAutoFill = true; if (pwe.Strings.Exists("KeeFox Never Auto Submit") || pwe.Strings.Exists("KPRPC Never Auto Submit")) neverAutoSubmit = true; if (pwe.Strings.Exists("KeeFox Priority")) { string priorityString = pwe.Strings.ReadSafe("KeeFox Priority"); if (!string.IsNullOrEmpty(priorityString)) { try { priority = int.Parse(priorityString); } catch { } if (priority < 0 || priority > 100000) priority = 0; } } if (pwe.Strings.Exists("KPRPC Priority")) { string priorityString = pwe.Strings.ReadSafe("KPRPC Priority"); if (!string.IsNullOrEmpty(priorityString)) { try { priority = int.Parse(priorityString); } catch { } if (priority < 0 || priority > 100000) priority = 0; } } string realm = ""; try { realm = GetPwEntryString(pwe, "Form HTTP realm", db); } catch (Exception) { realm = ""; } if (string.IsNullOrEmpty(realm)) { try { realm = GetPwEntryString(pwe, "KPRPC Form HTTP realm", db); } catch (Exception) { realm = ""; } } if (string.IsNullOrEmpty(realm)) { try { realm = GetPwEntryString(pwe, "KPRPC HTTP realm", db); } catch (Exception) { realm = ""; } } EntryConfig conf = new EntryConfig(); conf.Hide = false; string hide = ""; try { hide = GetPwEntryString(pwe, "Hide from KeeFox", db); if (!string.IsNullOrEmpty(hide)) conf.Hide = true; } catch (Exception) { hide = ""; } if (string.IsNullOrEmpty(hide)) { try { hide = GetPwEntryString(pwe, "Hide from KPRPC", db); if (!string.IsNullOrEmpty(hide)) conf.Hide = true; } catch (Exception) { hide = ""; } } conf.BlockHostnameOnlyMatch = false; string block = ""; try { block = GetPwEntryString(pwe, "KPRPC Block hostname-only match", db); if (!string.IsNullOrEmpty(block)) conf.BlockHostnameOnlyMatch = true; } catch (Exception) { block = ""; } FormField[] temp = (FormField[])formFieldList.ToArray(typeof(FormField)); conf.AltURLs = (string[])URLs.ToArray(typeof(string)); try { List<string> listNormalBlockedURLs = new List<string>(); string urls = GetPwEntryString(pwe, "KPRPC Blocked URLs", db); foreach (string url in urls.Split(' ')) if (!string.IsNullOrEmpty(url)) listNormalBlockedURLs.Add(url); conf.BlockedURLs = listNormalBlockedURLs.ToArray(); } catch (Exception) { } try { List<string> listRegExURLs = new List<string>(); string urls = GetPwEntryString(pwe, "KPRPC URL Regex match", db); foreach (string url in urls.Split(' ')) if (!string.IsNullOrEmpty(url)) listRegExURLs.Add(url); conf.RegExURLs = listRegExURLs.ToArray(); } catch (Exception) { } try { List<string> listRegExBlockedURLs = new List<string>(); string urls = GetPwEntryString(pwe, "KPRPC URL Regex block", db); foreach (string url in urls.Split(' ')) if (!string.IsNullOrEmpty(url)) listRegExBlockedURLs.Add(url); conf.RegExBlockedURLs = listRegExBlockedURLs.ToArray(); } catch (Exception) { } conf.AlwaysAutoFill = alwaysAutoFill; conf.AlwaysAutoSubmit = alwaysAutoSubmit; conf.FormActionURL = GetPwEntryString(pwe, "Form match URL", db); conf.FormFieldList = temp; conf.HTTPRealm = realm; conf.NeverAutoFill = neverAutoFill; conf.NeverAutoSubmit = neverAutoSubmit; conf.Priority = priority; conf.Version = 1; // Store the new config info pwe.SetKPRPCConfig(conf); // Delete all old advanced strings... List<string> advancedStringKeysToDelete = new List<string>(); foreach (KeyValuePair<string, ProtectedString> kvp in pwe.Strings) { if (StringIsFromKPRPCv1(kvp.Key)) { // Not sure how kindly KeePass would take to DB changes while iterating so we'll store a list for later advancedStringKeysToDelete.Add(kvp.Key); } } foreach (string item in advancedStringKeysToDelete) { pwe.Strings.Remove(item); } }
private void setPwEntryFromEntry(PwEntry pwe, Entry login) { bool firstPasswordFound = false; EntryConfig conf = new EntryConfig(); List<FormField> ffl = new List<FormField>(); // Go through each form field, mostly just making a copy but with occasional tweaks such as default username and password selection // by convention, we'll always have the first text field as the username when both reading and writing from the EntryConfig foreach (FormField kpff in login.FormFieldList) { if (kpff.Type == FormFieldType.FFTpassword && !firstPasswordFound) { ffl.Add(new FormField(kpff.Name, "KeePass password", "{PASSWORD}", kpff.Type, kpff.Id, kpff.Page)); pwe.Strings.Set("Password", new ProtectedString(host.Database.MemoryProtection.ProtectPassword, kpff.Value)); firstPasswordFound = true; } else if (kpff.Type == FormFieldType.FFTusername) { ffl.Add(new FormField(kpff.Name, "KeePass username", "{USERNAME}", kpff.Type, kpff.Id, kpff.Page)); pwe.Strings.Set("UserName", new ProtectedString(host.Database.MemoryProtection.ProtectUserName, kpff.Value)); } else { ffl.Add(new FormField(kpff.Name, kpff.Name, kpff.Value, kpff.Type, kpff.Id, kpff.Page)); } } conf.FormFieldList = ffl.ToArray(); List<string> altURLs = new List<string>(); for (int i = 0; i < login.URLs.Length; i++) { string url = login.URLs[i]; if (i == 0) { URLSummary urlsum = URLSummary.FromURL(url); // Require more strict default matching for entries that come // with a port configured (user can override in the rare case // that they want the loose domain-level matching) if (!string.IsNullOrEmpty(urlsum.Port)) conf.BlockDomainOnlyMatch = true; pwe.Strings.Set("URL", new ProtectedString(host.Database.MemoryProtection.ProtectUrl, url ?? "")); } else altURLs.Add(url); } conf.AltURLs = altURLs.ToArray(); conf.HTTPRealm = login.HTTPRealm; conf.Version = 1; // Set some of the string fields pwe.Strings.Set(PwDefs.TitleField, new ProtectedString(host.Database.MemoryProtection.ProtectTitle, login.Title ?? "")); // update the icon for this entry (in most cases we'll // just detect that it is the same standard icon as before) PwUuid customIconUUID = PwUuid.Zero; PwIcon iconId = PwIcon.Key; if (login.IconImageData != null && login.IconImageData.Length > 0 && base64ToIcon(login.IconImageData, ref customIconUUID, ref iconId)) { if (customIconUUID == PwUuid.Zero) pwe.IconId = iconId; else pwe.CustomIconUuid = customIconUUID; } pwe.SetKPRPCConfig(conf); }
private void MergeEntries(PwEntry destination, PwEntry source, int urlMergeMode, PwDatabase db) { EntryConfig destConfig = destination.GetKPRPCConfig(); if (destConfig == null) return; EntryConfig sourceConfig = source.GetKPRPCConfig(); if (sourceConfig == null) return; destination.CreateBackup(db); destConfig.HTTPRealm = sourceConfig.HTTPRealm; destination.IconId = source.IconId; destination.CustomIconUuid = source.CustomIconUuid; destination.Strings.Set("UserName", new ProtectedString( host.Database.MemoryProtection.ProtectUserName, source.Strings.ReadSafe("UserName"))); destination.Strings.Set("Password", new ProtectedString( host.Database.MemoryProtection.ProtectPassword, source.Strings.ReadSafe("Password"))); destConfig.FormFieldList = sourceConfig.FormFieldList; // This algorithm could probably be made more efficient (lots of O(n) operations // but we're dealing with pretty small n so I've gone with the conceptually // easiest approach for now). List<string> destURLs = new List<string>(); destURLs.Add(destination.Strings.ReadSafe("URL")); if (destConfig.AltURLs != null) destURLs.AddRange(destConfig.AltURLs); List<string> sourceURLs = new List<string>(); sourceURLs.Add(source.Strings.ReadSafe("URL")); if (sourceConfig.AltURLs != null) sourceURLs.AddRange(sourceConfig.AltURLs); switch (urlMergeMode) { case 1: MergeInNewURLs(destURLs, sourceURLs); break; case 2: destURLs.RemoveAt(0); MergeInNewURLs(destURLs, sourceURLs); break; case 3: if (sourceURLs.Count > 0) { foreach (string sourceUrl in sourceURLs) if (!destURLs.Contains(sourceUrl)) destURLs.Add(sourceUrl); } break; case 4: default: // No changes to URLs break; } // These might not have changed but meh destination.Strings.Set("URL", new ProtectedString(host.Database.MemoryProtection.ProtectUrl, destURLs[0])); destConfig.AltURLs = new string[0]; if (destURLs.Count > 1) destConfig.AltURLs = destURLs.GetRange(1,destURLs.Count-1).ToArray(); destination.SetKPRPCConfig(destConfig); destination.Touch(true); }
private void AddPasswordBackupLogin(string password, string url) { if (!host.Database.IsOpen) return; PwDatabase chosenDB = SelectDatabase(""); var parentGroup = KeePassRPCPlugin.GetAndInstallKeeFoxPasswordBackupGroup(chosenDB); PwEntry newLogin = new PwEntry(true, true); newLogin.Strings.Set(PwDefs.TitleField, new ProtectedString( chosenDB.MemoryProtection.ProtectTitle, "KeeFox generated password at: " + DateTime.Now)); newLogin.Strings.Set(PwDefs.UrlField, new ProtectedString( chosenDB.MemoryProtection.ProtectUrl, url)); newLogin.Strings.Set(PwDefs.PasswordField, new ProtectedString( chosenDB.MemoryProtection.ProtectPassword, password)); EntryConfig conf = new EntryConfig(); conf.BlockDomainOnlyMatch = true; conf.Hide = true; newLogin.SetKPRPCConfig(conf); parentGroup.AddEntry(newLogin, true); // We can't save the database at this point because KeePass steals // window focus while saving; that breaks Firefox's Australis UI panels. host.MainWindow.BeginInvoke(new dlgUpdateUINoSave(updateUINoSave)); return; }