PHP Remote backup and restore



  • 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