Change the "--enable-setuid-install" option to install dumpcap and TShark
[obnox/wireshark/wip.git] / epan / privileges.c
1 /* privileges.c
2  * Routines for handling privileges, e.g. set-UID and set-GID on UNIX.
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 2006 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
30
31 #include "privileges.h"
32 #include "emem.h"
33
34 #ifdef _WIN32
35
36 /*
37  * Called when the program starts, to save whatever credential information
38  * we'll need later.
39  */
40 void
41 get_credential_info(void)
42 {
43 }
44
45 /*
46  * For now, we say the program wasn't started with special privileges.
47  * There are ways of running programs with credentials other than those
48  * for the session in which it's run, but I don't know whether that'd be
49  * done with Wireshark/TShark or not.
50  */
51 gboolean
52 started_with_special_privs(void)
53 {
54         return FALSE;
55 }
56
57 /*
58  * For now, we say the program isn't running with special privileges.
59  * There are ways of running programs with credentials other than those
60  * for the session in which it's run, but I don't know whether that'd be
61  * done with Wireshark/TShark or not.
62  */
63 gboolean
64 running_with_special_privs(void)
65 {
66         return FALSE;
67 }
68
69 /*
70  * For now, we don't do anything when asked to relinquish special privileges.
71  */
72 void
73 relinquish_special_privs_perm(void)
74 {
75 }
76
77 /*
78  * Get the current username.  String must be g_free()d after use.
79  */
80 gchar *
81 get_cur_username(void) {
82         gchar *username;
83         username = g_strdup("UNKNOWN");
84         return username;
85 }
86
87 /*
88  * Get the current group.  String must be g_free()d after use.
89  */
90 gchar *
91 get_cur_groupname(void) {
92         gchar *groupname;
93         groupname = g_strdup("UNKNOWN");
94         return groupname;
95 }
96
97 #else /* _WIN32 */
98
99 #ifdef HAVE_SYS_TYPES_H
100 # include <sys/types.h>
101 #endif
102
103 #ifdef HAVE_UNISTD_H
104 #include <unistd.h>
105 #endif
106
107 #ifdef HAVE_PWD_H
108 #include <pwd.h>
109 #endif
110
111 #ifdef HAVE_GRP_H
112 #include <grp.h>
113 #endif
114
115 #include <glib.h>
116 #include <string.h>
117 #include <errno.h>
118
119 static uid_t ruid, euid;
120 static gid_t rgid, egid;
121
122 /*
123  * Called when the program starts, to save whatever credential information
124  * we'll need later.
125  * That'd be the real and effective UID and GID on UNIX.
126  */
127 void
128 get_credential_info(void)
129 {
130         ruid = getuid();
131         euid = geteuid();
132         rgid = getgid();
133         egid = getegid();
134 }
135
136 /*
137  * "Started with special privileges" means "started out set-UID or set-GID",
138  * or run as the root user or group.
139  */
140 gboolean
141 started_with_special_privs(void)
142 {
143 #ifdef HAVE_ISSETUGID
144         return issetugid();
145 #else
146         return (ruid != euid || rgid != egid || ruid == 0 || rgid == 0);
147 #endif
148 }
149
150 /*
151  * Return TRUE if the real, effective, or saved (if we can check it) user
152  * ID or group are 0.
153  */
154 gboolean
155 running_with_special_privs(void)
156 {
157 #ifdef HAVE_SETRESUID
158         uid_t ru, eu, su;
159 #endif
160 #ifdef HAVE_SETRESGID
161         gid_t rg, eg, sg;
162 #endif
163
164 #ifdef HAVE_SETRESUID
165         getresuid(&ru, &eu, &su);
166         if (ru == 0 || eu == 0 || su == 0)
167                 return TRUE;
168 #else
169         if (getuid() == 0 || geteuid() == 0)
170                 return TRUE;
171 #endif
172 #ifdef HAVE_SETRESGID
173         getresgid(&rg, &eg, &sg);
174         if (rg == 0 || eg == 0 || sg == 0)
175                 return TRUE;
176 #else
177         if (getgid() == 0 || getegid() == 0)
178                 return TRUE;
179 #endif
180         return FALSE;
181 }
182
183 /*
184  * Permanently relinquish  set-UID and set-GID privileges.
185  * Ignore errors for now - if we have the privileges, we should
186  * be able to relinquish them.
187  */
188
189 void
190 relinquish_special_privs_perm(void)
191 {
192         /* If we're running setuid, switch to the calling user */
193 #ifdef HAVE_SETRESGID
194         setresgid(rgid, rgid, rgid);
195 #else
196         setgid(rgid);
197         setegid(rgid);
198 #endif
199
200 #ifdef HAVE_SETRESUID
201         setresuid(ruid, ruid, ruid);
202 #else
203         setuid(ruid);
204         seteuid(ruid);
205 #endif
206
207 }
208
209 /*
210  * Get the current username.  String must be g_free()d after use.
211  */
212 gchar *
213 get_cur_username(void) {
214         gchar *username;
215         struct passwd *pw = getpwuid(getuid());
216
217         if (pw) {
218                 username = g_strdup(pw->pw_name);
219         } else {
220                 username = g_strdup("UNKNOWN");
221         }
222         endpwent();
223         return username;
224 }
225
226 /*
227  * Get the current group.  String must be g_free()d after use.
228  */
229 gchar *
230 get_cur_groupname(void) {
231         gchar *groupname;
232         struct group *gr = getgrgid(getgid());
233
234         if (gr) {
235                 groupname = g_strdup(gr->gr_name);
236         } else {
237                 groupname = g_strdup("UNKNOWN");
238         }
239         endgrent();
240         return groupname;
241 }
242
243 #endif /* _WIN32 */