Implement a progressive iauth system.
[ircu2.10.12-pk.git] / tools / iauth-test
1 #! /usr/bin/perl
2 # iauth-test: test script for IRC authorization (iauth) protocol
3 # Copyright 2006 Michael Poole
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation.
8
9 require 5.008; # We assume deferred signal handlers, new in 5.008.
10 use strict;
11 use warnings;
12 use vars qw(%pending);
13
14 use Carp;       # for carp
15 use Config;     # for $Config{sig_name} and $Config{sig_num}
16 use FileHandle; # for autoflush method on file handles
17
18 # This script is intended to help test an implementation of the iauth
19 # protocol by exercising every command in the protocol and by
20 # exercising most distinct combinations of commands.  It assumes IPv4
21 # support in the server and POSIX real-time signal support in the OS
22 # (recognized and supported by Perl).
23
24 # Certain behavior is triggered by receipt of real-time signals.
25 # SIGRTMIN + 0 -> Send server notice ('>').
26 # SIGRTMIN + 1 -> Toggle debug level ('G').
27 # SIGRTMIN + 2 -> Set policy options ('O').
28 # SIGRTMIN + 3 -> Simulate config change ('a', 'A').
29 # SIGRTMIN + 4 -> Simulate statistics change ('s', 'S').
30
31 # In the following discussion, sX means message X from the server, and
32 # iX means message X from iauth.  The hard part is the ordering of
33 # various events during client registration.  This includes sC, sP,
34 # sU, su, sn, sN/d, sH and sT; and o/U/u, iN, iI, iC and iD/R/k/K.
35
36 # sC is first, sD/sT/iD/R/k/K is last.  If sH is sent, no more sU, su,
37 # sn, sN, sd or sH messages may be sent.  If iI is sent, iN should
38 # also be sent (either before or after iI).  Multiple sP, sU and iC
39 # messages may be sent. Otherwse, the ordering of unrelated messages
40 # from either source are not constrained, but only one message from
41 # each set of alternatives may be sent.
42
43 # This means the sets of commands with interesting orderings are:
44 # sU, su, io/U/u
45 # sN/d, iN, iI
46 # sH, sT or iD/R/k/K
47
48 # 127.x.y.z IP addresses are used to exercise these orderings; see the
49 # %handlers variable below.
50
51 sub dolog ($) {
52     print LOG "$_[0]\n";
53 }
54
55 sub reply ($;$$) {
56     my ($msg, $client, $extra) = @_;
57
58     if (not defined $msg) {
59         # Accept this for easier handling of client reply messages.
60         return;
61     } elsif (ref $msg eq '') {
62         $msg =~ s/^(.) ?/$1 $client->{id} $client->{ip} $client->{port} / if $client;
63         dolog "< $msg";
64         print "$msg\n";
65     } elsif (ref $msg eq 'ARRAY') {
66         grep { reply($_, $client, $extra); } @$msg;
67     } elsif (ref $msg eq 'CODE') {
68         &$msg($client, $extra);
69     } else {
70         die "Unknown reply message type.";
71     }
72 }
73
74 # Find the names of signals with values SIGRTMIN+1, +2, etc.
75 BEGIN {
76     my @sig_name;
77     my %sig_num;
78
79     sub populate_signals () {
80         die "No sigs?"
81             unless $Config{sig_name} and $Config{sig_num};
82         my @names = split ' ', $Config{sig_name};
83         @sig_num{@names} = split ' ', $Config{sig_num};
84         foreach (@names) { $sig_name[$sig_num{$_}] ||= $_; }
85     }
86
87     sub assign_signal_handlers() {
88         my $sigrtmin = $sig_num{RTMIN};
89         die "No realtime signals?"
90             unless $sigrtmin;
91         $SIG{$sig_name[$sigrtmin+0]} = \&send_server_notice;
92         $SIG{$sig_name[$sigrtmin+1]} = \&toggle_debug_level;
93         $SIG{$sig_name[$sigrtmin+2]} = \&set_policy_options;
94         $SIG{$sig_name[$sigrtmin+3]} = \&sim_config_changed;
95         $SIG{$sig_name[$sigrtmin+4]} = \&sim_stats_change;
96     }
97 }
98
99 BEGIN {
100     my $debug_level = 0;
101     my $max_debug_level = 2;
102
103     sub toggle_debug_level () {
104         if (++$debug_level > $max_debug_level) {
105             $debug_level = 0;
106         }
107         reply "G $debug_level";
108     }
109 }
110
111 BEGIN {
112     my %rotation = (
113         '' => 'AU',
114         'AU' => 'AURTW',
115         'AURTW' => '',
116     );
117     my $policy = '';
118
119     sub set_policy_options () {
120         $policy = $rotation{$policy};
121         reply "O $policy";
122     }
123 }
124
125 BEGIN {
126     my $generation = 0;
127
128     sub sim_config_changed () {
129         reply "a";
130         reply "A config $generation";
131         $generation++;
132     }
133 }
134
135 BEGIN {
136     my $generation = 0;
137
138     sub sim_stats_change () {
139         reply "s";
140         reply "S stats $generation";
141         $generation++;
142     }
143 }
144
145 sub send_server_notice () {
146     reply "> Hello the server!";
147 }
148
149 my %handlers = (
150                 # Default handliner: immediately report done.
151                 'default'    => { C_reply => 'D' },
152                 # 127.0.0.x: various timings for iD/iR/ik/iK.
153                 '127.0.0.1'  => { C_reply => 'D' },
154                 '127.0.0.2'  => { C_reply => 'R account-1' },
155                 '127.0.0.3'  => { C_reply => 'k' },
156                 '127.0.0.4'  => { C_reply => 'K' },
157                 '127.0.0.15' => { },
158                 '127.0.0.16' => { H_reply => 'D' },
159                 '127.0.0.17' => { H_reply => 'R account-2' },
160                 '127.0.0.18' => { H_reply => 'k' },
161                 '127.0.0.19' => { H_reply => 'K' },
162                 '127.0.0.32' => { T_reply => 'D' },
163                 '127.0.0.33' => { T_reply => 'R account-3' },
164                 '127.0.0.34' => { T_reply => 'k' },
165                 '127.0.0.35' => { T_reply => 'K' },
166                 # 127.0.1.x: io/iU/iu functionality.
167                 '127.0.1.0'  => { C_reply => 'o forced',
168                                   H_reply => 'D' },
169                 '127.0.1.1'  => { C_reply => 'U trusted',
170                                   H_reply => 'D' },
171                 '127.0.1.2'  => { C_reply => 'u untrusted',
172                                   H_reply => 'D' },
173                 # 127.0.2.x: iI/iN functionality.
174                 '127.0.2.0'  => { C_reply => 'N iauth.assigned.host',
175                                   H_reply => 'D' },
176                 '127.0.2.1'  => { C_reply => \&ip_change },
177                 # 127.0.3.x: iC/sP functionality.
178                 '127.0.3.0'  => { C_reply => 'C Please enter the password.',
179                                   P_reply => \&passwd_check },
180 );
181
182 sub handle_new_client ($$$$) {
183     my ($id, $ip, $port, $extra) = @_;
184     my $handler = $handlers{$ip} || $handlers{default};
185     my $client = { id => $id, ip => $ip, port => $port, handler => $handler };
186
187     # If we have any deferred reply handlers, we must save the client.
188     $pending{$id} = $client if grep /^[^C]_reply$/, keys %$handler;
189     reply $client->{handler}->{C_reply}, $client, $extra;
190 }
191
192 sub ip_change ($$) {
193     my ($client, $extra) = @_;
194     reply 'I 127.255.255.254', $client;
195     $client->{ip} = '127.255.255.254';
196     reply 'N other.assigned.host', $client;
197     reply 'D', $client;
198 }
199
200 sub passwd_check ($$) {
201     my ($client, $extra) = @_;
202     if ($extra eq 'secret') {
203         reply 'D', $client;
204     } else {
205         reply 'C :Bad password', $client;
206     }
207 }
208
209 open LOG, ">> iauth.log";
210 populate_signals();
211 assign_signal_handlers();
212 autoflush LOG 1;
213 autoflush STDOUT 1;
214 autoflush STDERR 1;
215 dolog "IAuth starting " . scalar(localtime(time));
216
217 while (<>) {
218     my ($id, $client);
219
220     # Chomp newline and log incoming message.
221     s/\r?\n?\r?$//;
222     dolog "> $_";
223
224     # If there's an ID at the start of the line, parse it out.
225     if (s/^(\d+) //) { $id = $1; $client = $pending{$id}; }
226
227     # Figure out how to handle the command.
228     if (/^C (\S+) (\S+) (.+)$/) {
229         handle_new_client($id, $1, $2, $3);
230     } elsif (/^([DT])/ and $client) {
231         reply $client->{handler}->{"${1}_reply"}, $client;
232         delete $pending{$id};
233     } elsif (/^([d])/ and $client) {
234         reply $client->{handler}->{"${1}_reply"}, $client;
235     } elsif (/^([HNPUu]) (.+)/ and $client) {
236         reply $client->{handler}->{"${1}_reply"}, $client, $2;
237     }
238 }