[LON-CAPA-cvs] cvs: loncom / CrGrant.pl

foxr lon-capa-cvs@mail.lon-capa.org
Fri, 09 Jul 2004 09:11:48 -0000


foxr		Fri Jul  9 05:11:48 2004 EDT

  Modified files:              
    /loncom	CrGrant.pl 
  Log:
  Complete debugging of certificate granting script.
  
  
Index: loncom/CrGrant.pl
diff -u loncom/CrGrant.pl:1.3 loncom/CrGrant.pl:1.4
--- loncom/CrGrant.pl:1.3	Tue Jul  6 07:05:45 2004
+++ loncom/CrGrant.pl	Fri Jul  9 05:11:48 2004
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # CrGrant.pl  - Grant a loncapa SSL certificate.
 #
-# $Id: CrGrant.pl,v 1.3 2004/07/06 11:05:45 foxr Exp $
+# $Id: CrGrant.pl,v 1.4 2004/07/09 09:11:48 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -88,6 +88,7 @@
 my $loncapa_cert_dir;		# Name of target cert dir (from config)
 my $loncapa_hostcert_name;	# Name of host's signed cert file (config)
 my $loncapa_cacert_name;        # Name of the CA's certificate file (config)
+my $return_address;		# Email return address.
 
 #  Items I just need to know:
 
@@ -99,7 +100,7 @@
 
 # Debug/log support
 
-my $DEBUG=1;
+my $DEBUG=0;
 
 sub Debug {
     my $msg = shift;
@@ -183,7 +184,16 @@
     else {
 	die "LonCAPA configuration error: Can't read lonnetCertificateAuthority variable";
     }
+    #  Get the email address of the certificate manager:
+    #  this is the email return address:
 
+    if($perlvarref->{SSLEmail}) {
+	$return_address = $perlvarref->{SSLEmail};
+	Debug("Return address will be $return_address");
+    }
+    else {
+	die "LonCAPA configuration error can't read SSLEmail configuration item";
+    }
 
 }
 
@@ -315,6 +325,8 @@
 #
 # Implicit Outputs:
 #    A file named CertInstall.sh
+# Return
+#    Name of the file we created.
 #
 sub CreateInstallScript {
     open INSTALLER,">CertInstall.sh";
@@ -378,8 +390,8 @@
 
 echo done
 
-# rm -f \$CACERT
-# rm -f \$HOSTCERT
+rm -f \$CACERT
+rm -f \$HOSTCERT
 
 #    Do they want to restart loncapa:
 #
@@ -393,23 +405,105 @@
 echo
 read -p "Restart loncapa now [yN]?"  yesno
 
-if [ "{\$yesno:0:1}" = "Y" ] 
+if [ "\${yesno:0:1}" = "Y" -o "\${yesno:0:1}"  = "y" ] 
 then
    /etc/init.d/loncontrol restart
 fi
 BASH_TRAILER
 
     close INSTALLER;
-}
 
+    return "CertInstall.sh";
+}
+#
+#    Create a mime Email that consists of a cover letter of installation
+#    instructions and an attachment that is the installation script.
+# Parameters:
+#     script    - The name of the script that will be attached
+#                 to the email.
+#     send_address - Where the mail will be sent.
+# Returns:
+#     The MIME::Entity handle of the script.
+#
 sub CreateEmail {
-    return "Dummy message";	# Stub.
+    Debug("Creating Email");
+    my ($installer_file, $send_address) = @_;
+
+    #  The top level mime entity is the mail headers and the
+    #  cover letter:
+
+    my $mime_message = MIME::Entity->build(Type    => "multipart/mixed",
+					   From    => $return_address,
+					   To      => $send_address,
+					   Subject =>"LonCAPA certificates");
+    if(!$mime_message) {
+	die "Unable to create top level MIME Message";
+    }
+
+     $mime_message->attach(Data =>["  This email contains your lonCAPA SSL certificates.  These\n",
+     "certificates allow your system to interact with the world wide\n",
+     "cluster of LonCAPA systems, and allow you to access and share\n",
+     "public resources for courses you host.\n\n",
+     "   The certificates are shipped as a self installing shell script\n",
+     "To install these certificates:\n\n",
+     "1. Extract the attachment to this email message\n",
+     "2. Save the attachment where it can be recovered in case you need\n",
+     "   to re-install these certificates later on for some reason\n",
+     "3. As root execute the certificate request file:
+           . $installer_file\n",
+     "   (Note: If you used a Windows based email program to extract the\n",
+     "   this file and then tranferred it to your unix lonCAPA system you \n",
+     "   Will probably need to convert the file first e.g.: \n",
+     "     dos2unix $installer_file\n",
+     "     . $installer_file\n",
+     "   The installer file will install the certificates and ask you\n",
+     "   if you want to restart the LonCAPA system.  You must restart the\n",
+     "   LonCAPA system for it to use the new certificates.\n\n",
+     "      Thank you for choosing LonCAPA for your course delivery needs,\n",
+     "      The LonCAPA team.\n"]);
+
+    Debug("Main message body created");
+
+
+    #  Attach the certificate intaller:
+
+    $mime_message->attach(Type    => "text/plain",
+			  Path    => $installer_file);
+    Debug("Installer attached");
+
+    return $mime_message;
+
 }
 
+#
+#   Sends a mime message to an email address.
+# Parameters:
+#    message   - A MIME::Entity containing the message.
+# Implicit inputs:
+#   Mail is sent via /usr/lib/sendmail -t -oi -oem"
+#   This should work on all systems with a properly configured
+#   sendmail or compatible mail transfer agent.
 sub SendEmail {
-    my ($EmailAddress, $Message) = @_;
+    my ($message) =  @_;
+
+    Debug("Mailing");
+
+    open MAILPIPE, "| /usr/lib/sendmail -t -oi -oem" or 
+	die "Failed to open pipe to sendmail: $!";
+
+    $message->print(\*MAILPIPE);
+    Debug("Submitted to sendmail");
+    close MAILPIPE;
+}
+#
+#  Cleanup destroys the certificate file and its installer.
+#
+#
+sub Cleanup {
+    my ($installer) = @_;
+    unlink($installer);
+    unlink("hostcertificate.pem");
 }
-sub Cleanup {}
 
 
 #  Program entry point
@@ -435,9 +529,9 @@
     exit -1;
 }
 
-&CreateInstallScript;
-my $Message = &CreateEmail;
-&SendEmail($email_address, $Message);
-&Cleanup;
+my $script_name = &CreateInstallScript;
+my $Message = &CreateEmail($script_name, $email_address);
+&SendEmail($Message);
+&Cleanup($script_name);
 
 # POD documentation.