Sudo up your app with the GRoot.class :)

Post

Posted
Rating:
#1 (In Topic #477)
Banned
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.
(attachment removed)

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…

Code (gambas)

  1. Public Sub _new()
  2.  
  3.  If GRoot.GetRoot() Then Me.Close()
  4.  
  5. ' Only continues if root
  6.  

for an app that can run without root as well…

Code (gambas)

  1. Public Sub _new()
  2.  
  3.  If GRoot.GetRoot() Then
  4.   If Not GRoot.Cancelled Then
  5.    Me.Close()
  6.    Return
  7.  
  8. ' Continues with or without root access
  9.  

Shimples :)

If anyone knows of a better way to make an app get it's own root access please tell me.
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’
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.
Online now: No Back to the top

Post

Posted
Rating:
#3
Banned
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…
Online now: No Back to the top

Post

Posted
Rating:
#4
Banned
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

Code (gambas)

  1. Public Sub Main()
  2. If GRoot.GetRoot() Then Return
  3.  
  4. ' rest of code
Online now: No Back to the top

Post

Posted
Rating:
#5
Banned
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 :)
Online now: No Back to the top

Post

Posted
Rating:
#6
Banned
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.
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’
OK this version works. What use have you in mind for this?
Online now: No Back to the top

Post

Posted
Rating:
#8
Banned

cogier said

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 :)
Online now: No Back to the top

Post

Posted
Rating:
#9
Banned
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 :)
Online now: No Back to the top
1 guest and 0 members have just viewed this.