Ejemplo n.º 1
0
        public string getDataByGroup(string value, KeePassLib.PwGroup kpGroup, string kpColumn2Search = "Title", string kpColumn2Return = "Password")
        {
            string returnValue = string.Empty;
            var    ioconninfo  = new KeePassLib.Serialization.IOConnectionInfo();

            if (!(string.IsNullOrEmpty(kpGroup.ToString())))
            {
                try
                {
                    KeePassLib.PwEntry pw = kpGroup.Entries.FirstOrDefault(i => i.Strings.ReadSafe(kpColumn2Search) == value);

                    if (pw != null)
                    {
                        returnValue = pw.Strings.ReadSafe(kpColumn2Return);
                    }
                    else
                    {
                        returnValue = string.Empty;
                    }

                    pw = null;
                }
                catch
                {
                    throw;
                }
            }

            return(returnValue);
        }
Ejemplo n.º 2
0
 private ProtectedString ParseFromImageFile(string sFile)
 {
     KeePassLib.Serialization.IOConnectionInfo iocInfo = new KeePassLib.Serialization.IOConnectionInfo();
     iocInfo.Path = sFile;
     byte[] buffer = KeePassLib.Serialization.IOConnection.ReadFile(iocInfo);
     return(ParseFromImageByteArray(buffer));
 }
Ejemplo n.º 3
0
        public KeePassLib.PwGroup getGroup(string name)
        {
            KeePassLib.PwGroup group = new KeePassLib.PwGroup();

            var ioconninfo = new KeePassLib.Serialization.IOConnectionInfo();

            if (!(string.IsNullOrEmpty(KeepassDBFilePath)))
            {
                ioconninfo.Path = base64Decode(KeepassDBFilePath);
                KeePassLib.Keys.CompositeKey compkey = new KeePassLib.Keys.CompositeKey();
                if (string.IsNullOrEmpty(KeepassKeyFilePath) && string.IsNullOrEmpty(KeepassMasterPassword))
                {
                    throw new Exception("A Key file or Master Password has not been set!");
                }
                else
                {
                    if (!(string.IsNullOrEmpty(KeepassKeyFilePath)))
                    {
                        compkey.AddUserKey(new KeePassLib.Keys.KcpKeyFile(base64Decode(KeepassKeyFilePath)));
                    }
                    if (!(string.IsNullOrEmpty(KeepassMasterPassword)))
                    {
                        compkey.AddUserKey(new KeePassLib.Keys.KcpPassword(base64Decode(KeepassMasterPassword)));
                    }
                    var db = new KeePassLib.PwDatabase();

                    try
                    {
                        db.Open(ioconninfo, compkey, null);
                        KeePassLib.Collections.PwObjectList <KeePassLib.PwGroup> groups = db.RootGroup.GetGroups(true);

                        group = groups.First(i => i.Name == name);
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        if (db.IsOpen)
                        {
                            db.Close();
                            db = null;
                        }
                    }
                }
            }
            else
            {
                throw new Exception("Keepass DB Path has not been set!");
            }

            return(group);
        }
Ejemplo n.º 4
0
        private static List <CredentialSet> LoadKeePass()
        {
            try
            {
                var ioConnInfo = new KeePassLib.Serialization.IOConnectionInfo {
                    Path = Main.Settings.Settings.KeePassPath
                };
                var compKey = new KeePassLib.Keys.CompositeKey();
                compKey.AddUserKey(new KeePassLib.Keys.KcpPassword(Main.Settings.Settings.KeePassPassword));

                var db = new KeePassLib.PwDatabase();
                db.Open(ioConnInfo, compKey, null);

                var entries = db.RootGroup.GetEntries(true);

                List <CredentialSet> list = new List <CredentialSet>();

                foreach (var entry in entries)
                {
                    string title    = entry.Strings.ReadSafe("Title");
                    string userName = entry.Strings.ReadSafe("UserName");
                    string domain   = entry.Strings.ReadSafe("Domain");

                    if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(userName))
                    {
                        CredentialSet credentialSet = new CredentialSet
                        {
                            Name     = title,
                            Username = string.IsNullOrEmpty(domain) && userName.Contains("\\") ? userName.Split(new string[] { "\\" }, StringSplitOptions.None)[1] : userName,
                            Domain   = string.IsNullOrEmpty(domain) && userName.Contains("\\") ? userName.Split(new string[] { "\\" }, StringSplitOptions.None)[0] : domain,
                            Password = entry.Strings.ReadSafe("Password")
                        };

                        list.Add(credentialSet);

                        string id = entry.Uuid.ToHexString();

                        if (!keyPassCredentialsById.ContainsKey(id))
                        {
                            keyPassCredentialsById.Add(id, credentialSet);
                        }
                    }
                }

                db.Close();

                return(list);
            } catch (Exception ex)
            {
                Log.Error("Error loading KeePass-File due to the following reason: " + ex.Message, ex);
                return(new List <CredentialSet>());
            }
        }
Ejemplo n.º 5
0
        protected bool DownloadFile(string sFile, string sTargetFolder)
        {
            const int MAXATTEMPTS = 3;
            int       iAttempts   = 0;
            string    sSource     = GetShortestAbsolutePath(sFile);

            while (iAttempts++ < MAXATTEMPTS)
            {
                try
                {
                    string sTarget = GetShortestAbsolutePath(sTargetFolder + UrlUtil.GetFileName(sSource));
                    KeePassLib.Serialization.IOConnectionInfo ioc = KeePassLib.Serialization.IOConnectionInfo.FromPath(sSource);
                    Stream s = KeePassLib.Serialization.IOConnection.OpenRead(ioc);
                    if (s == null)
                    {
                        throw new InvalidOperationException();
                    }
                    MemoryStream ms = new MemoryStream();
                    MemUtil.CopyStream(s, ms);
                    s.Close();
                    byte[] pb = ms.ToArray();
                    ms.Close();
                    //Create target folder, Directory.CreateDirectory internally checks for existance of the folder
                    Directory.CreateDirectory(sTargetFolder);
                    File.WriteAllBytes(sTarget, pb);
                    m_lDownloaded.Add(sTarget);
                    PluginDebug.AddInfo("Download success", 0, "Source: " + sSource, "Target: " + sTargetFolder, "Download attempt: " + iAttempts.ToString());
                    return(true);
                }
                catch (Exception ex)
                {
                    PluginDebug.AddInfo("Download failed", 0, "Source: " + sSource, "Target: " + sTargetFolder, "Download attempt: " + iAttempts.ToString(), ex.Message);

                    System.Net.WebException exWeb = ex as System.Net.WebException;
                    if (exWeb == null)
                    {
                        continue;
                    }
                    System.Net.HttpWebResponse wrResponse = exWeb.Response as System.Net.HttpWebResponse;
                    if ((wrResponse == null) || (wrResponse.StatusCode != System.Net.HttpStatusCode.NotFound))
                    {
                        continue;
                    }
                    iAttempts = MAXATTEMPTS;
                }
            }
            return(false);
        }
Ejemplo n.º 6
0
        private static bool Export(KeePassLib.PwDatabase database, Uri filePath, KeePassLib.Security.ProtectedString password, KeePassLib.Interfaces.IStatusLogger logger)
        {
            Exception argumentError = CheckArgument(database, filePath, password);

            if (!ReferenceEquals(argumentError, null))
            {
                throw argumentError;
            }

            if (string.Equals(database.IOConnectionInfo.Path, filePath.LocalPath, StringComparison.InvariantCultureIgnoreCase))
            {
                return(false); //Don't export myself
            }
            //Create new database in temporary file
            KeePassLib.PwDatabase exportedDatabase = new KeePassLib.PwDatabase();
            exportedDatabase.Compression = KeePassLib.PwCompressionAlgorithm.GZip;
            KeePassLib.Serialization.IOConnectionInfo connectionInfo = new KeePassLib.Serialization.IOConnectionInfo();
            string storageDirectory = Path.GetDirectoryName(filePath.LocalPath);
            string tmpPath          = Path.Combine(storageDirectory, string.Format("{0}{1}", Guid.NewGuid(), KeePassDatabaseExtension));

            connectionInfo.Path         = tmpPath;
            connectionInfo.CredSaveMode = KeePassLib.Serialization.IOCredSaveMode.SaveCred;
            KeePassLib.Keys.CompositeKey exportedKey = new KeePassLib.Keys.CompositeKey();
            exportedKey.AddUserKey(new KeePassLib.Keys.KcpPassword(password.ReadString()));
            exportedDatabase.New(connectionInfo, exportedKey);
            exportedDatabase.RootGroup.Name = database.RootGroup.Name;

            //Merge current database in temporary file
            exportedDatabase.MergeIn(database, KeePassLib.PwMergeMethod.OverwriteExisting, logger);
            exportedDatabase.Save(logger);
            exportedDatabase.Close();

            //Move temporary file into target backup path
            if (File.Exists(filePath.LocalPath))
            {
                File.Delete(filePath.LocalPath);
            }
            File.Move(tmpPath, filePath.LocalPath);

            return(true);
        }
Ejemplo n.º 7
0
        private static KeePassLib.Serialization.IOConnectionInfo ReadIOConnectionInfo(XmlReader xr)
        {
            KeePassLib.Serialization.IOConnectionInfo o = new KeePassLib.Serialization.IOConnectionInfo();

            if(SkipEmptyElement(xr)) return o;

            Debug.Assert(xr.NodeType == XmlNodeType.Element);
            xr.ReadStartElement();
            xr.MoveToContent();

            while(true)
            {
                XmlNodeType nt = xr.NodeType;
                if((nt == XmlNodeType.EndElement) || (nt == XmlNodeType.None)) break;
                if(nt != XmlNodeType.Element) { Debug.Assert(false); continue; }

                switch(xr.LocalName)
                {
                    case "Path":
                        o.Path = ReadString(xr);
                        break;
                    case "UserName":
                        o.UserName = ReadString(xr);
                        break;
                    case "Password":
                        o.Password = ReadString(xr);
                        break;
                    case "CredProtMode":
                        o.CredProtMode = ReadIOCredProtMode(xr);
                        break;
                    case "CredSaveMode":
                        o.CredSaveMode = ReadIOCredSaveMode(xr);
                        break;
                    default:
                        Debug.Assert(false);
                        xr.Skip();
                        break;
                }

                xr.MoveToContent();
            }

            Debug.Assert(xr.NodeType == XmlNodeType.EndElement);
            xr.ReadEndElement();
            return o;
        }
Ejemplo n.º 8
0
        private static List<CredentialSet> LoadKeePass()
        {        	
        	try
        	{
				var ioConnInfo = new KeePassLib.Serialization.IOConnectionInfo { Path = Main.Settings.Settings.KeePassPath };
				var compKey = new KeePassLib.Keys.CompositeKey();
				compKey.AddUserKey(new KeePassLib.Keys.KcpPassword(Main.Settings.Settings.KeePassPassword));
				
				var db = new KeePassLib.PwDatabase();
				db.Open(ioConnInfo, compKey, null);
	
				var entries = db.RootGroup.GetEntries(true);
				
				List<CredentialSet> list = new List<CredentialSet>();
								
				foreach (var entry in entries)
				{
					string title = entry.Strings.ReadSafe("Title");
					string userName = entry.Strings.ReadSafe("UserName");
					string domain = entry.Strings.ReadSafe("Domain");
					
					if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(userName))
					{
						list.Add(new CredentialSet
		                {
		                    Name = title,
		                    Username = string.IsNullOrEmpty(domain) && userName.Contains("\\")  ? userName.Split(new string[] {"\\"}, StringSplitOptions.None)[1] : userName,
		                    Domain = string.IsNullOrEmpty(domain) && userName.Contains("\\")  ? userName.Split(new string[] {"\\"}, StringSplitOptions.None)[0] : domain,
		                    Password = entry.Strings.ReadSafe("Password")
				         });
					}
				}

				db.Close();
				
				return list;
        	} catch (Exception ex)
        	{
                Log.Error("Error loading KeePass-File due to the following reason: " + ex.Message, ex);
                return  new List<CredentialSet>();
        	}
        }
Ejemplo n.º 9
0
        public string getData(string value, string kpColumn2Search = "Title", string kpColumn2Return = "Password")
        {
            string returnValue = string.Empty;
            var    ioconninfo  = new KeePassLib.Serialization.IOConnectionInfo();

            if (!(string.IsNullOrEmpty(KeepassDBFilePath)))
            {
                ioconninfo.Path = base64Decode(KeepassDBFilePath);
                KeePassLib.Keys.CompositeKey compkey = new KeePassLib.Keys.CompositeKey();
                if (string.IsNullOrEmpty(KeepassKeyFilePath) && string.IsNullOrEmpty(KeepassMasterPassword))
                {
                    throw new Exception("A Key file or Master Password has not been set!");
                }
                else
                {
                    if (!(string.IsNullOrEmpty(KeepassKeyFilePath)))
                    {
                        compkey.AddUserKey(new KeePassLib.Keys.KcpKeyFile(base64Decode(KeepassKeyFilePath)));
                    }
                    if (!(string.IsNullOrEmpty(KeepassMasterPassword)))
                    {
                        compkey.AddUserKey(new KeePassLib.Keys.KcpPassword(base64Decode(KeepassMasterPassword)));
                    }
                    var db = new KeePassLib.PwDatabase();

                    try
                    {
                        db.Open(ioconninfo, compkey, null);

                        KeePassLib.Collections.PwObjectList <KeePassLib.PwEntry> entries = db.RootGroup.GetEntries(true);
                        //var data =  from entry in db.rootgroup.getentries(true) where entry.strings.readsafe("title") == "tyler-u-client-id" select entry;

                        KeePassLib.PwEntry pw = entries.FirstOrDefault(i => i.Strings.ReadSafe(kpColumn2Search) == value);

                        if (pw != null)
                        {
                            returnValue = pw.Strings.ReadSafe(kpColumn2Return);
                        }
                        else
                        {
                            returnValue = string.Empty;
                        }

                        pw = null;
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        if (db.IsOpen)
                        {
                            db.Close();
                            db = null;
                        }
                    }
                }
            }
            else
            {
                throw new Exception("Keepass DB Path has not been set!");
            }

            return(returnValue);
        }