diff --git a/composer.json b/composer.json index 6f613a8d..1662b76c 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,9 @@ "db tables", "db size", "db columns", - "db status" + "db status", + "db users", + "db users create" ] }, "autoload": { diff --git a/db-command.php b/db-command.php index d08f55bb..540b1776 100644 --- a/db-command.php +++ b/db-command.php @@ -10,3 +10,4 @@ } WP_CLI::add_command( 'db', 'DB_Command' ); +WP_CLI::add_command( 'db users', 'WP_CLI\DB\Users_Command' ); diff --git a/features/db-users.feature b/features/db-users.feature new file mode 100644 index 00000000..8a884cb9 --- /dev/null +++ b/features/db-users.feature @@ -0,0 +1,141 @@ +Feature: Manage database users + + # The cleanup uses `I try` with a plain DROP USER, as MySQL 5.6 has no DROP USER IF EXISTS. + @require-mysql-or-mariadb + Scenario: Create a database user without privileges + Given a WP install + And I try `wp db query "DROP USER 'wpcli_dbusers_1'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}` + + When I run `wp db users create wpcli_dbusers_1 '%' --password=first-Pa55 --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}` + Then STDOUT should be: + """ + Success: Created database user 'wpcli_dbusers_1'@'%'. + """ + + When I run `wp db query "SELECT User FROM mysql.user WHERE User = 'wpcli_dbusers_1'" --skip-column-names --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}` + Then STDOUT should be: + """ + wpcli_dbusers_1 + """ + + When I try `wp db query "SELECT 1" --dbuser=wpcli_dbusers_1 --dbpass=first-Pa55` + Then the return code should be 1 + And STDERR should contain: + """ + denied + """ + + When I try `wp db users create wpcli_dbusers_1 '%' --password=dupe-Pa55-Value --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}` + Then the return code should be 1 + And STDERR should contain: + """ + Error: ERROR 1396 + """ + And STDERR should not contain: + """ + dupe-Pa55-Value + """ + + When I run `wp db query "DROP USER 'wpcli_dbusers_1'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}` + + @require-mysql-or-mariadb + Scenario: Create a database user with privileges on the WordPress database + Given a WP install + And I try `wp db query "DROP USER 'wpcli_dbusers_2'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}` + + When I run `echo "it's a \"tricky\" pass\\word;" | wp db users create wpcli_dbusers_2 '%' --grant-privileges --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}` + Then STDOUT should be: + """ + Success: Created database user 'wpcli_dbusers_2'@'%' with all privileges on '{DB_NAME}'. + """ + + When I run `wp db query "SELECT option_value FROM wp_options WHERE option_name = 'siteurl'" --skip-column-names --dbuser=wpcli_dbusers_2 --dbpass="it's a \"tricky\" pass\\word;"` + Then STDOUT should be: + """ + https://example.com + """ + + When I run `wp db query "DROP USER 'wpcli_dbusers_2'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}` + + @require-mysql-or-mariadb + Scenario: A password with an internal CRLF is kept as is + Given a WP install + And I try `wp db query "DROP USER 'wpcli_dbusers_5'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}` + + When I run `printf 'crlf\r\nPa55\n' | wp db users create wpcli_dbusers_5 '%' --grant-privileges --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}` + Then STDOUT should contain: + """ + Success: + """ + + When I run `wp db query "SELECT 'connected'" --skip-column-names --dbuser=wpcli_dbusers_5 --dbpass="$(printf 'crlf\r\nPa55')"` + Then STDOUT should be: + """ + connected + """ + + When I try `wp db query "SELECT 'connected'" --skip-column-names --dbuser=wpcli_dbusers_5 --dbpass="$(printf 'crlf\nPa55')"` + Then the return code should be 1 + And STDERR should contain: + """ + ERROR 1045 + """ + + When I run `wp db query "DROP USER 'wpcli_dbusers_5'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}` + + @require-mysql-or-mariadb + Scenario: The new user's password is not leaked into the debug output + Given a WP install + And I try `wp db query "DROP USER 'wpcli_dbusers_3'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}` + + When I try `wp db users create wpcli_dbusers_3 '%' --password=leak-Ch3ck-Value --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD} --debug=db` + Then the return code should be 0 + And STDERR should contain: + """ + Creating database user 'wpcli_dbusers_3'@'%'. + """ + And STDERR should not contain: + """ + leak-Ch3ck-Value + """ + + When I run `wp db query "DROP USER 'wpcli_dbusers_3'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}` + + @require-mysql-or-mariadb + Scenario: A password is required + Given a WP install + + When I try `wp db users create wpcli_dbusers_4 '%' --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD} < /dev/null` + Then the return code should be 1 + And STDERR should contain: + """ + Error: A password is required. + """ + + @require-mysql-or-mariadb + Scenario: Only the create subcommand is available + Given a WP install + + When I try `wp db users drop --yes` + Then the return code should be 1 + And STDERR should contain: + """ + is not a registered subcommand of 'db users' + """ + + When I run `wp db check` + Then STDOUT should contain: + """ + Success: Database checked. + """ + + @require-sqlite + Scenario: Database users are not supported with SQLite + Given a WP install + + When I try `wp db users create someone --password=some-Pa55` + Then the return code should be 1 + And STDERR should be: + """ + Error: Database users are not supported with SQLite. + """ diff --git a/src/Users_Command.php b/src/Users_Command.php new file mode 100644 index 00000000..d84fb8c1 --- /dev/null +++ b/src/Users_Command.php @@ -0,0 +1,271 @@ +]: + * Password: + * Success: Created database user 'appuser'@'localhost' with all privileges on 'wp_database'. + * + * @when after_wp_config_load + */ +class Users_Command extends WP_CLI_Command { + + use DB_Command_SQLite; + + /** + * Creates a database user. + * + * The statements are sent to the `mysql` client through STDIN, so the new + * user's password doesn't show up in the process list or in `--debug` output. + * + * ## OPTIONS + * + * + * : Name of the user to create. + * + * [] + * : Host the user is allowed to connect from. Use `%` for any host. + * --- + * default: localhost + * --- + * + * [--password=] + * : Password for the new user. If omitted, it is read from STDIN when input is piped, and prompted for otherwise. Avoid passing it here on shared systems, as it ends up in the shell history. + * + * [--grant-privileges] + * : Grant the new user all privileges on the WordPress database (DB_NAME). + * + * [--dbuser=] + * : Username to connect as. Needs the `CREATE USER` privilege. Defaults to DB_USER. + * + * [--dbpass=] + * : Password to connect with. Defaults to DB_PASSWORD. + * + * [--defaults] + * : Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration. + * + * ## EXAMPLES + * + * # Create a user that can connect from any host, reading its password from a file. + * # This connects as DB_USER, which then needs the CREATE USER privilege. + * $ wp db users create appuser '%' < password.txt + * Success: Created database user 'appuser'@'%'. + * + * # Create a user with full privileges on the WordPress database. + * $ wp db users create appuser localhost --grant-privileges --dbuser=root --prompt=dbpass + * 1/1 [--dbpass=]: + * Password: + * Success: Created database user 'appuser'@'localhost' with all privileges on 'wp_database'. + */ + public function create( $args, $assoc_args ) { + if ( $this->is_sqlite() ) { + WP_CLI::error( 'Database users are not supported with SQLite.' ); + } + + $username = $args[0]; + $host = isset( $args[1] ) ? $args[1] : 'localhost'; + + if ( '' === $username ) { + WP_CLI::error( 'The username cannot be empty.' ); + } + + $password = $this->get_password( $assoc_args ); + $account = self::esc_sql_string( $username ) . '@' . self::esc_sql_string( $host ); + + // Turn off NO_BACKSLASH_ESCAPES for this session, so the escaping of the + // string literals below is correct regardless of the server's SQL mode. + $statements = [ + "SET SESSION sql_mode = TRIM( BOTH ',' FROM REPLACE( CONCAT( ',', @@SESSION.sql_mode, ',' ), ',NO_BACKSLASH_ESCAPES,', ',' ) )", + "CREATE USER {$account} IDENTIFIED BY " . self::esc_sql_string( $password ), + ]; + + $grant = (bool) Utils\get_flag_value( $assoc_args, 'grant-privileges', false ); + if ( $grant ) { + $statements[] = 'GRANT ALL PRIVILEGES ON ' . self::esc_grant_database( DB_NAME, $this->has_partial_revokes( $assoc_args ) ) . ".* TO {$account}"; + } + + WP_CLI::debug( "Creating database user '{$username}'@'{$host}'" . ( $grant ? ' with all privileges on ' . DB_NAME : '' ) . '.', 'db' ); + + $this->run_statements( implode( ";\n", $statements ) . ";\n", $assoc_args, $password ); + + if ( $grant ) { + WP_CLI::success( "Created database user '{$username}'@'{$host}' with all privileges on '" . DB_NAME . "'." ); + } else { + WP_CLI::success( "Created database user '{$username}'@'{$host}'." ); + } + } + + /** + * Gets the new user's password from the arguments, STDIN or a prompt. + * + * @param array $assoc_args Associative arguments. + * @return string Password. + */ + private function get_password( $assoc_args ) { + if ( isset( $assoc_args['password'] ) && true !== $assoc_args['password'] ) { + $password = (string) $assoc_args['password']; + } elseif ( function_exists( 'stream_isatty' ) && stream_isatty( STDIN ) ) { + $password = (string) \cli\prompt( 'Password', false, ': ', true ); + } else { + $password = (string) stream_get_contents( STDIN ); + // Strip the trailing newline of piped input, but nothing else. + $password = (string) preg_replace( '/\r?\n\z/', '', $password ); + } + + if ( '' === $password ) { + WP_CLI::error( 'A password is required. Pass it with --password, pipe it to STDIN, or enter it when prompted.' ); + } + + return $password; + } + + /** + * Checks whether the server has MySQL's `partial_revokes` enabled. + * + * With it, `_` and `%` in GRANT database names are literal characters + * instead of wildcards. Only checked when DB_NAME contains them. + * + * @param array $assoc_args Associative arguments. + * @return bool + */ + private function has_partial_revokes( $assoc_args ) { + if ( false === strpbrk( DB_NAME, '_%' ) ) { + return false; + } + + // MariaDB and MySQL before 8.0.16 have no such variable, which returns no rows. + $output = $this->run_statements( "SHOW GLOBAL VARIABLES LIKE 'partial_revokes';\n", $assoc_args ); + + return (bool) preg_match( '/^partial_revokes\s+(ON|1)\s*$/mi', $output ); + } + + /** + * Runs SQL statements with the `mysql` client, passing them through STDIN. + * + * @param string $sql SQL statements to run. + * @param array $assoc_args Associative arguments. + * @param string $secret Optional value to keep out of error messages. + * @return string Output of the statements. + */ + private function run_statements( $sql, $assoc_args, $secret = '' ) { + $mysql_args = [ + 'user' => Utils\get_flag_value( $assoc_args, 'dbuser', DB_USER ), + ]; + + // phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysql_host_to_cli_args, PHPCompatibility.Extensions.RemovedExtensions.mysql_DeprecatedRemoved -- Misidentified as PHP native MySQL function. + $mysql_args = array_merge( $mysql_args, Utils\mysql_host_to_cli_args( DB_HOST ) ); + + if ( defined( 'DB_CHARSET' ) && constant( 'DB_CHARSET' ) ) { + $mysql_args['default-character-set'] = constant( 'DB_CHARSET' ); + } + + // See DB_Command::run(): skip the certificate check like WordPress does. + if ( 'mariadb' === Utils\get_db_type() ) { + $mysql_args['skip-ssl-verify-server-cert'] = true; + } + + $defaults = Utils\get_flag_value( $assoc_args, 'defaults', false ) ? '' : ' --no-defaults'; + // --binary-mode keeps the client from turning CRLF in the statements (and so in passwords) into LF. + $command = Utils\force_env_on_nix_systems( Utils\get_mysql_binary_path() . $defaults . ' --no-auto-rehash --batch --binary-mode' ) + . Utils\assoc_args_to_str( $mysql_args ); + + WP_CLI::debug( "Final MySQL command: {$command}", 'db' ); + + // The connection password goes through the environment, the statements through STDIN. + $old_password = getenv( 'MYSQL_PWD' ); + $dbpass = Utils\get_flag_value( $assoc_args, 'dbpass', DB_PASSWORD ); + putenv( 'MYSQL_PWD=' . ( is_string( $dbpass ) ? $dbpass : '' ) ); + + $pipes = []; + $process = Utils\proc_open_compat( + $command, + [ + 0 => [ 'pipe', 'r' ], + 1 => [ 'pipe', 'w' ], + 2 => [ 'pipe', 'w' ], + ], + $pipes + ); + + if ( false === $old_password ) { + putenv( 'MYSQL_PWD' ); + } else { + putenv( 'MYSQL_PWD=' . $old_password ); + } + + if ( ! is_resource( $process ) ) { + WP_CLI::error( 'Could not run the mysql client.' ); + } + + fwrite( $pipes[0], $sql ); + fclose( $pipes[0] ); + + $stdout = (string) stream_get_contents( $pipes[1] ); + $stderr = (string) stream_get_contents( $pipes[2] ); + fclose( $pipes[1] ); + fclose( $pipes[2] ); + + $exit_code = proc_close( $process ); + + if ( 0 !== $exit_code ) { + // The MariaDB client echoes a failed statement, including the new user's password, + // before the error. Only keep the error lines, and redact the password just in case. + $errors = preg_grep( '/^ERROR\b/', explode( "\n", str_replace( "\r\n", "\n", $stderr ) ) ); + $errors = trim( implode( "\n", $errors ? $errors : [] ) ); + if ( '' !== $secret ) { + $errors = str_replace( [ $secret, substr( self::esc_sql_string( $secret ), 1, -1 ) ], '***', $errors ); + } + WP_CLI::error( '' !== $errors ? $errors : "The mysql client exited with code {$exit_code}." ); + } + + return $stdout; + } + + /** + * Quotes a value as an SQL string literal. + * + * Assumes backslash escapes are enabled, see create(). + * + * @param string $value Value to quote. + * @return string Quoted value. + */ + private static function esc_sql_string( $value ) { + return "'" . str_replace( [ '\\', "'" ], [ '\\\\', "''" ], $value ) . "'"; + } + + /** + * Quotes a database name for use in a GRANT statement. + * + * GRANT treats `_` and `%` in database names as wildcards, so they are + * escaped to only match this one database. With MySQL's `partial_revokes` + * enabled they are literal characters, and escaping them would make the + * name match no database. + * + * @param string $database Database name. + * @param bool $partial_revokes Whether `partial_revokes` is enabled. + * @return string Quoted database name. + */ + private static function esc_grant_database( $database, $partial_revokes = false ) { + $database = str_replace( '`', '``', $database ); + if ( ! $partial_revokes ) { + $database = str_replace( [ '_', '%' ], [ '\\_', '\\%' ], $database ); + } + return '`' . $database . '`'; + } +}