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
47 init_process_policies(void)
49 typedef BOOL (*SetProcessDEPPolicyHandler)(DWORD);
50 SetProcessDEPPolicyHandler PSetProcessDEPPolicy;
52 #ifndef PROCESS_DEP_ENABLE
53 #define PROCESS_DEP_ENABLE 1
56 if (PSetProcessDEPPolicy = (SetProcessDEPPolicyHandler) GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "SetProcessDEPPolicy")) {
57 PSetProcessDEPPolicy(PROCESS_DEP_ENABLE);
64 * For now, we say the program wasn't started with special privileges.
65 * There are ways of running programs with credentials other than those
66 * for the session in which it's run, but I don't know whether that'd be
67 * done with Wireshark/TShark or not.
70 started_with_special_privs(void)
76 * For now, we say the program isn't running with special privileges.
77 * There are ways of running programs with credentials other than those
78 * for the session in which it's run, but I don't know whether that'd be
79 * done with Wireshark/TShark or not.
82 running_with_special_privs(void)
88 * For now, we don't do anything when asked to relinquish special privileges.
91 relinquish_special_privs_perm(void)
96 * Get the current username. String must be g_free()d after use.
99 get_cur_username(void) {
101 username = g_strdup("UNKNOWN");
106 * Get the current group. String must be g_free()d after use.
109 get_cur_groupname(void) {
111 groupname = g_strdup("UNKNOWN");
116 * If npf.sys is running, return TRUE.
119 npf_sys_is_running() {
120 SC_HANDLE h_scm, h_serv;
123 h_scm = OpenSCManager(NULL, NULL, 0);
127 h_serv = OpenService(h_scm, _T("npf"), SC_MANAGER_CONNECT|SERVICE_QUERY_STATUS);
131 if (QueryServiceStatus(h_serv, &ss)) {
132 if (ss.dwCurrentState & SERVICE_RUNNING)
141 #ifdef HAVE_SYS_TYPES_H
142 # include <sys/types.h>
161 static uid_t ruid, euid;
162 static gid_t rgid, egid;
163 static gboolean init_process_polices_called = FALSE;
166 * Called when the program starts, to save whatever credential information
168 * That'd be the real and effective UID and GID on UNIX.
171 init_process_polices(void)
178 init_process_polices_called = TRUE;
182 * "Started with special privileges" means "started out set-UID or set-GID",
183 * or run as the root user or group.
186 started_with_special_privs(void)
188 g_assert(init_process_polices_called);
189 #ifdef HAVE_ISSETUGID
192 return (ruid != euid || rgid != egid || ruid == 0 || rgid == 0);
197 * Return TRUE if the real, effective, or saved (if we can check it) user
201 running_with_special_privs(void)
203 #ifdef HAVE_SETRESUID
206 #ifdef HAVE_SETRESGID
210 #ifdef HAVE_SETRESUID
211 getresuid(&ru, &eu, &su);
212 if (ru == 0 || eu == 0 || su == 0)
215 if (getuid() == 0 || geteuid() == 0)
218 #ifdef HAVE_SETRESGID
219 getresgid(&rg, &eg, &sg);
220 if (rg == 0 || eg == 0 || sg == 0)
223 if (getgid() == 0 || getegid() == 0)
230 * Permanently relinquish set-UID and set-GID privileges.
231 * Ignore errors for now - if we have the privileges, we should
232 * be able to relinquish them.
236 relinquish_special_privs_perm(void)
239 * If we were started with special privileges, set the
240 * real and effective group and user IDs to the original
241 * values of the real and effective group and user IDs.
242 * If we're not, don't bother - doing so seems to mung
243 * our group set, at least in OS X 10.5.
245 * (Set the effective UID last - that takes away our
246 * rights to set anything else.)
248 if (started_with_special_privs()) {
249 #ifdef HAVE_SETRESGID
250 setresgid(rgid, rgid, rgid);
256 #ifdef HAVE_SETRESUID
257 setresuid(ruid, ruid, ruid);
266 * Get the current username. String must be g_free()d after use.
269 get_cur_username(void) {
271 struct passwd *pw = getpwuid(getuid());
274 username = g_strdup(pw->pw_name);
276 username = g_strdup("UNKNOWN");
283 * Get the current group. String must be g_free()d after use.
286 get_cur_groupname(void) {
288 struct group *gr = getgrgid(getgid());
291 groupname = g_strdup(gr->gr_name);
293 groupname = g_strdup("UNKNOWN");
307 * indent-tabs-mode: tabs
310 * ex: set shiftwidth=8 tabstop=8 noexpandtab
311 * :indentSize=8:tabSize=8:noTabs=false: