| 1 |
<?php |
<?php |
| 2 |
// $Id: webserver_auth.module,v 1.12 2005/04/29 19:27:31 weitzman Exp $ |
// $Id: webserver_auth.module,v 1.22 2008/07/17 20:37:37 weitzman Exp $ |
| 3 |
|
|
| 4 |
|
function webserver_auth_menu() { |
| 5 |
|
$items = array(); |
| 6 |
|
$items['admin/settings/webserver_auth'] = array( |
| 7 |
|
'title' => t('Webserver authentication'), |
| 8 |
|
'description' => t('Configure a domain for generating email addresses. Optional.'), |
| 9 |
|
'page callback' => 'drupal_get_form', |
| 10 |
|
'page arguments' => array('webserver_auth_settings'), |
| 11 |
|
'access arguments' => array('administer site configuration'), |
| 12 |
|
); |
| 13 |
|
return $items; |
| 14 |
|
} |
| 15 |
|
|
| 16 |
function webserver_auth_init() { |
function webserver_auth_init() { |
| 17 |
global $user, $account; |
global $user; |
| 18 |
|
|
| 19 |
if ($user->uid) { |
$authname = ''; |
| 20 |
//do nothing because user is already logged into Drupal |
|
| 21 |
|
// Make sure we get the remote user whichever way it is available. |
| 22 |
|
if (isset($_SERVER['REDIRECT_REMOTE_USER'])) { |
| 23 |
|
$authname = $_SERVER['REDIRECT_REMOTE_USER']; |
| 24 |
|
} |
| 25 |
|
elseif (isset($_SERVER['REMOTE_USER'])) { |
| 26 |
|
$authname = $_SERVER['REMOTE_USER']; |
| 27 |
} |
} |
|
else { |
|
|
if ($name = $_SERVER["REMOTE_USER"]) { |
|
|
// user is logged into webserver. |
|
|
$account->name = $name; |
|
|
//modules get to change the user bits before saving. use a global $account to do so. |
|
|
// only loaded modules will see this hook |
|
|
module_invoke_all("webserver_auth"); |
|
|
// if we are in bootstrap, load user.module ourselves |
|
|
if (!module_exist('user')) { |
|
|
drupal_load('module', 'user'); |
|
|
} |
|
| 28 |
|
|
| 29 |
// try to log into Drupal. if unsuccessful, register the user |
// Perform some cleanup so plaintext passwords aren't available under |
| 30 |
$user = user_external_load($account->name); |
// mod_auth_kerb. |
| 31 |
if (!$user->uid) { |
unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); |
| 32 |
if (variable_get("user_register", 1) == 1) { |
|
| 33 |
$user_default = array("name" => $account->name, "pass" => "cyan", "init" => db_escape_string($name), "authname_webserver_auth" => $account->name, "status" => 1, "roles" => array(_user_authenticated_id())); |
// Retrieve user credentials |
| 34 |
// TODO - the hook_user('register') will fire but only for loaded modules. cold be a problem for sites using page cache and that hook+operation |
$result = db_query("SELECT uid FROM {authmap} WHERE authname = '%s' AND module = 'webserver_auth'", $authname); |
| 35 |
$user = user_save("", array_merge($user_default, $account)); |
$expected = db_fetch_array($result); |
| 36 |
watchdog("user", "new user: $user->name (webserver_auth)", l(t("edit user"), "admin/user/edit/$user->uid")); |
|
| 37 |
} |
if (isset($user) && $user->uid === $expected['uid']) { |
| 38 |
|
// Do nothing: user is already logged into Drupal with session data matching |
| 39 |
|
// HTTP authentication. |
| 40 |
|
} |
| 41 |
|
else { |
| 42 |
|
if (!empty($authname)) { |
| 43 |
|
// User is logged into webserver via HTTP authentication. |
| 44 |
|
// Try to log into Drupal. |
| 45 |
|
$user = user_external_load($authname); |
| 46 |
|
|
| 47 |
|
if (!$user) { |
| 48 |
|
// If unsuccessful, register the user. This will trigger |
| 49 |
|
// webserver_auth_user() and any other _user() hooks. |
| 50 |
|
user_external_login_register($authname, 'webserver_auth'); |
| 51 |
} |
} |
| 52 |
} |
} |
|
else { |
|
|
// do nothing. user isn't logged into web server |
|
|
} |
|
| 53 |
} |
} |
| 54 |
} |
} |
| 55 |
|
|
| 56 |
// using a global to change your bits. module_invoke_all miffs me. |
/** |
| 57 |
function webserver_auth_webserver_auth() { |
* Implementation of hook_user(). |
| 58 |
global $account; |
*/ |
| 59 |
|
function webserver_auth_user($op, &$edit, &$account, $category = NULL) { |
| 60 |
// pretties up the username for NTLM authentication (i.e. Windows) |
if ($op == 'submit' && $category = 'account') { |
| 61 |
if ($_SERVER["AUTH_TYPE"] == "NTLM" || $_SERVER["AUTH_TYPE"] == 'Negotiate') { |
// Only fiddle with new accounts. |
| 62 |
$account->name = substr(trim($account->name), strrpos(trim($account->name), "\\")+1); |
if (empty($account->uid)) { |
| 63 |
} |
$account->name = trim($account->name); |
| 64 |
|
// Pretty up the username for NTLM authentication (i.e. Windows) |
| 65 |
|
if (variable_get('webserver_auth_strip_prefix', TRUE)) { |
| 66 |
|
// Get 'bar' from 'foo1\foo2\bar' |
| 67 |
|
$account->name = array_pop(explode("\\", $account->name)); |
| 68 |
|
} |
| 69 |
|
if (variable_get('webserver_auth_strip_domain', TRUE)) { |
| 70 |
|
// Get 'foo' from 'foo@bar' |
| 71 |
|
$account->name = array_shift(explode('@', $account->name)); |
| 72 |
|
} |
| 73 |
|
|
| 74 |
if ($domain = variable_get("webserver_auth_domain", "")) { |
// Generate an e-mail address automagically |
| 75 |
if ($account->name) { |
if ($domain = variable_get('webserver_auth_email_domain', '')) { |
| 76 |
$account->mail = $account->name. "@$domain"; |
if ($account->name) { |
| 77 |
|
$account->mail = $account->name. '@'. $domain; |
| 78 |
|
} |
| 79 |
|
} |
| 80 |
|
// run some custom code to modify the user object at creation time |
| 81 |
|
if ($code = variable_get('webserver_auth_insert', '')) { |
| 82 |
|
eval('?>'. $code); |
| 83 |
|
} |
| 84 |
} |
} |
| 85 |
} |
} |
| 86 |
} |
elseif ($op == 'logout') { |
| 87 |
|
global $base_url; |
| 88 |
function webserver_auth_settings() { |
// kick user out of a secure session so they aren't automatically logged back in |
| 89 |
$output = form_textfield(t("Email Domain"), "webserver_auth_domain", variable_get("webserver_auth_domain", ""), 30, 55, t("Append this domain name to each new user in order generate his email address.")); |
$base_url = str_replace('https://', 'http://', $base_url); |
|
return $output; |
|
|
} |
|
|
|
|
|
function webserver_auth_help($section) { |
|
|
$output =""; |
|
|
|
|
|
switch ($section) { |
|
|
case 'admin/help#webserver_auth': |
|
|
break; |
|
|
case 'admin/modules#description': |
|
|
$output .= t("Use web server authentication instead of Drupal"); |
|
|
break; |
|
| 90 |
} |
} |
|
|
|
|
return $output; |
|
| 91 |
} |
} |
| 92 |
|
|
|
|
|
|
?> |
|
| 93 |
|
function webserver_auth_settings() { |
| 94 |
|
$form['webserver_auth_email_domain'] = array( |
| 95 |
|
'#type' => 'textfield', |
| 96 |
|
'#title' => t('Email domain'), |
| 97 |
|
'#default_value' => variable_get('webserver_auth_email_domain', ''), |
| 98 |
|
'#size' => 30, |
| 99 |
|
'#maxlength' => 55, |
| 100 |
|
'#description' => t('Append this domain name to each new user in order generate his email address.'), |
| 101 |
|
); |
| 102 |
|
$form['advanced'] = array( |
| 103 |
|
'#type' => 'fieldset', |
| 104 |
|
'#title' => t('Advanced settings'), |
| 105 |
|
'#collapsible' => TRUE, |
| 106 |
|
'#collapsed' => TRUE, |
| 107 |
|
'webserver_auth_strip_prefix' => array( |
| 108 |
|
'#type' => 'checkbox', |
| 109 |
|
'#title' => t('Strip prefix'), |
| 110 |
|
'#default_value' => variable_get('webserver_auth_strip_prefix', TRUE), |
| 111 |
|
'#description' => t("Strip NTLM-style prefixes (e.g. 'foo1\foo2') from the login name ('foo1\foo2\bar') to generate the username ('bar')."), |
| 112 |
|
), |
| 113 |
|
'webserver_auth_strip_domain' => array( |
| 114 |
|
'#type' => 'checkbox', |
| 115 |
|
'#title' => t('Strip domain'), |
| 116 |
|
'#default_value' => variable_get('webserver_auth_strip_domain', TRUE), |
| 117 |
|
'#description' => t("Strip a domain name (e.g. '@EXAMPLE.COM') from the login name ('newuser@EXAMPLE.COM') to generate the username ('newuser')."), |
| 118 |
|
), |
| 119 |
|
'webserver_auth_insert' => array( |
| 120 |
|
'#type' => 'textarea', |
| 121 |
|
'#title' => 'User account modification', |
| 122 |
|
'#default_value' => variable_get('webserver_auth_insert', ''), |
| 123 |
|
'#description' => t("Modify user accounts at the time of creation. Use PHP code (enclosed in <code><?php</code> and <code>?></code>). The variable <code>\$account</code> is available as in <a href=\"http://api.drupal.org/api/function/hook_user/6\">hook_user('submit',...)</a>. Changes to the \$account object will be automatically saved."), |
| 124 |
|
), |
| 125 |
|
); |
| 126 |
|
return system_settings_form($form); |
| 127 |
|
} |