[Done] Server with Port 102 need admin rights
Posted: Tue Nov 06, 2018 11:32 pm
How to enable Server Port 102 (ISOonTCP).
Is blocked from System...
Is blocked from System...
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
AuthorizationExecuteWithPrivileges(...)
Code: Select all
;-TOP
; Comment : Module RunAsAdmin
; Author : mk-soft
; Version : v1.01
; Date : 07.11.2018
; OS : OSX
; Thanks to:
; - Airr
; - Wolfram
EnableExplicit
; ***************************************************************************************
;- Begin Module RunAsAdmin
DeclareModule RunAsAdmin
Declare Login()
EndDeclareModule
; ---
Module RunAsAdmin
; -----------------------------------------------------------------------------------
; Error codes returned by Authorization API.
Enumeration AuthorizationResult
#errAuthorizationSuccess = 0 ;/* The authorization was successful. */
#errAuthorizationInvalidSet = -60001 ;/* The authorization rights are invalid. */
#errAuthorizationInvalidRef = -60002 ;/* The authorization reference is invalid. */
#errAuthorizationInvalidTag = -60003 ;/* The authorization tag is invalid. */
#errAuthorizationInvalidPointer = -60004 ;/* The returned authorization is invalid. */
#errAuthorizationDenied = -60005 ;/* The authorization was denied. */
#errAuthorizationCanceled = -60006 ;/* The authorization was cancelled by the user. */
#errAuthorizationInteractionNotAllowed = -60007 ;/* The authorization was denied since no user interaction was possible. */
#errAuthorizationInternal = -60008 ;/* Unable To obtain authorization For this operation. */
#errAuthorizationExternalizeNotAllowed = -60009 ;/* The authorization is Not allowed To be converted To an external format. */
#errAuthorizationInternalizeNotAllowed = -60010 ;/* The authorization is Not allowed To be created from an external format. */
#errAuthorizationInvalidFlags = -60011 ;/* The provided option flag(s) are invalid For this authorization operation. */
#errAuthorizationToolExecuteFailure = -60031 ;/* The specified program could Not be executed. */
#errAuthorizationToolEnvironmenterror = -60032 ;/* An invalid status was returned during execution of a privileged tool. */
#errAuthorizationBadAddress = -60033 ;/* The requested socket address is invalid (must be 0-1023 inclusive). */
EndEnumeration
; Authorization
#kAuthorizationEmptyEnvironment = #Null
#kAuthorizationRightExecute = 0 ; UTF8("system.privilege.admin")
#kAuthorizationFlagDefaults = 0
#kAuthorizationFlagInteractionAllowed = (1 << 0)
#kAuthorizationFlagPreAuthorize = (1 << 4)
#kAuthorizationFlagExtendRights = (1 << 1)
; -----------------------------------------------------------------------------------
; Structures
Structure AuthorizationItem
*name ;A zero-terminated string in UTF-8 encoding.
valueLength.i
*value
flags.i
EndStructure
Structure AuthorizationRights
AuthorizationItemSet.i
*AuthorizationRights
EndStructure
Structure CMD
*parameter1
*parameter2
*parameter3
*parameter4
*parameter5
*parameter6
*parameter7
*parameter8
cmd_terminator.i
EndStructure
ImportC "/System/Library/Frameworks/Security.framework/Security"
AuthorizationCreate(rights, environment, flags, *AuthorizationRef)
AuthorizationExecuteWithPrivileges(AuthorizationRef, cmd, flags, *arguments, file_ptr)
AuthorizationFree(authRef, flags)
AuthorizationCopyRights(authorization, *rights, *environment, flags.l, *authorizedRights)
EndImport
; -----------------------------------------------------------------------------------
Procedure _Login()
Protected authorizationRef.i, status.i, flags.i, *tool, result.i
Protected right.AuthorizationItem
Protected rights.AuthorizationRights
Protected ArgList.CMD
status = AuthorizationCreate(#Null, #kAuthorizationEmptyEnvironment, #kAuthorizationFlagDefaults, @authorizationRef)
If status <> #errAuthorizationSuccess
MessageRequester("Error", "Creating Initial Authorization: Errorcode " + Str(status), #PB_MessageRequester_Error)
Else
right\name = UTF8("system.privilege.admin")
rights\AuthorizationItemSet = 1
rights\AuthorizationRights = @right
flags = #kAuthorizationFlagDefaults | #kAuthorizationFlagInteractionAllowed | #kAuthorizationFlagPreAuthorize | #kAuthorizationFlagExtendRights
status = AuthorizationCopyRights(authorizationRef, @rights, #Null, flags, #Null);
If status <> #errAuthorizationSuccess
MessageRequester("Error", "Copy Authorization Rights: Errorcode " + Str(status), #PB_MessageRequester_Error)
End
EndIf
EndIf
*tool = UTF8(ProgramFilename())
ArgList\parameter1 = UTF8("1")
status = AuthorizationExecuteWithPrivileges(authorizationRef, *tool, #kAuthorizationFlagDefaults, @ArgList, #Null);
If status <> #errAuthorizationSuccess
MessageRequester("Error", "Execute With Privileges: Errorcode " + Str(status))
EndIf
End
EndProcedure
; -----------------------------------------------------------------------------------
Procedure Login()
If CountProgramParameters() = 0
_Login()
Else
ProcedureReturn #True
EndIf
EndProcedure
; -----------------------------------------------------------------------------------
EndModule
;- Begin Module RunAsAdmin
; ***************************************************************************************
CompilerIf #PB_Compiler_IsMainFile
;IncludeFile "snap7_server_mac.pb"
If RunAsAdmin::Login()
MessageRequester("Info", "Program run as admin!", #PB_MessageRequester_Info)
;Main()
EndIf
CompilerEndIf
Code: Select all
; Update link