|
@@ -9,13 +9,6 @@ define('GENPASS_DISPLAY_ADMIN', 1);
|
|
define('GENPASS_DISPLAY_USER', 2);
|
|
define('GENPASS_DISPLAY_USER', 2);
|
|
define('GENPASS_DISPLAY_BOTH', 3);
|
|
define('GENPASS_DISPLAY_BOTH', 3);
|
|
|
|
|
|
-/**
|
|
|
|
- * Implements of hook_init().
|
|
|
|
- */
|
|
|
|
-function genpass_init() {
|
|
|
|
- drupal_add_css(drupal_get_path('module', 'genpass') . '/genpass.css');
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Defines default characters allowed for passwords.
|
|
* Defines default characters allowed for passwords.
|
|
*/
|
|
*/
|
|
@@ -33,27 +26,31 @@ function genpass_generate() {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Generate a new password using genpass's internal password generation
|
|
|
|
- * algorithm.
|
|
|
|
- * Based on the original D6 user_password function (with more characters)
|
|
|
|
|
|
+ * Generates random password.
|
|
*
|
|
*
|
|
- * @return a fresh password according to the settings made in /admin/user/settings
|
|
|
|
|
|
+ * @see user_password()
|
|
*
|
|
*
|
|
- * @see genpass_form_alter()
|
|
|
|
|
|
+ * @return string
|
|
|
|
+ * The random string.
|
|
*/
|
|
*/
|
|
function genpass_password() {
|
|
function genpass_password() {
|
|
$pass = '';
|
|
$pass = '';
|
|
- $length = variable_get('genpass_length', 8);
|
|
|
|
|
|
+ $length = variable_get('genpass_length', 12);
|
|
$allowable_characters = variable_get('genpass_entropy', _GENPASS_REQUIRED_entropy());
|
|
$allowable_characters = variable_get('genpass_entropy', _GENPASS_REQUIRED_entropy());
|
|
|
|
|
|
// Zero-based count of characters in the allowable list:
|
|
// Zero-based count of characters in the allowable list:
|
|
$len = strlen($allowable_characters) - 1;
|
|
$len = strlen($allowable_characters) - 1;
|
|
-
|
|
|
|
|
|
+
|
|
// Loop the number of times specified by $length.
|
|
// Loop the number of times specified by $length.
|
|
for ($i = 0; $i < $length; $i++) {
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
+ do {
|
|
|
|
+ // Find a secure random number within the range needed.
|
|
|
|
+ $index = ord(drupal_random_bytes(1));
|
|
|
|
+ } while ($index > $len);
|
|
|
|
+
|
|
// Each iteration, pick a random character from the
|
|
// Each iteration, pick a random character from the
|
|
// allowable string and append it to the password:
|
|
// allowable string and append it to the password:
|
|
- $pass .= $allowable_characters[mt_rand(0, $len)];
|
|
|
|
|
|
+ $pass .= $allowable_characters[$index];
|
|
}
|
|
}
|
|
|
|
|
|
return $pass;
|
|
return $pass;
|
|
@@ -63,7 +60,7 @@ function genpass_password() {
|
|
/**
|
|
/**
|
|
* Helper function to find a item in the user form, since its position
|
|
* Helper function to find a item in the user form, since its position
|
|
* within the form-array depends on the profile module (account-category).
|
|
* within the form-array depends on the profile module (account-category).
|
|
- */
|
|
|
|
|
|
+ */
|
|
function &_genpass_get_form_item(&$form, $field) {
|
|
function &_genpass_get_form_item(&$form, $field) {
|
|
if (isset($form['account'][$field])) {
|
|
if (isset($form['account'][$field])) {
|
|
return $form['account'][$field];
|
|
return $form['account'][$field];
|
|
@@ -97,7 +94,7 @@ function genpass_form_alter(&$form, $form_state, $form_id) {
|
|
$form['registration_cancellation']['genpass_length'] = array(
|
|
$form['registration_cancellation']['genpass_length'] = array(
|
|
'#type' => 'textfield',
|
|
'#type' => 'textfield',
|
|
'#title' => t('Generated password length'),
|
|
'#title' => t('Generated password length'),
|
|
- '#default_value' => variable_get('genpass_length', 8),
|
|
|
|
|
|
+ '#default_value' => variable_get('genpass_length', 12),
|
|
'#size' => 2,
|
|
'#size' => 2,
|
|
'#maxlength' => 2,
|
|
'#maxlength' => 2,
|
|
'#description' => t('Set the length of generated passwords here. Allowed range: 5 to 32.'),
|
|
'#description' => t('Set the length of generated passwords here. Allowed range: 5 to 32.'),
|
|
@@ -118,7 +115,7 @@ function genpass_form_alter(&$form, $form_state, $form_id) {
|
|
'#default_value' => genpass_algorithm_module(),
|
|
'#default_value' => genpass_algorithm_module(),
|
|
'#options' => genpass_add_samples(genpass_algorithm_modules()),
|
|
'#options' => genpass_add_samples(genpass_algorithm_modules()),
|
|
'#description' => t('If third party modules define a password generation algorithm, you can select which one to use. Note that algorithms other than genpass will ignore the preferred entropy and password length. The currently selected algorithm produced the password @pw.', array('@pw' => genpass_generate())),
|
|
'#description' => t('If third party modules define a password generation algorithm, you can select which one to use. Note that algorithms other than genpass will ignore the preferred entropy and password length. The currently selected algorithm produced the password @pw.', array('@pw' => genpass_generate())),
|
|
- );
|
|
|
|
|
|
+ );
|
|
$form['registration_cancellation']['genpass_display'] = array(
|
|
$form['registration_cancellation']['genpass_display'] = array(
|
|
'#type' => 'radios',
|
|
'#type' => 'radios',
|
|
'#title' => t('Generated password display'),
|
|
'#title' => t('Generated password display'),
|
|
@@ -139,25 +136,25 @@ function genpass_form_alter(&$form, $form_state, $form_id) {
|
|
// User registration form at admin/people/create
|
|
// User registration form at admin/people/create
|
|
case 'user_register_form':
|
|
case 'user_register_form':
|
|
$mode = variable_get('genpass_mode', GENPASS_REQUIRED);
|
|
$mode = variable_get('genpass_mode', GENPASS_REQUIRED);
|
|
-
|
|
|
|
|
|
+
|
|
// Add validation function, where password may get set
|
|
// Add validation function, where password may get set
|
|
$form['#validate'][] = 'genpass_register_validate';
|
|
$form['#validate'][] = 'genpass_register_validate';
|
|
-
|
|
|
|
|
|
+
|
|
// Administrator is creating the user
|
|
// Administrator is creating the user
|
|
- if ($_GET['q'] == 'admin/user/user/create') {
|
|
|
|
|
|
+ if ($_GET['q'] == 'admin/people/create') {
|
|
// Switch to optional mode
|
|
// Switch to optional mode
|
|
$mode = GENPASS_OPTIONAL;
|
|
$mode = GENPASS_OPTIONAL;
|
|
// Help avoid obvious consequence of password being optional
|
|
// Help avoid obvious consequence of password being optional
|
|
$notify_item =& _genpass_get_form_item($form, 'notify');
|
|
$notify_item =& _genpass_get_form_item($form, 'notify');
|
|
$notify_item['#description'] = t('This is recommended when auto-generating the password; otherwise, neither you nor the new user will know the password.');
|
|
$notify_item['#description'] = t('This is recommended when auto-generating the password; otherwise, neither you nor the new user will know the password.');
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
// Pass mode to validation function
|
|
// Pass mode to validation function
|
|
$form['genpass_mode'] = array(
|
|
$form['genpass_mode'] = array(
|
|
'#type' => 'value',
|
|
'#type' => 'value',
|
|
'#value' => $mode,
|
|
'#value' => $mode,
|
|
);
|
|
);
|
|
-
|
|
|
|
|
|
+
|
|
$pass_item =& _genpass_get_form_item($form, 'pass');
|
|
$pass_item =& _genpass_get_form_item($form, 'pass');
|
|
switch ($mode) {
|
|
switch ($mode) {
|
|
// If password is optional, don't require it, and give the user an
|
|
// If password is optional, don't require it, and give the user an
|
|
@@ -174,7 +171,7 @@ function genpass_form_alter(&$form, $form_state, $form_id) {
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -200,38 +197,44 @@ function genpass_user_admin_settings_validate($form, &$form_state) {
|
|
* User registration validation.
|
|
* User registration validation.
|
|
*/
|
|
*/
|
|
function genpass_register_validate($form, &$form_state) {
|
|
function genpass_register_validate($form, &$form_state) {
|
|
- if (empty($form_state['values']['pass']) && !form_get_errors()) {
|
|
|
|
-
|
|
|
|
- // Generate and set password
|
|
|
|
|
|
+ if (empty($form_state['values']['pass'])) {
|
|
|
|
+ // Generate and set password.
|
|
$pass = genpass_generate();
|
|
$pass = genpass_generate();
|
|
$pass_item =& _genpass_get_form_item($form, 'pass');
|
|
$pass_item =& _genpass_get_form_item($form, 'pass');
|
|
form_set_value($pass_item, $pass, $form_state);
|
|
form_set_value($pass_item, $pass, $form_state);
|
|
|
|
|
|
- $display = variable_get('genpass_display', GENPASS_DISPLAY_BOTH);
|
|
|
|
|
|
+ if (!form_get_errors()) {
|
|
|
|
+ $display = variable_get('genpass_display', GENPASS_DISPLAY_BOTH);
|
|
|
|
+
|
|
|
|
+ // Administrator created the user.
|
|
|
|
+ if ($_GET['q'] == 'admin/people/create') {
|
|
|
|
+ $message = t('Since you did not provide a password, it was generated automatically for this account.');
|
|
|
|
|
|
- // Administrator created the user.
|
|
|
|
- if ($_GET['q'] == 'admin/people/create') {
|
|
|
|
- $message = t('Since you did not provide a password, it was generated automatically for this account.');
|
|
|
|
- if (in_array($display, array(GENPASS_DISPLAY_ADMIN, GENPASS_DISPLAY_BOTH))) {
|
|
|
|
- $message .= ' ' . t('The password is: <strong class="genpass-password">!password</strong>', array('!password' => $pass));
|
|
|
|
|
|
+ if (in_array($display, array(GENPASS_DISPLAY_ADMIN, GENPASS_DISPLAY_BOTH))) {
|
|
|
|
+ $message .= ' ' . t('The password is: <strong class="nowrap">@password</strong>', array('@password' => $pass));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }
|
|
|
|
- // Optional - User did not provide password, so it was generated
|
|
|
|
- elseif ($form_state['values']['genpass_mode'] == GENPASS_OPTIONAL) {
|
|
|
|
- $message = t('Since you did not provide a password, it was generated for you.');
|
|
|
|
- if (in_array($display, array(GENPASS_DISPLAY_USER, GENPASS_DISPLAY_BOTH))) {
|
|
|
|
- $message .= ' ' . t('Your password is: <strong class="genpass-password">!password</strong>', array('!password' => $pass));
|
|
|
|
|
|
+
|
|
|
|
+ // Optional - User did not provide password, so it was generated
|
|
|
|
+ elseif ($form_state['values']['genpass_mode'] == GENPASS_OPTIONAL) {
|
|
|
|
+ $message = t('Since you did not provide a password, it was generated for you.');
|
|
|
|
+
|
|
|
|
+ if (in_array($display, array(GENPASS_DISPLAY_USER, GENPASS_DISPLAY_BOTH))) {
|
|
|
|
+ $message .= ' ' . t('Your password is: <strong class="nowrap">@password</strong>', array('@password' => $pass));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Restricted - User was forced to receive a generated password
|
|
|
|
+ elseif ($form_state['values']['genpass_mode'] == GENPASS_RESTRICTED && in_array($display, array(GENPASS_DISPLAY_USER, GENPASS_DISPLAY_BOTH))) {
|
|
|
|
+ $message = t('The following password was generated for you: <strong class="nowrap">@password</strong>', array('@password' => $pass));
|
|
}
|
|
}
|
|
- }
|
|
|
|
- // Restricted - User was forced to receive a generated password
|
|
|
|
- elseif ($form_state['values']['genpass_mode'] == GENPASS_RESTRICTED && in_array($display, array(GENPASS_DISPLAY_USER, GENPASS_DISPLAY_BOTH))) {
|
|
|
|
- $message = t('The following password was generated for you: <strong class="genpass-password">!password</strong>', array('!password' => $pass));
|
|
|
|
- }
|
|
|
|
|
|
|
|
- if (!empty($message)) {
|
|
|
|
- drupal_set_message($message);
|
|
|
|
|
|
+ if (!empty($message)) {
|
|
|
|
+ drupal_set_message($message);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
return $form;
|
|
return $form;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -261,7 +264,7 @@ function genpass_algorithm_modules() {
|
|
function genpass_algorithm_module() {
|
|
function genpass_algorithm_module() {
|
|
$modules = genpass_algorithm_modules();
|
|
$modules = genpass_algorithm_modules();
|
|
$module = variable_get('genpass_algorithm', 'genpass');
|
|
$module = variable_get('genpass_algorithm', 'genpass');
|
|
-
|
|
|
|
|
|
+
|
|
if (in_array($module, array_keys($modules))) {
|
|
if (in_array($module, array_keys($modules))) {
|
|
return $module;
|
|
return $module;
|
|
}
|
|
}
|