It is currently Mon Nov 12, 2018 7:00 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Module RunAsAdmin (root rights)
PostPosted: Thu Nov 08, 2018 7:49 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1437
Location: Germany
Sometime we need root rights for programs

Now for OSX and Linux. Only Window is not ready...

Update v1.03
- New login window for linux

Code:
;-TOP

; Comment : Module RunAsAdmin
; Author  : mk-soft
; Version : v1.03
; Date    : 09.11.2018
; OS      : OSX, Linux

; Thanks to:
; - Airr
; - Wolfram

EnableExplicit

; ***************************************************************************************

;- Begin Module RunAsAdmin

DeclareModule RunAsAdmin
  Declare Login()
EndDeclareModule

; ---

Module RunAsAdmin
 
  ; -----------------------------------------------------------------------------------
 
  CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
    ; 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
  CompilerEndIf
 
  ; -----------------------------------------------------------------------------------
 
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      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", "No 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
    CompilerCase #PB_OS_Linux
     
      Global WinLogin
      Global Label_Prog, Text_Program, Label_User, Label_Passwd, String_User, String_Passwd, Button_Ok
     
      ; -------------------------------------------------------------------------------
     
      Procedure OpenWinLogin(x = 100, y = 100, width = 420, height = 180)
        WinLogin = OpenWindow(#PB_Any, x, y, width, height, "Login", #PB_Window_SystemMenu)
        Label_Prog = TextGadget(#PB_Any, 10, 10, 90, 25, "Program:")
        Text_Program = TextGadget(#PB_Any, 110, 10, 300, 25, "")
        Label_User = TextGadget(#PB_Any, 10, 50, 90, 25, "User:")
        Label_Passwd = TextGadget(#PB_Any, 10, 80, 90, 25, "Password:")
        String_User = StringGadget(#PB_Any, 110, 50, 300, 25, "")
        String_Passwd = StringGadget(#PB_Any, 110, 80, 300, 25, "", #PB_String_Password)
        Button_Ok = ButtonGadget(#PB_Any, 310, 130, 100, 30, "Ok")
      EndProcedure

      ; -------------------------------------------------------------------------------
     
      Procedure _Login()
        Protected user.s, passwd.s, cmd.s
       
        OpenWinLogin()
        SetGadgetText(Text_Program, GetFilePart(ProgramFilename()))
        SetGadgetText(String_User, UserName())
       
        Repeat
          Select WaitWindowEvent()
            Case #PB_Event_CloseWindow
              End
            Case #PB_Event_Gadget
              If EventGadget() = Button_Ok
                Break
              EndIf
          EndSelect
        ForEver
        user = GetGadgetText(String_User)
        passwd = GetGadgetText(String_Passwd)
        CloseWindow(WinLogin)
       
        If user = UserName()
          cmd.s = "-c " + #DQUOTE$ + "echo " + passwd + " | sudo -S " + ProgramFilename() + " 1" + #DQUOTE$
        Else
          cmd.s = "-c " + #DQUOTE$ + "echo " + passwd + " | sudo -u " + user + " -S " + ProgramFilename() + " 1" + #DQUOTE$
        EndIf
        If RunProgram("bash", cmd, "")
          End
        Else
          MessageRequester("Error", "Execute With Privileges!", #PB_MessageRequester_Error)
          End
        EndIf
     
      EndProcedure
    CompilerCase #PB_OS_Windows
      Procedure _Login()
        ;TODO
        ProcedureReturn 1
      EndProcedure
     
  CompilerEndSelect
 
  ; -----------------------------------------------------------------------------------
 
  Procedure Login()
    If CountProgramParameters() = 0
      ProcedureReturn _Login()
    Else
      ProcedureReturn #True
    EndIf
  EndProcedure
 
  ; -----------------------------------------------------------------------------------
 
EndModule

;- End Module RunAsAdmin

; ***************************************************************************************

CompilerIf #PB_Compiler_IsMainFile
 
  ;IncludeFile "..."
 
  If RunAsAdmin::Login()
    MessageRequester("Info", "Program run as admin!", #PB_MessageRequester_Info)
  EndIf
 
CompilerEndIf

_________________
My Projects OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer /
PB v3.30 / v5.60 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Module RunAsAdmin (root rights)
PostPosted: Fri Nov 09, 2018 1:16 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1437
Location: Germany
Update v1.03
- New login window for linux

_________________
My Projects OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer /
PB v3.30 / v5.60 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Module RunAsAdmin (root rights)
PostPosted: Sun Nov 11, 2018 2:24 pm 
Offline
Enthusiast
Enthusiast

Joined: Mon Jan 20, 2014 6:32 pm
Posts: 177
Maybe use pkexec on linux?

https://linux.die.net/man/1/pkexec

_________________
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA


Top
 Profile  
Reply with quote  
 Post subject: Re: Module RunAsAdmin (root rights)
PostPosted: Sun Nov 11, 2018 3:05 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1437
Location: Germany
I had a look at pkexec.
For "pkexec" and GUI applications you have to create a policy file.
So you can't start the program "gedit" without this "police file".

_________________
My Projects OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer /
PB v3.30 / v5.60 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye