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: 361
Joined: Thursday 23rd July 2020 5:20pm

Sudo up your app with the GRoot.class :)

Post by BruceSteers » Sunday 04th October 2020 3:04am

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
groot.class.png (291.07 KiB) Viewed 1465 times
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.
Attachments
GRoot.class.zip
(1.39 KiB) Downloaded 47 times
Last edited by BruceSteers on Sunday 04th October 2020 4:43pm, edited 1 time in total.
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
cogier
Site Admin
Posts: 637
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

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

Post by cogier » Sunday 04th October 2020 2:01pm

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: 361
Joined: Thursday 23rd July 2020 5:20pm

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

Post by BruceSteers » Sunday 04th October 2020 4:40pm

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...
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 361
Joined: Thursday 23rd July 2020 5:20pm

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

Post by BruceSteers » Sunday 04th October 2020 4:56pm

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
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 361
Joined: Thursday 23rd July 2020 5:20pm

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

Post by BruceSteers » Sunday 04th October 2020 5:16pm

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 :)
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 361
Joined: Thursday 23rd July 2020 5:20pm

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

Post by BruceSteers » Sunday 04th October 2020 7:48pm

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.
Attachments
GRoot_class.zip
(2.54 KiB) Downloaded 52 times
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
cogier
Site Admin
Posts: 637
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

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

Post by cogier » Monday 05th October 2020 3:14pm

OK this version works. What use have you in mind for this?

User avatar
BruceSteers
Posts: 361
Joined: Thursday 23rd July 2020 5:20pm

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

Post by BruceSteers » Monday 05th October 2020 4:13pm

cogier wrote:
Monday 05th 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 05th October 2020 4:18pm, edited 1 time in total.
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 361
Joined: Thursday 23rd July 2020 5:20pm

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

Post by BruceSteers » Monday 05th October 2020 4:16pm

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 :)
Wishing well
Bruce

If at first you don't succeed , try it differently.

Post Reply