File Coverage

blib/lib/PheMail/Server.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 30 32 93.7


line stmt bran cond sub pod time code
1             package PheMail::Server;
2              
3 1     1   7144 use 5.006;
  1         3  
  1         40  
4 1     1   5 use strict;
  1         2  
  1         33  
5 1     1   5 use warnings;
  1         7  
  1         43  
6 1     1   953 use Net::Server::Fork;
  1         89478  
  1         35  
7 1     1   8 use Digest::MD5 qw(md5 md5_hex);
  1         3  
  1         84  
8 1     1   815 use PheMail::General;
  1         363  
  1         51  
9 1     1   859 use Unix::Syslog qw(:macros :subs);
  1         9885  
  1         1434  
10 1     1   11411 use Math::XOR;
  0            
  0            
11             use vars qw( $whatsaid %peers $port $debug $restrict
12             $timeout $previous_alarm $timeout_sec $xor);
13              
14             require Exporter;
15              
16             our @ISA = qw(Exporter Net::Server::Fork);
17              
18             # Items to export into callers namespace by default. Note: do not export
19             # names by default without a very good reason. Use EXPORT_OK instead.
20             # Do not simply export all your public functions/methods/constants.
21              
22             # This allows declaration use PheMail::Server ':all';
23             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
24             # will save memory.
25             our %EXPORT_TAGS = ( 'all' => [ qw(
26            
27             ) ] );
28              
29             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
30              
31             our @EXPORT = qw(
32            
33             );
34             our $VERSION = '0.04';
35              
36             # Debug level (Defaults to 0)
37             $debug = 0;
38              
39             # use XOR encryption?
40             $xor = 1;
41              
42             # Restrict connection to mainserver only?
43             $restrict = 1;
44              
45             # Timeout for client
46             $timeout_sec = 20;
47              
48             # Open syslog descriptor
49             openlog("phemaild",LOG_PID,LOG_MAIL);
50              
51             # Define portnumber here. Get it from the configfile.
52             $port = ReadConfig("daemonport");
53              
54             # Preloaded methods go here.
55              
56             sub do_log($$) {
57             my($level,$msg) = @_;
58             if ($level <= $debug) {
59             syslog (LOG_INFO, "%s",$msg);
60             }
61             }
62              
63             sub ResolveHost($) {
64             my $ipaddr= shift;
65             return $ipaddr;
66             }
67              
68             sub exor($) {
69             my $string = shift;
70             if (!$xor) { return $string; }
71             my $enc = xor_buf($string,ReadConfig("salt"));
72             return $enc;
73             }
74             sub process_request {
75             my $self = shift;
76             do_log(0,"connect ".ResolveHost($self->{server}->{'peeraddr'}));
77             print "PheMaild ".$VERSION.": Hello ".$self->{server}->{'peeraddr'}."\r\n";
78             if ($restrict) {
79             if ($self->{server}->{'peeraddr'} ne ReadConfig("serverhost")) {
80             print "I'm gonna have to cut you off.\r\n";
81             print "I am only accepting trusted hosts.\r\nGoodbye.\r\n";
82             return;
83             }
84             }
85             print "PheMaild ".$VERSION.": Ready.\r\n";
86             print ":FOO:\r\n";
87             $peers{$self->{server}->{'peeraddr'}} = "on";
88             eval {
89             while () {
90             local $SIG{ALRM} = sub {
91             die;
92             };
93             $timeout = $timeout_sec; # give the user 30 seconds
94             $previous_alarm = alarm($timeout);
95             $_ =~ s/\r?\n//g;
96             $whatsaid = $_;
97             if($_ eq 'requestsalt') {
98             print "salt=|".md5_hex(ReadConfig("salt"))."|:FOO:\r\n";
99             do_log(0,ResolveHost($self->{server}->{'peeraddr'})." requested serversalt.");
100             next;
101             }
102             if($_ eq 'xor') {
103             if ($xor) {
104             print "xor=|xor|:FOO:\r\n";
105             } else {
106             print "xor=|foo|:FOO:\r\n";
107             }
108             next;
109             }
110             if($_ eq 'noop') {
111             print exor("ok")."\r\n";
112             next;
113             }
114             if($_ eq 'who') {
115             do_log(0,ResolveHost($self->{server}->{'peeraddr'})." Requesting who-list");
116             print "clients=|";
117             foreach my $client (keys %peers) {
118             print $client.",";
119             }
120             print "|:FOO:\r\n";
121             next;
122             }
123             if($_ eq 'username') {
124             print "username=|".exor(getpwuid($<))."|:FOO:\r\n";
125             next;
126             }
127             if($_ eq 'load') {
128             open(LOAD,"uptime|") or print "Couldn't open uptime: $!\r\n";
129             while() {
130             if (/^$/) { next; }
131             print "load=|".exor($1)."|:FOO:\r\n" if /load\s*averages?:\s(.+)$/;
132             }
133             close(LOAD);
134             next;
135             }
136             if($_ eq 'uptime') {
137             open(UPTIME,"uptime|") or print "Couldn't open uptime: $!\r\n";
138             while() {
139             if (/^$/) { next; }
140             print "uptime=|".exor($1)."|:FOO:\r\n" if /up +(.*), +\d+ users?,/;
141             }
142             close(UPTIME);
143             next;
144             }
145             if($_ eq 'whoareyou') {
146             print "iam=|".exor(ReadConfig("whoami"))."|:FOO:\r\n";
147             next;
148             }
149             if($_ eq 'quit') {
150             do_log(0,"disconnect ".ResolveHost($self->{server}->{'peeraddr'}));
151             print "PheMaild ".$VERSION.": Goodbye.\n";
152             return;
153             } else {
154             do_log(3,ResolveHost($self->{server}->{'peeraddr'})." sent: ".$whatsaid);
155             }
156             }
157             alarm($previous_alarm); # initialize previous alarm
158             };
159             if($@=~/timed out/i){
160             return;
161             }
162             }
163             sub RunServer {
164             __PACKAGE__->run(port => $port);
165             }
166             1;
167             __END__