Changeset 522

Show
Ignore:
Timestamp:
04/15/08 12:53:02 (9 months ago)
Author:
gbooker
Message:

Added some checking and an attempt to correct permissions on the import helper

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/SapphireFrappliance/SapphireImportHelper.m

    r514 r522  
    1919 */ 
    2020 
     21#import <Security/Security.h> 
     22#include <sys/stat.h> 
     23#include <sys/mount.h> 
     24 
    2125#import "SapphireImportHelper.h" 
    2226#import "SapphireMetaData.h" 
     
    198202} 
    199203 
     204- (BOOL)isSlashReadOnly 
     205{ 
     206        struct statfs *mntbufp; 
     207         
     208    int i, mountCount = getmntinfo(&mntbufp, MNT_NOWAIT); 
     209        for(i=0; i<mountCount; i++) 
     210        { 
     211                if(!strcmp(mntbufp[i].f_mntonname, "/")) 
     212                        return (mntbufp[i].f_flags & MNT_RDONLY) ? YES : NO; 
     213        } 
     214         
     215        return NO; 
     216} 
     217 
     218- (BOOL)fixClientPermissions:(NSString *)path 
     219{ 
     220        /* Permissions are incorrect */ 
     221        AuthorizationItem authItems[2] = { 
     222                {kAuthorizationEnvironmentUsername, strlen("frontrow"), "frontrow", 0}, 
     223                {kAuthorizationEnvironmentPassword, strlen("frontrow"), "frontrow", 0}, 
     224        }; 
     225        AuthorizationEnvironment environ = {2, authItems}; 
     226        AuthorizationItem rightSet[] = {{kAuthorizationRightExecute, 0, NULL, 0}}; 
     227        AuthorizationRights rights = {1, rightSet}; 
     228        AuthorizationRef auth; 
     229        OSStatus result = AuthorizationCreate(&rights, &environ, kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights, &auth); 
     230        if(result == errAuthorizationSuccess) 
     231        { 
     232                BOOL roslash = [self isSlashReadOnly]; 
     233                if(roslash) 
     234                { 
     235                        char *command = "mount -uw /"; 
     236                        char *arguments[] = {"-c", command, NULL}; 
     237                        AuthorizationExecuteWithPrivileges(auth, "/bin/sh", kAuthorizationFlagDefaults, arguments, NULL); 
     238                } 
     239                char *command = "chmod +rx \"$HELP\""; 
     240                setenv("HELP", [path fileSystemRepresentation], 1); 
     241                char *arguments[] = {"-c", command, NULL}; 
     242                result = AuthorizationExecuteWithPrivileges(auth, "/bin/sh", kAuthorizationFlagDefaults, arguments, NULL); 
     243                unsetenv("HELP"); 
     244                if(roslash) 
     245                { 
     246                        char *command = "mount -ur /"; 
     247                        char *arguments[] = {"-c", command, NULL}; 
     248                        AuthorizationExecuteWithPrivileges(auth, "/bin/sh", kAuthorizationFlagDefaults, arguments, NULL); 
     249                } 
     250        } 
     251        if(result != errAuthorizationSuccess) 
     252                return NO; 
     253         
     254        return YES; 
     255} 
     256 
    200257- (void)startClient 
    201258{ 
    202259        NSString *path = [[NSBundle bundleForClass:[SapphireImportHelper class]] pathForResource:@"ImportHelper" ofType:@""]; 
    203         [NSTask launchedTaskWithLaunchPath:path arguments:[NSArray array]]; 
     260        NSDictionary *attrs = [[NSFileManager defaultManager] fileAttributesAtPath:path traverseLink:YES]; 
     261        if(([[attrs objectForKey:NSFilePosixPermissions] intValue] | S_IXOTH) || [self fixClientPermissions:path]) 
     262        { 
     263                @try { 
     264                        [NSTask launchedTaskWithLaunchPath:path arguments:[NSArray array]]; 
     265                } 
     266                @catch (NSException * e) { 
     267                        NSLog(@"Could not launch helper because of exception %@ launching %@.  Make this file executable", e, path); 
     268                }                
     269        } 
     270        else 
     271                NSLog(@"Could not correct helper permissions on %@.  Make this file executable!", path); 
    204272} 
    205273