From 7427c871abee6768cdd1c12d0ddf23f13cb37197 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Mon, 15 Feb 2010 23:47:16 +0100 Subject: Various stylistic improvements suggested by Max (such as using the // op). --- emergencyc.pl | 20 ++++---------------- emergencyd.pl | 16 +++++++--------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/emergencyc.pl b/emergencyc.pl index a7d55d2..b8bebb6 100755 --- a/emergencyc.pl +++ b/emergencyc.pl @@ -46,18 +46,8 @@ if ( defined($opts{K}) ) { } die "No key specified (use -K or -k option)" unless defined($key); -my $host; -if ( defined($opts{h}) ) { - $host = $opts{h}; -} else { - $host = "localhost"; -} -my $port; -if ( defined($opts{p}) ) { - $port = $opts{p}; -} else { - $port = DEFAULT_PORT; -} +my $host = $opts{h} // "localhost"; +my $port = $opts{p} // DEFAULT_PORT; my @res = getaddrinfo($host, $port, AF_UNSPEC, SOCK_DGRAM) or die "Cannot resolve host $host port $port"; @@ -72,10 +62,8 @@ sub curtime { return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",$year+1900,$mon+1,$mday,$hour,$min,$sec); } -my $command = $ARGV[0]; -$command = "PING" unless defined($command); -my $timestamp = $opts{t}; -$timestamp = curtime unless defined($timestamp); +my $command = $ARGV[0] // "PING"; +my $timestamp = $opts{t} // curtime; my $validate = "$command|$timestamp"; my $maccheck = hmac_sha256_hex($validate, $key); send $socket, "$command|$timestamp|$maccheck", 0, $haddr; diff --git a/emergencyd.pl b/emergencyd.pl index 5766484..200881b 100755 --- a/emergencyd.pl +++ b/emergencyd.pl @@ -90,13 +90,12 @@ my $proto = getprotobyname("udp") or die "Can't resolve udp protocol: $!"; my $port; if ( defined($opts{p}) ) { $port = $opts{p}; - $port =~ /^(\d+)$/ or die "Invalid port number (-p option) $port"; + $port =~ /^\d+$/ or die "Invalid port number (-p option) $port"; } else { $port = DEFAULT_PORT; } -my $socket; -socket $socket, PF_INET6, SOCK_DGRAM, $proto or die "Can't create socket: $!"; +socket my $socket, PF_INET6, SOCK_DGRAM, $proto or die "Can't create socket: $!"; if ( defined(IPV6_V6ONLY) ) { setsockopt $socket, IPPROTO_IPV6, IPV6_V6ONLY, 0 or die "Can't set IPV6_V6ONLY option to 0: $!"; } @@ -114,7 +113,7 @@ if ( $opts{f} ) { } sub curtime { - my $fiddle = shift; $fiddle = 0 unless defined($fiddle); + my $fiddle = shift // 0; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time+$fiddle); return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",$year+1900,$mon+1,$mday,$hour,$min,$sec); } @@ -126,9 +125,9 @@ while (1) { my $buf; my $sender = recv($socket, $buf, 16384, 0); my @lines = split /\015*\012|\|/s, $buf; - my $command = $lines[0]; $command = "" unless defined($command); - my $timestamp = $lines[1]; $timestamp = "" unless defined($timestamp); - my $maccheck = $lines[2]; $maccheck = "" unless defined($maccheck); + my $command = $lines[0] // ""; + my $timestamp = $lines[1] // ""; + my $maccheck = $lines[2] // ""; next PACKET if $command eq ""; if ( $command eq "PING" ) { send $socket, "PONG\n", 0, $sender; @@ -157,8 +156,7 @@ while (1) { if ( $command eq "NOOP" ) { send $socket, "NOOP\n", 0, $sender; } elsif ( $command eq "DPID" ) { - my $pid = POSIX::getpid; - send $socket, "DPID\n$pid\n", 0, $sender; + send $socket, "DPID\n$$\n", 0, $sender; } elsif ( $command eq "DIE!" ) { send $socket, "BYE!\n", 0, $sender; exit 0; -- cgit v1.2.3 From c93beedc46c0495a45c449fc66a7eb04630567f4 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Tue, 16 Feb 2010 17:28:50 +0100 Subject: Work against the dreadful habit of concatenatingallmywords in identifiers. --- emergencyc.pl | 14 +++++++------- emergencyd.pl | 24 ++++++++++++------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/emergencyc.pl b/emergencyc.pl index b8bebb6..296297f 100755 --- a/emergencyc.pl +++ b/emergencyc.pl @@ -10,8 +10,8 @@ # Options recognized: # -# -K specifies the key to use; or -k specifies a -# keyfile (the client will use the first line as key). +# -K specifies the key to use; or -k specifies a key +# file (the client will use the first line as key). # # -h and -p specifies the host and port to connect # to. @@ -39,10 +39,10 @@ my $key; if ( defined($opts{K}) ) { $key = $opts{K}; } elsif ( defined($opts{k}) ) { - open my $keyfile, "<", $opts{k} or die "Cannot open key file $opts{k}: $!"; - $key = <$keyfile>; + open my $key_file, "<", $opts{k} or die "Cannot open key file $opts{k}: $!"; + $key = <$key_file>; chomp $key; - close $keyfile; + close $key_file; } die "No key specified (use -K or -k option)" unless defined($key); @@ -65,8 +65,8 @@ sub curtime { my $command = $ARGV[0] // "PING"; my $timestamp = $opts{t} // curtime; my $validate = "$command|$timestamp"; -my $maccheck = hmac_sha256_hex($validate, $key); -send $socket, "$command|$timestamp|$maccheck", 0, $haddr; +my $mac_check = hmac_sha256_hex($validate, $key); +send $socket, "$command|$timestamp|$mac_check", 0, $haddr; my $buf; my $sender; diff --git a/emergencyd.pl b/emergencyd.pl index 200881b..6019029 100755 --- a/emergencyd.pl +++ b/emergencyd.pl @@ -46,7 +46,7 @@ # binds to the IPv6 unspecified address with the IPV6_V6ONLY option # set to 0, thus listening on both IPv6 and IPv4 families. # -# -k specifies the keyfile to use. This file contains one +# -k specifies the key file to use. This file contains one # or more keys, one per line, which will all be equally valid when # computing the MAC. # @@ -72,19 +72,19 @@ getopts("k:p:f", \%opts); my @authorized_keys; -my $keyfilename = $opts{k}; -die "No key file specified (use -k option)" unless defined($keyfilename); -sub readkeys { - open my $keyfile, "<", $keyfilename +my $key_filename = $opts{k}; +die "No key file specified (use -k option)" unless defined($key_filename); +sub read_keys { + open my $key_file, "<", $key_filename or die "Cannot open key file $opts{k}: $!"; @authorized_keys = (); - while (<$keyfile>) { + while (<$key_file>) { chomp; push @authorized_keys, $_; } - close $keyfile; + close $key_file; } -readkeys; +read_keys; my $proto = getprotobyname("udp") or die "Can't resolve udp protocol: $!"; my $port; @@ -135,13 +135,13 @@ while (1) { send $socket, ("DATE\n".curtime."\n".$mintime."\n"), 0, $sender; } else { my $validate = "$command|$timestamp"; - my $macchecked = 0; + my $mac_checked = 0; foreach my $key ( @authorized_keys ) { if ( $maccheck eq hmac_sha256_hex($validate, $key) ) { - $macchecked = 1; + $mac_checked = 1; } } - unless ( $macchecked ) { + unless ( $mac_checked ) { send $socket, "!MAC\n", 0, $sender; next PACKET; } @@ -162,7 +162,7 @@ while (1) { exit 0; } elsif ( $command eq "RKEY" ) { my $resp = "DONE\n"; - eval { readkeys }; + eval { read_keys }; if ( $@ ) { $resp = "!ERR\n$@"; } -- cgit v1.2.3 From 426c4f4bcad9dd890ad8dbbc28be5b3690fd6664 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Tue, 16 Feb 2010 17:32:23 +0100 Subject: Rewrap long lines. --- emergencyc.pl | 6 ++++-- emergencyd.pl | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/emergencyc.pl b/emergencyc.pl index 296297f..86c9da7 100755 --- a/emergencyc.pl +++ b/emergencyc.pl @@ -39,7 +39,8 @@ my $key; if ( defined($opts{K}) ) { $key = $opts{K}; } elsif ( defined($opts{k}) ) { - open my $key_file, "<", $opts{k} or die "Cannot open key file $opts{k}: $!"; + open my $key_file, "<", $opts{k} + or die "Cannot open key file $opts{k}: $!"; $key = <$key_file>; chomp $key; close $key_file; @@ -59,7 +60,8 @@ bind $socket, sockaddr_in6(0, in6addr_any); sub curtime { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time); - return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",$year+1900,$mon+1,$mday,$hour,$min,$sec); + return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", + $year+1900,$mon+1,$mday,$hour,$min,$sec); } my $command = $ARGV[0] // "PING"; diff --git a/emergencyd.pl b/emergencyd.pl index 6019029..a0512fd 100755 --- a/emergencyd.pl +++ b/emergencyd.pl @@ -95,9 +95,11 @@ if ( defined($opts{p}) ) { $port = DEFAULT_PORT; } -socket my $socket, PF_INET6, SOCK_DGRAM, $proto or die "Can't create socket: $!"; +socket my $socket, PF_INET6, SOCK_DGRAM, $proto + or die "Can't create socket: $!"; if ( defined(IPV6_V6ONLY) ) { - setsockopt $socket, IPPROTO_IPV6, IPV6_V6ONLY, 0 or die "Can't set IPV6_V6ONLY option to 0: $!"; + setsockopt $socket, IPPROTO_IPV6, IPV6_V6ONLY, 0 + or die "Can't set IPV6_V6ONLY option to 0: $!"; } bind $socket, sockaddr_in6($port, in6addr_any) or die "Can't bind socket: $!"; @@ -114,8 +116,10 @@ if ( $opts{f} ) { sub curtime { my $fiddle = shift // 0; - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time+$fiddle); - return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",$year+1900,$mon+1,$mday,$hour,$min,$sec); + my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) + = gmtime(time+$fiddle); + return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", + $year+1900,$mon+1,$mday,$hour,$min,$sec); } my $mintime = "0"; @@ -171,8 +175,10 @@ while (1) { my $s = $1; my $resp = "DONE\n"; eval { - open my $sysrq_trigger, ">", "/proc/sysrq-trigger" or die "Couldn't open /proc/sysrq-trigger for writing: $!"; - print $sysrq_trigger $s or die "Couldn't write to /proc/sysrq-trigger: $!"; + open my $sysrq_trigger, ">", "/proc/sysrq-trigger" + or die "Couldn't open /proc/sysrq-trigger for writing: $!"; + print $sysrq_trigger $s + or die "Couldn't write to /proc/sysrq-trigger: $!"; close $sysrq_trigger; }; if ( $@ ) { -- cgit v1.2.3 From fb276c9a2c882505aa635926f36c9e6e9330ba9a Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Tue, 16 Feb 2010 17:40:35 +0100 Subject: Pay homage to Unix bondage and discipline. --- emergencyd.pl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/emergencyd.pl b/emergencyd.pl index a0512fd..f2fed7a 100755 --- a/emergencyd.pl +++ b/emergencyd.pl @@ -104,6 +104,9 @@ if ( defined(IPV6_V6ONLY) ) { bind $socket, sockaddr_in6($port, in6addr_any) or die "Can't bind socket: $!"; if ( $opts{f} ) { + chdir("/"); + open STDIN, "/dev/null"; + POSIX::setsid; $SIG{HUP} = "IGNORE"; $SIG{INT} = "IGNORE"; my $childpid = fork; -- cgit v1.2.3 From 8eeeb14f4284e2ee1fa040ae0209a6c02432005d Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Tue, 16 Feb 2010 17:46:19 +0100 Subject: Check recv() return value. --- emergencyc.pl | 7 +++++-- emergencyd.pl | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/emergencyc.pl b/emergencyc.pl index 86c9da7..5d59be1 100755 --- a/emergencyc.pl +++ b/emergencyc.pl @@ -77,10 +77,13 @@ eval { alarm 5; do { $sender = recv($socket, $buf, 16384, 0); - } while ( $sender ne $haddr ); + } while ( defined($sender) && $sender ne $haddr ); }; if ( $@ ) { - printf "timeout\n"; + print "timeout\n"; + exit 1; +} elsif ( !defined($sender) ) { + die "Failed to receive packet: $!"; } else { printf "%s", $buf; } diff --git a/emergencyd.pl b/emergencyd.pl index f2fed7a..47c39e4 100755 --- a/emergencyd.pl +++ b/emergencyd.pl @@ -131,6 +131,7 @@ PACKET: while (1) { my $buf; my $sender = recv($socket, $buf, 16384, 0); + die "Failed to receive packet: $!" unless defined($sender); my @lines = split /\015*\012|\|/s, $buf; my $command = $lines[0] // ""; my $timestamp = $lines[1] // ""; -- cgit v1.2.3 From 170ee75143501719f9a4230ed8859719965d290e Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Tue, 16 Feb 2010 17:54:49 +0100 Subject: The incomprehensible rules of Unix B&D dictate that setsid should move. Gratuitous annoyance: setsid does not work for a process group leader, so we have to do it after the fork. --- emergencyd.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emergencyd.pl b/emergencyd.pl index 47c39e4..c37b938 100755 --- a/emergencyd.pl +++ b/emergencyd.pl @@ -106,7 +106,6 @@ bind $socket, sockaddr_in6($port, in6addr_any) or die "Can't bind socket: $!"; if ( $opts{f} ) { chdir("/"); open STDIN, "/dev/null"; - POSIX::setsid; $SIG{HUP} = "IGNORE"; $SIG{INT} = "IGNORE"; my $childpid = fork; @@ -115,6 +114,7 @@ if ( $opts{f} ) { print "$childpid\n"; exit 0; } + POSIX::setsid; } sub curtime { -- cgit v1.2.3