<commit_before><?php

/*
 * Copyright (C) 2014 Deciso B.V.
 * Copyright (C) 2008 Shrew Soft Inc. <<EMAIL>>
 * Copyright (C) 2003-2005 <NAME> <<EMAIL>>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

require_once("guiconfig.inc");
require_once("interfaces.inc");
require_once("plugins.inc.d/ipsec.inc");

/**
 * combine ealgos and keylen_* tags
 */
function pconfig_to_ealgos($pconfig)
{
    $ealgos = [];
    if (isset($pconfig['ealgos'])) {
        foreach (ipsec_p2_ealgos() as $algo_name => $algo_data) {
            if (in_array($algo_name, $pconfig['ealgos'])) {
                $ealgos[] = ['name' => $algo_name];
            }
        }
    }
    return $ealgos;
}

function ealgos_to_pconfig(& $ealgos, & $pconfig)
{
    $p2_ealgos = ipsec_p2_ealgos();
    $pconfig['ealgos'] = [];
    foreach ($ealgos as $cnf_algo_data) {
        foreach ($p2_ealgos as $algo_name => $algo_data) {
            if ($algo_name == $cnf_algo_data['name']) {
                $pconfig['ealgos'][] = $algo_name;
            } elseif ($algo_data['name'] == $cnf_algo_data['name']) {
                // XXX: extract and convert legacy encryption-algorithm-option setting
                if ($cnf_algo_data['keylen'] == $algo_data['keylen'] || $cnf_algo_data['keylen'] == "auto") {
                    $pconfig['ealgos'][] = $algo_name;
                }
            }
        }
    }

    return $ealgos;
}

/**
 * convert <tag>id_address, <tag>id_netbits, <tag>id_type
 * to type/address/netbits structure
 */
function pconfig_to_idinfo($prefix, $pconfig)
{
    $type = isset($pconfig[$prefix."id_type"]) ? $pconfig[$prefix."id_type"] : null;
    $address = isset($pconfig[$prefix."id_address"]) ? $pconfig[$prefix."id_address"] : null;
    $netbits = isset($pconfig[$prefix."id_netbits"]) ? $pconfig[$prefix."id_netbits"] : null;

    switch ($type) {
        case "address":
            return array('type' => $type, 'address' => $address);
        case "network":
            return array('type' => $type, 'address' => $address, 'netbits' => $netbits);
        default:
            return array('type' => $type );
    }
}

/**
 * reverse pconfig_to_idinfo from $idinfo array to $pconfig
 */
function idinfo_to_pconfig($prefix, $idinfo, & $pconfig)
{
    switch ($idinfo['type']) {
        case "address":
            $pconfig[$prefix."id_type"] = $idinfo['type'];
            $pconfig[$prefix."id_address"] = $idinfo['address'];
            break;
        case "network":
            $pconfig[$prefix."id_type"] = $idinfo['type'];
            $pconfig[$prefix."id_address"] = $idinfo['address'];
            $pconfig[$prefix."id_netbits"] = $idinfo['netbits'];
            break;
        default:
            $pconfig[$prefix."id_type"] = $idinfo['type'];
            break;
    }
}

/**
 * search phase 2 entries for record with uniqid
 */
function getIndexByUniqueId($uniqid)
{
    global $config;
    $p2index = null;
    if ($uniqid != null) {
        foreach ($config['ipsec']['phase2'] as $idx => $ph2) {
            if ($ph2['uniqid'] == $uniqid) {
                $p2index = $idx;
                break;
            }
        }
    }
    return $p2index;
}

config_read_array('ipsec', 'client');
config_read_array('ipsec', 'phase2');

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    // lookup p2index
    if (!empty($_GET['dup'])) {
        $p2index = getIndexByUniqueId($_GET['dup']);
    } elseif (!empty($_GET['p2index'])) {
        $p2index = getIndexByUniqueId($_GET['p2index']);
    } else {
        $p2index = null;
    }
    // initialize form data
    $pconfig = array();

    $phase2_fields = "ikeid,mode,descr,uniqid,proto,hash-algorithm-option,pfsgroup,lifetime,pinghost,protocol,spd,";
    $phase2_fields .= "tunnel_local,tunnel_remote";
    if ($p2index !== null) {
        // 1-on-1 copy
        foreach (explode(",", $phase2_fields) as $fieldname) {
            $fieldname = trim($fieldname);
            if (isset($config['ipsec']['phase2'][$p2index][$fieldname])) {
                $pconfig[$fieldname] = $config['ipsec']['phase2'][$p2index][$fieldname];
            } elseif (!isset($pconfig[$fieldname])) {
                // initialize element
                $pconfig[$fieldname] = null;
            }
        }
        // fields with some kind of logic
        $pconfig['disabled'] = isset($config['ipsec']['phase2'][$p2index]['disabled']);

        idinfo_to_pconfig("local", $config['ipsec']['phase2'][$p2index]['localid'], $pconfig);
        idinfo_to_pconfig("remote", $config['ipsec']['phase2'][$p2index]['remoteid'], $pconfig);
        if (!empty($config['ipsec']['phase2'][$p2index]['encryption-algorithm-option'])) {
            ealgos_to_pconfig($config['ipsec']['phase2'][$p2index]['encryption-algorithm-option'], $pconfig);
        } else {
            $pconfig['ealgos'] = [];
        }

        if (isset($config['ipsec']['phase2'][$p2index]['mobile'])) {
            $pconfig['mobile'] = true;
        }

        if (!empty($_GET['dup'])) {
            $pconfig['uniqid'] = uniqid();
        }
    } else {
        if (isset($_GET['ikeid'])) {
            $pconfig['ikeid'] = $_GET['ikeid'];
        }
        /* defaults */
        $pconfig['localid_type'] = "lan";
        $pconfig['remoteid_type'] = "network";
        $pconfig['protocol'] = "esp";
        $pconfig['ealgos'] = ['aes256gcm16'];
        $pconfig['hash-algorithm-option'] = ['hmac_sha256'];
        $pconfig['pfsgroup'] = "0";
        $pconfig['lifetime'] = "3600";
        $pconfig['uniqid'] = uniqid();

        /* mobile client */
        if (isset($_GET['mobile'])) {
            $pconfig['mobile'] = true;
        }
        // init empty
        foreach (explode(",", $phase2_fields) as $fieldname) {
            $fieldname = trim($fieldname);
            if (!isset($pconfig[$fieldname])) {
                $pconfig[$fieldname] = null;
            }
        }
    }
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!empty($_POST['uniqid'])) {<commit_msg>VPN / IPsec - mobile property passing in phase 2, make sure we can add a mobile phase 2 again, at some point we should remove the phase 2 mobile attribute for being a copy of its parent. for https://github.com/opnsense/core/issues/5598<commit_after><?php

/*
 * Copyright (C) 2014 Deciso B.V.
 * Copyright (C) 2008 Shrew Soft Inc. <<EMAIL>>
 * Copyright (C) 2003-2005 <NAME> <<EMAIL>>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

require_once("guiconfig.inc");
require_once("interfaces.inc");
require_once("plugins.inc.d/ipsec.inc");

/**
 * combine ealgos and keylen_* tags
 */
function pconfig_to_ealgos($pconfig)
{
    $ealgos = [];
    if (isset($pconfig['ealgos'])) {
        foreach (ipsec_p2_ealgos() as $algo_name => $algo_data) {
            if (in_array($algo_name, $pconfig['ealgos'])) {
                $ealgos[] = ['name' => $algo_name];
            }
        }
    }
    return $ealgos;
}

function ealgos_to_pconfig(& $ealgos, & $pconfig)
{
    $p2_ealgos = ipsec_p2_ealgos();
    $pconfig['ealgos'] = [];
    foreach ($ealgos as $cnf_algo_data) {
        foreach ($p2_ealgos as $algo_name => $algo_data) {
            if ($algo_name == $cnf_algo_data['name']) {
                $pconfig['ealgos'][] = $algo_name;
            } elseif ($algo_data['name'] == $cnf_algo_data['name']) {
                // XXX: extract and convert legacy encryption-algorithm-option setting
                if ($cnf_algo_data['keylen'] == $algo_data['keylen'] || $cnf_algo_data['keylen'] == "auto") {
                    $pconfig['ealgos'][] = $algo_name;
                }
            }
        }
    }

    return $ealgos;
}

/**
 * convert <tag>id_address, <tag>id_netbits, <tag>id_type
 * to type/address/netbits structure
 */
function pconfig_to_idinfo($prefix, $pconfig)
{
    $type = isset($pconfig[$prefix."id_type"]) ? $pconfig[$prefix."id_type"] : null;
    $address = isset($pconfig[$prefix."id_address"]) ? $pconfig[$prefix."id_address"] : null;
    $netbits = isset($pconfig[$prefix."id_netbits"]) ? $pconfig[$prefix."id_netbits"] : null;

    switch ($type) {
        case "address":
            return array('type' => $type, 'address' => $address);
        case "network":
            return array('type' => $type, 'address' => $address, 'netbits' => $netbits);
        default:
            return array('type' => $type );
    }
}

/**
 * reverse pconfig_to_idinfo from $idinfo array to $pconfig
 */
function idinfo_to_pconfig($prefix, $idinfo, & $pconfig)
{
    switch ($idinfo['type']) {
        case "address":
            $pconfig[$prefix."id_type"] = $idinfo['type'];
            $pconfig[$prefix."id_address"] = $idinfo['address'];
            break;
        case "network":
            $pconfig[$prefix."id_type"] = $idinfo['type'];
            $pconfig[$prefix."id_address"] = $idinfo['address'];
            $pconfig[$prefix."id_netbits"] = $idinfo['netbits'];
            break;
        default:
            $pconfig[$prefix."id_type"] = $idinfo['type'];
            break;
    }
}

/**
 * search phase 2 entries for record with uniqid
 */
function getIndexByUniqueId($uniqid)
{
    global $config;
    $p2index = null;
    if ($uniqid != null) {
        foreach ($config['ipsec']['phase2'] as $idx => $ph2) {
            if ($ph2['uniqid'] == $uniqid) {
                $p2index = $idx;
                break;
            }
        }
    }
    return $p2index;
}

config_read_array('ipsec', 'client');
config_read_array('ipsec', 'phase2');

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    // lookup p2index
    if (!empty($_GET['dup'])) {
        $p2index = getIndexByUniqueId($_GET['dup']);
    } elseif (!empty($_GET['p2index'])) {
        $p2index = getIndexByUniqueId($_GET['p2index']);
    } else {
        $p2index = null;
    }
    // initialize form data
    $pconfig = array();

    $phase2_fields = "ikeid,mode,descr,uniqid,proto,hash-algorithm-option,pfsgroup,lifetime,pinghost,protocol,spd,";
    $phase2_fields .= "tunnel_local,tunnel_remote";
    if ($p2index !== null) {
        // 1-on-1 copy
        foreach (explode(",", $phase2_fields) as $fieldname) {
            $fieldname = trim($fieldname);
            if (isset($config['ipsec']['phase2'][$p2index][$fieldname])) {
                $pconfig[$fieldname] = $config['ipsec']['phase2'][$p2index][$fieldname];
            } elseif (!isset($pconfig[$fieldname])) {
                // initialize element
                $pconfig[$fieldname] = null;
            }
        }
        // fields with some kind of logic
        $pconfig['disabled'] = isset($config['ipsec']['phase2'][$p2index]['disabled']);

        idinfo_to_pconfig("local", $config['ipsec']['phase2'][$p2index]['localid'], $pconfig);
        idinfo_to_pconfig("remote", $config['ipsec']['phase2'][$p2index]['remoteid'], $pconfig);
        if (!empty($config['ipsec']['phase2'][$p2index]['encryption-algorithm-option'])) {
            ealgos_to_pconfig($config['ipsec']['phase2'][$p2index]['encryption-algorithm-option'], $pconfig);
        } else {
            $pconfig['ealgos'] = [];
        }

        if (isset($config['ipsec']['phase2'][$p2index]['mobile'])) {
            $pconfig['mobile'] = true;
        }

        if (!empty($_GET['dup'])) {
            $pconfig['uniqid'] = uniqid();
        }
    } else {
        if (isset($_GET['ikeid'])) {
            $pconfig['ikeid'] = $_GET['ikeid'];
        }
        /* defaults */
        $pconfig['localid_type'] = "lan";
        $pconfig['remoteid_type'] = "network";
        $pconfig['protocol'] = "esp";
        $pconfig['ealgos'] = ['aes256gcm16'];
        $pconfig['hash-algorithm-option'] = ['hmac_sha256'];
        $pconfig['pfsgroup'] = "0";
        $pconfig['lifetime'] = "3600";
        $pconfig['uniqid'] = uniqid();

        /* mobile client */
        foreach ($config['ipsec']['phase1'] as $phase1ent) {
            if ($phase1ent['ikeid'] == $pconfig['ikeid']) {
                $pconfig['mobile'] = true;
                break;
            }
        }

        // init empty
        foreach (explode(",", $phase2_fields) as $fieldname) {
            $fieldname = trim($fieldname);
            if (!isset($pconfig[$fieldname])) {
                $pconfig[$fieldname] = null;
            }
        }
    }
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!empty($_POST['uniqid'])) {
{'fraction_non_alphanumeric': 0.16491275714576686, 'fraction_numerical': 0.010455563853622106, 'mean_word_length': 2.75765306122449, 'pattern_counts': {'":': 8, '<': 21, '<?xml version=': 0, '>': 39, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3369614', 'n_tokens_mistral': 4747, 'n_tokens_neox': 4590, 'n_words': 1033}