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 (*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 if (PSetProcessDEPPolicy = (SetProcessDEPPolicyHandler) GetProcAddress(kernel32Handle, "SetProcessDEPPolicy")) {
74 PSetProcessDEPPolicy(PROCESS_DEP_ENABLE);
82 * For now, we say the program wasn't started with special privileges.
83 * There are ways of running programs with credentials other than those
84 * for the session in which it's run, but I don't know whether that'd be
85 * done with Wireshark/TShark or not.
88 started_with_special_privs(void)
94 * For now, we say the program isn't running with special privileges.
95 * There are ways of running programs with credentials other than those
96 * for the session in which it's run, but I don't know whether that'd be
97 * done with Wireshark/TShark or not.
100 running_with_special_privs(void)
106 * For now, we don't do anything when asked to relinquish special privileges.
109 relinquish_special_privs_perm(void)
114 * Get the current username. String must be g_free()d after use.
117 get_cur_username(void) {
119 username = g_strdup("UNKNOWN");
124 * Get the current group. String must be g_free()d after use.
127 get_cur_groupname(void) {
129 groupname = g_strdup("UNKNOWN");
134 * If npf.sys is running, return TRUE.
137 npf_sys_is_running() {
138 SC_HANDLE h_scm, h_serv;
141 h_scm = OpenSCManager(NULL, NULL, 0);
145 h_serv = OpenService(h_scm, _T("npf"), SC_MANAGER_CONNECT|SERVICE_QUERY_STATUS);
149 if (QueryServiceStatus(h_serv, &ss)) {
150 if (ss.dwCurrentState & SERVICE_RUNNING)
159 #ifdef HAVE_SYS_TYPES_H
160 # include <sys/types.h>
179 static uid_t ruid, euid;
180 static gid_t rgid, egid;
181 static gboolean init_process_policies_called = FALSE;
184 * Called when the program starts, to save whatever credential information
185 * we'll need later, and to do whatever other specialized platform-dependent
186 * initialization we want.
188 * The credential information we'll need later on UNIX is the real and
189 * effective UID and GID.
191 * XXX - do any UN*Xes have opt-in "no execute on data pages by default"
192 * permission? This would be the place to request it.
195 init_process_policies(void)
202 init_process_policies_called = TRUE;
206 * "Started with special privileges" means "started out set-UID or set-GID",
207 * or run as the root user or group.
210 started_with_special_privs(void)
212 g_assert(init_process_policies_called);
213 #ifdef HAVE_ISSETUGID
216 return (ruid != euid || rgid != egid || ruid == 0 || rgid == 0);
221 * Return TRUE if the real, effective, or saved (if we can check it) user
225 running_with_special_privs(void)
227 #ifdef HAVE_SETRESUID
230 #ifdef HAVE_SETRESGID
234 #ifdef HAVE_SETRESUID
235 getresuid(&ru, &eu, &su);
236 if (ru == 0 || eu == 0 || su == 0)
239 if (getuid() == 0 || geteuid() == 0)
242 #ifdef HAVE_SETRESGID
243 getresgid(&rg, &eg, &sg);
244 if (rg == 0 || eg == 0 || sg == 0)
247 if (getgid() == 0 || getegid() == 0)
254 * Permanently relinquish set-UID and set-GID privileges.
255 * Ignore errors for now - if we have the privileges, we should
256 * be able to relinquish them.
260 relinquish_special_privs_perm(void)
263 * If we were started with special privileges, set the
264 * real and effective group and user IDs to the original
265 * values of the real and effective group and user IDs.
266 * If we're not, don't bother - doing so seems to mung
267 * our group set, at least in OS X 10.5.
269 * (Set the effective UID last - that takes away our
270 * rights to set anything else.)
272 if (started_with_special_privs()) {
273 #ifdef HAVE_SETRESGID
274 setresgid(rgid, rgid, rgid);
280 #ifdef HAVE_SETRESUID
281 setresuid(ruid, ruid, ruid);
290 * Get the current username. String must be g_free()d after use.
293 get_cur_username(void) {
295 struct passwd *pw = getpwuid(getuid());
298 username = g_strdup(pw->pw_name);
300 username = g_strdup("UNKNOWN");
307 * Get the current group. String must be g_free()d after use.
310 get_cur_groupname(void) {
312 struct group *gr = getgrgid(getgid());
315 groupname = g_strdup(gr->gr_name);
317 groupname = g_strdup("UNKNOWN");
331 * indent-tabs-mode: tabs
334 * ex: set shiftwidth=8 tabstop=8 noexpandtab
335 * :indentSize=8:tabSize=8:noTabs=false: