maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   How to use root within script/program? (https://talk.maemo.org/showthread.php?t=70473)

AlMehdi 2011-02-28 23:17

How to use root within script/program?
 
I have been trying to find the proper way how to let a script have root permission. But haven't found an answer. I am trying to make it easier for people using the Awoken-Icon-theme changing the /usr/share/pixmaps/ icons. And am pretty much finish with the gui.

I don't want the user needing to open an terminal and do sudo gainroot. Even better if Rootsh wasn't needed at all. Right now all my solutions ends up in the use/installation of rootsh. Is there a way passed this?

Like the use of a sudoers file in "/etc/sudoers.d/". If i make this file it doesn't seam to recognize it. It still gives be permission denied when doing "/usr/bin/script".

Same goes for if i am putting:
Code:

if [ `id -u` != 0 ]; then
root<<EOF
/usr/bin/python /home/user/.scriptfolder/script.py
EOF
    exit $?
fi

or any other incarnation of this. This works if the user have Rootsh installed though. But what if he don't?

Also what is the proper way of giving "chmod +x"? Does it keep the executable permission through an install/upgrade?

I do not have scratchbox .. am doing all the testing on the n900.

Saturn 2011-02-28 23:46

Re: How to use root within script/program?
 
One way to achive that would be:

Make a sudoers file with this inside:
Code:

user ALL = NOPASSWD: /home/user/.scriptfolder/script.py
(Note it would be safer if the script was placed in /opt/somewhere and owned by root with 744 privileges though)

and place it in /etc/sudoers.d/

in the first line of your py code add this to make it execute directly:
Code:

#!/usr/bin/python2.5
from inside you code to execute the script as root:
Code:

os.system('sudo  /home/user/.scriptfolder/script.py')
Should work..

AlMehdi 2011-03-01 01:52

Re: How to use root within script/program?
 
Have pretty much done that already.. except for the "#!/usr/bin/python2.5" string.

I have mixed PyQT with Bash. Could that cause the problem? Basically i run an gui with python. It has three buttons. The action of the buttons lay in a sh-script. The sh-script contains some for-loops that cp/rm/mv files.

I have not gained enough competence to do that in python yet.. why i do it through a shell-script.

I will go through it again to see if i missed something.. thanks a lot for the reply ;)

9000 2011-03-01 02:02

Re: How to use root within script/program?
 
Quote:

Originally Posted by AlMehdi (Post 957671)
I have been trying to find the proper way how to let a script have root permission. But haven't found an answer. I am trying to make it easier for people using the Awoken-Icon-theme changing the /usr/share/pixmaps/ icons. And am pretty much finish with the gui.

I don't want the user needing to open an terminal and do sudo gainroot. Even better if Rootsh wasn't needed at all. Right now all my solutions ends up in the use/installation of rootsh. Is there a way passed this?

Like the use of a sudoers file in "/etc/sudoers.d/". If i make this file it doesn't seam to recognize it. It still gives be permission denied when doing "/usr/bin/script".

Same goes for if i am putting:
Code:

if [ `id -u` != 0 ]; then
root<<EOF
/usr/bin/python /home/user/.scriptfolder/script.py
EOF
    exit $?
fi

or any other incarnation of this. This works if the user have Rootsh installed though. But what if he don't?

Also what is the proper way of giving "chmod +x"? Does it keep the executable permission through an install/upgrade?

I do not have scratchbox .. am doing all the testing on the n900.

How about:

Code:

if [ `id -u` != 0 ]; then
exec root<<EOF
exec /usr/bin/python $0 $*
EOF
    exit $?
fi

Just for your food of thought.

rich c 2011-03-01 02:14

Re: How to use root within script/program?
 
Setuid maybe? Here's an explainiation I pretty much picked at random from a Google search.

Saturn 2011-03-01 07:19

Re: How to use root within script/program?
 
The GUI should be run as user and the three scripts as root. Otherwise the GUI will not have all graphic elements.

In the sudoers file you can have more than one scripts defined like (there is no line break):
Code:

user ALL = NOPASSWD: /somewhere/script1.sh /somewhere/script2.sh
If you want send me the code and I will have a look for you.

mece 2011-03-01 08:19

Re: How to use root within script/program?
 
without any modifications to sudoers and whatnot you can use run-standalone.sh like this:

sudo /usr/bin/run-standalone.sh /path/to/runthisscriptasroot.sh

That will run "runthisscriptasroot.sh" as root.

EDIT:
tested it with this script:

#!/bin/sh
whoami > whoami.txt

works :)

AlMehdi 2011-03-01 11:03

Re: How to use root within script/program?
 
Thanks for all of the suggestions

Update:

The run file is /usr/share/application/hildon/awoken.desktop. It uses the gui-file /opt/awoken-extras/awoken.py that uses /opt/awoken-extras/data.sh depending on what button you pressed.

By using the "run-standalone.sh" i am able to run the awoken.py file as root.. but when it want to execute data.sh it get a permission error. I have tried with both user and root permission on the awoken.py file.

So it is: awoken.desktop >> awoken.py >> data.sh

I have tried with and without the awkoen.sudoers in /etc/sudoers.d/

The files are in it's actual folder so if somebody want to try it on the n900 the only need is to unpack it to root. You should comment out the data.sh so no icons are changed.

The file is here: http://dl.dropbox.com/u/3143026/awok...e-1.1-5.tar.gz

Rob1n 2011-03-01 13:35

Re: How to use root within script/program?
 
Quote:

Originally Posted by AlMehdi (Post 957993)
I have tried with and without the awkoen.sudoers in /etc/sudoers.d/

Just to check, did you run update-sudoers after adding the file? It won't do anything otherwise.

AlMehdi 2011-03-01 18:28

Re: How to use root within script/program?
 
Quote:

Originally Posted by Rob1n (Post 958109)
Just to check, did you run update-sudoers after adding the file? It won't do anything otherwise.

Yes, multiple times. But it takes a while before it show up inside /etc/sudoers.

Saturn have looked upon my code and i think he solved it. The install file will contain a postinst file to sett the proper permissions.

ejasmudar 2011-03-05 05:20

Re: How to use root within script/program?
 
Quote:

Originally Posted by mece (Post 957873)
without any modifications to sudoers and whatnot you can use run-standalone.sh like this:

sudo /usr/bin/run-standalone.sh /path/to/runthisscriptasroot.sh

That will run "runthisscriptasroot.sh" as root.

EDIT:
tested it with this script:

#!/bin/sh
whoami > whoami.txt

works :)

Hmm, i am also having a similiar problem,
I tried
Code:

sudo /usr/bin/run-standalone.sh /opt/script.sh
but i am getting error:
Code:

standalone.sh: line 11: /opt/script.sh:permission denied.
Please help

Tiboric 2011-03-05 05:43

Re: How to use root within script/program?
 
Quote:

sudo run-standalone.sh /opt/shortcutmkr/main.py
is how i launch SCM with root privileges also have to
Quote:

sudo chmod +x main.py
to get it to run.

ejasmudar 2011-03-05 07:44

Re: How to use root within script/program?
 
Quote:

Originally Posted by Tiboric;961024[CODE
]sudo chmod +x main.py[/CODE]
to get it to run.

So do i make it so that sudo chmod +x script.sh is done automatically after install? I am trying to make a deb package

CharlesM 2011-03-05 08:52

Re: How to use root within script/program?
 
as root

chown root YOURFILE
chmod 4775 YOURFILE

voila!

ejasmudar 2011-03-05 09:29

Re: How to use root within script/program?
 
Quote:

Originally Posted by CharlesM (Post 961062)
as root

chown root YOURFILE
chmod 4775 YOURFILE

voila!

Yes, but these commands have to be performed by the users right?

What I want is, after instaling the deb file, the users should be able to start the app normally but it should have root priviledges. For refrerence, this are the scripts that I am planning to make into a package.
So, while creating the deb file, in the .desktop file, for exec=, what command should I give? Right now i tried with
Code:

sudo run-stanalone.sh /opt/script.sh
But no luck.

Am I doing something glaringly obvious and dumb here?

ejasmudar 2011-03-09 03:37

Re: How to use root within script/program?
 
OK, i have finally figured out how to run some of scripts as root, when started by user.
I have a bunch of 5 scripts, with a main script acting as the menu or gateway. The main script is launched normally via an icon as
Code:

sh parentscript.sh
in that script, it calls the other scripts as
Code:

sudo run-standalone.sh sh childscript1.sh
This methodology seems to be working for me. But one the users of this app is having a problem that xterm is asking for password on execution of sudo run-standalon.sh.

Can anybody help us?

kitwalker 2011-03-09 04:02

Re: How to use root within script/program?
 
There's another method that works great which I saw based on this:

http://talk.maemo.org/showpost.php?p...1&postcount=16

From the above link (disable_ts.zip)

If you put this in a script file:
Code:

sh -c 'echo "sleep 1; echo 1 > /sys/devices/platform/omap2_mcspi.1/spi1.0/disable_ts" | sudo gainroot'
echo 1 > /sys/devices/platform/omap2_mcspi.1/spi1.0/disable_ts should fail as permission denied if executed as normal user since it is modifying a sysfs file. But the above method of piping to sudo gainroot works!

ejasmudar 2011-03-09 04:50

Re: How to use root within script/program?
 
Hey thanks a lot! piping the command to sudo gainroot works like a charm!

Tiboric 2011-03-09 05:38

Re: How to use root within script/program?
 
Quote:

Originally Posted by ejasmudar (Post 961052)
So do i make it so that sudo chmod +x script.sh is done automatically after install? I am trying to make a deb package

yes in your postinstall script

ovekaaven 2011-03-16 23:02

Re: How to use root within script/program?
 
Setting permissions in the postinst is a band-aid. The correct permissions should already have been in the packed deb, i.e. the permissions should preferably be set in debian/rules or other makefile/buildscript, not in the package postinst.


All times are GMT. The time now is 14:09.

vBulletin® Version 3.8.8