[net.unix-wizards] updating an executing setid file when not root

steffen@ihu1h.UUCP (Joe Steffen) (08/09/84)

I'm porting a System V program to BSD 4.2 and ran into this problem.  How
do you create a file with the setid bits set from a program that is setid to
a login other than root?  In System III and V this will do it:

	fd = open(target, O_WRONLY | O_CREAT | O_EXCL, 06711);
	
BSD does not have this form of open, so I tried:

	fd = creat(target, 06711);

but the setid bits were ignored.  I added

	chmod(target, 06711);
	
but the setid bits were still ignored.

This program is executed by a command set with a file over a network.  The
program verifies a passwd in the command, and the replaces a file in the
setid login with the file received from the network.

Since the file may be executing, it is linked to a temp name,
the real name is unlinked, and a new file is created with the real name and
the old permissions.  Since this program needs to update itself
occasionally, it needs to set the setid bits in the created file.

-- 


	Joe Steffen, AT&T Bell Labs, Naperville, IL, (312) 979-5381

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (08/20/84)

?  4.2BSD DOES have the new three-parameter open(2).
You can only set the set-[ug]id bits if the effective ID matches
to file-owner ID, or if the effective ID is 0.  One thing that may
be tripping you up is that any write to the file will clear the
set-[ug]id mode bits (this is another 4.2BSD incompatible feature).
Try chmod(2) after you have finished writing the file.