要求用户提升特权并在没有Apple开发人员证书的情况下提升应用程序

问题描述:

显然,从10.7开始,AuthorizationExecuteWithPrivileges已被弃用。我收集的信息的一般要点似乎建议使用ServiceManagement.frameworkSMJobBless()函数来部署辅助应用程序。要求用户提升特权并在没有Apple开发人员证书的情况下提升应用程序

虽然我的理解是,这需要从Apple购买开发人员证书来为我的应用程序和帮助程序进行代码签名 - 否则这将无法工作。它是否正确?

我最初使用AuthorizationExecuteWithPrivileges来要求用户提升权限,因为他们需要访问另一个正在运行的进程。没有这一点,我的应用程序不能作为它打算的非官方插件工作。代码签名方式真的是唯一的途径吗?我试图避免购买开发者证书,因为它的成本很高。

有没有人找到任何替代方法来重新启动具有提升权限的应用程序,当然用户权限?

代码签名的方式真的是唯一的途径吗?

据我所知,没有安全的替代AuthorizationExecuteWithPrivileges

在优胜美地里它仍然可以正常工作。还没有尝试过El Capitan。

如果通话在将来消失,您可以尝试拨打fail gracefully

我试图避免购买开发者证书,因为它的纯粹成本。

那么,如果它有帮助,代码签名证书将有效几年。

我很确定我已经让我的开发者帐户没有任何问题失效。

所以它基本上是每五年99美元。

+0

我仍然在寻找是否有选项不需要Apple的授权/购买许可证,但是要感谢关于如何在“最坏情况”情况下优雅地失败的示例!有趣的是看看那里是如何处理的! – Tiago

+0

@loco你可能已经看到了这个,但[你可以使用Applescript](https://*.com/questions/6841937/authorizationexecutewithprivileges-is-deprecated)。如果你走这条路线,确保你使用'quoted form'来[清理可执行文件路径和参数](https://developer.apple.com/library/mac/technotes/tn2065/_index.html#//apple_ref/DOC/UID/DTS10003093-CH1-SECTION3)。 – 2015-07-23 02:19:32

@CarlosP's answer与代码逃脱路径&参数:

- (BOOL)runProcessAsAdministrator:(NSString*)scriptPath 
        withArguments:(NSArray*)arguments 
          output:(NSString**)output 
       errorDescription:(NSString**)errorDescription { 

    //Check path. 
    if (![scriptPath hasPrefix:@"/"]) { 
     @throw [NSException exceptionWithName: 
        NSInvalidArgumentException reason:@"Absolute path required." userInfo:nil]; 
    } 

    //Define script. 
    static NSAppleScript* appleScript = nil; 
    if (!appleScript) { 
     appleScript = [[NSAppleScript alloc] initWithSource: 
      @"on run commandWithArguments\n" 
      " activate\n" 
      " repeat with currentArgument in commandWithArguments\n" 
      "  set contents of currentArgument to quoted form of currentArgument\n" 
      " end repeat\n" 
      " set AppleScript's text item delimiters to space\n" 
      " return do shell script (commandWithArguments as text) with administrator privileges\n" 
      "end run"]; 
    } 

    //Set command. 
    NSAppleEventDescriptor* commandWithArguments = [NSAppleEventDescriptor listDescriptor]; 
    [commandWithArguments insertDescriptor: 
     [NSAppleEventDescriptor descriptorWithString:scriptPath] atIndex:0]; 

    //Set arguments. 
    for (NSString* currentArgument in arguments) { 
     [commandWithArguments insertDescriptor: 
      [NSAppleEventDescriptor descriptorWithString:currentArgument] atIndex:0]; 
    } 

    //Create target & event. 
    ProcessSerialNumber  processSerial = {0, kCurrentProcess}; 
    NSAppleEventDescriptor* scriptTarget = 
     [NSAppleEventDescriptor descriptorWithDescriptorType:typeProcessSerialNumber bytes:&processSerial length:sizeof(ProcessSerialNumber)]; 
    NSAppleEventDescriptor* scriptEvent  = 
     [NSAppleEventDescriptor appleEventWithEventClass:kCoreEventClass 
               eventID:kAEOpenApplication 
             targetDescriptor:scriptTarget 
               returnID:kAutoGenerateReturnID 
              transactionID:kAnyTransactionID]; 
    [scriptEvent setParamDescriptor:commandWithArguments forKeyword:keyDirectObject]; 

    //Run script. 
    NSDictionary*   errorInfo = [NSDictionary dictionary]; 
    NSAppleEventDescriptor* eventResult = [appleScript executeAppleEvent:scriptEvent error:&errorInfo]; 

    //Success? 
    if (!eventResult) { 
     if (errorDescription) 
      *errorDescription = [errorInfo objectForKey:NSAppleScriptErrorMessage]; 
     return NO; 
    } else { 
     if (output) 
      *output = [eventResult stringValue]; 
     return YES; 
    } 

} 

更新

在优山美地,do shell script只是调用的AuthorizationExecuteWithPrivileges一个version嵌入StandardAdditions.osax

可以想象,的with administrator privileges选项将在AuthorizationExecuteWithPrivileges时消失。

个人而言,我会直接继续拨打AuthorizationExecuteWithPrivileges

do shell script自动具有reaping the process的优势。这需要一些extra workAuthorizationExecuteWithPrivileges

+0

感谢@null,我并不完全确定使用这种方法重新启动具有提升权限的应用程序的可行性。这不是理想的,因为它是一个hacky applescript方法 - 但是如果它起作用,它就会起作用。我会尝试一下,手指交叉。 – Tiago

+0

@loco对不起,完全错过了你的问题的“重新启动”部分。要做到这一点,[你需要做一些调整](https://developer.apple.com/library/mac/technotes/tn2065/_index.html#//apple_ref/doc/uid/DTS10003093-CH1-SECTION5) 。所以,像''使用管理员权限'执行shell脚本(commandWithArguments作为文本)&“&>/dev/null&”。我相信你已经调查了以root身份启动一个GUI应用程序的所有含义 - 我今天第一次玩这个游戏。 – 2015-07-29 00:50:54