stdout ); } $name_in_path = explode( '/', $cwd ); $site_url = array_intersect( array_column( $sites, 'site_url' ), $name_in_path ); if ( 1 === count( $site_url ) ) { $name = reset( $site_url ); $path = Site::find( $name ); if ( $path ) { $site_path = $path->site_fs_path; if ( substr( $cwd, 0, strlen( $site_path ) ) === $site_path ) { return $name; } } } } return false; } /** * Function to set the site-name in the args when ee is running in a site folder and the site-name has not been passed * in the args. If the site-name could not be found it will throw an error. * * @param array $args The passed arguments. * @param String $command The command passing the arguments to auto-detect site-name. * @param String $function The function passing the arguments to auto-detect site-name. * @param integer $arg_pos Argument position where Site-name will be present. * * @return array Arguments with site-name set. */ function auto_site_name( $args, $command, $function, $arg_pos = 0 ) { if ( isset( $args[ $arg_pos ] ) ) { $possible_site_name = $args[ $arg_pos ]; if ( substr( $possible_site_name, 0, 7 ) === 'http://' || substr( $possible_site_name, 0, 8 ) === 'https://' ) { $possible_site_name = str_replace( [ 'https://', 'http://' ], '', $possible_site_name ); } $url_path = parse_url( EE\Utils\remove_trailing_slash( $possible_site_name ), PHP_URL_PATH ); if ( Site::find( $url_path ) ) { return $args; } } $site_url = get_site_name(); if ( $site_url ) { if ( isset( $args[ $arg_pos ] ) ) { EE::error( $args[ $arg_pos ] . " is not a valid site-name. Did you mean `ee $command $function $site_url`?" ); } array_splice( $args, $arg_pos, 0, $site_url ); } else { EE::error( "Could not find the site you wish to run $command $function command on.\nEither pass it as an argument: `ee $command $function ` \nor run `ee $command $function` from inside the site folder." ); } return $args; } /** * Populate basic site info from db. * * @param bool $site_enabled_check Check if site is enabled. Throw error message if not enabled. * @param bool $exit_if_not_found Check if site exists. Throw error message if not, else return false. * @param bool $return_array Return array of data or object. * * @return mixed $site_data Site data from db. */ function get_site_info( $args, $site_enabled_check = true, $exit_if_not_found = true, $return_array = true ) { $site_url = \EE\Utils\remove_trailing_slash( $args[0] ); $data = Site::find( $site_url ); $array_data = ( array ) $data; $site_data = $return_array ? reset( $array_data ) : $data; if ( ! $data ) { if ( $exit_if_not_found ) { \EE::error( sprintf( 'Site %s does not exist.', $site_url ) ); } return false; } // Keeps this site's credentials out of the logs of whatever the command runs next. if ( method_exists( 'EE', 'obfuscate' ) ) { EE::obfuscate( [ $data->db_password, $data->db_root_password, $data->app_admin_password ] ); } if ( ! $data->site_enabled && $site_enabled_check ) { \EE::error( sprintf( 'Site %1$s is not enabled. Use `ee site enable %1$s` to enable it.', $data->site_url ) ); } return $site_data; } /** * Populate basic site info from db. * * @param array $domains Array of all domains. * * @return string $preferred_challenge Type of challenge preffered. */ function get_preferred_ssl_challenge( array $domains ) { foreach ( $domains as $domain ) { if ( preg_match( '/^\*/', $domain ) ) { return 'dns'; } } return get_config_value( 'preferred_ssl_challenge', '' ); } /** * Waits up to a minute for the global database to accept root logins. */ function wait_for_global_db() { $health_script = 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"exit"'; $db_script_path = \EE\Utils\get_temp_dir() . 'db_exec'; file_put_contents( $db_script_path, $health_script ); $mysql_unhealthy = true; EE::exec( sprintf( 'docker cp %s %s:/db_exec', $db_script_path, GLOBAL_DB_CONTAINER ) ); $count = 0; while ( $mysql_unhealthy ) { $mysql_unhealthy = ! EE::exec( sprintf( 'docker exec %s sh db_exec', GLOBAL_DB_CONTAINER ) ); if ( $count ++ > 60 ) { break; } sleep( 1 ); } } /** * Quotes a value as an SQL string literal. * * @param string $value Value to quote. * * @return string */ function sql_quote_string( $value ) { return "'" . str_replace( [ '\\', "'" ], [ '\\\\', "\\'" ], $value ) . "'"; } /** * Quotes a value as an SQL identifier. * * @param string $value Identifier to quote. * * @return string */ function sql_quote_identifier( $value ) { return '`' . str_replace( '`', '``', $value ) . '`'; } /** * Quotes a database name for a database-level GRANT or REVOKE. * * `_`, `%` and `\` are pattern characters there, even inside backticks: unescaped, `a_b` would also match `axb`. * The name may not exceed 64 characters, so when there is no room to escape them all, each one left over becomes `_`, which still matches it. * * @param string $db_name Database name. * * @return string */ function sql_quote_grant_database( $db_name ) { $length = preg_match_all( '/./su', $db_name ); $spare = 64 - ( false === $length ? strlen( $db_name ) : $length ); $pattern = preg_replace_callback( '/[\\\\_%]/', function ( $match ) use ( &$spare ) { return $spare-- > 0 ? '\\' . $match[0] : '_'; }, $db_name ); return sql_quote_identifier( $pattern ); } /** * Length of a database name in a GRANT once its pattern characters are escaped. * * @param string $db_name Database name. * * @return int Characters. */ function get_grant_name_length( $db_name ) { $length = preg_match_all( '/./su', $db_name ); return ( false === $length ? strlen( $db_name ) : $length ) + preg_match_all( '/[\\\\_%]/', $db_name ); } /** * Whether a GRANT can cover exactly this database: the escaped name has to fit in 64 characters. * * @param string $db_name Database name. * * @return bool */ function db_name_fits_exact_grant( $db_name ) { return get_grant_name_length( $db_name ) <= 64; } /** * Cuts a database name so that, with $suffix appended, its escaped GRANT name fits in 64 characters and the name in 64 bytes. * * @param string $db_name Database name. * @param string $suffix Suffix to append. * * @return string */ function cut_db_name_for_grant( $db_name, $suffix = '' ) { $chars = preg_split( '//u', $db_name, -1, PREG_SPLIT_NO_EMPTY ); if ( false === $chars ) { $chars = str_split( $db_name ); } $room = 64 - get_grant_name_length( $suffix ); $bytes = 64 - strlen( $suffix ); $name = ''; foreach ( $chars as $char ) { $room -= false === strpos( '\\_%', $char ) ? 1 : 2; $bytes -= strlen( $char ); if ( $room < 0 || $bytes < 0 ) { break; } $name .= $char; } return $name . $suffix; } /** * Runs SQL as root on the global database. * * The SQL goes through a file, so names and passwords never pass through a shell. * * @param string $sql SQL statements to run. * @param array $obfuscate Secrets in the SQL, masked in the logged output. * * @return bool|object Result of EE::launch(), or false if the SQL could not be copied to the container. */ function run_global_db_sql( $sql, $obfuscate = [] ) { $sql_path = tempnam( \EE\Utils\get_temp_dir(), 'ee-db-' ); file_put_contents( $sql_path, $sql ); $container_path = '/tmp/' . basename( $sql_path ) . '.sql'; $copied = EE::exec( sprintf( 'docker cp %s %s:%s', escapeshellarg( $sql_path ), GLOBAL_DB_CONTAINER, $container_path ) ); unlink( $sql_path ); if ( ! $copied ) { return false; } // The client echoes a failing statement to STDERR, which is logged; `--loose-` only warns on clients without the option. $script = sprintf( 'mysql --loose-skip-print-query-on-error -uroot -p"$MYSQL_ROOT_PASSWORD" -N -B < %1$s; rc=$?; rm -f %1$s; exit $rc', $container_path ); return EE::launch( sprintf( 'docker exec %s sh -c %s', GLOBAL_DB_CONTAINER, escapeshellarg( $script ) ), false, true, $obfuscate ); } /** * Runs a single-value query on the global database and exits if it cannot be answered. * * @param string $sql Query to run. * * @return bool Whether the query returned a row. */ function global_db_query_has_row( $sql ) { $result = run_global_db_sql( $sql ); if ( ! $result || 0 !== $result->return_code ) { EE::error( 'Could not query the global database. Please check if it is running (`ee service status db`) and see the logs.' ); } return '' !== trim( $result->stdout ); } /** * Checks whether a database exists on the global database server. * * @param string $db_name Database name. * * @return bool */ function global_db_has_database( $db_name ) { return global_db_query_has_row( sprintf( 'SELECT 1 FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = %s;', sql_quote_string( $db_name ) ) ); } /** * Checks whether a user exists on the global database server, for any host. * * @param string $db_user Database user. * * @return bool */ function global_db_has_user( $db_user ) { return global_db_query_has_row( sprintf( 'SELECT 1 FROM mysql.user WHERE User = %s LIMIT 1;', sql_quote_string( $db_user ) ) ); } /** * Default database name of a site, cut so that a GRANT can cover exactly this database. * * @param string $site_url Name of the site. * * @return string */ function get_default_db_name( $site_url ) { return cut_db_name_for_grant( str_replace( [ '.', '-' ], '_', $site_url ) ); } /** * Picks the database name of a new global-db site and refuses a database or user that is already taken. * * Taken means it exists on the server or another site uses it. A taken user or --dbname is refused, because sharing it would let one site's delete or failed create drop the other's; a taken default name gets a suffix. * * @param string $db_name Requested or default database name. * @param string $db_user Database user to be created. * @param bool $explicit_db_name Whether the name was passed with --dbname. * * @return string Database name to create. */ function reserve_global_db_names( $db_name, $db_user, $explicit_db_name ) { wait_for_global_db(); $names_in_use = []; $users_in_use = []; foreach ( Site::all( [ 'site_url', 'db_host', 'db_name', 'db_user' ] ) as $site ) { if ( GLOBAL_DB === $site->db_host ) { $names_in_use[ $site->db_name ] = $site->site_url; $users_in_use[ $site->db_user ] = $site->site_url; } } if ( isset( $users_in_use[ $db_user ] ) || global_db_has_user( $db_user ) ) { $owner = isset( $users_in_use[ $db_user ] ) ? " by site {$users_in_use[ $db_user ]}" : ''; EE::error( sprintf( 'Database user `%s` is already used%s on the global database. Please pass a different --dbuser, or leave it out to generate one.', $db_user, $owner ) ); } if ( strlen( $db_name ) > 64 ) { EE::error( sprintf( 'Database name `%s` is longer than 64 characters.', $db_name ) ); } $is_taken = function ( $name ) use ( $names_in_use ) { return isset( $names_in_use[ $name ] ) || global_db_has_database( $name ); }; if ( ! $is_taken( $db_name ) ) { return $db_name; } if ( $explicit_db_name ) { $owner = isset( $names_in_use[ $db_name ] ) ? " by site {$names_in_use[ $db_name ]}" : ''; EE::error( sprintf( 'Database `%s` is already used%s on the global database. Please pass a different --dbname, or leave it out to use a free default name.', $db_name, $owner ) ); } for ( $i = 2; $i <= 100; $i++ ) { $suffix = '_' . $i; $candidate = cut_db_name_for_grant( $db_name, $suffix ); if ( ! $is_taken( $candidate ) ) { EE::log( sprintf( 'Database `%s` already exists, using `%s` instead.', $db_name, $candidate ) ); return $candidate; } } EE::error( sprintf( 'Could not find a free database name for `%s`. Please pass one with --dbname.', $db_name ) ); } /** * Create user in remote or global db. * * On the global db either both the database and the user are created, or neither: an object that already exists makes its CREATE fail, and only what this call created is dropped again. * * @param string $db_host Database Hostname. * @param string $db_name Database name to be created. * @param string $db_user Database user to be created. * @param string $db_pass Database password to be created. * * @return array|bool Finally created database name, user and password. */ function create_user_in_db( $db_host, $db_name = '', $db_user = '', $db_pass = '' ) { $db_name = empty( $db_name ) ? \EE\Utils\random_password( 5 ) : $db_name; $db_user = empty( $db_user ) ? \EE\Utils\random_password( 5 ) : $db_user; $db_pass = empty( $db_pass ) ? \EE\Utils\random_password() : $db_pass; if ( GLOBAL_DB === $db_host ) { wait_for_global_db(); $user = sql_quote_string( $db_user ) . "@'%'"; $database = sql_quote_identifier( $db_name ); $ok = function ( $sql, $obfuscate = [] ) { $result = run_global_db_sql( $sql, $obfuscate ); return $result && 0 === $result->return_code; }; if ( ! $ok( sprintf( 'CREATE USER %s IDENTIFIED BY %s;', $user, sql_quote_string( $db_pass ) ), [ sql_quote_string( $db_pass ), $db_pass ] ) ) { return false; } if ( ! $ok( sprintf( 'CREATE DATABASE %s;', $database ) ) ) { $ok( sprintf( 'DROP USER %s;', $user ) ); return false; } if ( ! db_name_fits_exact_grant( $db_name ) ) { EE::warning( sprintf( 'Database name `%s` is too long for its grant to cover only this database: user `%s` may also get access to other databases whose names have the same length. A shorter --dbname avoids this.', $db_name, $db_user ) ); } if ( ! $ok( sprintf( 'GRANT ALL PRIVILEGES ON %s.* TO %s; FLUSH PRIVILEGES;', sql_quote_grant_database( $db_name ), $user ) ) ) { // Separate calls: a batch stops at its first error. $ok( sprintf( 'DROP DATABASE %s;', $database ) ); $ok( sprintf( 'DROP USER %s;', $user ) ); return false; } } else { //TODO: Handle remote case. } return [ 'db_name' => $db_name, 'db_user' => $db_user, 'db_pass' => $db_pass, ]; } /** * Function to cleanup database. * * @param string $db_host Database host from which database is to be removed. * @param string $db_name Database name to be removed. * @param string $db_user Database user to remove the host. * @param string $db_pass Database password of the user. */ function cleanup_db( $db_host, $db_name, $db_user = '', $db_pass = '' ) { if ( GLOBAL_DB === $db_host ) { run_global_db_sql( sprintf( 'DROP DATABASE %s;', sql_quote_identifier( $db_name ) ) ); } } /** * Function to cleanup database user. * * @param string $db_host Database host from which user is to be removed. * @param string $db_user_to_be_cleaned Database user to be removed. * @param string $db_privileged_pass User having sufficient privilege to delete the given user. * @param string $db_privileged_user Password of that privileged user. */ function cleanup_db_user( $db_host, $db_user_to_be_cleaned, $db_privileged_pass = '', $db_privileged_user = 'root' ) { if ( GLOBAL_DB === $db_host ) { run_global_db_sql( sprintf( "DROP USER %s@'%%';", sql_quote_string( $db_user_to_be_cleaned ) ) ); } } /** * Creates site root directory if does not exist. * Throws error if it does exist. * * @param string $site_fs_path Root directory of the site. * @param string $site_url Name of the site. */ function create_site_root( $site_fs_path, $site_url ) { $fs = new Filesystem(); if ( $fs->exists( $site_fs_path ) ) { EE::error( "Webroot directory for site $site_url already exists." ); } $whoami = EE::launch( 'whoami', false, true ); $terminal_username = rtrim( $whoami->stdout ); $fs->mkdir( $site_fs_path ); $fs->chown( $site_fs_path, $terminal_username ); } /** * Name docker-compose gives the project of a site directory. * * @param string $site_url Name of the site. * * @return string */ function get_compose_project_name( $site_url ) { return ltrim( preg_replace( '/[^a-z0-9_-]/', '', strtolower( $site_url ) ), '_-' ); } /** * Exits before anything is created if a new site would reuse another site's webroot, volumes or compose project. * * Volume names drop `.` and `-` from the site name and the compose project keeps only `a-z0-9_-`, so different names can map to the same ones (`a-b.test`, `a.b.test`, `ab.test`, `_ab.test`). Existing sites keep their names, so the create is refused. * * @param string $site_url Name of the new site. * @param string $site_fs_path Webroot of the new site. */ function check_site_name_conflicts( $site_url, $site_fs_path ) { $fs = new Filesystem(); if ( $fs->exists( $site_fs_path ) ) { EE::error( "Webroot directory for site $site_url already exists." ); } $prefix = \EE_DOCKER::get_docker_style_prefix( $site_url ); $project = get_compose_project_name( $site_url ); $reason = 'Please use a different site name.'; foreach ( Site::all( [ 'site_url' ] ) as $site ) { if ( $site->site_url === $site_url ) { continue; } if ( \EE_DOCKER::get_docker_style_prefix( $site->site_url ) === $prefix ) { EE::error( sprintf( 'Site %1$s would share docker volumes (%2$s_*) with the existing site %3$s. %4$s', $site_url, $prefix, $site->site_url, $reason ) ); } // A disabled site has no containers for the check below to find. if ( get_compose_project_name( $site->site_url ) === $project ) { EE::error( sprintf( 'Site %1$s would share the docker-compose project %2$s with the existing site %3$s. %4$s', $site_url, $project, $site->site_url, $reason ) ); } } // Leftovers of a deleted site with a colliding name would be mounted or adopted as they are. $volumes = EE::launch( 'docker volume ls --format \'{{.Name}} {{.Label "io.easyengine.site"}}\'' ); foreach ( array_filter( explode( "\n", trim( $volumes->stdout ) ) ) as $line ) { $parts = explode( ' ', trim( $line ), 2 ); $owner = isset( $parts[1] ) ? $parts[1] : ''; if ( 0 === strpos( $parts[0], $prefix . '_' ) && $owner !== $site_url ) { EE::error( sprintf( 'Docker volume %1$s already exists%2$s, and site %3$s would use it. %4$s', $parts[0], $owner ? " (site $owner)" : '', $site_url, $reason ) ); } } $containers = EE::launch( sprintf( 'docker ps -a --filter %s --format \'{{.Names}} {{.Label "io.easyengine.site"}}\'', escapeshellarg( 'label=com.docker.compose.project=' . $project ) ) ); foreach ( array_filter( explode( "\n", trim( $containers->stdout ) ) ) as $line ) { $parts = explode( ' ', trim( $line ), 2 ); $owner = isset( $parts[1] ) ? $parts[1] : ''; if ( $owner !== $site_url ) { EE::error( sprintf( 'Container %1$s already belongs to the docker-compose project %2$s%3$s, which site %4$s would use. %5$s', $parts[0], $project, $owner ? " (site $owner)" : '', $site_url, $reason ) ); } } } /** * Adds www to non-www redirection to site * * @param string $site_url name of the site. * @param bool $ssl enable ssl or not. * @param bool $inherit inherit cert or not. */ function add_site_redirects( string $site_url, bool $ssl, bool $inherit ) { $fs = new Filesystem(); $confd_path = EE_ROOT_DIR . '/services/nginx-proxy/conf.d/'; $config_file_path = $confd_path . $site_url . '-redirect.conf'; $has_www = strpos( $site_url, 'www.' ) === 0; $cert_site_name = $site_url; $ssl_policy = get_ssl_policy(); $conf_ssl_policy = 'ssl_policy_' . str_replace( '-', '_', $ssl_policy ); if ( $inherit ) { $cert_site_name = implode( '.', array_slice( explode( '.', $site_url ), 1 ) ); } // Check for existence of cert and key files for the cert_site_name $certs_dir = EE_ROOT_DIR . '/services/nginx-proxy/certs/'; $crt_file = $certs_dir . $cert_site_name . '.crt'; $key_file = $certs_dir . $cert_site_name . '.key'; if ( ( $ssl && file_exists( $crt_file ) && file_exists( $key_file ) ) || ! $ssl ) { if ( $has_www ) { $server_name = ltrim( $site_url, '.www' ); } else { $server_name = 'www.' . $site_url; } $conf_data = [ 'site_name' => $site_url, 'cert_site_name' => $cert_site_name, 'server_name' => $server_name, 'ssl' => $ssl, $conf_ssl_policy => true, ]; $content = EE\Utils\mustache_render( SITE_TEMPLATE_ROOT . '/redirect.conf.mustache', $conf_data ); $fs->dumpFile( $config_file_path, ltrim( $content, PHP_EOL ) ); } else { EE::log( sprintf( 'SSL cert/key missing for %s, skipping redirect config.', $cert_site_name ) ); } } /** * Function to check config and return a valid ssl-policy. * * @return string Valid ssl-policy. */ function get_ssl_policy() { $ssl_policy = get_config_value( 'ssl-policy', 'Mozilla-Modern' ); $valid_configurations = [ 'Mozilla-Old', 'Mozilla-Intermediate', 'Mozilla-Modern', 'AWS-TLS-1-2-2017-01', 'AWS-TLS-1-1-2017-01', 'AWS-2016-08', 'AWS-2015-05', 'AWS-2015-03', 'AWS-2015-02', ]; return in_array( $ssl_policy, $valid_configurations, true ) ? $ssl_policy : 'Mozilla-Modern'; } /** * Checks whether a site name is a lowercase hostname with at least two labels. * * @param string $site_url Name of the site. * * @return bool */ function is_valid_site_name( $site_url ) { // The name ends up in shell commands, file paths and docker names, so allow only RFC 1123 hostnames (IDNs in punycode). $label = '[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?'; // 64, not 253: the name is also a container hostname and a certificate CN, both capped at 64. return is_string( $site_url ) && strlen( $site_url ) <= 64 && 1 === preg_match( '/^' . $label . '(?:\.' . $label . ')+$/D', $site_url ); } /** * Exits with an error if the site name is not a valid hostname. * * @param string $site_url Name of the site. */ function validate_site_name( $site_url ) { if ( ! is_valid_site_name( $site_url ) ) { \EE::error( sprintf( 'Invalid site name: %s. A site name must be a hostname like example.com: two or more dot-separated labels of 1-63 letters, digits or hyphens that do not start or end with a hyphen, at most 64 characters in total. Use the punycode (xn--) form for internationalized domain names.', $site_url ) ); } } /** * Function to create entry in /etc/hosts. * * @param string $site_url Name of the site. */ function create_etc_hosts_entry( $site_url ) { if ( IS_DARWIN ) { // setup_dnsmasq_for_darwin only if domain ends with `.test` $ends_with_string = '.test'; $diff = strlen( $site_url ) - strlen( $ends_with_string ); if ( $diff >= 0 && false !== strpos( $site_url, $ends_with_string, $diff ) ) { setup_dnsmasq_for_darwin(); } return; } $host_line = LOCALHOST_IP . "\t$site_url"; $etc_hosts = file_get_contents( '/etc/hosts' ); if ( ! preg_match( '/\s+' . preg_quote( $site_url, '/' ) . '$/m', $etc_hosts ) ) { if ( EE::exec( '/bin/bash -c ' . escapeshellarg( 'echo ' . escapeshellarg( $host_line ) . ' >> /etc/hosts' ) ) ) { EE::success( 'Host entry successfully added.' ); } else { EE::warning( "Failed to add $site_url in host entry, Please do it manually!" ); } } else { EE::log( 'Host entry already exists.' ); } } /** * Setup dnsmasq for darwin to resolve `*.test` domain. * * @return bool success. */ function setup_dnsmasq_for_darwin() { if ( ! IS_DARWIN ) { return false; } // check if brew is installed. if ( EE::exec( 'command -v brew' ) ) { $fs = new Filesystem(); if ( $fs->exists( '/etc/resolver/test' ) ) { return true; } } else { return false; } // check if dnsmasq is installed. if ( ! EE::exec( 'brew ls --versions dnsmasq' ) ) { return false; } // create config directory. EE::exec( 'mkdir -p $(brew --prefix)/etc/' ); // Setup `*.test` domain. EE::exec( "echo 'address=/.test/127.0.0.1' > $(brew --prefix)/etc/dnsmasq.conf" ); EE::log( 'Setting up dnsmasq for *.test domain. You might need to enter password.' ); // Add to LaunchDaemons so that it works after reboot. EE::exec( 'sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons' ); // Create resolver directory. EE::exec( 'sudo mkdir -v /etc/resolver' ); // Adding 127.0.0.1 nameserver to resolvers. EE::exec( "sudo bash -c 'echo \"nameserver 127.0.0.1\" > /etc/resolver/test'" ); // start it. if ( EE::exec( 'sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist' ) ) { return true; } return false; } /** * Checking site is running or not. * * @param string $site_url Name of the site. * * @throws \Exception when fails to connect to site. */ function site_status_check( $site_url ) { EE::log( 'Checking and verifying site-up status. This may take some time.' ); $config_80_port = \EE\Utils\get_config_value( 'proxy_80_port', 80 ); $httpcode = \EE\Utils\get_curl_info( $site_url, $config_80_port ); $i = 0; $auth = false; while ( 200 !== $httpcode && 302 !== $httpcode && 301 !== $httpcode ) { EE::debug( "$site_url status httpcode: $httpcode" ); if ( 401 === $httpcode ) { $user_pass = get_global_auth(); $auth = $user_pass['username'] . ':' . $user_pass['password']; } $httpcode = \EE\Utils\get_curl_info( $site_url, $config_80_port, false, $auth, true ); echo '.'; sleep( 2 ); if ( $i ++ > 60 ) { break; } } EE::debug( "$site_url status httpcode: $httpcode" ); echo PHP_EOL; if ( 200 !== $httpcode && 302 !== $httpcode && 301 !== $httpcode ) { throw new \Exception( 'Problem connecting to site!' ); } } /** * Function to pull the latest images and bring up the site containers and set EasyEngine header. * * @param string $site_fs_path Root directory of the site. * @param array $containers The minimum required conatainers to start the site. Default null, leads to starting of * all containers. * * @throws \Exception when docker-compose up fails. */ function start_site_containers( $site_fs_path, $containers = [] ) { chdir( $site_fs_path ); EE::log( 'Starting site\'s services.' ); if ( ! \EE_DOCKER::docker_compose_up( $site_fs_path, $containers ) ) { throw new \Exception( 'There was some error in docker-compose up.' ); } } /** * Function to restart given containers for a site and update EasyEngine header. * * @param string $site_fs_path Root directory of the site. * @param string|array $containers Containers to restart. */ function restart_site_containers( $site_fs_path, $containers ) { chdir( $site_fs_path ); $all_containers = is_array( $containers ) ? implode( ' ', $containers ) : $containers; EE::exec( \EE_DOCKER::docker_compose_with_custom() . " restart $all_containers" ); } /** * Function to stop given containers for a site. * * @param string $site_fs_path Root directory of the site. * @param string|array $containers Containers to stop. */ function stop_site_containers( $site_fs_path, $containers ) { chdir( $site_fs_path ); $all_containers = is_array( $containers ) ? implode( ' ', $containers ) : $containers; EE::exec( \EE_DOCKER::docker_compose_with_custom() . " stop $all_containers" ); EE::exec( \EE_DOCKER::docker_compose_with_custom() . " rm -f $all_containers" ); } /** * Generic function to run a docker compose command. Must be ran inside correct directory. * * @param string $action docker-compose action to run. * @param string $container The container on which action has to be run. * @param string $action_to_display The action message to be displayed. * @param string $service_to_display The service message to be displayed. */ function run_compose_command( $action, $container, $action_to_display = null, $service_to_display = null ) { $display_action = $action_to_display ? $action_to_display : $action; $display_service = $service_to_display ? $service_to_display : $container; EE::log( ucfirst( $display_action ) . 'ing ' . $display_service ); EE::exec( \EE_DOCKER::docker_compose_with_custom() . " $action $container", true, true ); } /** * Function to copy and configure files needed for postfix. * * @param string $site_url Name of the site to configure postfix files for. * @param string $site_service_dir Configuration directory of the site `site_root/services`. */ function set_postfix_files( $site_url, $site_service_dir ) { $fs = new Filesystem(); $fs->mkdir( $site_service_dir . '/postfix/ssl' ); $ssl_dir = $site_service_dir . '/postfix/ssl'; $key = $ssl_dir . '/server.key'; // A CN longer than 64 characters makes openssl fail; the cert is only used for opportunistic STARTTLS. $common_name = substr( 'smtp.' . $site_url, 0, 64 ); $generated = EE::exec( sprintf( 'openssl req -new -x509 -nodes -days 365 -subj %s -out %s -keyout %s', escapeshellarg( '/CN=' . $common_name ), escapeshellarg( $ssl_dir . '/server.crt' ), escapeshellarg( $key ) ) ); if ( ! $generated || ! EE::exec( 'chmod 0600 ' . escapeshellarg( $key ) ) ) { throw new \Exception( 'Unable to generate ssl key for postfix' ); } } /** * Function to execute docker-compose exec calls to postfix to get it configured and running for the site. * * @param string $site_url Name of the for which postfix has to be configured. * @param string $site_fs_path Site root. */ function configure_postfix( $site_url, $site_fs_path ) { chdir( $site_fs_path ); $default_from = EE::launch( \EE_DOCKER::docker_compose_with_custom() . ' exec postfix sh -c \'echo $REPLY_EMAIL\'' )->stdout; if ( ! trim( $default_from ) ) { $default_from = "no-reply@$site_url"; } EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' exec php sh -c ' . escapeshellarg( 'echo ' . escapeshellarg( "host postfix\ntls off\nfrom $default_from" ) . ' > /etc/msmtprc' ) ); $relay_host = EE::launch( \EE_DOCKER::docker_compose_with_custom() . ' exec postfix sh -c \'echo $RELAY_HOST\'' )->stdout; $relay_host = trim( $relay_host, "\n\r" ); EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' exec postfix postconf -e ' . escapeshellarg( 'relayhost = ' . $relay_host ) ); EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' exec postfix postconf -e \'smtpd_recipient_restrictions = permit_mynetworks\'' ); $launch = EE::launch( sprintf( 'docker inspect -f \'{{ with (index .IPAM.Config 0) }}{{ .Subnet }}{{ end }}\' %s', escapeshellarg( $site_url ) ) ); $subnet_cidr = trim( $launch->stdout ); EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' exec postfix postconf -e ' . escapeshellarg( "mynetworks = $subnet_cidr 127.0.0.0/8" ) ); EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' exec postfix postconf -e ' . escapeshellarg( "myhostname = $site_url" ) ); EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' exec postfix postconf -e \'syslog_name = $myhostname\'' ); EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' restart postfix' ); } /** * Reload the global nginx proxy. */ function reload_global_nginx_proxy() { // `EE::launch()` returns a ProcessRun object (truthy), so gate on the exit code to avoid reloading a broken config. $test = test_global_nginx_proxy_config(); if ( 0 !== $test->return_code ) { \EE::warning( 'nginx config test failed, skipping reload of ' . EE_PROXY_TYPE . ":\n" . $test->stderr ); return false; } return \EE::launch( sprintf( 'docker exec %s sh -c "/usr/sbin/nginx -s reload"', EE_PROXY_TYPE ) ); } /** * Regenerates the global proxy's default.conf and runs `nginx -t` on it. * * @param bool $retry_any_failure Retry once on any failure, not only on one reported in default.conf. * * @return \EE\ProcessRun Result of the last `nginx -t`. */ function test_global_nginx_proxy_config( $retry_any_failure = false ) { $regenerate = sprintf( 'docker exec %s sh -c "/app/docker-entrypoint.sh /usr/local/bin/docker-gen /app/nginx.tmpl /etc/nginx/conf.d/default.conf"', EE_PROXY_TYPE ); $test_cmd = sprintf( 'docker exec %s sh -c "nginx -t"', EE_PROXY_TYPE ); // Regenerate first so `nginx -t` validates the config that will actually be served. \EE::launch( $regenerate ); $test = \EE::launch( $test_cmd ); // The proxy's own docker-gen rewrites default.conf in place after container events, so a test racing it can read a partial file. if ( 0 !== $test->return_code && ( $retry_any_failure || false !== strpos( $test->stderr, '/etc/nginx/conf.d/default.conf' ) ) ) { \EE::debug( "nginx config test failed, retrying once:\n" . $test->stderr ); sleep( 1 ); \EE::launch( $regenerate ); $test = \EE::launch( $test_cmd ); } return $test; } /** * Reads files so that they can be put back with `restore_files()`. * * @param array $paths Absolute file paths. * * @return array Path => content, or null for a file that does not exist. * * @throws \Exception When an existing file can't be read. */ function backup_files( array $paths ) { $backup = []; foreach ( array_unique( $paths ) as $path ) { $content = is_file( $path ) ? file_get_contents( $path ) : null; // Restoring a failed read would overwrite the file with an empty one. if ( false === $content ) { throw new \Exception( "Could not read $path to back it up." ); } $backup[ $path ] = $content; } return $backup; } /** * Puts files back as `backup_files()` found them: rewrites the ones that existed and removes the others. * * @param array $backup Return value of `backup_files()`. */ function restore_files( array $backup ) { $fs = new Filesystem(); foreach ( $backup as $path => $content ) { if ( null === $content ) { if ( file_exists( $path ) ) { $fs->remove( $path ); } } else { $fs->dumpFile( $path, $content ); } } } /** * Paths of the files a failed LE issuance must not leave changed: the served certificate, its ACME key pair, certificates and DN, and the www redirect. * * Authorization and order state is left out: the next order revokes and replaces it. * * @param string $site_url Name of the site. * * @return array Absolute file paths. */ function get_site_ssl_file_paths( $site_url ) { $proxy_dir = EE_ROOT_DIR . '/services/nginx-proxy'; $acme_dir = "$proxy_dir/acme-conf"; return [ "$proxy_dir/certs/$site_url.crt", "$proxy_dir/certs/$site_url.key", "$proxy_dir/certs/$site_url.chain.pem", "$proxy_dir/conf.d/$site_url-redirect.conf", "$acme_dir/certs/$site_url/private/key.public.pem", "$acme_dir/certs/$site_url/private/key.private.pem", "$acme_dir/certs/$site_url/private/combined.pem", "$acme_dir/certs/$site_url/public/cert.pem", "$acme_dir/certs/$site_url/public/chain.pem", "$acme_dir/certs/$site_url/public/fullchain.pem", "$acme_dir/var/$site_url/distinguished_name.json", ]; } /** * Removes a site's certificate files from nginx-proxy and its ACME state, whatever the site's current SSL type. * * @param string $site_url Name of the site. * @param array $domains Other domains of the site (alias domains, www variant) whose ACME state goes too. * @param array $keep Files to leave in place, e.g. a custom pair the user keeps in the certs dir. */ function remove_site_ssl_files( $site_url, array $domains = [], array $keep = [] ) { $proxy_dir = EE_ROOT_DIR . '/services/nginx-proxy'; $paths = [ "$proxy_dir/certs/$site_url.crt", "$proxy_dir/certs/$site_url.key", "$proxy_dir/certs/$site_url.chain.pem", "$proxy_dir/acme-conf/certs/$site_url", ]; foreach ( array_unique( array_merge( [ $site_url ], $domains ) ) as $domain ) { if ( '' !== $domain && false === strpos( $domain, '/' ) ) { $paths[] = "$proxy_dir/acme-conf/var/$domain"; } } $keep = array_filter( array_map( 'realpath', array_filter( $keep ) ) ); $paths = array_values( array_filter( $paths, function ( $path ) use ( $keep ) { return file_exists( $path ) && ! in_array( realpath( $path ), $keep, true ); } ) ); if ( empty( $paths ) ) { return; } \EE::log( 'Removing ssl certs and other config files.' ); try { ( new Filesystem() )->remove( $paths ); } catch ( \Exception $e ) { \EE::warning( $e->getMessage() ); } } /** * The www or non-www counterpart of a domain. * * @param string $domain Domain name. * * @return string */ function get_www_counterpart( $domain ) { return 0 === strpos( $domain, 'www.' ) ? substr( $domain, 4 ) : 'www.' . $domain; } /** * Get global auth if it exists. */ function get_global_auth() { if ( ! class_exists( '\EE\Model\Auth' ) ) { return false; } $auth = \EE\Model\Auth::where( [ 'site_url' => 'default', ] ); if ( empty( $auth ) ) { return false; } return [ 'username' => $auth[0]->username, 'password' => $auth[0]->password, ]; } /** * Clear site cache with specific key. * * @param string $key Cache key to clear. * * @return bool Whether the purge ran. */ function clean_site_cache( $key ) { // No -it: without a terminal (cron, clone, scripts) docker refuses to attach a TTY and nothing is purged. return EE::exec( sprintf( 'docker exec %s redis-cli --eval purge_all_cache.lua 0 , %s', GLOBAL_REDIS_CONTAINER, escapeshellarg( $key . '*' ) ) ); } /** * Function to get the public-dir from assoc args with checks and sanitizations. * * @param $assoc_args * * @return string processed value for public-dir. */ function get_public_dir( $assoc_args ) { // Create container fs path for site. $public_root = get_flag_value( $assoc_args, 'public-dir' ); $public_root = str_replace( '/var/www/htdocs/', '', trailingslashit( $public_root ) ); $public_root = remove_trailing_slash( $public_root ); $sanitized_public_dir = sanitize_file_folder_name( $public_root ); $user_input_public_dir = sprintf( '/var/www/htdocs/%s', trim( $sanitized_public_dir, '/' ) ); return empty( $public_root ) ? '/var/www/htdocs' : $user_input_public_dir; } /** * Get final source directory for site webroot. * * @param string $original_src_dir source directory. * @param string $container_fs_path public directory set by user if any. * * @return string final webroot for site. */ function get_webroot( $original_src_dir, $container_fs_path ) { $public_dir_path = str_replace( '/var/www/htdocs/', '', trailingslashit( $container_fs_path ) ); return empty( $public_dir_path ) ? $original_src_dir : $original_src_dir . '/' . rtrim( $public_dir_path, '/' ); } /** * Get all existing alias domains from db. * * @return array of all alias domains. */ function get_all_alias_domains() { $existing_alias_domains = Site::all( [ 'alias_domains' ] ); $existing_site_domains = Site::all( [ 'site_url' ] ); $all_existing_alias_domains = []; $all_existing_site_domains = []; if ( ! empty( $existing_alias_domains ) ) { $all_existing_alias_domains = array_column( $existing_alias_domains, 'alias_domains' ); } if ( ! empty( $existing_site_domains ) ) { $all_existing_site_domains = array_column( $existing_site_domains, 'site_url' ); } $array_of_alias_domains = []; foreach ( $all_existing_alias_domains as $existing_alias_domains ) { foreach ( explode( ',', $existing_alias_domains ) as $ad ) { if ( ! empty( $ad ) ) { $array_of_alias_domains[] = $ad; } } } return array_diff( $array_of_alias_domains, $all_existing_site_domains ); } /** * Update information of site in EE database * * @param string $site_url URL os site. * @param array $data Data to update. * * @return string final webroot for site. */ function update_site_db_entry( string $site_url, array $data ) { $site_id = Site::update( [ 'site_url' => $site_url ], $data ); if ( ! $site_id ) { throw new \Exception( 'Unable to update values in EE database.' ); } } /** * Get all domains of site. * * @param string $site_url alias domain whose parent needs to be found. * * @return string parent site. */ function get_domains_of_site( string $site_url ): array { $alias_domains = Site::find( $site_url )->alias_domains; $all_domains = explode( ',', $alias_domains ); array_push( $all_domains, $site_url ); return array_unique( $all_domains ); } /** * Get parent site of an alias domain. * * @param string $alias alias domain whose parent needs to be found. * * @return string parent site. */ function get_parent_of_alias( $alias ) { if ( ! in_array( $alias, get_all_alias_domains(), true ) ) { // the alis domain does not exist. So it has no parent. return ''; } $output = EE::db() ->table( 'sites' ) ->select( ...[ 'site_url' ] ) ->where( 'alias_domains', 'like', '%' . $alias . '%' ) ->first(); return reset( $output ); } /** * Check if given array of domains exist as alias for some site in db or not. * * @param array $domains array of domains to be checked. */ function check_alias_in_db( $domains ) { $alias_error = false; foreach ( $domains as $domain_check ) { if ( $alias_error ) { break; } $parent_site = get_parent_of_alias( trim( $domain_check ) ); $alias_error = ! empty( $parent_site ); $domain_having_parent = $alias_error ? $domain_check : ''; } if ( $alias_error ) { \EE::error( sprintf( "Site %1\$s already exists as an alias domain for site: %2\$s. Please delete it from alias domains of %2\$s if you want to create an independent site for it.", $domain_having_parent, $parent_site ) ); } } /** * Splits a comma separated list of alias domains, trimming them and dropping blank entries. * * @param string|bool $domains Comma separated alias domains, as passed to the alias domain flags. * * @return array */ function split_alias_domains( $domains ) { // A flag passed without a value is `true`, which would otherwise become the alias domain `1`. if ( ! is_string( $domains ) ) { return []; } return array_values( array_filter( array_map( 'trim', explode( ',', $domains ) ), 'strlen' ) ); } /** * Checks whether a name is one of the global proxy file names (e.g. auth-command's htpasswd and ACL files), in any case. * * @param string $name File name. * * @return bool */ function is_reserved_proxy_file_name( $name ) { return in_array( strtolower( (string) $name ), [ 'default', 'default_admin_tools' ], true ); } /** * Checks whether an alias domain is a plain hostname or `*.hostname` that is safe to use as a proxy file name. * * @param string $domain Alias domain. * * @return bool */ function is_valid_alias_domain( $domain ) { // No leading `_`, so an alias can't take over the `_wildcard.` files of another site. $label = '[A-Za-z0-9](?:[A-Za-z0-9_-]*[A-Za-z0-9_])?'; return is_string( $domain ) && 1 === preg_match( '/^(?:\*\.)?' . $label . '(?:\.' . $label . ')*$/D', $domain ) && ! is_reserved_proxy_file_name( $domain ); } /** * Exits with an error listing the alias domains that are not a plain hostname or `*.hostname`. * * @param array $domains Alias domains. */ function validate_alias_domains( $domains ) { $invalid = array_filter( $domains, function ( $domain ) { return ! is_valid_alias_domain( $domain ); } ); if ( ! empty( $invalid ) ) { \EE::error( sprintf( 'Invalid alias domain(s): %s. An alias domain must be a hostname or `*.hostname` whose labels use letters, digits, `-` and `_`, do not start with `-` or `_` (a leading `_` is reserved for proxy files like `_wildcard.`) and do not end with `-`. It can not be `default` or `default_admin_tools`.', implode( ', ', $invalid ) ) ); } } /** * 'sysctl' parameters for docker-compose file. * * @return array of all 'sysctl' parameters. */ function sysctl_parameters() { // Intentionally made not strict. It could also be in form of string inside config. if ( isset( \EE::get_runner()->config['sysctl'] ) && true == \EE::get_runner()->config['sysctl'] ) { return [ 'sysctl' => [ [ 'name' => 'net.ipv4.tcp_synack_retries=2' ], [ 'name' => 'net.ipv4.ip_local_port_range=2000 65535' ], [ 'name' => 'net.ipv4.tcp_rfc1337=1' ], [ 'name' => 'net.ipv4.tcp_fin_timeout=15' ], [ 'name' => 'net.ipv4.tcp_keepalive_time=300' ], [ 'name' => 'net.ipv4.tcp_keepalive_probes=5' ], [ 'name' => 'net.ipv4.tcp_keepalive_intvl=15' ], [ 'name' => 'net.core.somaxconn=65536' ], [ 'name' => 'net.ipv4.tcp_max_tw_buckets=1440000' ], ], ]; } return []; } /** * Removes entry of the site from /etc/hosts * * @param string $site_url site name. * */ function remove_etc_hosts_entry( $site_url ) { $fs = new Filesystem(); $hosts_file = file_get_contents( '/etc/hosts' ); $site_url_escaped = preg_quote( $site_url, '/' ); $hosts_file_new = preg_replace( "/127\.0\.0\.1\s+$site_url_escaped\n/", '', $hosts_file ); $fs->dumpFile( '/etc/hosts', $hosts_file_new ); } /** * Whether a Let's Encrypt site still waits for its first certificate, e.g. for `ee site ssl-verify` after a manual DNS-01 challenge. * * Such a site is served over HTTP only: with HTTPS on, nginx-proxy would serve it with the closest matching certificate of another site. * * @param string $site_url Name of the site. * @param string|null $site_ssl The site's SSL type. * * @return bool */ function is_le_ssl_pending( $site_url, $site_ssl ) { $cert = EE_SERVICE_DIR . '/nginx-proxy/certs/' . $site_url; return 'le' === $site_ssl && ! ( file_exists( "$cert.crt" ) && file_exists( "$cert.key" ) ); } /** * Checks if the site certificate needs renewal. * * The way it differs from Site_Letsencrypt::isRenewalNecessary is that the latter * checks the certificate from ACMEPHP cache. This checks the certificate from * Nginx Proxy cert directory. So this function will also work for non-le certificates * (custom certs). * * @param $site_url string URL of the site whose SSL we need to check * @return bool */ function ssl_needs_creation( $site_url ) { $certificatePath = EE_SERVICE_DIR . '/nginx-proxy/certs/' . $site_url . '.crt'; if ( file_exists( $certificatePath ) ) { $certificate = new Certificate( file_get_contents( $certificatePath ) ); $certificateParser = new \EE\Site\Type\EECertificateParser(); $parsedCertificate = $certificateParser->parse( $certificate ); // 3024000 = 35 days. if ( $parsedCertificate->getValidTo()->format( 'U' ) - time() >= 3024000 ) { \EE::log( sprintf( 'Current certificate is valid until %s, renewal is not necessary.', $parsedCertificate->getValidTo()->format( 'Y-m-d H:i:s' ) ) ); return false; } } return true; } /** * Waits until every authoritative nameserver of each record's zone serves the expected TXT values. * * The nameservers are queried directly: a recursive resolver can keep serving what it cached before the record existed, * such as the records a wildcard matched. * * @param array $records Expected TXT values by record name. * @param int $timeout Seconds to wait in total. * @param array $nameservers Nameservers to query by record name, as [ ip => name ], instead of looking them up. * @param int $min_wait Seconds to wait at least once the values are served. * * @return bool True once every nameserver serves every value, false when they don't within $timeout. */ function wait_for_dns_txt_records( array $records, $timeout = 120, array $nameservers = [], $min_wait = 10 ) { if ( empty( $records ) ) { return true; } $start = microtime( true ); $deadline = $start + $timeout; $pending = []; foreach ( $records as $name => $values ) { $servers = isset( $nameservers[ $name ] ) ? $nameservers[ $name ] : get_authoritative_nameservers( $name ); if ( empty( $servers ) ) { EE::warning( "Could not find the authoritative nameservers of $name, so its propagation can't be checked." ); continue; } foreach ( $servers as $ip => $server ) { $pending[] = [ $name, $ip, $server ]; } } $delay = 2; while ( ! empty( $pending ) ) { foreach ( $pending as $key => list( $name, $ip ) ) { $served = query_txt_records( $ip, $name ); if ( null !== $served && ! array_diff( $records[ $name ], $served ) ) { EE::debug( "$name serves the DNS challenge value on {$pending[ $key ][2]} ($ip)." ); unset( $pending[ $key ] ); } if ( function_exists( 'pcntl_signal_dispatch' ) ) { pcntl_signal_dispatch(); } if ( microtime( true ) >= $deadline ) { break; } } $left = $deadline - microtime( true ); if ( empty( $pending ) || $left <= 0 ) { break; } usleep( (int) ( min( $delay, $left ) * 1000000 ) ); $delay = min( $delay * 2, 16 ); } if ( empty( $pending ) ) { // Anycast nameservers answer from the site nearest to this server; give the others the time the fixed wait used to. $left = $start + $min_wait - microtime( true ); if ( $left > 0 ) { usleep( (int) ( $left * 1000000 ) ); } return true; } $missing = array_map( function ( $entry ) { return sprintf( '%s on %s (%s)', $entry[0], $entry[2], $entry[1] ); }, $pending ); EE::warning( sprintf( 'The DNS challenge value is not yet served after %ds for: %s. Trying the validation anyway.', $timeout, implode( ', ', $missing ) ) ); return false; } /** * Finds the authoritative nameservers of the zone that holds a name. * * @param string $name A domain name. * * @return array Nameserver names by IPv4 address, empty when none are found. */ function get_authoritative_nameservers( $name ) { $labels = explode( '.', rtrim( $name, '.' ) ); // The closest enclosing zone cut; a name a wildcard matches is answered with its target's records, so require the queried owner. while ( count( $labels ) > 1 ) { $zone = implode( '.', $labels ); $servers = []; foreach ( (array) @dns_get_record( $zone, DNS_NS ) as $record ) { if ( isset( $record['type'], $record['host'], $record['target'] ) && 'NS' === $record['type'] && 0 === strcasecmp( rtrim( $record['host'], '.' ), $zone ) ) { foreach ( (array) gethostbynamel( $record['target'] ) as $ip ) { if ( $ip ) { $servers[ $ip ] = $record['target']; } } } } if ( ! empty( $servers ) ) { return $servers; } array_shift( $labels ); } return []; } /** * Asks one nameserver, without recursion, for the TXT records of a name. * * @param string $server Nameserver IPv4 address. * @param string $name Record name. * @param int $timeout Seconds to wait for the answer. * * @return array|null The TXT values in the answer (empty for NXDOMAIN), null when there is no usable answer. */ function query_txt_records( $server, $name, $timeout = 3 ) { $qname = ''; foreach ( explode( '.', rtrim( $name, '.' ) ) as $label ) { if ( '' === $label || strlen( $label ) > 63 ) { return null; } $qname .= chr( strlen( $label ) ) . $label; } $id = random_int( 0, 0xffff ); // One question (TXT, IN) and an EDNS0 OPT record so a large answer fits in one UDP packet. Authoritative servers ignore RD, // but a network that redirects port 53 to its own resolver needs it. $query = pack( 'n6', $id, 0x0100, 1, 0, 0, 1 ) . $qname . "\0" . pack( 'n2', 16, 1 ) . "\0" . pack( 'n2Nn', 41, 1232, 0, 0 ); $answer = parse_dns_txt_answer( dns_exchange( $server, $query, false, $timeout ), $id ); if ( 'truncated' === $answer ) { $answer = parse_dns_txt_answer( dns_exchange( $server, $query, true, $timeout ), $id ); } return is_array( $answer ) ? $answer : null; } /** * Sends a DNS query to port 53 and returns the response matching its ID. * * @param string $server Nameserver IPv4 address. * @param string $query Wire-format query. * @param bool $tcp Use TCP instead of UDP. * @param int $timeout Seconds to wait. * * @return string The response, or an empty string. */ function dns_exchange( $server, $query, $tcp, $timeout ) { $deadline = microtime( true ) + $timeout; $socket = @stream_socket_client( ( $tcp ? 'tcp' : 'udp' ) . "://$server:53", $errno, $errstr, $timeout ); if ( ! $socket ) { return ''; } fwrite( $socket, $tcp ? pack( 'n', strlen( $query ) ) . $query : $query ); $response = ''; $expected = null; while ( ( $left = $deadline - microtime( true ) ) > 0 ) { $read = [ $socket ]; $write = null; $except = null; if ( ! @stream_select( $read, $write, $except, (int) $left, (int) ( fmod( $left, 1 ) * 1000000 ) ) ) { break; } $chunk = fread( $socket, 65535 ); if ( false === $chunk || ( '' === $chunk && feof( $socket ) ) ) { break; } if ( ! $tcp ) { // Skip a stray datagram, such as a late answer to an earlier query. if ( strlen( $chunk ) >= 2 && substr( $chunk, 0, 2 ) === substr( $query, 0, 2 ) ) { $response = $chunk; break; } continue; } $response .= $chunk; if ( null === $expected && strlen( $response ) >= 2 ) { $expected = unpack( 'n', $response )[1]; } if ( null !== $expected && strlen( $response ) >= $expected + 2 ) { $response = substr( $response, 2, $expected ); break; } } fclose( $socket ); return ( $tcp && null !== $expected && strlen( $response ) !== $expected ) ? '' : $response; } /** * Reads the TXT values from the answer section of a DNS response. * * @param string $response Wire-format response. * @param int $id ID of the query. * * @return array|string|null TXT values, 'truncated' when the answer needs TCP, or null for an unusable response. */ function parse_dns_txt_answer( $response, $id ) { $length = strlen( $response ); if ( $length < 12 ) { return null; } $header = unpack( 'nid/nflags/nqd/nan', $response ); if ( $header['id'] !== $id || ! ( $header['flags'] & 0x8000 ) ) { return null; } if ( $header['flags'] & 0x0200 ) { return 'truncated'; } $rcode = $header['flags'] & 0x000f; if ( 3 === $rcode ) { return []; } if ( 0 !== $rcode ) { return null; } $skip_name = function ( $offset ) use ( $response, $length ) { while ( $offset < $length ) { $label = ord( $response[ $offset ] ); if ( 0 === $label ) { return $offset + 1; } if ( 0xc0 === ( $label & 0xc0 ) ) { return $offset + 2; } $offset += $label + 1; } return null; }; $offset = 12; for ( $i = 0; $i < $header['qd']; $i++ ) { $offset = $skip_name( $offset ); if ( null === $offset ) { return null; } $offset += 4; } $values = []; for ( $i = 0; $i < $header['an']; $i++ ) { $offset = $skip_name( $offset ); if ( null === $offset || $offset + 10 > $length ) { return null; } $record = unpack( 'ntype/nclass/Nttl/nlength', substr( $response, $offset, 10 ) ); $offset += 10; if ( $offset + $record['length'] > $length ) { return null; } // A wildcard match can also put a CNAME and its target's records in the answer; only TXT data counts. if ( 16 === $record['type'] ) { $rdata = substr( $response, $offset, $record['length'] ); $value = ''; for ( $pos = 0; $pos < $record['length']; $pos += $size + 1 ) { $size = ord( $rdata[ $pos ] ); $value .= substr( $rdata, $pos + 1, $size ); } $values[] = $value; } $offset += $record['length']; } return $values; } /** * Get a new available subnet. * * @param int $mask * @return string|void * @throws EE\ExitException */ function get_available_subnet( int $mask = 24 ) { $existing_host_subnets = EE::launch( 'ip route show | cut -d \' \' -f1 | grep ^10' ); $existing_host_subnets = array_filter( explode( "\n", $existing_host_subnets->stdout ) ); $frontend_subnet = Option::get( 'frontend_subnet_ip' ); $backend_subnet = Option::get( 'backend_subnet_ip' ); if ( $frontend_subnet ) { array_push( $existing_host_subnets, $frontend_subnet ); } if ( $backend_subnet ) { array_push( $existing_host_subnets, $backend_subnet ); } $existing_subnets = array_filter( array_unique( $existing_host_subnets ) ); sort( $existing_subnets, SORT_NATURAL ); $ip = '10.0.0.0'; while ( $ip !== '10.255.255.0' ) { list( $subnet_start, $subnet_end ) = get_subnet_range( $ip, $mask ); $subnet_start = long2ip( $subnet_start ); $subnet_end = long2ip( $subnet_end ); if ( ! ip_in_existing_subnets( $subnet_start, $existing_subnets ) && ! ip_in_existing_subnets( $subnet_end, $existing_subnets ) ) { return $ip . '/' . $mask; } $ip = ip2long( $subnet_end ) + 1; $ip = long2ip( $ip ); } EE::error( 'It seems you have run out of your private IP adress space.' ); } function subnet_mask_int2long( int $mask ) { return ~(( 1 << ( 32 - $mask )) - 1 ); } /** * Check if IP is in existing subnets * * @param $ip string IP to check in existing subnets * @return bool */ function ip_in_existing_subnets( string $ip, array $existing_subnets ) { foreach( $existing_subnets as $subnet ) { if ( ip_in_subnet( $ip, $subnet ) ) { return true; } } return false; } /** * Check if an IP is in a subnet. * * @param $IP string IP that needs to be checked * @param $CIDR string Subnet in which IP will be searched. * @return bool */ function ip_in_subnet(string $IP, string $CIDR) { list( $subnet, $mask ) = explode ('/', $CIDR ); $mask = $mask ?? 16; $ip_subnet = ip2long( $subnet ); $ip_mask = subnet_mask_int2long( $mask ); $src_ip = ip2long( $IP ); return (( $src_ip & $ip_mask ) == ( $ip_subnet & $ip_mask )); } /** * Return starting and ending IP address of a subnet range. * * @param $ip * @param $mask * @return int[]|string[] */ function get_subnet_range( $ip, $mask ) { $ipl = ip2long( $ip ); $maskl = subnet_mask_int2long( $mask ); $range_start = $ipl & $maskl; $range_end = $ipl | ~$maskl; return [ $range_start, $range_end ]; } /** * Bash helpers for fetching and checking WordPress core inside a site's php container. * * WP-CLI <= 2.12 extracts .tar.gz packages with PharData, which truncates paths longer than 100 bytes in the PAX headers of WordPress >= 6.7.2 en_US packages (wp-cli/wp-cli#6320). So WP-CLI only resolves, downloads and md5-checks the package here, and tar/unzip extract it. WP-CLI >= 3.0 saves a .zip instead (wp-cli/core-command#333). * * @return string Unescaped bash that enables set -e and defines wp_cli, ee_wp_fetch, ee_wp_extract and ee_wp_verify. */ function get_wp_core_shell_functions() { return <<<'BASH' set -e wp_cli() { php -d memory_limit=256M "$(command -v wp)" "$@"; } # ee_wp_fetch [core download args]: prints the path of the downloaded package. ee_wp_fetch() { local dir=$1 pkg shift wp_cli core download --no-extract --path="$dir" "$@" >&2 || return 1 pkg=$(find "$dir" -maxdepth 1 -type f \( -name '*.tar.gz' -o -name '*.tgz' -o -name '*.zip' \) | head -n 1) if [ -z "$pkg" ]; then echo 'Error: WordPress package not found after download.' >&2 return 1 fi printf '%s\n' "$pkg" } # ee_wp_extract []: extracts wordpress/ (default: all) into /. ee_wp_extract() { local pkg=$1 dest=${2%/} sub=${3:-} strip=1 x mkdir -p "$dest/$sub" || return 1 case "$pkg" in *.zip) x="${pkg%/*}/x" mkdir -p "$x" || return 1 if command -v unzip >/dev/null 2>&1; then unzip -q -o "$pkg" ${sub:+"wordpress/$sub/*"} -d "$x" || return 1 else php -r '$z = new ZipArchive(); if ( true !== $z->open( $argv[1] ) || ! $z->extractTo( $argv[2] ) ) { fwrite( STDERR, "Error: Could not extract $argv[1].\n" ); exit( 1 ); }' "$pkg" "$x" || return 1 fi cp -R "$x/wordpress/$sub/." "$dest/$sub/" || return 1 ;; *) [ -z "$sub" ] || strip=$(( $(printf '%s' "$sub" | tr -cd '/' | wc -c) + 2 )) tar -xzf "$pkg" --no-same-owner --strip-components="$strip" -C "$dest/$sub" "wordpress/$sub" || return 1 ;; esac } # ee_wp_verify : fails unless core verifies, but only warns when the checksums can't be fetched from WordPress.org. ee_wp_verify() { local out out=$(wp_cli core verify-checksums --path="$1" 2>&1) && return 0 case "$out" in *"File doesn't exist:"* | *"File doesn't verify against checksum:"*) ;; *"Couldn't get checksums"* | *"api.wordpress.org"* | *"Failed to decode JSON"*) printf 'Warning: Could not verify WordPress core checksums: %s\n' "$out" >&2 return 0 ;; esac printf '%s\n' "$out" >&2 return 1 } BASH; } /** * Build the in-container command that downloads WordPress core into a site. * * @param string $path WordPress root inside the php container. * @param array $args Optional `version`, `locale`, `skip-content` and `force` (replace an existing install). * * @return string Command escaped for EE_DOCKER::docker_compose_exec() with $shell_wrapper = true. */ function get_wp_core_download_command( string $path, array $args = [] ) { $version = isset( $args['version'] ) ? (string) $args['version'] : ''; $is_nightly = in_array( strtolower( $version ), [ 'nightly', 'trunk' ], true ); $download_args = ''; foreach ( [ 'locale', 'version' ] as $key ) { if ( isset( $args[ $key ] ) && '' !== (string) $args[ $key ] ) { $download_args .= " --$key=" . escapeshellarg( (string) $args[ $key ] ); } } $script = get_wp_core_shell_functions() . 'dest=' . escapeshellarg( $path ) . "\n"; if ( ! empty( $args['skip-content'] ) || $is_nightly ) { // WP-CLI downloads the .zip here and extracts it with ZipArchive, which isn't affected. $script .= 'wp_cli core download --path="$dest"' . $download_args . ( empty( $args['skip-content'] ) ? '' : ' --skip-content' ) . ( empty( $args['force'] ) ? '' : ' --force' ) . "\n"; } else { if ( empty( $args['force'] ) ) { // WP-CLI's own check only sees the temp dir it downloads into. $script .= "if [ -e \"\$dest/wp-load.php\" ]; then echo 'Error: WordPress files seem to already be present here.' >&2; exit 1; fi\n"; } // Extract into the temp dir first, so a failure leaves the existing core untouched. $script .= "tmp=\$(mktemp -d)\ntrap 'rm -rf \"\$tmp\"' EXIT\nmkdir \"\$tmp/dl\"\n" . "pkg=\$(ee_wp_fetch \"\$tmp/dl\"$download_args)\n" . "if ! ee_wp_extract \"\$pkg\" \"\$tmp/wp\"; then\n" // A package from WP-CLI's cache isn't md5-checked again, so fetch a fresh copy once. . "\trm -rf \"\$tmp/dl\" \"\$tmp/wp\"\n\tmkdir \"\$tmp/dl\"\n" . "\tpkg=\$(WP_CLI_CACHE_DIR=\"\$tmp/cache\" ee_wp_fetch \"\$tmp/dl\"$download_args)\n" . "\tee_wp_extract \"\$pkg\" \"\$tmp/wp\"\nfi\n"; if ( ! empty( $args['force'] ) ) { // Drop wp-admin and wp-includes files of the previous core version. $script .= "rm -rf \"\${dest:?}/wp-admin\" \"\${dest:?}/wp-includes\"\n"; } $script .= "mkdir -p \"\$dest\"\ncp -R \"\$tmp/wp/.\" \"\$dest/\"\n"; } // Nightly builds have no published checksums. if ( ! $is_nightly ) { $script .= "ee_wp_verify \"\$dest\"\n"; } // docker_compose_exec() wraps the command in bash -c "...". return addcslashes( $script, '"$`\\' ); } /** * Add `session.upload_progress.enabled = Off` to a site's custom.ini content, unless the content already sets it globally. * * Same rule as the v4.13.3 upload-progress migrations: an effective global line, On or Off, is the user's choice and is kept. * * @param string $content custom.ini content. * * @return string|null The new content, or null when it already sets the directive. */ function add_session_upload_progress_off( string $content ) { $bom = ''; if ( 0 === strpos( $content, "\xEF\xBB\xBF" ) ) { $bom = "\xEF\xBB\xBF"; $content = substr( $content, 3 ); } if ( ini_sets_globally( $content, 'session.upload_progress.enabled' ) ) { return null; } $eol = false !== strpos( $content, "\r\n" ) ? "\r\n" : ( false === strpos( $content, "\n" ) && false !== strpos( $content, "\r" ) ? "\r" : "\n" ); $lines = '; Session upload progress is off for security; set it to On only if your app needs it.' . $eol . 'session.upload_progress.enabled = Off' . $eol; if ( '' === trim( $content ) ) { return $bom . $lines; } // At the very top, so neither a [PATH=]/[HOST=] section nor a multi-line quoted value can capture it. return $bom . $lines . $eol . $content; } /** * Whether ini content sets a directive outside any [PATH=]/[HOST=] section, read the way PHP reads php.ini. * * @param string $content ini content without BOM. * @param string $key Directive name (case-sensitive, like PHP). * * @return bool */ function ini_sets_globally( string $content, string $key ) { return null !== ini_global_value( $content, $key ); } /** * The value a directive gets outside any [PATH=]/[HOST=] section, read the way PHP reads php.ini. * * @param string $content ini content without BOM. * @param string $key Directive name (case-sensitive, like PHP). * * @return string|null The value as PHP's ini scanner gives it (On is "1", Off is ""), or null when it isn't set there. */ function ini_global_value( string $content, string $key ) { $global = ini_global_part( $content ); // Without sections, entries of ordinary sections are global and `foo[key] = x` stays an array, so it can't match. $ini = @parse_ini_string( $global, false, INI_SCANNER_NORMAL ); // PHP applies the lines above a syntax error, so only that part counts. for ( $lines = preg_split( '/(?<=\n)|(?<=\r)(?!\n)/', $global ), $n = count( $lines ) - 1; false === $ini && $n > 0; $n-- ) { $ini = @parse_ini_string( implode( '', array_slice( $lines, 0, $n ) ), false, INI_SCANNER_NORMAL ); } return false !== $ini && array_key_exists( $key, $ini ) && ! is_array( $ini[ $key ] ) ? (string) $ini[ $key ] : null; } /** * The part of ini content before the first section named PATH or HOST plus at least one more character. * * PHP scopes every entry after such a header to it, even under later ordinary headers. The parser decides where each * header ends and what it is called, so any line ending, quoted names and several headers on one line are handled. * * @param string $content ini content without BOM. * * @return string */ function ini_global_part( string $content ) { $probe = '__ee_ini_probe_' . md5( $content ); for ( $open = strpos( $content, '[' ); false !== $open; $open = strpos( $content, '[', $open + 1 ) ) { // Only where the parser expects a new entry can '[' start a header: not in a quoted value, a comment or a value. $probed = @parse_ini_string( substr( $content, 0, $open ) . $probe . ' = 1', true, INI_SCANNER_NORMAL ); if ( false === $probed || ! array_key_exists( $probe, $probed ) && ! array_filter( $probed, function ( $entry ) use ( $probe ) { return is_array( $entry ) && array_key_exists( $probe, $entry ); } ) ) { continue; } // A quoted name may contain ']', so try each one until the text up to it parses. for ( $close = strpos( $content, ']', $open ); false !== $close; $close = strpos( $content, ']', $close + 1 ) ) { if ( false === @parse_ini_string( substr( $content, 0, $close + 1 ), true, INI_SCANNER_NORMAL ) ) { continue; } $header = @parse_ini_string( substr( $content, $open, $close - $open + 1 ), true, INI_SCANNER_NORMAL ); // PHP scopes only a name with something after PATH or HOST; entries under a bare [PATH] stay global. if ( is_array( $header ) && 1 === count( $header ) && preg_match( '/^(PATH|HOST)./is', (string) key( $header ) ) ) { return substr( $content, 0, $open ); } break; } } return $content; } /** * Whether a site's PHP version deprecates session.sid_length and session.sid_bits_per_character (PHP 8.4 and later). * * @param string $php_version Site PHP version; `latest` sites run the php8.4 image. * * @return bool */ function php_deprecates_session_sid_settings( $php_version ) { $php_version = (string) $php_version; return 'latest' === $php_version || ( preg_match( '/^\d+\.\d+$/', $php_version ) && version_compare( $php_version, '8.4', '>=' ) ); } /** * Remove session.sid_length and session.sid_bits_per_character, wherever they are set, and the shipped comments that document them. * Every other line is kept byte for byte. * * @param string $content php.ini content. * * @return array New content, and the removed directive lines => whether the value is the one EasyEngine shipped. * * @throws \Exception When the edit would change how PHP reads any other setting, or the file doesn't parse. */ function strip_session_sid_settings( string $content ) { // Values EasyEngine shipped; any other value was set by the user. $shipped_values = [ 'session.sid_length' => '26', 'session.sid_bits_per_character' => '5', ]; // Comments shipped right above each directive, removed together with it. $shipped_comments = [ 'session.sid_length' => [ '; Set session ID character length. This value could be between 22 to 256.', '; Shorter length than default is supported only for compatibility reason.', '; Users should use 32 or more chars.', '; http://php.net/session.sid-length', '; Default Value: 32', '; Development Value: 26', '; Production Value: 26', ], 'session.sid_bits_per_character' => [ '; Define how many bits are stored in each character when converting', '; the binary hash data to something readable.', '; Possible values:', '; 4 (4 bits: 0-9, a-f)', '; 5 (5 bits: 0-9, a-v)', '; 6 (6 bits: 0-9, a-z, A-Z, "-", ",")', '; Default Value: 4', '; Development Value: 5', '; Production Value: 5', '; http://php.net/session.hash-bits-per-character', ], ]; // Entry of the quick reference at the top of the shipped php.ini. $shipped_quick_reference = [ '; session.sid_bits_per_character', '; Default Value: 4', '; Development Value: 5', '; Production Value: 5', ]; // After every LF, and after a CR not followed by LF, so a CR-only file isn't read as one line. $lines = preg_split( '/(?<=\n)|(?<=\r)(?!\n)/', $content, -1, PREG_SPLIT_NO_EMPTY ); $norm = array_map( function ( $line ) { return rtrim( $line, " \t\r\n" ); }, $lines ); $drop = []; $removed = []; $quick_count = count( $shipped_quick_reference ); foreach ( array_keys( $norm ) as $i ) { if ( array_slice( $norm, $i, $quick_count ) === $shipped_quick_reference ) { $drop += array_fill( $i, $quick_count, true ); if ( isset( $norm[ $i + $quick_count ] ) && '' === $norm[ $i + $quick_count ] ) { $drop[ $i + $quick_count ] = true; } } } foreach ( $norm as $i => $line ) { if ( ! preg_match( '/^\s*(session\.sid_length|session\.sid_bits_per_character)\s*=(.*)$/', $line, $matches ) ) { continue; } $name = $matches[1]; $drop[ $i ] = true; // Inline comment and quotes don't change the value. $value = trim( preg_replace( '/;.*$/', '', $matches[2] ), " \t\"'" ); $removed[ trim( $line ) ] = $shipped_values[ $name ] === $value; $comments = $shipped_comments[ $name ]; $count = count( $comments ); if ( $i >= $count && array_slice( $norm, $i - $count, $count ) === $comments ) { $drop += array_fill( $i - $count, $count, true ); if ( isset( $norm[ $i + 1 ] ) && '' === $norm[ $i + 1 ] ) { $drop[ $i + 1 ] = true; } } } $new_content = implode( '', array_diff_key( $lines, $drop ) ); // A quoted value can span lines, so PHP must read the result as the original minus the two directives. if ( $new_content !== $content ) { if ( references_session_sid_settings( $new_content ) ) { throw new \Exception( 'another setting refers to them as ${...}, which PHP would then read as empty or as its fallback, so the file was left as it is; remove session.sid_length and session.sid_bits_per_character by hand.' ); } // Flat too: a repeated section header hides the earlier section from the sectioned parse, but PHP still reads it. foreach ( [ true, false ] as $sections ) { $expected = @parse_ini_string( $content, $sections, INI_SCANNER_NORMAL ); if ( is_array( $expected ) ) { foreach ( $expected as $key => $value ) { if ( is_array( $value ) ) { $expected[ $key ] = array_diff_key( $value, $shipped_values ); } } $expected = array_diff_key( $expected, $shipped_values ); } if ( ! is_array( $expected ) || @parse_ini_string( $new_content, $sections, INI_SCANNER_NORMAL ) !== $expected ) { throw new \Exception( 'the file does not parse, or removing them line by line would change other settings (a quoted value spanning lines), so it was left as it is; remove session.sid_length and session.sid_bits_per_character by hand.' ); } } // A repeated section header can hide a removed line inside a quoted value from the parses above; such a line has an edge where the file so far doesn't parse. $edges = []; foreach ( array_keys( $drop ) as $i ) { $edges[ $i ] = true; $edges[ $i + 1 ] = true; } foreach ( array_keys( $edges ) as $edge ) { if ( ! is_array( @parse_ini_string( implode( '', array_slice( $lines, 0, $edge ) ), false, INI_SCANNER_NORMAL ) ) ) { throw new \Exception( 'the file does not parse, or removing them line by line would change other settings (a quoted value spanning lines), so it was left as it is; remove session.sid_length and session.sid_bits_per_character by hand.' ); } } } return [ $new_content, $removed ]; } /** * Whether INI content refers to session.sid_length or session.sid_bits_per_character as ${...}. * PHP resolves ${...} from the settings read so far, which parse_ini_string() can't see; it trims spaces around the name and takes a :- fallback. * * @param string $content INI content. * * @return bool */ function references_session_sid_settings( string $content ) { return (bool) preg_match( '/\$\{[ \t]*session\.sid_(length|bits_per_character)[ \t]*(\}|:-)/', $content ); } /** * Real path of a directory in a site's php config. config/php is EasyEngine's own link to the site's config volume; everything below it belongs to the site's php container. * * @param string $site_fs_path Site root. * @param string $sub Directory below config/php. * * @return string * @throws \Exception */ function site_php_conf_dir( string $site_fs_path, string $sub = 'php/conf.d' ) { $base = realpath( $site_fs_path . '/config/php' ); if ( false === $base ) { throw new \Exception( 'could not resolve config/php' ); } return $base . '/' . $sub; } /** * Open a file in a directory the site's php container can change, as a handle for every later read and write, so nothing swapped in at that path is followed afterwards. * * Host root must not write through a symlink, a hard link or a swapped directory, so the handle has to be the regular file in $dir. On Linux the kernel's own path of the open file decides; elsewhere the path is checked before and after opening. * * Left open: a symlink, FIFO or device renamed into place between the lstat() check and the open is opened (non-blocking) and closed unread and unwritten, and opening some devices has side effects. Only an open with O_NOFOLLOW/openat, which PHP lacks, or one inside the container closes that. * * @param string $file Path. * @param string $mode 'r' or 'r+'. Nothing is created: PHP expands every path it creates or links to an absolute one, so it can't be confined to a directory the container can swap. * @param string $dir Real path of the directory the file must be in. * * @return resource * @throws \Exception */ function open_container_file( string $file, string $mode, string $dir ) { if ( 'r' !== $mode && 'r+' !== $mode ) { throw new \InvalidArgumentException( "Unsupported mode $mode." ); } $name = basename( $file ); clearstatcache( true ); if ( realpath( dirname( $file ) ) !== $dir ) { throw new \Exception( "a directory above $name is a symlink" ); } // PHP can't open without following a symlink (no O_NOFOLLOW, O_PATH or openat), so refuse anything but a regular file before opening it. $before = @lstat( $file ); if ( ! $before ) { throw new \Exception( "$name is missing" ); } if ( 0100000 !== ( $before['mode'] & 0170000 ) ) { throw new \Exception( "$name is not a regular file" ); } // Non-blocking ('n', O_NONBLOCK): a FIFO or device swapped in after the check above is still opened, but can't stall, and is closed below before any read or write. $handle = @fopen( $file, $mode . 'n' ); if ( false === $handle ) { throw new \Exception( "could not open $name" ); } $opened = fstat( $handle ); clearstatcache( true ); $linked = @lstat( $file ); $real = opened_file_path( $handle ); $error = null; if ( ! $opened || ! $linked || $opened['dev'] !== $before['dev'] || $opened['ino'] !== $before['ino'] || $opened['dev'] !== $linked['dev'] || $opened['ino'] !== $linked['ino'] || 0100000 !== ( $opened['mode'] & 0170000 ) || 0100000 !== ( $linked['mode'] & 0170000 ) ) { $error = "$name is not a regular file"; } elseif ( realpath( dirname( $file ) ) !== $dir || ( null !== $real && "$dir/$name" !== $real ) ) { $error = "a directory above $name is a symlink"; } elseif ( 'r' !== $mode && 1 !== $opened['nlink'] ) { $error = "$name has other hard links"; } if ( null !== $error ) { fclose( $handle ); throw new \Exception( $error ); } stream_set_blocking( $handle, true ); return $handle; } /** * The kernel's path of an open file, from /proc/self/fd, which no later swap of a directory changes. * * @param resource $handle Open file. * * @return string|null Null without /proc (macOS); '' when the file isn't found there. */ function opened_file_path( $handle ) { $want = fstat( $handle ); $fds = $want ? @scandir( '/proc/self/fd' ) : false; if ( ! $fds ) { return null; } clearstatcache( true ); foreach ( $fds as $fd ) { if ( ! preg_match( '/^\d+$/', $fd ) ) { continue; } $stat = @stat( "/proc/self/fd/$fd" ); if ( $stat && $stat['dev'] === $want['dev'] && $stat['ino'] === $want['ino'] ) { $path = @readlink( "/proc/self/fd/$fd" ); return false === $path ? '' : $path; } } return ''; } /** * Overwrite an open file through its handle: owner, mode and inode stay (macOS bind-mounts single config files). * * @param resource $handle Handle from open_container_file(). * @param string $content New content. * * @throws \Exception When the file doesn't hold exactly $content afterwards. */ function rewrite_container_file( $handle, string $content ) { // The read-back stops one byte past the content, so data the container appends can't make it unbounded. if ( ! rewind( $handle ) || strlen( $content ) !== fwrite( $handle, $content ) || ! fflush( $handle ) || ! ftruncate( $handle, strlen( $content ) ) || ! rewind( $handle ) || stream_get_contents( $handle, strlen( $content ) + 1 ) !== $content ) { throw new \Exception( 'could not write ' . basename( stream_get_meta_data( $handle )['uri'] ) ); } } /** * Read an open file, at most $max bytes, so a file the container keeps growing can't exhaust memory. * * @param resource $handle Handle from open_container_file(). * @param int $max Largest size accepted. * * @return string * @throws \Exception When it can't be read or is larger. */ function read_container_file( $handle, int $max = 65536 ) { $name = basename( stream_get_meta_data( $handle )['uri'] ); $content = stream_get_contents( $handle, $max + 1 ); if ( false === $content ) { throw new \Exception( "could not read $name" ); } if ( strlen( $content ) > $max ) { throw new \Exception( "$name is larger than $max bytes" ); } return $content; } /** * Read a file in a directory the site's php container can change. * * @param string $file Path. * @param string $dir Real path of the directory the file must be in. * @param int $max Largest size accepted. * * @return string * @throws \Exception When it is missing, not a regular file in $dir, unreadable or larger than $max. */ function get_container_file_contents( string $file, string $dir, int $max = 1048576 ) { $handle = open_container_file( $file, 'r', $dir ); try { return read_container_file( $handle, $max ); } finally { fclose( $handle ); } } /** * Overwrite an existing file in a directory the site's php container can change, in place, so owner, mode and inode stay. * * @param string $file Path. * @param string $dir Real path of the directory the file must be in. * @param string $content New content. * @param string|null $expected Content the file must still hold, e.g. the one the change was made from; null for any. * @param int $max Largest current size accepted. * * @return bool Whether it was written; false when it already held $content. * @throws \Exception When it isn't a regular file in $dir, holds something else than $expected, or can't be written (the old content is put back). */ function put_container_file_contents( string $file, string $dir, string $content, ?string $expected = null, int $max = 1048576 ) { // Compared read-only first, so a file that needs no change isn't opened for writing (a hard link or an immutable file would refuse that). $current = get_container_file_contents( $file, $dir, $max ); if ( null !== $expected && $current !== $expected ) { throw new \Exception( basename( $file ) . ' changed while it was checked' ); } if ( $current === $content ) { return false; } $handle = open_container_file( $file, 'r+', $dir ); try { if ( read_container_file( $handle, $max ) !== $current ) { throw new \Exception( basename( $file ) . ' changed while it was checked' ); } try { rewrite_container_file( $handle, $content ); } catch ( \Exception $e ) { // A short write leaves part of the file; put the old one back. rewrite_container_file( $handle, $current ); throw $e; } } finally { fclose( $handle ); } return true; } /** * Refuse a directory tree that holds anything but directories and regular files. * * Only conclusive while nothing can change the tree, e.g. with the site's php container stopped. * * @param string $dir Real path of the directory. * * @throws \Exception Naming the first other entry. */ function check_plain_tree( string $dir ) { clearstatcache( true ); $names = @scandir( $dir ); if ( false === $names ) { throw new \Exception( "could not read $dir" ); } foreach ( array_diff( $names, [ '.', '..' ] ) as $name ) { $path = "$dir/$name"; $stat = @lstat( $path ); $type = $stat ? $stat['mode'] & 0170000 : 0; if ( 0040000 === $type ) { check_plain_tree( $path ); } elseif ( 0100000 !== $type ) { throw new \Exception( "$path is not a regular file or directory" ); } } } /** * Give a user a directory in a site's config volume and every directory and regular file below it, without following symlinks. * * Symlinks, FIFOs and devices are left alone, and a directory must still be at its real path before its entries are changed. * * @param string $dir Real path of the directory. * @param string|int $user User name or ID. * * @throws \Exception */ function chown_container_tree( string $dir, $user ) { clearstatcache( true ); $stat = @lstat( $dir ); if ( ! $stat || 0040000 !== ( $stat['mode'] & 0170000 ) || realpath( $dir ) !== $dir ) { throw new \Exception( "$dir is not a directory, or a directory above it is a symlink" ); } if ( ! @lchown( $dir, $user ) ) { throw new \Exception( "could not change the owner of $dir" ); } $names = @scandir( $dir ); if ( false === $names ) { throw new \Exception( "could not read $dir" ); } foreach ( array_diff( $names, [ '.', '..' ] ) as $name ) { $path = "$dir/$name"; clearstatcache( true ); $entry = @lstat( $path ); $type = $entry ? $entry['mode'] & 0170000 : 0; if ( 0040000 === $type ) { chown_container_tree( $path, $user ); } elseif ( 0100000 === $type ) { if ( realpath( $dir ) !== $dir ) { throw new \Exception( "a directory above $path is a symlink" ); } if ( ! @lchown( $path, $user ) ) { throw new \Exception( "could not change the owner of $path" ); } } } }