Generate URL field from domain stored in title

0

I have a lot of entries in my kdbx database where the title is the domain the password belongs to. In most but not all of them, the URL field is empty:

| Title           | Username | Password | URL                       | ... |
|-----------------+----------+----------+---------------------------+-----|
| example.com     |          | ***      |                           | ... |
| foo.example.net | foo      | ***      |                           | ... |
| bar.example.net | bar      | ***      |                           | ... |
| example.org     | bla      | ***      | https://example.org/login | ... |
| ...             | ...      | ...      | ...                       | ... |

How to fill the URL field based on the title field so the DB looks like the one below?

| Title           | Username | Password | URL                       | ... |
|-----------------+----------+----------+---------------------------+-----|
| example.com     |          | ***      | https://example.com/      | ... |
| foo.example.net | foo      | ***      | https://foo.example.net   | ... |
| bar.example.net | bar      | ***      | https://bar.example.net   | ... |
| example.org     | bla      | ***      | https://example.org/login | ... |
| ...             | ...      | ...      | ...                       | ... |

Erik

Posted 2019-10-28T14:17:06.353

Reputation: 644

2

Should be possible via KeePass scripting: https://keepass.info/help/v2_dev/scr_index.html

– Robert – 2019-10-28T14:25:17.277

Answers

0

This bash script does the job using KeepassXC (tested with v2.4.3). Usage is ./fill_urls.sh DATABASE.

The database has to be protected with a password and not with a key file.

#!/bin/bash

do_keepass () {
    echo $password | keepassxc-cli $@ --quiet
}

is_group () {
    local path=$1
    [[ $path =~ /$ ]]
}

all_entry_paths () {
    local root=$1
    is_group $root || (echo "$root is not a group"; exit 1)
    do_keepass ls $db $root | while read line; do
        local path="${root}${line}"
        if is_group $path; then
            all_entry_paths $path
        else
            echo $path
        fi
    done
}

is_domain () {
    local string=$1
    [[ $string =~ [a-zA-Z0-9-]+\.[a-zA-Z]{2,} ]]
}

has_url () {
    local path=$1
    [ ! -z $(do_keepass show $db $path --attributes=URL) ]
}

make_url () {
    local title=$1
    echo "https://${title}"
}

main () {
    all_entry_paths / | while read path; do
        ! is_group $path || (echo "$path is not an entry"; exit 1)
        local title=$(basename $path)
        if is_domain $title && ! has_url $path; then
            local new_url=$(make_url $title)
            echo "Setting URL of $path to $new_url" > /dev/stderr
            do_keepass edit --url $new_url $db $path
        fi
    done
}

echo -n "Password: " > /dev/stderr
read -s password
echo > /dev/stderr
db=$1

main

Erik

Posted 2019-10-28T14:17:06.353

Reputation: 644