-
Notifications
You must be signed in to change notification settings - Fork 175
/
ljsysban.pl
executable file
·209 lines (177 loc) · 8 KB
/
ljsysban.pl
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
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/perl
#
# This code was forked from the LiveJournal project owned and operated
# by Live Journal, Inc. The code has been modified and expanded by
# Dreamwidth Studios, LLC. These files were originally licensed under
# the terms of the license supplied by Live Journal, Inc, which can
# currently be found at:
#
# http://code.livejournal.org/trac/livejournal/browser/trunk/LICENSE-LiveJournal.txt
#
# In accordance with the original license, this code and all its
# modifications are provided under the GNU General Public License.
# A copy of that license can be found in the LICENSE file included as
# part of this distribution.
use strict;
use Getopt::Long;
# parse input options
my ($list, $add, $modify, $banid, $status, $bandate, $banuntil, $banlength, $what, $value, $note);
exit 1 unless GetOptions('list' => \$list,
'add' => \$add,
'modify' => \$modify,
'banid=s' => \$banid,
'status=s' => \$status,
'bandate=s' => \$bandate,
'banuntil=s' => \$banuntil,
'banlength=s' => \$banlength,
'what=s' => \$what,
'value=s' => \$value,
'note=s' => \$note,
);
# did they give valid input?
my $an_opt = ($what || $value || $status || $bandate || $banuntil || $banlength || $note);
unless (($list && (($banid && ! $an_opt) || (! $banid && $an_opt)) ||
($add && $what && $value) ||
($modify && $banid && $an_opt))) {
die "Usage: ljsysban.pl [opts]\n\n" .
" --list { <--banid=?> | or one of:\n" .
" [--what=? --status=? --bandate=datetime --banuntil=datetime\n" .
" --value=? --note=?]\n" .
" }\n\n" .
" --add <--what=? --value=?\n" .
" [--status=? --bandate=datetime { --banuntil=datetime | --banlength=duration } --note=?]>\n\n" .
" --modify <--banid=?>\n" .
" [--status=? --bandate=datetime { --banuntil=datetime |\n" .
" --banlength=duration } --value=? --note=?]\n\n" .
"datetime in format 'YYYY-MM-DD HH:MM:SS', duration in format 'N[dhms]' e.g. '5d' or '3h'.\n\n" .
"examples:\n" .
" ljsysban.pl --list --what=ip --value=127.0.0.1\n" .
" ljsysban.pl --add --what=email --value=test\@test.com --banuntil='2006-06-01 00:00:00' --note='test'\n" .
" ljsysban.pl --add --what=uniq --value=jd87fdnef8jf8jef --banlength=3d --note='3 day ban'\n\n";
}
# now load in the beast
BEGIN {
require "$ENV{'LJHOME'}/cgi-bin/ljlib.pl";
}
use LJ::Sysban;
my $dbh = LJ::get_db_writer();
# list bans
if ($list) {
my $where;
if ($banid) {
$where = "banid=" . $dbh->quote($banid);
} else {
my @where = ();
push @where, ("what=" . $dbh->quote($what)) if $what;
push @where, ("value=" . $dbh->quote($value)) if $value;
push @where, ("status=" . $dbh->quote($status)) if $status;
push @where, ("bandate=" . $dbh->quote($bandate)) if $bandate;
push @where, ("banuntil=" . $dbh->quote($banuntil)) if $banuntil;
push @where, ("note=" . $dbh->quote($note)) if $note;
$where = join(" AND ", @where);
}
my $sth = $dbh->prepare("SELECT * FROM sysban WHERE $where ORDER BY bandate ASC");
$sth->execute;
my $ct;
while (my $ban = $sth->fetchrow_hashref) {
print "> banid: $ban->{'banid'}, status: $ban->{'status'}, ";
print "bandate: " . ($ban->{'bandate'} ? $ban->{'bandate'} : "BOT") . ", ";
print "banuntil: " . ($ban->{'banuntil'} ? $ban->{'banuntil'} : "EOT") . "\n";
print "> what: $ban->{'what'}, value: $ban->{'value'}\n";
print "> note: $ban->{'note'}\n" if $ban->{'note'};
print "\n";
$ct++;
}
print "\n\tNO MATCHES\n\n" unless $ct;
exit;
}
# verify ban length and convert to banuntil as necessary
if ($banlength) {
die "--banlength must be of format N[dmhs] such as 3d, 5h, 60m, 35s.\n"
unless $banlength =~ /^(\d+)([dhms])$/i;
my ($num, $type) = ($1, lc $2);
$banlength = "DATE_ADD(NOW(), INTERVAL $num " .
{ 'd' => "DAY", 'h' => "HOUR", 'm' => "MINUTE", 's' => "SECOND" }->{$type} . ")";
$banuntil = $dbh->selectrow_array("SELECT $banlength");
die $dbh->errstr if $dbh->err;
}
# add new ban
if ($add) {
$status = ($status eq 'expired' ? 'expired' : 'active');
$dbh->do("INSERT INTO sysban (status, what, value, note, bandate, banuntil)" .
"VALUES (?, ?, ?, ?, " .
($bandate ? $dbh->quote($bandate) : 'NOW()') . ", " .
($banuntil ? $dbh->quote($banuntil) : 'NULL') . ")",
undef, $status, $what, $value, $note);
die $dbh->errstr if $dbh->err;
my $insertid = $dbh->{'mysql_insertid'};
LJ::Sysban::ban_do( $what, $value, LJ::mysqldate_to_time( $banuntil ) );
# log in statushistory
LJ::statushistory_add(0, 0, 'sysban_add',
"banid=$insertid; status=$status; " .
"bandate=" . ($bandate || LJ::mysql_time()) . "; " .
"banuntil=" . ($banuntil || 'NULL') . "; " .
"what=$what; value=$value; " .
"note=$note;");
print "CREATED: banid=$insertid\n";
exit;
}
# modify existing ban
if ($modify) {
# load selected ban
my $ban = $dbh->selectrow_hashref("SELECT * FROM sysban WHERE banid=?", undef, $banid);
die $dbh->errstr if $dbh->err;
my @set = ();
# ip/uniq ban and we're going to change the value
if (($value && $value ne $ban->{'value'}) ||
$banuntil && $banuntil ne $ban->{'banuntil'} ||
($status && $status ne $ban->{'status'} && $status eq 'expired')) {
LJ::Sysban::ban_undo( $ban->{what}, $value || $ban->{value} );
}
# what - must have a value
if ($what && $what ne $ban->{'what'}) {
$ban->{'what'} = $what;
push @set, "what=" . $dbh->quote($ban->{'what'});
}
# ip/uniq ban and we are going to change the value
if (($value && $value ne $ban->{'value'}) ||
$banuntil && $banuntil ne $ban->{'banuntil'} ||
($status && $status ne $ban->{'status'} && $status eq 'active')) {
my $new_value = $value || $ban->{value};
my $new_banuntil = LJ::mysqldate_to_time( $banuntil || $ban->{banuntil} );
LJ::Sysban::ban_do( $ban->{what}, $new_value, $new_banuntil );
}
# value - must have a value
if ($value && $value ne $ban->{'value'}) {
$ban->{'value'} = $value;
push @set, "value=" . $dbh->quote($ban->{'value'});
}
# status - must have a value
if ($status && $status ne $ban->{'status'}) {
$ban->{'status'} = ($status eq 'expired' ? 'expired' : 'active');
push @set, "status=" . $dbh->quote($ban->{'status'});
}
# banuntil
if ($banuntil && $banuntil ne $ban->{'banuntil'}) {
$ban->{'banuntil'} = ($banuntil && $banuntil ne 'NULL') ? $banuntil : 0;
push @set, "banuntil=" . ($ban->{'banuntil'} ? $dbh->quote($ban->{'banuntil'}) : 'NULL');
}
# bandate - must have a value
if ($bandate && $bandate ne $ban->{'bandate'}) {
$ban->{'bandate'} = $bandate;
push @set, "bandate=" . $dbh->quote($ban->{'bandate'});
}
# note - can be changed to blank
if (defined $note && $note ne $ban->{'note'}) {
$ban->{'note'} = $note;
push @set, "note=" . $dbh->quote($ban->{'note'});
}
# do update
$dbh->do("UPDATE sysban SET " . join(", ", @set) . " WHERE banid=?", undef, $ban->{'banid'});
# log in statushistory
my $msg; map { $msg .= " " if $msg;
$msg .= "$_=$ban->{$_};" } qw(banid status bandate banuntil what value note);
LJ::statushistory_add(0, 0, 'sysban_mod', $msg);
print "MODIFIED: banid=$banid\n";
exit;
}