Navigation

    Netgate Discussion Forum
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search

    PHP Remote backup and restore

    General pfSense Questions
    1
    1
    743
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ?
      Guest last edited by

      Hello,

      I'm an admin for a LAN party here in Denmark, and we use pfSense as the router.
      I've lately been trying to improve on our program that generates our users for "Captive portal".
      We use the build in "User Manager" to manage all the users for "Captive Portal", the reason for this is the LAN is only 3 days, and a FreeRadius server seems rather overkill.
      We have around 400+ users every LAN, so it takes a lot of time doing it manually. Right now we have a program that connects with HTTPS and adds the users (takes around 5 minutes).

      So I came up with the idea of using the backup file instead, and I managed to login and download the "system"backup file  (with https), and modify it.
      But now I've gotten to the part where I have to upload the file, and it seems impossible! I'm using PHP and curl for login, download and upload, but it doesn't work..

      Here is my code:

      $username = urlencode('admin');
      $password = urlencode('password');
      $loginUrl = 'http://192.168.1.1/index.php';
      
      //init curl
      $ch = curl_init();
      
      //Set the URL to work with
      curl_setopt($ch, CURLOPT_URL, $loginUrl);
      
      // ENABLE HTTP POST
      curl_setopt($ch, CURLOPT_POST, 1);
      
      //Set the post parameters
      curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('login' => 'Login', 'usernamefld' => 'admin', 'passwordfld' => 'password')));
      
      //Handle cookies for the login
      curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
      
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      
      //execute the request (the login)
      $store = curl_exec($ch);
      
      //the login is now done and you can continue to get the
      //protected content.
      //set the URL to the protected file
      curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('Submit' => 'download', 'backuparea' => 'system', 'donotbackuprrd' => 'on')));
      curl_setopt($ch, CURLOPT_URL, "http://192.168.1.1/diag_backup.php");
      
      //execute the request
      $content = curl_exec($ch);
      file_put_contents('system455.xml', trim($content));
      ?>
      

      and here is the code for the upload:

      $username = urlencode('admin');
      $password = urlencode('password');
      $loginUrl = 'http://192.168.1.1/index.php';
      
      //init curl
      $ch = curl_init();
      
      //Set the URL to work with
      curl_setopt($ch, CURLOPT_URL, $loginUrl);
      
      // ENABLE HTTP POST
      curl_setopt($ch, CURLOPT_POST, 1);
      
      //Set the post parameters
      curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('login' => 'Login', 'usernamefld' => 'admin', 'passwordfld' => 'password')));
      
      //Handle cookies for the login
      curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
      
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      
      //execute the request (the login)
      $store = curl_exec($ch);
      
      $file = realpath('newsystem.xml');
      print_r($file);
      
      curl_setopt($ch,CURLOPT_HTTPHEADER,array("Expect:  "));
      curl_setopt($ch, CURLOPT_POSTFIELDS, array('donotbackuprrd' => 'on', 'Submit' => 'Restore configuration', 'restorearea' => 'system', 'conffile' => file_get_contents('newsystem.xml')));
      curl_setopt($ch, CURLOPT_URL, "http://192.168.1.1/diag_backup.php");
      
      $store = curl_exec($ch);
      
      print_r($store);
      ?>
      

      I should note, the code is very messy, AND the "newsystem.xml" is the file generated by another script. The file works fine if i upload it manually…

      Any help would be great!

      Sincerely,
      Johan

      1 Reply Last reply Reply Quote 0
      • First post
        Last post

      Products

      • Platform Overview
      • TNSR
      • pfSense
      • Appliances

      Services

      • Training
      • Professional Services

      Support

      • Subscription Plans
      • Contact Support
      • Product Lifecycle
      • Documentation

      News

      • Media Coverage
      • Press
      • Events

      Resources

      • Blog
      • FAQ
      • Find a Partner
      • Resource Library
      • Security Information

      Company

      • About Us
      • Careers
      • Partners
      • Contact Us
      • Legal
      Our Mission

      We provide leading-edge network security at a fair price - regardless of organizational size or network sophistication. We believe that an open-source security model offers disruptive pricing along with the agility required to quickly address emerging threats.

      Subscribe to our Newsletter

      Product information, software announcements, and special offers. See our newsletter archive to sign up for future newsletters and to read past announcements.

      © 2021 Rubicon Communications, LLC | Privacy Policy