2 * Routines for handling privileges, e.g. set-UID and set-GID on UNIX.
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 2006 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #if defined(HAVE_SETRESUID) || defined(HAVE_SETREGUID)
30 #define _GNU_SOURCE /* Otherwise [sg]etres[gu]id won't be defined on Linux */
35 #include "privileges.h"
43 * Called when the program starts, to save whatever credential information
44 * we'll need later, and to do whatever other specialized platform-dependent
45 * initialization we want.
48 init_process_policies(void)
50 HMODULE kernel32Handle;
51 typedef BOOL (WINAPI *SetProcessDEPPolicyHandler)(DWORD);
52 SetProcessDEPPolicyHandler PSetProcessDEPPolicy;
54 #ifndef PROCESS_DEP_ENABLE
55 #define PROCESS_DEP_ENABLE 1
59 * If we have SetProcessDEPPolicy(), turn "data execution
60 * prevention" on - i.e., if the MMU lets you set execute
61 * permission on a per-page basis, turn execute permission
62 * off on most data pages. PSetProcessDEPPolicy() fails on
63 * 64-bit Windows (it's *always* on there), but if it fails,
64 * we don't care (we did our best), so we don't check for
67 * XXX - if the GetModuleHandle() call fails, should we report
68 * an error? That "shouldn't happen" - it's the equivalent
69 * of libc.{so,sl,a} or libSystem.dylib being missing on UN*X.
71 kernel32Handle = GetModuleHandle(_T("kernel32.dll"));
72 if (kernel32Handle != NULL) {
73 PSetProcessDEPPolicy = (SetProcessDEPPolicyHandler) GetProcAddress(kernel32Handle, "SetProcessDEPPolicy");
74 if (PSetProcessDEPPolicy) {
75 PSetProcessDEPPolicy(PROCESS_DEP_ENABLE);
83 * For now, we say the program wasn't started with special privileges.
84 * There are ways of running programs with credentials other than those
85 * for the session in which it's run, but I don't know whether that'd be
86 * done with Wireshark/TShark or not.
89 started_with_special_privs(void)
95 * For now, we say the program isn't running with special privileges.
96 * There are ways of running programs with credentials other than those
97 * for the session in which it's run, but I don't know whether that'd be
98 * done with Wireshark/TShark or not.
101 running_with_special_privs(void)
107 * For now, we don't do anything when asked to relinquish special privileges.
110 relinquish_special_privs_perm(void)
115 * Get the current username. String must be g_free()d after use.
118 get_cur_username(void) {
120 username = g_strdup("UNKNOWN");
125 * Get the current group. String must be g_free()d after use.
128 get_cur_groupname(void) {
130 groupname = g_strdup("UNKNOWN");
135 * If npf.sys is running, return TRUE.
138 npf_sys_is_running() {
139 SC_HANDLE h_scm, h_serv;
142 h_scm = OpenSCManager(NULL, NULL, 0);
146 h_serv = OpenService(h_scm, _T("npf"), SC_MANAGER_CONNECT|SERVICE_QUERY_STATUS);
150 if (QueryServiceStatus(h_serv, &ss)) {
151 if (ss.dwCurrentState & SERVICE_RUNNING)
160 #ifdef HAVE_SYS_TYPES_H
161 # include <sys/types.h>
180 static uid_t ruid, euid;
181 static gid_t rgid, egid;
182 static gboolean init_process_policies_called = FALSE;
185 * Called when the program starts, to save whatever credential information
186 * we'll need later, and to do whatever other specialized platform-dependent
187 * initialization we want.
189 * The credential information we'll need later on UNIX is the real and
190 * effective UID and GID.
192 * XXX - do any UN*Xes have opt-in "no execute on data pages by default"
193 * permission? This would be the place to request it.
196 init_process_policies(void)
203 init_process_policies_called = TRUE;
207 * "Started with special privileges" means "started out set-UID or set-GID",
208 * or run as the root user or group.
211 started_with_special_privs(void)
213 g_assert(init_process_policies_called);
214 #ifdef HAVE_ISSETUGID
217 return (ruid != euid || rgid != egid || ruid == 0 || rgid == 0);
222 * Return TRUE if the real, effective, or saved (if we can check it) user
226 running_with_special_privs(void)
228 #ifdef HAVE_SETRESUID
231 #ifdef HAVE_SETRESGID
235 #ifdef HAVE_SETRESUID
236 getresuid(&ru, &eu, &su);
237 if (ru == 0 || eu == 0 || su == 0)
240 if (getuid() == 0 || geteuid() == 0)
243 #ifdef HAVE_SETRESGID
244 getresgid(&rg, &eg, &sg);
245 if (rg == 0 || eg == 0 || sg == 0)
248 if (getgid() == 0 || getegid() == 0)
255 * Permanently relinquish set-UID and set-GID privileges.
256 * Ignore errors for now - if we have the privileges, we should
257 * be able to relinquish them.
261 relinquish_special_privs_perm(void)
264 * If we were started with special privileges, set the
265 * real and effective group and user IDs to the original
266 * values of the real and effective group and user IDs.
267 * If we're not, don't bother - doing so seems to mung
268 * our group set, at least in OS X 10.5.
270 * (Set the effective UID last - that takes away our
271 * rights to set anything else.)
273 if (started_with_special_privs()) {
274 #ifdef HAVE_SETRESGID
275 setresgid(rgid, rgid, rgid);
281 #ifdef HAVE_SETRESUID
282 setresuid(ruid, ruid, ruid);
291 * Get the current username. String must be g_free()d after use.
294 get_cur_username(void) {
296 struct passwd *pw = getpwuid(getuid());
299 username = g_strdup(pw->pw_name);
301 username = g_strdup("UNKNOWN");
308 * Get the current group. String must be g_free()d after use.
311 get_cur_groupname(void) {
313 struct group *gr = getgrgid(getgid());
316 groupname = g_strdup(gr->gr_name);
318 groupname = g_strdup("UNKNOWN");
332 * indent-tabs-mode: t
335 * ex: set shiftwidth=8 tabstop=8 noexpandtab:
336 * :indentSize=8:tabSize=8:noTabs=false: