Created
August 4, 2012 17:40
-
-
Save dayglojesus/3258894 to your computer and use it in GitHub Desktop.
Pimp out DSLocalRecords as NSDictionaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'osx/cocoa' | |
require 'pp' | |
require 'delegate' | |
load './password.rb' | |
include OSX | |
def load_plist(file) | |
NSMutableDictionary.dictionaryWithContentsOfFile(file) | |
end | |
class DSLocalRecord < DelegateClass(NSMutableDictionary) | |
DSLOCAL_ROOT = '/private/var/db/dslocal/nodes' | |
def initialize(args) | |
@data = normalize(args) | |
@record_type = record_type | |
@node = @data.delete(:node) || 'Default' | |
@name = @data[:name][0] | |
@record = synthesize(@data) | |
super(@record) | |
end | |
def exists? | |
# pp @composite | |
# pp @real | |
@composite.isEqualToDictionary(@real) | |
end | |
private | |
def synthesize(data) | |
# @file = "#{DSLOCAL_ROOT}/#{@node}/#{@record_type + 's'}/#{@name}.plist" | |
# @file = "./users/#{@name}.plist" | |
@file = "./users/#{@name}-ml.plist" | |
@real = load_plist(@file) | |
if @real | |
@composite = @real.dup | |
@composite.addEntriesFromDictionary(data) | |
else | |
@composite = defaults(data) | |
end | |
@composite | |
end | |
def normalize(input) | |
input.inject({}){ |memo,(k,v)| memo[k] = [v]; memo } | |
end | |
def record_type | |
string = self.class.to_s | |
parts = string.split(/::/) | |
parts.last.to_s.downcase | |
end | |
end | |
class User < DSLocalRecord | |
def initialize(args) | |
@password = DSLocalPassword.new(args.delete(:password)) | |
# @defaults = defaults | |
args['ShadowHashData'] = @password.content | |
super(args) | |
end | |
def defaults | |
# defaults = { | |
# 'name' => ["#{data[:name]}"], | |
# 'realname' => ["#{data[:name]}"], | |
# 'uid' => ["#{next_uid}"], | |
# 'generateduid' => ["#{next_guid}"], | |
# 'home' => ["/Users/#{data[:name]}"], | |
# 'shell' => ['/bin/bash'], | |
# 'gid' => ['20'], | |
# 'passwd' => ['********'], | |
# 'comment' => [''], | |
# # 'authentication_authority' => [''], | |
# 'authentication_authority' => [";ShadowHash;HASHLIST:<SALTED-SHA512>"], | |
# # 'ShadowHashData' => [''], | |
# } | |
end | |
end | |
# Lion | |
# x = User.new(:name => 'bar', :password => {:label => 'SALTED-SHA512', :hash => 'a6065f17b5ddd603d284264c7363b7b211df819bc211f7e0047668e4f35ec558a3b5dee1a5cc3a287b9c3b252813782336d07f1bd0579eeaceb00f9bc99c42e349bc1f64'} ) | |
# Mountain Lion | |
x = User.new(:name => 'bar', :password => {:label => 'SALTED-SHA512-PBKDF2', :iterations => 29069, | |
:entropy => '969b62dca51304cc0852dc547309643b480d656f83df441aeaed342c836ddc730bc6350a1a61dd847a1aee910c8648a4a895b07addd066a45cf1233c52c3c73adb5f77d0ba82b71d134a8e9a0c0ed5d6a0e789bfae6beb48bf48e34bfd20509e7b22763753fbd4ae302e27717cea3deede0b38fc52640e5229a7dcbc8d66d609', | |
:salt => '79e5cb312d40cc7e89b00f67f928a31c221213bd73cba0f58dc3bcd4215f6552'} ) | |
pp x.exists? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment