Debian Codegraph @

debian/tests/test-s2s-tls

raw
1 #!/usr/bin/perl
2
3 #%# Copyright (C) 2014-2024 Christoph Biedl <[email protected]>
4 #%# License: GPL-2.0+
5
6 =head1 NAME
7
8 ngircd-test-tls-link - test certificate validation in ngircd's TLS based s2s links
9
10 =head1 VERSION
11
12 Version YYYY.MM.DD
13
14 =cut
15
16 our $VERSION = 'YYYY.MM.DD';
17
18 =head1 SYNOPSIS
19
20 ngircd-test-tls-link --x509-dir /path/to/dir --x509-populate
21
22 ngircd-test-tls-link --x509-dir /path/to/dir [<test-name> ...]
23
24 =cut
25
26 use 5.010;
27 use strict;
28 use warnings;
29
30 use Test::More;
31
32 use File::Basename;
33 use File::Copy;
34 use File::Slurp;
35 use File::Temp qw<tempdir>;
36 use Getopt::Long;
37 use Pod::Usage;
38 use Proc::Simple;
39 use Socket;
40 use Time::HiRes qw<sleep time>;
41
42 my $ngircd_exe_default = '/usr/sbin/ngircd';
43 my @ngircd_exe = ( $ngircd_exe_default );
44 my @tls_mode;
45 my $x509_dir;
46 my $x509_populate;
47 my $verbose = 0;
48
49 =head1 OPTIONS
50
51 =over
52
53 =cut
54
55 {
56 my $help;
57 my $man;
58 my %GetOptions = (
59 'help|?' => \$help,
60 'man' => \$man,
61 );
62
63 =item B<--x509-dir> F<directory>
64
65 A directory to hold the x509 credentials needed. Default: Use a
66 temporary directory which is deleted upon exit. This delays every
67 execution of this program by a few seconds.
68
69 =cut
70
71 $GetOptions{'x509-dir=s'} = \$x509_dir;
72
73 =item B<--x509-populate>
74
75 Populate a given C<--x509-dir> with the files needed, then exit.
76
77 =cut
78
79 $GetOptions{'x509-populate'} = \$x509_populate;
80
81 =item B<--tls-mode> C<mode>
82
83 One of C<openssl> or C<gnutls>.
84
85 The type of TLS configuration to use. Must match ngircd's compilation
86 option. Use only if auto-detection failed, and report a bug then.
87
88 For TLS interoperability tests, you can provide two strings, separated
89 by C<:> (colon).
90
91 Default: Detect from the binary.
92
93 =cut
94
95 $GetOptions{'tls-mode=s'} = sub {
96 @tls_mode = split (/:/, $_[1], 2);
97 };
98
99 =item B<--ngircd> F<program>
100
101 The ngircd binary. For TLS interoperability tests, you can provide two
102 programs, separated by C<:> (colon).
103
104 Default: F</usr/sbin/ngircd>
105
106 =cut
107
108 $GetOptions{'ngircd=s'} = sub {
109 @ngircd_exe = split (/:/, $_[1], 2);
110 };
111
112 =item B<--verbose>
113
114 Add verbosity.
115
116 =cut
117
118 $GetOptions{'verbose+'} = \$verbose++;
119
120 =item B<--Version>
121
122 Show the version number and exit.
123
124 =cut
125
126 $GetOptions{'Version'} = sub {
127 printf "%s version %s\n",
128 (split (/\//, $0))[-1],
129 $VERSION;
130 exit 1;
131 };
132
133 =item [ <test> ... ]
134
135 Select specific tests to run only. Default: Run all tests.
136
137 =back
138
139 =cut
140
141 GetOptions (%GetOptions) or pod2usage (2);
142 $help and pod2usage (1);
143 $man and pod2usage (-exitstatus => 0, -verbose => 2);
144
145 if (@tls_mode) {
146 foreach my $m (@tls_mode) {
147 ($m =~ /^(openssl|gnutls)$/) or
148 die ("Invalid '--tls-mode' value '$m'");
149 }
150 } else {
151 for (my $i = 0; $i < scalar (@ngircd_exe); $i++) {
152 my $x = $ngircd_exe[$i];
153 my $tls_mode;
154 (-x $x) or
155 die ("Not an executable: '$x'");
156 my $pipe;
157 open ($pipe, '-|', 'ldd', $x) or
158 die ("Cannot run ldd on '$x': $!");
159 while (defined (my $line = <$pipe>)) {
160 if ($line =~ /^\tlibssl\.so\./) {
161 $tls_mode = 'openssl';
162 last;
163 } elsif ($line =~ /^\tlibgnutls/) {
164 $tls_mode = 'gnutls';
165 last;
166 }
167 }
168 close ($pipe);
169 if ($tls_mode) {
170 note ("Auto-detected TLS mode for '$x' is '$tls_mode'");
171 $tls_mode[$i] = $tls_mode;
172 } else {
173 die ("BUG: Failed to determine TLS mode for '$x'");
174 }
175 }
176 }
177 $ngircd_exe[1] //= $ngircd_exe[0];
178 $tls_mode[1] //= $tls_mode[0];
179 }
180
181
182 my $temp_dir = tempdir (
183 "ngircd-test-tls-link.$$.XXXXX",
184 'TMPDIR' => 1,
185 'CLEANUP' => 1,
186 );
187
188 my $exit_after_x509_populate;
189 if ($x509_dir) {
190 (-d $x509_dir || $x509_populate) or
191 die ("Not a directory: '$x509_dir' - perhaps you forgot --x509-populate?");
192 $exit_after_x509_populate = $x509_populate;
193 } else {
194 # not provided, use a temporary directoryy
195 $x509_dir = "$temp_dir/CA";
196 # always populate it, and continue
197 $x509_populate and
198 note ('Ignoring --x509-populate option as no --x509-dir was provided');
199 $x509_populate = 1;
200 $exit_after_x509_populate = undef;
201 }
202 my $conf_dir = "$temp_dir/conf";
203 my $hosts_file ="$temp_dir/hosts";
204
205 write_file (
206 $hosts_file,
207 <<__EOS__,
208 127.0.0.1 server1
209 127.0.0.1 server2
210 127.0.0.1 server1.example.com
211 127.0.0.1 server2.example.com
212 __EOS__
213 );
214
215 foreach my $dir (($x509_dir, $conf_dir)) {
216 (
217 -d $dir ||
218 mkdir ($dir)
219 ) or die ("Cannot create directory '$dir'");
220 }
221
222 my $src_dir = dirname ($0);
223
224 my $one_month_future = $^T + 30*86400;
225
226 my %tests = (
227 'regular' => {
228 'expect' => 'pass',
229 'fail' => qr/Connection [0-9]+ with "server2:6692" closed/,
230 ':order' => __LINE__,
231 },
232
233 'different-CAs' => {
234 'expect' => 'pass',
235 'server1-extra' => {
236 'SSL' => {
237 'CAFile' => "$x509_dir/CA2/root-ca.crt",
238 },
239 },
240 'server2-extra' => {
241 'SSL' => {
242 'CertFile' => "$x509_dir/CA2/server2.example.com.crt",
243 'KeyFile' => "$x509_dir/CA2/server2.example.com.key",
244 },
245 },
246 ':order' => __LINE__,
247 },
248
249 'CN-mismatch' => {
250 # using certificate with wrong CN, must fail
251 'expect' => 'fail',
252 'server2-extra' => {
253 'SSL' => {
254 'CertFile' => "$x509_dir/CA1/server3.example.com.crt",
255 'KeyFile' => "$x509_dir/CA1/server3.example.com.key",
256 },
257 },
258 'pass' => qr/Peer certificate check failed for/,
259 ':order' => __LINE__,
260 },
261
262 'using-peer-cert-as-CA' => {
263 # using peer cert as CA a.k.a. self-signed
264 'unsupported' => 1, # fails on openssl
265 'expect' => 'pass',
266 'server1-extra' => {
267 'SSL' => {
268 'CAFile' => "$x509_dir/CA1/server2.example.com.crt",
269 },
270 },
271 ':order' => __LINE__,
272 },
273
274 'peer-cert-signed-by-unknown-CA' => {
275 # peer cert signed by unknown CA, must fail
276 'expect' => 'fail',
277 'server2-extra' => {
278 'SSL' => {
279 'CertFile' => "$x509_dir/CA2/server2.example.com.crt",
280 'KeyFile' => "$x509_dir/CA2/server2.example.com.key",
281 },
282 },
283 ':order' => __LINE__,
284 },
285
286 'peer-cert-signed-by-unknown-CA-but-verify-disabled' => {
287 'expect' => 'pass',
288 'server1-extra' => {
289 'Server' => {
290 'SSLVerify' => 'no',
291 }
292 },
293 'server2-extra' => {
294 'SSL' => {
295 'CertFile' => "$x509_dir/CA2/server2.example.com.crt",
296 'KeyFile' => "$x509_dir/CA2/server2.example.com.key",
297 },
298 },
299 'pass' => qr/Synchronization with "ngircd\.test\.server2" done/,
300 'noop' => qr/Certificate validation failed/,
301 ':order' => __LINE__,
302 },
303
304 'peer-cert-revoked' => {
305 # certificate is revoked, must fail
306 'expect' => 'fail',
307 'server1-extra' => {
308 'SSL' => {
309 'CAFile' => "$x509_dir/CA3/root-ca.crt",
310 'CertFile' => "$x509_dir/CA3/server1.example.com.crt",
311 'KeyFile' => "$x509_dir/CA3/server1.example.com.key",
312 'CRLFile' => "$x509_dir/CA3/crl.pem",
313 },
314 },
315 'server2-extra' => {
316 'SSL' => {
317 'CAFile' => "$x509_dir/CA3/root-ca.crt",
318 'CertFile' => "$x509_dir/CA3/server2.example.com.crt",
319 'KeyFile' => "$x509_dir/CA3/server2.example.com.key",
320 },
321 },
322 ':order' => __LINE__,
323 },
324
325 'peer-cert-revoked-but-verify-disabled' => {
326 'expect' => 'pass',
327 'server1-extra' => {
328 'SSL' => {
329 'CAFile' => "$x509_dir/CA3/root-ca.crt",
330 'CertFile' => "$x509_dir/CA3/server1.example.com.crt",
331 'KeyFile' => "$x509_dir/CA3/server1.example.com.key",
332 'CRLFile' => "$x509_dir/CA3/crl.pem",
333 },
334 'Server' => {
335 'SSLVerify' => 'no',
336 }
337 },
338 'server2-extra' => {
339 'SSL' => {
340 'CAFile' => "$x509_dir/CA3/root-ca.crt",
341 'CertFile' => "$x509_dir/CA3/server2.example.com.crt",
342 'KeyFile' => "$x509_dir/CA3/server2.example.com.key",
343 },
344 },
345 'pass' => qr/Synchronization with "ngircd\.test\.server2" done/,
346 'noop' => qr/Certificate validation failed/,
347 ':order' => __LINE__,
348 },
349
350 'revocation-by-wrong-CA' => {
351 # CRL signed by a different CA
352 'expect' => 'fail',
353 'server1-extra' => {
354 'SSL' => {
355 'CRLFile' => "$x509_dir/CA2/crl.pem",
356 },
357 },
358 ':order' => __LINE__,
359 },
360
361 'wildcard-cert' => {
362 # server uses a wildcard certificate
363 'expect' => 'pass',
364 'server1-extra' => {
365 'SSL' => {
366 'CAFile' => "$x509_dir/CA4/root-ca.crt",
367 'CertFile' => "$x509_dir/CA4/server1.example.com.crt",
368 'KeyFile' => "$x509_dir/CA4/server1.example.com.key",
369 },
370 },
371 'server2-extra' => {
372 'SSL' => {
373 'CAFile' => "$x509_dir/CA4/root-ca.crt",
374 'CertFile' => "$x509_dir/CA4/*.example.com.crt",
375 'KeyFile' => "$x509_dir/CA4/*.example.com.key",
376 },
377 },
378 ':order' => __LINE__,
379 },
380
381 'mixed-case' => {
382 # uppercase letters in certificate name
383 'expect' => 'pass',
384 'server1-extra' => {
385 'SSL' => {
386 'CAFile' => "$x509_dir/CA5/root-ca.crt",
387 'CertFile' => "$x509_dir/CA5/server1.example.com.crt",
388 'KeyFile' => "$x509_dir/CA5/server1.example.com.key",
389 },
390 },
391 'server2-extra' => {
392 'SSL' => {
393 'CAFile' => "$x509_dir/CA5/root-ca.crt",
394 'CertFile' => "$x509_dir/CA5/Server2.example.com.crt",
395 'KeyFile' => "$x509_dir/CA5/Server2.example.com.key",
396 },
397 },
398 ':order' => __LINE__,
399 },
400
401 'subject-alternate-name' => {
402 'expect' => 'pass',
403 'server1-extra' => {
404 'SSL' => {
405 'CAFile' => "$x509_dir/CA6/root-ca.crt",
406 'CertFile' => "$x509_dir/CA6/server1.example.com.crt",
407 'KeyFile' => "$x509_dir/CA6/server1.example.com.key",
408 },
409 },
410 'server2-extra' => {
411 'SSL' => {
412 'CAFile' => "$x509_dir/CA6/root-ca.crt",
413 'CertFile' => "$x509_dir/CA6/server2.example.com.crt",
414 'KeyFile' => "$x509_dir/CA6/server2.example.com.key",
415 },
416 },
417 ':order' => __LINE__,
418 },
419
420 'subject-alternate-name-mismatch' => {
421 'expect' => 'fail',
422 'pass' => qr/Failed to verify the hostname, expected/,
423 'server1-extra' => {
424 'SSL' => {
425 'CAFile' => "$x509_dir/CA6/root-ca.crt",
426 'CertFile' => "$x509_dir/CA6/server1.example.com.crt",
427 'KeyFile' => "$x509_dir/CA6/server1.example.com.key",
428 },
429 },
430 'server2-extra' => {
431 'SSL' => {
432 'CAFile' => "$x509_dir/CA6/root-ca.crt",
433 'CertFile' => "$x509_dir/CA6/server3.example.com.crt",
434 'KeyFile' => "$x509_dir/CA6/server3.example.com.key",
435 },
436 },
437 ':order' => __LINE__,
438 },
439
440 'cert-expired' => {
441 'expect' => 'fail',
442 'pass' => qr/SSL error: A TLS fatal alert has been received/,
443 'prefix' => [ 'faketime', "\@$one_month_future" ],
444 ':order' => __LINE__,
445 },
446 );
447
448
449 sub write_config {
450 my ($file, $number, $tls_mode, @extras) = @_;
451
452 my $cipher_list = ($tls_mode =~ /^o/ ?
453 'HIGH:!aNULL:@STRENGTH:!SSLv3' :
454 'SECURE128:-VERS-SSL3.0'
455 );
456
457 my $peer_number = 3 - $number;
458
459 # write server configurations
460 my %config = (
461 'Global' => {
462 'Name' => "ngircd.test.server$number",
463 'Info' => "ngIRCd Test-Server $number",
464 'Listen' => '127.0.0.1',
465 'Ports' => "678$number",
466 'AdminEMail' => "admin\@server$number.example",
467 'ServerUID' => $<,
468 'ServerGID' => $(,
469 'MotdFile' => '/dev/null',
470 },
471 'Options' => {
472 'OperCanUseMode' => 'yes',
473 'Ident' => 'no',
474 'IncludeDir' => '',
475 'PAM' => 'no',
476 },
477 'Operator' => {
478 'Name' => 'TestOp',
479 'Password' => '123',
480 },
481 'Server' => {
482 'Name' => "ngircd.test.server$peer_number",
483 'Host' => "server$peer_number.example.com",
484 'Port' => "669$peer_number",
485 'MyPassword' => "pwd$number",
486 'PeerPassword' => "pwd$peer_number",
487 'SSLConnect' => 'yes',
488 'SSLVerify' => 'yes',
489 },
490 'SSL' => {
491 'CAFile' => "$x509_dir/CA1/root-ca.crt",
492 'CertFile' => "$x509_dir/CA1/server$number.example.com.crt",
493 'CipherList' => $cipher_list,
494 'DHFile' => "$x509_dir/dhparams.pem",
495 'KeyFile' => "$x509_dir/CA1/server$number.example.com.key",
496 'Ports' => "669$number",
497 },
498 );
499
500 foreach my $extra (@extras) {
501 foreach my $section (keys %$extra) {
502 foreach my $key (keys %{$extra->{$section}}) {
503 if (
504 exists ($config{$section}{$key}) &&
505 defined ($config{$section}{$key}) &&
506 exists ($extra->{$section}{$key}) &&
507 defined ($extra->{$section}{$key}) &&
508 $config{$section}{$key} eq $extra->{$section}{$key}
509 ) {
510 note ("W: Identical re-definition of server/section/key: $number/$section/$key");
511 }
512 $config{$section}{$key} = $extra->{$section}{$key};
513 }
514 }
515 }
516
517 my $return = '';
518 my $fh;
519 open ($fh, '>', \$return);
520 foreach my $section (sort keys %config) {
521 print $fh "[$section]\n";
522 foreach my $key (sort keys %{$config{$section}}) {
523 my $value = $config{$section}{$key};
524 defined ($value) and
525 printf $fh " %s = %s\n", $key, $value;
526 }
527 }
528 close ($fh);
529
530 write_file ($file, $return);
531 }
532
533
534 sub write_configs {
535 my (
536 $server1_config_file,
537 $server2_config_file,
538 $server1_config_extra,
539 $server2_config_extra,
540 ) = @_;
541
542 write_config (
543 $server1_config_file,
544 '1',
545 $tls_mode[0],
546 {
547 'SSL' => {
548 'Ports' => undef,
549 },
550 },
551 $server1_config_extra,
552 );
553
554 write_config (
555 $server2_config_file,
556 '2',
557 $tls_mode[1],
558 {
559 'Server' => {
560 'Passive' => 'yes',
561 },
562 },
563 $server2_config_extra,
564 );
565 }
566
567
568 sub test1 {
569 my ($name, $test) = @_;
570
571 {
572 my $l = length ($name);
573 note ('+-' . ('-' x $l) . '-+');
574 note ("| $name | ");
575 note ('+-' . ('-' x $l) . '-+');
576 }
577
578 my $server1_config = "$conf_dir/ngircd-test1.conf";
579 my $server2_config = "$conf_dir/ngircd-test2.conf";
580 write_configs (
581 $server1_config,
582 $server2_config,
583 $test->{'server1-extra'},
584 $test->{'server2-extra'},
585 ),
586
587 my $prefix = $test->{'prefix'} // [];
588
589 # start the receiving server2 first, it might need a
590 # little extra time
591 my $server2_log = "$temp_dir/server2.log";
592 write_file ($server2_log, ''); # so open below won't fail
593 my $server2 = Proc::Simple->new;
594 $server2->redirect_output ($server2_log, $server2_log);
595 $server2->start ((
596 @$prefix,
597 $ngircd_exe[1],
598 '--config', $server2_config,
599 '--nodaemon',
600 ));
601 $server2->kill_on_destroy (1);
602 sleep (0.5);
603
604 # start connecting server1
605 my $server1_log = "$temp_dir/server1.log";
606 write_file ($server1_log, ''); # so open below won't fail
607 my $server1 = Proc::Simple->new;
608 $server1->redirect_output ($server1_log, $server1_log);
609 $server1->start ((
610 @$prefix,
611 $ngircd_exe[0],
612 '--config', $server1_config,
613 '--nodaemon',
614 ));
615 $server1->kill_on_destroy (1);
616 sleep (0.5);
617
618 my $fh;
619 open ($fh, '<', $server1_log) or
620 die ("Cannot read '$server1_log': $!");
621
622 # read output from server1, find 'pass' or 'fail' line
623 my $timeout = time + 10;
624
625 my $t0 = time;
626 my $firstline;
627 my $got_verdict;
628 TAIL:
629 while (1) {
630 if (time > $timeout) {
631 fail (sprintf ('Timeout (%u sec)', time - $t0));
632 last TAIL;
633 }
634 my $curpos;
635 my $line;
636 for ($curpos = tell ($fh); $line = <$fh>; $curpos = tell ($fh)) {
637 chomp ($line);
638 $firstline //= $line;
639 $verbose and note (sprintf ('%.2f %s', time-$t0, $line));
640
641 # generic pass/fail pattern
642 my $passed;
643 if ($line =~ /(Can't bind socket to address 127\.0\.0\.1.*$)/) {
644 # previous instance running
645 fail ($1);
646 $got_verdict = 1;
647 last TAIL;
648 } elsif (
649 # configured pass/fail pattern
650 $test->{'pass'} && $line =~ /$test->{'pass'}/
651 ) {
652 $passed = 1;
653 } elsif ($test->{'fail'} && $line =~ /$test->{'fail'}/) {
654 $passed = 0;
655 } elsif ($test->{'noop'} && $line =~ /$test->{'noop'}/) {
656 next;
657 } elsif ($line =~ /Synchronization with "ngircd\.test\.server2" done/) {
658 $passed = $test->{'expect'};
659 } elsif ($line =~ /Certificate validation failed/) {
660 $passed = 1 - $test->{'expect'};
661 } elsif ($line =~ /Fatail: /) {
662 # some fatal error
663 fail ($1);
664 }
665 if (defined ($passed)) {
666 if ($passed) {
667 pass ("Got expected line: '$line'");
668 } else {
669 fail ("Got line that should not be there: '$line'");
670 }
671 $got_verdict = 1;
672 last TAIL;
673 }
674 }
675 if (!$server1->poll) {
676 note ('server1 has left the building');
677 $got_verdict = 1;
678 last TAIL;
679 }
680 sleep (0.1);
681 seek ($fh, $curpos, 0);
682 }
683 close ($fh);
684
685 $server1->kill;
686 $server2->kill;
687
688 wait;
689
690 ok ($got_verdict, 'have a verdict');
691
692 if ($firstline && $firstline =~ /^\[[0-9]+:[0-9] +[0-9]+\./) {
693 # have absolute timestamps
694 my @log = read_file ($server1_log);
695 my $first_server2_line = scalar (@log);
696 push @log, read_file ($server2_log);
697
698 my $RED = "\e[1;31m";
699 my $GREEN = "\e[1;32m";
700 my $NORMAL = "\e[0m";
701
702
703 my @log_sorter;
704 for (my $i = 0; $i < scalar (@log); $i++) {
705 my $line = $log[$i];
706 chomp ($line);
707 my $srt;
708 my $server = $i < $first_server2_line ? 1 : 2;
709 if ($line =~ /^
710 \[
711 (?<pid>[0-9]+):
712 (?<prio>[0-9])\s+
713 (?<time>[0-9]+\.[0-9]+)
714 \]\s(?<msg>.+)
715 $/x) {
716 $srt = [
717 $+{'time'},
718 $server,
719 $+{'prio'},
720 $+{'msg'},
721 ];
722 } else {
723 fail ("Cannot parse log line '$line'");
724 $srt = [ 0, $server, 0, $line ];
725 }
726 $log_sorter[$i] = $srt;
727 }
728
729 # sort the log file by time, process, line
730 my @idx = sort {
731 $log_sorter[$a][0] <=> $log_sorter[$b][0] ||
732 $log_sorter[$a][1] <=> $log_sorter[$b][1] ||
733 $log_sorter[$a][3] cmp $log_sorter[$b][3] ||
734 $a cmp $b
735 } 0..$#log_sorter;
736
737 note ('combined log ([server:prio ms])');
738 my $t1 = $log_sorter[$idx[0]][0];
739 foreach my $idx (@idx) {
740 my $data = $log_sorter[$idx];
741 note (sprintf (
742 '| %s[%s:%d %.6f] %s%s',
743 ($data->[1] == 1 ? $GREEN : $RED),
744 $data->[1],
745 $data->[2],
746 $data->[0] - $t1,
747 $data->[3],
748 $NORMAL,
749 ));
750 }
751 } else {
752 # Just combine
753 my @log;
754 @log = read_file ($server1_log);
755 note ('server 1 log:');
756 foreach my $line (@log) {
757 note ("1: $line");
758 }
759 @log = read_file ($server2_log);
760 note ('server 2 log:');
761 foreach my $line (@log) {
762 note ("2: $line");
763 }
764 }
765 }
766
767
768 # start the show
769
770 if ($x509_populate) {
771 note ('setting up x509 stuff');
772
773 my $fail_x509;
774
775 $ENV{'RNDFILE'} = "$temp_dir/.rnd";
776
777 for my $f (qw<dhparams.pem openssl.cnf openssl.san.cnf>) {
778 copy ("$src_dir/data/$f", "$x509_dir/$f") or
779 die ("Failed to copy $f: $!");
780 }
781
782 foreach my $command ((
783 [ "$src_dir/gen-x509-stuff", $x509_dir, qw<CA1 server1.example.com> ],
784 [ "$src_dir/gen-x509-stuff", $x509_dir, qw<CA1 server2.example.com> ],
785 [ "$src_dir/gen-x509-stuff", $x509_dir, qw<CA1 server3.example.com> ],
786
787 [ "$src_dir/gen-x509-stuff", $x509_dir, qw<CA2 server2.example.com> ],
788
789 [ "$src_dir/gen-x509-stuff", $x509_dir, qw<CA3 server1.example.com> ],
790 [ "$src_dir/gen-x509-stuff", $x509_dir, qw<--revoke CA3 server2.example.com> ],
791
792 [ "$src_dir/gen-x509-stuff", $x509_dir, qw<CA4 server1.example.com> ],
793 [ "$src_dir/gen-x509-stuff", $x509_dir, qw<CA4 *.example.com> ],
794
795 [ "$src_dir/gen-x509-stuff", $x509_dir, qw<CA5 server1.example.com> ],
796 [ "$src_dir/gen-x509-stuff", $x509_dir, qw<CA5 Server2.example.com> ],
797
798 [ "$src_dir/gen-x509-stuff", $x509_dir, qw<CA6 server1.example.com> ],
799 [ "$src_dir/gen-x509-stuff", $x509_dir, qw<--san CA6 server2.example.com> ],
800 [ "$src_dir/gen-x509-stuff", $x509_dir, qw<--san CA6 server3.example.com> ],
801 )) {
802 my $run = Proc::Simple->new;
803 my $log = "$temp_dir/gen-stuff.log";
804 write_file ($log, '');
805 $run->redirect_output ($log, $log);
806 $run->start (@$command);
807 my $exit = $run->wait;
808 my $fail = !is (
809 $exit,
810 0,
811 'gen-x509-stuff ' . join (' ', @$command[2..$#$command]),
812 );
813 $fail and $fail_x509++;
814 if ($fail || $verbose) {
815 my $out = read_file ($log);
816 $log and note ("Output:\n$out");
817 }
818 }
819 $fail_x509 and exit 1;
820 if ($exit_after_x509_populate) {
821 pass ('here we go');
822 done_testing;
823 exit 0;
824 }
825 }
826
827 $ENV{'LD_PRELOAD'} = 'libnss_wrapper.so';
828 $ENV{'NSS_WRAPPER_HOSTS'} = $hosts_file;
829
830 note ('checking mocked resolver');
831 {
832 my $fail;
833
834 foreach my $hostname (qw<server1 server2>) {
835 my $output = `getent hosts $hostname`;
836 is ($?, 0, "resolve $hostname") or $fail++;
837 is ($?, 0, "resolve $hostname.example.com") or $fail++;
838 }
839 }
840
841 my @tests;
842 # check test description integrity
843 {
844 my $fail;
845 my @required = qw<expect :order>; # NB: pass and fail may be missing
846 foreach my $test (sort keys %tests) {
847 foreach my $r (@required) {
848 exists ($tests{$test}{$r}) and next;
849 fail ("No '$r' field in test '$test'");
850 $fail++;
851 }
852 }
853 $fail and die ('Cannot continue');
854 @tests =
855 sort { $tests{$a}{':order'} <=> $tests{$b}{':order'} }
856 keys %tests;
857
858 foreach my $test (@tests) {
859 my $got = $tests{$test}{'expect'};
860 if ($got =~ /^(pass|fail)$/) {
861 $tests{$test}{'expect'} = $got eq 'pass' ? 1 : 0;
862 next;
863 }
864 fail ("The 'expect' in test '$test' is '$got', not 'pass' or 'fail'");
865 $fail++;
866 }
867 $fail and die ('Cannot continue');
868 # drop those who are expected to fail
869 @tests = grep { !$tests{$_}{'unsupported'} } @tests;
870 }
871
872 if (@ARGV) {
873 my $warned;
874 foreach my $t (@ARGV) {
875 if (exists ($tests{$t})) {
876 $tests{$t}{'unsupported'} and
877 note ("Warn: Test '$t' is marked unsupported. Expect breakage");
878 test1 ($t, $tests{$t});
879 next;
880 }
881 fail ("Don't know how to test '$t'");
882 if (!$warned) {
883 note ("Available tests:\n" . join ("\n", map { " $_" } @tests));
884 $warned = 1;
885 }
886 }
887 } else {
888 # run all
889 foreach my $test (@tests) {
890 test1 ($test, $tests{$test});
891 }
892 }
893
894 done_testing;
895
896 exit 0;
897
898 =head1 DESCRIPTION
899
900 Build ngircd for both TLS linkages, possibly using the following script:
901
902 #!/bin/sh
903 set -e
904 case "$1" in
905 openssl | gnutls)
906 [ -f ./configure ] || ./autogen.sh
907 ./configure \
908 --prefix=/usr \
909 --mandir="\${prefix}/share/man" \
910 --infodir="\${prefix}/share/info" \
911 --sysconfdir=/etc/ngircd \
912 "--with-$1"
913 make 2>&1 | tee "../build.$1.log"
914 cp src/ngircd/ngircd "../ngircd-$1"
915 ;;
916 *)
917 echo "Usage: $0 <gnutls|openssl>"
918 exit 1
919 ;;
920 esac
921
922 One time only: Create the certificates
923
924 perl ngircd-test-tls-link \
925 --x509-populate \
926 --x509-dir ../x509-data/ \
927 --ngircd <path to any ngircd binary>
928
929 Then run this program
930
931 For GnuTLS:
932
933 ngircd-test-tls-link \
934 --x509-dir ../x509-data/ \
935 --ngircd ../ngircd-gnutls
936
937 Likewise for OpenSSL:
938
939 (...)
940 --ngircd ../ngircd-openssl
941
942 For interoperability tests:
943
944 ngircd-test-tls-link \
945 --x509-dir ../x509-data/ \
946 --ngircd ../ngircd-gnutls:../ngircd-openssl
947
948 ... also with the two binaries swapped.
949
950 =head1 DEPENDENCIES
951
952 The following programs, libraries and Perl modules must be installed:
953
954 faketime
955 openssl
956 libnss-wrapper
957 File::Slurp
958 Proc::Simple
959
960 =head1 BUGS
961
962 Hack.
963
964 =head1 SEE ALSO
965
966 ngircd(8)
967
968 =head1 AUTHOR
969
970 Christoph Biedl C<< <[email protected]> >>
971
972 =head1 ACKNOWLEDGEMENTS
973
974 Alex Barton for ngircd.
975
976 =head1 COPYRIGHT & LICENSE
977
978 Copyright (C) 2014-2024 Christoph Biedl <[email protected]>
979
980 This program is free software; you can redistribute it and/or
981 modify it under the terms of the GNU General Public License as
982 published by the Free Software Foundation; either version 2 of the
983 License, or (at your option) any later version.
984
985 This package is distributed in the hope that it will be useful,
986 but WITHOUT ANY WARRANTY; without even the implied warranty of
987 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
988 GNU General Public License for more details.
989
990 You should have received a copy of the GNU General Public License
991 along with this program. If not, see <http://www.gnu.org/licenses/>
992
993 On Debian systems, the complete text of the GNU General Public
994 License version 2 can be found in
995 "/usr/share/common-licenses/GPL-2".
996
997 =cut