| @@ -58,12 +58,9 @@ | ||
| 58 | 58 | * whatever they had: the activation hook does not run on an update, and |
| 59 | 59 | * Vigilante_Comment_Security::resolve_xmlrpc_mode() answers 'full' when |
| 60 | 60 | * nothing is stored, which is what every version since 1.0.0 did. |
| 61 | 61 | */ |
| 62 | - if ( ! isset( $first_run['wp_hardening'] ) || ! is_array( $first_run['wp_hardening'] ) ) { | |
| 63 | - $first_run['wp_hardening'] = array(); | |
| 64 | - } | |
| 65 | - $first_run['wp_hardening']['xmlrpc_mode'] = 'pingback'; | |
| 62 | + $first_run = Vigilante_Settings::apply_install_tweaks( $first_run ); | |
| 66 | 63 | |
| 67 | 64 | update_option( Vigilante_Settings::OPTION_NAME, $first_run ); |
| 68 | 65 | // Refresh settings instance to get new values |
| 69 | 66 | $settings->clear_cache(); |
| @@ -75,11 +72,8 @@ | ||
| 75 | 72 | $settings = new Vigilante_Settings(); |
| 76 | 73 | } |
| 77 | 74 | } |
| 78 | 75 | |
| 79 | - // Create backup of current files FIRST (before any modifications) | |
| 80 | - self::create_activation_backup( $settings ); | |
| 81 | - | |
| 82 | 76 | // Apply htaccess protection (part of firewall module) |
| 83 | 77 | if ( $settings->is_module_enabled( 'firewall' ) ) { |
| 84 | 78 | self::apply_htaccess_protection( $settings ); |
| 85 | 79 | } |
| @@ -112,8 +106,11 @@ | ||
| 112 | 106 | |
| 113 | 107 | // Schedule cron events |
| 114 | 108 | self::schedule_events(); |
| 115 | 109 | |
| 110 | + // Capture the self-integrity anchor (A3: manifest fingerprint in DB). | |
| 111 | + self::anchor_self_integrity( $settings ); | |
| 112 | + | |
| 116 | 113 | // Set activation transient for admin notice |
| 117 | 114 | set_transient( 'vigilante_activated', true, 30 ); |
| 118 | 115 | |
| 119 | 116 | // Store activation time |
| @@ -129,8 +126,35 @@ | ||
| 129 | 126 | ob_end_clean(); |
| 130 | 127 | } |
| 131 | 128 | |
| 132 | 129 | /** |
| 130 | + * Anchor the self-integrity check on activation | |
| 131 | + * | |
| 132 | + * The first activation captures the fingerprint of the shipped manifest, | |
| 133 | + * unless WordPress.org distributes something else for that version, so | |
| 134 | + * the self-check has a baseline from the very first run. A | |
| 135 | + * reactivation keeps the anchor it already has and checks against it: | |
| 136 | + * capturing again adopted whatever manifest the folder held at that | |
| 137 | + * moment, a regenerated one included, the same reason the critical files | |
| 138 | + * baseline is not thrown away on reactivation. | |
| 139 | + * | |
| 140 | + * @since 3.0.0 | |
| 141 | + * | |
| 142 | + * @param Vigilante_Settings $settings Settings instance. | |
| 143 | + * @return void | |
| 144 | + */ | |
| 145 | + public static function anchor_self_integrity( $settings ) { | |
| 146 | + if ( ! class_exists( 'Vigilante_Self_Integrity' ) ) { | |
| 147 | + require_once VIGILANTE_INCLUDES_DIR . 'class-self-integrity.php'; | |
| 148 | + } | |
| 149 | + // run_check() captures on the first run itself, but only when | |
| 150 | + // WordPress.org does not contradict the manifest; with an anchor already | |
| 151 | + // there it checks against it. | |
| 152 | + $self_integrity = new Vigilante_Self_Integrity( $settings ); | |
| 153 | + $self_integrity->run_check( 'activation' ); | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 133 | 157 | * Idempotent migrations for existing installations. |
| 134 | 158 | * |
| 135 | 159 | * @param array $current_options Current vigilante_options array. |
| 136 | 160 | * @return bool True if any migration changed the stored option. |
| @@ -229,25 +253,8 @@ | ||
| 229 | 253 | return true; |
| 230 | 254 | } |
| 231 | 255 | |
| 232 | 256 | /** |
| 233 | - * Create backup of important files | |
| 234 | - * | |
| 235 | - * @param Vigilante_Settings $settings Settings instance. | |
| 236 | - */ | |
| 237 | - private static function create_activation_backup( $settings ) { | |
| 238 | - require_once VIGILANTE_INCLUDES_DIR . 'class-backup-manager.php'; | |
| 239 | - | |
| 240 | - $backup_manager = new Vigilante_Backup_Manager(); | |
| 241 | - $result = $backup_manager->create_backups(); | |
| 242 | - | |
| 243 | - if ( is_wp_error( $result ) ) { | |
| 244 | - // Store error for admin notice | |
| 245 | - set_transient( 'vigilante_backup_error', $result->get_error_message(), 60 ); | |
| 246 | - } | |
| 247 | - } | |
| 248 | - | |
| 249 | - /** | |
| 250 | 257 | * Apply htaccess protection |
| 251 | 258 | * |
| 252 | 259 | * @param Vigilante_Settings $settings Settings instance. |
| 253 | 260 | */ |
| @@ -253,8 +260,9 @@ | ||
| 253 | 260 | */ |
| 254 | 261 | private static function apply_htaccess_protection( $settings ) { |
| 255 | 262 | // Only apply if Apache server |
| 256 | 263 | if ( ! self::is_apache() ) { |
| 264 | + self::mark_server_files_pending(); | |
| 257 | 265 | return; |
| 258 | 266 | } |
| 259 | 267 | |
| 260 | 268 | require_once VIGILANTE_INCLUDES_DIR . 'class-htaccess-protection.php'; |
| @@ -270,8 +278,9 @@ | ||
| 270 | 278 | */ |
| 271 | 279 | private static function apply_security_headers( $settings ) { |
| 272 | 280 | // Only apply if Apache server |
| 273 | 281 | if ( ! self::is_apache() ) { |
| 282 | + self::mark_server_files_pending(); | |
| 274 | 283 | return; |
| 275 | 284 | } |
| 276 | 285 | |
| 277 | 286 | require_once VIGILANTE_INCLUDES_DIR . 'class-security-headers.php'; |
| @@ -335,8 +344,14 @@ | ||
| 335 | 344 | * |
| 336 | 345 | * @param Vigilante_Settings $settings Settings instance. |
| 337 | 346 | */ |
| 338 | 347 | private static function remove_sensitive_files( $settings ) { |
| 348 | + // They sit in the root every site of a network shares. Until 2.11.6 the | |
| 349 | + // activation on any site removed them. | |
| 350 | + if ( ! Vigilante_Settings::can_write_shared_files() ) { | |
| 351 | + return; | |
| 352 | + } | |
| 353 | + | |
| 339 | 354 | $advanced = $settings->get_section( 'advanced' ); |
| 340 | 355 | |
| 341 | 356 | // Remove readme.html |
| 342 | 357 | if ( ! empty( $advanced['remove_readme'] ) ) { |
| @@ -376,9 +391,15 @@ | ||
| 376 | 391 | $database = new Vigilante_Database(); |
| 377 | 392 | $activity_log = null; // Not needed for baseline generation |
| 378 | 393 | |
| 379 | 394 | $fi = new Vigilante_File_Integrity( $settings, $database, $activity_log ); |
| 380 | - $fi->regenerate_all_baselines(); | |
| 395 | + | |
| 396 | + // Same care as the migration: reactivating the plugin on a site that | |
| 397 | + // already has an approved baseline must not throw it away and adopt | |
| 398 | + // whatever the files say today. | |
| 399 | + if ( ! $fi->get_critical_files_baseline() ) { | |
| 400 | + $fi->regenerate_all_baselines(); | |
| 401 | + } | |
| 381 | 402 | } |
| 382 | 403 | |
| 383 | 404 | /** |
| 384 | 405 | * Schedule cron events |
| @@ -474,12 +495,50 @@ | ||
| 474 | 495 | * |
| 475 | 496 | * @return bool |
| 476 | 497 | */ |
| 477 | 498 | private static function is_apache() { |
| 478 | - if ( ! function_exists( 'apache_get_modules' ) ) { | |
| 479 | - // Check server software | |
| 480 | - $server = isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : ''; | |
| 481 | - return stripos( $server, 'apache' ) !== false || stripos( $server, 'litespeed' ) !== false; | |
| 499 | + require_once VIGILANTE_INCLUDES_DIR . 'class-htaccess-manager.php'; | |
| 500 | + | |
| 501 | + // One detection for the whole plugin. This used to be a second copy of | |
| 502 | + // the same logic, so fixing one never fixed the other. | |
| 503 | + return Vigilante_Htaccess_Manager::get_instance()->is_apache(); | |
| 504 | + } | |
| 505 | + | |
| 506 | + /** | |
| 507 | + * Leave the server layer pending when the server could not be identified | |
| 508 | + * | |
| 509 | + * An activation from WP-CLI has no request to read the server software | |
| 510 | + * from, so before 2.9.9 the two apply_* guards below simply returned and | |
| 511 | + * the site was left without the .htaccess layer, with every switch showing | |
| 512 | + * as on. Now it is written down, so the first web request applies it, and | |
| 513 | + * it is logged, so it is visible that it happened. | |
| 514 | + * | |
| 515 | + * @since 2.9.9 | |
| 516 | + */ | |
| 517 | + private static function mark_server_files_pending() { | |
| 518 | + require_once VIGILANTE_INCLUDES_DIR . 'class-htaccess-manager.php'; | |
| 519 | + | |
| 520 | + // On a server known not to be Apache there is nothing to write, ever. | |
| 521 | + if ( ! Vigilante_Htaccess_Manager::get_instance()->server_is_unknown() ) { | |
| 522 | + return; | |
| 482 | 523 | } |
| 483 | - return true; | |
| 524 | + | |
| 525 | + update_option( 'vigilante_server_files_pending', 1 ); | |
| 526 | + | |
| 527 | + // The activation runs before the plugin has loaded its own files, so | |
| 528 | + // every link of the chain has to be pulled in: the log asks the database | |
| 529 | + // for the client IP, and that resolves it through the IP helper. | |
| 530 | + require_once VIGILANTE_INCLUDES_DIR . 'class-ip-utils.php'; | |
| 531 | + require_once VIGILANTE_INCLUDES_DIR . 'class-database.php'; | |
| 532 | + require_once VIGILANTE_INCLUDES_DIR . 'class-activity-log.php'; | |
| 533 | + | |
| 534 | + $settings = new Vigilante_Settings(); | |
| 535 | + $activity_log = new Vigilante_Activity_Log( $settings, new Vigilante_Database() ); | |
| 536 | + $activity_log->log( | |
| 537 | + 'system', | |
| 538 | + 'server_rules_pending', | |
| 539 | + __( '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' ), | |
| 540 | + array( 'sapi' => PHP_SAPI ), | |
| 541 | + 'warning' | |
| 542 | + ); | |
| 484 | 543 | } |
| 485 | 544 | } |