2

I would like to know if there is a way from a Cocoa application to know if the user has "set date & time automatically" set in his System Preferences. If so, I would also like to know the time of the last clock update. Thanks a lot for any help you may provide.

Alfonso Tesauro
  • 1,730
  • 13
  • 21
  • What are you trying to do? – mipadi Apr 21 '15 at 20:20
  • Hello, thanks for the quick reply... I would like to deny usage of my application if internet time is not active so that I can trust the system-clock for sure. It seems that 'systemsetup -getusingnetworktime' is the official solution, but it requires root privileges, won't even run as administrator. Thanks – Alfonso Tesauro Apr 21 '15 at 20:24

1 Answers1

1

You could try using STPrivilegedTask (or similar) to run a task with root privileges, and then parse the result. Hack-y/not the best, but it should work.

Edit:

- (BOOL)networkTimeEnabled {
    STPrivilegedTask *task = [[STPrivilegedTask alloc] initWithLaunchPath:@"/usr/bin/sudo" arguments:@[@"/usr/sbin/systemsetup",@"-getusingnetworktime"]];
    if ([task launch] == noErr) {
        NSData *data = [task.outputFileHandle readDataToEndOfFile];
        [task waitUntilExit];

        NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSString *status = [result componentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet][2];
        return ([status isEqualToString:@"On"]);
    }
    return NO;
}
Seb Jachec
  • 3,003
  • 2
  • 30
  • 56
  • Thanks a lot Seb for your answer... unfortunately I tried exactly that, but systemsetup continues to fail under STPrivilegedTask, as it needs to be execute by root, not just administrator. I have checked that by disassembling systemsetup. I have also patched it in its root check, but distributing my app with a tampered systemsetup running inside it would be outside any rule of good design. Thanks again. Another suggested solution would be to search for running ntpd processes owned by root. If there is one, then you may assume network time is on, but , again, that is just an assumption. – Alfonso Tesauro Apr 24 '15 at 16:44
  • @AlfonsoTesauro Hmm. To be honest, `systemsetup` worked for me both in Terminal with `sudo` and via this method. Your assumption about the `ntpd` process running appears to be correct (tested by disabling/enabling "Set date & time Automatically"), but it seems [methods to get a full process listing to actually check for it have been deprecated](http://stackoverflow.com/questions/2518160/programmatically-check-if-a-process-is-running-on-mac).. – Seb Jachec Apr 24 '15 at 18:22
  • Apparently they changed this systemsetup behaviour recently. Are you running Yosemite 10.10.3 ? About the processes, I was thinking of calling ps -ax with a NSTask... I believe that is a fairly supported method and can be presumed as existing on many Mac Os X versions. – Alfonso Tesauro Apr 24 '15 at 18:30
  • @AlfonsoTesauro I am running Yosemite 10.10.3. Seems like that your NSTask solution is a good idea anyway. You could try `pgrep -f "ntpd"` or something similar by the way - it'll return either the process number of `ntpd` if it's running or nothing if it isn't. – Seb Jachec Apr 24 '15 at 18:33
  • I have read about disassembling and patching systemsetup on this [link](https://truesecdev.wordpress.com/2015/04/09/hidden-backdoor-api-to-root-privileges-in-apple-os-x/). Thanks a lot for your help. – Alfonso Tesauro Apr 24 '15 at 18:34
  • @AlfonsoTesauro Ok! I'm not sure if that's a recommended approach, but good luck with your coding/work anyway. – Seb Jachec Apr 24 '15 at 18:41