joshua-d-miller / macOSLAPS

Swift binary that will change a local administrator password to a random generated password. Similar behavior to LAPS for Windows
MIT License
385 stars 58 forks source link

runtime error on non ad joined machine #4

Closed bartreardon closed 7 years ago

bartreardon commented 7 years ago

ADTools.swift line 21:

    let adDict = ad_info[0]! as? NSDictionary ?? nil

generates this error when run on a machine that isn't bound to AD

fatal error: unexpectedly found nil while unwrapping an Optional value
2017-08-03 19:48:57.508779+1000 macOSLAPS[60124:24091722] fatal error: unexpectedly found nil while unwrapping an Optional value
joshua-d-miller commented 7 years ago

Just updated the build. If you were using Xcode go ahead and load the project and compile it and give it a shot.

bartreardon commented 7 years ago

no worries - will give it a go soon and let you know

bartreardon commented 7 years ago

initial testing appears to work as expected but I'm getting an odd error on one of my machines after I join the domain - will do some further testing

bartreardon commented 7 years ago

I get this on a couple of my test machines - running the on the machine I built it on works fine though

./macOSLAPS 
fatal error: 'try!' expression unexpectedly raised an error: Foundation._GenericObjCError.nilError: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-802.0.53/src/swift/stdlib/public/core/ErrorType.swift, line 182
Current stack trace:
0    macOSLAPS                          0x0000000101e20350 swift_reportError + 129
1    macOSLAPS                          0x0000000101e3cc60 _swift_stdlib_reportFatalErrorInFile + 100
2    macOSLAPS                          0x0000000101b9c350 (_assertionFailure(StaticString, String, file : StaticString, line : UInt, flags : UInt32) -> Never).(closure #1).(closure #1).(closure #1) + 124
3    macOSLAPS                          0x0000000101de1f50 partial apply for (_assertionFailure(StaticString, String, file : StaticString, line : UInt, flags : UInt32) -> Never).(closure #1).(closure #1).(closure #1) + 93
4    macOSLAPS                          0x0000000101b9b040 specialized specialized StaticString.withUTF8Buffer<A> ((UnsafeBufferPointer<UInt8>) -> A) -> A + 342
5    macOSLAPS                          0x0000000101de4090 partial apply for (_assertionFailure(StaticString, String, file : StaticString, line : UInt, flags : UInt32) -> Never).(closure #1).(closure #1) + 144
6    macOSLAPS                          0x0000000101b9bc70 specialized specialized String._withUnsafeBufferPointerToUTF8<A> ((UnsafeBufferPointer<UInt8>) throws -> A) throws -> A + 127
7    macOSLAPS                          0x0000000101da5f30 partial apply for (_assertionFailure(StaticString, String, file : StaticString, line : UInt, flags : UInt32) -> Never).(closure #1) + 185
8    macOSLAPS                          0x0000000101b9b040 specialized specialized StaticString.withUTF8Buffer<A> ((UnsafeBufferPointer<UInt8>) -> A) -> A + 342
9    macOSLAPS                          0x0000000101d5d280 specialized _assertionFailure(StaticString, String, file : StaticString, line : UInt, flags : UInt32) -> Never + 144
10   macOSLAPS                          0x0000000101be9a50 swift_unexpectedError_merged + 289
11   macOSLAPS                          0x0000000101b0ffb0 specialized ad_tools(computer_record : [ODRecord], tool : String, password : String?, new_ad_exp_date : String?) -> String? + 1645
12   macOSLAPS                          0x0000000101b08ce0 macOSLAPS() -> () + 291
13   macOSLAPS                          0x0000000101b08c70 main + 97
14   libdyld.dylib                      0x00007fffe966f234 start + 1
Illegal instruction: 4
bartreardon commented 7 years ago

ok - that's weird. I also get this error with 1.0.1 and a similar error on 1.0.0.5 on a bunch of my machines

bartreardon commented 7 years ago

ok - got it - this line in ADTools.swift -> ad_tools()

var expirationtime = try! String(describing: value.values(forAttribute: "dsAttrTypeNative:ms-Mcs-AdmPwdExpirationTime")[0])

will throw an exception if ms-Mcs-AdmPwdExpirationTime is not set on the AD object. so any of my other test machines that have never had this set throw the error.

tested putting the code in a do {} catch {} block and it seems to help on 10.12 and 10.13. on 10.10 it still has an error

Info|Thu Aug 10, 2017 04:37:28 pm|macOSLAPS|Password Change is required as the LAPS password for macadmin has expired
Could not cast value of type 'NSTaggedPointerString' (0x7fff72a76f98) to 'NSNumber' (0x7fff71e1c2f0).
bartreardon commented 7 years ago

something like this:

if tool == "Expiration Time" {
    var expirationtime = "126227988000000000" // Setting a default expiration date of 01/01/2001
    do {
        expirationtime = try String(describing: value.values(forAttribute: "dsAttrTypeNative:ms-Mcs-AdmPwdExpirationTime")[0])
    } catch {
        laps_log.print("There has never been a random password generated for this device. Setting a default expiration date of 01/01/2001 in Active Directory to force a password change...", .warn)
    }
    return(expirationtime)
}

(edit - I realise this is a different bug to the original now)

joshua-d-miller commented 7 years ago

Since you resolved this with a pull request I'm going to go ahead and close this one as I'm sure you already tested it 🥇