-
Notifications
You must be signed in to change notification settings - Fork 4
/
1password2pass.rb
executable file
·180 lines (159 loc) · 5.56 KB
/
1password2pass.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env ruby
# Copyright (C) 2014 Tobias V. Langhoff <[email protected]>. All Rights Reserved.
# This file is licensed under GPLv2+. Please see the following file for more
# information: http://git.zx2c4.com/password-store/tree/COPYING
#
# 1Password Importer
#
# Reads files exported from 1Password and imports them into pass. Supports comma
# and tab delimited text files, as well as logins (but not other items) stored
# in the 1Password Interchange File (1PIF) format.
#
# Supports using the title (default) or URL as pass-name, depending on your
# preferred organization. Also supports importing metadata, adding them with
# `pass insert --multiline`; the username and URL are compatible with
# https://github.com/jvenant/passff.
require "optparse"
require "ostruct"
accepted_formats = [".txt", ".1pif"]
def filter_name(name, pattern, replacement)
if replacement == false
return name
end
name_filtered = name.gsub(pattern, replacement)
if name_filtered != name
puts "WARNING: Changed entry name from '" + name + "' to '" + name_filtered + "'"
name = name_filtered
end
name
end
# Default options
options = OpenStruct.new
options.force = false
options.name = :title
options.notes = true
options.meta = true
options.name_filter_pattern = /[\/\\]/
options.name_filter_replacement = "_"
optparse = OptionParser.new do |opts|
opts.banner = "Usage: #{opts.program_name}.rb [options] filename"
opts.on_tail("-h", "--help", "Display this screen") { puts opts; exit }
opts.on("-f", "--force", "Overwrite existing passwords") do
options.force = true
end
opts.on("-d", "--default [FOLDER]", "Place passwords into FOLDER") do |group|
options.group = group
end
opts.on("-n", "--name [PASS-NAME]", [:title, :url],
"Select field to use as pass-name: title (default) or URL") do |name|
options.name = name
end
opts.on("-m", "--[no-]meta",
"Import metadata and insert it below the password") do |meta|
options.meta = meta
end
opts.on("-z", "--[no-]name-filter-replacement REPLACEMENT",
"Replaces critical symbols (\\,/,...) with REPLACEMENT. Default replacement is a single underscore (_).") do |sym|
options.name_filter_replacement = sym
end
begin
opts.parse!
rescue OptionParser::InvalidOption
$stderr.puts optparse
exit
end
end
# Check for a valid filename
filename = ARGV.pop
unless filename
abort optparse.to_s
end
unless accepted_formats.include?(File.extname(filename.downcase))
abort "Supported file types: comma/tab delimited .txt files and .1pif files."
end
passwords = []
# Parse comma or tab delimited text
if File.extname(filename) =~ /.txt/i
require "csv"
# Very simple way to guess the delimiter
delimiter = ""
File.open(filename) do |file|
first_line = file.readline
if first_line =~ /,/
delimiter = ","
elsif first_line =~ /\t/
delimiter = "\t"
else
abort "Supported file types: comma/tab delimited .txt files and .1pif files."
end
end
# Import CSV/TSV
CSV.foreach(filename, {col_sep: delimiter, headers: true, header_converters: :symbol}) do |entry|
pass = {}
pass[:name] = "#{(options.group + "/") if options.group}#{filter_name(entry[options.name], options.name_filter_pattern, options.name_filter_replacement)}"
pass[:title] = entry[:title]
pass[:password] = entry[:password]
pass[:login] = entry[:username]
pass[:url] = entry[:url]
pass[:notes] = entry[:notes]
passwords << pass
end
# Parse 1PIF
elsif File.extname(filename) =~ /.1pif/i
require "json"
File.readlines(filename).each do |line|
next if line =~ /^\*\*\*/
entry = JSON.parse(line, {symbolize_names: true})
options.name = :location if options.name == :url
# Import 1PIF
next unless entry[:typeName] == "webforms.WebForm"
next if entry[:secureContents][:fields].nil?
pass = {}
pass[:name] = "#{(options.group + "/") if options.group}#{filter_name(entry[options.name], options.name_filter_pattern, options.name_filter_replacement)}"
pass[:title] = entry[:title]
begin
pass[:password] = entry[:secureContents][:fields].detect do |field|
field[:name] == "password" or field[:designation] == "password"
end[:value]
rescue
puts "WARNING: No password found in entry " + entry[:title]
pass[:password] = {}
end
begin
pass[:login] = entry[:secureContents][:fields].detect do |field|
field[:name] == "username" or field[:designation] == "username"
end[:value]
rescue
puts "WARNING: No username found in entry " + entry[:title]
pass[:login] = {}
end
pass[:url] = entry[:location]
pass[:notes] = entry[:secureContents][:notesPlain]
passwords << pass
end
end
puts "Read #{passwords.length} passwords."
errors = []
# Save the passwords
passwords.each do |pass|
IO.popen("pass insert #{"-f " if options.force}-m \"#{pass[:name]}\" > /dev/null", "w") do |io|
io.puts pass[:password]
if options.meta
io.puts "login: #{pass[:login]}" unless pass[:login].to_s.empty?
io.puts "url: #{pass[:url]}" unless pass[:url].to_s.empty?
io.puts pass[:notes] unless pass[:notes].to_s.empty?
end
end
if $? == 0
puts "Imported #{pass[:name]}"
else
$stderr.puts "ERROR: Failed to import #{pass[:name]}"
errors << pass
end
end
if errors.length > 0
$stderr.puts "Failed to import #{errors.map {|e| e[:name]}.join ", "}"
$stderr.puts "Check the errors. Make sure these passwords do not already "\
"exist. If you're sure you want to overwrite them with the "\
"new import, try again with --force."
end