Fix "disable this protocol by default".
[jlayton/wireshark.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
76 /*
77  * For now, we say the program wasn't started with special privileges.
78  * There are ways of running programs with credentials other than those
79  * for the session in which it's run, but I don't know whether that'd be
80  * done with Wireshark/TShark or not.
81  */
82 gboolean
83 started_with_special_privs(void)
84 {
85         return FALSE;
86 }
87
88 /*
89  * For now, we say the program isn't running with special privileges.
90  * There are ways of running programs with credentials other than those
91  * for the session in which it's run, but I don't know whether that'd be
92  * done with Wireshark/TShark or not.
93  */
94 gboolean
95 running_with_special_privs(void)
96 {
97         return FALSE;
98 }
99
100 /*
101  * For now, we don't do anything when asked to relinquish special privileges.
102  */
103 void
104 relinquish_special_privs_perm(void)
105 {
106 }
107
108 /*
109  * Get the current username.  String must be g_free()d after use.
110  */
111 gchar *
112 get_cur_username(void) {
113         gchar *username;
114         username = g_strdup("UNKNOWN");
115         return username;
116 }
117
118 /*
119  * Get the current group.  String must be g_free()d after use.
120  */
121 gchar *
122 get_cur_groupname(void) {
123         gchar *groupname;
124         groupname = g_strdup("UNKNOWN");
125         return groupname;
126 }
127
128 #else /* _WIN32 */
129
130 #ifdef HAVE_SYS_TYPES_H
131 # include <sys/types.h>
132 #endif
133
134 #ifdef HAVE_UNISTD_H
135 #include <unistd.h>
136 #endif
137
138 #ifdef HAVE_PWD_H
139 #include <pwd.h>
140 #endif
141
142 #ifdef HAVE_GRP_H
143 #include <grp.h>
144 #endif
145
146 #include <string.h>
147 #include <errno.h>
148
149 static uid_t ruid, euid;
150 static gid_t rgid, egid;
151 static gboolean init_process_policies_called = FALSE;
152
153 /*
154  * Called when the program starts, to save whatever credential information
155  * we'll need later, and to do whatever other specialized platform-dependent
156  * initialization we want.
157  *
158  * The credential information we'll need later on UNIX is the real and
159  * effective UID and GID.
160  *
161  * XXX - do any UN*Xes have opt-in "no execute on data pages by default"
162  * permission?  This would be the place to request it.
163  */
164 void
165 init_process_policies(void)
166 {
167         ruid = getuid();
168         euid = geteuid();
169         rgid = getgid();
170         egid = getegid();
171
172         init_process_policies_called = TRUE;
173 }
174
175 /*
176  * "Started with special privileges" means "started out set-UID or set-GID",
177  * or run as the root user or group.
178  */
179 gboolean
180 started_with_special_privs(void)
181 {
182         g_assert(init_process_policies_called);
183 #ifdef HAVE_ISSETUGID
184         return issetugid();
185 #else
186         return (ruid != euid || rgid != egid || ruid == 0 || rgid == 0);
187 #endif
188 }
189
190 /*
191  * Return TRUE if the real, effective, or saved (if we can check it) user
192  * ID or group are 0.
193  */
194 gboolean
195 running_with_special_privs(void)
196 {
197 #ifdef HAVE_SETRESUID
198         uid_t ru, eu, su;
199 #endif
200 #ifdef HAVE_SETRESGID
201         gid_t rg, eg, sg;
202 #endif
203
204 #ifdef HAVE_SETRESUID
205         getresuid(&ru, &eu, &su);
206         if (ru == 0 || eu == 0 || su == 0)
207                 return TRUE;
208 #else
209         if (getuid() == 0 || geteuid() == 0)
210                 return TRUE;
211 #endif
212 #ifdef HAVE_SETRESGID
213         getresgid(&rg, &eg, &sg);
214         if (rg == 0 || eg == 0 || sg == 0)
215                 return TRUE;
216 #else
217         if (getgid() == 0 || getegid() == 0)
218                 return TRUE;
219 #endif
220         return FALSE;
221 }
222
223 /*
224  * Permanently relinquish  set-UID and set-GID privileges.
225  * If error, abort since we probably shouldn't continue
226  * with elevated privileges.
227  * Note that if this error occurs when dumpcap is called from
228  * wireshark or tshark, the message seen will be
229  * "Child dumpcap process died:". This is obscure but we'll
230  *   consider it acceptable since it should be highly unlikely
231  *   that this error will occur.
232  */
233
234 static void
235 setxid_fail(const gchar *str)
236 {
237         g_error("Attempt to relinguish privileges failed [%s()] - aborting: %s\n",
238                 str, g_strerror(errno));
239 }
240
241 void
242 relinquish_special_privs_perm(void)
243 {
244         /*
245          * If we were started with special privileges, set the
246          * real and effective group and user IDs to the original
247          * values of the real and effective group and user IDs.
248          * If we're not, don't bother - doing so seems to mung
249          * our group set, at least in OS X 10.5.
250          *
251          * (Set the effective UID last - that takes away our
252          * rights to set anything else.)
253          */
254         if (started_with_special_privs()) {
255 #ifdef HAVE_SETRESGID
256                 if (setresgid(rgid, rgid, rgid) == -1) {setxid_fail("setresgid");}
257 #else
258                 if (setgid(rgid)                == -1) {setxid_fail("setgid"); }
259                 if (setegid(rgid)               == -1) {setxid_fail("setegid");}
260 #endif
261
262 #ifdef HAVE_SETRESUID
263                 if (setresuid(ruid, ruid, ruid) == -1) {setxid_fail("setresuid");}
264 #else
265                 if (setuid(ruid)                == -1) {setxid_fail("setuid"); }
266                 if (seteuid(ruid)               == -1) {setxid_fail("seteuid");}
267 #endif
268         }
269 }
270
271 /*
272  * Get the current username.  String must be g_free()d after use.
273  */
274 gchar *
275 get_cur_username(void) {
276         gchar *username;
277         struct passwd *pw = getpwuid(getuid());
278
279         if (pw) {
280                 username = g_strdup(pw->pw_name);
281         } else {
282                 username = g_strdup("UNKNOWN");
283         }
284         endpwent();
285         return username;
286 }
287
288 /*
289  * Get the current group.  String must be g_free()d after use.
290  */
291 gchar *
292 get_cur_groupname(void) {
293         gchar *groupname;
294         struct group *gr = getgrgid(getgid());
295
296         if (gr) {
297                 groupname = g_strdup(gr->gr_name);
298         } else {
299                 groupname = g_strdup("UNKNOWN");
300         }
301         endgrent();
302         return groupname;
303 }
304
305 #endif /* _WIN32 */
306
307 /*
308  * Editor modelines
309  *
310  * Local Variables:
311  * c-basic-offset: 8
312  * tab-width: 8
313  * indent-tabs-mode: t
314  * End:
315  *
316  * ex: set shiftwidth=8 tabstop=8 noexpandtab:
317  * :indentSize=8:tabSize=8:noTabs=false:
318  */