/[drupal]/contributions/modules/securesite/securesite.admin.inc
ViewVC logotype

Contents of /contributions/modules/securesite/securesite.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.13 - (show annotations) (download) (as text)
Wed Oct 29 22:20:36 2008 UTC (12 months, 4 weeks ago) by darrenoh
Branch: MAIN
CVS Tags: DRUPAL-6--2-0, HEAD
Branch point for: DRUPAL-6--2
Changes since 1.12: +13 -2 lines
File MIME type: text/x-php
Eliminated cron.php bypass.
1 <?php
2 // $Id: securesite.admin.inc,v 1.12 2008/10/29 18:58:02 darrenoh Exp $
3
4 /**
5 * @file securesite.admin.inc
6 *
7 * Secure Site admin pages
8 */
9
10 /**
11 * Module help page.
12 */
13 function _securesite_admin_help() {
14 return '<p>'. t('Secure Site allows site administrators to make a site or part of a site private. You can restrict access to the site by role. This means the site will be inaccessible to search engines and other crawlers, but you can still allow access to certain people.') .'</p>'."\n".
15 '<p>'. t('You can also secure remote access to RSS feeds. You can keep content private and protected, but still allow users to get notification of new content and other actions via RSS with news readers that support <em>user:pass@example.com/node/feed</em> URLs, or have direct support for user name and password settings. This is especially useful when paired with the Organic Groups module or other node access systems.') .'</p>'."\n".
16 '<h3>'. t('Configuration') .'</h3>'."\n".
17 '<ul>'."\n".
18 ' <li>'. t('Force authentication') ."\n".
19 ' <p>'. t('This setting controls whether users will be forced to authenticate before viewing pages. By default, authentication is never forced.') .'</p>'."\n".
20 ' <ol>'."\n".
21 ' <li>'. t('Never') ."\n".
22 ' <p>'. t('This setting will prevent Secure Site from hiding pages.') .'</p>'."\n".
23 ' </li>'."\n".
24 ' <li>'. t('Always') ."\n".
25 ' <p>'. t('This setting will hide your entire site from unauthenticated users.') ."\n".
26 ' </li>'."\n".
27 ' <li>'. t('During maintenance') ."\n".
28 ' <p>'. t('This setting will hide your site during maintenance.') ."\n".
29 ' </li>'."\n".
30 ' <li>'. t('On restricted pages') ."\n".
31 ' <p>'. t('This setting will hide only pages that anonymous users cannot access.') ."\n".
32 ' </li>'."\n".
33 ' </ol>'."\n".
34 ' </li>'."\n".
35 ' <li>'. t('Authentication type') ."\n".
36 ' <p>'. t('Three methods of authentication are available. Please note that HTTP authentication requires extra configuration if PHP is not installed as an Apache module. See the <a href="#issues">Known issues</a> section for details.') ."\n".
37 ' <ol>'."\n".
38 ' <li>'. t('HTTP digest') ."\n".
39 ' <p>'. t('This will enable HTTP digest authentication. The user&rsquo;s browser will prompt for the user&rsquo;s name and password before displaying the page.') .'</p>'."\n".
40 ' <p>'. t('Digest authentication protects a user&rsquo;s password from eavesdroppers when you are not using SSL to encrypt the connection. However, it can only be used when a copy of the password is stored on the server. For security reasons, Drupal does not store passwords. You will need to configure scripts to securely save passwords and authenticate users. See the <a href="#passwords">Secure password storage</a> section for details.') .'</p>'."\n".
41 ' <p>'. t('When digest authentication is enabled, passwords will be saved when users log in or set their passwords. If you use digest authentication to protect your whole site, you should allow guest access or allow another authentication type until users whose passwords are not yet saved have logged in. Otherwise, you may lock yourself out of your own site.') .'</p>'."\n".
42 ' </li>'."\n".
43 ' <li>'. t('HTTP basic') ."\n".
44 ' <p>'. t('This will enable HTTP basic authentication. The user&rsquo;s browser will prompt for the user&rsquo;s name and password before displaying the page. Basic authentication is not secure unless you are using SSL to encrypt the connection.') .'</p>'."\n".
45 ' </li>'."\n".
46 ' <li>'. t('HTML log-in form') ."\n".
47 ' <p>'. t('This method uses a themeable HTML log-in form for user name and password input. This method is the most reliable as it does not rely on the browser for authentication. Like HTTP basic, it is insecure unless you are using SSL to encrypt the connection.') .'</p>'."\n".
48 ' </li>'."\n".
49 ' </ol>'."\n".
50 ' <p>'. t('HTTP authentication is recommended for secure feeds, because feed readers are not likely to be able to handle forms. You can enable all three types of authentication at the same time.') .'</p>'."\n".
51 ' </li>'."\n".
52 ' <li>'. t('Digest authentication script') ."\n".
53 ' <p>'. t('For security, HTTP digest authentication uses an external script to check passwords. Enter the digest authentication script exactly as it would appear on the command line.') .'</p>'."\n".
54 ' </li>'."\n".
55 ' <li>'. t('Password storage script') ."\n".
56 ' <p>'. t('For security, HTTP digest authentication uses an external script to save passwords. Enter the password storage script exactly as it would appear on the command line.') .'</p>'."\n".
57 ' </li>'."\n".
58 ' <li>'. t('Authentication realm') ."\n".
59 ' <p>'. t('You can use this field to name your log-in area. This is primarily used with HTTP Auth.') .'</p>'."\n".
60 ' </li>'."\n".
61 ' <li>'. t('Guest user name and password') ."\n".
62 ' <p>'. t('If you give anonymous users the <em>!access</em> permission, you can set a user name and password for anonymous users. If not set, guests can use any name and password.', array('!access' => l(t('access secured pages'), 'admin/user/permissions', array('fragment' => 'module-securesite')))) .'</p>'."\n".
63 ' </li>'."\n".
64 ' <li>'. t('Customize HTML forms') ."\n".
65 ' <p>'. t('<em>Custom message for log-in form</em> and <em>Custom message for password reset form</em> are used in the HTML forms when they are displayed. If the latter box is empty, Secure Site will not offer to reset passwords. Please note, the log-in form is only displayed when the HTML log-in form authentication mode is used.') .'</p>'."\n".
66 ' </li>'."\n".
67 '</ul>'."\n".
68 '<h3><a name="passwords">'. t('Secure password storage') .'</a></h3>'."\n".
69 '<p>'. t('Digest authentication avoids transmitting passwords by exchanging character strings (digests) that prove both the user and the Web server know the password. This requires passwords for all users to be stored on the server. It is very important to ensure that these passwords cannot be exposed to unauthorized users. Drupal must be able to store passwords but should not be able to retrieve them.') .'</p>'."\n".
70 '<p>'. t('Secure Site provides scripts that can handle stored passwords securely when properly set up. These scripts are contained in the <em>digest_md5</em> directory. There are two scripts in this directory:') .'</p>'."\n".
71 '<dl>'."\n".
72 ' <dt>stored_passwords.php</dt><dd>'. t('Add, delete, and update user passwords.') .'</dd>'."\n".
73 ' <dt>digest_md5.php</dt><dd>'. t('Perform digest authentication') .'</dd>'."\n".
74 '</dl>'."\n".
75 '<p>'. t('You can get help for these scripts by typing the script name followed by <em>--help</em> on the command line. You must be able to run PHP from the command line. Some configuration is required to make the scripts work properly:') .'</p>'."\n".
76 '<ol>'."\n".
77 ' <li>'. t('Set up a secure database') ."\n".
78 ' <p>'. t('You can set up a password database in the same way you create a Drupal database. However, you should create a special database user for your password database. No other database users should have access to the password database.') .'</p>'."\n".
79 ' </li>'."\n".
80 ' <li>'. t('Edit the configuration file') ."\n".
81 ' <p>'. t('Configuration settings for the scripts are in <em>digest_md5.conf.php</em>, in the <em>digest_md5</em> directory. You must set <em>$db_url</em> to point to your password database. If you want to be able to use the scripts from the command-line, you must set <em>$drupal</em> to the absolute path to your Drupal installation. When you are done editing the configuration file, make it read-only.') .'</p>'."\n".
82 ' </li>'."\n".
83 ' <li>'. t('Control access to the scripts') ."\n".
84 ' <p>'. t('The first thing you can do secure the scripts is to move the <em>digest_md5</em> directory to a location that is not accessible from the Internet. The configuration file especially needs protection, because it contains information that allows access to the password database. On the Secure Site settings page, change the digest authentication script and password storage script to point to the new location. For example, if you moved the <em>digest_md5</em> directory to <em>/usr/local</em>, you would use') .'</p>'."\n".
85 ' <pre>/usr/local/digest_md5/digest_md5.php'."\n".
86 '/usr/local/digest_md5/stored_passwords.php</pre>'."\n".
87 ' <p>'. t('If the <em>sudo</em> command is available on your system, you can change the file system permissions on the all the files in the <em>digest_md5</em> directory so that only adminstrators have access to them. You would then add the user your Web server runs as to the sudoers file. A sample sudoers file is provided in the <em>digest_md5</em> directory for comparison. The important lines are') .'</p>'."\n".
88 ' <pre>Defaults:apache !authenticate'."\n".
89 'Defaults:apache !lecture'."\n".
90 'apache ALL=/usr/local/digest_md5/stored_passwords.php [A-z]*'."\n".
91 'apache ALL=/usr/local/digest_md5/digest_md5.php [A-z]*</pre>'."\n".
92 ' <p>'. t('This allows <em>apache</em> to use sudo only to run <em>stored_passwords.php</em> and <em>digest_md5.php</em>. Replace <em>apache</em> with the name of the Web server user on your system, and replace <em>/usr/local</em> with the directory in which you placed the <em>digest_md5</em> directory. On the Secure Site settings page, add <em>sudo</em> at the beginning of the line for the digest authentication script and the password storage script:') .'</p>'."\n".
93 ' <pre>sudo /usr/local/digest_md5/digest_md5.php'."\n".
94 'sudo /usr/local/digest_md5/stored_passwords.php</pre>'."\n".
95 ' <p>'. t('If the rest of your system is secure, Drupal can now store passwords without being able to retrieve them.') .'</p>'."\n".
96 ' </li>'."\n".
97 '</ol>'."\n".
98 '<h3>'. t('Theming') .'</h3>'."\n".
99 '<p>'. t('Secure Site&rsquo;s HTML output is controlled by three files:') .'</p>'."\n".
100 '<dl>'."\n".
101 ' <dt>securesite-page.tpl.php<dt><dd>'. t('Template for Secure Site pages. Works in the same way as page.tpl.php.') .'</dd>'."\n".
102 ' <dt>securesite-user-login.tpl.php<dt><dd>'. t('Template for the user log-in form.') .'</dd>'."\n".
103 ' <dt>securesite-user-pass.tpl.php<dt><dd>'. t('Template for the password reset form.') .'</dd>'."\n".
104 '</dl>'."\n".
105 '<p>'. t('You can theme Secure Site&rsquo;s HTML output by copying these files to your theme&rsquo;s directory. The files in your theme&rsquo;s directory will become the templates for all Secure Site HTML output.') .'</p>'."\n".
106 '<h3>'. t('Configuring cron jobs') .'</h3>'."\n".
107 '<p>'. t('If HTTP authentication is forced, cron jobs will need to authenticate themselves. See !link for more details on configuring cron jobs. These examples show how to add a user name and password:', array('!link' => l(t('Configuring cron jobs'), 'http://drupal.org/cron'))) .'</p>'."\n".
108 '<pre>45 * * * * /usr/bin/lynx -auth=<em>username</em>:<em>password</em> -source http://example.com/cron.php'."\n".
109 '45 * * * * /usr/bin/wget --user=<em>username</em> --password=<em>password</em> -O - -q http://example.com/cron.php'."\n".
110 '45 * * * * /usr/bin/curl --user <em>username</em>:<em>password</em> --silent --compressed http://example.com/cron.php</pre>'."\n".
111 '<h3><a name="issues">'. t('Known issues') .'</a></h3>'."\n".
112 '<ul>'."\n".
113 ' <li>'. t('Authentication on PHP/CGI installations') ."\n".
114 ' <p>'. t('If you are using HTTP Auth and are unable to log-in, PHP could be running in CGI mode. When run in CGI mode, the normal HTTP Auth log-in variables are not available to PHP. To work-around this issue, add the following rewrite rule at the end of the .htaccess file in Drupal&rsquo;s root installation directory:') .'</p>'."\n".
115 ' <pre>RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]</pre>'."\n".
116 ' <p>'. t('After making the suggested change in Drupal 6, the rewrite rules would look like this:') .'</p>'."\n".
117 ' <pre># Rewrite URLs of the form \'x\' to the form \'index.php?q=x\'.'."\n".
118 'RewriteCond %{REQUEST_FILENAME} !-f'."\n".
119 'RewriteCond %{REQUEST_FILENAME} !-d'."\n".
120 'RewriteCond %{REQUEST_URI} !=/favicon.ico'."\n".
121 'RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]'."\n".
122 'RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]</pre>'."\n".
123 ' </li>'."\n".
124 ' <li>'. t('Authentication when running Drupal via IIS') ."\n".
125 ' <p>'. t('If you are using HTTP Auth and are unable to log-in when Drupal is running on an IIS server, make sure that the PHP directive <em>cgi.rfc2616_headers</em> is set to <em>0</em> (the default value).') .'</p>'."\n".
126 ' </li>'."\n".
127 '</ul>'."\n";
128 }
129
130 /**
131 * FAPI definition for Secure Site admin settings form
132 * @ingroup forms
133 * @see system_settings_form()
134 * @see securesite_admin_settings_validate()
135 * @see securesite_admin_settings_submit()
136 */
137 function securesite_admin_settings() {
138 $form['authentication'] = array(
139 '#type' => 'fieldset',
140 '#title' => t('Authentication'),
141 '#description' => t('Enable Secure Site below. Users must have the <em>!access</em> permission in order to access the site if authentication is forced.', array('!access' => l(t('access secured pages'), 'admin/user/permissions', array('fragment' => 'module-securesite'))))
142 );
143 $form['authentication']['securesite_enabled'] = array(
144 '#type' => 'radios',
145 '#title' => t('Force authentication'),
146 '#default_value' => variable_get('securesite_enabled', SECURESITE_DISABLED),
147 '#options' => array(
148 SECURESITE_DISABLED => t('Never'),
149 SECURESITE_ALWAYS => t('Always'),
150 SECURESITE_OFFLINE => t('During maintenance'),
151 SECURESITE_403 => t('On restricted pages'),
152 ),
153 '#description' => t('Choose when to force authentication.'),
154 );
155 $form['authentication']['securesite_type'] = array(
156 '#type' => 'checkboxes',
157 '#title' => t('Allowed authentication types'),
158 '#default_value' => variable_get('securesite_type', array(SECURESITE_BASIC)),
159 '#options' => array(
160 SECURESITE_DIGEST => t('HTTP digest'),
161 SECURESITE_BASIC => t('HTTP basic'),
162 SECURESITE_FORM => t('HTML log-in form'),
163 ),
164 '#required' => TRUE,
165 );
166 $form['authentication']['securesite_type']['#description'] = "\n".'<p>'.
167 t('HTTP authentication requires extra configuration if PHP is not installed as an Apache module. See the !link section of the Secure Site help for details.', array('!link' => l(t('Known issues'), 'admin/help/securesite', array('fragment' => 'issues')))) .'</p>'."\n".'<p>'.
168 t('Digest authentication protects a user&rsquo;s password from eavesdroppers when you are not using SSL to encrypt the connection. However, it can only be used when a copy of the password is stored on the server.') .' '.
169 t('For security reasons, Drupal does not store passwords. You will need to configure scripts to securely save passwords and authenticate users. See the !link section of the Secure Site help for details.', array('!link' => l(t('Secure password storage'), 'admin/help/securesite', array('fragment' => 'passwords')))) .'</p>'."\n".'<p>'.
170 t('When digest authentication is enabled, passwords will be saved when users log in or set their passwords. If you use digest authentication to protect your whole site, you should allow guest access or allow another authentication type until users whose passwords are not yet saved have logged in. Otherwise, <strong>you may lock yourself out of your own site.</strong>') .'</p>'."\n";
171 $form['authentication']['securesite_digest_script'] = array(
172 '#type' => 'textfield',
173 '#title' => t('Digest authentication script'),
174 '#default_value' => variable_get('securesite_digest_script', drupal_get_path('module', 'securesite') .'/digest_md5/digest_md5.php'),
175 '#length' => 30,
176 '#maxlength' => 300,
177 '#description' => t('Enter the digest authentication script exactly as it should appear on the command line. Use absolute paths.'),
178 );
179 $form['authentication']['securesite_password_script'] = array(
180 '#type' => 'textfield',
181 '#title' => t('Password storage script'),
182 '#default_value' => variable_get('securesite_password_script', drupal_get_path('module', 'securesite') .'/digest_md5/stored_passwords.php'),
183 '#length' => 30,
184 '#maxlength' => 300,
185 '#description' => t('Enter the password storage script exactly as it should appear on the command line. Use absolute paths.'),
186 );
187 $form['authentication']['securesite_realm'] = array(
188 '#type' => 'textfield',
189 '#title' => t('Authentication realm'),
190 '#default_value' => variable_get('securesite_realm', variable_get('site_name', 'Drupal')),
191 '#length' => 30,
192 '#maxlength' => 40,
193 '#description' => t('Name to identify the log-in area in the HTTP authentication dialog.'),
194 );
195 $form['guest'] = array(
196 '#type' => 'fieldset',
197 '#title' => t('Guest access'),
198 '#description' => t('Guest access allows anonymous users to view secure pages, though they will still be prompted for a user name and password. If you give anonymous users the <em>!access</em> permission, you can set the user name and password for anonymous users below.', array('!access' => l(t('access secured pages'), 'admin/user/permissions', array('fragment' => 'module-securesite')))),
199 );
200 $guest_access = !user_access('access secured pages', drupal_anonymous_user());
201 $form['guest']['securesite_guest_name'] = array(
202 '#type' => 'textfield',
203 '#title' => t('Guest user'),
204 '#default_value' => variable_get('securesite_guest_name', ''),
205 '#length' => 30,
206 '#maxlength' => 40,
207 '#description' => t('Leave empty to accept any name.'),
208 '#disabled' => $guest_access,
209 );
210 $form['guest']['securesite_guest_pass'] = array(
211 '#type' => 'textfield',
212 '#title' => t('Guest password'),
213 '#default_value' => variable_get('securesite_guest_pass', ''),
214 '#length' => 30,
215 '#maxlength' => 40,
216 '#description' => t('Leave empty to accept any password.'),
217 '#disabled' => $guest_access,
218 );
219 $form['login_form'] = array(
220 '#type' => 'fieldset',
221 '#title' => t('Customize HTML forms'),
222 '#description' => t('Configure the message displayed on the HTML log-in form (if enabled) and password reset form below.')
223 );
224 $form['login_form']['securesite_login_form'] = array(
225 '#type' => 'textarea',
226 '#title' => t('Custom message for HTML log-in form'),
227 '#default_value' => variable_get('securesite_login_form', t('Enter your user name and password:')),
228 '#length' => 60,
229 '#height' => 3,
230 );
231 $form['login_form']['securesite_reset_form'] = array(
232 '#type' => 'textarea',
233 '#title' => t('Custom message for password reset form'),
234 '#default_value' => variable_get('securesite_reset_form', t('Enter your user name or e-mail address:')),
235 '#length' => 60,
236 '#height' => 3,
237 '#description' => t('Leave empty to disable Secure Site&rsquo;s password reset form.'),
238 );
239 $form['#submit'][] = 'securesite_admin_settings_submit';
240 return system_settings_form($form);
241 }
242
243 function securesite_admin_settings_validate($form, &$form_state) {
244 foreach ($form_state['values']['securesite_type'] as $type => $value) {
245 if (empty($value)) {
246 unset($form_state['values']['securesite_type'][$type]);
247 }
248 }
249 sort($form_state['values']['securesite_type']);
250 }
251
252 /**
253 * Restore core 403 error page when not using Secure Site page.
254 */
255 function securesite_admin_settings_submit($form, &$form_state) {
256 $values = $form_state['values'];
257 if ($values['securesite_enabled'] != SECURESITE_403 || isset($values['op']) && $values['op'] == t('Reset to defaults')) {
258 variable_set('site_403', variable_get('securesite_403', variable_get('site_403', '')));
259 variable_del('securesite_403');
260 }
261 $script = variable_get('securesite_password_script', drupal_get_path('module', 'securesite') .'/digest_md5/stored_passwords.php');
262 $realm = variable_get('securesite_realm', variable_get('site_name', 'Drupal'));
263 $securesite_guest_name = variable_get('securesite_guest_name', '');
264 if (
265 in_array(SECURESITE_DIGEST, variable_get('securesite_type', array(SECURESITE_BASIC))) && ( // Digest authentication was enabled.
266 $values['securesite_guest_name'] != $securesite_guest_name || // Guest user name has changed.
267 !in_array(SECURESITE_DIGEST, $values['securesite_type']) || // Digest authentication is being disabled.
268 isset($values['op']) && $values['op'] == t('Reset to defaults') // Values are being reset to defaults.
269 )
270 ) {
271 // Delete guest user password.
272 $args = array(
273 escapeshellarg("name=$securesite_guest_name"),
274 escapeshellarg("realm=$realm"),
275 escapeshellarg('op=delete'),
276 );
277 exec($script .' '. implode(' ', $args));
278 }
279 elseif (in_array(SECURESITE_DIGEST, $values['securesite_type'])) {
280 // If digest authentication is enabled, update guest user password.
281 $args = array(
282 escapeshellarg("name=$values[securesite_guest_name]"),
283 escapeshellarg("realm=$realm"),
284 escapeshellarg("pass=$values[securesite_guest_pass]"),
285 );
286 exec($script .' '. implode(' ', $args));
287 }
288 }
289

  ViewVC Help
Powered by ViewVC 1.1.2