6 use Sys::Syslog qw(syslog);
7 use Net::Server::PreFork;
10 use Data::Dumper; # For debugging
11 require UNIVERSAL::require;
13 #use Sip qw(readline);
14 use Sip::Constants qw(:all);
15 use Sip::Configuration;
16 use Sip::Checksum qw(checksum verify_cksum);
19 use constant LOG_SIP => "local6"; # Local alias for the logging facility
21 our @ISA = qw(Net::Server::PreFork);
27 RAW => \&raw_transport,
28 telnet => \&telnet_transport,
29 http => \&http_transport,
34 my $config = new Sip::Configuration $ARGV[0];
41 foreach my $svc (keys %{$config->{listeners}}) {
42 push @parms, "port=" . $svc;
48 push @parms, "log_file=Sys::Syslog", "syslog_ident=acs-server",
49 "syslog_facility=" . LOG_SIP;
52 # Server Management: set parameters for the Net::Server::PreFork
53 # module. The module silently ignores parameters that it doesn't
54 # recognize, and complains about invalid values for parameters
57 if (defined($config->{'server-params'})) {
58 while (my ($key, $val) = each %{$config->{'server-params'}}) {
59 push @parms, $key . '=' . $val;
66 # This is the main event.
67 SIPServer->run(@parms);
73 # process_request is the callback used by Net::Server to handle
74 # an incoming connection request.
80 my ($sockaddr, $port, $proto);
83 $self->{config} = $config;
85 $sockname = getsockname(STDIN);
86 ($port, $sockaddr) = sockaddr_in($sockname);
87 $sockaddr = inet_ntoa($sockaddr);
88 $proto = $self->{server}->{client}->NS_proto();
90 $self->{service} = $config->find_service($sockaddr, $port, $proto);
92 if (!defined($self->{service})) {
93 syslog("LOG_ERR", "process_request: Unknown recognized server connection: %s:%s/%s", $sockaddr, $port, $proto);
94 die "process_request: Bad server connection";
97 $transport = $transports{$self->{service}->{transport}};
99 if (!defined($transport)) {
100 syslog("LOG_WARN", "Unknown transport '%s', dropping", $service->{transport});
115 my $service = $self->{service};
121 local $SIG{ALRM} = sub { die "alarm\n"; };
122 syslog("LOG_DEBUG", "raw_transport: timeout is %d",
123 $service->{timeout});
125 alarm $service->{timeout};
126 $input = Sip::read_SIP_packet(*STDIN);
131 syslog("LOG_INFO", "raw_transport: shutting down: EOF during login");
135 $input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator
137 last if Sip::MsgType::handle($input, $self, LOGIN);
142 syslog("LOG_ERR", "raw_transport: LOGIN ERROR: '$@'");
143 die "raw_transport: login error, exiting";
144 } elsif (!$self->{account}) {
145 syslog("LOG_ERR", "raw_transport: LOGIN FAILED");
146 die "raw_transport: Login failed, exiting";
149 syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
150 $self->{account}->{id},
151 $self->{account}->{institution});
153 $self->sip_protocol_loop();
155 syslog("LOG_INFO", "raw_transport: shutting down");
158 sub telnet_transport {
164 my $config = $self->{config};
166 # Until the terminal has logged in, we don't trust it
167 # so use a timeout to protect ourselves from hanging.
169 local $SIG{ALRM} = sub { die "alarm\n"; };
173 $| = 1; # Unbuffered output
174 $timeout = $config->{timeout} if (exists($config->{timeout}));
187 $uid =~ s/[\r\n]+$//;
188 $pwd =~ s/[\r\n]+$//;
190 if (exists($config->{accounts}->{$uid})
191 && ($pwd eq $config->{accounts}->{$uid}->password())) {
192 $account = $config->{accounts}->{$uid};
195 syslog("LOG_WARNING", "Invalid login attempt: '%s'", $uid);
196 print("Invalid login\n");
202 syslog("LOG_ERR", "telnet_transport: Login timed out");
203 die "Telnet Login Timed out";
204 } elsif (!defined($account)) {
205 syslog("LOG_ERR", "telnet_transport: Login Failed");
208 print "Login OK. Initiating SIP\n";
211 $self->{account} = $account;
213 $self->sip_protocol_loop();
214 syslog("LOG_INFO", "telnet_transport: shutting down");
222 # The terminal has logged in, using either the SIP login process
223 # over a raw socket, or via the pseudo-unix login provided by the
224 # telnet transport. From that point on, both the raw and the telnet
225 # processes are the same:
226 sub sip_protocol_loop {
229 my $service = $self->{service};
230 my $config = $self->{config};
233 # Now that the terminal has logged in, the first message
234 # we recieve must be an SC_STATUS message. But it might be
235 # an SC_REQUEST_RESEND. So, as long as we keep receiving
236 # SC_REQUEST_RESEND, we keep waiting for an SC_STATUS
238 # Comprise reports that no other ILS actually enforces this
239 # constraint, so we'll relax about it too. As long as everybody
240 # uses the SIP "raw" login process, rather than telnet, this
241 # will be fine, becaues the LOGIN protocol exchange will force
242 # us into SIP 2.00 anyway. Machines that want to log in using
243 # telnet MUST send an SC Status message first, even though we're
246 #$expect = SC_STATUS;
249 while ($input = Sip::read_SIP_packet(*STDIN)) {
252 $input =~ s/[\r\n]+$//sm; # Strip off any trailing line ends
254 $status = Sip::MsgType::handle($input, $self, $expect);
255 next if $status eq REQUEST_ACS_RESEND;
258 syslog("LOG_ERR", "raw_transport: failed to handle %s",
259 substr($input, 0, 2));
260 die "raw_transport: dying";
261 } elsif ($expect && ($status ne $expect)) {
262 # We received a non-"RESEND" that wasn't what we were
265 "raw_transport: expected %s, received %s, exiting",
267 die "raw_transport: exiting: expected '$expect', received '$status'";
269 # We successfully received and processed what we were expecting