summaryrefslogtreecommitdiff
path: root/geheim.rb
blob: d095d0617c9c903e04738db5ad166c36740d8397 (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#!/usr/bin/env ruby

require 'base64'
require 'digest'
require 'digest/sha2'
require 'fileutils'
require 'io/console'
require 'openssl'
require 'json'

VERSION = 'v0.2.0'

# Configuration
class Config
  config_file = "#{ENV['HOME']}/.config/geheim.json"

  default_config = {
    data_dir: "#{ENV['HOME']}/git/geheimlager".freeze,
    export_dir: "#{ENV['HOME']}/.geheimlagerexport".freeze,
    key_file: "#{ENV['HOME']}/.geheimlager.key".freeze,
    key_length: 32,
    enc_alg: 'AES-256-CBC'.freeze,
    add_to_iv: 'Hello world'.freeze,
    edit_cmd: 'hx'.freeze,
    # edit_cmd: "nvim --cmd 'set noswapfile' --cmd 'set nobackup' --cmd 'set nowritebackup'".freeze,
    gnome_clipboard_cmd: 'gpaste-client'.freeze,
    macos_clipboard_cmd: 'pbcopy'.freeze,
    sync_repos: %w[git1 git2].freeze
  }

  @@config = begin
    default_config.merge(JSON.parse(File.read(config_file))).freeze
  rescue StandardError => e
    puts "Unable to read #{config_file}, using defaults! #{e}"
    default_config.freeze
  end

  def self.method_missing(method_name)
    raise "Unknown config key '#{method_name}'" unless respond_to_missing?(method_name)

    @@config[method_name]
  end

  def self.respond_to_missing?(method_name)
    @@config.key?(method_name)
  end
end

# Logging capabilities
module Log
  def log(message)
    out message, '>'
  end

  def warn(message)
    out message, 'WARN'
  end

  def prompt(message)
    out message, '<', :nonl
  end

  def fatal(message)
    out message, 'FATAL'
    exit 3
  end

  private

  def out(message, prefix, flag = :none)
    message = message.to_s unless message.instance_of?(String)
    message.split("\n").each do |line|
      if flag == :nonl
        print "#{prefix} #{line}"
      else
        puts "#{prefix} #{line}"
      end
    end
  end
end

# Git versioning
module Git
  include Log

  def initialize
    super()
    @wd = Dir.pwd
  end

  def git_add(file:)
    dirname = File.dirname(file)
    basename = File.basename(file)
    Dir.chdir(dirname)
    log `git add "#{basename}"`
    Dir.chdir(@wd)
  end

  def git_rm(file:)
    dirname = File.dirname(file)
    basename = File.basename(file)
    Dir.chdir(dirname)
    log `git rm "#{basename}"`
    Dir.chdir(@wd)
  end

  def git_status
    Dir.chdir(Config.data_dir)
    log `git status`
    Dir.chdir(@wd)
  end

  def git_commit
    Dir.chdir(Config.data_dir)
    log `git commit -a -m 'Changing stuff, not telling what in commit history'`
    Dir.chdir(@wd)
  end

  def git_reset
    Dir.chdir(Config.data_dir)
    log `git reset --hard`
    Dir.chdir(@wd)
  end

  def git_sync
    log "Synchronising #{Config.data_dir}"
    Dir.chdir(Config.data_dir)
    Config.sync_repos.each do |repo|
      log `git pull #{repo} master`
      log `git push #{repo} master`
    end
    log `git status`
    Dir.chdir(@wd)
  end
end

# Encryption functionality
module Encryption
  include Log
  @@key = nil

  def initialize
    super()
    return unless @@key.nil?

    pin = read_pin
    # Set up initialization vector
    iv = "#{pin * 2}#{Config.add_to_iv}#{pin * 2}"
    @@iv = iv[0..15]
    # ... and the encryption key!
    @@key = enforce_key_length(File.read(Config.key_file), Config.key_length)
  end

  def encrypt(plain:)
    aes = OpenSSL::Cipher::Cipher.new(Config.enc_alg)
    aes.encrypt
    aes.key = @@key
    aes.iv = @@iv

    encrypted = aes.update(plain)
    encrypted << aes.final

    encrypted
  end

  def decrypt(encrypted:)
    aes = OpenSSL::Cipher::Cipher.new(Config.enc_alg)
    aes.decrypt
    aes.key = @@key
    aes.iv = @@iv

    plain = aes.update(encrypted)
    plain << aes.final
    plain
  end

  def test
    plain_input = 'foo bar baz'
    encrypted = encrypt(plain: plain_input)
    plain = decrypt(encrypted: encrypted)
    pp plain == plain_input
  end

  private

  def enforce_key_length(key, force_size)
    new_key = key
    new_key += key while new_key.size < force_size
    new_key[0..force_size - 1]
  end

  def read_pin
    return ENV['PIN'] if ENV['PIN']

    prompt 'PIN: '
    return $stdin.gets.chomp if `uname`.include?('Android')

    $stdin.noecho(&:gets).chomp
  end
end

# Comitting a file
class CommitFile
  include Git
  include Log

  def commit_content(file:, content:, force: false)
    if File.exist?(file) && !force
      warn "#{file} already exists. Use 'force' flag to overwrite."
      return false
    end

    dirname = File.dirname(file)
    unless File.directory?(dirname)
      log "Creating #{dirname}"
      FileUtils.mkdir_p(dirname)
    end

    log "Writing #{file}"
    File.open(file, 'w') { |fd| fd.write(content) }
    git_add(file: file)
  end
end

# Clipboard support
module Clipboard
  include Log
  @clipboard_cmd = nil

  def initialize
    super()
    @clipboard_cmd =
      ENV['UNAME'] == 'Darwin' ? Config.macos_clipboard_cmd : Config.gnome_clipboard_cmd
  end

  def paste(data)
    fatal "Can't paste to clipboard" if @clipboard_cmd.nil?
    user, password, other = extract(data.to_s)
    read, write = IO.pipe
    spawn(@clipboard_cmd, in: read)
    write.write(password)
    puts other
    log "Pasted password for user '#{user}' to the clipboard"
  end

  private

  def extract(data)
    parts = data.match(/(?<User>\S+):(?<Password>\S+)/)
    cleared_data = data.gsub(/(\S+):\S+/, '\1:CENSORED')
    [parts['User'], parts['Password'], cleared_data]
  end
end

# Secret data store
class GeheimData < CommitFile
  include Encryption
  include Git
  include Log

  attr_accessor :data, :exported_path

  def initialize(data_file:, data: nil)
    super()

    @exported_path = nil
    @data_path = "#{Config.data_dir}/#{data_file}"
    @data = data.nil? ? decrypt(encrypted: File.read(@data_path)) : data
  rescue StandardError => e
    fatal e
  end

  def to_s
    "\t#{@data.gsub("\n", "\n\t")}\n"
  end

  def rm
    log "Deleting #{@data_path}"
    git_rm(file: @data_path)
  end

  def export(destination_file:)
    destination_dir = "#{Config.export_dir}/#{File.dirname(destination_file)}"
    unless File.directory?(destination_dir)
      log "Creating #{destination_dir}"
      FileUtils.mkdir_p(destination_dir)
    end

    destination_path = "#{destination_dir}/#{File.basename(destination_file)}"
    log "Exporting to #{destination_path}"
    File.open(destination_path, 'w') { |fd| fd.write(@data) }
    @exported_path = destination_path
  end

  def reimport_after_export
    @data = File.read(@exported_path)
    commit(force: true)
  end

  def commit(force: false)
    commit_content(file: @data_path, content: encrypt(plain: @data), force: force)
  end
end

# Data store's encrypted index
class Index < CommitFile
  attr_accessor :description, :data_file, :index_path

  include Encryption
  include Log

  def initialize(index_file:, description: nil)
    super()
    @data_file = index_file.sub('.index', '.data')
    @index_path = "#{Config.data_dir}/#{index_file}"
    @hash = File.basename(index_file).sub('.index', '')
    @description = description.nil? ? decrypt(encrypted: File.read(@index_path)) : description
  end

  def binary?
    if @description.include?('.txt')
      false
    elsif @description.include?('.README')
      false
    elsif @description.include?('.conf')
      false
    elsif @description.include?('.csv')
      false
    elsif @description.include?('.md')
      false
    else
      @description.include?('.')
    end
  end

  def get_data(data: nil)
    GeheimData.new(data_file: @data_file, data: data)
  end

  def to_s
    binary = binary? ? '(BINARY) ' : ''
    "#{@description}; #{binary}...#{@hash[-11...-1]}\n"
  end

  def <=>(other)
    @description <=> other.description
  end

  def rm
    log "Deleting #{@index_path}"
    git_rm(file: @index_path)
  end

  def commit(force: false)
    commit_content(file: @index_path, content: encrypt(plain: @description), force: force)
  end
end

# Secret store main class
class Geheim
  include Clipboard
  include Log

  def initialize
    super()
    unless File.directory?(Config.data_dir)
      log "Creating #{Config.data_dir}"
      FileUtils.mkdir_p(Config.data_dir)
    end
    @regex_cache = {}
  end

  def fzf(flag = :none)
    # Need to read an index first before opening the pipe to initialize
    # the encryption PIN.
    fzf = nil
    walk_indexes do |index|
      fzf = IO.popen('fzf', 'r+') if fzf.nil?
      fzf.write(index)
    end
    fzf.close_write
    match = fzf.read.chomp
    log match unless flag == :silent
    match.split(';').first
  end

  def search(search_term: nil, action: :none)
    ec = 1
    search_term = fzf(:silent) if search_term.nil?
    indexes = []
    walk_indexes(search_term: search_term) do |index|
      indexes << index
    end
    indexes.sort.each do |index|
      print index
      ec = 0
      case action
      when :cat, :paste
        if index.binary?
          log 'Not displaying/pasting binary data!'
          ec = 2
        elsif action == :paste
          paste(index.get_data)
        else
          puts index.get_data
        end
      when :pathexport
        index.get_data.export(destination_file: index.description)
      when :export
        destination_file = File.basename(index.description)
        index.get_data.export(destination_file: destination_file)
      when :open
        destination_file = File.basename(index.description)
        index.get_data.export(destination_file: destination_file)
        shred_file(file: open_exported(file: destination_file), delay: 0)
      when :edit
        destination_file = File.basename(index.description)
        data = index.get_data
        data.export(destination_file: destination_file)
        external_edit(file: destination_file)
        data.reimport_after_export
      end
      index.description
    end
    ec
  end

  def add(description:)
    hash = hash_path(description)

    log 'Data: '
    data = $stdin.gets.chomp
    index = Index.new(index_file: "#{hash}.index", description: description)
    data = index.get_data(data: data)

    data.commit
    index.commit
  end

  def import(description: nil, action: nil, file: nil, dest_dir: nil, force: false)
    src_path = file.gsub('//', '/').gsub(%r{^\./}, '')
    dest_path = if dest_dir.nil?
                  src_path
                elsif dest_dir.include?('.')
                  dest_dir
                else
                  "#{dest_dir}/#{File.basename(file)}".gsub('//', '/')
                end

    hash = hash_path(dest_path)

    fatal "#{file} does not exist!" unless File.exist?(src_path)
    log "Importing #{src_path} -> #{dest_path}"
    data = File.read(src_path)
    shred_file(file: src_path) if action == :newtxt
    description = dest_path if description.nil?

    index = Index.new(index_file: "#{hash}.index", description: description)
    data = index.get_data(data: data)

    data.commit(force: force)
    index.commit(force: force)
  end

  def import_recursive(directory:, dest_dir: nil)
    Dir.glob("#{directory}/**/*").each do |source_file|
      next if File.directory?(source_file)

      file = source_file.sub("#{directory}/", '')
      import(description: file, action: :import, file: source_file, dest_dir: dest_dir)
    end
  end

  def rm(search_term:)
    indexes = []
    walk_indexes(search_term: search_term) do |index|
      indexes << index
    end
    indexes.sort.each do |index|
      loop do
        log index
        prompt 'You really want to delete this? (y/n): '
        case $stdin.gets.chomp
        when 'y'
          data = index.get_data
          data.rm
          index.rm
          break
        when 'n'
          break
        end
      end
    end
    0
  end

  def shred_all_exported
    log 'Shredding all exported files'
    ec = 0
    Dir.glob("#{Config.export_dir}/*").each do |file|
      next unless File.file?(file)

      if (ec_ = shred_file(file: file)).positive?
        ec = ec_
      end
    end
    ec
  end

  private

  def shred_file(file:, delay: 0)
    sleep(delay) if delay.positive?
    `which shred`
    if $?.success?
      run_command("shred -vu #{file}")
    else
      run_command("rm -Pfv #{file}")
    end
  end

  def open_exported(file:)
    file_path = "#{Config.export_dir}/#{file}"

    case ENV['UNAME']
    when 'Darwin'
      run_command("open #{file_path}")
    when 'Microsoft'
      run_command("winopen #{file_path}")
    when 'Linux'
      run_command("evince #{file_path}")
    else
      # Termux (Android)
      run_command("termux-open #{file_path}")
    end
    file_path
  end

  def external_edit(file:)
    file_path = "#{Config.export_dir}/#{file}"
    edit_cmd = "#{Config.edit_cmd} #{file_path}"
    log edit_cmd
    system(edit_cmd)
    file_path
  end

  def run_command(cmd)
    log "#{cmd}: #{`#{cmd}`}"
    $?.exitstatus
  end

  def walk_indexes(search_term: nil)
    @regex_cache[search_term] = Regexp.new(/#{search_term}/) unless @regex_cache.key?(search_term)
    regex = @regex_cache[search_term]
    Dir.glob("#{Config.data_dir}/**/*.index").each do |index_file|
      index = Index.new(index_file: index_file.sub(Config.data_dir, ''))
      yield index if search_term.nil? || index.description.force_encoding('UTF-8').match(regex)
    end
  end

  def hash_path(path_string)
    path = []
    path_string.gsub('//', '/').split('/').each do |part|
      path << Digest::SHA256.hexdigest(part)
    end
    path.join('/')
  end
end

# Command line interface
class CLI
  include Git
  include Log

  def initialize(interactive: false)
    super()
    @interactive = interactive
  end

  def commands
    puts 'ls'
    puts 'search'
    puts 'cat'
    puts 'paste'
    puts 'get'
    puts 'add'
    puts 'export'
    puts 'pathexport'
    puts 'open'
    puts 'edit'
    puts 'import'
    puts 'import_r'
    puts 'rm'
    puts 'sync'
    puts 'status'
    puts 'commit'
    puts 'reset'
    puts 'fullcommit'
    puts 'shred'
    puts 'version'
    puts 'commands'
    puts 'help'
    puts 'shell'
    puts 'exit'
    puts 'last'
    0
  end

  def help
    log <<-HELP
      ls
      SEARCHTERM
      search SEARCHTERM
      cat SEARCHTERM
      get SEARCHTERM
      add DESCRIPTION
      export|pathexport|open|edit FILE
      import FILE [DEST_DIRECTORY] [force]
      import_r DIRECTORY [DEST_DIRECTORY]
      rm SEARCHTERM
      sync|status|commit|reset|fullcommit
      shred
      version
      commands
      help
      shell
    HELP
    0
  end

  def shell_loop(argv)
    last_result = nil
    ec = 0

    loop do
      if argv.length == 0 or @interactive
        @interactive ||= true
        print '% '
        argv = $stdin.gets.chomp.split(' ')
      end

      geheim = Geheim.new
      action = argv.first
      search_term = argv.length < 2 ? last_result : argv[1]

      ec = case action
           when 'ls'
             geheim.search(search_term: '.')
           when 'search'
             geheim.search(search_term: search_term)
           when 'cat'
             geheim.search(search_term: search_term, action: :cat)
           when 'paste'
             geheim.search(search_term: search_term, action: :paste)
           when 'export'
             geheim.search(search_term: search_term, action: :export)
           when 'pathexport'
             geheim.search(search_term: search_term, action: :pathexport)
           when 'edit'
             geheim.search(search_term: search_term, action: :edit)
           when 'open'
             geheim.search(search_term: search_term, action: :open)
           when 'add'
             geheim.add(description: search_term)
           when 'import'
             geheim.import(file: search_term, dest_dir: argv[2], force: !argv[3].nil?)
           when 'import_r'
             geheim.import_recursive(directory: search_term, dest_dir: argv[2])
           when 'rm'
             geheim.rm(search_term: search_term)
           when 'help'
             help
           when 'shell'
             @interactive = true
             log 'Switching to interactive mode'
           when 'exit'
             @interactive = false
             log 'Good bye'
           when 'status'
             git_status
           when 'commit'
             git_commit
           when 'reset'
             git_reset
           when 'sync'
             git_sync
           when 'fullcommit'
             git_sync
             git_commit
             git_sync
           when 'shred'
             geheim.shred_all_exported
           when 'version'
             log "geheim #{VERSION}"
             0
           when 'commands'
             commands
           when 'last'
             puts last_result
             last_result
           when nil
             last_result = geheim.fzf
           else
             last_result = geheim.search(search_term: action)
           end
      break unless @interactive
    end

    ec
  end
end

exit(CLI.new.shell_loop(ARGV))