Index: CHANGELOG.txt =================================================================== RCS file: /cvs/drupal/drupal/CHANGELOG.txt,v retrieving revision 1.259 diff -u -p -r1.259 CHANGELOG.txt --- CHANGELOG.txt 31 Mar 2008 20:50:05 -0000 1.259 +++ CHANGELOG.txt 1 Apr 2008 11:54:18 -0000 @@ -15,6 +15,10 @@ Drupal 7.0, xxxx-xx-xx (development vers - Removed ping module: * This module has been removed from the core download. Contributed alternatives are available. +- Removed access rules: + * The access rules capability of user module has been stripped down to a + simple method for blocking ip addresses. Email and username restrictions + are now available in a contributed module. Drupal 6.0, 2008-02-13 ---------------------- Index: includes/bootstrap.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v retrieving revision 1.206 diff -u -p -r1.206 bootstrap.inc --- includes/bootstrap.inc 10 Jan 2008 22:47:17 -0000 1.206 +++ includes/bootstrap.inc 1 Apr 2008 11:54:18 -0000 @@ -847,31 +847,31 @@ function drupal_get_messages($type = NUL } /** - * Perform an access check for a given mask and rule type. Rules are usually - * created via admin/user/rules page. + * Check to see if an IP address has been blocked. * - * If any allow rule matches, access is allowed. Otherwise, if any deny rule - * matches, access is denied. If no rule matches, access is allowed. + * Blocked IP addresses are stored in the database by default. However for + * performance reasons we allow an override in settings.php. This allows us + * to avoid querying the database at this critical stage of the bootstrap if + * an administrative interface for IP address blocking is not required. * - * @param $type string - * Type of access to check: Allowed values are: - * - 'host': host name or IP address - * - 'mail': e-mail address - * - 'user': username - * @param $mask string - * String or mask to test: '_' matches any character, '%' matches any - * number of characters. + * @param $blocked_ips array + * Array of IP addresses defined in $conf. + * @param $ip string + * IP address to check. * @return bool * TRUE if access is denied, FALSE if access is allowed. */ -function drupal_is_denied($type, $mask) { - // Because this function is called for every page request, both cached - // and non-cached pages, we tried to optimize it as much as possible. - // We deny access if the only matching records in the {access} table have - // status 0 (deny). If any have status 1 (allow), or if there are no - // matching records, we allow access. - $sql = "SELECT 1 FROM {access} WHERE type = '%s' AND LOWER('%s') LIKE LOWER(mask) AND status = %d"; - return db_result(db_query_range($sql, $type, $mask, 0, 0, 1)) && !db_result(db_query_range($sql, $type, $mask, 1, 0, 1)); +function drupal_is_denied($blocked_ips, $ip) { + // Because this function is called on every page request, we first check + // for an array of IP addresses in settings.php before querying the + // database. + if (isset($blocked_ips)) { + return in_array($ip, $blocked_ips); + } + else { + $sql = "SELECT 1 FROM {blocked_ips} WHERE ip = '%s'"; + return db_result(db_query($sql, $ip)); + } } /** @@ -953,8 +953,8 @@ function _drupal_bootstrap($phase) { break; case DRUPAL_BOOTSTRAP_ACCESS: - // Deny access to hosts which were banned - t() is not yet available. - if (drupal_is_denied('host', ip_address())) { + // Deny access to blocked IP addresses - t() is not yet available. + if (drupal_is_denied($conf['blocked_ips'], ip_address())) { header('HTTP/1.1 403 Forbidden'); print 'Sorry, '. check_plain(ip_address()) .' has been banned.'; exit(); Index: modules/system/system.install =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.install,v retrieving revision 1.244 diff -u -p -r1.244 system.install --- modules/system/system.install 21 Mar 2008 08:52:25 -0000 1.244 +++ modules/system/system.install 1 Apr 2008 11:54:19 -0000 @@ -526,6 +526,19 @@ function system_schema() { 'token' => array('token'), ), ); + + $schema['blocked_ips'] = array( + 'description' => t('Stores blocked ip addresses.'), + 'fields' => array( + 'ip' => array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + ), + ), + 'primary key' => array('ip'), + ); $schema['cache'] = array( 'description' => t('Generic cache table for caching things not separated out into their own tables. Contributed modules may also use this to store cached items.'), @@ -580,7 +593,7 @@ function system_schema() { $schema['cache_page']['description'] = t('Cache table used to store compressed pages for anonymous users, if page caching is enabled.'); $schema['cache_menu'] = $schema['cache']; $schema['cache_menu']['description'] = t('Cache table for the menu system to store router information as well as generated link trees for various menu/page/user combinations.'); - + $schema['files'] = array( 'description' => t('Stores information for uploaded files.'), 'fields' => array( @@ -2668,6 +2681,29 @@ function system_update_7001() { return $ret; } +/** + * Add a table to store blocked ip addresses. + */ +function system_update_7002() { + $ret = array(); + + $schema['blocked_ips'] = array( + 'description' => t('Stores blocked ip addresses.'), + 'fields' => array( + 'ip' => array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + ), + ), + 'primary key' => array('ip'), + ); + + db_create_table($ret, 'denied_ips', $schema['denied_ips']); + + return $ret; +} /** * @} End of "defgroup updates-6.x-to-7.x" Index: modules/user/user-rtl.css =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user-rtl.css,v retrieving revision 1.3 diff -u -p -r1.3 user-rtl.css --- modules/user/user-rtl.css 27 Nov 2007 12:09:26 -0000 1.3 +++ modules/user/user-rtl.css 1 Apr 2008 11:54:19 -0000 @@ -4,11 +4,6 @@ padding-left: 0; padding-right: 1.5em; } -#access-rules .access-type, #access-rules .rule-type { - margin-right: 0; - margin-left: 1em; - float: right; -} #user-admin-buttons { float: right; margin-left: 0; Index: modules/user/user.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.admin.inc,v retrieving revision 1.19 diff -u -p -r1.19 user.admin.inc --- modules/user/user.admin.inc 20 Feb 2008 13:46:43 -0000 1.19 +++ modules/user/user.admin.inc 1 Apr 2008 11:54:19 -0000 @@ -712,206 +712,6 @@ function user_admin_role_submit($form, & } /** - * Menu callback: list all access rules - */ -function user_admin_access_check() { - $output = drupal_get_form('user_admin_check_user'); - $output .= drupal_get_form('user_admin_check_mail'); - $output .= drupal_get_form('user_admin_check_host'); - return $output; -} - -/** - * Menu callback: add an access rule - */ -function user_admin_access_add($mask = NULL, $type = NULL) { - if ($edit = $_POST) { - if (!$edit['mask']) { - form_set_error('mask', t('You must enter a mask.')); - } - else { - db_query("INSERT INTO {access} (mask, type, status) VALUES ('%s', '%s', %d)", $edit['mask'], $edit['type'], $edit['status']); - $aid = db_last_insert_id('access', 'aid'); - drupal_set_message(t('The access rule has been added.')); - drupal_goto('admin/user/rules'); - } - } - else { - $edit['mask'] = $mask; - $edit['type'] = $type; - } - return drupal_get_form('user_admin_access_add_form', $edit, t('Add rule')); -} - -/** - * Menu callback: edit an access rule - */ -function user_admin_access_edit($aid = 0) { - if ($edit = $_POST) { - if (!$edit['mask']) { - form_set_error('mask', t('You must enter a mask.')); - } - else { - db_query("UPDATE {access} SET mask = '%s', type = '%s', status = '%s' WHERE aid = %d", $edit['mask'], $edit['type'], $edit['status'], $aid); - drupal_set_message(t('The access rule has been saved.')); - drupal_goto('admin/user/rules'); - } - } - else { - $edit = db_fetch_array(db_query('SELECT aid, type, status, mask FROM {access} WHERE aid = %d', $aid)); - } - return drupal_get_form('user_admin_access_edit_form', $edit, t('Save rule')); -} - -/** - * Form builder; Configure access rules. - * - * @ingroup forms - */ -function user_admin_access_form(&$form_state, $edit, $submit) { - $form['status'] = array( - '#type' => 'radios', - '#title' => t('Access type'), - '#default_value' => isset($edit['status']) ? $edit['status'] : 0, - '#options' => array('1' => t('Allow'), '0' => t('Deny')), - ); - $type_options = array('user' => t('Username'), 'mail' => t('E-mail'), 'host' => t('Host')); - $form['type'] = array( - '#type' => 'radios', - '#title' => t('Rule type'), - '#default_value' => (isset($type_options[$edit['type']]) ? $edit['type'] : 'user'), - '#options' => $type_options, - ); - $form['mask'] = array( - '#type' => 'textfield', - '#title' => t('Mask'), - '#size' => 30, - '#maxlength' => 64, - '#default_value' => $edit['mask'], - '#description' => '%: '. t('Matches any number of characters, even zero characters') .'.
_: '. t('Matches exactly one character.'), - '#required' => TRUE, - ); - $form['submit'] = array('#type' => 'submit', '#value' => $submit); - - return $form; -} - -function user_admin_access_check_validate($form, &$form_state) { - if (empty($form_state['values']['test'])) { - form_set_error($form_state['values']['type'], t('No value entered. Please enter a test string and try again.')); - } -} - -function user_admin_check_user() { - $form['user'] = array('#type' => 'fieldset', '#title' => t('Username')); - $form['user']['test'] = array('#type' => 'textfield', '#title' => '', '#description' => t('Enter a username to check if it will be denied or allowed.'), '#size' => 30, '#maxlength' => USERNAME_MAX_LENGTH); - $form['user']['type'] = array('#type' => 'hidden', '#value' => 'user'); - $form['user']['submit'] = array('#type' => 'submit', '#value' => t('Check username')); - $form['#submit'][] = 'user_admin_access_check_submit'; - $form['#validate'][] = 'user_admin_access_check_validate'; - $form['#theme'] = 'user_admin_access_check'; - return $form; -} - -function user_admin_check_mail() { - $form['mail'] = array('#type' => 'fieldset', '#title' => t('E-mail')); - $form['mail']['test'] = array('#type' => 'textfield', '#title' => '', '#description' => t('Enter an e-mail address to check if it will be denied or allowed.'), '#size' => 30, '#maxlength' => EMAIL_MAX_LENGTH); - $form['mail']['type'] = array('#type' => 'hidden', '#value' => 'mail'); - $form['mail']['submit'] = array('#type' => 'submit', '#value' => t('Check e-mail')); - $form['#submit'][] = 'user_admin_access_check_submit'; - $form['#validate'][] = 'user_admin_access_check_validate'; - $form['#theme'] = 'user_admin_access_check'; - return $form; -} - -function user_admin_check_host() { - $form['host'] = array('#type' => 'fieldset', '#title' => t('Hostname')); - $form['host']['test'] = array('#type' => 'textfield', '#title' => '', '#description' => t('Enter a hostname or IP address to check if it will be denied or allowed.'), '#size' => 30, '#maxlength' => 64); - $form['host']['type'] = array('#type' => 'hidden', '#value' => 'host'); - $form['host']['submit'] = array('#type' => 'submit', '#value' => t('Check hostname')); - $form['#submit'][] = 'user_admin_access_check_submit'; - $form['#validate'][] = 'user_admin_access_check_validate'; - $form['#theme'] = 'user_admin_access_check'; - return $form; -} - -function user_admin_access_check_submit($form, &$form_state) { - switch ($form_state['values']['type']) { - case 'user': - if (drupal_is_denied('user', $form_state['values']['test'])) { - drupal_set_message(t('The username %name is not allowed.', array('%name' => $form_state['values']['test']))); - } - else { - drupal_set_message(t('The username %name is allowed.', array('%name' => $form_state['values']['test']))); - } - break; - case 'mail': - if (drupal_is_denied('mail', $form_state['values']['test'])) { - drupal_set_message(t('The e-mail address %mail is not allowed.', array('%mail' => $form_state['values']['test']))); - } - else { - drupal_set_message(t('The e-mail address %mail is allowed.', array('%mail' => $form_state['values']['test']))); - } - break; - case 'host': - if (drupal_is_denied('host', $form_state['values']['test'])) { - drupal_set_message(t('The hostname %host is not allowed.', array('%host' => $form_state['values']['test']))); - } - else { - drupal_set_message(t('The hostname %host is allowed.', array('%host' => $form_state['values']['test']))); - } - break; - default: - break; - } -} - -/** - * Menu callback: delete an access rule - * - * @ingroup forms - * @see user_admin_access_delete_confirm_submit() - */ -function user_admin_access_delete_confirm($form_state, $aid = 0) { - $access_types = array('user' => t('username'), 'mail' => t('e-mail'), 'host' => t('host')); - $edit = db_fetch_object(db_query('SELECT aid, type, status, mask FROM {access} WHERE aid = %d', $aid)); - - $form = array(); - $form['aid'] = array('#type' => 'hidden', '#value' => $aid); - $output = confirm_form($form, - t('Are you sure you want to delete the @type rule for %rule?', array('@type' => $access_types[$edit->type], '%rule' => $edit->mask)), - 'admin/user/rules', - t('This action cannot be undone.'), - t('Delete'), - t('Cancel')); - return $output; -} - -function user_admin_access_delete_confirm_submit($form, &$form_state) { - db_query('DELETE FROM {access} WHERE aid = %d', $form_state['values']['aid']); - drupal_set_message(t('The access rule has been deleted.')); - $form_state['redirect'] = 'admin/user/rules'; - return; -} - -/** - * Menu callback: list all access rules - */ -function user_admin_access() { - $header = array(array('data' => t('Access type'), 'field' => 'status'), array('data' => t('Rule type'), 'field' => 'type'), array('data' => t('Mask'), 'field' => 'mask'), array('data' => t('Operations'), 'colspan' => 2)); - $result = db_query("SELECT aid, type, status, mask FROM {access}". tablesort_sql($header)); - $access_types = array('user' => t('username'), 'mail' => t('e-mail'), 'host' => t('host')); - $rows = array(); - while ($rule = db_fetch_object($result)) { - $rows[] = array($rule->status ? t('allow') : t('deny'), $access_types[$rule->type], $rule->mask, l(t('edit'), 'admin/user/rules/edit/'. $rule->aid), l(t('delete'), 'admin/user/rules/delete/'. $rule->aid)); - } - if (empty($rows)) { - $rows[] = array(array('data' => ''. t('There are currently no access rules.') .'', 'colspan' => 5)); - } - return theme('table', $header, $rows); -} - -/** * Theme user administration overview. * * @ingroup themeable Index: modules/user/user.css =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.css,v retrieving revision 1.8 diff -u -p -r1.8 user.css --- modules/user/user.css 20 Feb 2008 13:46:43 -0000 1.8 +++ modules/user/user.css 1 Apr 2008 11:54:19 -0000 @@ -9,16 +9,6 @@ #permissions tr.odd .form-item, tr.even .form-item { white-space: normal; } -#access-rules .access-type, #access-rules .rule-type { - margin-right: 1em; /* LTR */ - float: left; /* LTR */ -} -#access-rules .access-type .form-item, #access-rules .rule-type .form-item { - margin-top: 0; -} -#access-rules .mask { - clear: both; -} #user-login-form { text-align: center; } Index: modules/user/user.install =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.install,v retrieving revision 1.8 diff -u -p -r1.8 user.install --- modules/user/user.install 31 Mar 2008 20:50:05 -0000 1.8 +++ modules/user/user.install 1 Apr 2008 11:54:19 -0000 @@ -5,39 +5,6 @@ * Implementation of hook_schema(). */ function user_schema() { - $schema['access'] = array( - 'description' => t('Stores site access rules.'), - 'fields' => array( - 'aid' => array( - 'type' => 'serial', - 'not null' => TRUE, - 'description' => t('Primary Key: Unique access ID.'), - ), - 'mask' => array( - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - 'default' => '', - 'description' => t('Text mask used for filtering access.'), - ), - 'type' => array( - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - 'default' => '', - 'description' => t('Type of access rule: name, mail or host.'), - ), - 'status' => array( - 'type' => 'int', - 'not null' => TRUE, - 'default' => 0, - 'size' => 'tiny', - 'description' => t('Whether rule is to allow(1) or deny(0) access.'), - ), - ), - 'primary key' => array('aid'), - ); - $schema['authmap'] = array( 'description' => t('Stores distributed authentication mapping.'), 'fields' => array( Index: modules/user/user.module =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.module,v retrieving revision 1.898 diff -u -p -r1.898 user.module --- modules/user/user.module 31 Mar 2008 20:50:05 -0000 1.898 +++ modules/user/user.module 1 Apr 2008 11:54:20 -0000 @@ -92,7 +92,7 @@ function user_external_load($authname) { * Perform standard Drupal login operations for a user object. * * The user object must already be authenticated. This function verifies - * that the user account is not blocked/denied and then performs the login, + * that the user account is not blocked and then performs the login, * updates the login timestamp in the database, invokes hook_user('login'), * and regenerates the session. * @@ -113,7 +113,7 @@ function user_external_login($account, $ $state['values']['name'] = $account->name; } - // Check if user is blocked or denied by access rules. + // Check if user is blocked. user_login_name_validate($form, $state, (array)$account); if (form_get_errors()) { // Invalid login. @@ -952,43 +952,6 @@ function user_menu() { 'type' => MENU_CALLBACK, 'file' => 'user.admin.inc', ); - $items['admin/user/rules'] = array( - 'title' => 'Access rules', - 'description' => 'List and create rules to disallow usernames, e-mail addresses, and IP addresses.', - 'page callback' => 'user_admin_access', - 'access arguments' => array('administer permissions'), - 'file' => 'user.admin.inc', - ); - $items['admin/user/rules/list'] = array( - 'title' => 'List', - 'type' => MENU_DEFAULT_LOCAL_TASK, - 'weight' => -10, - ); - $items['admin/user/rules/add'] = array( - 'title' => 'Add rule', - 'page callback' => 'user_admin_access_add', - 'type' => MENU_LOCAL_TASK, - 'file' => 'user.admin.inc', - ); - $items['admin/user/rules/check'] = array( - 'title' => 'Check rules', - 'page callback' => 'user_admin_access_check', - 'type' => MENU_LOCAL_TASK, - 'file' => 'user.admin.inc', - ); - $items['admin/user/rules/edit'] = array( - 'title' => 'Edit rule', - 'page callback' => 'user_admin_access_edit', - 'type' => MENU_CALLBACK, - 'file' => 'user.admin.inc', - ); - $items['admin/user/rules/delete'] = array( - 'title' => 'Delete rule', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('user_admin_access_delete_confirm'), - 'type' => MENU_CALLBACK, - 'file' => 'user.admin.inc', - ); $items['logout'] = array( 'title' => 'Log out', @@ -1219,7 +1182,7 @@ function user_login(&$form_state, $msg = } /** - * Set up a series for validators which check for blocked/denied users, + * Set up a series for validators which check for blocked users, * then authenticate against local database, then return an error if * authentication fails. Distributed authentication modules are welcome * to use hook_form_alter() to change this series in order to @@ -1240,8 +1203,7 @@ function user_login_default_validators() } /** - * A FAPI validate handler. Sets an error if supplied username has been blocked - * or denied access. + * A FAPI validate handler. Sets an error if supplied username has been blocked. */ function user_login_name_validate($form, &$form_state) { if (isset($form_state['values']['name'])) { @@ -1249,10 +1211,6 @@ function user_login_name_validate($form, // blocked in user administration form_set_error('name', t('The username %name has not been activated or is blocked.', array('%name' => $form_state['values']['name']))); } - else if (drupal_is_denied('user', $form_state['values']['name'])) { - // denied by access controls - form_set_error('name', t('The name %name is a reserved username.', array('%name' => $form_state['values']['name']))); - } } } @@ -1504,9 +1462,6 @@ function _user_edit_validate($uid, &$edi else if (db_result(db_query("SELECT COUNT(*) FROM {users} WHERE uid != %d AND LOWER(name) = LOWER('%s')", $uid, $edit['name'])) > 0) { form_set_error('name', t('The name %name is already taken.', array('%name' => $edit['name']))); } - else if (drupal_is_denied('user', $edit['name'])) { - form_set_error('name', t('The name %name has been denied access.', array('%name' => $edit['name']))); - } } // Validate the e-mail address: @@ -1516,9 +1471,6 @@ function _user_edit_validate($uid, &$edi else if (db_result(db_query("SELECT COUNT(*) FROM {users} WHERE uid != %d AND LOWER(mail) = LOWER('%s')", $uid, $edit['mail'])) > 0) { form_set_error('mail', t('The e-mail address %email is already registered. Have you forgotten your password?', array('%email' => $edit['mail'], '@password' => url('user/password')))); } - else if (drupal_is_denied('mail', $edit['mail'])) { - form_set_error('mail', t('The e-mail address %email has been denied access.', array('%email' => $edit['mail']))); - } } function _user_edit_submit($uid, &$edit) { @@ -1853,8 +1805,6 @@ function user_help($path, $arg) { case 'admin/user/user/create': case 'admin/user/user/account/create': return '

'. t("This web page allows administrators to register new users. Users' e-mail addresses and usernames must be unique.") .'

'; - case 'admin/user/rules': - return '

'. t('Set up username and e-mail address access rules for new and existing accounts (currently logged in accounts will not be logged out). If a username or e-mail address for an account matches any deny rule, but not an allow rule, then the account will not be allowed to be created or to log in. A host rule is effective for every page view, not just registrations.') .'

'; case 'admin/user/permissions': return '

'. t('Permissions let you control what users can do on your site. Each user role (defined on the user roles page) has its own set of permissions. For example, you could give users classified as "Administrators" permission to "administer nodes" but deny this power to ordinary, "authenticated" users. You can use permissions to reveal new features to privileged users (those with subscriptions, for example). Permissions also allow trusted users to share the administrative burden of running a busy site.', array('@role' => url('admin/user/roles'))) .'

'; case 'admin/user/roles': @@ -2211,16 +2161,6 @@ function user_block_user_action(&$object } /** - * Implementation of a Drupal action. - * Adds an access rule that blocks the user's IP address. - */ -function user_block_ip_action() { - $ip = ip_address(); - db_query("INSERT INTO {access} (mask, type, status) VALUES ('%s', '%s', %d)", $ip, 'host', 0); - watchdog('action', 'Banned IP address %ip', array('%ip' => $ip)); -} - -/** * Submit handler for the user registration form. * * This function is shared by the installation form and the normal registration form, Index: sites/default/default.settings.php =================================================================== RCS file: /cvs/drupal/drupal/sites/default/default.settings.php,v retrieving revision 1.9 diff -u -p -r1.9 default.settings.php --- sites/default/default.settings.php 21 Mar 2008 09:16:37 -0000 1.9 +++ sites/default/default.settings.php 1 Apr 2008 11:54:20 -0000 @@ -226,3 +226,17 @@ ini_set('url_rewriter.tags', ''); # 'forum' => 'Discussion board', # '@count min' => '@count minutes', # ); + +/** + * + * Use this setting to bypass database queries for denied IP addresses. + * If you enable this setting after using the administrative interface to + * block ip addresses you will need to add them again here. This allows + * Drupal to serve pages to anonymous visitors without any database access + * using certain caching configurations. + * + * Remove the leading hash signs to enable. + */ +# $conf['blocked_ips'] = array( +# 'a.b.c.d', +# ); \ No newline at end of file