The Styleguide section has been moved to the Wireshark Developer's Guide.
[obnox/wireshark/wip.git] / tools / setuid-root.pl.in
1 #!/usr/bin/perl -w
2 #
3 # setuid-root - Enable/disable setuid for tshark and dumpcap.
4 #
5 # $Id$
6 #
7 # Copyright 2007, Luis Ontanon and Gerald Combs
8 #
9 # Wireshark - Network traffic analyzer
10 # By Gerald Combs <gerald@wireshark.org>
11 # Copyright 1998 Gerald Combs
12 #
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
27 sub usage() {
28   die <<FIN
29 Usage: $0 {enable|disable} [revert owner]
30
31 Examples:
32   $0 enable             # Changes owner to root and enables setuid
33   $0 disable            # Changes owner to \$SUDO_USER and disables setuid
34   $0 disable kurtv      # Changes owner to kurtv and disables setuid
35 FIN
36 }
37
38 $< == 0 or die "only root can run this script";
39
40 $bin_prefix = "@BIN_PREFIX@";
41
42 if ($#ARGV < 0) { usage(); }
43
44 $command = shift;
45 $command =~ tr/A-Z/a-z/;
46
47 $tshark_bin = "@TSHARK_BIN@";
48 $dumpcap_bin = "@DUMPCAP_BIN@";
49
50 die "Don't know prefix path" if length($bin_prefix) < 1;
51 die "Don't know tshark binary name" if length($tshark_bin) < 1;
52 die "Don't know dumpcap binary name" if length($dumpcap_bin) < 1;
53
54 $revert_owner = "";
55 if ($#ARGV >= 0) {
56   $revert_owner = shift;
57 }
58
59 if (length($revert_owner) < 1 && length($ENV{SUDO_USER}) > 0) {
60   $revert_owner = $ENV{SUDO_USER};
61 }
62
63 if ($command eq "enable") {
64   system("chown root $bin_prefix/$tshark_bin");
65   system("chown root $bin_prefix/$dumpcap_bin");
66   system("chmod ug+s $bin_prefix/$tshark_bin");
67   system("chmod ug+s $bin_prefix/$dumpcap_bin");
68   exit 0;
69
70
71 if ($command eq "disable"){
72   system("chmod ug-s $bin_prefix/$tshark_bin");
73   system("chmod ug-s $bin_prefix/$dumpcap_bin");
74   die "Can't revert owner" if length($revert_owner) < 1;
75   system("chown $revert_owner $bin_prefix/$tshark_bin");
76   system("chown $revert_owner $bin_prefix/$dumpcap_bin");
77   exit(0);
78 }
79
80 usage();