xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/set_eugid.c (revision 41fbaed053f8fbfdf9d2a4ee0a7386a3c83f8505)
1*41fbaed0Stron /*	$NetBSD: set_eugid.c,v 1.1.1.1 2009/06/23 10:09:00 tron Exp $	*/
2*41fbaed0Stron 
3*41fbaed0Stron /*++
4*41fbaed0Stron /* NAME
5*41fbaed0Stron /*	set_eugid 3
6*41fbaed0Stron /* SUMMARY
7*41fbaed0Stron /*	set effective user and group attributes
8*41fbaed0Stron /* SYNOPSIS
9*41fbaed0Stron /*	#include <set_eugid.h>
10*41fbaed0Stron /*
11*41fbaed0Stron /*	void	set_eugid(euid, egid)
12*41fbaed0Stron /*	uid_t	euid;
13*41fbaed0Stron /*	gid_t	egid;
14*41fbaed0Stron /*
15*41fbaed0Stron /*	void	SAVE_AND_SET_EUGID(uid, gid)
16*41fbaed0Stron /*	uid_t	uid;
17*41fbaed0Stron /*	gid_t gid;
18*41fbaed0Stron /*
19*41fbaed0Stron /*	void	RESTORE_SAVED_EUGID()
20*41fbaed0Stron /* DESCRIPTION
21*41fbaed0Stron /*	set_eugid() sets the effective user and group process attributes
22*41fbaed0Stron /*	and updates the process group access list to be just the specified
23*41fbaed0Stron /*	effective group id.
24*41fbaed0Stron /*
25*41fbaed0Stron /*	SAVE_AND_SET_EUGID() opens a block that executes with the
26*41fbaed0Stron /*	specified privilege. RESTORE_SAVED_EUGID() closes the block.
27*41fbaed0Stron /* DIAGNOSTICS
28*41fbaed0Stron /*	All system call errors are fatal.
29*41fbaed0Stron /* SEE ALSO
30*41fbaed0Stron /*	seteuid(2), setegid(2), setgroups(2)
31*41fbaed0Stron /* LICENSE
32*41fbaed0Stron /* .ad
33*41fbaed0Stron /* .fi
34*41fbaed0Stron /*	The Secure Mailer license must be distributed with this software.
35*41fbaed0Stron /* AUTHOR(S)
36*41fbaed0Stron /*	Wietse Venema
37*41fbaed0Stron /*	IBM T.J. Watson Research
38*41fbaed0Stron /*	P.O. Box 704
39*41fbaed0Stron /*	Yorktown Heights, NY 10598, USA
40*41fbaed0Stron /*--*/
41*41fbaed0Stron 
42*41fbaed0Stron /* System library. */
43*41fbaed0Stron 
44*41fbaed0Stron #include <sys_defs.h>
45*41fbaed0Stron #include <unistd.h>
46*41fbaed0Stron #include <grp.h>
47*41fbaed0Stron #include <errno.h>
48*41fbaed0Stron 
49*41fbaed0Stron /* Utility library. */
50*41fbaed0Stron 
51*41fbaed0Stron #include "msg.h"
52*41fbaed0Stron #include "set_eugid.h"
53*41fbaed0Stron 
54*41fbaed0Stron /* set_eugid - set effective user and group attributes */
55*41fbaed0Stron 
set_eugid(uid_t euid,gid_t egid)56*41fbaed0Stron void    set_eugid(uid_t euid, gid_t egid)
57*41fbaed0Stron {
58*41fbaed0Stron     int     saved_errno = errno;
59*41fbaed0Stron 
60*41fbaed0Stron     if (geteuid() != 0)
61*41fbaed0Stron 	if (seteuid(0))
62*41fbaed0Stron 	    msg_fatal("set_eugid: seteuid(0): %m");
63*41fbaed0Stron     if (setegid(egid) < 0)
64*41fbaed0Stron 	msg_fatal("set_eugid: setegid(%ld): %m", (long) egid);
65*41fbaed0Stron     if (setgroups(1, &egid) < 0)
66*41fbaed0Stron 	msg_fatal("set_eugid: setgroups(%ld): %m", (long) egid);
67*41fbaed0Stron     if (euid != 0 && seteuid(euid) < 0)
68*41fbaed0Stron 	msg_fatal("set_eugid: seteuid(%ld): %m", (long) euid);
69*41fbaed0Stron     if (msg_verbose)
70*41fbaed0Stron 	msg_info("set_eugid: euid %ld egid %ld", (long) euid, (long) egid);
71*41fbaed0Stron     errno = saved_errno;
72*41fbaed0Stron }
73