Ignore:
Timestamp:
03/12/03 21:30:33 (22 years ago)
Author:
Nicholas Riley
Message:

Broken, to-be-removed authorization implementation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cocoa/Pester/Source/wakein.m

    r117 r118  
    88
    99#import <Cocoa/Cocoa.h>
     10#import "PSPowerManager.h"
    1011#import "wakein.h"
    11 #import "unistd.h"
    12 #import "PSPowerManager.h"
    1312
    14 void usage() {
    15     fprintf(stderr, "usage: wakein secs\n");
    16     exit(PSWakeErrorSyntax);
     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
     21static 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;
    1757}
    1858
    19 int main(int argc, const char *argv[])
    20 {
    21     [[NSAutoreleasePool alloc] init];
     59int main(int argc, const char *argv[]) {
     60    OSStatus err;
     61    int result;
     62    AuthorizationRef auth;
    2263
    23     if (argc != 2) usage();
     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);
    2473
    25     long long secs;
    26     if (![[NSScanner scannerWithString: [NSString stringWithUTF8String: argv[1]]] scanLongLong: &secs])
    27         usage();
    28 
    29     if (secs < 0 || secs > ULONG_MAX)
    30         usage();
    31 
    32     if (geteuid() != 0) {
    33         fprintf(stderr, "wakein: must be root\n");
    34         return PSWakeErrorPermissions;
    35     }
    36 
    37     NS_DURING
    38         [PSPowerManager setWakeInterval: secs];
    39     NS_HANDLER
    40         fprintf(stderr, "%s\n", [[localException description] UTF8String]);
    41         return PSWakeErrorException;
    42     NS_ENDHANDLER
    43 
    44     return 0;
     74    return result;
    4575}
Note: See TracChangeset for help on using the changeset viewer.