-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasswd
More file actions
executable file
·85 lines (64 loc) · 1.81 KB
/
passwd
File metadata and controls
executable file
·85 lines (64 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env ruby
require 'net/ldap'
require 'securerandom'
require 'digest/sha1'
require 'base64'
require 'io/console'
# Generate 16 hex characters of random
def generate_salt
SecureRandom.hex(16)
end
# Hash the password using the given salt. If no salt is supplied, use a new
# one.
def encode_password(plaintext, salt=generate_salt)
raise ArgumentError.new("Password must not be nil") if plaintext.nil?
ssha = Digest::SHA1.digest(plaintext+salt) + salt
return "{SSHA}" + Base64.strict_encode64(ssha).chomp
end
# Check the supplied password against the given hash and return true if they
# match, else false.
def check_password(password, ssha)
decoded = Base64.decode64(ssha.gsub(/^{SSHA}/, ''))
hash = decoded[0..19] # isolate the hash
salt = decoded[20..-1] # isolate the salt
return encode_password(password, salt) == ssha
end
LDAPPASSWD = File.read("ldap.passwd").chomp
print "Username: "
uid = gets.chomp
ldap = Net::LDAP.new(
:host => '127.0.0.1',
:port => 389,
:auth => {
:method => :simple,
:username => "cn=admin,dc=york,dc=hackspace,dc=org,dc=uk",
:password => LDAPPASSWD
}
)
filter = Net::LDAP::Filter.eq( "uid", uid )
treebase = "ou=Users,dc=york,dc=hackspace,dc=org,dc=uk"
hash = ldap.search( :base => treebase, :filter => filter )[0].userPassword[0]
loop do
print "Password: "
password = STDIN.noecho(&:gets).chomp
puts
break if check_password(password, hash)
puts "Incorrect password. Try again, Dumdum."
end
newpass = ""
loop do
print "New password: "
newpass = STDIN.noecho(&:gets).chomp
puts
print "Again: "
newpass2 = STDIN.noecho(&:gets).chomp
puts
break if newpass == newpass2
puts "Passwords do not match. Try again, Dumdum."
end
newhash = encode_password(newpass)
ldap.replace_attribute(
"uid=#{uid},#{treebase}",
:userPassword,
newhash
)