#!/usr/bin/perl -w use strict; # Fingerproxy: a fingerd that is actually a generic socket proxy and clock. # Offered under the Floodgap Free Software License: # http://www.floodgap.com/software/ffsl/ # Copyright 2009, 2014, 2016, 2019 Cameron Kaiser # # This must be connected to inetd to function. # It is not a daemon. # # ### VERSION 0.2 # 0.2: add \r\n, time and curl modes (6/2/19) # 0.1: Initial release (11/11/09) ######################################## # YOU MUST CONFIGURE THIS SCRIPT FIRST # ######################################## # If your environment uses libraries that inetd may not normally have # set, set those environment variables here. If you don't need this, # remove this line. $ENV{'LIBPATH'} = "/opt/freeware/lib"; # Set $NC to the location of netcat, or a tool that works like netcat. # DON'T USE TELNET FOR THIS! # Add any default arguments you want; for security reasons you can't do # that below. my $NC = "/usr/local/bin/nc -w 10"; # Set $CURL to the location of cURL, or the null string if you don't # want to support this. Make sure the -K - arguments are present; the # URL is transmitted on stdin to avoid unsafe things like metacharacters. # Make sure $CURL is set, though, even if it's a "" (null string). my $CURL = "/usr/local/bin/curl -K -"; # Set $FINGERD to where your fingerd normally resides. If you don't # set this, then fingerproxy will handle all of the requests itself. my $FINGERD = "/usr/sbin/fingerd"; # Remove the line below. If you don't, fingerproxy won't run. die "You didn't configure this script properly"; ####################################################### # END OF CONFIGURATION SECTION. WHITE HATS ONLY BELOW # ####################################################### # stuff warnings $SIG{"__WARN__"} = sub { ; }; # The finger protocol accepts a single line of data, and that's what we'll # do, with processing for binary characters and other entities. select(STDOUT); $|++; my $request = ; # A fingerproxy command looks like this: # hostname|port|data ... # If that's not what we got, maybe we're trying to handle a regular # finger request? if ($request !~ /\|/ && length($FINGERD)) { my $pid = open(DAEMON, "|$FINGERD") || die("failed fingerd: $!\n"); print DAEMON $request; close(DAEMON); exit; } $request =~ s/^\/[wW]\s+//; # ignore any leading /w some finger clients add # Check for modes. my $mode = 0; while ($request =~ s/^\|//) { $mode++; } if ($mode > 3) { print STDOUT "1unsupported mode $mode"; # future proof exit; } # If mode 3, this is a time request. Emit the date and time. # DATE MM/DD/YY # TIME 24:MM if ($mode == 3) { my @ltime = (localtime); my $year = ($ltime[5] < 100) ? $ltime[5] : ($ltime[5]-100); printf(STDOUT "DATE %02i/%02i/%02i\r\n", $ltime[4]+1, $ltime[3], $year); # Since it does not allow seconds, if the minute is not 59 # and seconds > 30, round up minutes. $ltime[1]++ if ($ltime[1] < 59 && $ltime[0] > 30); printf(STDOUT "TIME %02i:%02i\r\n", $ltime[2], $ltime[1]); exit; } # If mode 2, treat as a URL argument to cURL, if supported. if ($mode == 2) { $request =~ s/[\r\l\n\s].+$//; if (!length($CURL) || !length($request) || $request !~ m#://#) { print STDOUT "2$request"; exit; } my $pid = open(NETCAT, "|$CURL") || do { print STDOUT "3$!"; # system error exit; }; close(STDERR); print NETCAT <<"EOF"; -L --silent --fail --show-error --max-redirs=0 url=$request user-agent="User-Agent: NokiaN90-1/3.0545.5.1 Series60/2.8 Profile/MIDP-2.0 Configuration/CLDC-1.1 (Alpha Micro Fingerproxy HTTP)" EOF close(NETCAT); # automatic wait exit; } # mode 0 or 1 if ($request !~ /\|/) { print STDOUT "1$request"; # malformed request exit; } # A reply gives a single character response code ("0" = no error), # and then any data. $request =~ s/[\012\015]+$//; # remove trailing \n or \r\n my ($host, $port, $data) = split(/\|/, $request, 3); $port += 0; $host = lc($host); if (!length($host) || $port < 1) { print STDOUT "1$request"; # malformed request exit; } if ($host =~ /[^-a-z0-9_\.]/ || $host =~ /^[^a-z0-9]/) { print STDOUT "2$request"; # malformed request: to avoid trivial security issues exit; } # Process the data. # We understand: \n \r \t \b \0xXX # And we accept \\ as backslash. my $uniq = 0; $uniq++ while ($data =~ /BACKSLASH_ESCAPE_SEQUENCE$uniq/); $uniq = "BACKSLASH_ESCAPE_SEQUENCE$uniq"; $data =~ s/\\\\/$uniq/g; $data =~ s/\\n/\012/g; $data =~ s/\\r/\015/g; $data =~ s/\\t/\011/g; $data =~ s/\\b/\010/g; $data =~ s/\\0x([a-z0-9]{2})/pack("H2",$1)/eg; $data =~ s/$uniq/\\/g; # If mode 1, add "\r\n" to the end. if ($mode == 1) { $data .= "\r\n"; } my $pid = open(NETCAT, "|$NC $host $port") || do { print STDOUT "3$!"; # system error exit; }; print STDOUT "0"; print NETCAT $data; close(NETCAT); # automatic wait exit;