From c194b9de2b4596753b127a55b327edd97fc71aae Mon Sep 17 00:00:00 2001
From: erkin <e.altunbas@vyos.io>
Date: Fri, 16 Apr 2021 04:16:32 +0300
Subject: T3356: Replace curl calls in vyatta-save-config.pl with calls to
 remote.py

---
 scripts/vyatta-save-config.pl | 54 +++++--------------------------------------
 1 file changed, 6 insertions(+), 48 deletions(-)

diff --git a/scripts/vyatta-save-config.pl b/scripts/vyatta-save-config.pl
index 19941a7..693c4da 100755
--- a/scripts/vyatta-save-config.pl
+++ b/scripts/vyatta-save-config.pl
@@ -57,11 +57,7 @@ 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') {
-        } elsif ($proto eq 'sftp') {
-        } else {
+	if (grep { lc($topdir) ne $_ } ("ftp", "tftp", "http", "https", "scp", "sftp")) {
             print "Invalid url protocol [$proto]\n";
             exit 1;
         }
@@ -86,7 +82,7 @@ print "Saving configuration to '$shortened_save_file'...\n";
 my $save;
 if ($mode eq 'local') {
 
-    # first check if this file exists, and if so ensure this is a config file.
+    # First check if this file exists, and if so ensure this is a config file.
     if (-e $save_file) {
         my $result = `grep -e ' === vyatta-config-version:' -e '// vyos-config-version:' $save_file`;
         if (!defined $result || length($result) == 0) {
@@ -94,15 +90,11 @@ if ($mode eq 'local') {
             exit 1;
         }
     }
+    # TODO: This overwrites the file if it exists. We should create a backup first.
+    open $save, '>', $save_file or die "Can not open file '$save_file': $!\n";
 
-    # this overwrites the file if it exists. we could create a backup first.
-    open $save, '>', $save_file
-        or die "Can not open file '$save_file': $!\n";
 } elsif ($mode eq 'url') {
-    die "Package [curl] not installed\n" unless (-f '/usr/bin/curl');
-
-    open $save, '>', $url_tmp_file
-        or die "Cannot open file '$url_tmp_file': $!\n";
+    open $save, '>', $url_tmp_file or die "Cannot open file '$url_tmp_file': $!\n";
 }
 
 select $save;
@@ -122,42 +114,8 @@ fsync $save;
 close $save;
 
 if ($mode eq 'url') {
-
-    my $rc = 0;
-    if ($proto =~ /^(scp|sftp)$/){
-        $save_file =~ m/(?:scp|sftp):\/\/(.*?)\//;
-        my $host = $1;
-        my $user = getpwuid($<);
-        if ($host =~ m/(.*)@(.*)/) {
-            $user = $1;
-            $host = $2;
-        }
-
-        $rc = system("curl -u $user -# -T $url_tmp_file $save_file");
-        if($rc >> 8 == 51){
-            my $rsa_key = `ssh-keyscan -t rsa $host 2>/dev/null`;
-            print "The authenticity of host '$host' can't be established.\n";
-            my $fingerprint = `ssh-keygen -lf /dev/stdin <<< \"$rsa_key\" | awk {' print \$2 '}`;
-            chomp $fingerprint;
-            print "RSA key fingerprint is $fingerprint.\n";
-            if (prompt("Are you sure you want to continue connecting (yes/no) [Yes]? ", -tynd=>"y")) {
-                mkdir "$ENV{HOME}/.ssh/",0700 unless -d "$ENV{HOME}/.ssh";
-                open(my $known_hosts, ">>", "$ENV{HOME}/.ssh/known_hosts")
-                    or die "Cannot open known_hosts: $!";
-                print $known_hosts "$rsa_key\n";
-                close($known_hosts);
-                $rc = system("curl -u $user -# -T $url_tmp_file $save_file");
-                print "\n";
-            }
-        }
-    } else {
-        $rc = system("curl -# -T $url_tmp_file $save_file");
-    }
+    system("python3 -c 'from vyos.remote import upload; upload(\"$url_tmp_file\", \"$save_file\")'");
     system("rm -f $url_tmp_file");
-    if ($rc) {
-        print "Error saving $save_file\n";
-        exit 1;
-    }
 }
 
 print "Done\n";
-- 
cgit v1.2.3