-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdebug.perl
executable file
·198 lines (165 loc) · 5.75 KB
/
debug.perl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/perl
# debug.perl
# Debug
#
# Created by Timothy Perfitt on 11/6/09.
# Copyright 2013 Twocanoes. All rights reserved.
use Getopt::Long;
use File::Slurp qw(write_file);
use File::Copy;
if ($> >0) {
print STDERR "Not running as root. Running as $>. Exiting\n";
sleep 50;
exit 0;
}
#set EUID and RUID to the same (should be 0);
$< = $>;
print STDERR "debug.perl starting\n";
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/sbin';
my $result=GetOptions("dns!" => \$dns,
"ds!" => \$ds,
"tcpdump=s" => \$tcpdump,
"syslog!"=>\$syslog,
"install!"=>\$install,
"ports=s"=>\$ports,
"disable!"=>\$disable);
if (!$result) { die "usage: debug.perl [-dns] [-tcpdump <interface>] [-ds]" } ;
if ($install) {
if ($disable) {
print STDERR "disabling\n";
system("rm /var/scripts/debug.perl");
if (-e "/Library/LaunchDaemons/com.twocanoes.debug.plist") {
system("launchctl unload -w /Library/LaunchDaemons/com.twocanoes.debug.plist");
system("rm /Library/LaunchDaemons/com.twocanoes.debug.plist");
system("launchctl remove com.twocanoes.tcpdump");
}
if (-e "/tmp/.dnsdebugging") {
system("rm /tmp/.dnsdebugging");
system("killall -USR2 mDNSResponder");
}
if (-e "/tmp/.syslogverbose") {
system("rm /tmp/.syslogverbose");
print "setting syslog master filter to off\n";
system ("syslog -c 0 off; syslog -c syslog info");
}
if (-e "/tmp/.tcpdumprunning"){
system("rm /tmp/.tcpdumprunning");
system("launchctl remove com.twocanoes.tcpdump");
}
print "setting opendirectoryd logging level back to default \n";
system("/usr/bin/odutil set log default");
exit 0;
}
print "installing\n";
my $installargs="";
if ($dns) { $installargs=$installargs."<string>-dns</string>" };
if ($ds) { $installargs=$installargs."<string>-ds</string>" };
if ($tcpdump && $ports) { $installargs=$installargs."<string>-tcpdump</string><string>$tcpdump</string><string>-ports</string><string>$ports</string>" };
if ($syslog) { $installargs=$installargs."<string>-syslog</string>" };
my $launchditem = <<HERE;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.twocanoes.debug</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/perl</string>
<string>/var/scripts/debug.perl</string>
$installargs
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
HERE
my $mode = 0755;
if (!(-e "/var/scripts")) {
mkdir "/var/scripts";
chmod $mode,"/var/scripts";
chown 0,0,"/var/scripts";
}
copy("$0","/var/scripts/debug.perl");
$mode = 0755;
chmod $mode,"/var/scripts/debug.perl";
chown 0,0,"/var/scripts/debug.perl";
# if (-e "/Library/LaunchDaemons/com.twocanoes.debug.plist") {
print STDERR "unloading old launchd item\n";
system("launchctl unload -w /Library/LaunchDaemons/com.twocanoes.debug.plist");
# }
write_file('/Library/LaunchDaemons/com.twocanoes.debug.plist', $launchditem);
if (-e "/var/scripts/debug.perl" ) { print STDERR "we have foujnd the file!\n";}
else {print STDERR "no file found\n";}
print STDERR "loading new launchd item\n";
system("launchctl load -w /Library/LaunchDaemons/com.twocanoes.debug.plist");
exit 0;
}
if ($ds) {
print "opendirectoryd debugging requested to be turned on\n";
system("/usr/bin/odutil set log debug");
}
else {
print "opendirectoryd debugging requested to be turned off\n";
system("/usr/bin/odutil set log default");
}
if ($dns){
if (-e "/tmp/.dnsdebugging") {
print "DNS Debugging is already on!\n";
}
else {
my $cnt=0;
while (not -e "/var/run/mDNSResponder") {
print "waiting for mDNSResponder\n";
sleep 1;
$cnt++;
if ($cn>30) {
print "giving up on mDNSResponder starting\n";
last;
}
}
print "setting DNS Debugging\n";
system("syslog -c mDNSResponder -d");
system("killall -INFO mDNSResponder");
system("killall -USR2 mDNSResponder");
system("touch /tmp/.dnsdebugging");
}
}
else {
if (-e "/tmp/.dnsdebugging") {
print "Turning off DNS logging\n";
system("rm /tmp/.dnsdebugging");
system("killall -USR2 mDNSResponder");
}
else {
print "DNS Debugging already off\n";
}
}
if ($syslog){
system("touch /tmp/.syslogverbose");
print "setting syslog to debug level\n";
system ("syslog -c 0 -d; syslog -c syslog -d");
}
else {
system("rm /tmp/.syslogverbose");
print "setting syslog master filter to off\n";
system ("syslog -c 0 off; syslog -c syslog info");
}
if ($tcpdump && $ports) {
print "!!!!!!!!";
system("touch /tmp/.tcpdumprunning");
print STDERR "setting tcpdump Debugging\n";
my $now=`date +%Y%h%d%H%M%S`;
chomp($now);
if (-e "/var/scripts/.interface") {
$tcpdump=`cat /var/scripts/.interface`;
chomp($tcpdump);
}
system("launchctl remove com.twocanoes.tcpdump");
print "running system(\"launchctl submit -l com.twocanoes.tcpdump tcpdump -i ${tcpdump} -s 0 -K -w /var/log/tcpdump.$now.dmp $ports\")";
system("launchctl submit -l com.twocanoes.tcpdump tcpdump -i ${tcpdump} -s 0 -K -w /var/log/tcpdump.$now.dmp port $ports");
}
else {
system("launchctl remove com.twocanoes.tcpdump");
system("rm /tmp/.tcpdumprunning");
}