Debian lenny version packages
[pkg-perl] / deb-src / libnet-ssleay-perl / libnet-ssleay-perl-1.35 / examples / ssl-inetd-serv.pl
1 #!/usr/bin/perl
2 # ssl-inetd-serv.pl - SSL echo server run from inetd
3 #
4 # Copyright (c) 1996,1998 Sampo Kellomaki <sampo@iki.fi>. All Rights Reserved.
5 # Date:   27.6.1996, 19.6.1998
6 #
7 # /etc/inetd.conf:
8 #   ssltst  stream  tcp nowait root /usr/sampo/ssl-inetd-serv.pl ssl-inetd
9 #
10 # /etc/services:
11 #   ssltst              1234/tcp
12 #
13
14 use Net::SSLeay qw(die_now die_if_ssl_error);
15 Net::SSLeay::load_error_strings();
16 Net::SSLeay::SSLeay_add_ssl_algorithms();
17
18 chdir '/usr/sampo' or die "chdir: $!";
19
20 $| = 1;  # STDOUT Piping hot!
21
22 open LOG, ">>log" or die "Can't open log file $!";
23 select LOG; $| = 1;
24 print "ssl-inetd-serv.pl started\n";
25
26 print "Creating SSL context...\n";
27 $ctx = Net::SSLeay::CTX_new or die_now("CTX_new ($ctx) ($!)");
28 print "Setting private key and certificate...\n";
29 Net::SSLeay::set_server_cert_and_key($ctx, 'cert.pem', 'key.pem') or die "key";
30
31 print "Creating SSL connection (context was '$ctx')...\n";
32 $ssl = Net::SSLeay::new($ctx) or die_now("new ($ssl) ($!)");
33
34 print "Setting fds (ctx $ctx, con $ssl)...\n";
35 Net::SSLeay::set_rfd($ssl, fileno(STDIN));
36 Net::SSLeay::set_wfd($ssl, fileno(STDOUT));
37
38 print "Entering SSL negotiation phase...\n";
39     
40 Net::SSLeay::accept($ssl);
41 die_if_ssl_error("accept: $!");
42
43 print "Cipher '" . Net::SSLeay::get_cipher($ssl) . "'\n";
44
45 #
46 # Connected. Exchange some data.
47 #
48
49 $got = Net::SSLeay::ssl_read_all($ssl) or die "$$: ssl read failed";
50 print "Got `$got' (" . length ($got) . " chars)\n";
51 $got = uc $got;
52 Net::SSLeay::ssl_write_all($ssl, $got) or die "$$: ssl write failed";
53
54 print "Tearing down the connection.\n";
55
56 Net::SSLeay::free ($ssl);
57 Net::SSLeay::CTX_free ($ctx);
58
59 close LOG;
60
61 __END__