def self.run(args = Array.new)
raise "The SIMP Passgen Tool requires at least one argument to work" if args.empty?
super
return if @help_requested
unless @target_dir
env_path = ::Utils.puppet_info[:environment_path]
if File.directory?(env_path)
@target_dir = File.join(env_path, 'simp/simp_autofiles/gen_passwd')
else
raise(OptionParser::ParseError, 'Could not find a target passgen directory, please specify one with the `-d` option')
end
end
raise "Target directory '#{@target_dir}' does not exist" unless File.directory?(@target_dir)
begin
Dir.chdir(@target_dir) do
@user_names = Dir.glob("*").select do |x|
File.file?(x) && (x !~ /\..+$/)
end
end
rescue => err
raise "Error occurred while accessing '#{@target_dir}':\n Causing Error: #{err}"
end
if @show_list
puts "Usernames:\n\t#{@user_names.join("\n\t")}"
puts
end
@show_users.each do |user|
if @user_names.include?(user)
Dir.chdir(@target_dir) do
puts "Username: #{user}"
current_password = File.open("#{@target_dir}/#{user}", 'r').gets
puts " Current: #{current_password}"
last_password = nil
last_password_file = "#{@target_dir}/#{user}.last"
if File.exists?(last_password_file)
last_password = File.open(last_password_file, 'r').gets
end
puts " Previous: #{last_password}" if last_password
end
else
raise "Invalid username '#{user}' selected.\n\n Valid: #{@user_names.join(', ')}"
end
puts
end
@set_users.each do |user|
password_filename = "#{@target_dir}/#{user}"
puts "Username: #{user}"
password = Utils.get_password
if File.exists?(password_filename)
if Utils.yes_or_no("Would you like to rotate the old password?", false)
begin
FileUtils.mv(password_filename, password_filename + '.last')
rescue => err
raise "Error occurred while moving '#{password_filename}' to '#{password_filename + '.last'}'\n Causing Error: #{err}"
end
end
end
begin
File.open(password_filename, 'w') { |file| file.puts password }
rescue => err
raise "Error occurred while writing '#{password_filename}'\n Causing Error: #{err}"
end
puts
end
@remove_users.each do |user|
password_filename = "#{@target_dir}/#{user}"
if File.exists?(password_filename)
if Utils.yes_or_no("Are you sure you want to remove all entries for #{user}?", false)
show_password(user)
last_password_filename = password_filename + '.last'
if File.exists?(last_password_filename)
File.delete(last_password_filename)
puts "#{last_password_filename} deleted"
end
File.delete(password_filename)
puts "#{password_filename} deleted"
end
end
puts
end
end