|
1 | 1 | <?php declare( strict_types = 1 ); |
2 | 2 |
|
| 3 | +use WP_MySQL_Proxy\MySQL_Authentication; |
| 4 | +use WP_MySQL_Proxy\MySQL_Protocol; |
3 | 5 | use WP_MySQL_Proxy\MySQL_Proxy; |
4 | 6 | use WP_MySQL_Proxy\Adapter\SQLite_Adapter; |
5 | 7 | use WP_MySQL_Proxy\Logger; |
6 | 8 |
|
7 | 9 | require_once $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php'; |
8 | 10 |
|
9 | | -// Process CLI arguments: |
10 | | -$shortopts = 'hd:p:l:'; |
11 | | -$longopts = array( 'help', 'database:', 'port:', 'log-level:' ); |
12 | | -$opts = getopt( $shortopts, $longopts ); |
| 11 | +// Parse every argument: getopt() silently discards unknown or incomplete options. |
| 12 | +$aliases = array( |
| 13 | + 'h' => 'help', |
| 14 | + 'd' => 'database', |
| 15 | + 'P' => 'port', |
| 16 | + 'l' => 'log-level', |
| 17 | + 'u' => 'user', |
| 18 | +); |
| 19 | +$options = array( 'help', 'database', 'port', 'log-level', 'bind-address', 'user', 'password-file', 'auth-plugin' ); |
| 20 | +$args = array_slice( $argv, 1 ); |
| 21 | +$opts = array(); |
| 22 | + |
| 23 | +// Never include argument values in errors: an unknown option may contain a password. |
| 24 | +$option_error = "Error: Invalid command-line options. Use --help for supported options and required values.\n"; |
| 25 | + |
| 26 | +for ( $i = 0; $i < count( $args ); $i++ ) { |
| 27 | + $arg = $args[ $i ]; |
| 28 | + $name = ''; |
| 29 | + $value = null; |
| 30 | + if ( 0 === strpos( $arg, '--' ) ) { |
| 31 | + $parts = explode( '=', substr( $arg, 2 ), 2 ); |
| 32 | + $name = $parts[0]; |
| 33 | + $value = $parts[1] ?? null; |
| 34 | + } elseif ( '-' === substr( $arg, 0, 1 ) && strlen( $arg ) > 1 ) { |
| 35 | + $name = $aliases[ $arg[1] ] ?? ''; |
| 36 | + if ( strlen( $arg ) > 2 ) { |
| 37 | + if ( 'help' === $name ) { |
| 38 | + array_splice( $args, $i + 1, 0, array( '-' . substr( $arg, 2 ) ) ); |
| 39 | + } else { |
| 40 | + $value = substr( $arg, 2 ); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if ( ! in_array( $name, $options, true ) || array_key_exists( $name, $opts ) |
| 46 | + || ( 'help' === $name && null !== $value ) |
| 47 | + ) { |
| 48 | + fwrite( STDERR, $option_error ); |
| 49 | + exit( 1 ); |
| 50 | + } |
| 51 | + if ( 'help' === $name ) { |
| 52 | + $opts[ $name ] = true; |
| 53 | + continue; |
| 54 | + } |
| 55 | + // A separate value can't look like an option, so a missing value isn't |
| 56 | + // filled with the next option. Use --option=-value for such a value. |
| 57 | + if ( null === $value && isset( $args[ $i + 1 ] ) && 0 !== strpos( $args[ $i + 1 ], '-' ) ) { |
| 58 | + $value = $args[ ++$i ]; |
| 59 | + } |
| 60 | + if ( null === $value ) { |
| 61 | + fwrite( STDERR, $option_error ); |
| 62 | + exit( 1 ); |
| 63 | + } |
| 64 | + $opts[ $name ] = $value; |
| 65 | +} |
13 | 66 |
|
14 | 67 | $help = <<<USAGE |
15 | | -Usage: wp-mysql-proxy.php [--port <port>] [--database <path/to/db.sqlite>] [--log-level <log_level>] |
| 68 | +Usage: wp-mysql-proxy.php --user <username> [--password-file <path>] [--bind-address <address>] [--auth-plugin <name>] [--port <port>] [--database <path/to/db.sqlite>] [--log-level <log_level>] |
16 | 69 |
|
17 | 70 | Options: |
18 | 71 | -h, --help Show this help message and exit. |
19 | | - -p, --port=<port> The port to listen on. Default: 3306 |
| 72 | + -u, --user=<username> The proxy account name. Required. |
| 73 | + --password-file=<path> Read the password from a file instead of WP_MYSQL_PROXY_PASSWORD. |
| 74 | + --bind-address=<ip> The IPv4 or IPv6 address to listen on. Default: 127.0.0.1 |
| 75 | + --auth-plugin=<name> The account's authentication plugin, either |
| 76 | + caching_sha2_password or mysql_native_password. |
| 77 | + Use the latter only for clients that need it. |
| 78 | + Default: caching_sha2_password |
| 79 | + -P, --port=<port> The port to listen on. Default: 3306 |
20 | 80 | -d, --database=<path> The path to the SQLite database file. Default: :memory: |
21 | 81 | -l, --log-level=<level> The log level to use. One of 'error', 'warning', 'info', 'debug'. Default: info |
22 | 82 |
|
| 83 | +A nonempty password is required via WP_MYSQL_PROXY_PASSWORD or --password-file. |
| 84 | +Password files are read verbatim, including any trailing newline. |
| 85 | +Connections are not encrypted (no TLS). For remote clients, use an SSH tunnel. |
| 86 | +
|
23 | 87 | USAGE; |
24 | 88 |
|
25 | 89 | // Help. |
26 | | -if ( isset( $opts['h'] ) || isset( $opts['help'] ) ) { |
| 90 | +if ( isset( $opts['help'] ) ) { |
27 | 91 | fwrite( STDERR, $help ); |
28 | 92 | exit( 0 ); |
29 | 93 | } |
30 | 94 |
|
31 | 95 | // Database path. |
32 | | -$db_path = $opts['d'] ?? $opts['database'] ?? ':memory:'; |
| 96 | +$db_path = $opts['database'] ?? ':memory:'; |
| 97 | + |
| 98 | +// Validate options before SQLite_Adapter opens or creates the database file. |
| 99 | +// MySQL_Proxy checks the bind address too, but only after that. |
| 100 | + |
| 101 | +// Bind address. |
| 102 | +$bind_address = $opts['bind-address'] ?? '127.0.0.1'; |
| 103 | +if ( false === filter_var( $bind_address, FILTER_VALIDATE_IP ) ) { |
| 104 | + fwrite( STDERR, "Error: --bind-address must be a valid IPv4 or IPv6 address.\n" ); |
| 105 | + exit( 1 ); |
| 106 | +} |
33 | 107 |
|
34 | 108 | // Port. |
35 | | -$port = (int) ( $opts['p'] ?? $opts['port'] ?? 3306 ); |
36 | | -if ( $port < 1 || $port > 65535 ) { |
| 109 | +$port = filter_var( $opts['port'] ?? 3306, FILTER_VALIDATE_INT ); |
| 110 | +if ( false === $port || $port < 1 || $port > 65535 ) { |
37 | 111 | fwrite( STDERR, "Error: --port must be an integer between 1 and 65535. Use --help for more information.\n" ); |
38 | 112 | exit( 1 ); |
39 | 113 | } |
40 | 114 |
|
41 | 115 | // Log level. |
42 | | -$log_level = $opts['l'] ?? $opts['log-level'] ?? 'info'; |
| 116 | +$log_level = $opts['log-level'] ?? 'info'; |
43 | 117 | if ( ! in_array( $log_level, Logger::LEVELS, true ) ) { |
44 | 118 | fwrite( STDERR, 'Error: --log-level must be one of: ' . implode( ', ', Logger::LEVELS ) . ". Use --help for more information.\n" ); |
45 | 119 | exit( 1 ); |
46 | 120 | } |
47 | 121 |
|
| 122 | +// Account. Do not accept passwords as command-line arguments. |
| 123 | +$password = getenv( 'WP_MYSQL_PROXY_PASSWORD' ); |
| 124 | +if ( isset( $opts['password-file'] ) ) { |
| 125 | + $password_file = $opts['password-file']; |
| 126 | + |
| 127 | + // Pipes, such as /dev/stdin or a process substitution, keep the password off the disk. |
| 128 | + if ( ! is_readable( $password_file ) || is_dir( $password_file ) ) { |
| 129 | + fwrite( STDERR, "Error: --password-file must name a readable file.\n" ); |
| 130 | + exit( 1 ); |
| 131 | + } |
| 132 | + |
| 133 | + // On Linux, PHP can't open a pipe by its /dev/fd path, so read a descriptor of |
| 134 | + // this process through php://fd. See https://github.com/php/php-src/issues/9551. |
| 135 | + $source = $password_file; |
| 136 | + if ( '/dev/stdin' === $password_file ) { |
| 137 | + $source = 'php://fd/0'; |
| 138 | + } elseif ( preg_match( '~^/(?:dev|proc/self)/fd/(\d+)$~', $password_file, $match ) ) { |
| 139 | + $source = 'php://fd/' . $match[1]; |
| 140 | + } |
| 141 | + |
| 142 | + // Report a failed read without PHP's warning, which names the path. |
| 143 | + $password = @file_get_contents( $source ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 144 | + if ( false === $password ) { |
| 145 | + fwrite( STDERR, "Error: --password-file could not be read.\n" ); |
| 146 | + exit( 1 ); |
| 147 | + } |
| 148 | +} |
| 149 | +try { |
| 150 | + $authentication = new MySQL_Authentication( |
| 151 | + $opts['user'] ?? '', |
| 152 | + (string) $password, |
| 153 | + $opts['auth-plugin'] ?? MySQL_Protocol::DEFAULT_AUTH_PLUGIN |
| 154 | + ); |
| 155 | +} catch ( InvalidArgumentException $e ) { |
| 156 | + fwrite( STDERR, 'Error: ' . $e->getMessage() . " Use --help for more information.\n" ); |
| 157 | + exit( 1 ); |
| 158 | +} |
| 159 | +unset( $password ); |
| 160 | + |
48 | 161 | if ( ! class_exists( 'WP_MySQL_On_SQLite' ) ) { |
49 | 162 | fwrite( |
50 | 163 | STDERR, |
|
57 | 170 | $proxy = new MySQL_Proxy( |
58 | 171 | new SQLite_Adapter( $db_path ), |
59 | 172 | array( |
60 | | - 'port' => $port, |
61 | | - 'log_level' => $log_level, |
| 173 | + 'authentication' => $authentication, |
| 174 | + 'bind_address' => $bind_address, |
| 175 | + 'port' => $port, |
| 176 | + 'log_level' => $log_level, |
62 | 177 | ) |
63 | 178 | ); |
64 | 179 | $proxy->start(); |
0 commit comments