Add HP Switch Protocol SAP value
[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
33 #ifdef _WIN32
34
35 /*
36  * Called when the program starts, to save whatever credential information
37  * we'll need later.
38  */
39 void
40 get_credential_info(void)
41 {
42 }
43
44 /*
45  * For now, we say the program wasn't started with special privileges.
46  * There are ways of running programs with credentials other than those
47  * for the session in which it's run, but I don't know whether that'd be
48  * done with Wireshark/TShark or not.
49  */
50 gboolean
51 started_with_special_privs(void)
52 {
53         return FALSE;
54 }
55
56 /*
57  * For now, we don't do anything when asked to relinquish special privileges.
58  */
59 void
60 relinquish_special_privs_perm(void)
61 {
62 }
63
64 #else /* _WIN32 */
65
66 #ifdef HAVE_SYS_TYPES_H
67 # include <sys/types.h>
68 #endif
69
70 #ifdef HAVE_UNISTD_H
71 #include <unistd.h>
72 #endif
73
74 static uid_t ruid, euid;
75 static gid_t rgid, egid;
76
77 /*
78  * Called when the program starts, to save whatever credential information
79  * we'll need later.
80  * That'd be the real and effective UID and GID on UNIX.
81  */
82 void
83 get_credential_info(void)
84 {
85         ruid = getuid();
86         euid = geteuid();
87         rgid = getgid();
88         egid = getegid();
89 }
90
91 /*
92  * "Started with special privileges" means "started out set-UID or set-GID".
93  */
94 gboolean
95 started_with_special_privs(void)
96 {
97 #ifdef HAVE_ISSETUGID
98         return issetugid();
99 #else
100         return (ruid != euid || rgid != egid);
101 #endif
102 }
103
104 /*
105  * Permanently relinquish  set-UID and set-GID privileges.
106  * Ignore errors for now - if we have the privileges, we should
107  * be able to relinquish them.
108  */
109 void
110 relinquish_special_privs_perm(void)
111 {
112         setuid(ruid);
113         setgid(rgid);
114 }
115
116 #endif /* _WIN32 */