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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a file with setuid from Perl script

Status
Not open for further replies.

redrafiki

Programmer
Oct 26, 2010
5
US
Hello, I have a perl script that runs in a Linux system, it basically reads a list of files and attributes and copies them to the right directory and set the attributes accordingly.

The script works fine with files with "regular" attributes like 0755, I'm doing something like this:
copy($filezip, $filetarget) or die "File cannot be copied.";
chmod oct($fileperms),$filetarget or die "Perms not changed.";
chown $uid, $gid, $filetarget;

But when the file has attributes that set the suid permissions (4755) the target file is not set with these attributes.

I tried running chmod from a system call and it didn't work:
system("/bin/chmod $fileperms $filetarget");

I tried using a umask as follows and it didn't work either:
my $perms = ((stat($filezip))[2] & 07777);
my $newperms = $perms ^ 04000;
chmod $newperms, $filetarget or die "Perms not changed.";

Does anyone knows how to set the suid permission from a perl script?

Thanks
 
Strange as this works for me ....

Code:
-bash-3.00$ cat testperl.pl
#!/bin/perl

system("/bin/touch fredtest.tat");
system("/bin/chmod 4777 fredtest.tat");


-bash-3.00$
-bash-3.00$ perl ./testperl.pl


-bash-3.00$ ls -alrth
total 10
drwxr-xr-x  40 fred  other       2.5K May 12 11:16 ..
-rw-r--r--   1 fred  other         89 May 12 11:26 testperl.pl
drwxr-xr-x   2 fred  other        512 May 12 11:28 .
-rwsrwxrwx   1 fred  other          0 May 12 11:28 fredtest.tat
-bash-3.00$

So maybe it's where you get your variables, are they strings? I suggest printing them to confirm.

I hope that helps

Laurie.
 
Thanks Laurie, I found the answer for this already.

chown always resets the set-uid and set-gid values, so it always has to run before chmod in order to set the set-uid.

I ran the chown command first then chmod and it worked.

Thanks

I think this post can be closed now
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top