summaryrefslogtreecommitdiff
path: root/pingd
blob: fcfa2d536417cd177b06f672770d476ba41a6846 (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
284
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#!/usr/bin/perl -wT

$| = 1;

# (c) 2002 Peter Palfrader <peter@palfrader.org>
# $Id: pingd,v 1.34 2002/07/11 17:45:59 weasel Exp $
#

=pod

=head1 NAME

pingd - echolot ping daemon

=head1 SYNOPSIS

=over

=item B<pingd> B<start>

=item B<pingd> B<stop>

=item B<pingd> B<process>

=item B<pingd> B<add> I<address> [I<address> ...]

=item B<pingd> B<delete> I<address> [I<address> ...]

=item B<pingd> B<set> option=value [option=value..] I<address> [I<address> ...]

=item B<pingd> B<setremailercaps> I<capsstring>

=item B<pingd> B<deleteremailercaps> I<address>

=item B<pingd> B<getkeyconf>

=item B<pingd> B<dumpconf>

=back

=head1 DESCRIPTION

pingd is a the heart of echolot. Echolot is a pinger for anonymous remailers. 

A Pinger in the context of anonymous remailers is a program that regularily
sends messages through remailers to check their reliability. It then calculates
reliability statistics which are used by remailer clients to choose the chain
of remailers to use.

Additionally it collects configuration parameters and keys of all remailers and
offers them in a format readable by remailer clients. 

When called without parameters pingd schedules tasks like sending pings,
processing incoming mail and requesting remailer-xxx data and runs them in
configurable intervalls.

=head1 COMMANDS

=over

=item B<start>

Start the ping daemon.

=item B<stop>

Send the running pingd process a SIGTERM.

=item B<process>

Sends a HUP signal to the daemon which instructs it to process the commands.

For other affects of sending the HUP Signal see the SIGNALS section below.

=item B<add> I<address> [I<address> ...]

Add I<address> to the list of remailers to query for
keys and confs.

=item B<delete> I<address> [I<address> ...]

Delete I<address> from the list of remailers to query for
keys and confs. Delete all statistics and keys for that remailer.

=item B<set> option=value [option=value..] I<address> [I<address> ...]

Possible options and values:

=over

=item B<showit=>{B<on>,B<off>}

Set B<showit> (show remailer in mlist, rlist etc.) for remailer I<address> to
either B<on> or B<off>.

=item B<pingit=>{B<on>,B<off>}

Set B<pingit> (send out pings to that remailer) for remailer I<address> to
either B<on> or B<off>.

=item B<fetch=>{B<on>,B<off>}

Set B<fetch> (fetch remailer-key and remailer-conf) for remailer I<address> to
either B<on> or B<off>.

=back

=item B<setremailercaps> I<capsstring>

Some remailers (Mixmaster V2 - currently lcs and passthru2) don't return a
useable remailer-conf message. For such remailers you need to set it manually.

For instance:

    ./pingd setremailercaps '$remailer{"passthru2"} = "<mixer@immd1.informatik.uni-erlangen.de> mix middle";'
    ./pingd setremailercaps '$remailer{"lcs"} = "<mix@anon.lcs.mit.edu> mix klen1000";'

=item B<deleteremailercaps> I<address>

Delete remailer-conf data for I<address>. The config data will be reset from
the next valid remailer-conf reply by the remailer.

=item B<getkeyconf>

Send a command to immediatly request keys and configuration from remailers.

=item B<dumpconf>

Dumps the current configuration to standard output.

=back

=head1 OPTIONS

=over

=item --verbose

Verbose mode. Causes B<pingd> to print debugging messages about its progress.

=item --help

Print a short help and exit sucessfully.

=item --version

Print version number and exit sucessfully.

=item --nohup

Usefull only with the B<add>, B<set>, B<setremailercaps>,
B<deleteremailercaps> or B<getkeyconf> command.

Don't send a HUP signal to the daemon which instructs it to process the
commands after adding the command to the task list.

Per default such a signal is sent.

=item --process

Usefull only with the B<start> command.

Read and process the commands file on startup.

=item --detach

Usefull only with the B<start> command.

Tell B<pingd> to detach.

=back

=head1 FILES

The configuration file is searched in those places in that order:

=over   

=item the file pointed to by the B<ECHOLOT_CONF> environment variable

=item `pwd`/pingd.conf

=item $HOME/echolot/pingd.conf

=item $HOME/pingd.conf

=item $HOME/.pingd.conf

=item /etc/pingd.conf

=back

=head1 SIGNALS

On B<SIGINT>, B<SIGQUIT>, and B<SIGTERM> B<pingd> will schedule a shutdown
for as soon as the current actions are finished or immediatly if no actions are
currently beeing processed. It will then write all metadata and pingdata to
disk and close all files cleanly before exiting.

On B<SIGHUP> <pingd> will execute any pending commands from the commands file
(B<commands.txt> per default).  It also closes and reopens the file 'output'
which is used for stdout and stderr in case the daemon was told to detach.
This can be used if you want to rotate that file.

=head1 AUTHOR

Peter Palfrader E<lt>peter@palfrader.orgE<gt>

=head1 BUGS

Please report them at E<lt>URL:http://savannah.gnu.org/bugs/?group=echolotE<gt>

=cut

use strict;
use Getopt::Long;
use English;
use Carp;
use lib qw{ . lib };
use Echolot::Config;
use Echolot::Globals;
use Echolot::Storage::File;
use Echolot::Scheduler;
use Echolot::Conf;
use Echolot::Mailin;
use Echolot::Pinger;
use Echolot::Stats;
use Echolot::Commands;
use Echolot::Thesaurus;

delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};


my $VERSION = '2.0beta8';


my $redirected_stdio = 0;

sub setSigHandlers() {
	$SIG{'HUP'} = sub {
		print "Got SIGHUP. scheduling readcommands\n";
		Echolot::Globals::get()->{'scheduler'}->schedule('readcommands', time() );
		if ($redirected_stdio) {
			close STDOUT;
			close STDERR;
			open (STDOUT, ">>output") or die ("Cannot open 'output' as STDOUT\n");
			open (STDERR, ">&STDOUT") or die ("Cannot dup STDOUT as STDERR\n");
		};
	};
	$SIG{'INT'} = sub {
		print "Got SIGINT. scheduling exit\n";
		Echolot::Globals::get()->{'scheduler'}->schedule('exit', time() );
	};
	$SIG{'QUIT'} = sub {
		print "Got SIGQUIT. scheduling exit\n";
		Echolot::Globals::get()->{'scheduler'}->schedule('exit', time() );
	};
	$SIG{'TERM'} = sub {
		print "Got SIGTERM. scheduling exit\n";
		Echolot::Globals::get()->{'scheduler'}->schedule('exit', time() );
	};
};



sub commit_prospective_address() {
	Echolot::Globals::get()->{'storage'}->commit_prospective_address();
};
sub expire() {
	Echolot::Globals::get()->{'storage'}->expire();
};





sub command_adddelete(@) {
	my $command = shift @_;
	my @argv = @_;

	die ("command_adddelete requires command\n") unless defined $command;
	die ("add requires argument <address>\n") unless scalar @argv;
	my @addresses;
	for my $address (@argv) {
		die ("argument <address> is not a valid email address\n") unless ($address =~ /^[a-zA-Z0-9+._-]+\@[a-zA-Z0-9+.-]+$/ );
		push @addresses, $address;
	};
	for my $address (@addresses) {
		Echolot::Commands::addCommand("$command $address");
	};
};

sub command_set(@) {
	my @argv = @_;

	my @settings;
	while (scalar @argv && $argv[0] =~ /^(showit|pingit|fetch)=(on|off)$/) {
		push @settings, $argv[0];
		shift @argv;
	};

	my @addresses;
	for my $address (@argv) {
		die ("argument $address is not a valid email address\n") unless ($address =~ /^[a-zA-Z0-9+._-]+\@[a-zA-Z0-9+.-]+$/ );
		push @addresses, $address;
	};

	for my $address (@argv) {
		for my $setting (@settings) {
			Echolot::Commands::addCommand("set $address $setting");
		};
	};
};

sub command_setremailercaps(@) {
	my @argv = @_;

	my @caps;
	for my $caps (@argv) {
		my ($remailer_nick, $remailer_address) = ($caps =~ /^\s*  \$remailer{"(.*)"}  \s*=\s*  "<(.*@.*)>.*";   \s*$/ix);
		die ("caps '$caps' is not a valid remailer caps line\n") unless (defined $remailer_nick && defined $remailer_address);
		push @caps, {
			address => $remailer_address,
			caps => $caps };
	};
	for my $caps (@caps) {
		Echolot::Commands::addCommand("setremailercaps ".$caps->{'address'}." ".$caps->{'caps'});
	};
};

sub command_deleteremailercaps(@) {
	my @argv = @_;

	my @addresses;
	for my $address (@argv) {
		die ("argument $address is not a valid email address\n") unless ($address =~ /^[a-zA-Z0-9+._-]+\@[a-zA-Z0-9+.-]+$/ );
		push @addresses, $address;
	};

	for my $address (@argv) {
		Echolot::Commands::addCommand("deleteremailercaps $address");
	};
};


sub pid_exists() {
	return (-e Echolot::Config::get()->{'pidfile'});
};

sub daemon_run($) {
	my ($process) = @_;

	die ("Pidfile '".Echolot::Config::get()->{'pidfile'}."' exists\n")
		if pid_exists();
	open (PIDFILE, '>'.Echolot::Config::get()->{'pidfile'}) or
		confess ("Cannot open pidfile '".Echolot::Config::get()->{'pidfile'}."': $!\n");
	print PIDFILE "$PROCESS_ID ".Echolot::Globals::get()->{'hostname'}." ".time()."\n";
	close PIDFILE;

	Echolot::Globals::initStorage();
	setSigHandlers();

	Echolot::Globals::get()->{'scheduler'} = new Echolot::Scheduler;
	my $scheduler = Echolot::Globals::get()->{'scheduler'};
	$scheduler->add('exit'                ,     -1                                         , 0, 'exit' );
	$scheduler->add('readcommands'        ,     -1                                         , 0, \&Echolot::Commands::processCommands );

	$scheduler->add('processmail'         , Echolot::Config::get()->{'processmail'}        , 0, \&Echolot::Mailin::process );
	$scheduler->add('ping'                , Echolot::Config::get()->{'pinger_interval'}    , 0, \&Echolot::Pinger::send_pings );
	$scheduler->add('buildstats'          , Echolot::Config::get()->{'buildstats'}         , 0, \&Echolot::Stats::build_stats );
	$scheduler->add('buildkeys'           , Echolot::Config::get()->{'buildkeys'}          , 0, \&Echolot::Stats::build_keys );
	$scheduler->add('buildthesaurus'      , Echolot::Config::get()->{'build_thesaurus'}    , 0, \&Echolot::Thesaurus::build_thesaurus );

	$scheduler->add('commitprospectives'  , Echolot::Config::get()->{'commitprospectives'} , 0, \&commit_prospective_address );
	$scheduler->add('expire'              , Echolot::Config::get()->{'expire'}             , 0, \&expire );
	$scheduler->add('getkeyconf'          , Echolot::Config::get()->{'getkeyconf'}         , 0, \&Echolot::Conf::send_requests );
	$scheduler->add('check_resurrection'  , Echolot::Config::get()->{'check_resurrection'} , 0, \&Echolot::Conf::check_resurrection );

	Echolot::Globals::get()->{'scheduler'}->schedule('readcommands', time() )
		if ($process);

	$scheduler->run();

	Echolot::Globals::get()->{'storage'}->commit();
	Echolot::Globals::get()->{'storage'}->finish();

	unlink (Echolot::Config::get()->{'pidfile'}) or
		cluck ("Cannot unlink pidfile ".Echolot::Config::get()->{'pidfile'});
};

sub send_sig($) {
	my ($sig) = @_;

	die ("Pidfile '".Echolot::Config::get()->{'pidfile'}."' does not exist\n")
		unless pid_exists();
	open (PIDFILE, '<'.Echolot::Config::get()->{'pidfile'}) or
		confess ("Cannot open pidfile '".Echolot::Config::get()->{'pidfile'}."': $!\n");
	my $line = <PIDFILE>;
	close PIDFILE;

	my ($pid, $host, $time) = $line =~ /^(\d+) \s+ (\S+) \s+ (\d+) \s* $/x or
		confess ("Cannot parse pidfile '$line'\n");
	my $sent = kill $sig, $pid;
	($sent == 1) or
		confess ("Did not send signal $sig to exactly one process but $sent. (pidfile reads $line)\n");
};

sub daemon_hup() {
	send_sig(1);
};

sub daemon_stop() {
	send_sig(15);
};

sub make_dirs() {
	for my $dir (
		Echolot::Config::get()->{'resultdir'},
		Echolot::Config::get()->{'thesaurusdir'},
		Echolot::Config::get()->{'private_resultdir'},
		Echolot::Config::get()->{'gnupghome'},
		Echolot::Config::get()->{'tmpdir'},
		Echolot::Config::get()->{'storage'}->{'File'}->{'basedir'}
	) {
		if ( ! -d $dir ) {
			mkdir ($dir) or
				confess ("Cannot create directory $dir: $!\n");
		};
	};
	for my $dir (
		Echolot::Config::get()->{'mailindir'},
		Echolot::Config::get()->{'mailindir'}.'/cur',
		Echolot::Config::get()->{'mailindir'}.'/tmp',
		Echolot::Config::get()->{'mailindir'}.'/new',
		Echolot::Config::get()->{'mailerrordir'},
		Echolot::Config::get()->{'mailerrordir'}.'/cur',
		Echolot::Config::get()->{'mailerrordir'}.'/tmp',
		Echolot::Config::get()->{'mailerrordir'}.'/new'
	) {
		if ( ! -d $dir ) {
			mkdir ($dir, 0700) or
				confess ("Cannot create directory $dir: $!\n");
		};
	};
};












my $params;
Getopt::Long::config('bundling');
if (!GetOptions (
	'help'    =>  \$params->{'help'},
	'version' =>  \$params->{'version'},
	'verbose' =>  \$params->{'verbose'},
	'nohup'   =>  \$params->{'nohup'},
	'detach'  =>  \$params->{'detach'},
	'process' =>  \$params->{'process'},
	)) {
	die ("$PROGRAM_NAME: Usage: $PROGRAM_NAME [-fwhv]\n");
};
if ($params->{'help'}) {
    print ("Usage: $PROGRAM_NAME [options] command\n");
    print ("See man pingd or perldoc pingd for more info.\n");
    print ("echolot $VERSION - (c) 2002 Peter Palfrader <peter\@palfrader.org>\n");
    print ("http://savannah.gnu.org/projects/echolot/\n");
    exit 0;
};
if ($params->{'version'}) {
    print ("echolot $VERSION\n");
    print ("(c) 2002 Peter Palfrader <peter\@palfrader.org>\n");
    print ("http://savannah.gnu.org/projects/echolot/\n");
    exit 0;
};

my $COMMAND = shift @ARGV;
die ("command required\n") unless defined $COMMAND;


Echolot::Config::init( $params );
chdir( Echolot::Config::get()->{'homedir'} );
Echolot::Globals::init();


if ($COMMAND eq 'add' || $COMMAND eq 'delete') {
	command_adddelete($COMMAND, @ARGV);
	if (!$params->{'nohup'} && pid_exists()) {
		daemon_hup()
	} else {
		print "Don't forget to run $PROGRAM_NAME process.\n";
	};
} elsif ($COMMAND eq 'set') {
	command_set(@ARGV);
	if (!$params->{'nohup'} && pid_exists()) {
		daemon_hup()
	} else {
		print "Don't forget to run $PROGRAM_NAME process.\n";
	};
} elsif ($COMMAND eq 'setremailercaps') {
	command_setremailercaps(@ARGV);
	if (!$params->{'nohup'} && pid_exists()) {
		daemon_hup()
	} else {
		print "Don't forget to run $PROGRAM_NAME process.\n";
	};
} elsif ($COMMAND eq 'deleteremailercaps') {
	command_deleteremailercaps(@ARGV);
	if (!$params->{'nohup'} && pid_exists()) {
		daemon_hup()
	} else {
		print "Don't forget to run $PROGRAM_NAME process.\n";
	};
} elsif ($COMMAND eq 'getkeyconf') {
	Echolot::Commands::addCommand("getkeyconf");
	if (!$params->{'nohup'} && pid_exists()) {
		daemon_hup()
	} else {
		print "Don't forget to run $PROGRAM_NAME process.\n";
	};
} elsif ($COMMAND eq 'process') {
	daemon_hup();
} elsif ($COMMAND eq 'stop') {
	daemon_stop();
} elsif ($COMMAND eq 'start') {
	die ("Pidfile '".Echolot::Config::get()->{'pidfile'}."' exists\n")
		if pid_exists();
	make_dirs();
	if ($params->{'detach'}) {
		print "Detaching.\n";
		unless (fork()) {
			close STDOUT;
			close STDERR;
			open (STDOUT, ">>output") or die ("Cannot open 'output' as STDOUT\n");
			open (STDERR, ">&STDOUT") or die ("Cannot dup STDOUT as STDERR\n");
			close STDIN;
			$redirected_stdio = 1;
			print "Startup at ".scalar localtime().".\n";
			daemon_run( $params->{'process'} );
			print "done at ".scalar localtime().".\n";
		};
	} else {
		daemon_run( $params->{'process'} );
	};
} elsif ($COMMAND eq 'dumpconf') {
	Echolot::Config::dump();
} elsif ($COMMAND eq 'convert') {
	Echolot::Globals::initStorage();
	setSigHandlers();

	Echolot::Globals::get()->{'storage'}->convert();

	Echolot::Globals::get()->{'storage'}->commit();
	Echolot::Globals::get()->{'storage'}->finish();
} else {
	die ("Command $COMMAND unknown");
};

exit 0;

# vim: set ts=4 shiftwidth=4: