OSDN Git Service

Don't allow wp-cli as root without --allow_root
authorWesley Spikes <wesley.spikes@gmail.com>
Tue, 21 Jan 2014 05:55:38 +0000 (21:55 -0800)
committerWesley Spikes <wesley.spikes@gmail.com>
Tue, 21 Jan 2014 05:59:22 +0000 (21:59 -0800)
php/WP_CLI/Runner.php
php/config-spec.php

index 57f000a..c14bdb0 100644 (file)
@@ -396,11 +396,40 @@ class Runner {
                list( $this->config, $this->extra_config ) = $configurator->to_array();
        }
 
+       private function check_root() {
+               if ( $this->config['allow_root'] )
+                       return; # they're aware of the risks!
+               if ( !function_exists( 'posix_geteuid') )
+                       return; # posix functions not available
+               if ( posix_geteuid() !== 0 )
+                       return; # not root
+
+               WP_CLI::error(
+                       "YIKES! It looks like you're running this as root. You probably meant to" .
+                       "run this as the user that your WordPress install exists under.\n" .
+                       "\n" .
+                       "If you REALLY mean to run this as root, we won't stop you, but just " .
+                       "bear in mind that any code on this site will then have full control of " .
+                       "your server, making it quite DANGEROUS.\n" .
+                       "\n" .
+                       "If you'd like to continue as root, please run this again, adding this " .
+                       "flag:  --allow_root\n" .
+                       "\n" .
+                       "If you'd like to run it as the user that this site is under, you can " .
+                       "run the following to become the respective user:\n" .
+                       "\n" .
+                       "    su USER -c -- wp ...\n" .
+                       "\n"
+               );
+       }
+
        public function before_wp_load() {
                $this->init_config();
                $this->init_colorization();
                $this->init_logger();
 
+               $this->check_root();
+
                if ( empty( $this->arguments ) )
                        $this->arguments[] = 'help';
 
index d4e206e..e052609 100644 (file)
@@ -76,5 +76,11 @@ return array(
                'multiple' => true,
                'default' => array(),
        ),
+
+       'allow_root' => array(
+               'runtime' => '=<allow_root>',
+               'desc' => '(NOT RECCOMENDED) Allow wp-cli to run as root. This poses a security risk, so you probably do not want to do this.',
+       ),
+
 );