Sudo up your app with the GRoot.class :)

So you have written that new, must have program. Let us see it here.
Post Reply
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Sudo up your app with the GRoot.class :)

Post 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.
Last edited by BruceSteers on Sunday 4th October 2020 4:43pm, edited 1 time in total.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

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

Post 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.
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post 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...
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post 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
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post 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 :)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post 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.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

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

Post by cogier »

OK this version works. What use have you in mind for this?
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post 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 :)
Last edited by BruceSteers on Monday 5th October 2020 4:18pm, edited 1 time in total.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post 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 :)
If at first you don't succeed , try doing something differently.
BruceS
Post Reply