diff --git a/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php b/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php index b8e59d26..6d2fd0a4 100644 --- a/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php +++ b/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php @@ -4,6 +4,9 @@ use EE; use EE\Model\Site; +use function EE\Site\Utils\get_container_file_contents; +use function EE\Site\Utils\put_container_file_contents; +use function EE\Site\Utils\site_php_conf_dir; /** * Removes session.sid_length and session.sid_bits_per_character, deprecated as of PHP 8.4, from the php.ini of PHP 8.4 and 8.5 sites. @@ -12,6 +15,9 @@ */ class DropDeprecatedSessionSidSettings extends Base { + /** Largest php.ini read; the shipped one is about 70 KB. */ + const MAX_SIZE = 1048576; + /** `latest` sites run the php8.4 image. */ const PHP_VERSIONS = [ '8.4', '8.5', 'latest' ]; @@ -106,15 +112,7 @@ public function down() { if ( false === $old ) { throw new \Exception( 'Could not read ' . $change['backup'] ); } - $handle = self::open_ini( $change['ini'] ); - if ( ! $handle ) { - throw new \Exception( $change['ini'] . ' is gone.' ); - } - try { - self::rewrite( $handle, $old, $change['ini'] ); - } finally { - fclose( $handle ); - } + put_container_file_contents( $change['ini'], $change['dir'], $old, null, self::MAX_SIZE ); $this->fs->remove( $change['backup'] ); self::reload_fpm( $change['site'] ); } catch ( \Throwable $e ) { @@ -134,42 +132,41 @@ private function migrate_site( $site ) { $ini = $site->site_fs_path . '/config/php/php/php.ini'; // Missing on macOS, where php.ini stays in the container. - $handle = self::open_ini( $ini ); - if ( ! $handle ) { + if ( ! file_exists( $ini ) && ! is_link( $ini ) ) { EE::debug( "Skipping $site->site_url: no php.ini at $ini." ); return; } - try { - $content = stream_get_contents( $handle ); - if ( false === $content ) { - throw new \Exception( "Could not read $ini." ); - } + // The site's php container can swap anything in its config volume, so php.ini is read and rewritten in place through a checked handle. + $dir = site_php_conf_dir( $site->site_fs_path, 'php' ); + $content = get_container_file_contents( $ini, $dir, self::MAX_SIZE ); - list( $new_content, $removed ) = self::strip_settings( $content ); - if ( $new_content === $content ) { - EE::debug( "$ini has no deprecated session ID settings." ); + list( $new_content, $removed ) = self::strip_settings( $content ); + if ( $new_content === $content ) { + EE::debug( "$ini has no deprecated session ID settings." ); - return; - } + return; + } - $backup = EE_BACKUP_DIR . '/' . $site->site_url . '/php.ini.with-session-sid-settings'; - $this->fs->dumpFile( $backup, $content ); + $backup = EE_BACKUP_DIR . '/' . $site->site_url . '/php.ini.with-session-sid-settings'; + $this->fs->dumpFile( $backup, $content ); - try { - self::rewrite( $handle, $new_content, $ini ); - } catch ( \Exception $e ) { - self::rewrite( $handle, $content, $ini ); - throw $e; + try { + put_container_file_contents( $ini, $dir, $new_content, $content, self::MAX_SIZE ); + } catch ( \Exception $e ) { + // A failed write may have left php.ini partly written; any other failure left it as it was. + if ( 0 === strpos( $e->getMessage(), 'could not write' ) ) { + throw new \Exception( $e->getMessage() . "; the previous file is $backup" ); } - } finally { - fclose( $handle ); + $this->fs->remove( $backup ); + throw $e; } $this->changed[] = [ 'site' => $site, 'ini' => $ini, + 'dir' => $dir, 'backup' => $backup, ]; EE::debug( "Removed the deprecated session ID settings from $ini; the previous file is $backup." ); @@ -195,7 +192,8 @@ private function migrate_site( $site ) { */ public static function strip_settings( $content ) { - $lines = preg_split( '/(?<=\n)/', $content, -1, PREG_SPLIT_NO_EMPTY ); + // 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" ); @@ -264,54 +262,6 @@ function ( $line ) { return [ $new_content, $removed ]; } - /** - * Opens php.ini for rewriting in place, so no other path is created, followed or re-resolved. - * The directory is mounted into the site's php container, so nothing in it is trusted. - * - * @param string $file php.ini path. - * - * @return resource|null Null when there is no regular file. - * @throws \Exception - */ - private static function open_ini( $file ) { - - if ( is_link( $file ) || ! is_file( $file ) ) { - return null; - } - - $handle = fopen( $file, 'r+' ); - if ( false === $handle ) { - throw new \Exception( "Could not open $file." ); - } - - // Swapped for a symlink after the check, or a hard link to another file. - $opened = fstat( $handle ); - clearstatcache( true, $file ); - $linked = lstat( $file ); - if ( ! $opened || ! $linked || $opened['dev'] !== $linked['dev'] || $opened['ino'] !== $linked['ino'] || 0100000 !== ( $linked['mode'] & 0170000 ) || 1 !== $opened['nlink'] ) { - fclose( $handle ); - throw new \Exception( "$file is not a regular file." ); - } - - return $handle; - } - - /** - * The removal only shortens the file, so writing over it needs no new space; mode and owner stay. - * - * @param resource $handle Handle from open_ini(). - * @param string $content New content. - * @param string $file php.ini path, for the error. - * - * @throws \Exception - */ - private static function rewrite( $handle, $content, $file ) { - - if ( ! rewind( $handle ) || strlen( $content ) !== fwrite( $handle, $content ) || ! fflush( $handle ) || ! ftruncate( $handle, strlen( $content ) ) ) { - throw new \Exception( "Could not write $file." ); - } - } - /** * PHP-FPM reads php.ini only when it starts; a graceful reload (as `ee site reload --php` does) applies it. * Stopped containers read it when they start. diff --git a/src/helper/Site_Backup_Restore.php b/src/helper/Site_Backup_Restore.php index 639e7ba0..d2c83d59 100644 --- a/src/helper/Site_Backup_Restore.php +++ b/src/helper/Site_Backup_Restore.php @@ -53,6 +53,9 @@ class Site_Backup_Restore { // Global backup lock handle for serializing backups private $global_backup_lock_handle = null; + // ID of the site's php container while its config is restored; '' when it isn't running. + private $php_container_id; + public function __construct() { $this->fs = new Filesystem(); } @@ -1684,17 +1687,279 @@ private function restore_php_conf( $backup_dir ) { } if ( $this->fs->exists( sprintf( '%s/php', $backup_dir ) ) ) { - $restore_command = sprintf( 'rsync -a %s/php/php-fpm.d/ %s/config/php/php-fpm.d/', $backup_dir, EE_ROOT_DIR . '/sites/' . $this->site_data['site_url'] ); - EE::exec( $restore_command ); + $this->restore_php_conf_files( $backup_dir . '/php' ); + } - $restore_command = sprintf( 'rsync -a %s/php/php/php.ini %s/config/php/php/php.ini', $backup_dir, EE_ROOT_DIR . '/sites/' . $this->site_data['site_url'] ); - EE::exec( $restore_command ); + $this->keep_session_upload_progress_off(); + } - $restore_command = sprintf( 'rsync -a %s/php/php/conf.d/custom.ini %s/config/php/php/conf.d/custom.ini', $backup_dir, EE_ROOT_DIR . '/sites/' . $this->site_data['site_url'] ); - EE::exec( $restore_command ); + /** + * Copy the backed-up php-fpm.d tree, php.ini and custom.ini over the site's ones. + * + * The container can swap anything in its config volume, so existing files are written in place through checked handles and missing entries are created inside the container; the backup came from that volume too, so it may hold only directories and regular files. + * Permission bits come from the backup; owners don't (the backup has none), so a created entry belongs to its parent's owner and an existing one keeps its own. + * + * @param string $source Extracted `php` directory of the backup. + */ + private function restore_php_conf_files( $source ) { + + $site_url = $this->site_data['site_url']; + $site_dir = EE_ROOT_DIR . '/sites/' . $site_url; + clearstatcache( true ); + // Some 7-Zip versions store config/php itself as a link, which points back at the live volume. + if ( is_link( $source ) ) { + EE::warning( 'Could not restore the php config: the backup holds a symlink instead of it.' ); + + return; } - $this->keep_session_upload_progress_off(); + $entries = []; + $refused = []; + foreach ( [ 'php', 'php/php.ini', 'php/conf.d', 'php/conf.d/custom.ini' ] as $path ) { + $this->list_php_conf_backup( $source, $path, false, $entries, $refused ); + } + $this->list_php_conf_backup( $source, 'php-fpm.d', true, $entries, $refused ); + foreach ( $refused as $path => $reason ) { + EE::warning( "Could not restore config/php/$path: $reason." ); + } + + $this->php_container_id = null; + $kept = EE_BACKUP_DIR . '/' . $site_url . '-php-conf-not-restored-' . date( 'Ymd-His' ); + $not_restored = []; + $skipped = []; + $mode_not_set = []; + $dir_modes = []; + $set_mode = function ( $path, $mode ) use ( &$mode_not_set ) { + try { + if ( ! $this->php_container_op( 'mode', $path, $mode ) ) { + throw new \Exception( 'could not change it in its php container' ); + } + + return true; + } catch ( \Exception $e ) { + $mode_not_set[] = "config/php/$path ({$e->getMessage()})"; + + return false; + } + }; + foreach ( $entries as $path => $stat ) { + $is_dir = 0040000 === ( $stat['mode'] & 0170000 ); + $mode = sprintf( '%04o', $stat['mode'] & 0777 ); + // A directory stays writable and searchable by its owner until everything below it is restored. + $mode_now = $is_dir ? sprintf( '%04o', $stat['mode'] & 0777 | 0700 ) : $mode; + $parent = dirname( $path ); + $content = null; + if ( isset( $skipped[ $parent ] ) ) { + $skipped[ $path ] = true; + continue; + } + try { + if ( ! $is_dir ) { + $content = \EE\Site\Utils\get_container_file_contents( "$source/$path", realpath( $source ) . ( '.' === $parent ? '' : "/$parent" ) ); + } + if ( isset( $not_restored[ $parent ] ) ) { + $not_restored[ $path ] = $not_restored[ $parent ]; + if ( $is_dir ) { + continue; + } + throw new \Exception( "config/php/{$not_restored[ $parent ]} was not restored" ); + } + $dir = rtrim( \EE\Site\Utils\site_php_conf_dir( $site_dir, '.' === $parent ? '' : $parent ), '/' ); + $dest = "$dir/" . basename( $path ); + clearstatcache( true ); + $current = @lstat( $dest ); + if ( realpath( $dir ) !== $dir ) { + throw new \Exception( "config/php/$parent is not a directory, or a directory above it is a symlink" ); + } + if ( ! $current ) { + // On macOS config/php isn't mounted (only custom.ini is), so an entry missing there has no effect, as before. + if ( 'Darwin' === PHP_OS_FAMILY ) { + EE::debug( "Not restoring config/php/$path: missing in the site." ); + $skipped[ $path ] = true; + continue; + } + if ( ! $this->php_container_op( $is_dir ? 'dir' : 'file', $path, $mode_now, $content ) ) { + $mode_not_set[] = "config/php/$path (could not change it in its php container)"; + } elseif ( $mode_now !== $mode ) { + $dir_modes[ $path ] = $mode; + } + } elseif ( $is_dir ) { + if ( 0040000 !== ( $current['mode'] & 0170000 ) || realpath( $dest ) !== $dest ) { + throw new \Exception( 'it is not a directory in the site' ); + } + } else { + \EE\Site\Utils\put_container_file_contents( $dest, $dir, $content ); + } + // Not on macOS, where the container doesn't see config/php. + if ( $current && 'Darwin' !== PHP_OS_FAMILY ) { + if ( ( sprintf( '%04o', $current['mode'] & 0777 ) === $mode_now || $set_mode( $path, $mode_now ) ) && $mode_now !== $mode ) { + $dir_modes[ $path ] = $mode; + } + } + } catch ( \Exception $e ) { + $not_restored[ $path ] = $not_restored[ $path ] ?? $path; + $message = "Could not restore config/php/$path ({$e->getMessage()})."; + if ( null !== $content ) { + try { + $this->fs->dumpFile( "$kept/$path", $content ); + $fix = isset( $not_restored[ $parent ] ) ? "once config/php/{$not_restored[ $parent ]} is a plain directory again" : 'once the cause is fixed'; + $message .= " The backed-up file is kept as $kept/$path; put it back $fix, then run: ee site reload $site_url"; + } catch ( \Exception $dump_error ) { + $message .= ' Could not keep the backed-up file either: ' . $dump_error->getMessage(); + } + } + EE::warning( $message ); + } + } + // Deepest first, so a directory is tightened only after everything below it. + foreach ( array_reverse( $dir_modes, true ) as $path => $mode ) { + $set_mode( $path, $mode ); + } + if ( $mode_not_set ) { + EE::warning( 'Restored, but could not set the backed-up mode of: ' . implode( ', ', $mode_not_set ) . '.' ); + } + } + + /** + * Collect a backed-up entry below the extracted `php` directory, and with $recurse everything below it, parents first. + * + * @param string $source Extracted `php` directory of the backup. + * @param string $path Path below it. + * @param bool $recurse Whether to collect a directory's entries. + * @param array $entries Path => lstat() of each directory and regular file. + * @param array $refused Path => reason for each entry that is skipped. + * @param int $depth Directory depth. + * + * @return bool False once the entry limit is reached. + */ + private function list_php_conf_backup( $source, $path, $recurse, array &$entries, array &$refused, $depth = 0 ) { + + $parent = dirname( $path ); + if ( '.' !== $parent && ! isset( $entries[ $parent ] ) ) { + return true; + } + $shown = addcslashes( $path, "\0..\37\177" ); + // Refused entries count too, so a crafted tree can't produce unbounded warnings. + if ( count( $entries ) + count( $refused ) >= 1000 ) { + $refused[ $shown ] = 'the backup holds more than 1000 entries, so this and the rest were not restored'; + + return false; + } + if ( $shown !== $path ) { + $refused[ $shown ] = 'its name holds control characters'; + + return true; + } + $stat = @lstat( "$source/$path" ); + if ( ! $stat ) { + return true; + } + $type = $stat['mode'] & 0170000; + if ( 0040000 !== $type && 0100000 !== $type ) { + $refused[ $path ] = 'the backup holds a symlink or special file there'; + + return true; + } + if ( $depth > 16 ) { + $refused[ $path ] = 'it is nested too deep'; + + return true; + } + $entries[ $path ] = $stat; + if ( ! $recurse || 0040000 !== $type ) { + return true; + } + $handle = @opendir( "$source/$path" ); + if ( false === $handle ) { + return true; + } + $more = true; + while ( $more && false !== ( $name = readdir( $handle ) ) ) { + if ( '.' === $name || '..' === $name ) { + continue; + } + $more = $this->list_php_conf_backup( $source, "$path/$name", true, $entries, $refused, $depth + 1 ); + } + closedir( $handle ); + + return $more; + } + + /** + * Create a directory or file the site's php config lacks, or set the mode of one, from inside its running php container, where a symlink the container made can't point at the host. + * + * The step runs as the owner of the parent directory, which must be a real directory (no symlink in its path), so it can't change what that owner couldn't, and a created entry belongs to that owner. + * + * @param string $op 'dir', 'file' or 'mode'. + * @param string $path Path below config/php (/usr/local/etc in the container). + * @param string $mode Octal permission bits. + * @param string|null $content Content of a created file. + * + * @return bool Whether the mode was set; false when only that failed. + * @throws \Exception + */ + private function php_container_op( $op, $path, $mode, $content = null ) { + + if ( ! preg_match( '#^(php-fpm\.d|php)(/[^/]+)*$#', $path ) || preg_match( '#(^|/)\.\.?(/|$)|[\x00-\x1f\x7f]#', $path ) || ! preg_match( '/^0[0-7]{3}$/', $mode ) ) { + throw new \Exception( 'unexpected path or mode' ); + } + + if ( null === $this->php_container_id ) { + $cwd = getcwd(); + chdir( EE_ROOT_DIR . '/sites/' . $this->site_data['site_url'] ); + $id = trim( EE::launch( \EE_DOCKER::docker_compose_with_custom() . ' ps -q php' )->stdout ); + if ( false !== $cwd ) { + chdir( $cwd ); + } + $this->php_container_id = ( '' !== $id && 'running' === \EE_DOCKER::container_status( $id ) ) ? $id : ''; + } + if ( '' === $this->php_container_id ) { + throw new \Exception( 'its php container is not running' ); + } + + // Changes into the parent and acts relative to it, so a directory swapped above it afterwards can't redirect the step. + // dd's conv=excl creates with O_EXCL, so anything put there after the check (a FIFO too) is refused without being opened. + $script = 'set -e; op=$2; m=$3; p=$1/$4; d=${p%/*}; n=./${p##*/} +cd -P -- "$d" 2>/dev/null && [ "$(pwd -P)" = "$d" ] || exit 3 +if [ "$op" = owner ]; then stat -c %u:%g .; exit 0; fi +if [ "$op" = mode ]; then [ ! -L "$n" ] || exit 4; chmod -- "$m" "$n" || exit 5; exit 0; fi +[ ! -e "$n" ] && [ ! -L "$n" ] || exit 4 +if [ "$op" = dir ]; then mkdir -m "$m" -- "$n"; exit 0; fi +(umask 077; dd of="$n" conv=excl status=none 2>/dev/null) || { if [ -e "$n" ] || [ -L "$n" ]; then exit 4; fi; exit 1; } +chmod -- "$m" "$n" || exit 5'; + $run = function ( $user, $op, $stdin = null ) use ( $path, $mode, $script ) { + return EE::launch( sprintf( 'docker exec %s-u %s %s sh -c %s sh /usr/local/etc %s %s %s%s', $stdin ? '-i ' : '', escapeshellarg( $user ), escapeshellarg( $this->php_container_id ), escapeshellarg( $script ), escapeshellarg( $op ), escapeshellarg( $mode ), escapeshellarg( $path ), $stdin ? ' < ' . escapeshellarg( $stdin ) : '' ) ); + }; + + $owner = $run( 'root', 'owner' ); + $user = trim( $owner->stdout ); + if ( 3 === $owner->return_code || ! preg_match( '/^\d+:\d+$/', $user ) ) { + throw new \Exception( 'a directory above it is not a real directory in its php container' ); + } + + $tmp = null; + if ( 'file' === $op ) { + $tmp = tempnam( \EE\Utils\get_temp_dir(), 'ee-php-conf-' ); + if ( false === $tmp || strlen( $content ) !== file_put_contents( $tmp, $content ) ) { + throw new \Exception( 'could not write a temporary file' ); + } + } + $result = $run( $user, $op, $tmp ); + if ( $tmp ) { + unlink( $tmp ); + } + switch ( $result->return_code ) { + case 0: + return true; + case 5: + return false; + case 3: + throw new \Exception( 'a directory above it is not a real directory in its php container' ); + case 4: + throw new \Exception( 'mode' === $op ? 'it is a symlink in its php container' : 'it appeared in its php container while it was restored' ); + default: + throw new \Exception( 'mode' === $op ? 'could not change it in its php container' : 'it is missing in the site and could not be created in its php container' ); + } } /** @@ -1715,34 +1980,15 @@ private function keep_session_upload_progress_off() { // The directory is in the site's config volume, which its php container can change: every read and write goes through a checked handle. try { - $dir = \EE\Site\Utils\site_php_conf_dir( EE_ROOT_DIR . '/sites/' . $site_url ); - $handle = \EE\Site\Utils\open_container_file( $file, 'r', $dir ); - try { - $content = \EE\Site\Utils\read_container_file( $handle ); - } finally { - fclose( $handle ); - } + $dir = \EE\Site\Utils\site_php_conf_dir( EE_ROOT_DIR . '/sites/' . $site_url ); + $content = \EE\Site\Utils\get_container_file_contents( $file, $dir, 65536 ); $new_content = \EE\Site\Utils\add_session_upload_progress_off( $content ); if ( null === $new_content ) { return; } - $handle = \EE\Site\Utils\open_container_file( $file, 'r+', $dir ); - try { - if ( \EE\Site\Utils\read_container_file( $handle ) !== $content ) { - throw new \Exception( 'it changed while it was checked' ); - } - try { - \EE\Site\Utils\rewrite_container_file( $handle, $new_content ); - } catch ( \Exception $e ) { - // A short write leaves part of the file; put the restored one back. - \EE\Site\Utils\rewrite_container_file( $handle, $content ); - throw $e; - } - } finally { - fclose( $handle ); - } + \EE\Site\Utils\put_container_file_contents( $file, $dir, $new_content, $content, 65536 ); } catch ( \Exception $e ) { EE::warning( "Could not turn off session.upload_progress in the restored custom.ini ({$e->getMessage()}). $manual" ); diff --git a/src/helper/class-ee-site.php b/src/helper/class-ee-site.php index f9dd4ff2..df063437 100644 --- a/src/helper/class-ee-site.php +++ b/src/helper/class-ee-site.php @@ -934,19 +934,38 @@ protected function update_php( $args, $assoc_args ) { $array_data = ( array ) $this->site_data; $this->site_data = reset( $array_data ); - EE::log( 'Taking backup of old php config.' ); $site_backup_dir = $this->site_data['site_fs_path'] . '/.backup'; $php_conf_backup_dir = $site_backup_dir . '/config/php-' . $old_php_version; $php_conf_dir = $this->site_data['site_fs_path'] . '/config/php'; $php_confd_dir = $this->site_data['site_fs_path'] . '/config/php/php/conf.d'; - $this->fs->mkdir( $php_conf_backup_dir ); - $this->fs->mirror( $php_conf_dir, $php_conf_backup_dir ); + $rollback = [ + 'site' => $this->site_data, + 'backup' => $php_conf_backup_dir, + 'stopped' => false, + 'switched' => false, + ]; + + // Pull the new image before php stops, so the site is down only while its config is switched. $this->dump_docker_compose_yml( [ 'nohttps' => $no_https ] ); - EE::log( 'Starting site with new PHP version: ' . $php_version . '. This may take sometime.' ); - $this->enable( $args, [ 'force' => true ] ); + chdir( $this->site_data['site_fs_path'] ); + if ( ! EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' pull php' ) ) { + throw new \Exception( "Could not pull the image of PHP $php_version." ); + } + + // The php container can swap anything in its config volume for a symlink, so it stays stopped until the volume is replaced. + $rollback['stopped'] = $this->stop_php_for_config_change( $php_conf_dir ); + + EE::log( 'Taking backup of old php config.' ); + // A fresh copy, so nothing is written through a symlink an older backup holds; that one is kept aside. + if ( $this->fs->exists( $php_conf_backup_dir ) || is_link( $php_conf_backup_dir ) ) { + $this->fs->rename( $php_conf_backup_dir, $php_conf_backup_dir . '.' . date( 'YmdHis' ) ); + } + $this->fs->mkdir( $php_conf_backup_dir ); + $this->fs->mirror( $php_conf_dir, $php_conf_backup_dir ); EE::log( 'Updating php config.' ); + $rollback['switched'] = true; $temp_dir = EE\Utils\get_temp_dir(); $zip_path = $temp_dir . "phpconf-$php_version.zip"; $unzip_folder = $temp_dir . "php-$php_version"; @@ -978,7 +997,14 @@ protected function update_php( $args, $assoc_args ) { EE::exec( "chown -R www-data: $php_conf_dir" ); + EE::log( 'Starting site with new PHP version: ' . $php_version . '. This may take sometime.' ); + $rollback['stopped'] = true; + $this->enable( $args, [ 'force' => true ], false ); + } catch ( \Exception $e ) { + if ( ! empty( $rollback ) ) { + $this->roll_back_php_update( $rollback, $old_php_version, $no_https ); + } EE::error( $e->getMessage() ); } $site->save(); @@ -987,6 +1013,109 @@ protected function update_php( $args, $assoc_args ) { delem_log( 'site php version update end' ); } + /** + * Put a failed `update --php` back: the old config (with php stopped again), the old compose file, and a running php container. + * + * @param array $rollback Site data as an array, the config backup dir, whether php was stopped and whether the config was switched. + * @param string $old_version PHP version to go back to. + * @param bool $no_https Whether the site has no SSL. + */ + private function roll_back_php_update( $rollback, $old_version, $no_https ) { + + $site_fs_path = $rollback['site']['site_fs_path']; + $manual = 'Run: ee site enable ' . $rollback['site']['site_url'] . ' --force'; + chdir( $site_fs_path ); + $compose = \EE_DOCKER::docker_compose_with_custom(); + + if ( $rollback['switched'] ) { + try { + // enable() may have started the new container already; the volume is only safe to touch with it stopped. + if ( ! EE::exec( "$compose stop php" ) ) { + throw new \Exception( 'could not stop the php container' ); + } + $volume = realpath( $site_fs_path . '/config/php' ); + self::check_config_volume_unused( $volume ); + \EE\Site\Utils\check_plain_tree( $volume ); + $this->fs->remove( array_map( function ( $name ) use ( $volume ) { + return "$volume/$name"; + }, array_diff( scandir( $volume ), [ '.', '..' ] ) ) ); + $this->fs->mirror( $rollback['backup'], $volume ); + \EE\Site\Utils\chown_container_tree( "$volume/php/conf.d", 'www-data' ); + EE::log( 'Restored the previous php config.' ); + } catch ( \Exception $e ) { + EE::warning( "Could not restore the previous php config from {$rollback['backup']} ({$e->getMessage()}). $manual" ); + } + } + + $this->site_data = $rollback['site']; + $this->site_data['php_version'] = $old_version; + try { + $this->dump_docker_compose_yml( [ 'nohttps' => $no_https ] ); + } catch ( \Exception $e ) { + // Keep going so php restarts and the caller reports its error; compose still names the new image, so --refresh, not --force. + $manual = 'Run: ee site enable ' . $rollback['site']['site_url'] . ' --refresh'; + EE::warning( "Could not restore docker-compose.yml to PHP $old_version ({$e->getMessage()}). $manual" ); + } + + if ( $rollback['stopped'] && ! \EE_DOCKER::docker_compose_up( $site_fs_path, [ 'php' ] ) ) { + EE::warning( "Could not start the php container again. $manual" ); + } + } + + /** + * Stop the site's php container, the only one that mounts its config volume, and check the volume holds only directories and regular files. + * + * @param string $php_conf_dir The site's config/php. + * + * @return bool Whether a running container was stopped. + * @throws \Exception When it can't be stopped, another running container mounts the volume, or the volume holds anything else. + */ + private function stop_php_for_config_change( $php_conf_dir ) { + + chdir( $this->site_data['site_fs_path'] ); + $compose = \EE_DOCKER::docker_compose_with_custom(); + $id = trim( EE::launch( "$compose ps -q php" )->stdout ); + $running = '' !== $id && 'running' === \EE_DOCKER::container_status( $id ); + if ( $running && ! EE::exec( "$compose stop php" ) ) { + throw new \Exception( 'Could not stop the php container to update its config.' ); + } + + try { + $volume = realpath( $php_conf_dir ); + if ( false === $volume ) { + throw new \Exception( "Could not resolve $php_conf_dir." ); + } + self::check_config_volume_unused( $volume ); + \EE\Site\Utils\check_plain_tree( $volume ); + } catch ( \Exception $e ) { + $message = 'Could not update the php config: ' . rtrim( $e->getMessage(), '.' ) . '. Nothing was changed.'; + if ( $running && ! EE::exec( "$compose start php" ) ) { + $message .= ' Could not start the php container again. Run: ee site enable ' . $this->site_data['site_url'] . ' --force'; + } + throw new \Exception( $message ); + } + + return $running; + } + + /** + * On Linux config/php links to the volume's _data; a custom compose file may mount it in another service too. + * + * @param string $volume Real path of config/php. + * + * @throws \Exception When a running container still mounts it. + */ + private static function check_config_volume_unused( $volume ) { + + if ( '_data' !== basename( $volume ) ) { + return; + } + $users = EE::launch( 'docker ps -q --filter ' . escapeshellarg( 'volume=' . basename( dirname( $volume ) ) ) ); + if ( $users->return_code || '' !== trim( $users->stdout ) ) { + throw new \Exception( 'The php config volume is still mounted by a running container (' . implode( ', ', preg_split( '/\s+/', trim( $users->stdout ) ) ) . '); stop it and re-run the update.' ); + } + } + protected function sendmail_path_update( $msmtp ) { $custom_ini_path = $this->site_data['site_fs_path'] . '/config/php/php/conf.d/custom.ini'; @@ -1242,7 +1371,12 @@ public function enable( $args, $assoc_args, $exit_on_error = true ) { # Required when newrelic is enabled on site. Newrelic ini is updated via docker-entrypoint. $php_confd_dir = $this->site_data->site_fs_path . '/config/php/php/conf.d'; if ( $this->fs->exists( $php_confd_dir ) ) { - $this->fs->chown( $php_confd_dir, 'www-data', true ); + // The php container may be running and can swap anything in conf.d, so no symlink is followed. + try { + \EE\Site\Utils\chown_container_tree( \EE\Site\Utils\site_php_conf_dir( $this->site_data->site_fs_path ), 'www-data' ); + } catch ( \Exception $e ) { + \EE::warning( sprintf( 'Could not give www-data the php conf.d of %s: %s', $this->site_data->site_url, $e->getMessage() ) ); + } } if ( \EE_DOCKER::docker_compose_up( $this->site_data->site_fs_path, $containers_to_start ) ) { @@ -1280,6 +1414,10 @@ public function enable( $args, $assoc_args, $exit_on_error = true ) { try { update_site_db_entry( $this->site_data['site_url'], $this->site_data ); } catch ( \Exception $e ) { + // A caller that rolls back (update --php) handles it. + if ( ! $exit_on_error ) { + throw $e; + } EE::error( $e->getMessage() ); } diff --git a/src/helper/site-utils.php b/src/helper/site-utils.php index 11175edf..a05e8987 100644 --- a/src/helper/site-utils.php +++ b/src/helper/site-utils.php @@ -1729,21 +1729,22 @@ function ini_global_part( string $content ) { } /** - * Real path of a site's conf.d. config/php is EasyEngine's own link to the site's config volume; everything below it belongs to the site's php container. + * 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 ) { +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 . '/php/conf.d'; + return $base . '/' . $sub; } /** @@ -1751,6 +1752,8 @@ function site_php_conf_dir( string $site_fs_path ) { * * 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. @@ -1770,7 +1773,16 @@ function open_container_file( string $file, string $mode, string $dir ) { throw new \Exception( "a directory above $name is a symlink" ); } - // Non-blocking ('n', O_NONBLOCK), so a FIFO swapped in can't stall the open before it is rejected below. + // 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" ); @@ -1781,7 +1793,7 @@ function open_container_file( string $file, string $mode, string $dir ) { $linked = @lstat( $file ); $real = opened_file_path( $handle ); $error = null; - if ( ! $opened || ! $linked || $opened['dev'] !== $linked['dev'] || $opened['ino'] !== $linked['ino'] || 0100000 !== ( $linked['mode'] & 0170000 ) ) { + 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"; @@ -1866,3 +1878,137 @@ function read_container_file( $handle, int $max = 65536 ) { 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" ); + } + } + } +}