Friday, October 07, 2016

Gotcha of the Day: Generating formatted output from a Berkeley DB file

I was debugging a problem with virtual users in vsftpd and narrowed down my issue to this: how do I convert the Berkeley DB file filled usernames and passwords into plain text?

I saw that there was a script lying around that used db_load to add users to the virtual file. When I typed db_ and hit TAB I saw various options, the most promising of which was db_dump. However typing:

  db_dump /etc/vsftpd/vusers.db

spit out gibberish. What I wanted was a nicely formatted output.

Reading the docs I learned that db_dump -p ... will dump the database in printable form rather than hex. Running db_dump -p generated the following:

VERSION=3
format=print
type=hash
h_nelem=61
db_pagesize=4096
HEADER=END
 bob
 XvLtPKE69eAR2Glc
 alice
 TXpZxXNjoyKfhzWG
 ...  

Closer, but still not quite the format I was after. Time to bust out sed and awk. Here's a command line I conjured up:

db_dump -p /etc/vsftpd/vusers.db | \
 grep '^ ' | \
 awk 'BEGIN { line=1 ; } \
     { printf "%s", $0;  \
       if(line % 2 == 0) { printf "\n"; } \
       else { printf "\t"; }; \
       line++ }' | \
 sed 's/^ //'

The output is now:

bob   XvLtPKE69eAR2Glc
alice TXpZxXNjoyKfhzWG
...

Where the columns are separated by tabs.

Mission accomplished.

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails