LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-30-2005, 02:37 AM   #1
arip
LQ Newbie
 
Registered: Nov 2005
Location: Helsinki, Finland
Distribution: Ubuntu
Posts: 3

Rep: Reputation: 0
Executing program as non-root


I have following question: Program A has to be run as root. Program A executes program B, which must not be run as root.

A: -rwsr-xr-x root root
B: -rwxr-xr-x user users

A:
Code:
if(pid=fork() == 0)
{
    // here euid=0, uid=29999
    seteuid(getuid());     // trying to become a user
    execl("./B");
    exit(0);
}
else
{
    setuid(0);
    // do something
    waitpid(pid,&ret,0);
}
B behaves like it has been started from shell as root (~# B), and won't run. B runs if it is started as user (~$ B). How can I run B as user?
 
Old 11-30-2005, 05:40 AM   #2
NoobieDoobieDo
Member
 
Registered: Oct 2004
Distribution: Debian 4.0
Posts: 65

Rep: Reputation: 15
Make program A say this :

sudo -u NONROOTUSER /path/to/program
 
Old 11-30-2005, 06:24 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

/* 
   ... 
   ... 
   ...
*/

uid_t newuid;
gid_t newgid;

newuid = 500;
newgid = 500;

/* Drop root UID: */
setregid(newgid, newgid);
if (getgid() == 0 || getegid() == 0) {
    perror("Failed to drop GID root");
    exit(1);
}
/* Drop root GID: */
setuid(newuid);
if (getuid() == 0 || geteuid() == 0) {
    perror("Failed to drop UID root");
    exit(1);
}

/* Do things as user with newuid/newgid here. */
 
Old 12-01-2005, 03:47 AM   #4
arip
LQ Newbie
 
Registered: Nov 2005
Location: Helsinki, Finland
Distribution: Ubuntu
Posts: 3

Original Poster
Rep: Reputation: 0
I give some more details for this case:

The device i'm programming is Nokia 770 Linux-based internet tablet. There are two different reasons the machine refuses to run a program:
1) Program that uses GTK library can't be run as setuid(0). When I start a GTK program with execl from setuid(0) program I have to first set those UIDs to non-root, and the program starts, but:
2) If I try to run GTK program from root-prompt, it gives error message that it can't load some theme-files. This is the way the program behaves when I try to run it from setuid(0) program and UIDs set to non-root.

For that "sudo -u..." thing: I get error mesage: "Sorry, user user is not allowed to execute './program" as user on Nokia 770-45."

-Ari
 
Old 12-01-2005, 05:41 AM   #5
arip
LQ Newbie
 
Registered: Nov 2005
Location: Helsinki, Finland
Distribution: Ubuntu
Posts: 3

Original Poster
Rep: Reputation: 0
Re: Executing program as non-root

Quote:
Originally posted by arip
I have following question: Program A has to be run as root. Program A executes program B, which must not be run as root.

A: -rwsr-xr-x root root
B: -rwxr-xr-x user users

A:
Code:
if(pid=fork() == 0)
{
    // here euid=0, uid=29999
    seteuid(getuid());     // trying to become a user
    execl("./B");
    exit(0);
}
else
{
    setuid(0);
    // do something
    waitpid(pid,&ret,0);
}
B behaves like it has been started from shell as root (~# B), and won't run. B runs if it is started as user (~$ B). How can I run B as user?
Solution for this seems to be that I run B first, and then B starts A
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
executing a program as root funkymunky Linux - General 2 05-06-2005 12:48 PM
Writing and executing a program Russb Linux - Software 1 07-25-2004 06:49 PM
executing a program in new terminal? kashifbilal Programming 7 04-26-2004 04:52 AM
Servlets executing a C program. webee Programming 1 02-04-2003 05:19 PM
Executing a program with out system() in c++ sobdk Programming 2 11-02-2002 07:45 AM


All times are GMT -5. The time now is 11:37 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration