From 82c0cd30275f8cbfcec95cdc129e4f88cea12f0f Mon Sep 17 00:00:00 2001
From: Stig Thormodsrud <stig@vyatta.com>
Date: Tue, 24 Jun 2008 17:32:50 -0700
Subject: Fix 3143: Unable to save/load config from a remote server.

---
 scripts/vyatta-load-config.pl | 76 ++++++++++++++++++++++++++++++++++++++-----
 scripts/vyatta-save-config.pl | 69 ++++++++++++++++++++++++++++++++-------
 2 files changed, 125 insertions(+), 20 deletions(-)

diff --git a/scripts/vyatta-load-config.pl b/scripts/vyatta-load-config.pl
index 9513ffa..29d4dec 100755
--- a/scripts/vyatta-load-config.pl
+++ b/scripts/vyatta-load-config.pl
@@ -29,23 +29,83 @@ my $etcdir = $ENV{vyatta_sysconfdir};
 my $sbindir = $ENV{vyatta_sbindir};
 my $bootpath = $etcdir . "/config";
 my $load_file = $bootpath . "/config.boot";
+my $url_tmp_file = $bootpath . "/config.boot.$$";
 
 if ($#ARGV > 0) {
   print "Usage: load <config_file_name>\n";
   exit 1;
 }
 
+my $mode = 'local';
+my $proto;
+
 if (defined($ARGV[0])) {
-   $load_file = $ARGV[0];
-   if (!($load_file =~ /^\//)) {
-      # relative path
-      $load_file = "$bootpath/$load_file";
-   }
+  $load_file = $ARGV[0];
 }
 
-if (!open(CFG, "<$load_file")) {
-  print "Cannot open configuration file $load_file\n";
-  exit 1;
+if ($load_file =~ /^[^\/]\w+:\//) {
+  if ($load_file =~ /^(\w+):\/\/\w/) {
+    $mode = 'url';
+    $proto = lc($1);
+    if ($proto eq 'tftp') {
+    } elsif ($proto eq 'ftp') {
+    } elsif ($proto eq 'http') {
+    } elsif ($proto eq 'scp') {
+    } else {
+      print "Invalid url protocol [$proto]\n";
+      exit 1;
+    }
+  } else {
+    print "Invalid url [$load_file]\n";
+    exit 1;
+  }
+}
+
+if ($mode eq 'local' and !($load_file =~ /^\//)) {
+  # relative path
+  $load_file = "$bootpath/$load_file";
+}
+
+if ($mode eq 'local') {
+  if (!open(CFG, "<$load_file")) {
+    print "Cannot open configuration file $load_file\n";
+    exit 1;
+  }
+} elsif ($mode eq 'url') {
+  if (! -f '/usr/bin/curl') {
+    print "Package [curl] not installed\n";
+    exit 1;
+  }
+  if ($proto eq 'http') {
+    #
+    # error codes are send back in html, so 1st try a header
+    # and look for "HTTP/1.1 200 OK"
+    #
+    my $rc = `curl -q -I $load_file 2>&1`;
+    if ($rc =~ /HTTP\/\d+\.?\d\s+(\d+)\s+(.*)$/mi) {
+      my $rc_code   = $1;
+      my $rc_string = $2;
+      if ($rc_code == 200) {
+	# good resonse
+      } else {
+	print "http error: [$rc_code] $rc_string\n";
+        exit 1;
+      }
+    } else {
+      print "Error: $rc\n";
+      exit 1;
+    }
+  }
+  my $rc = system("curl -# -o $url_tmp_file $load_file");
+  if ($rc) {
+      print "Can not open remote configuration file $load_file\n";
+      exit 1;
+  }
+  if (!open(CFG, "<$url_tmp_file")) {  
+    print "Cannot open configuration file $load_file\n";
+    exit 1;
+  }
+  $load_file = $url_tmp_file;
 }
 while (<CFG>) {
   if (/\/\*XORP Configuration File, v1.0\*\//) {
diff --git a/scripts/vyatta-save-config.pl b/scripts/vyatta-save-config.pl
index c759503..2c02766 100755
--- a/scripts/vyatta-save-config.pl
+++ b/scripts/vyatta-save-config.pl
@@ -26,6 +26,8 @@ use VyattaConfigOutput;
 my $etcdir = "/opt/vyatta/etc";
 my $bootpath = $etcdir . "/config";
 my $save_file = $bootpath . "/config.boot";
+my $url_tmp_file = $bootpath . "/config.boot.$$";
+
 
 if ($#ARGV > 0) {
   print "Usage: save [config_file_name]\n";
@@ -34,25 +36,68 @@ if ($#ARGV > 0) {
 
 if (defined($ARGV[0])) {
   $save_file = $ARGV[0];
-  if (!($save_file =~ /^\//)) {
-    # relative path
-    $save_file = "$bootpath/$save_file";
+}
+
+my $mode = 'local';
+my $proto;
+
+if ($save_file =~ /^[^\/]\w+:\//) {
+  if ($save_file =~ /^(\w+):\/\/\w/) {
+    $mode = 'url';
+    $proto = lc($1);
+    if ($proto eq 'tftp') {
+    } elsif ($proto eq 'ftp') {
+    } elsif ($proto eq 'scp') {
+    } else {
+      print "Invalid url protocol [$proto]\n";
+      exit 1;
+    }
+  } else {
+    print "Invalid url [$save_file]\n";
+    exit 1;
   }
 }
 
-# this overwrites the file if it exists. we could create a backup first.
-if (! open(SAVE, ">$save_file")) {
-  print "Cannot open file '$save_file': $!\n";
-  exit 1;
+if ($mode eq 'local' and !($save_file =~ /^\//)) {
+  # relative path
+  $save_file = "$bootpath/$save_file";
+}
+
+my $version_str = `/opt/vyatta/sbin/vyatta_current_conf_ver.pl`;
+print "Saving configuration to '$save_file'...\n";
+
+if ($mode eq 'local') {
+  # this overwrites the file if it exists. we could create a backup first.
+  if (! open(SAVE, ">$save_file")) {
+    print "Cannot open file '$save_file': $!\n";
+    exit 1;
+  }
+} elsif ($mode eq 'url') {
+  if (! -f '/usr/bin/curl') {
+    print "Package [curl] not installed\n";
+    exit 1;
+  }
+  if (! open(SAVE, ">$url_tmp_file")) {
+    print "Cannot open file '$url_tmp_file': $!\n";
+    exit 1;
+  }
 }
 
-print "Saving configuration to '$save_file'...";
 select SAVE;
 VyattaConfigOutput::set_show_all(1);
 VyattaConfigOutput::outputActiveConfig();
-my $version_str = `/opt/vyatta/sbin/vyatta_current_conf_ver.pl`;
-print SAVE $version_str;
-select STDOUT;
-print "\nDone\n";
+print $version_str;
 close SAVE;
+select STDOUT;
+
+if ($mode eq 'url') {
+  my $rc = system("curl -# -T $url_tmp_file $save_file");
+  system("rm -f $url_tmp_file");
+  if ($rc) {
+    print "Error saving $save_file\n";
+    exit 1;
+  }
+}
+
+print "Done\n";
 exit 0;
-- 
cgit v1.2.3