Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setuid and fork.

Status
Not open for further replies.

bdw238

MIS
Dec 15, 2005
52
GB
#!/usr/bin/perl -w

Can someone advise me please I am writing a script (run as root) that forks many child process's, which need to run as different user (ftppub, sftppub).


I need the main code of script to stay as root, whereas the forked processes have a uid of sftppub, ftppub. Can this been done?

Regards

Brian



e.g.


sub test {

my $pid;
unless ($pid = fork()) {
my ($login,$pass,$uid,$gid) = getpwname('ftppub');
$) = $gid;
$) = $gid;
$< = $uid;
$> = $uid;
print "E and R UID $< $>"; ## uid of ftppub
Do Something //
}
return $pid
}


sub test2 {

my $pid;
unless ($pid = fork()) {
my ($login,$pass,$uid,$gid) = getpwname('ftppub');
$) = $gid;
$) = $gid;
$< = $uid;
$> = $uid;
print "E and R UID $< $>"; ## uid of ftppub
Do Something //
}
return $pid
}


for (my $i=0;$i<10;$i++) {
test();
print "E and R UID $< $>"; // Should show root ID.
test2();
print "E and R UID $< $>"; // Should show root ID.
}
exit 1;



}
 
I discovered that you can not do this easily, reading the man setuid.

So decided to use perl expect to su to user and then run command with su -c arg.



Regards

Brian

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top