Class: RubeePass

Inherits:
Object
  • Object
show all
Defined in:
lib/rubeepass.rb

Defined Under Namespace

Classes: Entry, Error, Group, ProtectedDecryptor

Constant Summary collapse

@@END_OF_HEADER =
0
@@COMMENT =
1
@@CIPHER_ID =
2
@@COMPRESSION =
3
@@MASTER_SEED =
4
@@TRANSFORM_SEED =
5
@@TRANSFORM_ROUNDS =
6
@@ENCRYPTION_IV =
7
@@PROTECTED_STREAM_KEY =
8
@@STREAM_START_BYTES =
9
@@INNER_RANDOM_STREAM_ID =
10
@@MAGIC_SIG1 =
0x9aa2d903
@@MAGIC_SIG2 =
0xb54bfb67
@@VERSION =
0x00030000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kdbx, password, keyfile = nil) ⇒ RubeePass

Returns a new instance of RubeePass.



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rubeepass.rb', line 161

def initialize(kdbx, password, keyfile = nil)
    @aes_iv = nil
    @aes_key = nil
    @db = nil
    @gzip = nil
    @header = nil
    @kdbx = kdbx
    @key = nil
    @keyfile = keyfile
    @password = password
    @xml = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/rubeepass.rb', line 219

def method_missing(method_name, *args)
    if (method_name.to_s.match(/^clear_clipboard_after_/))
        mn = method_name.to_s.gsub!(/^clear_clipboard_after_/, "")
        case mn
        when /^[0-9]+_sec(ond)?s$/
            time = mn.gsub(/_sec(ond)?s$/, "").to_i
            clear_clipboard(time)
        when /^[0-9]+_min(ute)?s$/
            time = mn.gsub(/_min(ute)?s$/, "").to_i
            clear_clipboard(time * 60)
        else
            super
        end
    else
        super
    end
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



27
28
29
# File 'lib/rubeepass.rb', line 27

def db
  @db
end

#gzipObject (readonly)

Returns the value of attribute gzip.



28
29
30
# File 'lib/rubeepass.rb', line 28

def gzip
  @gzip
end

#protected_decryptorObject (readonly)

Returns the value of attribute protected_decryptor.



29
30
31
# File 'lib/rubeepass.rb', line 29

def protected_decryptor
  @protected_decryptor
end

#xmlObject (readonly)

Returns the value of attribute xml.



30
31
32
# File 'lib/rubeepass.rb', line 30

def xml
  @xml
end

Instance Method Details

#absolute_path(to, from = "/") ⇒ Object



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
# File 'lib/rubeepass.rb', line 32

def absolute_path(to, from = "/")
    return "/" if (to.nil? || to.empty? || (to == "/"))
    from = "/" if (to.start_with?("/"))

    path = Array.new

    from.split("/").each do |group|
        next if (group.empty?)
        case group
        when "."
            # Do nothing
        when ".."
            path.pop
        else
            path.push(group)
        end
    end

    to.split("/").each do |group|
        next if (group.empty?)
        case group
        when "."
            # Do nothing
        when ".."
            path.pop
        else
            path.push(group)
        end
    end

    return "/#{path.join("/")}"
end

#clear_clipboard(time = 0) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/rubeepass.rb', line 65

def clear_clipboard(time = 0)
    @thread.kill if (@thread)
    @thread = Thread.new do
        sleep time
        copy_to_clipboard("")
    end
end

#copy_to_clipboard(string) ⇒ Object



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
# File 'lib/rubeepass.rb', line 73

def copy_to_clipboard(string)
    string = " \x7F" if (string.nil? || string.empty?)
    if (OS::Underlying.windows?)
        puts "Your OS is not currently supported!"
        return
    elsif (OS.mac?)
        pbcopy = ScoobyDoo.where_are_you("pbcopy")
        rn = ScoobyDoo.where_are_you("reattach-to-user-namespace")

        cp = pbcopy
        if (ENV["TMUX"])
            cp = nil
            cp = "#{rn} #{pbcopy}" if (rn)
        end

        if (cp)
            system("echo #{string.shellescape} | #{cp}")
        else
            puts "Please install reattach-to-user-namespace!"
            return
        end
    elsif (OS.posix?)
        xclip = ScoobyDoo.where_are_you("xclip")
        xsel = ScoobyDoo.where_are_you("xsel")

        cp = nil
        if (xclip)
            cp = "xclip -i -selection clipboard"
        elsif (xsel)
            cp = "xsel -i --clipboard"
        end

        if (cp)
            system("echo #{string.shellescape} | #{cp}")
        else
            puts "Please install either xclip or xsel!"
            return
        end
    else
        puts "Your OS is not currently supported!"
        return
    end
end

#find_group(path) ⇒ Object



140
141
142
# File 'lib/rubeepass.rb', line 140

def find_group(path)
    return @db.find_group(path)
end

#fuzzy_find(input) ⇒ Object



144
145
146
# File 'lib/rubeepass.rb', line 144

def fuzzy_find(input)
    return @db.fuzzy_find(input)
end

#openObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/rubeepass.rb', line 237

def open
    file = File.open(@kdbx)

    read_magic_and_version(file)
    read_header(file)
    join_key_and_keyfile
    derive_aes_key
    read_gzip(file)

    file.close

    @protected_decryptor = ProtectedDecryptor.new(
        Digest::SHA256.digest(
            @header[@@PROTECTED_STREAM_KEY]
        ),
        ["E830094B97205D2A"].pack("H*")
    )

    extract_xml
    parse_xml

    return self
end

#to_sObject



484
485
486
# File 'lib/rubeepass.rb', line 484

def to_s
    return @db.to_s
end