Path: utzoo!mnetor!uunet!husc6!ut-sally!im4u!swrinde!petro!jrb
From: jrb@petro.UUCP (Jon Boede)
Newsgroups: comp.unix.questions
Subject: Re: A problem creating files...
Message-ID: <478@petro.UUCP>
Date: 8 Dec 87 17:36:26 GMT
References: <884@teksce.SCE.TEK.COM>
Sender: news@petro.UUCP
Reply-To: jrb@petro.UUCP (Jon Boede)
Organization: G.M. Andreen & Associates, Inc. San Antonio, Texas
Lines: 53
Keywords: ...as real user instead of effective user.
Summary: fork yourself :-)

In article <884@teksce.SCE.TEK.COM> johnhi@teksce.UUCP (John Higley) writes:
>I have a problem that I would like to pose to "the experts".

*I* probably shouldn't be replying to this, but... :-)

>Now for the problem:  The user of the application wants to be able to create
>an output file for certain information that he has found.  Unfortunately, the
>output file belongs to me (suid) and I do not have write permission in his
>directory.  What I would like to occur is that the output file belong to the
>real user (him) not the effective user (me).

I have delt with this problem by having the program fork(2) itself and then
have the child change it's u/gid back to what it's supposed to be...

	int status;

	switch (fork()) {
	case 0:
		setgid(getgid());
		setuid(getuid());
		/* open, write and close the file. */
		exit(successful ? 0 : 1);
	case -1:
		perror("reality collapse");
		break;
	default:
		wait(&status);
		/* check status to make sure. */
	}

One other trick along these lines that I liked was where the effective user
and the real user couldn't write into each other's directories but I wanted
the real user to be able to update "his entry" in the future... what I did was

link("/usr/effective/file","/usr/tmp/fileXXXXXX");
fork();
child process:
setuid(getuid()); setgid(getgid());
link("/usr/tmp/fileXXXXXX","/usr/real/the.copy");
unlink("/usr/tmp/fileXXXXXX");

Kind-of like building a brige over a 20' wide river by throwing two 10' ropes
from opposite banks :-)

If there's a better way to do this, I'm *very* interested -- mail me.

Hope it helps,
Jon
-- 
Jon Boede	...!{gatech,ihnp4,ssbn,swrinde,tness1,utanes}!petro!jrb
512/599-1847                           2555 N.E. Loop 410, #1403, 78217
	"People who are incapable of making decisions are
	 the ones who hit those barrels at freeway exits."