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

165 lines
3.7 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'
require "#{APP_ROOT}/MPW.rb"
2013-07-16 22:36:41 +02:00
class Cli
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]: ")
file_pwd = ask("Enter te path to password file [default=#{Dir.home()}/.mpw.pwd]: ")
timeout_pwd = ask("Enter the timeout (in seconde) to GPG password [default=300]: ")
2013-07-18 22:12:45 +02:00
if @m.setup(key, file_gpg, file_pwd, 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-07-17 22:31:28 +02:00
def decrypt()
if not @m.checkFilePassword()
passwd = ask("Password GPG: ") {|q| q.echo = false}
return @m.decrypt(passwd)
else
return @m.decrypt()
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-07-17 22:31:28 +02:00
def display(search, protocol=nil)
result = @m.search(search, 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|
puts "# --------------------"
puts "# Id: #{r[MPW::ID]}"
puts "# Server: #{r[MPW::SERVER]}"
puts "# Type: #{r[MPW::PROTOCOL]}"
puts "# Login: #{r[MPW::LOGIN]}"
puts "# Password: #{r[MPW::PASSWORD]}"
puts "# Port: #{r[MPW::PORT]}"
puts "# Comment: #{r[MPW::COMMENT]}"
end
else
puts "Nothing result!"
2013-07-16 22:36:41 +02:00
end
end
2013-07-17 22:31:28 +02:00
def add()
row = Array.new()
puts "# Add a new item"
puts "# --------------------"
server = ask("Enter the server name or ip: ")
protocol = ask("Enter the type of connection (ssh, web, other): ")
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): ")
@m.add(server, protocol, login, passwd, port, comment)
if @m.encrypt()
puts "Item has been added!"
else
puts "ERROR: #{@m.error_msg}"
end
end
2013-07-16 22:36:41 +02:00
2013-07-17 22:31:28 +02:00
def update(id)
row = @m.searchById(id)
if not row.empty?
puts "# Add a new password"
puts "# --------------------"
server = ask("Enter the server name or ip [#{row[MPW::SERVER]}]: ")
protocol = ask("Enter the type of connection [#{row[MPW::PROTOCOL]}]: ")
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, server, protocol, login, passwd, port, comment)
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
def remove(id, force=false)
if not force
confirm = ask("Are you sur to remove the item: #{id} ? (y/N) ")
if confirm =~ /^(y|yes|YES|Yes|Y)$/
force = true
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-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
def import(file)
if @m.import(file)
if @m.encrypt()
puts "The import is succesfull!"
else
puts "ERROR: #{@m.error_msg}"
end
else
puts "ERROR: #{@m.error_msg}"
end
end
2013-07-17 22:31:28 +02:00
def ssh(search)
@m.ssh(search)
2013-07-16 22:36:41 +02:00
end
end