Page 1 of 1

Sudo up your app with the GRoot.class :)

Posted: Sunday 4th October 2020 3:04am
by BruceSteers
So i wrote another little class to get a gambas app to sudo up on launch.

Works on the same principal as my GRoot app , no messing about with pkexec policies or anything like that this time.
It just re-launches using sudo in it's own hidden terminal that closes when the app closes.

It checks if you're root
if not it pops open a password asker.
groot.class.png
Just add this code to your Startup class or FMain

PS. As Cogier pointed out (Cheers dude) you also need to add the gb.form.terminal component in the project settings.

For a sudo only app...
Public Sub _new()

 If GRoot.GetRoot() Then Me.Close()

' Only continues if root
End
for an app that can run without root as well...
Public Sub _new()

 If GRoot.GetRoot() Then 
  If Not GRoot.Cancelled Then
   Me.Close()
   Return
  Endif
 Endif

' Continues with or without root access
End
Shimples :)

If anyone knows of a better way to make an app get it's own root access please tell me.

Re: Sudo up your app with the GRoot.class :)

Posted: Sunday 4th October 2020 2:01pm
by cogier
Hi Bruce,

I can't get your program to work. I've added gb.form.terminal and copied the commented sub to the FMain, the program pops up the 'Enter password box' but I can't enter text or click on any of the buttons! What am I doing wrong?

Have a look at my effort regarding 'sudo' here. It's not quite what you are doing but might offer some insight.

Re: Sudo up your app with the GRoot.class :)

Posted: Sunday 4th October 2020 4:40pm
by BruceSteers
Hi Charlie.
I've no idea why it isn't working.
I just added it to one of my apps and it worked like a charm.
(I had to also add the TerminalView component) I better add that to the info i guess, cheers :)

but nothing should be disabled?

Only thing springs to mind is i have not tested on it on a non standard project that does not have FMain.class as it's startup class.

I'll do some tests...

Re: Sudo up your app with the GRoot.class :)

Posted: Sunday 4th October 2020 4:56pm
by BruceSteers
Okay did dome tests on a project with no FMain.class just a Startup.class file.

In that case the Public Sub _new() method does not work and the Me.Close() fails as there's no form.

So instead it goes in the Main() Sub
and rather than "Me.Close" just Return
Public Sub Main()
If GRoot.GetRoot() Then Return

' rest of code
End

Re: Sudo up your app with the GRoot.class :)

Posted: Sunday 4th October 2020 5:16pm
by BruceSteers
Have a look at my effort regarding 'sudo' here. It's not quite what you are doing but might offer some insight.
Nice , yes different goal though.

This class is for specifically allowing a gambas GUI to ask for root and elevate on launch.
Like so many apps do :)

I thought i'd cracked it, :oops: would love to know what's going on with your trial.
maybe post me the project your trying it with so i can experiment and iron out the kinks :)
Cheers :)

Re: Sudo up your app with the GRoot.class :)

Posted: Sunday 4th October 2020 7:48pm
by BruceSteers
Okay a few tweaks on this version..

Mostly i removed the Process pointer as it wasn't really being used.

I also added the Application.Daemon = True line so it works in a world of it's own.
Using that method stops any stdout or error messages though as it detaches from the calling terminal but to be fair it's designed to be used on a GUI app that would not return shell messages and generally be launched from a launcher not terminal. but if you're testing debugging your app you can comment out the line.
It puts out some warning messages i think because the TerminalView isn't visible but it seems to work okay regardless.

And I made a ReadMe file.

Re: Sudo up your app with the GRoot.class :)

Posted: Monday 5th October 2020 3:14pm
by cogier
OK this version works. What use have you in mind for this?

Re: Sudo up your app with the GRoot.class :)

Posted: Monday 5th October 2020 4:13pm
by BruceSteers
cogier wrote: Monday 5th October 2020 3:14pm OK this version works. What use have you in mind for this?
It allows any gambas app to launch as root without having to sudo from terminal. or if not launching from terminal setting up pkexec policies and stuff to enable GUI only sudo.
Just a double click of the app icon or setting up a menu item/desktop launcher will not need a prefix like sudo or pkexec.
App asks for root itself with this method.

An app like my GrubHack app that needs root , now i just launch it however i like and it asks for password and elevates :)

Re: Sudo up your app with the GRoot.class :)

Posted: Monday 5th October 2020 4:16pm
by BruceSteers
Am currently trying to figure out a way to get it to test if password was correct and retry if not.

Then i think it'll be complete :)