blob: 18d503b0466421219bbaa151e9a6b7c0100179e3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
--- ngrep.c.orig 2004-03-28 21:15:59.000000000 -0500
+++ ngrep.c 2004-07-04 19:21:18.000000000 -0400
@@ -999,40 +999,29 @@
{
struct passwd *pw = getpwnam(DROPPRIVS_USER);
- gid_t newgid = pw->pw_uid, oldgid = getegid();
- uid_t newuid = pw->pw_gid, olduid = geteuid();
-
- if (!olduid)
- setgroups(1, &newgid);
-
- if (newgid != oldgid) {
-#if !defined(LINUX)
- setegid(newgid);
- if (setgid(newgid) == -1)
-#else
- if (setregid(newgid, newgid) == -1)
-#endif
- {
- perror("attempt to drop privileges failed");
- clean_exit(-1);
- }
+ gid_t newgid = pw->pw_gid, oldgid = getegid();
+ uid_t newuid = pw->pw_uid, olduid = geteuid();
+ gid_t groups[2];
+
+ groups[0] = groups[1] = newgid;
+ if (setgroups(1, groups) == -1)
+ {
+ perror("attempt to set groups failed");
+ clean_exit(-1);
}
- if (newuid != olduid) {
-#if !defined(LINUX)
-#if !defined(MACOSX)
- seteuid(newuid);
-#endif
- if (setuid(newuid) == -1)
-#else
- if (setreuid(newuid, newuid) == -1)
-#endif
- {
- perror("attempt to drop privileges failed");
- clean_exit(-1);
- }
+ if (setgid(newgid) == -1)
+ {
+ perror("attempt to drop group privileges failed");
+ clean_exit(-1);
}
-
+
+ if (setuid(newuid) == -1)
+ {
+ perror("attempt to drop user privileges failed");
+ clean_exit(-1);
+ }
+
if ((newgid != oldgid && (setegid(oldgid) != -1 || getegid() != newgid)) ||
(newuid != olduid && (seteuid(olduid) != -1 || geteuid() != newuid))) {
perror("attempt to drop privileges failed");
|