1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-02-20 01:50:04 +00:00
mpw/lib/Cli.rb

305 lines
7.3 KiB
Ruby
Raw Normal View History

2013-07-16 22:36:41 +02:00
#!/usr/bin/ruby
# author: nishiki
# mail: nishiki@yaegashi.fr
2013-07-17 22:31:28 +02:00
# info: a simple script who m your passwords
2013-07-16 22:36:41 +02:00
require 'rubygems'
require 'highline/import'
2013-07-18 21:56:45 +02:00
require 'pathname'
2013-09-04 19:48:06 +02:00
require "#{APP_ROOT}/lib/MPW.rb"
2013-07-16 22:36:41 +02:00
class Cli
2013-08-25 10:07:39 +02:00
# Constructor
2013-07-16 22:36:41 +02:00
def initialize()
2013-07-17 22:31:28 +02:00
@m = MPW.new()
if not @m.checkconfig()
2013-07-16 22:36:41 +02:00
self.setup()
2013-07-17 22:31:28 +02:00
end
if not self.decrypt()
puts "ERROR: #{@m.error_msg}"
exit 2
2013-07-16 22:36:41 +02:00
end
end
# Create a new config file
def setup()
2013-07-18 22:16:53 +02:00
puts "# Setup a new config file"
puts "# --------------------"
2013-07-16 22:36:41 +02:00
key = ask("Enter the GPG key: ")
file_gpg = ask("Enter the path to encrypt file [default=#{Dir.home()}/.mpw.gpg]: ")
2013-09-08 20:04:31 +02:00
timeout_pwd = ask("Enter the timeout (in seconde) to GPG password [default=60]: ")
2013-07-16 22:36:41 +02:00
2013-09-08 20:04:31 +02:00
if @m.setup(key, file_gpg, timeout_pwd)
2013-07-17 22:31:28 +02:00
puts "The config file has been created!"
else
puts "ERROR: #{@m.error_msg}"
2013-07-16 22:36:41 +02:00
end
2013-07-17 22:31:28 +02:00
end
2013-07-16 22:36:41 +02:00
2013-08-25 10:07:39 +02:00
# Request the GPG password and decrypt the file
2013-07-17 22:31:28 +02:00
def decrypt()
2013-09-08 20:04:31 +02:00
@passwd = ask("Password GPG: ") {|q| q.echo = false}
return @m.decrypt(@passwd)
2013-07-17 22:31:28 +02:00
end
2013-07-16 22:36:41 +02:00
2013-08-25 10:07:39 +02:00
# Display the query's result
# @args: search -> the string to search
# protocol -> search from a particular protocol
2013-09-11 20:47:51 +02:00
def display(search, protocol=nil, group=nil, format=nil)
result = @m.search(search, group, protocol)
2013-07-16 22:36:41 +02:00
2013-07-17 22:31:28 +02:00
if not result.empty?
result.each do |r|
if format.nil? || !format
2013-09-08 20:04:31 +02:00
self.displayFormat(r)
else
2013-09-08 20:04:31 +02:00
self.displayFormatAlt(r)
end
2013-07-17 22:31:28 +02:00
end
else
puts "Nothing result!"
2013-07-16 22:36:41 +02:00
end
end
# Display an item in the default format
2013-09-02 19:14:33 +02:00
# @args: item -> an array with the item information
def displayFormat(item)
puts "# --------------------"
puts "# Id: #{item[MPW::ID]}"
puts "# Name: #{item[MPW::NAME]}"
puts "# Group: #{item[MPW::GROUP]}"
puts "# Server: #{item[MPW::SERVER]}"
puts "# Protocol: #{item[MPW::PROTOCOL]}"
2013-09-02 19:14:33 +02:00
puts "# Login: #{item[MPW::LOGIN]}"
puts "# Password: #{item[MPW::PASSWORD]}"
puts "# Port: #{item[MPW::PORT]}"
puts "# Comment: #{item[MPW::COMMENT]}"
end
# Display an item in the alternative format
# @args: item -> an array with the item information
def displayFormatAlt(item)
item[MPW::PORT].nil? ? (port = '') : (port = ":#{item[MPW::PORT]}")
if item[MPW::PASSWORD].nil? || item[MPW::PASSWORD].empty?
2013-09-04 19:48:06 +02:00
if item[MPW::LOGIN].include('@')
puts "# #{item[MPW::ID]} #{item[MPW::PROTOCOL]}://#{item[MPW::LOGIN]}@#{item[MPW::SERVER]}#{port}"
else
puts "# #{item[MPW::ID]} #{item[MPW::PROTOCOL]}://{#{item[MPW::LOGIN]}}@#{item[MPW::SERVER]}#{port}"
end
else
puts "# #{item[MPW::ID]} #{item[MPW::PROTOCOL]}://{#{item[MPW::LOGIN]}:#{item[MPW::PASSWORD]}}@#{item[MPW::SERVER]}#{port}"
end
end
2013-08-25 10:07:39 +02:00
# Form to add a new item
2013-07-17 22:31:28 +02:00
def add()
row = Array.new()
puts "# Add a new item"
puts "# --------------------"
name = ask("Enter the name: ")
2013-09-09 21:48:42 +02:00
group = ask("Enter the group [default=NoGroup]: ")
server = ask("Enter the hostname or ip: ")
protocol = ask("Enter the protocol of the connection (ssh, http, other): ")
2013-07-17 22:31:28 +02:00
login = ask("Enter the login connection: ")
passwd = ask("Enter the the password: ")
port = ask("Enter the connection port (optinal): ")
comment = ask("Enter a comment (optinal): ")
if @m.add(name, group, server, protocol, login, passwd, port, comment)
if @m.encrypt()
puts "Item has been added!"
else
puts "ERROR: #{@m.error_msg}"
end
2013-07-17 22:31:28 +02:00
else
puts "ERROR: #{@m.error_msg}"
end
end
2013-07-16 22:36:41 +02:00
2013-08-25 10:07:39 +02:00
# Update an item
# @args: id -> the item's id
2013-07-17 22:31:28 +02:00
def update(id)
row = @m.searchById(id)
if not row.empty?
puts "# Update an item"
2013-07-17 22:31:28 +02:00
puts "# --------------------"
name = ask("Enter the name [#{row[MPW::NAME]}]: ")
group = ask("Enter the group [#{row[MPW::GROUP]}]: ")
server = ask("Enter the hostname or ip [#{row[MPW::SERVER]}]: ")
protocol = ask("Enter the protocol of the connection [#{row[MPW::PROTOCOL]}]: ")
2013-07-17 22:31:28 +02:00
login = ask("Enter the login connection [#{row[MPW::LOGIN]}]: ")
passwd = ask("Enter the the password: ")
port = ask("Enter the connection port [#{row[MPW::PORT]}]: ")
comment = ask("Enter a comment [#{row[MPW::COMMENT]}]: ")
if @m.update(id, name, group, server, protocol, login, passwd, port, comment)
2013-07-17 22:31:28 +02:00
if @m.encrypt()
puts "Item has been updated!"
else
puts "ERROR: #{@m.error_msg}"
end
else
puts "Nothing item has been updated!"
2013-07-16 22:36:41 +02:00
end
2013-07-17 22:31:28 +02:00
else
puts "Nothing result!"
end
end
2013-07-16 22:36:41 +02:00
2013-08-25 10:07:39 +02:00
# Remove an item
# @args: id -> the item's id
# force -> no resquest a validation
def remove(id, force=false)
if not force
2013-08-26 21:23:35 +02:00
result = @m.searchById(id)
if result.length > 0
2013-09-08 20:04:31 +02:00
self.displayFormat(result)
2013-08-26 21:23:35 +02:00
confirm = ask("Are you sure to remove the item: #{id} ? (y/N) ")
if confirm =~ /^(y|yes|YES|Yes|Y)$/
force = true
end
else
puts "Nothing result!"
end
end
if force
if @m.remove(id)
if @m.encrypt()
puts "The item #{id} has been removed!"
else
puts "ERROR: #{@m.error_msg}"
end
2013-07-17 22:31:28 +02:00
else
puts "Nothing item has been removed!"
2013-07-17 22:31:28 +02:00
end
2013-07-16 22:36:41 +02:00
end
2013-07-17 22:31:28 +02:00
end
2013-07-16 22:36:41 +02:00
2013-08-25 10:07:39 +02:00
# Export the items in a CSV file
# @args: file -> the destination file
2013-07-25 19:51:43 +02:00
def export(file)
if @m.export(file)
puts "The export in #{file} is succesfull!"
else
puts "ERROR: #{@m.error_msg}"
end
end
2013-08-25 10:07:39 +02:00
# Import items from a CSV file
# @args: file -> the import file
2013-08-26 22:19:37 +02:00
# force -> no resquest a validation
def import(file, force=false)
result = @m.importPreview(file)
if not force
if result.is_a?(Array) && !result.empty?
result.each do |r|
2013-09-08 20:04:31 +02:00
self.displayFormat(r)
2013-08-26 22:19:37 +02:00
end
confirm = ask("Are you sure to import this file: #{file} ? (y/N) ")
if confirm =~ /^(y|yes|YES|Yes|Y)$/
force = true
end
else
puts "No data to import!"
end
end
if force
if @m.import(file) && @m.encrypt()
2013-07-25 19:51:43 +02:00
puts "The import is succesfull!"
else
puts "ERROR: #{@m.error_msg}"
end
end
end
2013-09-08 17:56:10 +02:00
# Interactive mode
def interactive()
2013-09-09 21:48:42 +02:00
group = nil
2013-09-08 20:04:31 +02:00
last_access = Time.now.to_i
2013-09-08 17:56:10 +02:00
while true
2013-09-08 20:04:31 +02:00
if @m.timeout_pwd < Time.now.to_i - last_access
passwd_confirm = ask("Password GPG: ") {|q| q.echo = false}
if @passwd.eql?(passwd_confirm)
last_access = Time.now.to_i
else
puts 'Bad password!'
next
end
else
last_access = Time.now.to_i
end
2013-09-08 17:56:10 +02:00
command = ask("<mpw> ").split(' ')
case command[0]
when 'display', 'show', 'd', 's'
if !command[1].nil? && !command[1].empty?
2013-09-09 21:48:42 +02:00
self.display(command[1], group, command[2])
2013-09-08 17:56:10 +02:00
end
when 'add', 'a'
2013-09-08 20:04:31 +02:00
add()
2013-09-08 17:56:10 +02:00
when 'update', 'u'
if !command[1].nil? && !command[1].empty?
2013-09-08 20:04:31 +02:00
self.update(command[1])
2013-09-08 17:56:10 +02:00
end
when 'remove', 'delete', 'r', 'd'
if !command[1].nil? && !command[1].empty?
2013-09-08 20:04:31 +02:00
self.remove(command[1])
2013-09-08 17:56:10 +02:00
end
2013-09-09 21:48:42 +02:00
when 'group', 'g'
if !command[1].nil? && !command[1].empty?
group = command[1]
else
group = nil
end
2013-09-08 17:56:10 +02:00
when 'help', 'h', '?'
puts '# Help'
puts '# --------------------'
puts '# Display an item:'
puts '# display SEARCH'
puts '# show SEARCH'
puts '# s SEARCH'
puts '# d SEARCH'
puts '# Add an new item:'
puts '# add'
puts '# a'
puts '# Update an item:'
puts '# update ID'
puts '# u ID'
puts '# Remove an item:'
puts '# remove ID'
puts '# delete ID'
puts '# r ID'
puts '# d ID'
puts '# Quit the program:'
puts '# quit'
puts '# exit'
puts '# q'
when 'quit', 'exit', 'q'
break
2013-09-09 21:48:42 +02:00
else
2013-09-09 22:45:14 +02:00
if !command[0].nil? && !command[0].empty?
2013-09-09 21:48:42 +02:00
puts 'Unknow command!'
end
2013-09-08 17:56:10 +02:00
end
end
end
2013-07-16 22:36:41 +02:00
end