1

I understand my question is not that clear so let me try explaining it here.

What I am trying to do is suspend my computer after completing a certain task.

My script:

#my logic or task(Downloading a file)#
cmd = shlex.split("sudo pm-suspend")
>>> subprocess.call(cmd)
[sudo] password for noob: 

As you can see it asks for my password but the problem is I will not be sitting on my system while the script is getting executed.

So, my question is how to handle this problem or what other alternative way exist to tackle this problem.

Python version is 2.7.3.

RanRag
  • 48,359
  • 38
  • 114
  • 167

2 Answers2

3

Assuming your pm-suspend is at /usr/sbin/pm-suspend

Make your sudoers entry in /etc/sudoers:

<username> ALL=(ALL) NOPASSWD: /usr/sbin/pm-suspend
  • I believe by sudoers you meam `visudo`. – RanRag Jun 19 '12 at 18:07
  • 1
    @Noob I actually meant the `/etc/sudoers` file in general. You can edit it with whatever you like. –  Jun 19 '12 at 18:10
  • I added `NOPASSWD: /usr/sbin/pm-suspend` to `/etc/sudoers`. Now I am getting `sudo: >>> /etc/sudoers: syntax error near line 31 <<< sudo: parse error in /etc/sudoers near line 31 sudo: no valid sudoers sources found, quitting sudo: unable to initialize policy plugin ` – RanRag Jun 19 '12 at 18:12
  • @Noob I updated the answer. Does your line look like that? Also, do a `locate pm-suspend` to find the correct path for pm-suspend. –  Jun 19 '12 at 18:14
  • I did use locate it showed `/usr/sbin/pm-suspend` but I am not able to edit the `sudoers` file because of the above mentioned error. – RanRag Jun 19 '12 at 18:16
  • @Noob - Michael is correct: fixing /etc/sudoers is definitely the right way to go. Here's one easy solution for fixing the file: http://askubuntu.com/questions/73864/how-to-modify-a-invalid-etc-sudoers-file-it-throws-out-anderror-and-not-allowi – paulsm4 Jun 19 '12 at 18:18
  • 1
    @Noob Edited again. That should be the correct command. If the sudoers file is not allowing you to edit it you may need to boot in recovery mode to make the appropriate changes. –  Jun 19 '12 at 18:20
2

You can use the SetUID bit to cause a script to run with the permissions of the owner of the file. You can allow a specific file to always run as root by changing its owner to root and setting its SetUID bit. To set the SetUID bit:

chmod 4755 <filename>

masks: 4000 is SetUID, 0700 is owner rwx, 0050 and 0005 are group and world rx.

It is imperative that you make sure that users other than the owner cannot edit this file, because if they can, they will be able to run arbitrary commands as your user, which is a security risk.

To be effective as you need it, you must also set the file's owner to root:

sudo chown root <filename>

In this case, <filename> should be whatever script you intend to run. It must be executable - if it is not, i.e. you are trying to run a python program not set up to be executed standalone, you will need to use a wrapper that launches it.

More information: http://en.wikipedia.org/wiki/Setuid

Be careful, there are a number of security risks associated with using the SetUID bit. Post further comments if you need clarification.

A commenter has pointed out that in all likelihood, this will not work for shell scripts. You will instead need to use a wrapper that will call your process from a compiled language such as C or C++.

/* setuid_wrapper.cpp */
#include <unistd.h>

int main(int c, char * v[])
{
    // the program to execute
    // replace this with the program you want to call.
    const char * executable = "/bin/false";

    // arguments to pass to program
    // MUST be null terminated, MUST start with executable path
    const char * arguments[] = {executable, "arg1", "arg2", "etc...", NULL};

    execv(executable, arguments);
    perror("execv: ");
    return 1;
}

compile with:

g++ -o setuid_wrapper setuid_wrapper.cpp

Follow the directions earlier to change its owner to root and set the SetUID bit, and configure your system to run it instead of your script when needed.

Wug
  • 12,956
  • 4
  • 34
  • 54
  • huh, really? You're sure this isnt a shell specific behavior? I'm sure I've done it before... edit- maybe its distro specific. That would make sense. – Wug Jun 19 '12 at 18:23
  • http://nixshell.wordpress.com/2007/04/21/suid-shell-scripts-setting-the-sticky-bit/ – Keith Jun 19 '12 at 18:26
  • It's OS specific. Some older Unixes did allow it. Linux does not. Try it and see. – Keith Jun 19 '12 at 18:34