#! /usr/bin/perl
#  VERSION 1.2
# __________________________________________  P Fan - 12/2021
#
# NAME
#
#   sshproc [ start | restart | stop | status ]
#   
# SYNOPSIS
#
#   This will attempt to do the above for the sshd service.
#   It is for Yosemite and forward.
#   
#   This requires root except for status.  If no argument is given, then
#   it will report its status.
#
#   It checks the status of sshd via
# launchctl print-disabled system | grep com.openssh.sshd | grep true
# launchctl print system/com.openssh.sshd
#
#   For sshd to work, you need it to be enabled and booted up.
#   Sometimes even when both are up, it still will not work - most likely
#   one of them is wedged.  Almost always restarting both will fix it
#      sshproc restart
#
#   start
#      We start up whatever of the above two is not up.
#
#   stop 
#      We stop whatever of the above two is not up.
# 
#   restart
#      Basically it does what is in stop.  Then it is enabled and 
#      bootstrap up.
#
#   status
#      This tells you whether sshd is disable/enabled and booted out/up.  
#
#   
#   If sshd is wedged then you should do  sshproc restart
#
#   Note: launchctl output may depend on the user invoking it.
# ____________________________________________________________________


    # *********************************
    # PRELIMINARIES
    # *********************************

    # what do you want to do

$restart = $start = $stop = $status = "";
if (@ARGV > 0) {
    if ($ARGV[0] eq "start") {
	print "\nYou have requested sshd be started.\n";
	$start = "y";
    }
    elsif ($ARGV[0] eq "restart") {
	print "\nYou have requested sshd be restarted.\n";
	$restart = "y";
    }
    elsif ($ARGV[0] eq "stop") {
	print "\nYou have requested sshd be stopped.\n";
	$stop = "y";
    }
    elsif ($ARGV[0] eq "status") {
	print "\nYou have requested the status of sshd.\n";
	$status = "y";
    }

    else {
 	die "\nUsage: sshproc [ start | restart | stop | status ]\n\n";
    }
}
else {
    print "\nYou have requested the status of sshd.\n";
    $status = "y";
}



    # will do status report for any user but other tasks require root

if ($status ne "y") {
    $ENV{USER} eq "root" || 
       die "\nYou must be root to use this program.\n\n";
}



    # Get a single value for $ver
    #
    # Let  ($v1, $v2)  be the first two numbers of sw_vers.
    # If $v1 > 10, then it is big sur or above and we set $ver be $v1 + 5
    # Otherwise, $ver is set to $v2.

chomp ($osversion = `sw_vers -productVersion`);
($v1, $v2) = (split (/\./, $osversion)) [0,1];

if ($v1 > 10) {
    $ver = $v1 + 5; 	# translate version to a single value
}
else {
    $ver = $v2;
}


$ver > 9 || die "\nThis sshproc program is for Yosemite forward.\n\n";






    # *********************************
    # START WORK
    # *********************************

    # Get sshd status.

$id = "com.openssh.sshd";
$sdisable = $sout = "";
$sshddisable = $sshdout = "";

    # 
    # Is sshd disabled or booted out?
    #

$sdisable = `launchctl print-disabled system | grep $id | grep true`;
$sout     = `launchctl print system/$id 2> /dev/null`;

if ($sdisable ne "") {
    $sshddisable = "y";
}
if ($sout eq "") {
    $sshdout = "y";
}



    # =====================
    # Start up sshd
    # =====================

if ($start eq "y") {
    if ($sshddisable eq "y") {
	print "\nSshd appear to be disabled.\n".
              "I will try to enable it.\n";
	`launchctl enable system/com.openssh.sshd`;
    }
    if ($sshdout eq "y") {
	print "\nSshd appear to be booted out.\n".
              "I will try to boot it up.\n";
	`launchctl bootstrap system /System/Library/LaunchDaemons/ssh.plist`;
    }

    print "\nSshd should now be enabled and booted up.\n".
	  "If sshd still does not work, it may be wedged.  Try doing\n".
          "sshproc restart  to fix it.\n\n";
}


    # =====================
    # Restart sshd
    # =====================

elsif ($restart eq "y") {
    print "\nI will bootout and/or disable sshd when appropriate.\n";

    if ($sshdout ne "y") {
        `launchctl bootout system /System/Library/LaunchDaemons/ssh.plist`;
    }
    if ($sshddisable ne "y") {
	`launchctl disable system/com.openssh.sshd`;
    }

    print "\nSshd is now booted out and disabled.\n";

    `launchctl enable system/com.openssh.sshd`;
    `launchctl bootstrap system /System/Library/LaunchDaemons/ssh.plist`;

    print "I have now enable sshd and booted it up.\n";
}


    # =====================
    # Stop sshd
    # =====================

elsif ($stop eq "y") {
    # sshd is up
    if ($sshdout ne "y") {
        `launchctl bootout system /System/Library/LaunchDaemons/ssh.plist`;
    }
    
    # sshd is enabled
    if ($sshddisable ne "y") {
	`launchctl disable system/com.openssh.sshd`;
    }

    print "\nI have bootout and disable sshd.\n";
}

    

    # =====================
    # Status of sshd
    # =====================

elsif ($status eq "y") {
    print "\nHere are the status of sshd.\n";

    if ($sshddisable eq "y") {
	print "   Sshd is disabled.\n";
    }
    else {
	print "   Sshd is enabled.\n";
    }

    if ($sshdout eq "y") {
	print "   Sshd is booted out.\n";
    }
    else {
	print "   Sshd is booted up.\n";
    }
}


print "\n";





    # *********************************
    # SPECIAL CASE
    # *********************************
    # For Yosemite, we print a note.

if ($ver < 11) {
    print "For Yosemite and lower, you may need to use the following.\n".
     "launchctl unload /System/Library/LaunchDaemons/ssh.plist\n".
     "launchctl load /System/Library/LaunchDaemons/ssh.plist";
}

