xref: /csrg-svn/lib/libc/compat-43/setreuid.c (revision 60986)
154846Smckusick /*
2*60986Sbostic  * Copyright (c) 1992, 1993
3*60986Sbostic  *	The Regents of the University of California.  All rights reserved.
454846Smckusick  *
554846Smckusick  * %sccs.include.redist.c%
654846Smckusick  */
754846Smckusick 
854846Smckusick #if defined(LIBC_SCCS) && !defined(lint)
9*60986Sbostic static char sccsid[] = "@(#)setreuid.c	8.1 (Berkeley) 06/02/93";
1054846Smckusick #endif /* LIBC_SCCS and not lint */
1154846Smckusick 
1254846Smckusick #include <sys/types.h>
1354846Smckusick #include <errno.h>
1454846Smckusick 
1554846Smckusick int
setreuid(ruid,euid)1654846Smckusick setreuid(ruid, euid)
1754846Smckusick 	uid_t ruid, euid;
1854846Smckusick {
1954846Smckusick 	static uid_t saveduid = -1;
2054846Smckusick 
2154846Smckusick 	if (saveduid == -1)
2254846Smckusick 		saveduid = geteuid();
2354846Smckusick 	/*
2454846Smckusick 	 * we assume that the intent here is to be able to
2554846Smckusick 	 * get back ruid priviledge. So we make sure that
2654846Smckusick 	 * we will be able to do so, but do not actually
2754846Smckusick 	 * set the ruid.
2854846Smckusick 	 */
2954846Smckusick 	if (ruid != -1 && ruid != getuid() && ruid != saveduid) {
3054846Smckusick 		errno = EPERM;
3154846Smckusick 		return (-1);
3254846Smckusick 	}
3354846Smckusick 	if (euid != -1 && seteuid(euid) < 0)
3454846Smckusick 		return (-1);
3554846Smckusick 	return (0);
3654846Smckusick }
37