From 6f73c36226d8583b90ddcff50850701c06de2ee2 Mon Sep 17 00:00:00 2001 From: Renato Botelho Date: Tue, 25 Aug 2015 08:21:15 -0300 Subject: Import build scripts from pfSense-tools repo --- tools/scripts/generate-privdefs.php | 216 +++++++++++++++ tools/scripts/update_package_pfPorts.php | 449 +++++++++++++++++++++++++++++++ 2 files changed, 665 insertions(+) create mode 100755 tools/scripts/generate-privdefs.php create mode 100755 tools/scripts/update_package_pfPorts.php (limited to 'tools/scripts') diff --git a/tools/scripts/generate-privdefs.php b/tools/scripts/generate-privdefs.php new file mode 100755 index 0000000..917a94d --- /dev/null +++ b/tools/scripts/generate-privdefs.php @@ -0,0 +1,216 @@ +#!/usr/local/bin/php -f +/usr/local/www + * directory and builds a privilege definition file + * based on the embedded metadata tags. For more info + * please see /etc/inc/meta.inc + */ + +if (count($argv) < 2) { + echo "usage: generate-privdefs \n"; + echo "\n"; + echo "This utility generates privilege definitions and writes them to\n"; + echo "'/etc/inc/priv.defs.inc'. The parameter should\n"; + echo "be specified as your base pfSense working directory.\n"; + echo "\n"; + echo "Examples:\n"; + echo "#generate-privdefs /\n"; + echo "#generate-privdefs /home/pfsense/RELENG_1/pfSense/\n"; + echo "\n"; + exit -1; +} + +$prefix = $argv[1]; +if (!file_exists($prefix)) { + echo "prefix {$prefix} is invalid"; + exit -1; +} + +$metainc = $prefix."etc/inc/meta.inc"; + +if (!file_exists($metainc)) { + echo "unable to locate {$metainc} file\n"; + exit -1; +} + +require_once($metainc); + +echo "--Locating www php files--\n"; + +$path = $prefix."/usr/local/www"; +list_phpfiles($path, $found); + +echo "--Gathering privilege metadata--\n"; + +$data; +foreach ($found as $fname) + read_file_metadata($path."/".$fname, $data, "PRIV"); + +echo "--Generating privilege definitions--\n"; +$privdef = $prefix."etc/inc/priv.defs.inc"; + +$fp = fopen($privdef,"w"); +if (!$fp) { + echo "unable to open {$privdef}\n"; + exit -2; +} + +$pdata; +$pdata = " $tags) { + + foreach ($tags as $tname => $vals) { + + $ident = ""; + $name = ""; + $descr = ""; + $match = array(); + + foreach ($vals as $vname => $vlist) { + + switch ($vname) { + case "IDENT": + $ident = $vlist[0]; + break; + case "NAME": + $name = $vlist[0]; + break; + case "DESCR": + $descr = $vlist[0]; + break; + case "MATCH": + $match = $vlist; + break; + } + } + + if (!$ident) { + echo "invalid IDENT in {$fname} privilege\n"; + continue; + } + + if (!count($match)) { + echo "invalid MATCH in {$fname} privilege\n"; + continue; + } + + $pdata .= "\$priv_list['{$ident}'] = array();\n"; + $pdata .= "\$priv_list['{$ident}']['name'] = \"WebCfg - {$name}\";\n"; + $pdata .= "\$priv_list['{$ident}']['descr'] = \"{$descr}\";\n"; + $pdata .= "\$priv_list['{$ident}']['match'] = array();\n"; + + foreach ($match as $url) + $pdata .= "\$priv_list['{$ident}']['match'][] = \"{$url}\";\n"; + + $pdata .= "\n"; + } +} + +$pdata .= "\n"; +$pdata .= "\$priv_rmvd = array();\n"; +$pdata .= "\n"; + +$pdata .= "?>\n"; +fwrite($fp, $pdata); + +fclose($fp); + +/* + * TODO : Build additional functionality + * + +echo "--Checking for pages without privilege definitions--\n"; + +foreach ($found as $fname) { + $match = false; + foreach ($pages_current as $pname => $pdesc) { + if (!strcmp($pname,$fname)) { + $match = true; + break; + } + } + if (!$match) + echo "missing: $fname\n"; +} + +echo "--Checking for stale privilege definitions--\n"; + +foreach ($pages_current as $pname => $pdesc) { + $match = false; + foreach ($found as $fname) { + if (!strncmp($fname,$pname,strlen($fname))) { + $match = true; + break; + } + } + if (!$match) + echo "stale: $pname\n"; +} + + */ + +?> diff --git a/tools/scripts/update_package_pfPorts.php b/tools/scripts/update_package_pfPorts.php new file mode 100755 index 0000000..648ebca --- /dev/null +++ b/tools/scripts/update_package_pfPorts.php @@ -0,0 +1,449 @@ +#!/usr/local/bin/php -q + 0) { + echo "!!! Ignoring {$pkg['name']}, maximum version is {$pkg['maximum_version']}\n"; + continue; + } + } + + if (isset($pkg['required_version']) && !empty($pkg['required_version'])) { + if (version_compare_numeric($pfs_version, $pkg['required_version']) < 0) { + echo "!!! Ignoring {$pkg['name']}, required version is {$pkg['required_version']}\n"; + continue; + } + } + + create_port($pkg); +} + +function fix_php_calls($file) { + if (!file_exists($file)) { + return; + } + + if (!preg_match('/\.(php|inc)$/', $file)) { + return; + } + + $content = file_get_contents($file); + $new_content = preg_replace('/\/usr\/local\/bin\/php/', '/usr/local/bin/php-cgi', $content); + file_put_contents($file, $new_content); + unset($content, $new_content); +} + +function create_port($pkg) { + global $ports_dir, $tools_dir, $builder_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($builder_dir . "/templates/pkg-install.in"); + file_put_contents($port_path . "/files/pkg-install.in", $pkg_install); + unset($pkg_install); + + $pkg_deinstall = file_get_contents($builder_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; + } + } + if (isset($pkg['conflicts']) && !empty($pkg['conflicts'])) { + $makefile[] = ""; + $makefile[] = "CONFLICTS=\t" . $port_name_prefix . $pkg['conflicts'] . '-[0-9]*'; + } + $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 "; + + 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); +} + +function usage() { + global $argv; + echo "Usage: {$argv[0]} -x [-p ]\n"; + echo " Flags:\n"; + echo " -s Product version to pass to set_version.sh during chroot build\n"; + echo " -U Do NOT run build.sh --update-sources\n"; + echo " Examples:\n"; + echo " {$argv[0]} -x /home/packages/pkg_info.10.xml -p squid -s RELENG_2_2\n"; + exit; +} +?> -- cgit v1.1