1*41fbaed0Stron /* $NetBSD: set_ugid.c,v 1.1.1.1 2009/06/23 10:09:00 tron Exp $ */
2*41fbaed0Stron
3*41fbaed0Stron /*++
4*41fbaed0Stron /* NAME
5*41fbaed0Stron /* set_ugid 3
6*41fbaed0Stron /* SUMMARY
7*41fbaed0Stron /* set real, effective and saved user and group attributes
8*41fbaed0Stron /* SYNOPSIS
9*41fbaed0Stron /* #include <set_ugid.h>
10*41fbaed0Stron /*
11*41fbaed0Stron /* void set_ugid(uid, gid)
12*41fbaed0Stron /* uid_t uid;
13*41fbaed0Stron /* gid_t gid;
14*41fbaed0Stron /* DESCRIPTION
15*41fbaed0Stron /* set_ugid() sets the real, effective and saved user and group process
16*41fbaed0Stron /* attributes and updates the process group access list to be just the
17*41fbaed0Stron /* user's primary group. This operation is irreversible.
18*41fbaed0Stron /* DIAGNOSTICS
19*41fbaed0Stron /* All system call errors are fatal.
20*41fbaed0Stron /* SEE ALSO
21*41fbaed0Stron /* setuid(2), setgid(2), setgroups(2)
22*41fbaed0Stron /* LICENSE
23*41fbaed0Stron /* .ad
24*41fbaed0Stron /* .fi
25*41fbaed0Stron /* The Secure Mailer license must be distributed with this software.
26*41fbaed0Stron /* AUTHOR(S)
27*41fbaed0Stron /* Wietse Venema
28*41fbaed0Stron /* IBM T.J. Watson Research
29*41fbaed0Stron /* P.O. Box 704
30*41fbaed0Stron /* Yorktown Heights, NY 10598, USA
31*41fbaed0Stron /*--*/
32*41fbaed0Stron
33*41fbaed0Stron /* System library. */
34*41fbaed0Stron
35*41fbaed0Stron #include <sys_defs.h>
36*41fbaed0Stron #include <unistd.h>
37*41fbaed0Stron #include <grp.h>
38*41fbaed0Stron #include <errno.h>
39*41fbaed0Stron
40*41fbaed0Stron /* Utility library. */
41*41fbaed0Stron
42*41fbaed0Stron #include "msg.h"
43*41fbaed0Stron #include "set_ugid.h"
44*41fbaed0Stron
45*41fbaed0Stron /* set_ugid - set real, effective and saved user and group attributes */
46*41fbaed0Stron
set_ugid(uid_t uid,gid_t gid)47*41fbaed0Stron void set_ugid(uid_t uid, gid_t gid)
48*41fbaed0Stron {
49*41fbaed0Stron int saved_errno = errno;
50*41fbaed0Stron
51*41fbaed0Stron if (geteuid() != 0)
52*41fbaed0Stron if (seteuid(0) < 0)
53*41fbaed0Stron msg_fatal("seteuid(0): %m");
54*41fbaed0Stron if (setgid(gid) < 0)
55*41fbaed0Stron msg_fatal("setgid(%ld): %m", (long) gid);
56*41fbaed0Stron if (setgroups(1, &gid) < 0)
57*41fbaed0Stron msg_fatal("setgroups(1, &%ld): %m", (long) gid);
58*41fbaed0Stron if (setuid(uid) < 0)
59*41fbaed0Stron msg_fatal("setuid(%ld): %m", (long) uid);
60*41fbaed0Stron if (msg_verbose > 1)
61*41fbaed0Stron msg_info("setugid: uid %ld gid %ld", (long) uid, (long) gid);
62*41fbaed0Stron errno = saved_errno;
63*41fbaed0Stron }
64