Esempio n. 1
0
require "ipsec.inc";
if (!is_array($config['ipsec']['phase2'])) {
    $config['ipsec']['phase2'] = array();
}
$ipsec_status = array();
$a_phase2 =& $config['ipsec']['phase2'];
$status = ipsec_smp_dump_status();
if (is_array($status['query']) && $status['query']['ikesalist'] && $status['query']['ikesalist']['ikesa']) {
    foreach ($a_phase2 as $ph2ent) {
        ipsec_lookup_phase1($ph2ent, $ph1ent);
        $tunnel = array();
        if (!isset($ph2ent['disabled']) && !isset($ph1ent['disabled'])) {
            if (ipsec_phase1_status($status['query']['ikesalist']['ikesa'], $ph1ent['ikeid'])) {
                $tunnel['state'] = "up";
            } elseif (!isset($config['ipsec']['enable'])) {
                $tunnel['state'] = "disabled";
            } else {
                $tunnel['state'] = "down";
            }
            $tunnel['src'] = ipsec_get_phase1_src($ph1ent);
            $tunnel['endpoint'] = $ph1ent['remote-gateway'];
            $tunnel['local'] = ipsec_idinfo_to_text($ph2ent['localid']);
            $tunnel['remote'] = ipsec_idinfo_to_text($ph2ent['remoteid']);
            $tunnel['name'] = "{$ph2ent['descr']}";
            $ipsec_status['tunnel'][] = $tunnel;
        }
    }
}
$listtags = array("tunnel");
$xml = dump_xml_config($ipsec_status, "ipsec");
echo $xml;
function create_port($pkg)
{
    global $ports_dir, $tools_dir, $packages_dir, $product_name;
    if (isset($pkg['internal_name'])) {
        $pkg_name = $pkg['internal_name'];
    } else {
        $pkg_name = $pkg['name'];
    }
    if (empty($pkg_name)) {
        echo "!!! Error: Package name cannot be empty\n";
        exit(1);
    }
    if (!preg_match('/^[a-zA-Z0-9\\.\\-_]+$/', $pkg_name)) {
        echo "!!! Error: Package name '{$pkg_name}' is invalid\n";
        exit(1);
    }
    if (isset($pkg['port_category']) && !empty($pkg['port_category'])) {
        $category = $pkg['port_category'];
    } else {
        $category = 'sysutils';
    }
    $port_name_prefix = $product_name . '-pkg-';
    $port_name = $port_name_prefix . $pkg_name;
    $port_path = $ports_dir . '/' . $category . '/' . $port_name;
    if (is_dir($port_path)) {
        $_gb = exec("rm -rf {$port_path}");
    }
    mkdir($port_path . "/files", 0755, true);
    if (isset($pkg['descr']) && !empty($pkg['descr'])) {
        $pkg_descr = $pkg['descr'];
    } else {
        /* provide a generic description when it's not available */
        $pkg_descr = "{$pkg_name} {$product_name} package";
    }
    if (isset($pkg['pkginfolink']) && !empty($pkg['pkginfolink'])) {
        $pkg_descr .= "\n\nWWW: {$pkg['pkginfolink']}";
    }
    $pkg_descr .= "\n";
    file_put_contents($port_path . "/pkg-descr.tmp", $pkg_descr);
    unset($pkg_descr);
    $_gb = exec("/usr/bin/fmt -w 80 {$port_path}/pkg-descr.tmp > {$port_path}/pkg-descr 2>/dev/null");
    @unlink("{$port_path}/pkg-descr.tmp");
    if (isset($pkg['after_install_info']) && !empty($pkg['after_install_info'])) {
        file_put_contents($port_path . "/pkg-message", $pkg['after_install_info'] . "\n");
    }
    $pkg_install = file_get_contents($tools_dir . "/templates/pkg-install.in");
    file_put_contents($port_path . "/files/pkg-install.in", $pkg_install);
    unset($pkg_install);
    $pkg_deinstall = file_get_contents($tools_dir . "/templates/pkg-deinstall.in");
    file_put_contents($port_path . "/files/pkg-deinstall.in", $pkg_deinstall);
    unset($pkg_deinstall);
    $config_file = preg_replace('/^https*:\\/\\/[^\\/]+\\/packages\\//', '', $pkg['config_file']);
    if (!file_exists($packages_dir . '/' . $config_file)) {
        echo "!!! Error, config file {$config_file} not found\n";
        exit(1);
    }
    $pkg_config = parse_xml_config_pkg($packages_dir . '/' . $config_file, "packagegui");
    if (empty($pkg_config)) {
        echo "!!! Error, config file {$config_file} is invalid\n";
        exit(1);
    }
    if (!is_dir($port_path . '/files/usr/local/pkg')) {
        mkdir($port_path . '/files/usr/local/pkg', 0755, true);
    }
    copy($packages_dir . '/' . $config_file, $port_path . '/files/usr/local/pkg/' . basename($config_file));
    $plist_files = array('pkg/' . basename($config_file));
    $plist_dirs = array();
    $mkdirs = array('${MKDIR} ${STAGEDIR}${PREFIX}/pkg');
    $install = array('${INSTALL_DATA} -m 0644 ${FILESDIR}${PREFIX}/pkg/' . basename($config_file) . " \\\n\t\t" . '${STAGEDIR}${PREFIX}/pkg');
    if (!empty($pkg_config['additional_files_needed'])) {
        foreach ($pkg_config['additional_files_needed'] as $item) {
            if (is_array($item['item'])) {
                $item['item'] = $item['item'][0];
            }
            if (isset($item['do_not_add_to_port'])) {
                continue;
            }
            $file_relpath = preg_replace('/^https*:\\/\\/[^\\/]+\\/packages\\//', '', $item['item']);
            if (!file_exists($packages_dir . '/' . $file_relpath)) {
                echo "!!! Error: Additional file needed {$file_relpath} not found\n";
                exit(1);
            }
            if (!is_dir($port_path . '/files' . $item['prefix'])) {
                mkdir($port_path . '/files' . $item['prefix'], 0755, true);
            }
            copy($packages_dir . '/' . $file_relpath, $port_path . '/files' . $item['prefix'] . '/' . basename($file_relpath));
            fix_php_calls($port_path . '/files' . $item['prefix'] . '/' . basename($file_relpath));
            /* Remove /usr/local/ from prefix */
            $plist_entry = preg_replace('/^\\/usr\\/local\\//', '', $item['prefix']);
            $plist_entry = preg_replace('/\\/*$/', '', $plist_entry);
            if (substr($plist_entry, 0, 1) == '/' && !in_array("@dir {$plist_entry}", $plist_dirs)) {
                $plist_dirs[] = "@dir {$plist_entry}";
            }
            $plist_entry .= '/' . basename($item['item']);
            if (!in_array($plist_entry, $plist_files)) {
                $plist_files[] = $plist_entry;
            }
            unset($plist_entry);
            if (preg_match('/^\\/usr\\/local\\//', $item['prefix'])) {
                $mkdirs_entry = preg_replace('/^\\/usr\\/local\\//', '${PREFIX}/', $item['prefix']);
            } else {
                $mkdirs_entry = $item['prefix'];
            }
            $mkdirs_entry = preg_replace('/\\/*$/', '', $mkdirs_entry);
            $install_entry = '${INSTALL_DATA} ';
            if (isset($item['chmod']) && !empty($item['chmod'])) {
                $install_entry .= "-m {$item['chmod']} ";
            }
            $install_entry .= '${FILESDIR}' . $mkdirs_entry . '/' . basename($item['item']) . " \\\n\t\t";
            $install_entry .= '${STAGEDIR}' . $mkdirs_entry;
            $mkdirs_entry = '${MKDIR} ${STAGEDIR}' . $mkdirs_entry;
            if (!in_array($mkdirs_entry, $mkdirs)) {
                $mkdirs[] = $mkdirs_entry;
            }
            if (!in_array($install_entry, $install)) {
                $install[] = $install_entry;
            }
            unset($install_entry, $mkdirs_entry);
        }
    }
    if (!is_dir($port_path . '/files/usr/local/share/' . $port_name)) {
        mkdir($port_path . '/files/usr/local/share/' . $port_name, 0755, true);
    }
    $info['package'][] = $pkg;
    $info_xml = dump_xml_config($info, 'pfsensepkgs');
    file_put_contents($port_path . '/files/usr/local/share/' . $port_name . '/info.xml', $info_xml);
    unset($info, $info_xml);
    $plist_files[] = '%%DATADIR%%/info.xml';
    $mkdirs[] = '${MKDIR} ${STAGEDIR}${DATADIR}';
    $install[] = '${INSTALL_DATA} ${FILESDIR}${DATADIR}/info.xml ' . "\\\n\t\t" . '${STAGEDIR}${DATADIR}';
    $version = $pkg['version'];
    /* Detect PORTEPOCH */
    if (($pos = strpos($version, ',')) != FALSE) {
        $epoch = substr($version, $pos + 1);
        $version = substr($version, 0, $pos);
    }
    /* Detect PORTREVISION */
    if (($pos = strpos($version, '_')) != FALSE) {
        $revision = substr($version, $pos + 1);
        $version = substr($version, 0, $pos);
    }
    $makefile = array();
    $makefile[] = '# $FreeBSD$';
    $makefile[] = '';
    $makefile[] = "PORTNAME=\t{$port_name}";
    $makefile[] = "PORTVERSION=\t{$version}";
    if (isset($revision)) {
        $makefile[] = "PORTREVISION=\t{$revision}";
    }
    if (isset($epoch)) {
        $makefile[] = "PORTEPOCH=\t{$epoch}";
    }
    // XXX: use categories from xml */
    $makefile[] = "CATEGORIES=\t{$category}";
    $makefile[] = "MASTER_SITES=\t# empty";
    $makefile[] = "DISTFILES=\t# empty";
    $makefile[] = "EXTRACT_ONLY=\t# empty";
    $makefile[] = "";
    $makefile[] = "MAINTAINER=\tcoreteam@pfsense.org";
    // XXX: Provide comment on xml */
    $makefile[] = "COMMENT=\t{$product_name} package {$pkg_name}";
    if (isset($pkg['run_depends']) && !empty($pkg['run_depends'])) {
        $run_depends = array();
        foreach (preg_split('/\\s+/', trim($pkg['run_depends'])) as $depend) {
            list($file_depend, $port_depend) = explode(':', $depend);
            $file_depend = '${LOCALBASE}/' . $file_depend;
            $port_depend = '${PORTSDIR}/' . $port_depend;
            $run_depends[] = $file_depend . ':' . $port_depend;
        }
        if (!empty($run_depends)) {
            $makefile[] = "";
            $first = true;
            foreach ($run_depends as $run_depend) {
                if ($first) {
                    $makefile_entry = "RUN_DEPENDS=\t" . $run_depend;
                    $first = false;
                } else {
                    $makefile_entry .= " \\\n\t\t" . $run_depend;
                }
            }
            $makefile[] = $makefile_entry;
            unset($makefile_entry);
        }
        unset($run_depends);
    }
    if (isset($pkg['lib_depends']) && !empty($pkg['lib_depends'])) {
        $lib_depends = array();
        foreach (preg_split('/\\s+/', trim($pkg['lib_depends'])) as $depend) {
            list($lib_depend, $port_depend) = explode(':', $depend);
            $port_depend = '${PORTSDIR}/' . $port_depend;
            $lib_depends[] = $lib_depend . ':' . $port_depend;
        }
        if (!empty($lib_depends)) {
            $makefile[] = "";
            $first = true;
            foreach ($lib_depends as $lib_depend) {
                if ($first) {
                    $makefile_entry = "LIB_DEPENDS=\t" . $lib_depend;
                    $first = false;
                } else {
                    $makefile_entry .= " \\\n\t\t" . $lib_depend;
                }
            }
            $makefile[] = $makefile_entry;
            unset($makefile_entry);
        }
        unset($run_depends);
    }
    if (isset($pkg['port_uses']) && !empty($pkg['port_uses'])) {
        $makefile[] = "";
        foreach (preg_split('/\\s+/', trim($pkg['port_uses'])) as $port_use) {
            $port_use = preg_replace('/=/', "=\t", $port_use);
            $makefile[] = $port_use;
        }
    }
    $conflicts = '';
    if (isset($pkg['noembedded'])) {
        $conflicts = $product_name . '-base-nanobsd-[0-9]*';
    }
    if (isset($pkg['conflicts']) && !empty($pkg['conflicts'])) {
        $conflicts = trim($conflicts . ' ' . $port_name_prefix . $pkg['conflicts'] . '-[0-9]*');
    }
    if (!empty($conflicts)) {
        $makefile[] = "";
        $makefile[] = "CONFLICTS=\t" . $conflicts;
    }
    $makefile[] = "";
    $makefile[] = "NO_BUILD=\tyes";
    $makefile[] = "NO_MTREE=\tyes";
    $makefile[] = "";
    $makefile[] = "SUB_FILES=\tpkg-install pkg-deinstall";
    $makefile[] = "SUB_LIST=\tPORTNAME=\${PORTNAME}";
    $makefile[] = "";
    $makefile[] = "do-extract:";
    $makefile[] = "\t\${MKDIR} \${WRKSRC}";
    $makefile[] = "";
    $makefile[] = "do-install:";
    foreach ($mkdirs as $item) {
        $makefile[] = "\t" . $item;
    }
    foreach ($install as $item) {
        $makefile[] = "\t" . $item;
    }
    $makefile[] = "";
    $makefile[] = ".include <bsd.port.mk>";
    file_put_contents($port_path . '/Makefile', implode("\n", $makefile) . "\n");
    unset($makefile);
    file_put_contents($port_path . '/pkg-plist', implode("\n", $plist_files) . "\n");
    if (!empty($plist_dirs)) {
        file_put_contents($port_path . '/pkg-plist', implode("\n", $plist_dirs) . "\n", FILE_APPEND);
    }
    unset($plist_files, $plist_dirs);
}