Clean up a comment.
[metze/wireshark/wip.git] / wsutil / privileges.c
1 /* privileges.c
2  * Routines for handling privileges, e.g. set-UID and set-GID on UNIX.
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 2006 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "config.h"
24
25 #if defined(HAVE_SETRESUID) || defined(HAVE_SETREGUID)
26 #define _GNU_SOURCE /* Otherwise [sg]etres[gu]id won't be defined on Linux */
27 #endif
28
29 #include <glib.h>
30
31 #include "privileges.h"
32
33 #ifdef _WIN32
34 #include <windows.h>
35 #include <wchar.h>
36 #include <tchar.h>
37
38 /*
39  * Called when the program starts, to save whatever credential information
40  * we'll need later, and to do whatever other specialized platform-dependent
41  * initialization we want.
42  */
43 void
44 init_process_policies(void)
45 {
46         HMODULE kernel32Handle;
47         typedef BOOL (WINAPI *SetProcessDEPPolicyHandler)(DWORD);
48         SetProcessDEPPolicyHandler PSetProcessDEPPolicy;
49
50 #ifndef PROCESS_DEP_ENABLE
51 #define PROCESS_DEP_ENABLE 1
52 #endif
53
54         /*
55          * If we have SetProcessDEPPolicy(), turn "data execution
56          * prevention" on - i.e., if the MMU lets you set execute
57          * permission on a per-page basis, turn execute permission
58          * off on most data pages.  SetProcessDEPPolicy() fails on
59          * 64-bit Windows (it's *always* on there), but if it fails,
60          * we don't care (we did our best), so we don't check for
61          * errors.
62          *
63          * XXX - if the GetModuleHandle() call fails, should we report
64          * an error?  That "shouldn't happen" - it's the equivalent
65          * of libc.{so,sl,a} or libSystem.dylib being missing on UN*X.
66          */
67         kernel32Handle = GetModuleHandle(_T("kernel32.dll"));
68         if (kernel32Handle != NULL) {
69                 PSetProcessDEPPolicy = (SetProcessDEPPolicyHandler) GetProcAddress(kernel32Handle, "SetProcessDEPPolicy");
70                 if (PSetProcessDEPPolicy) {
71                         PSetProcessDEPPolicy(PROCESS_DEP_ENABLE);
72                 }
73         }
74
75         npf_sys_is_running();
76 }
77
78 /*
79  * For now, we say the program wasn't started with special privileges.
80  * There are ways of running programs with credentials other than those
81  * for the session in which it's run, but I don't know whether that'd be
82  * done with Wireshark/TShark or not.
83  */
84 gboolean
85 started_with_special_privs(void)
86 {
87         return FALSE;
88 }
89
90 /*
91  * For now, we say the program isn't running with special privileges.
92  * There are ways of running programs with credentials other than those
93  * for the session in which it's run, but I don't know whether that'd be
94  * done with Wireshark/TShark or not.
95  */
96 gboolean
97 running_with_special_privs(void)
98 {
99         return FALSE;
100 }
101
102 /*
103  * For now, we don't do anything when asked to relinquish special privileges.
104  */
105 void
106 relinquish_special_privs_perm(void)
107 {
108 }
109
110 /*
111  * Get the current username.  String must be g_free()d after use.
112  */
113 gchar *
114 get_cur_username(void) {
115         gchar *username;
116         username = g_strdup("UNKNOWN");
117         return username;
118 }
119
120 /*
121  * Get the current group.  String must be g_free()d after use.
122  */
123 gchar *
124 get_cur_groupname(void) {
125         gchar *groupname;
126         groupname = g_strdup("UNKNOWN");
127         return groupname;
128 }
129
130 /*
131  * If npf.sys is running, return TRUE.
132  */
133 gboolean
134 npf_sys_is_running() {
135         SC_HANDLE h_scm, h_serv;
136         SERVICE_STATUS ss;
137
138         h_scm = OpenSCManager(NULL, NULL, 0);
139         if (!h_scm)
140                 return FALSE;
141
142         h_serv = OpenService(h_scm, _T("npf"), SC_MANAGER_CONNECT|SERVICE_QUERY_STATUS);
143         if (!h_serv)
144                 return FALSE;
145
146         if (QueryServiceStatus(h_serv, &ss)) {
147                 if (ss.dwCurrentState & SERVICE_RUNNING)
148                         return TRUE;
149         }
150         return FALSE;
151 }
152
153
154 #else /* _WIN32 */
155
156 #ifdef HAVE_SYS_TYPES_H
157 # include <sys/types.h>
158 #endif
159
160 #ifdef HAVE_UNISTD_H
161 #include <unistd.h>
162 #endif
163
164 #ifdef HAVE_PWD_H
165 #include <pwd.h>
166 #endif
167
168 #ifdef HAVE_GRP_H
169 #include <grp.h>
170 #endif
171
172 #include <glib.h>
173 #include <string.h>
174 #include <errno.h>
175
176 static uid_t ruid, euid;
177 static gid_t rgid, egid;
178 static gboolean init_process_policies_called = FALSE;
179
180 /*
181  * Called when the program starts, to save whatever credential information
182  * we'll need later, and to do whatever other specialized platform-dependent
183  * initialization we want.
184  *
185  * The credential information we'll need later on UNIX is the real and
186  * effective UID and GID.
187  *
188  * XXX - do any UN*Xes have opt-in "no execute on data pages by default"
189  * permission?  This would be the place to request it.
190  */
191 void
192 init_process_policies(void)
193 {
194         ruid = getuid();
195         euid = geteuid();
196         rgid = getgid();
197         egid = getegid();
198
199         init_process_policies_called = TRUE;
200 }
201
202 /*
203  * "Started with special privileges" means "started out set-UID or set-GID",
204  * or run as the root user or group.
205  */
206 gboolean
207 started_with_special_privs(void)
208 {
209         g_assert(init_process_policies_called);
210 #ifdef HAVE_ISSETUGID
211         return issetugid();
212 #else
213         return (ruid != euid || rgid != egid || ruid == 0 || rgid == 0);
214 #endif
215 }
216
217 /*
218  * Return TRUE if the real, effective, or saved (if we can check it) user
219  * ID or group are 0.
220  */
221 gboolean
222 running_with_special_privs(void)
223 {
224 #ifdef HAVE_SETRESUID
225         uid_t ru, eu, su;
226 #endif
227 #ifdef HAVE_SETRESGID
228         gid_t rg, eg, sg;
229 #endif
230
231 #ifdef HAVE_SETRESUID
232         getresuid(&ru, &eu, &su);
233         if (ru == 0 || eu == 0 || su == 0)
234                 return TRUE;
235 #else
236         if (getuid() == 0 || geteuid() == 0)
237                 return TRUE;
238 #endif
239 #ifdef HAVE_SETRESGID
240         getresgid(&rg, &eg, &sg);
241         if (rg == 0 || eg == 0 || sg == 0)
242                 return TRUE;
243 #else
244         if (getgid() == 0 || getegid() == 0)
245                 return TRUE;
246 #endif
247         return FALSE;
248 }
249
250 /*
251  * Permanently relinquish  set-UID and set-GID privileges.
252  * If error, abort since we probably shouldn't continue
253  * with elevated privileges.
254  * Note that if this error occurs when dumpcap is called from
255  * wireshark or tshark, the message seen will be
256  * "Child dumpcap process died:". This is obscure but we'll
257  *   consider it acceptable since it should be highly unlikely
258  *   that this error will occur.
259  */
260
261 static void
262 setxid_fail(const gchar *str)
263 {
264         g_error("Attempt to relinguish privileges failed [%s()] - aborting: %s\n",
265                 str, g_strerror(errno));
266 }
267
268 void
269 relinquish_special_privs_perm(void)
270 {
271         /*
272          * If we were started with special privileges, set the
273          * real and effective group and user IDs to the original
274          * values of the real and effective group and user IDs.
275          * If we're not, don't bother - doing so seems to mung
276          * our group set, at least in OS X 10.5.
277          *
278          * (Set the effective UID last - that takes away our
279          * rights to set anything else.)
280          */
281         if (started_with_special_privs()) {
282 #ifdef HAVE_SETRESGID
283                 if (setresgid(rgid, rgid, rgid) == -1) {setxid_fail("setresgid");}
284 #else
285                 if (setgid(rgid)                == -1) {setxid_fail("setgid"); }
286                 if (setegid(rgid)               == -1) {setxid_fail("setegid");}
287 #endif
288
289 #ifdef HAVE_SETRESUID
290                 if (setresuid(ruid, ruid, ruid) == -1) {setxid_fail("setresuid");}
291 #else
292                 if (setuid(ruid)                == -1) {setxid_fail("setuid"); }
293                 if (seteuid(ruid)               == -1) {setxid_fail("seteuid");}
294 #endif
295         }
296 }
297
298 /*
299  * Get the current username.  String must be g_free()d after use.
300  */
301 gchar *
302 get_cur_username(void) {
303         gchar *username;
304         struct passwd *pw = getpwuid(getuid());
305
306         if (pw) {
307                 username = g_strdup(pw->pw_name);
308         } else {
309                 username = g_strdup("UNKNOWN");
310         }
311         endpwent();
312         return username;
313 }
314
315 /*
316  * Get the current group.  String must be g_free()d after use.
317  */
318 gchar *
319 get_cur_groupname(void) {
320         gchar *groupname;
321         struct group *gr = getgrgid(getgid());
322
323         if (gr) {
324                 groupname = g_strdup(gr->gr_name);
325         } else {
326                 groupname = g_strdup("UNKNOWN");
327         }
328         endgrent();
329         return groupname;
330 }
331
332 #endif /* _WIN32 */
333
334 /*
335  * Editor modelines
336  *
337  * Local Variables:
338  * c-basic-offset: 8
339  * tab-width: 8
340  * indent-tabs-mode: t
341  * End:
342  *
343  * ex: set shiftwidth=8 tabstop=8 noexpandtab:
344  * :indentSize=8:tabSize=8:noTabs=false:
345  */