Permalink
Browse files

- Exception handler on require

- Add -e option to directly point to a DB
- Exit on bad option instead of proceeding
- Better exception handling on sqlite db
- Add <group> to XML, doesn't work without.
- Fix broken English in README
  • Loading branch information...
1 parent 8c09374 commit 06f5614ce984ea86db94a7680d99fa2fcc3fff8a @alanpca committed Feb 5, 2011
Showing with 48 additions and 17 deletions.
  1. +7 −4 README
  2. +41 −13 chrome2keepass.rb
View
11 README
@@ -1,5 +1,6 @@
chrome2keepass will take a Chrome password database and convert it to proper
-XML format importation into Keepass (http://keepass.info/download.html).
+XML format which can be imported into Keepass
+(http://keepass.info/download.html).
If you're like me, then you sometimes want to access sites from a remote
computer, but have a massive amount of different passwords that are
@@ -22,11 +23,13 @@ Usage: chrome2keepass [options]
Default: 'Default'
-l, --location [LOCATION] Location to chrome config
Default: '$HOME/.config/chromium'
+ -e, --exact [LOCATION] Exact path to Chrome password database
+ Typically used with backup copy
-f, --filename [FILENAME] Filename to use instead of STDOUT
-h, --help Show this help
-WARNING: This will dump your passwords to your screen, plaintext. Don't
-run this when there are prying eyes around. The output is meant to be redirected
-to a file that you can import into Keepassx.
+WARNING: This will dump your passwords to your screen, plaintext if you don't
+use the -f option. Don't run this when there are prying eyes around. If there
+are others around, dump it to a file using -f.
Copyright (C) 2011 Alan P. Laudicina <contact@alanp.ca>
View
@@ -29,11 +29,16 @@
=end
-require 'optparse'
-require 'ostruct'
-require 'sqlite3'
-require 'cgi'
-require 'builder'
+begin
+ require 'optparse'
+ require 'ostruct'
+ require 'sqlite3'
+ require 'cgi'
+ require 'builder'
+rescue LoadError => e
+ puts "You don't have a required gem: " + e.message.split[-1]
+ exit
+end
def get_title(entry)
# This is here for future support of renaming
@@ -84,6 +89,11 @@ def getoptions(args)
options.location = location
end
+ opts.on("-e", "--exact [LOCATION]", "Exact path to Chrome password database",
+ "Typically used with backup copy") do |filename|
+ options.exactfile = filename
+ end
+
opts.on("-f", "--filename [FILENAME]", "Filename to use instead of STDOUT") do |filename|
options.filename = filename
end
@@ -121,10 +131,11 @@ def getoptions(args)
# We got some bad arguments, let the user know
print "Bad arguments: "
values['others'].each { |badopt| print badopt }
+ exit
else
output = $stdout
- if options.filename != nil?
+ if !options.filename.nil?
if !File.exist?(options.filename)
begin
output = File.new(options.filename, 'w')
@@ -139,26 +150,43 @@ def getoptions(args)
end
# Set the location of the DB based on options sent by user
- sqdb = options.location + "/" + options.profile + "/Login Data"
+ if options.exactfile.nil?
+ sqdb = options.location + "/" + options.profile + "/Login Data"
+ else
+ sqdb = options.exactfile
+ end
+
+ if !File.exist?(sqdb)
+ puts "Cannot open the database at location: " + sqdb
+ exit
+ end
+
db = SQLite3::Database.new(sqdb)
# Give the results as a hash
db.results_as_hash = true
begin
rows = db.execute("SELECT * FROM `logins`")
- # Leave on SQLException
+ # Leave on Exception
rescue SQLite3::SQLException
- puts "Database is locked or the location was invalid"
+ puts "There is a problem with your Chrome password database"
+ exit
+ rescue SQLite3::BusyException
+ puts "The Chrome database has been locked, please exit Chrome and try again"
exit
end
xml = Builder::XmlMarkup.new(:target => output, :indent => 2)
xml.declare! :DOCTYPE, :KEEPASSX_DATABASE
xml.database {
- rows.each do |entry|
- # Cycle through each entry and print it out
- put_entry(xml, entry)
- end
+ xml.group {
+ xml.title "chrome2keepass"
+ xml.icon "1"
+ rows.each do |entry|
+ # Cycle through each entry and print it out
+ put_entry(xml, entry)
+ end
+ }
}
end

0 comments on commit 06f5614

Please sign in to comment.