1 | //
|
---|
2 | // wakein.m
|
---|
3 | // Pester
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on Tue Mar 11 2003.
|
---|
6 | // Copyright (c) 2003 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import <Cocoa/Cocoa.h>
|
---|
10 | #import "PSPowerManager.h"
|
---|
11 | #import "wakein.h"
|
---|
12 |
|
---|
13 | // System interfaces
|
---|
14 | #include <stdio.h>
|
---|
15 | #include <unistd.h>
|
---|
16 | // MoreIsBetter interfaces
|
---|
17 | #include "MoreUNIX.h"
|
---|
18 | #include "MoreSecurity.h"
|
---|
19 | #include "MoreCFQ.h"
|
---|
20 |
|
---|
21 | static OSStatus SetAutoWake(AuthorizationRef auth, CFDictionaryRef request, CFDictionaryRef *result) {
|
---|
22 | OSStatus err;
|
---|
23 | assert(auth != NULL);
|
---|
24 | assert(request != NULL);
|
---|
25 | assert( result != NULL);
|
---|
26 | assert(*result == NULL);
|
---|
27 | assert(geteuid() == getuid());
|
---|
28 | static const char *kRightName = "net.sabi.Pester.wakein.SetAutoWake";
|
---|
29 | static const AuthorizationFlags kAuthFlags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights;
|
---|
30 | AuthorizationItem right = { kRightName, 0, NULL, 0 };
|
---|
31 | AuthorizationRights rights = { 1, &right };
|
---|
32 | err = AuthorizationCopyRights(auth, &rights, kAuthorizationEmptyEnvironment, kAuthFlags, NULL);
|
---|
33 | // sometimes we don't get here via authexec, so ignore this
|
---|
34 |
|
---|
35 | err = MoreSecSetPrivilegedEUID();
|
---|
36 | if (err != noErr) return err;
|
---|
37 |
|
---|
38 | [[NSAutoreleasePool alloc] init];
|
---|
39 |
|
---|
40 | NSNumber *wakeTime = [(NSDictionary *)request objectForKey: kPesterWakeTime];
|
---|
41 | NSLog(@"setting wake time: %@", wakeTime);
|
---|
42 | if (wakeTime == nil) return noErr;
|
---|
43 |
|
---|
44 | long long secs = [wakeTime unsignedLongLongValue];
|
---|
45 | if (secs < 0 || secs > ULONG_MAX) return paramErr;
|
---|
46 |
|
---|
47 | NS_DURING
|
---|
48 | [PSPowerManager setWakeInterval: (unsigned long)secs];
|
---|
49 | NS_HANDLER
|
---|
50 | *result = (CFDictionaryRef)[NSDictionary dictionaryWithObject: [localException description] forKey: kPesterWakeException];
|
---|
51 | return ioErr;
|
---|
52 | NS_ENDHANDLER
|
---|
53 |
|
---|
54 | *result = (CFDictionaryRef)[NSDictionary dictionary];
|
---|
55 |
|
---|
56 | return noErr;
|
---|
57 | }
|
---|
58 |
|
---|
59 | int main(int argc, const char *argv[]) {
|
---|
60 | OSStatus err;
|
---|
61 | int result;
|
---|
62 | AuthorizationRef auth;
|
---|
63 |
|
---|
64 | auth = MoreSecHelperToolCopyAuthRef();
|
---|
65 | err = MoreSecDestroyInheritedEnvironment(kMoreSecKeepStandardFilesMask, argv);
|
---|
66 | if (err == 0) {
|
---|
67 | err = MoreUNIXIgnoreSIGPIPE();
|
---|
68 | }
|
---|
69 | if (err == 0) {
|
---|
70 | err = MoreSecHelperToolMain(STDIN_FILENO, STDOUT_FILENO, auth, SetAutoWake, argc, argv);
|
---|
71 | }
|
---|
72 | result = MoreSecErrorToHelperToolResult(err);
|
---|
73 |
|
---|
74 | return result;
|
---|
75 | }
|
---|