Firemonkey OS X - request elevated permissions

I am developing an OS X Firemonkey application that should run "openvpn", but OpenVPN requires administrator privileges to create the tunnel interface.

I am trying to convert the first sample from this blog post to Delphi: http://www.michaelvobrien.com/blog/2009/07/authorizationexecutewithprivileges-a-simple-example/

An attempt to run this code sometimes leads to an authorization prompt, as expected, but pressing the OK button will completely freeze the debugger, and the entire system should be turned off. It works better without a debugger, but sometimes it will depend ... I caught the return code before it worked a couple of times, and that was errAuthorizationToolExecuteFailure

I'm not very good at how OSX does something, is there a better way? Apple does not recommend using AuthorizationExecuteWithPrivilegesfor this. I do not know any other way to start openvpn with the necessary permissions.

uses
  Macapi.CoreFoundation, Macapi.Foundation, Macapi.Security;

 

const
  kAuthorizationEmptyEnvironment = nil;

procedure TForm1.Button1Click(Sender: TObject);
var
  AuthRef: AuthorizationRef;
  Status: OSStatus;
begin
  Status := AuthorizationCreate(nil,kAuthorizationEmptyEnvironment,kAuthorizationFlagDefaults,@AuthRef);
  Status := AuthorizationExecuteWithPrivileges(AuthRef,'/sbin/dmesg',0,'',nil)
end;
+4
source share
1 answer

The parameter arguments AuthorizationExecuteWithPrivilegesexpects a pointer to the PAnsiChars array, which ends with the nil pointer as the last element of the array. You just pass a pointer to an empty string. This will lead to random crashes depending on what happens in memory after a pointer to an empty string.

Try the following:

procedure TForm1.Button1Click(Sender: TObject);
var
  AuthRef: AuthorizationRef;
  Status: OSStatus;
  argv: array[0..0] of PAnsiChar;
begin
  ArgV[0] := nil;

  AuthRef := nil;
  Status := AuthorizationCreate(nil,kAuthorizationEmptyEnvironment,kAuthorizationFlagDefaults,@AuthRef);
  Status := AuthorizationExecuteWithPrivileges(AuthRef, '/sbin/dmesg', 0, @argv[0], nil);
  AuthorizationFree(AuthRef, kAuthorizationFlagDefaults);
end;
+1
source

All Articles