you may use sudo. please see
http://www.gratisoft.us/su
Main Topics
Browse All TopicsI'm writing an app that needs to read the MBR record of the disk, for this it must have root access but it must only be run by a user.
Command line solutions are not an option.
Is there a way of temporarily impersonating root via code in my program?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
you may use sudo. please see
http://www.gratisoft.us/su
please read the second line of the first comment.....
You can start an other program (with root preperties, that can only read the boot sector of a disk and write it into a pipeline you read in your own program.....)
the way it works is: (sortof, you have to readup on details for pipe(), fork(), execv*()...)
int channels[2];
pipe(channels)
if (fork()==0) {
close (channel[1]);
close(1);
dup(channel[0]);
close(channel[0]);
execv("/opt/myapp/mbrreade
exit(200);
} else {
close(channels[0]);
read(channels[1], mbrbuffer,sizeof(mbrbuffer
close (channels[1]);
}
your mbrreader.c, after compilation: chown root mbrreader, chmod o+sx mbrreader.
main()
{
fd= open(argv[1],"r");
read(fd,mbr,512);
write(1,mbr,512);
exit(0);
}
Check for errors etc..
I've been fiddling with the below code, and still cant get it going!
Can you see anything wrong with it?
Cheers,
KB.
Makefile to suite, if you care to compile...
--------CUT-----------
all: printmbr
printmbr: printmbr.o
gcc printmbr.o -o printmbr
printmbr.o: printmbr.c
gcc -c printmbr.c
clean:
rm -rf *o printmbr
--------CUT-----------
Work well, if your UID/GID = 0....
If I su to a test user, it doesn't work which is what I require...
For a little more insight into the project,
I'm running Apache/PHP, I really don't want the apache/php process to have root privileges since it makes security so much more complicated!
I require a php script to call an executable that outputs data in a pre-defined format (Readable from my php script), but that executable requires root privileges to read the mbr of any given disk...
This is why sudo cannot work (Apache/PHP has no tty environment, sudo requires one).
Hope the context allows you to give me an alternate solution since the above, tho close, does not give me what I need.
Cheers,
KB.
THe program is one thing, the thing to make it work is:Assuming your printmbr is still named the same and the tool you write to read one block from the first diskblock is called readmbr:
chown root:root readmbr # make root owner
chmod ugo+x readmbr # make exec. for any one
chmod ugo-rw readmbr # remove write & read access
chmod u+s readmbr # make setuid
Now the readmbr will run as root where your main program doesn't.
note NOT printmbr as you made it should run as root, but the '/bin/echo' part
only...
ls -l of directory contents:
-rw-r--r-- 1 root root 258 2009-11-05 11:36 Makefile
-rwxr-xr-x 1 root root 6323 2009-11-10 18:58 printmbr
-rw-r--r-- 1 root root 1380 2009-11-10 18:58 printmbr.c
-rw-r--r-- 1 root root 1932 2009-11-10 18:58 printmbr.o
-rws--x--x 1 root root 5494 2009-11-05 22:43 readmbr
-rw-r--r-- 1 root root 843 2009-11-05 22:43 readmbr.c
-rw-r--r-- 1 root root 1364 2009-11-05 22:43 readmbr.o
Output of ./printmbr as user 'test'
[test@localhost printmbr]$ ./printmbr
Cannot open /dev/sda
fread(PIPE_PARENT): 0, errno = 29
Value = 0
Result = ''
[test@localhost printmbr]$
Is there something I'm missing?
Do I need to setuid() within readmbr?
Business Accounts
Answer for Membership
by: nociPosted on 2009-11-04 at 05:10:34ID: 25738895
Yes you can do it through a commandline using sudo + sudo config. run something like
system("sudo dd if=/dev/sda of=/tmp/mymbr bs=512 count=1").
Or you can write a small program, that has setuid bit set and is owned by root, causing it to run with root uid.
Dont make the whole app privileged, otherwise you will have to audit it for buffer overflows and other misshaps to prevent accidental root access. (anything started by that program will f.e. run also as root).