summaryrefslogtreecommitdiff
path: root/Echolot/Pinger.pm
blob: 1f7173b7d66938ad4066fbb95a723a5df85bf263 (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
package Echolot::Pinger;

# (c) 2002 Peter Palfrader <peter@palfrader.org>
# $Id: Pinger.pm,v 1.28 2003/02/20 14:22:38 weasel Exp $
#

=pod

=head1 Name

Echolot::Pinger - actual sending and receiving of Pings.

=head1 DESCRIPTION

This package provides functions for sending out and receiving pings.

=cut

use strict;
use English;
use Echolot::Log;
use Echolot::Pinger::Mix;
use Echolot::Pinger::CPunk;

sub do_mix_ping($$$$$$) {
	my ($address, $type, $keyid, $to, $with_from, $body) = @_;

	($type eq 'mix') or
		Echolot::Log::warn("types should really be mix ($type)."),
		return 0;

	my %key = Echolot::Globals::get()->{'storage'}->get_key($address, $type, $keyid);
	Echolot::Pinger::Mix::ping(
		$body,
		$to,
		$with_from,
		[ $key{'nick'} ],
		{ $keyid => \%key } ) or
		return 0;

	return 1;
};

sub do_cpunk_ping($$$$$$) {
	my ($address, $type, $keyid, $to, $with_from, $body) = @_;

	my $keyhash = {};
	if ($type ne 'cpunk-clear') {
		my %key = Echolot::Globals::get()->{'storage'}->get_key($address, $type, $keyid);
		$keyhash->{$keyid} = \%key;
	};
	Echolot::Pinger::CPunk::ping(
		$body,
		$to,
		$with_from,
		[ { address    => $address,
		    keyid      => $keyid,
		    encrypt    => ($type ne 'cpunk-clear'),
		    pgp2compat => ($type eq 'cpunk-rsa') } ],
		$keyhash ) or
		return 0;

	return 1;
};

sub do_ping($$$$) {
	my ($type, $address, $key, $with_from) = @_;
	
	my $now = time();
	my $token = join(':', $address, $type, $key, $with_from, $now);
	my $mac = Echolot::Tools::make_mac($token);
	my $body = "remailer: $address\n".
		"type: $type\n".
		"key: $key\n".
		"with_from: $with_from\n".
		"sent: $now\n".
		"mac: $mac\n".
		Echolot::Tools::make_garbage();
	$body = Echolot::Tools::crypt_symmetrically($body, 'encrypt');
		
	my $to = Echolot::Tools::make_address('ping');
	if ($type eq 'mix') {
		do_mix_ping($address, $type, $key, $to, $with_from, $body);
	} elsif ($type eq 'cpunk-rsa' || $type eq 'cpunk-dsa' || $type eq 'cpunk-clear') {
		do_cpunk_ping($address, $type, $key, $to, $with_from, $body);
	} else {
		Echolot::Log::warn("Don't know how to handle ping type $type.");
		return 0;
	};

	Echolot::Globals::get()->{'storage'}->register_pingout($address, $type, $key, $now);
	return 1;
};

sub send_pings($;$) {
	my ($scheduled_for, $which) = @_;

	$which = '' unless defined $which;

	my $call_intervall = Echolot::Config::get()->{'pinger_interval'};
	my $send_every_n_calls = Echolot::Config::get()->{'ping_every_nth_time'};

	my $timemod = int ($scheduled_for / $call_intervall);
	my $this_call_id = $timemod % $send_every_n_calls;
	my $session_id = int ($scheduled_for / ($call_intervall * $send_every_n_calls));

	my @remailers = Echolot::Globals::get()->{'storage'}->get_addresses();
	for my $remailer (@remailers) {
		next unless $remailer->{'pingit'};
		my $address = $remailer->{'address'};

		next unless (
			$which eq 'all' ||
			$which eq $address ||
			$which eq '');

		for my $type (Echolot::Globals::get()->{'storage'}->get_types($address)) {
			next unless Echolot::Config::get()->{'do_pings'}->{$type};
			for my $key (Echolot::Globals::get()->{'storage'}->get_keys($address, $type)) {
				next unless (
					$which eq $address ||
					$which eq 'all' ||
					(($which eq '') && ($this_call_id eq (Echolot::Tools::makeShortNumHash($address.$type.$key.$session_id) % $send_every_n_calls))));

				my $with_from = (int($timemod / $send_every_n_calls)) % 2;
				Echolot::Log::debug("ping calling $type, $address, $key, $with_from.");
				do_ping($type, $address, $key, $with_from);
			}
		};
	};
	return 1;
};


sub receive($$$$) {
	my ($header, $msg, $token, $timestamp) = @_;

	my $now = time();

	my $body;
	my $bot = 0;
	my $top = 0;
	# < 2.0beta34 didn't encrypt pings.
	if ($msg =~ /^-----BEGIN PGP MESSAGE-----/m) {
		# work around borken middleman remailers that have a problem with some
		# sort of end of line characters and randhopping them through reliable
		# remailers..
		# they add an empty line between each usefull line
		$msg =~ s/(\r?\n)\r?\n/$1/g if ($msg =~ /^-----BEGIN PGP MESSAGE-----\r?\n\r?\n/m);

		$top = ($msg =~ m/^\S.*-----BEGIN PGP MESSAGE-----/ms) ? 1 : 0;
		$bot = ($msg =~ m/^-----END PGP MESSAGE-----.*\S/ms) ? 1 : 0;

		$body = Echolot::Tools::crypt_symmetrically($msg, 'decrypt');
	};
	$body = $msg unless defined $body;

	my ($addr) = $body =~ /^remailer: (.*)$/m;
	my ($type) = $body =~ /^type: (.*)$/m;
	my ($key) = $body =~ /^key: (.*)$/m;
	my ($sent) = $body =~ /^sent: (.*)$/m;
	my ($with_from) = $body =~ /^with_from: (.*)$/m;
	my ($mac) = $body =~ /^mac: (.*)$/m;

	my @values = ($addr, $type, $key, defined $with_from ? $with_from : 'undef', $sent, $mac); # undef was added after 2.0.10
	my $cleanstring = join ":", map { defined() ? $_ : "undef" } @values;
	my @values_obsolete = ($addr, $type, $key, $sent, $mac); # <= 2.0.10

	(grep { ! defined() } @values_obsolete) and
		Echolot::Log::warn("Received ping at $timestamp has undefined values: $cleanstring."),
		return 0;

	pop @values;
	pop @values_obsolete;
	Echolot::Tools::verify_mac(join(':', @values), $mac) or
		Echolot::Tools::verify_mac(join(':', @values_obsolete), $mac) or # old style without with_from
			Echolot::Log::warn("Received ping at $timestamp has wrong mac; $cleanstring."),
			return 0;

	Echolot::Globals::get()->{'storage'}->register_pingdone($addr, $type, $key, $sent, $now - $sent) or
		return 0;

	if (defined $with_from) { # <= 2.0.10 didn't have with_from
		my ($from) = $header =~ /From: (.*)/i;
		$from = 'undefined' unless defined $from;
		Echolot::Globals::get()->{'storage'}->register_fromline($addr, $type, $with_from, $from, $top, $bot);
	};

	return 1;
};

1;
# vim: set ts=4 shiftwidth=4: