0 ) { add_filter( 'auth_cookie_expiration', array( __CLASS__, 'cookie_expiration' ), 99 ); add_action( 'init', array( __CLASS__, 'enforce_session_length' ), 4 ); } if ( 'allow' !== Softvolt_Ai_Bodyguard_Settings::get( 'twofa_xmlrpc', 'block_2fa' ) ) { add_filter( 'authenticate', array( __CLASS__, 'xmlrpc_login' ), 99, 3 ); } if ( $on( 'registration_guard' ) ) { add_filter( 'registration_errors', array( __CLASS__, 'reserved_names' ), 20, 3 ); add_filter( 'woocommerce_registration_errors', array( __CLASS__, 'reserved_names' ), 20, 3 ); } if ( $on( 'registration_approval' ) ) { add_action( 'user_register', array( __CLASS__, 'mark_pending' ), 5 ); add_filter( 'wp_authenticate_user', array( __CLASS__, 'refuse_pending' ), 30 ); add_filter( 'registration_redirect', array( __CLASS__, 'registration_redirect' ) ); add_filter( 'login_message', array( __CLASS__, 'pending_message' ) ); } add_action( 'wp_login', array( __CLASS__, 'record_login' ), 30, 2 ); if ( is_admin() ) { add_filter( 'manage_users_columns', array( __CLASS__, 'users_columns' ) ); add_filter( 'manage_users_custom_column', array( __CLASS__, 'users_column' ), 10, 3 ); add_filter( 'user_row_actions', array( __CLASS__, 'row_actions' ), 10, 2 ); add_action( 'admin_init', array( __CLASS__, 'handle_row_action' ) ); add_filter( 'views_users', array( __CLASS__, 'users_views' ) ); add_action( 'pre_get_users', array( __CLASS__, 'filter_pending' ) ); } } /* ----------------------------- HTTP password ----------------------------- */ public static function http_auth() { static $done = false; if ( $done || ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { return; } $done = true; $path = Softvolt_Ai_Bodyguard_Helpers::request_path(); $scope = Softvolt_Ai_Bodyguard_Settings::get( 'http_auth_scope', 'admin' ); if ( 'site' !== $scope ) { $admin = preg_match( '#/wp-admin(?:/|$)#', $path ) && ! preg_match( '#/wp-admin/(?:admin-ajax|admin-post)\.php$#', $path ); if ( ! $admin && ! did_action( 'login_init' ) && ! Softvolt_Ai_Bodyguard_Firewall::is_login_path() ) { return; } } elseif ( preg_match( '#/wp-(?:cron|json/)#', $path ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { return; // Cron and REST keep working (REST has its own authentication). } if ( is_user_logged_in() && 'site' !== $scope ) { return; } $user = (string) Softvolt_Ai_Bodyguard_Settings::get( 'http_auth_user', '' ); $hash = (string) Softvolt_Ai_Bodyguard_Settings::secret( 'http_auth_hash' ); if ( '' === $user || '' === $hash ) { return; } list( $given_user, $given_pass ) = self::basic_credentials(); if ( '' !== $given_user && hash_equals( $user, $given_user ) && wp_check_password( $given_pass, $hash ) ) { return; } if ( '' !== $given_user ) { Softvolt_Ai_Bodyguard_Logger::log_throttled( 'httpauth' . Softvolt_Ai_Bodyguard_Helpers::client_ip(), 10, 'notice', 'login_failed', sprintf( /* translators: %s: given user */ __( 'Wrong HTTP password for "%s".', 'ai-guardwall' ), Softvolt_Ai_Bodyguard_Helpers::trim_text( $given_user, 40 ) ) ); Softvolt_Ai_Bodyguard_Login_Security::failed( 'http:' . $given_user ); } nocache_headers(); header( 'WWW-Authenticate: Basic realm="' . esc_attr( wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ) ) . ' (AI GuardWall)", charset="UTF-8"' ); status_header( 401 ); echo esc_html__( 'This area needs the extra password set by the site owner.', 'ai-guardwall' ); exit; } private static function basic_credentials() { if ( isset( $_SERVER['PHP_AUTH_USER'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- a password, only compared with the stored hash; sanitizing would change it. return array( sanitize_user( wp_unslash( (string) $_SERVER['PHP_AUTH_USER'] ) ), isset( $_SERVER['PHP_AUTH_PW'] ) ? (string) wp_unslash( $_SERVER['PHP_AUTH_PW'] ) : '' ); } foreach ( array( 'HTTP_AUTHORIZATION', 'REDIRECT_HTTP_AUTHORIZATION' ) as $k ) { $header = Softvolt_Ai_Bodyguard_Helpers::server( $k ); if ( 0 === stripos( $header, 'basic ' ) ) { $pair = (string) base64_decode( substr( $header, 6 ), true ); if ( false !== strpos( $pair, ':' ) ) { return explode( ':', $pair, 2 ); } } } return array( '', '' ); } /* ------------------------------ login door ------------------------------ */ private static function door_value() { return hash_hmac( 'sha256', (string) Softvolt_Ai_Bodyguard_Settings::get( 'login_door_word', '' ), wp_salt( 'auth' ) ); } /** * ?secret-word on any page opens the door for 30 days in this browser. */ public static function door_cookie() { $word = (string) Softvolt_Ai_Bodyguard_Settings::get( 'login_door_word', '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- a secret link, not a form. if ( '' !== $word && isset( $_GET[ $word ] ) && ! headers_sent() ) { setcookie( self::DOOR_COOKIE, self::door_value(), time() + 30 * DAY_IN_SECONDS, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, is_ssl(), true ); $_COOKIE[ self::DOOR_COOKIE ] = self::door_value(); } } public static function door_open() { return isset( $_COOKIE[ self::DOOR_COOKIE ] ) && hash_equals( self::door_value(), sanitize_text_field( wp_unslash( $_COOKIE[ self::DOOR_COOKIE ] ) ) ); } public static function login_door() { self::door_cookie(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended $action = isset( $_REQUEST['action'] ) ? sanitize_key( wp_unslash( $_REQUEST['action'] ) ) : 'login'; if ( in_array( $action, array( 'logout', 'postpass', 'rp', 'resetpass', 'confirmaction', 'svabg_2fa', 'svabg_passkey' ), true ) || is_user_logged_in() || self::door_open() ) { return; } Softvolt_Ai_Bodyguard_Logger::log_throttled( 'door' . Softvolt_Ai_Bodyguard_Helpers::client_ip(), 30, 'info', 'login_failed', __( 'The login page was requested without the secret word (sent home).', 'ai-guardwall' ) ); wp_safe_redirect( home_url( '/' ) ); exit; } public static function door_url() { $word = (string) Softvolt_Ai_Bodyguard_Settings::get( 'login_door_word', '' ); return '' === $word ? '' : add_query_arg( $word, '1', wp_login_url() ); } /* ---------------------------- session length ---------------------------- */ public static function cookie_expiration( $seconds ) { $max = (int) Softvolt_Ai_Bodyguard_Settings::get( 'session_max_hours', 0 ) * HOUR_IN_SECONDS; return $max > 0 ? min( (int) $seconds, $max ) : $seconds; } /** * Sessions that started longer ago than the limit end, however active. */ public static function enforce_session_length() { if ( ! is_user_logged_in() ) { return; } $max = (int) Softvolt_Ai_Bodyguard_Settings::get( 'session_max_hours', 0 ) * HOUR_IN_SECONDS; $token = wp_get_session_token(); if ( $max <= 0 || '' === $token ) { return; } $session = WP_Session_Tokens::get_instance( get_current_user_id() )->get( $token ); if ( is_array( $session ) && ! empty( $session['login'] ) && time() - (int) $session['login'] > $max ) { Softvolt_Ai_Bodyguard_Logger::log( 'info', 'logout', __( 'Session ended: it reached the maximum length.', 'ai-guardwall' ), array(), get_current_user_id() ); wp_logout(); wp_safe_redirect( add_query_arg( 'svabg_idle', '1', wp_login_url() ) ); exit; } } /* ------------------------------- sessions ------------------------------- */ /** * Everyone with an active login session. * * @return array[] */ public static function sessions() { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = %s", 'session_tokens' ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $out = array(); $mine = wp_get_session_token(); foreach ( (array) $rows as $row ) { $tokens = maybe_unserialize( $row['meta_value'] ); $user = get_userdata( (int) $row['user_id'] ); if ( ! is_array( $tokens ) || ! $user ) { continue; } foreach ( $tokens as $verifier => $s ) { if ( ! is_array( $s ) || empty( $s['expiration'] ) || (int) $s['expiration'] < time() ) { continue; } $out[] = array( 'user_id' => (int) $user->ID, 'login' => $user->user_login, 'name' => $user->display_name, 'roles' => implode( ', ', array_map( 'translate_user_role', array_map( array( __CLASS__, 'role_name' ), (array) $user->roles ) ) ), 'admin' => user_can( $user, 'manage_options' ), 'ip' => isset( $s['ip'] ) ? (string) $s['ip'] : '', 'ua' => isset( $s['ua'] ) ? Softvolt_Ai_Bodyguard_Helpers::trim_text( (string) $s['ua'], 120 ) : '', 'started' => isset( $s['login'] ) ? (int) $s['login'] : 0, 'started_h' => isset( $s['login'] ) ? human_time_diff( (int) $s['login'] ) : '', 'expires_h' => human_time_diff( (int) $s['expiration'] ), 'verifier' => (string) $verifier, 'current' => get_current_user_id() === (int) $user->ID && '' !== $mine && hash_equals( (string) $verifier, self::verifier( $mine ) ), ); } } usort( $out, function ( $a, $b ) { return $b['started'] - $a['started']; } ); return $out; } public static function role_name( $role ) { $roles = wp_roles()->roles; return isset( $roles[ $role ] ) ? $roles[ $role ]['name'] : $role; } private static function verifier( $token ) { return function_exists( 'hash' ) ? hash( 'sha256', $token ) : sha1( $token ); } /** * End one session (by verifier) or every session of a user, or everyone except the caller. * * @param int $user_id User (0 = everyone). * @param string $verifier One session, or '' for all of that user's. * @return int Sessions ended. */ public static function logout( $user_id, $verifier = '' ) { global $wpdb; $n = 0; $mine = self::verifier( wp_get_session_token() ); $me = get_current_user_id(); $users = $user_id ? array( (int) $user_id ) : array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", 'session_tokens' ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ( $users as $uid ) { $tokens = get_user_meta( $uid, 'session_tokens', true ); if ( ! is_array( $tokens ) ) { continue; } foreach ( array_keys( $tokens ) as $v ) { if ( ( '' !== $verifier && ! hash_equals( $verifier, (string) $v ) ) || ( $uid === $me && hash_equals( $mine, (string) $v ) ) ) { continue; } unset( $tokens[ $v ] ); $n++; } if ( $tokens ) { update_user_meta( $uid, 'session_tokens', $tokens ); } else { delete_user_meta( $uid, 'session_tokens' ); } } if ( $n ) { Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'logout', sprintf( /* translators: %d: number */ _n( '%d login session ended by an administrator.', '%d login sessions ended by an administrator.', $n, 'ai-guardwall' ), $n ) ); } return $n; } /* ------------------------------- XML-RPC -------------------------------- */ public static function xmlrpc_login( $user, $username, $password ) { if ( ! defined( 'XMLRPC_REQUEST' ) || ! XMLRPC_REQUEST || ! $user instanceof WP_User ) { return $user; } $mode = Softvolt_Ai_Bodyguard_Settings::get( 'twofa_xmlrpc', 'block_2fa' ); $twofa = Softvolt_Ai_Bodyguard_Settings::hardening_enabled( 'two_factor' ) && Softvolt_Ai_Bodyguard_Two_Factor::required_for( $user ); if ( 'block_all' === $mode || ( 'block_2fa' === $mode && $twofa ) ) { Softvolt_Ai_Bodyguard_Logger::log_throttled( 'xmlrpc' . $user->ID, 30, 'notice', 'login_failed', sprintf( /* translators: %s: user login */ __( 'XML-RPC login refused for "%s".', 'ai-guardwall' ), $user->user_login ), array(), $user->ID ); return new WP_Error( 'svabg_xmlrpc', __( 'Logging in through XML-RPC is not allowed for this account. Use an application password or the website.', 'ai-guardwall' ) ); } return $user; } /* ------------------------------ last login ------------------------------ */ public static function record_login( $user_login, $user ) { if ( $user instanceof WP_User ) { update_user_meta( $user->ID, self::META_LAST, array( 'time' => time(), 'ip' => Softvolt_Ai_Bodyguard_Helpers::client_ip(), ) ); } } public static function users_columns( $columns ) { $columns['svabg_last_login'] = __( 'Last login', 'ai-guardwall' ); return $columns; } public static function users_column( $output, $column, $user_id ) { if ( 'svabg_last_login' !== $column ) { return $output; } $out = ''; $last = get_user_meta( $user_id, self::META_LAST, true ); if ( is_array( $last ) && ! empty( $last['time'] ) ) { $out = '' . esc_html( sprintf( /* translators: %s: time span */ __( '%s ago', 'ai-guardwall' ), human_time_diff( (int) $last['time'] ) ) ) . '
' . esc_html( (string) $last['ip'] ) . ''; } else { $out = '' . esc_html__( 'Not since AI GuardWall was installed', 'ai-guardwall' ) . ''; } if ( get_user_meta( $user_id, self::META_PENDING, true ) ) { $out .= '
' . esc_html__( 'Waiting for approval', 'ai-guardwall' ) . ''; } return $out; } /* ------------------------------ registration ---------------------------- */ public static function reserved_names( $errors, $login, $email ) { $names = array_map( 'strtolower', Softvolt_Ai_Bodyguard_Settings::list_to_array( Softvolt_Ai_Bodyguard_Settings::get( 'reg_block_names', '' ) ) ); $login = strtolower( trim( (string) $login ) ); if ( '' !== $login && in_array( $login, $names, true ) ) { Softvolt_Ai_Bodyguard_Logger::log_throttled( 'regname' . Softvolt_Ai_Bodyguard_Helpers::client_ip(), 10, 'notice', 'registration', sprintf( /* translators: %s: login */ __( 'Registration with the reserved name "%s" refused.', 'ai-guardwall' ), $login ) ); $errors->add( 'svabg_reserved', __( 'Error: That username is not allowed. Please choose another one.', 'ai-guardwall' ) ); } return $errors; } public static function mark_pending( $user_id ) { // Only self-registrations wait: accounts made by administrators and customers created at checkout do not. // phpcs:ignore WordPress.Security.NonceVerification.Missing $self_service = did_action( 'login_init' ) || isset( $_POST['woocommerce-register-nonce'] ); if ( ! $self_service || current_user_can( 'create_users' ) ) { return; } update_user_meta( $user_id, self::META_PENDING, time() ); $user = get_userdata( $user_id ); Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'registration', sprintf( /* translators: %s: user ID */ __( 'New account "%s" is waiting for approval.', 'ai-guardwall' ), $user ? $user->user_login : $user_id ), array(), $user_id ); Softvolt_Ai_Bodyguard_Alerts::send( __( 'A new account is waiting for approval', 'ai-guardwall' ), array( sprintf( /* translators: 1: user ID, 2: value */ __( 'User: %1$s (%2$s)', 'ai-guardwall' ), $user ? $user->user_login : $user_id, $user ? $user->user_email : '' ), sprintf( /* translators: %s: admin address */ __( 'Approve or delete it: %s', 'ai-guardwall' ), admin_url( 'users.php?svabg_pending=1' ) ), ) ); } public static function refuse_pending( $user ) { if ( $user instanceof WP_User && get_user_meta( $user->ID, self::META_PENDING, true ) ) { return new WP_Error( 'svabg_pending', __( 'Your account is waiting for approval. You will get an email as soon as an administrator approves it.', 'ai-guardwall' ) ); } return $user; } public static function registration_redirect( $url ) { return add_query_arg( 'svabg_pending', '1', wp_login_url() ); } public static function pending_message( $message ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['svabg_pending'] ) ) { $message .= '

' . esc_html__( 'Thank you for registering. An administrator will review your account; you will get an email when you can log in.', 'ai-guardwall' ) . '

'; } return $message; } public static function row_actions( $actions, $user ) { if ( current_user_can( 'edit_users' ) && get_user_meta( $user->ID, self::META_PENDING, true ) ) { $actions['svabg_approve'] = '' . esc_html__( 'Approve', 'ai-guardwall' ) . ''; } return $actions; } public static function handle_row_action() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only whether the action was asked for; the nonce is checked next, before the value is read. if ( ! isset( $_GET['svabg_approve'] ) || ! current_user_can( 'edit_users' ) ) { return; } check_admin_referer( 'svabg_approve_user' ); $id = absint( $_GET['svabg_approve'] ); if ( ! $id || ! current_user_can( 'edit_user', $id ) ) { wp_die( esc_html__( 'You are not allowed to approve this user.', 'ai-guardwall' ), 403 ); } self::approve( $id ); wp_safe_redirect( remove_query_arg( array( 'svabg_approve', '_wpnonce' ), wp_get_referer() ? wp_get_referer() : admin_url( 'users.php' ) ) ); exit; } public static function approve( $user_id ) { $user = get_userdata( $user_id ); if ( ! $user || ! get_user_meta( $user_id, self::META_PENDING, true ) ) { return false; } delete_user_meta( $user_id, self::META_PENDING ); Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'registration', sprintf( /* translators: %s: user login */ __( 'Account "%s" approved.', 'ai-guardwall' ), $user->user_login ), array(), $user_id ); wp_mail( $user->user_email, sprintf( /* translators: %s: setting */ __( '[%s] Your account was approved', 'ai-guardwall' ), wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ) ), sprintf( __( "Hello %1\$s,\n\nyour account on %2\$s was approved. You can log in here:\n%3\$s", 'ai-guardwall' ), $user->display_name, home_url( '/' ), wp_login_url() ) ); return true; } public static function pending_users() { return get_users( array( 'meta_key' => self::META_PENDING, // phpcs:ignore WordPress.DB.SlowDBQuery 'fields' => array( 'ID', 'user_login', 'user_email', 'user_registered' ), 'number' => 200, ) ); } public static function users_views( $views ) { $n = count( self::pending_users() ); if ( $n ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $current = isset( $_GET['svabg_pending'] ) ? ' class="current" aria-current="page"' : ''; $views['svabg_pending'] = '' . esc_html__( 'Waiting for approval', 'ai-guardwall' ) . ' (' . (int) $n . ')'; } return $views; } public static function filter_pending( $query ) { global $pagenow; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( 'users.php' === $pagenow && isset( $_GET['svabg_pending'] ) && is_admin() ) { $query->set( 'meta_key', self::META_PENDING ); } } /* ----------------------------- display names ---------------------------- */ /** * Accounts whose public display name is their login name (that shows the * login name on every post and comment). * * @return array[] */ public static function exposed_logins() { $out = array(); foreach ( get_users( array( 'role__in' => array( 'administrator', 'editor', 'author', 'shop_manager' ), 'fields' => array( 'ID', 'user_login', 'display_name', 'user_nicename' ) ) ) as $u ) { if ( strtolower( trim( $u->display_name ) ) === strtolower( $u->user_login ) ) { $out[] = array( 'id' => (int) $u->ID, 'login' => $u->user_login, 'display' => $u->display_name ); } } return $out; } /** * Give an account a display name that is not its login name. */ public static function fix_display_name( $user_id ) { $user = get_userdata( $user_id ); if ( ! $user ) { return new WP_Error( 'svabg_user', __( 'User not found.', 'ai-guardwall' ) ); } $name = trim( $user->first_name . ' ' . $user->last_name ); if ( '' === $name || strtolower( $name ) === strtolower( $user->user_login ) ) { $name = $user->nickname && strtolower( $user->nickname ) !== strtolower( $user->user_login ) ? $user->nickname : ucfirst( strtok( (string) $user->user_email, '@' ) ); } if ( strtolower( $name ) === strtolower( $user->user_login ) ) { $name = __( 'Site team', 'ai-guardwall' ); } wp_update_user( array( 'ID' => $user_id, 'display_name' => $name ) ); Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'user', sprintf( /* translators: 1: user login, 2: name */ __( 'Display name of "%1$s" changed to "%2$s" so the login name is no longer public.', 'ai-guardwall' ), $user->user_login, $name ), array(), $user_id ); return $name; } }