[LON-CAPA-cvs] cvs: loncom / lond

foxr lon-capa-cvs@mail.lon-capa.org
Mon, 22 Dec 2003 12:01:54 -0000


foxr		Mon Dec 22 07:01:54 2003 EDT

  Modified files:              
    /loncom	lond 
  Log:
  Edit file:
  - Add top level edit script validation.
  
  
Index: loncom/lond
diff -u loncom/lond:1.167 loncom/lond:1.168
--- loncom/lond:1.167	Mon Dec 22 06:29:58 2003
+++ loncom/lond	Mon Dec 22 07:01:54 2003
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # lond "LON Daemon" Server (port "LOND" 5663)
 #
-# $Id: lond,v 1.167 2003/12/22 11:29:58 foxr Exp $
+# $Id: lond,v 1.168 2003/12/22 12:01:54 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -52,7 +52,7 @@
 my $status='';
 my $lastlog='';
 
-my $VERSION='$Revision: 1.167 $'; #' stupid emacs
+my $VERSION='$Revision: 1.168 $'; #' stupid emacs
 my $remoteVERSION;
 my $currenthostid;
 my $currentdomainid;
@@ -476,18 +476,67 @@
     }
     return 'ok';
 }
+#   Validate a line in a configuration file edit script:
+#   Validation includes:
+#     - Ensuring the command is valid.
+#     - Ensuring the command has sufficient parameters
+#   Parameters:
+#     scriptline - A line to validate (\n has been stripped for what it's worth).
 #
-#   Called to edit a file
+#   Return:
+#      0     - Invalid scriptline.
+#      1     - Valid scriptline
+#  NOTE:
+#     Only the command syntax is checked, not the executability of the
+#     command.
+#
+sub isValidEditCommand {
+    my $scriptline = shift;
+
+    #   Line elements are pipe separated:
+
+    my ($command, $key, $newline)  = split(/\|/, $scriptline);
+    &logthis('<font color="green"> isValideditCommand checking: '.
+	     "Command = '$command', Key = '$key', Newline = '$newline' </font>\n");
+    
+    if ($command eq "delete") {
+	#
+	#   key with no newline.
+	#
+	if( ($key eq "") || ($newline ne "")) {
+	    return 0;		# Must have key but no newline.
+	} else {
+	    return 1;		# Valid syntax.
+	}
+    } elsif (($command eq "append") || ($command eq "replace")) {
+	#
+	#   key and newline:
+	#
+	if (($key eq "") || ($newline eq "")) {
+	    return 0;
+	} else {
+	    return 1;
+	}
+    } else {
+	return 0;		# Invalid command.
+    }
+    return 0;			# Should not get here!!!
+}
+
+#   
+#
+#   Called to edit a configuration table  file
 #   Parameters:
 #      request           - The entire command/request sent by lonc or lonManage
 #   Return:
 #      The reply to send to the client.
+#
 sub EditFile {
     my $request = shift;
 
     #  Split the command into it's pieces:  edit:filetype:script
 
-    my ($request, $filetype, $script) = split(":", $request);
+    my ($request, $filetype, $script) = split(/:/, $request,3);	# : in script
 
     #  Check the pre-coditions for success:
 
@@ -500,6 +549,15 @@
     }
 
     #   Split the edit script and check it's validity.
+
+    my @scriptlines = split(/\n/, $script);  # one line per element.
+    my $linecount   = scalar(@scriptlines);
+    for(my $i = 0; $i < $linecount; $i++) {
+	chomp($scriptlines[$i]);
+	if(!isValidEditCommand($scriptlines[$i])) {
+	    return "error:edit with bad script line: '$scriptlines[$i]' \n";
+	}
+    }
 
     #   Execute the edit operation.