Class HTAuth::Passwd
In: lib/htauth/passwd.rb
lib/htauth/passwd.rb
Parent: Object

Methods

Constants

MAX_PASSWD_LENGTH = 255
MAX_PASSWD_LENGTH = 255

Attributes

passwd_file  [RW] 
passwd_file  [RW] 

Public Class methods

[Source]

    # File lib/htauth/passwd.rb, line 16
16:     def initialize
17:       @passwd_file = nil
18:     end

[Source]

    # File lib/htauth/passwd.rb, line 16
16:     def initialize
17:       @passwd_file = nil
18:     end

Public Instance methods

[Source]

    # File lib/htauth/passwd.rb, line 37
37:     def option_parser
38:       if not @option_parser then
39:         @option_parser = OptionParser.new do |op|
40:           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"
41: 
42:           op.separator ""
43: 
44:           op.on("-b", "--batch", "Batch mode, get the password from the command line, rather than prompt") do |b|
45:             options.batch_mode = b
46:           end
47: 
48:           op.on("-c", "--create", "Create a new file; this overwrites an existing file.") do |c|
49:             options.file_mode = HTAuth::File::CREATE
50:           end
51: 
52:           op.on("-d", "--crypt", "Force CRYPT encryption of the password (default).") do |c|
53:             options.algorithm = "crypt"
54:           end
55: 
56:           op.on("-D", "--delete", "Delete the specified user.") do |d|
57:             options.delete_entry = d
58:           end
59: 
60:           op.on("-h", "--help", "Display this help.") do |h|
61:             options.show_help = h
62:           end
63: 
64:           op.on("-m", "--md5", "Force MD5 encryption of the password (default on Windows).") do |m|
65:             options.algorithm = "md5"
66:           end
67: 
68:           op.on("-n", "--stdout", "Do not update the file; Display the results on stdout instead.") do |n|
69:             options.send_to_stdout = true
70:             options.passwdfile     = HTAuth::File::STDOUT_FLAG
71:           end
72: 
73:           op.on("-p", "--plaintext", "Do not encrypt the password (plaintext).") do |p|
74:             options.algorithm = "plaintext"
75:           end
76: 
77:           op.on("-s", "--sha1", "Force SHA encryption of the password.") do |s|
78:             options.algorithm = "sha1"
79:           end
80: 
81:           op.on("-v", "--version", "Show version info.") do |v|
82:             options.show_version = v
83:           end
84:         end
85:       end
86:       @option_parser
87:     end

[Source]

    # File lib/htauth/passwd.rb, line 37
37:     def option_parser
38:       if not @option_parser then
39:         @option_parser = OptionParser.new do |op|
40:           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"
41: 
42:           op.separator ""
43: 
44:           op.on("-b", "--batch", "Batch mode, get the password from the command line, rather than prompt") do |b|
45:             options.batch_mode = b
46:           end
47: 
48:           op.on("-c", "--create", "Create a new file; this overwrites an existing file.") do |c|
49:             options.file_mode = HTAuth::File::CREATE
50:           end
51: 
52:           op.on("-d", "--crypt", "Force CRYPT encryption of the password (default).") do |c|
53:             options.algorithm = "crypt"
54:           end
55: 
56:           op.on("-D", "--delete", "Delete the specified user.") do |d|
57:             options.delete_entry = d
58:           end
59: 
60:           op.on("-h", "--help", "Display this help.") do |h|
61:             options.show_help = h
62:           end
63: 
64:           op.on("-m", "--md5", "Force MD5 encryption of the password (default on Windows).") do |m|
65:             options.algorithm = "md5"
66:           end
67: 
68:           op.on("-n", "--stdout", "Do not update the file; Display the results on stdout instead.") do |n|
69:             options.send_to_stdout = true
70:             options.passwdfile     = HTAuth::File::STDOUT_FLAG
71:           end
72: 
73:           op.on("-p", "--plaintext", "Do not encrypt the password (plaintext).") do |p|
74:             options.algorithm = "plaintext"
75:           end
76: 
77:           op.on("-s", "--sha1", "Force SHA encryption of the password.") do |s|
78:             options.algorithm = "sha1"
79:           end
80: 
81:           op.on("-v", "--version", "Show version info.") do |v|
82:             options.show_version = v
83:           end
84:         end
85:       end
86:       @option_parser
87:     end

[Source]

    # File lib/htauth/passwd.rb, line 20
20:     def options
21:       if @options.nil? then
22:         @options                = ::OpenStruct.new
23:         @options.batch_mode     = false
24:         @options.file_mode      = File::ALTER
25:         @options.passwdfile     = nil
26:         @options.algorithm      = Algorithm::EXISTING
27:         @options.send_to_stdout = false
28:         @options.show_version   = false
29:         @options.show_help      = false
30:         @options.username       = nil
31:         @options.delete_entry   = false
32:         @options.password       = ""
33:       end
34:       @options
35:     end

[Source]

    # File lib/htauth/passwd.rb, line 20
20:     def options
21:       if @options.nil? then
22:         @options                = ::OpenStruct.new
23:         @options.batch_mode     = false
24:         @options.file_mode      = File::ALTER
25:         @options.passwdfile     = nil
26:         @options.algorithm      = Algorithm::EXISTING
27:         @options.send_to_stdout = false
28:         @options.show_version   = false
29:         @options.show_help      = false
30:         @options.username       = nil
31:         @options.delete_entry   = false
32:         @options.password       = ""
33:       end
34:       @options
35:     end

[Source]

     # File lib/htauth/passwd.rb, line 107
107:     def parse_options(argv)
108:       begin
109:         option_parser.parse!(argv)
110:         show_version if options.show_version
111:         show_help if options.show_help 
112: 
113:         raise ::OptionParser::ParseError, "Unable to send to stdout AND create a new file" if options.send_to_stdout and (options.file_mode == File::CREATE)
114:         raise ::OptionParser::ParseError, "a username is needed" if options.send_to_stdout and argv.size < 1
115:         raise ::OptionParser::ParseError, "a username and password are needed" if options.send_to_stdout and options.batch_mode  and ( argv.size < 2 ) 
116:         raise ::OptionParser::ParseError, "a passwordfile, username and password are needed " if not options.send_to_stdout and options.batch_mode and ( argv.size < 3 )
117:         raise ::OptionParser::ParseError, "a passwordfile and username are needed" if argv.size < 2
118: 
119:         options.passwdfile = argv.shift unless options.send_to_stdout
120:         options.username   = argv.shift
121:         options.password   = argv.shift if options.batch_mode
122: 
123:       rescue ::OptionParser::ParseError => pe
124:         $stderr.puts "ERROR: #{option_parser.program_name} - #{pe}"
125:         show_help
126:         exit 1
127:       end
128:     end

[Source]

     # File lib/htauth/passwd.rb, line 107
107:     def parse_options(argv)
108:       begin
109:         option_parser.parse!(argv)
110:         show_version if options.show_version
111:         show_help if options.show_help 
112: 
113:         raise ::OptionParser::ParseError, "Unable to send to stdout AND create a new file" if options.send_to_stdout and (options.file_mode == File::CREATE)
114:         raise ::OptionParser::ParseError, "a username is needed" if options.send_to_stdout and argv.size < 1
115:         raise ::OptionParser::ParseError, "a username and password are needed" if options.send_to_stdout and options.batch_mode  and ( argv.size < 2 ) 
116:         raise ::OptionParser::ParseError, "a passwordfile, username and password are needed " if not options.send_to_stdout and options.batch_mode and ( argv.size < 3 )
117:         raise ::OptionParser::ParseError, "a passwordfile and username are needed" if argv.size < 2
118: 
119:         options.passwdfile = argv.shift unless options.send_to_stdout
120:         options.username   = argv.shift
121:         options.password   = argv.shift if options.batch_mode
122: 
123:       rescue ::OptionParser::ParseError => pe
124:         $stderr.puts "ERROR: #{option_parser.program_name} - #{pe}"
125:         show_help
126:         exit 1
127:       end
128:     end

[Source]

     # File lib/htauth/passwd.rb, line 130
130:     def run(argv, env = ENV)
131:       begin
132:         parse_options(argv)
133:         passwd_file = PasswdFile.new(options.passwdfile, options.file_mode)
134: 
135:         if options.delete_entry then
136:           passwd_file.delete(options.username)
137:         else
138:           unless options.batch_mode 
139:             # initialize here so that if $stdin is overwritten it gest picked up
140:             hl = ::HighLine.new
141: 
142:             action = passwd_file.has_entry?(options.username) ? "Changing" : "Adding"
143: 
144:             $stdout.puts "#{action} password for #{options.username}."
145: 
146:             pw_in       = hl.ask("        New password: ") { |q| q.echo = '*' } 
147:             raise PasswordError, "password '#{pw_in}' too long" if pw_in.length >= MAX_PASSWD_LENGTH
148: 
149:             pw_validate = hl.ask("Re-type new password: ") { |q| q.echo = '*' }
150:             raise PasswordError, "They don't match, sorry." unless pw_in == pw_validate
151:             options.password = pw_in
152:           end
153:           passwd_file.add_or_update(options.username, options.password, options.algorithm)
154:         end
155: 
156:         passwd_file.save! 
157: 
158:       rescue HTAuth::FileAccessError => fae
159:         msg = "Password file failure (#{options.passwdfile}) "
160:         $stderr.puts "#{msg}: #{fae.message}"
161:         exit 1
162:       rescue HTAuth::PasswordError => pe
163:         $stderr.puts "#{pe.message}"
164:         exit 1
165:       rescue HTAuth::PasswdFileError => fe
166:         $stderr.puts "#{fe.message}"
167:         exit 1
168:       rescue SignalException => se
169:         $stderr.puts
170:         $stderr.puts "Interrupted"
171:         exit 1
172:       end
173:       exit 0
174:     end

[Source]

     # File lib/htauth/passwd.rb, line 130
130:     def run(argv, env = ENV)
131:       begin
132:         parse_options(argv)
133:         passwd_file = PasswdFile.new(options.passwdfile, options.file_mode)
134: 
135:         if options.delete_entry then
136:           passwd_file.delete(options.username)
137:         else
138:           unless options.batch_mode 
139:             # initialize here so that if $stdin is overwritten it gest picked up
140:             hl = ::HighLine.new
141: 
142:             action = passwd_file.has_entry?(options.username) ? "Changing" : "Adding"
143: 
144:             $stdout.puts "#{action} password for #{options.username}."
145: 
146:             pw_in       = hl.ask("        New password: ") { |q| q.echo = '*' } 
147:             raise PasswordError, "password '#{pw_in}' too long" if pw_in.length >= MAX_PASSWD_LENGTH
148: 
149:             pw_validate = hl.ask("Re-type new password: ") { |q| q.echo = '*' }
150:             raise PasswordError, "They don't match, sorry." unless pw_in == pw_validate
151:             options.password = pw_in
152:           end
153:           passwd_file.add_or_update(options.username, options.password, options.algorithm)
154:         end
155: 
156:         passwd_file.save! 
157: 
158:       rescue HTAuth::FileAccessError => fae
159:         msg = "Password file failure (#{options.passwdfile}) "
160:         $stderr.puts "#{msg}: #{fae.message}"
161:         exit 1
162:       rescue HTAuth::PasswordError => pe
163:         $stderr.puts "#{pe.message}"
164:         exit 1
165:       rescue HTAuth::PasswdFileError => fe
166:         $stderr.puts "#{fe.message}"
167:         exit 1
168:       rescue SignalException => se
169:         $stderr.puts
170:         $stderr.puts "Interrupted"
171:         exit 1
172:       end
173:       exit 0
174:     end

[Source]

     # File lib/htauth/passwd.rb, line 97
 97:     def show_help
 98:       $stdout.puts option_parser
 99:       exit 1
100:     end

[Source]

     # File lib/htauth/passwd.rb, line 97
 97:     def show_help
 98:       $stdout.puts option_parser
 99:       exit 1
100:     end

[Source]

     # File lib/htauth/passwd.rb, line 102
102:     def show_version
103:       $stdout.puts "#{option_parser.program_name}: version #{HTAuth::VERSION}"
104:       exit 1
105:     end

[Source]

     # File lib/htauth/passwd.rb, line 102
102:     def show_version
103:       $stdout.puts "#{option_parser.program_name}: version #{HTAuth::VERSION}"
104:       exit 1
105:     end

[Validate]