running SU commands in an app

Post your Gambas programming questions here.
Post Reply
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

running SU commands in an app

Post by BruceSteers »

Is there ANY possible way to get a gambas program to raise it's su privilages after launch?
I have a program doing some copy/move/delete commands that may or may not be in root only accessible locations.
I don't want to force the app to be run as root but would be handy if it could get su privileges once run if needed.

My currently used option is to gather any commands that need to be run as root into a temporary bash script then run the script via Desktop.RunAsRoot (pkexec) but this will ask for password each time i run a command,

I'm working on this solution... (it's a bit hacky but it works)..
I have a hidden TerminalView object that runs bash through pkexec so a one time password request activates the hidden su bash terminal and then I can run commands through it at will without having to authorize again.

Problems are output text comes with all the terminal escape chars/codes and needs filtering (not a problem if output is not needed) and i'm limited to only using shell commands not gambas internal ones.

anyone ever worked on having a gambas app raise it's privileges?
Ideally i'd like to up the level then use gambas internal copy/move methods rather than shell commands.

Attached is a rough tester for what i have so far. it's simply a textbox with some buttons and a textarea. enter the command you want to run in the textbox and press a button to run it, output goes in the textarea
you can run a command as root and it will ask for password, then running further commands does not as it goes through the hidden TerminalView with superuser bash.

I was getting truncated output. not sure if anyone can improve on my Process_Read() function.

Anyone got a better solution for elevating to SU after program is started?
Respects
Last edited by BruceSteers on Saturday 4th June 2022 3:58pm, 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: running SU commands in an app

Post by cogier »

I have not tried this, but if you wrote a CLI Gambas app to do the 'copy/move/delete' and create an executable file of the code. Then from a main GUI program, run the executable using sudo sending the necessary details of what files to 'copy/move/delete'. You could create a file to store any data you wish to return to the main program. I started writing a program to do copies only, for safety reasons, but discovered that copy does not require sudo if you copy from root to your Home folders.
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: running SU commands in an app

Post by BruceSteers »

cogier wrote: Friday 3rd June 2022 3:52pm I have not tried this, but if you wrote a CLI Gambas app to do the 'copy/move/delete' and create an executable file of the code. Then from a main GUI program, run the executable using sudo sending the necessary details of what files to 'copy/move/delete'. You could create a file to store any data you wish to return to the main program. I started writing a program to do copies only, for safety reasons, but discovered that copy does not require sudo if you copy from root to your Home folders.
That's kinda what this class does, except it does not need a separate CLI app as it has it's own internal terminal running an authorized bash.

It has pros and cons.
pros.
as your program runs it will ask for authorization once then be authorized for further commands unless .End() method is used to kill the auth.

cons.
The TerminalView process (Process.State, Process.Value etc) are pretty useless for the commands being run as the command run in the terminal is bash and the Process data refers to that not the cp/mv or whatever is being run by bash.

I've got over it by making any command sent to it add a trailing command " echo '*|com-done*|:'$?" because I am able to monitor the terminal output. so i set a flag as Running before a command is sent then monitor output for the *|com-done*|:$? echo.
Then set is as finished and get the commands $? error value.

I'm also using the above com-done trick to have a working "Wait" flag too like using Shell sCommand Wait.

updated version source is attached...


I've made .Shell() take a "Wait" argument
bash is now run with --norc option and colorizing aliases removed to save having to filter any xterm color codes.

after a command is run it populates a .Com_Value property (error value) and .Com_Text (command text output)
Fixed the truncating output problem.

Added wiki help lines to explain functions.
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: running SU commands in an app

Post by BruceSteers »

I loaded on Debian (was made on mint) and was getting issues that i have addressed. (V1.0.4)

EDIT:
(V1.0.6)

tested on a few systems now fedora, gambas 3.12 on debian buster
had to tweak a few things and workaround some quirks.

Commands that you will want the output will need to be tested.
It should pretty much work okay for commands that output is not needed like cp/mv and stuff

Todo:
the escape code removing routine needs to be completed to logically handle all codes. (i think i've handled most of the normal CSI stuff)
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: running SU commands in an app

Post by BruceSteers »

see above msg edit..

I have copied the terminal line processing routines from VT100 class and edited them to not do anything but strip the codes out.
It should be fairly robust now.
RootShell-1.1.tar.gz
(17.84 KiB) Downloaded 108 times
If at first you don't succeed , try doing something differently.
BruceS
Post Reply