Class: RubeePass

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

Defined Under Namespace

Modules: Header, InnerHeader, Magic, StreamAlgorithm Classes: AttachmentDecoder, Cipher, Entry, Error, Group, ProtectedDecryptor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RubeePass.



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/rubeepass.rb', line 320

def initialize(kdbx, password, keyfile = nil, hilight = false)
    @@hilight = hilight
    @kdbx = Pathname.new(kdbx).expand_path
    @keyfile = nil
    @keyfile = Pathname.new(keyfile).expand_path if (keyfile)
    @password = password

    if (@kdbx.nil?)
        raise RubeePass::Error::FileNotFound.new("null")
    elsif (!@kdbx.exist?)
        raise RubeePass::Error::FileNotFound.new(@kdbx)
    elsif (!@kdbx.readable?)
        raise RubeePass::Error::FileNotReadable.new(@kdbx)
    end

    if (@keyfile)
        if (!@keyfile.exist?)
            raise RubeePass::Error::FileNotFound.new(@keyfile)
        elsif (!@keyfile.readable?)
            raise RubeePass::Error::FileNotReadable.new(@keyfile)
        end
    end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/rubeepass.rb', line 377

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

#attachment_decoderObject (readonly)

Returns the value of attribute attachment_decoder.



54
55
56
# File 'lib/rubeepass.rb', line 54

def attachment_decoder
  @attachment_decoder
end

#dbObject (readonly)

Returns the value of attribute db.



55
56
57
# File 'lib/rubeepass.rb', line 55

def db
  @db
end

#protected_decryptorObject (readonly)

Returns the value of attribute protected_decryptor.



56
57
58
# File 'lib/rubeepass.rb', line 56

def protected_decryptor
  @protected_decryptor
end

#xmlObject (readonly)

Returns the value of attribute xml.



57
58
59
# File 'lib/rubeepass.rb', line 57

def xml
  @xml
end

Class Method Details

.hilight?Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/rubeepass.rb', line 100

def self.hilight?
    @@hilight ||= false
    return @@hilight
end

Instance Method Details

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



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

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



92
93
94
95
96
97
98
# File 'lib/rubeepass.rb', line 92

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

#copy_to_clipboard(string, err = true) ⇒ Object



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

def copy_to_clipboard(string, err = true)
    string = "" if (string.nil?)
    if (OS::Underlying.windows?)
        puts "Your OS is not currently supported!" if (err)
        return
    end

    return if (ENV["DISPLAY"].nil? || ENV["DISPLAY"].empty?)

    echo = ScoobyDoo.where_are_you("echo")

    if (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} -n #{string.shellescape} | #{cp}")
        else
            if (err)
                puts "Please install reattach-to-user-namespace!"
            end
            return
        end
    elsif (OS.posix?)
        xclip = ScoobyDoo.where_are_you("xclip")
        xsel = ScoobyDoo.where_are_you("xsel")

        ["clipboard", "primary", "secondary"].each do |sel|
            cp = nil
            if (xclip)
                # string = " \x7F" if (string.empty?)
                cp = "xclip -i -selection #{sel}"
            elsif (xsel)
                cp = "xsel -i --#{sel}"
            end

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

#export(export_file, format) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/rubeepass.rb', line 293

def export(export_file, format)
    start_opening

    File.open(export_file, "w") do |f|
        case format
        when "gzip"
            gz = Zlib::GzipWriter.new(f)
            gz.write(@xml)
            gz.close
        when "xml"
            f.write(@xml)
        end
    end
end

#find_group(path) ⇒ Object



308
309
310
# File 'lib/rubeepass.rb', line 308

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

#find_group_like(path) ⇒ Object



312
313
314
# File 'lib/rubeepass.rb', line 312

def find_group_like(path)
    return @db.find_group(path, true)
end

#fuzzy_find(input) ⇒ Object



316
317
318
# File 'lib/rubeepass.rb', line 316

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

#openObject



395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/rubeepass.rb', line 395

def open
    start_opening

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

    parse_xml

    return self
end

#pwnedpasswords(group = @db) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/rubeepass.rb', line 425

def pwnedpasswords(group = @db)
    return [] if (group.nil?)

    pwned = Array.new
    group.groups.each do |name, subgroup|
        pwned.concat(pwnedpasswords(subgroup))
    end
    group.entries.each do |name, entry|
        pwned.push(entry) if (entry.is_pwned?)
    end

    return pwned
end

#to_sObject



559
560
561
# File 'lib/rubeepass.rb', line 559

def to_s
    return @db.to_s
end

#wait_to_exitObject



563
564
565
566
567
568
569
570
# File 'lib/rubeepass.rb', line 563

def wait_to_exit
    return if (@thread.nil?)
    begin
        @thread.join
    rescue Interrupt
        puts
    end
end