Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating metadata and header #35

Closed
Raggaer opened this Issue Feb 26, 2019 · 4 comments

Comments

Projects
None yet
2 participants
@Raggaer
Copy link

Raggaer commented Feb 26, 2019

Hello, when working with an already created database, lets say I am creating and modifying entries, do I need to regenerate header and metadata before saving?

I am experiencing some problems while editing my database, some clients work fine (keepassxc, however searching does not seem to work after using my custom client...) however the default KeePass does not seem to work when I create a new entry on my database (database corrupted), heres how I create my entries:

entry := gokeepasslib.NewEntry()
entry.Values = append(entry.Values, gokeepasslib.ValueData{Key: "UserName", Value: gokeepasslib.V{Content: data.Username}})
entry.Values = append(entry.Values, gokeepasslib.ValueData{Key: "Title", Value: gokeepasslib.V{Content: data.Title}})
entry.Values = append(entry.Values, gokeepasslib.ValueData{Key: "URL", Value: gokeepasslib.V{Content: data.URL}})
entry.Values = append(entry.Values, gokeepasslib.ValueData{Key: "Password", Value: gokeepasslib.V{Content: data.Password, Protected: true}})
currentGroup().Entries = append(currentGroup().Entries, entry)

And here is how I retrieve current group:

func currentGroup() *gokeepasslib.Group {
	g := &database.Content.Root.Groups[0]
	for _, h := range groupHistory {
		g = &g.Groups[h]
	}
	return g
}

Maybe its related to metadata or something I am missing?
Sorry for all these questions, but I dont know where else to ask them. Thanks

@tobischo

This comment has been minimized.

Copy link
Owner

tobischo commented Feb 27, 2019

It's ok :)
This is a good place to ask them, since they are specific to this library.

Do you happen to have a more complete code example?

func currentGroup() *gokeepasslib.Group looks a little odd.
Where is groupHistory coming from?

How exactly is your database corrupted?
You cannot even open it anymore? Or are the passwords just garbage?

@Raggaer

This comment has been minimized.

Copy link
Author

Raggaer commented Feb 27, 2019

Okey so basically, groupHistory is just a slice of group indexes var groupHistory []int, so I can navigate the database group using cd 1 (where 1 is index number one on the database root group slice) then I can do again, cd 2 (where 2 is index number two on root.Groups[1].Groups[2] ...

The database just wont open with keepass, however with keepassxc it will open just fine. I tried importing the broken database using repair mode but this does not work either (no error but no entries are imported), I have to say tho that I work on linux and windows, I use my application on linux and on windows I use or keepass or keepassxc.

This is how I currently save the database (after creating a new entry for example):

func saveDatabase() error {
	database.LockProtectedEntries()
        defer database.UnlockProtectedEntries()
	f, err := os.OpenFile(databaseLocation, os.O_WRONLY|os.O_TRUNC, 0777)
	if err != nil {
		return err
	}
	defer f.Close()
	encoder := gokeepasslib.NewEncoder(f)
	if err := encoder.Encode(database); err != nil {
		return err
	}
	return nil
}

database is a global *gokeepasslib.Database and databaseLocation is just a string, so I truncate the database file and encode the database.

@tobischo

This comment has been minimized.

Copy link
Owner

tobischo commented Feb 27, 2019

Ah, the globals are quite confusing when only having a partial of the code

I quickly wrote the following code. TestAddEntry.kdbx is a file I created using Keepass2 on a windows computer and then I ran the code below.
I did not have rebuild the headers. I am not sure whether this is helpful to you, as this does not address the particular issue you had.
I am not sure I can find the cause without being able to recreate the issue you had.

I attached the code and the keepass files below

package main

import (
	"os"

	"github.com/tobischo/gokeepasslib"
)

func main() {
	sourceFilePath := "./TestAddEntry.kdbx"
	targetFilePath := "./TestWithAddedEntry.kdbx"

	sourceFile, _ := os.Open(sourceFilePath)

	db := gokeepasslib.NewDatabase()
	db.Credentials = gokeepasslib.NewPasswordCredentials("abcdefg")
	_ = gokeepasslib.NewDecoder(sourceFile).Decode(db)

	db.UnlockProtectedEntries()

	entry := gokeepasslib.NewEntry()
	entry.Values = append(entry.Values, gokeepasslib.ValueData{Key: "UserName", Value: gokeepasslib.V{Content: "Test"}})
	entry.Values = append(entry.Values, gokeepasslib.ValueData{Key: "Title", Value: gokeepasslib.V{Content: "Title"}})
	entry.Values = append(entry.Values, gokeepasslib.ValueData{Key: "URL", Value: gokeepasslib.V{Content: "URL"}})
	entry.Values = append(entry.Values, gokeepasslib.ValueData{Key: "Password", Value: gokeepasslib.V{Content: "supersecret", Protected: true}})

	db.Content.Root.Groups[0].Groups[1].Entries = append(db.Content.Root.Groups[0].Groups[1].Entries, entry)

	db.LockProtectedEntries()

	targetFile, err := os.OpenFile(targetFilePath, os.O_WRONLY|os.O_TRUNC, 0777)
	if err != nil {
		panic(err)
	}

	keepassEncoder := gokeepasslib.NewEncoder(targetFile)
	if err := keepassEncoder.Encode(db); err != nil {
		panic(err)
	}
}

testaddentry.zip

@Raggaer

This comment has been minimized.

Copy link
Author

Raggaer commented Feb 28, 2019

After trying to see what I was doing wrong, it seems I was using : for the filename, and that is invalid on Windows... now I feel very stupid. Sorry and thanks for the help =)

@Raggaer Raggaer closed this Feb 28, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.