Skip to content

Latest commit

 

History

History
executable file
·
1738 lines (1397 loc) · 51.9 KB

IcculusNews_daemon.pl

File metadata and controls

executable file
·
1738 lines (1397 loc) · 51.9 KB
 
May 19, 2002
May 19, 2002
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
#!/usr/bin/perl -w -T
#-----------------------------------------------------------------------------
#
# Copyright (C) 2000 Ryan C. Gordon (icculus@icculus.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#-----------------------------------------------------------------------------
# !!! FIXME: Remove most of the "die()" calls.
#-----------------------------------------------------------------------------
# Revision history:
#-----------------------------------------------------------------------------
use strict; # don't touch this line, nootch.
use warnings; # don't touch this line, either.
use DBI; # or this. I guess. Maybe.
#use Socket; # in fact, if it says "use", don't touch it.
#use IO::Handle; # blow.
Mar 24, 2006
Mar 24, 2006
33
use HTML::Entities;
May 19, 2002
May 19, 2002
34
35
# Version of IcculusNews. Change this if you are forking the code.
Mar 24, 2006
Mar 24, 2006
36
my $version = "2.0.2";
May 19, 2002
May 19, 2002
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Global rights constants.
use constant canSeeAllQueues => 1 << 0;
use constant canSeeAllDeleted => 1 << 1;
use constant canSeeAllUnapproved => 1 << 2;
use constant canDeleteAllItems => 1 << 3;
use constant canPurgeAllItems => 1 << 4;
use constant canApproveAllItems => 1 << 5;
use constant canTweakUsers => 1 << 6;
use constant canCreateQueues => 1 << 7;
use constant canLockUsers => 1 << 8;
use constant canLockAllQueues => 1 << 9;
use constant canChangeOthersPasswords => 1 << 10;
use constant canEditAllItems => 1 << 11;
use constant canNotAuthorize => 1 << 12;
use constant canAccessAllLockedQueues => 1 << 13;
Jun 11, 2002
Jun 11, 2002
54
use constant canCreateUsers => 1 << 14;
Jun 14, 2002
Jun 14, 2002
55
use constant canMoveAllItems => 1 << 15;
May 19, 2002
May 19, 2002
56
57
58
59
60
61
62
63
64
65
66
67
# Queue rights constants.
use constant canSeeInvisible => 1 << 0;
use constant canSeeDeleted => 1 << 1;
use constant canSeeUnapproved => 1 << 2;
use constant canDeleteItems => 1 << 3;
use constant canPurgeItems => 1 << 4;
use constant canApproveItems => 1 << 5;
use constant canEditItems => 1 << 6;
use constant canMakeInvisible => 1 << 7;
use constant canLockQueue => 1 << 8;
use constant canAccessLocked => 1 << 9;
Jun 14, 2002
Jun 14, 2002
68
use constant canMoveItems => 1 << 10;
May 19, 2002
May 19, 2002
69
70
71
72
73
74
75
76
77
78
79
80
# Queue flags constants.
use constant queueInvisible => 1 << 0;
use constant queueLocked => 1 << 1;
# syslog constants.
use constant syslogNone => 0;
use constant syslogError => 1;
use constant syslogDaemon => 2;
use constant syslogAuth => 3;
use constant syslogCommand => 4;
use constant syslogSuccess => 5;
Jun 11, 2002
Jun 11, 2002
81
use constant syslogAll => 0xFFFFFFFF;
May 19, 2002
May 19, 2002
82
83
84
85
86
87
88
89
90
91
92
93
#-----------------------------------------------------------------------------#
# CONFIGURATION VARIABLES: Change to suit your needs... #
#-----------------------------------------------------------------------------#
# Set this to one to log just errors to the standard Unix syslog facility
# (requires Sys::Syslog qw(:DEFAULT setlogsock) ...). Set it to two to also
# log daemon start/stop info. Three logs all auth attempts. Set it to four to
# also log all news commands sent by a client. Set it to zero to disable.
# (see constants, above)
my $use_syslog = syslogDaemon;
Jun 12, 2002
Jun 12, 2002
94
95
96
97
98
99
100
101
# Set this to name of your site. Some daemon messages and forgotten password
# emails will include it.
my $sitename = 'icculus.org';
# Set this to the email address for the guy that takes care of problems.
# Some error messages will include it.
my $admin_email = 'newsmaster@icculus.org';
May 19, 2002
May 19, 2002
102
103
104
105
106
107
108
109
110
111
# Set this to the number of seconds you'd like to delay before responding to
# an incorrect authentication. The longer the delay, the more you frustrate
# crackers that are trying to brute-force their way into someone else's
# login. Then again, the longer the delay, the more you frustrate your valid
# users when they mistype their passwords. 2 seconds seems to be a good
# compromise. This must be an integer value; no fractions, please.
my $invalid_auth_delay = 2;
# This is the maximum size, in bytes, that a command can be. This is
# to prevent malicious clients from trying to fill all of system memory.
Mar 24, 2006
Mar 24, 2006
112
my $max_command_size = 1024;
May 19, 2002
May 19, 2002
113
114
115
116
117
118
# This is the maximum size, in bytes, that a news entry can be when submitted
# through this daemon. This is to prevent malicious clients from trying to
# fill all of system memory. Make this big, though. Stuff in the database
# is free game; there is no size limit on what is read back from there, since
# we assume that data can be trusted.
Mar 24, 2006
Mar 24, 2006
119
my $max_news_size = 128 * 1024;
May 19, 2002
May 19, 2002
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
# This is the name of your anonymous account.
my $anon = "anonymous hoser";
# You can screw up your output with this, if you like.
# (Not used at the moment, check syslog instead.)
my $debug = 0;
# The processes path is replaced with this string, for security reasons, and
# to satisfy the requirements of Taint mode. Make this as simple as possible.
my $safe_path = '/usr/bin:/usr/local/bin';
# Turn the process into a daemon. This will handle creating/answering socket
# connections, and forking off children to handle them. This flag can be
# toggled via command line options (--daemonize, --no-daemonize, -d), but
# this sets the default. Daemonizing tends to speed up processing (since the
# script stays loaded/compiled), but may cause problems on systems that
# don't have a functional fork() or IO::Socket::INET package. If you don't
# daemonize, this program reads requests from stdin and writes results to
# stdout, which makes it suitable for command line use or execution from
# inetd and equivalents.
my $daemonize = 0;
# This is only used when daemonized. Specify the port on which to listen for
# incoming connections. The randomly-chosen "official" IcculusNews port is
# currently 263. Hopefully this won't have to change.
my $server_port = 263;
# Set this to immediately drop priveledges by setting uid and gid to these
# values. Set to undef to not attempt to drop privs. You can keep the privs
# by setting these to undef (risky!), if you really want to.
#my $wanted_uid = undef;
#my $wanted_gid = undef;
my $wanted_uid = 1083; # (This is the uid of "iccnews" ON _MY_ SYSTEM.)
my $wanted_gid = 1006; # (This is the gid of "iccnews" ON _MY_ SYSTEM.)
# This is only used when daemonized. Specify the maximum number of clients
# to service at once. A separate child process is fork()ed off for each
# client, and if there are more simulatenous connections then this value, the
# extra clients will be made to wait until some of the current requests are
# serviced. 5 to 10 is usually a good number. Set it higher if you get a
# massive amount of finger requests simultaneously.
my $max_connects = 10;
# This is how long, in seconds, before an idle connection will be summarily
# dropped. This prevents abuse from people hogging a connection without
# actually sending a request, without this, enough connections like this
# will block legitimate ones. At worst, they can only block for this long
# before being booted and thus freeing their connection slot for the next
# guy in line. Setting this to undef lets people sit forever, but removes
# reliance on the IO::Select package. Note that this timeout is how long
# the user has to complete the read_from_client() function, so don't set
# it so low that legitimate lag can kill them. The default is usually safe.
my $read_timeout = 15;
# This is the base directory for writing RDF files to. Make sure that this
# process has write access and that there's a dir separator at the end!
my $rdfbase = '/webspace/rdf/';
# New users are assigned these rights by default. Refer to "Global rights
# constants", above. If you want to lock out new accounts until you manually
# approve them, "canNotAuthorize" is appropriate. If you want anyone to be
# able to create personal news queues without running it by you first,
# "canCreateQueues" is a good idea.
Jun 11, 2002
Jun 11, 2002
184
185
186
187
188
189
190
my $default_user_rights = canCreateQueues | canCreateUsers;
# This is the same as $default_user_rights, but for the anonymous account.
# If you want to lock out new users altogether, don't set canCreateUsers
# here; this way account creation has to go through users you have
# authorized to create accounts, which is militant, but potentially useful.
my $anonymous_user_rights = canCreateUsers;
May 19, 2002
May 19, 2002
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Owners of a queue, when initially creating the queue, are assigned these
# rights by default. Refer to "Queue rights constants", above. You probably
# want to give them everything. These rights only apply to the queue creator
# when interacting with her own queue.
my $default_queue_owner_rights = canSeeInvisible | canSeeDeleted |
canSeeUnapproved | canDeleteItems |
canPurgeItems | canApproveItems |
canEditItems | canMakeInvisible |
canLockQueue | canAccessLocked;
# New queues are assigned these flags by default. Refer to "Queue flags
# constants", above. This should probably be left at zero, unless you want
# to make queues globally invisible until the creator toggles them.
my $default_queue_flags = 0;
May 19, 2002
May 19, 2002
207
208
209
210
# Newly created users are assigned to this queue by default. The first queue
# created is 1, so that's probably a good one, unless you've got some other
# queue you prefer. Users can change to a different queue via the QUEUE
# command, and assign a new default queue with the SETDEFAULTQUEUE command.
Jun 11, 2002
Jun 11, 2002
211
212
213
# If the default queue is 0, the web interface takes users to the "post"
# action by default, instead of the queue view.
my $default_queue = 0;
May 19, 2002
May 19, 2002
214
Jun 12, 2002
Jun 12, 2002
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# This is the minimum number of characters that will be used when generating
# a new password to replace a forgotten one. Eight is usually reasonable.
my $forgotten_password_minsize = 8;
# This is the maximum number of characters that will be used when generating
# a new password to replace a forgotten one. Ten to twelve is usually
# reasonable.
my $forgotten_password_maxsize = 12;
# Set this to the name of a device to read random data from. If you don't have
# such a device (not on a Unix box or something), set it to undef to just
# use the built-in rand() function.
my $random_device = '/dev/urandom';
May 19, 2002
May 19, 2002
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# This is the host to connect to for database access.
my $dbhost = 'localhost';
# This is the username for the database connection.
my $dbuser = 'newsmgr';
# The database password can be entered in three ways: Either hardcode it into
# $dbpass, (which is a security risk, but is faster if you have a completely
# closed system), or leave $dbpass set to undef, in which case this script
# will try to read the password from the file specified in $dbpassfile (which
# means that this script and the database can be touched by anyone with read
# access to that file), or leave both undef to have DBI get the password from
# the DBI_PASS environment variable, which is the most secure, but least
# convenient.
my $dbpass = undef;
my $dbpassfile = '/etc/IcculusNews_dbpass.txt';
# The name of the database to use once connected to the database server.
my $dbname = 'IcculusNews';
# Names of database tables.
my $dbtable_users = 'news_users';
my $dbtable_queues = 'news_queues';
my $dbtable_items = 'news_items';
my $dbtable_queue_rights = 'news_queue_rights';
#-----------------------------------------------------------------------------#
# The rest is probably okay without you laying yer dirty mits on it. #
#-----------------------------------------------------------------------------#
my $link = undef; # link to database.
my $auth_uid = undef; # authenticated user id.
my $ipaddr = undef; # IP address of client.
my $queue = 0;
my $current_global_rights = 0;
my $current_queue_rights = 0;
my %commands;
sub long2ip {
my $l = shift;
my $x1 = (($l & 0xFF000000) >> 24);
my $x2 = (($l & 0x00FF0000) >> 16);
my $x3 = (($l & 0x0000FF00) >> 8);
my $x4 = (($l & 0x000000FF) >> 0);
return("$x1.$x2.$x3.$x4");
}
sub do_log {
my $level = shift;
my $text = shift;
if ($use_syslog >= $level) {
Dec 11, 2004
Dec 11, 2004
284
$text =~ s/%/%%/g; # syslog apparently does formatting or something.
May 19, 2002
May 19, 2002
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
syslog("info", "$text\n")
or report_fatal("Couldn't write to syslog: $!");
}
}
my $in_error_handler = 0;
sub handle_error {
my ($errmsg, $fatal) = @_;
print "- $errmsg\012";
if (not $in_error_handler) {
$in_error_handler = 1;
do_log(syslogError, "IcculusNews error: \"$errmsg\"");
$in_error_handler = 0;
}
if ($fatal) {
$link->disconnect() if defined $link;
exit 42;
}
}
sub report_error {
handle_error($_[0], 0);
}
sub report_fatal {
handle_error($_[0], 1);
}
sub report_success {
my $errmsg = shift;
do_log(syslogSuccess, "IcculusNews success: \"$errmsg\"");
print "+ $errmsg\012";
}
sub new_crypt_salt {
return(join('', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand(64), rand(64)]));
}
sub get_database_link {
if (not defined $link) {
if (not defined $dbpass) {
if (defined $dbpassfile) {
open(FH, $dbpassfile)
or report_fatal("failed to open $dbpassfile: $!");
$dbpass = <FH>;
chomp($dbpass);
$dbpass =~ s/\A\s*//;
$dbpass =~ s/\s*\Z//;
close(FH);
}
}
my $dsn = "DBI:mysql:database=$dbname;host=$dbhost";
$link = DBI->connect($dsn, $dbuser, $dbpass)
or report_fatal(DBI::errstr);
}
return($link);
}
sub read_from_client {
my $max_chars = shift;
my $retval = '';
my $count = 0;
my $s = undef;
my $elapsed = undef;
my $starttime = undef;
if (defined $read_timeout) {
$s = new IO::Select();
$s->add(fileno(STDIN));
$starttime = time();
$elapsed = 0;
}
while (1) {
if (defined $read_timeout) {
my $ready = scalar($s->can_read($read_timeout - $elapsed));
report_fatal("input timeout.") if (not $ready);
$elapsed = (time() - $starttime);
}
my $ch;
my $rc = sysread(STDIN, $ch, 1);
report_fatal("unexpected EOF.") if ($rc != 1);
if ($ch ne "\015") {
return($retval) if ($ch eq "\012");
$retval .= $ch;
$count++;
report_fatal("input overflow. chatter less.") if ($count >= $max_chars);
}
}
return(undef); # shouldn't ever hit this.
}
sub process_command {
my $req = shift;
my $cmdstr = read_from_client($max_command_size);
my ($cmd, $args) = $cmdstr =~ /\A\s*([a-zA-Z]+)\s*(.*)\Z/;
$args = undef if ((defined $args) and ($args eq ''));
if (not defined $cmd) {
$cmd = '';
} else {
$cmd =~ tr/a-z/A-Z/;
}
report_fatal("Required $req") if ((defined $req) and ($req ne $cmd));
report_success("Uh, okay."), return 1 if ($cmd eq ''); # blank line.
do_log(syslogCommand, "IcculusNews command: \"$cmdstr\"");
if (defined $commands{$cmd}) {
return($commands{$cmd}->($args));
}
report_error("Unknown command \"$cmd\".");
return(1);
}
sub update_queue_rights {
$current_queue_rights = shift;
if ($current_global_rights & canSeeAllDeleted) {
$current_queue_rights |= canSeeDeleted;
}
if ($current_global_rights & canSeeAllUnapproved) {
$current_queue_rights |= canSeeUnapproved;
}
if ($current_global_rights & canDeleteAllItems) {
$current_queue_rights |= canDeleteItems;
}
if ($current_global_rights & canPurgeAllItems) {
$current_queue_rights |= canPurgeItems;
}
if ($current_global_rights & canApproveAllItems) {
$current_queue_rights |= canApproveItems;
}
if ($current_global_rights & canEditAllItems) {
$current_queue_rights |= canEditItems;
}
if ($current_global_rights & canSeeAllQueues) {
$current_queue_rights |= canSeeInvisible;
}
if ($current_global_rights & canLockAllQueues) {
$current_queue_rights |= canLockQueue;
}
if ($current_global_rights & canAccessAllLockedQueues) {
$current_queue_rights |= canAccessLocked;
}
Jun 14, 2002
Jun 14, 2002
456
457
458
459
if ($current_global_rights & canMoveAllItems) {
$current_queue_rights |= canMoveItems;
}
May 19, 2002
May 19, 2002
460
461
462
463
464
465
466
467
468
469
470
471
472
473
}
sub generate_rdf {
my $qid = shift;
my $max = shift;
$qid = $queue if not defined $qid;
return ('bogus queue id', undef, undef) if ($qid <= 0);
my $sql;
my $link = get_database_link();
my @row = undef;
my $sth = undef;
May 19, 2002
May 19, 2002
474
$sql = "select name, description, itemarchiveurl, itemviewurl, rdffile," .
May 19, 2002
May 19, 2002
475
476
477
478
479
480
481
482
" siteurl, rdfurl, rdfimageurl, rdfitemcount" .
" from $dbtable_queues where id=$qid";
$sth = $link->prepare($sql);
$sth->execute() or die "can't execute the query: $sth->errstr";
@row = $sth->fetchrow_array();
$sth->finish();
return ('failed to read queue attributes', undef, undef) if (not @row);
Mar 24, 2006
Mar 24, 2006
483
484
485
486
my $queuename = encode_entities($row[0]);
my $queuedesc = encode_entities($row[1]);
my $itemarcurl = encode_entities($row[2]);
my $itemviewurl = $row[3]; # must html encode later!
May 19, 2002
May 19, 2002
487
my $rdffile = $row[4];
Mar 24, 2006
Mar 24, 2006
488
489
490
my $siteurl = encode_entities($row[5]);
my $rdfurl = encode_entities($row[6]);
my $rdfimgurl = encode_entities($row[7]);
May 19, 2002
May 19, 2002
491
$max = $row[8] if not defined $max;
May 19, 2002
May 19, 2002
492
493
494
495
496
497
498
499
500
501
502
503
504
$sql = "select t1.author, t1.id, t1.title, t1.postdate, t2.name," .
" t1.ip, t1.approved, t1.deleted" .
" from $dbtable_items as t1" .
" left outer join $dbtable_users as t2" .
" on t1.author=t2.id" .
" where t1.queueid=$queue" .
" and t1.approved=1 and t1.deleted=0" .
" order by postdate desc limit $max";
$sth = $link->prepare($sql);
$sth->execute() or die "can't execute the query: $sth->errstr";
Dec 11, 2004
Dec 11, 2004
505
506
my $channelimg = '';
my $rdfimg = '';
May 19, 2002
May 19, 2002
507
if (defined $rdfimgurl) {
Dec 11, 2004
Dec 11, 2004
508
509
510
511
512
513
$channelimg = "<image rdf:resource=\"$rdfimgurl\" />";
$rdfimg .= "<image>\n";
$rdfimg .= " <title>$queuename</title>\n";
$rdfimg .= " <url>$rdfimgurl</url>\n";
$rdfimg .= " <link>$siteurl</link>\n";
$rdfimg .= " </image>";
May 19, 2002
May 19, 2002
514
515
}
Dec 11, 2004
Dec 11, 2004
516
517
my $rdfitems = '';
my $digestitems = '';
May 19, 2002
May 19, 2002
518
519
520
while (my @row = $sth->fetchrow_array()) {
my $authorid = $row[0];
my $itemid = $row[1];
Mar 24, 2006
Mar 24, 2006
521
522
523
my $itemtitle = encode_entities($row[2]);
my $itempostdate = encode_entities($row[3]);
my $authorname = encode_entities(((not defined $row[4]) ? $anon : $row[4]));
May 19, 2002
May 19, 2002
524
525
526
527
528
my $ipaddr = long2ip($row[5]);
my $approved = $row[6];
my $deleted = $row[7];
my $viewurl = $itemviewurl;
1 while ($viewurl =~ s/\%id/$itemid/);
Mar 24, 2006
Mar 24, 2006
529
$viewurl = encode_entities($viewurl);
May 19, 2002
May 19, 2002
530
Dec 11, 2004
Dec 11, 2004
531
532
533
534
535
536
537
538
539
540
541
542
543
544
$digestitems .= "<rdf:li rdf:resource=\"$viewurl\" />\n ";
$rdfitems .= "\n";
$rdfitems .= " <item rdf:about=\"$viewurl\">\n";
$rdfitems .= " <title>$itemtitle</title>\n";
$rdfitems .= " <link>$viewurl</link>\n";
$rdfitems .= " <author>$authorname</author>\n";
$rdfitems .= " <authorid>$authorid</authorid>\n";
$rdfitems .= " <itemid>$itemid</itemid>\n";
$rdfitems .= " <postdate>$itempostdate</postdate>\n";
$rdfitems .= " <ipaddr>$ipaddr</ipaddr>\n";
$rdfitems .= " <approved>$approved</approved>\n";
$rdfitems .= " <deleted>$deleted</deleted>\n";
$rdfitems .= " </item>";
May 19, 2002
May 19, 2002
545
546
547
}
$sth->finish();
Dec 11, 2004
Dec 11, 2004
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
my $rdf = <<__EOF__;
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/">
<channel rdf:about="$rdfurl">
<title>$queuename</title>
<link>$siteurl</link>
<archives>$itemarcurl</archives>
<description>$queuedesc</description>
$channelimg
<items>
<rdf:Seq>
$digestitems
</rdf:Seq>
</items>
</channel>
$rdfimg
$rdfitems
</rdf:RDF>
__EOF__
May 19, 2002
May 19, 2002
574
575
576
577
return(undef, $rdf, $rdffile);
}
May 19, 2002
May 19, 2002
578
sub update_rdf {
May 19, 2002
May 19, 2002
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
my ($err, $rdf, $filename) = generate_rdf();
do_log(syslogError, "RDF build failed: $err"), return 0 if (defined $err);
$filename ="$rdfbase$filename";
if (not open(RDF, '>', $filename)) {
do_log(syslogError, "Failed to open $filename: $!");
return 0;
}
print RDF $rdf;
close(RDF);
return 1;
}
sub toggle_approve {
my $id = shift;
my $newval = shift;
if ((not defined $id) or ($id !~ /\A\d+\Z/)) {
report_error('argument must be number.');
return 1;
}
report_error('no queue selected.'), return 1 if $queue == 0;
if (!($current_queue_rights & canApproveItems)) {
report_error("You don't have permission to do that.");
return 1;
}
$newval = 1 if $newval; # make sure it's '1', as opposed to non-zero.
my $otherval = (($newval) ? 0 : 1);
my $link = get_database_link();
my $sql = "update $dbtable_items set approved=$newval" .
" where id=$id and queueid=$queue and" .
" deleted=0 and approved=$otherval";
my $rc = $link->do($sql);
if (not defined $rc) {
report_error("can't execute the query: $link->errstr");
} elsif ($rc > 0) {
report_success("Approve flag toggled.");
update_rdf();
} else {
my $reason = "no idea why";
$sql = "select approved, deleted from $dbtable_items" .
" where id=$id and queueid=$queue";
my $sth = $link->prepare($sql);
$sth->execute() or die "can't execute the query: $sth->errstr";
my @row = $sth->fetchrow_array();
if (not @row) {
$reason = "no such item";
} else {
if ($row[0] == $newval) {
$reason = "already set like that";
} elsif ($row[1]) {
$reason = "item is flagged for deletion";
}
}
$sth->finish();
report_error("Failed to toggle approval flag: $reason.");
}
return(1);
}
sub toggle_delete {
my $id = shift;
my $newval = shift;
if ((not defined $id) or ($id !~ /\A\d+\Z/)) {
report_error('argument must be number.');
return 1;
}
report_error('no queue selected.'), return 1 if $queue == 0;
if (!($current_queue_rights & canDeleteItems)) {
report_error("You don't have permission to do that.");
return(1);
}
$newval = 1 if $newval; # make sure it's '1', as opposed to non-zero.
my $otherval = (($newval) ? 0 : 1);
my $link = get_database_link();
my $sql = "update $dbtable_items set deleted=$newval" .
" where id=$id and queueid=$queue and deleted=$otherval";
my $rc = $link->do($sql);
if (not defined $rc) {
report_error("can't execute: $link->errstr");
} elsif ($rc > 0) {
report_success("Deletion flag toggled.");
update_rdf();
} else {
my $reason = "no idea why";
$sql = "select deleted from $dbtable_items" .
" where id=$id and queueid=$queue";
my $sth = $link->prepare($sql);
$sth->execute() or die "can't execute the query: $sth->errstr";
my @row = $sth->fetchrow_array();
if (not @row) {
$reason = "no such item";
} else {
if ($row[0] == $otherval) {
$reason = "already set like that";
}
}
$sth->finish();
report_error("Failed to toggle deletion flag: $reason.");
}
return(1);
}
sub queue_is_forbidden {
my $queueflags = shift;
my $userrights = shift;
my $owner = shift;
#return 0 if ((defined $owner) and ($owner == $auth_uid));
if ($queueflags & queueInvisible) {
if (!($current_global_rights & canSeeAllQueues)) {
if (defined $userrights) {
return 1 if (!($userrights & canSeeInvisible));
}
}
}
if ($queueflags & queueLocked) {
if (!($current_global_rights & canAccessAllLockedQueues)) {
if (defined $userrights) {
return 1 if (!($userrights & canAccessLocked));
}
}
}
return 0; # it's kosher.
}
May 19, 2002
May 19, 2002
720
721
722
sub change_queue {
my $args = shift;
Jun 11, 2002
Jun 11, 2002
723
724
725
726
727
728
729
730
731
732
if ($args == 0) {
$queue = 0;
update_queue_rights(0);
} else {
my $link = get_database_link();
my $sql = "select q.flags, r.rights, q.owner" .
" from $dbtable_queues as q" .
" left outer join $dbtable_queue_rights as r" .
" on r.qid=q.id and r.uid=$auth_uid" .
" where q.id=$args";
May 19, 2002
May 19, 2002
733
Jun 11, 2002
Jun 11, 2002
734
735
736
737
my $sth = $link->prepare($sql);
$sth->execute() or report_fatal("can't execute query: $sth->errstr");
my @row = $sth->fetchrow_array();
$sth->finish();
May 19, 2002
May 19, 2002
738
Jun 11, 2002
Jun 11, 2002
739
740
741
742
743
744
745
746
$row[1] = 0 if ((@row) and (not defined $row[1]));
if ((not @row) or (queue_is_forbidden($row[0], $row[1], $row[2]))) {
return("Can't select that queue.");
} else {
update_queue_rights($row[1]);
$queue = $args;
}
May 19, 2002
May 19, 2002
747
}
Jun 11, 2002
Jun 11, 2002
748
May 19, 2002
May 19, 2002
749
750
751
752
return(undef); # no error.
}
Jun 14, 2002
Jun 14, 2002
753
754
755
756
757
758
759
760
761
762
763
764
765
766
sub can_change_queue {
my $newqueue = shift;
my $starting_queue = $queue;
my $starting_queue_rights = $current_queue_rights;
my $err = change_queue($newqueue);
# set this directly back.
$queue = $starting_queue;
$current_queue_rights = $starting_queue_rights;
return($err);
}
Jun 12, 2002
Jun 12, 2002
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
sub send_forgotten_password {
my ($user, $email, $pword) = @_;
$email = $1 if ($email =~ /\A(.*)\Z/); # untaint email address var.
my $msg = <<__EOF__;
From: $admin_email
Reply-To: $admin_email
To: $email
Subject: Forgotten IcculusNews password.
Hello, $user.
This is the IcculusNews daemon at $sitename. Someone, possibly you, has
told us that the account password associated with this email address has
been forgotten. In response, we have chosen a new password for the account.
If it wasn't you, don't worry; the smart-ass who entered the forgotten
password request does not have your newly-generated password, unless they
are reading this email.
Please log in to IcculusNews with the following password and change it to
something else. The sooner the better, too, since this email was probably
sent unencrypted across the Internet, and we're paranoid about wiretaps and
packet sniffers.
Username : $user
New Password : $pword
--The McManagement, $sitename.
$admin_email
__EOF__
Jun 12, 2002
Jun 12, 2002
801
open(MAILH, '|/usr/sbin/sendmail -t') or
Jun 12, 2002
Jun 12, 2002
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
return("Failed to run qmail-inject: $!");
if ( (not print MAILH $msg) or (not close(MAILH)) ) {
my $err = "Failed to write to qmail-inject pipe; $!";
close(MAILH);
return($err);
}
return(undef); # no error.
}
sub generate_pword {
my $retval = '';
my $diff = $forgotten_password_maxsize - $forgotten_password_minsize;
my $pwordbytes = $forgotten_password_minsize + int(rand($diff + 1));
my $dev = ((defined $random_device) and (open(RANDH, '<', $random_device)));
while ($pwordbytes > 0) {
my $idx = int((($dev) ? 62.0 * ord(getc(RANDH)) / 256.0 : rand(62)));
$retval .= (0..9, 'A'..'Z', 'a'..'z')[$idx];
$pwordbytes--;
}
close(RANDH) if ($dev);
return($retval);
}
May 19, 2002
May 19, 2002
831
832
833
834
835
836
837
838
839
840
# The actual commands the daemon responds to...
$commands{'AUTH'} = sub {
my $args = shift;
my $authuser;
if ((defined $args) and ($args eq '-')) {
$authuser = "anonymous account";
$auth_uid = 0;
$current_queue_rights = 0;
Jun 11, 2002
Jun 11, 2002
841
$current_global_rights = $anonymous_user_rights;
May 19, 2002
May 19, 2002
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
} else {
my ($user, $pass) = (undef, undef);
if (defined $args) {
($user, $pass) = $args =~ /\A\"(.+?)\"\s*\"(.+?)\"\Z/
}
if ((not defined $user) or (not defined $pass)) {
report_error('USAGE: AUTH <"user"> <"password">');
return(1);
}
$authuser = "\"$user\"";
my $link = get_database_link();
my $u = $link->quote($user);
May 19, 2002
May 19, 2002
858
859
my $sql = "select id, password, globalrights, defaultqueue" .
" from $dbtable_users where name=$u";
May 19, 2002
May 19, 2002
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
my $sth = $link->prepare($sql);
$sth->execute() or report_fatal("can't execute query: $sth->errstr");
my @row = $sth->fetchrow_array();
$sth->finish();
if ((not @row) or (crypt($pass, $row[1]) ne $row[1])) {
sleep($invalid_auth_delay);
report_error("Authorization for $authuser failed.");
return(1);
}
if ($row[2] & canNotAuthorize) {
report_error("This account has been disabled.");
return(1);
}
$auth_uid = $row[0];
$current_global_rights = $row[2];
May 19, 2002
May 19, 2002
879
880
881
882
883
my $err = change_queue($row[3]);
if (defined $err) {
report_error("Failed to select default queue: $err");
return(1);
}
May 19, 2002
May 19, 2002
884
885
886
887
}
do_log(syslogAuth, "IcculusNews auth: $authuser (id $auth_uid)");
May 19, 2002
May 19, 2002
888
report_success("$auth_uid, $queue");
May 19, 2002
May 19, 2002
889
890
891
892
return(1);
};
May 19, 2002
May 19, 2002
893
894
895
896
897
898
$commands{'USERINFO'} = sub {
report_success("$auth_uid, $queue");
return 1;
};
May 19, 2002
May 19, 2002
899
900
901
902
903
904
905
906
$commands{'QUEUEINFO'} = sub {
my $id = shift;
if ((not defined $id) or ($id !~ /\A\d+\Z/)) {
report_error('argument must be number.');
return 1;
}
my $link = get_database_link();
May 19, 2002
May 19, 2002
907
my $sql = "select q.name, q.description, q.itemarchiveurl," .
May 19, 2002
May 19, 2002
908
909
910
911
912
913
914
915
916
917
918
" q.itemviewurl, q.siteurl, q.rdfurl," .
" q.created, q.owner, u.name, q.flags, r.rights" .
" from $dbtable_queues as q, $dbtable_users as u" .
" left outer join $dbtable_queue_rights as r" .
" on r.qid=q.id and r.uid=$auth_uid" .
" where q.id=$id and q.owner=u.id";
my $sth = $link->prepare($sql);
$sth->execute() or report_fatal("can't execute query: $sth->errstr");
my @row = $sth->fetchrow_array();
$sth->finish();
May 19, 2002
May 19, 2002
919
920
$row[10] = 0 if ((@row) and (not defined $row[1]));
if ((not @row) or (queue_is_forbidden($row[9], $row[10], $row[7]))) {
May 19, 2002
May 19, 2002
921
922
923
924
925
926
927
928
929
930
931
932
933
report_error("Can't select that queue.");
return 1;
}
report_success("Here comes ...");
print("$row[0]\012");
print("$row[1]\012");
print("$row[2]\012");
print("$row[3]\012");
print("$row[4]\012");
print("$row[5]\012");
print("$row[6]\012");
print("$row[7]\012");
May 19, 2002
May 19, 2002
934
print("$row[8]\012");
May 19, 2002
May 19, 2002
935
936
937
938
939
940
941
942
943
return 1;
};
$commands{'QUEUE'} = sub {
my $args = shift;
if ((not defined $args) or ($args !~ /\A\d+\Z/)) {
report_error("Argument must be a number.");
} else {
May 19, 2002
May 19, 2002
944
945
946
my $err = change_queue($args);
if (defined $err) {
report_error($err);
May 19, 2002
May 19, 2002
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
} else {
report_success("Queue changed");
}
}
return(1);
};
$commands{'ENUM'} = sub {
my $args = shift;
my $type = undef;
my $link;
($type, $args) = $args =~ /\A(queues)\s*(.*)\Z/i if defined $args;
if (not defined $type) {
report_error("USAGE: ENUM <queues>");
return(1);
}
$link = get_database_link();
$type =~ tr/A-Z/a-z/;
if ($type eq "queues") {
my $sql = "select q.id, q.name, q.flags, r.rights, q.owner" .
" from $dbtable_queues as q" .
" left outer join $dbtable_queue_rights as r" .
" on r.qid=q.id and r.uid=$auth_uid";
my $sth = $link->prepare($sql);
$sth->execute() or report_fatal("can't execute query: $sth->errstr");
report_success("Here comes...");
while (my @row = $sth->fetchrow_array()) {
$row[3] = 0 if (not defined $row[3]);
next if (queue_is_forbidden($row[2], $row[3], $row[4]));
print("$row[0]\012");
print("$row[1]\012");
}
print(".\012");
$sth->finish();
}
return(1);
};
$commands{'DIGEST'} = sub {
Oct 18, 2002
Oct 18, 2002
995
996
997
998
999
1000
my $args = shift;
my $max = undef;
my $startIndex = undef;
if (defined $args) {
($startIndex, $max) = ($args =~ /\A(\d+|\-)\s*(\d+)\Z/);