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