Debian Codegraph @

src/common/give_up_root_privileges.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2001 Hakan Tandogan ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/
20 #include "common/give_up_root_privileges.h"cxx . . $ `<file>/src/common/give_up_root_privileges.h`/
21 #include <cerrno>
22 #include <cstring>
23 #include <cstdlib>
24
25 #ifdef HAVE_SYS_TYPES_Hcxx . . $ `build/config.h:16:9`!
26 # include <sys/types.h>
27 #endif
28 #ifdef HAVE_UNISTD_Hcxx . . $ `build/config.h:7:9`!
29 # include <unistd.h>
30 #endif
31 #ifdef HAVE_PWD_Hcxx . . $ `build/config.h:22:9`!
32 # include <pwd.h>
33 #endif
34 #ifdef HAVE_GRP_Hcxx . . $ `build/config.h:23:9`!
35 # include <grp.h>
36 #endif
37
38 #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/
39 #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/
40
41
42 #define ILLEGAL_IDcxx . . $ `src/common/give_up_root_privileges.cpp:42:9`! -1
43
44
45 namespace pvpgncxx . . $ pvpgn/
46 {
47
48 static int gurp_gname2idcxx . . $ pvpgn/gurp_gname2id(5c4306d8553b3696).(const char *name);
49 static int gurp_uname2idcxx . . $ pvpgn/gurp_uname2id(5c4306d8553b3696).(const char *name);
50
51
52 extern int give_up_root_privilegescxx . . $ pvpgn/give_up_root_privileges(5ae29d726ae57f88).(char const * user_name, char const * group_name)
53 {
54 int user_id = 0;
55 int group_id = 0;
56
57 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_debugcxx . . $ pvpgn/eventlog_level_debug., __FUNCTION__, "about to give up root privileges");
58
59 if (user_name)
60 {
61 if ((user_id = gurp_uname2idcxx . . $ pvpgn/gurp_uname2id(5c4306d8553b3696).(user_name)) == ILLEGAL_IDcxx . . $ `src/common/give_up_root_privileges.cpp:42:9`!)
62 {
63 return -1;
64 }
65 else
66 {
67 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_debugcxx . . $ pvpgn/eventlog_level_debug., __FUNCTION__, "should change to user = '{}' ({})", user_name, user_id);
68 }
69 }
70 if (group_name)
71 {
72 if ((group_id = gurp_gname2idcxx . . $ pvpgn/gurp_gname2id(5c4306d8553b3696).(group_name)) == ILLEGAL_IDcxx . . $ `src/common/give_up_root_privileges.cpp:42:9`!)
73 {
74 return -1;
75 }
76 else
77 {
78 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_debugcxx . . $ pvpgn/eventlog_level_debug., __FUNCTION__, "should change to group = '{}' ({})", group_name, group_id);
79 }
80 }
81
82 /* Change first the group ID, later we might not be able to anymore
83 * We can use setgid safely because we don't want to return to root
84 * privileges anymore
85 */
86
87 #ifdef HAVE_SETGID
88 if (group_name)
89 {
90 if (-1 == setgid(group_id))
91 {
92 eventlog(eventlog_level_fatal, __FUNCTION__, "could not set gid to {} (setgid: {})", group_id, std::strerror(errno));
93 return -1;
94 }
95 # ifdef HAVE_GETUID
96 eventlog(eventlog_level_info, __FUNCTION__, "Changed privileges to gid = {}", getgid());
97 # endif
98 }
99 #endif
100
101 #ifdef HAVE_SETUIDcxx . . $ `build/config.h:72:9`!
102 if (user_name)
103 {
104 if (-1 == setuid(user_id))
105 {
106 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_fatalcxx . . $ pvpgn/eventlog_level_fatal., __FUNCTION__, "could not set uid to {} (setuid: {})", user_id, stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
107 return -1;
108 }
109 # ifdef HAVE_GETGIDcxx . . $ `build/config.h:43:9`!
110 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_infocxx . . $ pvpgn/eventlog_level_info., __FUNCTION__, "Changed privileges to uid = {}", getuid());
111 # endif
112 }
113 #endif
114
115 return 0;
116 }
117
118
119 static int gurp_uname2idcxx . . $ pvpgn/gurp_uname2id(5c4306d8553b3696).(const char *name)
120 {
121 int id = ILLEGAL_IDcxx . . $ `src/common/give_up_root_privileges.cpp:42:9`!;
122
123 if (name != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)
124 {
125 if (name[0] == '#')
126 {
127 id = stdcxx . . $ std/::atoi(&name[1]);
128 }
129 else
130 {
131 #ifdef HAVE_GETPWNAMEcxx . . $ `build/config.h:50:9`!
132 struct passwd * ent;
133
134 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_debugcxx . . $ pvpgn/eventlog_level_debug., __FUNCTION__, "about to getpwnam({})", name);
135
136 if (!(ent = getpwnam(name)))
137 {
138 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_fatalcxx . . $ pvpgn/eventlog_level_fatal., __FUNCTION__, "cannot get password file entry for '{}' (getpwnam: {})", name, stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
139 return id;
140 }
141 id = ent->pw_uid;
142 #else
143 return id;
144 #endif
145 }
146 }
147
148 return id;
149 }
150
151
152 static int gurp_gname2idcxx . . $ pvpgn/gurp_gname2id(5c4306d8553b3696).(const char *name)
153 {
154 int id = ILLEGAL_IDcxx . . $ `src/common/give_up_root_privileges.cpp:42:9`!;
155
156 if (name != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)
157 {
158 if (name[0] == '#')
159 {
160 id = stdcxx . . $ std/::atoi(&name[1]);
161 }
162 else
163 {
164 #ifdef HAVE_GETGRNAMcxx . . $ `build/config.h:44:9`!
165 struct group * ent;
166
167 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_debugcxx . . $ pvpgn/eventlog_level_debug., __FUNCTION__, "about to getgrnam({})", name);
168
169 if (!(ent = getgrnam(name)))
170 {
171 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_fatalcxx . . $ pvpgn/eventlog_level_fatal., __FUNCTION__, "cannot get group file entry for '{}' (getgrnam: {})", name, stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
172 return id;
173 }
174 id = ent->gr_gid;
175 #else
176 return id;
177 #endif
178 }
179 }
180
181 return id;
182 }
183
184 }