| @@ -41,9 +41,28 @@ | ||
| 41 | 41 | $current_options = get_option( Vigilante_Settings::OPTION_NAME ); |
| 42 | 42 | |
| 43 | 43 | if ( false === $current_options ) { |
| 44 | 44 | // First installation - set defaults |
| 45 | - update_option( Vigilante_Settings::OPTION_NAME, $settings->get_default_options() ); | |
| 45 | + $first_run = $settings->get_default_options(); | |
| 46 | + | |
| 47 | + /* | |
| 48 | + * XML-RPC on a brand new install: block the pingback methods, which is | |
| 49 | + * what gets abused for amplification, and leave the rest reachable so | |
| 50 | + * the WordPress app, Jetpack or a remote manager keep working out of | |
| 51 | + * the box. Disabling it completely is the stricter choice and the one | |
| 52 | + * the settings screen recommends, but it is not imposed on a site that | |
| 53 | + * never asked for it. Brute force through XML-RPC stays covered either | |
| 54 | + * way, because those logins go through wp_authenticate() and the login | |
| 55 | + * lockout hooks into it. | |
| 56 | + * | |
| 57 | + * Only written here, on a first installation. Sites upgrading keep | |
| 58 | + * whatever they had: the activation hook does not run on an update, and | |
| 59 | + * Vigilante_Comment_Security::resolve_xmlrpc_mode() answers 'full' when | |
| 60 | + * nothing is stored, which is what every version since 1.0.0 did. | |
| 61 | + */ | |
| 62 | + $first_run = Vigilante_Settings::apply_install_tweaks( $first_run ); | |
| 63 | + | |
| 64 | + update_option( Vigilante_Settings::OPTION_NAME, $first_run ); | |
| 46 | 65 | // Refresh settings instance to get new values |
| 47 | 66 | $settings->clear_cache(); |
| 48 | 67 | $settings = new Vigilante_Settings(); |
| 49 | 68 | } else { |
| @@ -53,11 +72,8 @@ | ||
| 53 | 72 | $settings = new Vigilante_Settings(); |
| 54 | 73 | } |
| 55 | 74 | } |
| 56 | 75 | |
| 57 | - // Create backup of current files FIRST (before any modifications) | |
| 58 | - self::create_activation_backup( $settings ); | |
| 59 | - | |
| 60 | 76 | // Apply htaccess protection (part of firewall module) |
| 61 | 77 | if ( $settings->is_module_enabled( 'firewall' ) ) { |
| 62 | 78 | self::apply_htaccess_protection( $settings ); |
| 63 | 79 | } |
| @@ -207,25 +223,8 @@ | ||
| 207 | 223 | return true; |
| 208 | 224 | } |
| 209 | 225 | |
| 210 | 226 | /** |
| 211 | - * Create backup of important files | |
| 212 | - * | |
| 213 | - * @param Vigilante_Settings $settings Settings instance. | |
| 214 | - */ | |
| 215 | - private static function create_activation_backup( $settings ) { | |
| 216 | - require_once VIGILANTE_INCLUDES_DIR . 'class-backup-manager.php'; | |
| 217 | - | |
| 218 | - $backup_manager = new Vigilante_Backup_Manager(); | |
| 219 | - $result = $backup_manager->create_backups(); | |
| 220 | - | |
| 221 | - if ( is_wp_error( $result ) ) { | |
| 222 | - // Store error for admin notice | |
| 223 | - set_transient( 'vigilante_backup_error', $result->get_error_message(), 60 ); | |
| 224 | - } | |
| 225 | - } | |
| 226 | - | |
| 227 | - /** | |
| 228 | 227 | * Apply htaccess protection |
| 229 | 228 | * |
| 230 | 229 | * @param Vigilante_Settings $settings Settings instance. |
| 231 | 230 | */ |
| @@ -231,8 +230,9 @@ | ||
| 231 | 230 | */ |
| 232 | 231 | private static function apply_htaccess_protection( $settings ) { |
| 233 | 232 | // Only apply if Apache server |
| 234 | 233 | if ( ! self::is_apache() ) { |
| 234 | + self::mark_server_files_pending(); | |
| 235 | 235 | return; |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | require_once VIGILANTE_INCLUDES_DIR . 'class-htaccess-protection.php'; |
| @@ -248,8 +248,9 @@ | ||
| 248 | 248 | */ |
| 249 | 249 | private static function apply_security_headers( $settings ) { |
| 250 | 250 | // Only apply if Apache server |
| 251 | 251 | if ( ! self::is_apache() ) { |
| 252 | + self::mark_server_files_pending(); | |
| 252 | 253 | return; |
| 253 | 254 | } |
| 254 | 255 | |
| 255 | 256 | require_once VIGILANTE_INCLUDES_DIR . 'class-security-headers.php'; |
| @@ -313,8 +314,14 @@ | ||
| 313 | 314 | * |
| 314 | 315 | * @param Vigilante_Settings $settings Settings instance. |
| 315 | 316 | */ |
| 316 | 317 | private static function remove_sensitive_files( $settings ) { |
| 318 | + // They sit in the root every site of a network shares. Until 2.11.6 the | |
| 319 | + // activation on any site removed them. | |
| 320 | + if ( ! Vigilante_Settings::can_write_shared_files() ) { | |
| 321 | + return; | |
| 322 | + } | |
| 323 | + | |
| 317 | 324 | $advanced = $settings->get_section( 'advanced' ); |
| 318 | 325 | |
| 319 | 326 | // Remove readme.html |
| 320 | 327 | if ( ! empty( $advanced['remove_readme'] ) ) { |
| @@ -354,9 +361,15 @@ | ||
| 354 | 361 | $database = new Vigilante_Database(); |
| 355 | 362 | $activity_log = null; // Not needed for baseline generation |
| 356 | 363 | |
| 357 | 364 | $fi = new Vigilante_File_Integrity( $settings, $database, $activity_log ); |
| 358 | - $fi->regenerate_all_baselines(); | |
| 365 | + | |
| 366 | + // Same care as the migration: reactivating the plugin on a site that | |
| 367 | + // already has an approved baseline must not throw it away and adopt | |
| 368 | + // whatever the files say today. | |
| 369 | + if ( ! $fi->get_critical_files_baseline() ) { | |
| 370 | + $fi->regenerate_all_baselines(); | |
| 371 | + } | |
| 359 | 372 | } |
| 360 | 373 | |
| 361 | 374 | /** |
| 362 | 375 | * Schedule cron events |
| @@ -452,12 +465,50 @@ | ||
| 452 | 465 | * |
| 453 | 466 | * @return bool |
| 454 | 467 | */ |
| 455 | 468 | private static function is_apache() { |
| 456 | - if ( ! function_exists( 'apache_get_modules' ) ) { | |
| 457 | - // Check server software | |
| 458 | - $server = isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : ''; | |
| 459 | - return stripos( $server, 'apache' ) !== false || stripos( $server, 'litespeed' ) !== false; | |
| 469 | + require_once VIGILANTE_INCLUDES_DIR . 'class-htaccess-manager.php'; | |
| 470 | + | |
| 471 | + // One detection for the whole plugin. This used to be a second copy of | |
| 472 | + // the same logic, so fixing one never fixed the other. | |
| 473 | + return Vigilante_Htaccess_Manager::get_instance()->is_apache(); | |
| 474 | + } | |
| 475 | + | |
| 476 | + /** | |
| 477 | + * Leave the server layer pending when the server could not be identified | |
| 478 | + * | |
| 479 | + * An activation from WP-CLI has no request to read the server software | |
| 480 | + * from, so before 2.9.9 the two apply_* guards below simply returned and | |
| 481 | + * the site was left without the .htaccess layer, with every switch showing | |
| 482 | + * as on. Now it is written down, so the first web request applies it, and | |
| 483 | + * it is logged, so it is visible that it happened. | |
| 484 | + * | |
| 485 | + * @since 2.9.9 | |
| 486 | + */ | |
| 487 | + private static function mark_server_files_pending() { | |
| 488 | + require_once VIGILANTE_INCLUDES_DIR . 'class-htaccess-manager.php'; | |
| 489 | + | |
| 490 | + // On a server known not to be Apache there is nothing to write, ever. | |
| 491 | + if ( ! Vigilante_Htaccess_Manager::get_instance()->server_is_unknown() ) { | |
| 492 | + return; | |
| 460 | 493 | } |
| 461 | - return true; | |
| 494 | + | |
| 495 | + update_option( 'vigilante_server_files_pending', 1 ); | |
| 496 | + | |
| 497 | + // The activation runs before the plugin has loaded its own files, so | |
| 498 | + // every link of the chain has to be pulled in: the log asks the database | |
| 499 | + // for the client IP, and that resolves it through the IP helper. | |
| 500 | + require_once VIGILANTE_INCLUDES_DIR . 'class-ip-utils.php'; | |
| 501 | + require_once VIGILANTE_INCLUDES_DIR . 'class-database.php'; | |
| 502 | + require_once VIGILANTE_INCLUDES_DIR . 'class-activity-log.php'; | |
| 503 | + | |
| 504 | + $settings = new Vigilante_Settings(); | |
| 505 | + $activity_log = new Vigilante_Activity_Log( $settings, new Vigilante_Database() ); | |
| 506 | + $activity_log->log( | |
| 507 | + 'system', | |
| 508 | + 'server_rules_pending', | |
| 509 | + __( 'The server type could not be identified from this request, so the .htaccess rules were left pending and will be written on the first web request.', 'vigilante' ), | |
| 510 | + array( 'sapi' => PHP_SAPI ), | |
| 511 | + 'warning' | |
| 512 | + ); | |
| 462 | 513 | } |
| 463 | 514 | } |