Class | HTAuth::Passwd |
In: |
lib/htauth/passwd.rb
|
Parent: | Object |
MAX_PASSWD_LENGTH | = | 255 |
passwd_file | [RW] |
# File lib/htauth/passwd.rb, line 38 38: def option_parser 39: if not @option_parser then 40: @option_parser = OptionParser.new do |op| 41: op.banner = "Usage: \n \#{op.program_name} [-cmdpsD] passwordfile username\n \#{op.program_name} -b[cmdpsD] passwordfile username password\n\n \#{op.program_name} -n[mdps] username\n \#{op.program_name} -nb[mdps] username password\n" 42: 43: op.separator "" 44: 45: op.on("-b", "--batch", "Batch mode, get the password from the command line, rather than prompt") do |b| 46: options.batch_mode = b 47: end 48: 49: op.on("-c", "--create", "Create a new file; this overwrites an existing file.") do |c| 50: options.file_mode = HTAuth::File::CREATE 51: end 52: 53: op.on("-d", "--crypt", "Force CRYPT encryption of the password (default).") do |c| 54: options.algorithm = "crypt" 55: end 56: 57: op.on("-D", "--delete", "Delete the specified user.") do |d| 58: options.delete_entry = d 59: end 60: 61: op.on("-h", "--help", "Display this help.") do |h| 62: options.show_help = h 63: end 64: 65: op.on("-m", "--md5", "Force MD5 encryption of the password (default on Windows).") do |m| 66: options.algorithm = "md5" 67: end 68: 69: op.on("-n", "--stdout", "Do not update the file; Display the results on stdout instead.") do |n| 70: options.send_to_stdout = true 71: options.passwdfile = HTAuth::File::STDOUT_FLAG 72: end 73: 74: op.on("-p", "--plaintext", "Do not encrypt the password (plaintext).") do |p| 75: options.algorithm = "plaintext" 76: end 77: 78: op.on("-s", "--sha1", "Force SHA encryption of the password.") do |s| 79: options.algorithm = "sha1" 80: end 81: 82: op.on("-v", "--version", "Show version info.") do |v| 83: options.show_version = v 84: end 85: end 86: end 87: @option_parser 88: end
# File lib/htauth/passwd.rb, line 21 21: def options 22: if @options.nil? then 23: @options = ::OpenStruct.new 24: @options.batch_mode = false 25: @options.file_mode = File::ALTER 26: @options.passwdfile = nil 27: @options.algorithm = Algorithm::EXISTING 28: @options.send_to_stdout = false 29: @options.show_version = false 30: @options.show_help = false 31: @options.username = nil 32: @options.delete_entry = false 33: @options.password = "" 34: end 35: @options 36: end
# File lib/htauth/passwd.rb, line 108 108: def parse_options(argv) 109: begin 110: option_parser.parse!(argv) 111: show_version if options.show_version 112: show_help if options.show_help 113: 114: raise ::OptionParser::ParseError, "Unable to send to stdout AND create a new file" if options.send_to_stdout and (options.file_mode == File::CREATE) 115: raise ::OptionParser::ParseError, "a username is needed" if options.send_to_stdout and argv.size < 1 116: raise ::OptionParser::ParseError, "a username and password are needed" if options.send_to_stdout and options.batch_mode and ( argv.size < 2 ) 117: raise ::OptionParser::ParseError, "a passwordfile, username and password are needed " if not options.send_to_stdout and options.batch_mode and ( argv.size < 3 ) 118: raise ::OptionParser::ParseError, "a passwordfile and username are needed" if argv.size < 2 119: 120: options.passwdfile = argv.shift unless options.send_to_stdout 121: options.username = argv.shift 122: options.password = argv.shift if options.batch_mode 123: 124: rescue ::OptionParser::ParseError => pe 125: $stderr.puts "ERROR: #{option_parser.program_name} - #{pe}" 126: show_help 127: exit 1 128: end 129: end
# File lib/htauth/passwd.rb, line 131 131: def run(argv) 132: begin 133: parse_options(argv) 134: passwd_file = PasswdFile.new(options.passwdfile, options.file_mode) 135: 136: if options.delete_entry then 137: passwd_file.delete(options.username) 138: else 139: unless options.batch_mode 140: # initialize here so that if $stdin is overwritten it gest picked up 141: hl = ::HighLine.new 142: 143: action = passwd_file.has_entry?(options.username) ? "Changing" : "Adding" 144: 145: $stdout.puts "#{action} password for #{options.username}." 146: 147: pw_in = hl.ask(" New password: ") { |q| q.echo = '*' } 148: raise PasswordError, "password '#{pw_in}' too long" if pw_in.length >= MAX_PASSWD_LENGTH 149: 150: pw_validate = hl.ask("Re-type new password: ") { |q| q.echo = '*' } 151: raise PasswordError, "They don't match, sorry." unless pw_in == pw_validate 152: options.password = pw_in 153: end 154: passwd_file.add_or_update(options.username, options.password, options.algorithm) 155: end 156: 157: passwd_file.save! 158: 159: rescue HTAuth::FileAccessError => fae 160: msg = "Password file failure (#{options.passwdfile}) " 161: $stderr.puts "#{msg}: #{fae.message}" 162: exit 1 163: rescue HTAuth::PasswordError => pe 164: $stderr.puts "#{pe.message}" 165: exit 1 166: rescue HTAuth::PasswdFileError => fe 167: $stderr.puts "#{fe.message}" 168: exit 1 169: rescue SignalException => se 170: $stderr.puts 171: $stderr.puts "Interrupted" 172: exit 1 173: end 174: exit 0 175: end
# File lib/htauth/passwd.rb, line 98 98: def show_help 99: $stdout.puts option_parser 100: exit 1 101: end