Skip to content

Instantly share code, notes, and snippets.

@glarizza
Created January 4, 2012 19:34
Show Gist options
  • Save glarizza/1561644 to your computer and use it in GitHub Desktop.
Save glarizza/1561644 to your computer and use it in GitHub Desktop.
Setting Password
This method gets an already-prepared password string fed as the variable 'password_hash'
@@users_plist_dir = '/var/db/dslocal/nodes/Default/users'
if File.exists?("#{@@users_plist_dir}/#{resource_name}.plist")
# If a plist already exists in /var/db/dslocal/nodes/Default/users, then
# we will need to extract the binary plist from the 'ShadowHashData'
# key, log the new password into the resultant plist's 'SALTED-SHA512'
# key, and then save the entire structure back.
users_plist = Plist::parse_xml(`plutil -convert xml1 -o /dev/stdout #{@@users_plist_dir}/#{resource_name}.plist`)
password_hash_plist = users_plist['ShadowHashData'][0].string
IO.popen('plutil -convert xml1 -o - -', mode='r+') do |io|
io.write password_hash_plist
io.close_write
@hash_plist = io.read
end
converted_hash_plist = Plist::parse_xml(@hash_plist)
converted_hash_plist['SALTED-SHA512'].string = password_hash.unpack('a2'*(password_hash.size/2)).collect { |i| i.hex.chr }.join
IO.popen('plutil -convert binary1 -o - -', mode='r+') do |io|
io.write converted_hash_plist.to_plist
io.close_write
@changed_plist = io.read
end
users_plist['ShadowHashData'][0].string = @changed_plist
Plist::Emit.save_plist(users_plist, "#{@@users_plist_dir}/#{resource_name}.plist")
%x{plutil -convert binary1 #{@@users_plist_dir}/#{resource_name}.plist}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment