xref: /original-bsd/lib/libc/compat-43/setreuid.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)setreuid.c	8.1 (Berkeley) 06/02/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/types.h>
13 #include <errno.h>
14 
15 int
16 setreuid(ruid, euid)
17 	uid_t ruid, euid;
18 {
19 	static uid_t saveduid = -1;
20 
21 	if (saveduid == -1)
22 		saveduid = geteuid();
23 	/*
24 	 * we assume that the intent here is to be able to
25 	 * get back ruid priviledge. So we make sure that
26 	 * we will be able to do so, but do not actually
27 	 * set the ruid.
28 	 */
29 	if (ruid != -1 && ruid != getuid() && ruid != saveduid) {
30 		errno = EPERM;
31 		return (-1);
32 	}
33 	if (euid != -1 && seteuid(euid) < 0)
34 		return (-1);
35 	return (0);
36 }
37