erikberglund / SwiftPrivilegedHelper

Example application using a privileged helper tool with authentication in an unsandboxed application written in Swift
MIT License
180 stars 33 forks source link

Cannot run SwiftPrivilegedHelper #4

Closed danieldisoosa closed 5 years ago

danieldisoosa commented 6 years ago

Hi all

I am new to mac OS development.I tried to run this awesome privilege helper.But I couldn't execute the commands. When I tried with 'check with uathorisation' and 'check without uathorisation' I get error as 'Command exit status: 0' I successfully updated the plist files by running the SMJobBless python command and the plist files have no code sign issues.

In fact the error is "<_NSXPCDistantObjectWithError: 0x600000097110>" It couldn't call

""""""""""""""""""""""""""""""""""""""""""" @IBAction func checkWithoutAuthorization(_ sender: Any) {

    // Verify the user input is valid
    guard let path = inputPath() else { return }

    // Connect to the helper and run the function runCommandLs(path:reply:)
    let xpcService = self.helperConnection()?.remoteObjectProxyWithErrorHandler() { error -> Void in
        print("XPCService error: %@", error)
        } as? HelperProtocol

    xpcService?.runCommandLs(path:path, reply: { (exitStatus) in
        self.textViewOutput.appendText(line: "Command exit status: \(exitStatus)")
    })
}

"""""""""""""""""""""

The protocol functions '' runCommandLs'' are not calling now so that the commands are not executing.

I was struggling with this couple of days.Any help from any one willl be appreciated !!!! Any one please help?

Thank you

danieldisoosa commented 6 years ago

swiftprevilegedhelper output

erikberglund commented 6 years ago

Hi. When you first ran the application, did you get the question to install the helper tool?

screen shot 2018-02-07 at 08 32 48

When testing helper tools, you might have to remove them manually. Also, if you update anything in the code, you also have to bump the version of the helper so it can see a newer version is available in the application bundle to install. Else it will use the old helper and it might crash.

To manually remove the tool, run:

launchctl unload /Library/LaunchDaemons/com.github.erikberglund.MyApplicationHelper.plist
sudo rm /Library/LaunchDaemons/com.github.erikberglund.MyApplicationHelper.plist
sudo rm /Library/PrivilegedHelperTools/com.github.erikberglund.MyApplicationHelper

Then run the app again, and you should be prompted to install it again.

erikberglund commented 6 years ago

Also, the error you get, and why you aren't seeing anything sounds like the helper can't connect to the main application. It's doing that to be able to send the command output.

Have you changed any code in the project?

As the exit status is 0, the command is probably running fine, it's just the connection from the helper to the main application that seems to be a problem.

erikberglund commented 6 years ago

When the application connect to the helper, the helper gets an listener shouldAccept call, and in there it sets the remoteObjectInterface to the protocol ProcessProtocol.

Line 44 in Helper.swift:

newConnection.remoteObjectInterface = NSXPCInterface(with: ProcessProtocol.self)

So it expects the object that is connecting to the helper to have implemented that protocol exactly. If not, it might crash or possibly that is why you are getting the error if you have changed some parameters of the protocol functions for example.

I just tested the project on a clean machine and it worked as expected for me, so I can't replicate what you are seeing unfortunately.

erikberglund commented 5 years ago

I have updated the project for Swift 4.2 and now it's using an automated code signing method. Since it's rewritten quite a bit I suggest trying the updated version if you are still looking at this. I will close this issue aswell and will gladly take new issues for the updated version.