【问题标题】:Asking a user for elevated privileges and elevating the application without an Apple developer certificate要求用户提升权限并在没有 Apple 开发人员证书的情况下提升应用程序
【发布时间】:2015-09-28 18:37:37
【问题描述】:

显然,从 10.7 开始,AuthorizationExecuteWithPrivileges 已被弃用。我收集到的信息的一般要点似乎建议使用ServiceManagement.frameworkSMJobBless() 函数来部署帮助应用程序。

不过,我对此的理解是,这将需要从 Apple 购买开发人员证书来对我的应用程序和帮助程序进行代码签名 - 否则这将不起作用。它是否正确?

我最初使用AuthorizationExecuteWithPrivileges 要求用户提升权限,因为需要他们才能访问另一个正在运行的进程。没有它,我的应用程序将无法作为其预期的非官方插件运行。代码签名方式真的是唯一的出路吗?由于成本太高,我试图避免购买开发人员证书。

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

【问题讨论】:

    标签: objective-c macos osx-mavericks osx-yosemite


    【解决方案1】:

    @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 只是调用嵌入在StandardAdditions.osax 中的versionAuthorizationExecuteWithPrivileges

    可以想象,do shell scriptwith administrator privileges 选项会在 AuthorizationExecuteWithPrivileges 消失时消失。

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

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

    【讨论】:

    • 谢谢@null,我不完全确定使用它以提升的权限重新启动应用程序的可行性。这并不理想,因为它是一种 hacky applescript 方法 - 但如果它有效,它就可以工作。我会试试看,交叉手指。
    • @loco 抱歉,完全错过了您问题的“重新启动”部分。为此,you'll need a few tweaks。所以像do shell script (commandWithArguments as text) & " &> /dev/null &" with administrator privileges 这样的东西。我相信您已经研究过以 root 身份启动 GUI 应用程序的所有影响——我今天才第一次使用它。
    【解决方案2】:

    代码签名方式真的是唯一的出路吗?

    据我所知,没有比AuthorizationExecuteWithPrivileges 更安全的替代方案。

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

    如果将来电话消失,您可以尝试fail gracefully

    由于成本太高,我试图避免购买开发者证书。

    好吧,如果有帮助的话,代码签名证书将在几年内有效。

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

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

    【讨论】:

    • 我仍在寻找是否有不需要从 Apple 进行代码签名/购买许可证的选项,但感谢您提供有关如何在“最坏情况下优雅地失败”的示例的指针“ 设想!看看那里是如何处理的很有趣!
    • @loco 你可能已经看过了,但是you can use Applescript。如果你走这条路,请确保使用quoted formsanitize the executable path & arguments
    猜你喜欢
    • 2010-10-25
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2015-06-20
    • 2010-11-27
    • 1970-01-01
    • 2011-04-10
    • 2011-08-31
    相关资源
    最近更新 更多