aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorm.fu <m_fu@310415.no-reply.drupal.org>2009-03-16 20:05:06 +0000
committerm.fu <m_fu@310415.no-reply.drupal.org>2009-03-16 20:05:06 +0000
commit4edb2a7d8b02df5ceda10fcbe30b2ad4cf9e4a12 (patch)
treea97e26676a72eaf17980992d31769886f063ddf2
downloaddrupal_htpasswdsync-4edb2a7d8b02df5ceda10fcbe30b2ad4cf9e4a12.tar.gz
drupal_htpasswdsync-4edb2a7d8b02df5ceda10fcbe30b2ad4cf9e4a12.tar.bz2
drupal_htpasswdsync-4edb2a7d8b02df5ceda10fcbe30b2ad4cf9e4a12.zip
Initial release, drupal 6.x
-rw-r--r--HTPasswdSync.info4
-rw-r--r--HTPasswdSync.install42
-rw-r--r--HTPasswdSync.module383
-rw-r--r--README.txt60
4 files changed, 489 insertions, 0 deletions
diff --git a/HTPasswdSync.info b/HTPasswdSync.info
new file mode 100644
index 0000000..9a239ef
--- /dev/null
+++ b/HTPasswdSync.info
@@ -0,0 +1,4 @@
+; $Id$
+name = HTPasswdSync
+description = This module will duplicate all users changes in the configured htpasswd file.
+core = 6.x \ No newline at end of file
diff --git a/HTPasswdSync.install b/HTPasswdSync.install
new file mode 100644
index 0000000..6689436
--- /dev/null
+++ b/HTPasswdSync.install
@@ -0,0 +1,42 @@
+<?php
+// $Id$
+
+/* @file
+ * installation declaration for htpasswdsync module
+ *
+ */
+
+
+function htpasswdsync_install() {
+ drupal_install_schema('htpasswdsync_db');
+}
+
+function htpasswdsync_uninstall() {
+ drupal_uninstall_schema('htpasswdsync_db');
+ variable_del('htpasswdsync_htpasswd');
+ variable_del('htpasswdsync_htgroup');
+ variable_del('htpasswdsync_roles');
+}
+
+function htpasswdsync_db_schema() {
+ $schema['htpasswdsync_passwd'] = array(
+ 'fields' => array(
+ 'username' => array(
+ 'description' => 'The {users}.username.',
+ 'type' => 'varchar',
+ 'length' => 33,
+ 'not null' => TRUE,
+ 'default' => 0,
+ ),
+ 'passwd' => array(
+ 'description' => 'The crypted (crypt) password.',
+ 'type' => 'varchar',
+ 'length' => 33,
+ 'not null' => TRUE,
+ 'default' => '',
+ ),
+ ),
+ 'primary key' => array('username'),
+ );
+ return $schema;
+}
diff --git a/HTPasswdSync.module b/HTPasswdSync.module
new file mode 100644
index 0000000..2e1ed9d
--- /dev/null
+++ b/HTPasswdSync.module
@@ -0,0 +1,383 @@
+<?php
+// $Id$
+
+/* @file
+ * Synchronize users password and htpasswd file
+ *
+ * HTPasswd Sync module duplicates all users changes in the configured htpasswd
+ * file. Password a hashed with the crypt function. The htgroup file contains
+ * roles as groups.
+ */
+
+ /**
+ * return htpasswd file name
+ *
+ * @return
+ * full filname of the htpasswd
+ */
+ function _htpasswdsync_passfilename() {
+ return variable_get('htpasswdsync_htpasswd', "/etc/httpd/htpasswd");
+ }
+
+ /**
+ * return htgroup file name
+ *
+ * @return
+ * full filname of the htgroup file
+ */
+ function _htpasswdsync_grpfilename() {
+ return variable_get('htpasswdsync_htgroup', "/etc/httpd/htgroup");
+ }
+
+ /**
+ * return role to put in htgroup
+ *
+ * return the roles selectionned in the admin page to put into
+ * the htgroup file.
+ *
+ * @return
+ * array of role id
+ */
+ function _htpasswdsync_roles() {
+ return variable_get('htpasswdsync_roles', array());
+ }
+
+/**
+ * read an htfile
+ *
+ * read an htfile, format is key: value, one key per row
+ *
+ * @param &$arr
+ * array to read to
+ * @param $file
+ * file to read
+ * @return
+ * none
+ */
+
+ function _htpasswdsync_read_htfile(&$arr, $file) {
+ $f = fopen($file, "r");
+ while ($l = fgets($f)) {
+ // _htpasswdsync_log("###########" . $l . "\n");
+ list($u, $p) = split(":", rtrim($l), 2);
+ // _htpasswdsync_log("###########" . $u . "__" . $p . "\n");
+ if ($u != "") {
+ $arr[$u] = $p;
+ }
+ }
+ // _htpasswdsync_log("rrr\n" . print_r($arr, true) . "rrr\n");
+ fclose($f);
+ }
+
+ /**
+ * write an htfile
+ *
+ * write an array to an htfile, format is key: value, one key per row
+ *
+ * @param &$arr
+ * array to write
+ * @param $file
+ * file to write to
+ * @return
+ * none
+ */
+ function _htpasswdsync_write_htfile(&$arr, $file) {
+ $f = fopen($file, "w");
+ foreach ($arr as $u => $p) {
+ if ($u != "" && $p != "") {
+ fputs($f, $u .":". $p ."\n");
+ }
+ }
+ fclose($f);
+ }
+
+/* generate the htgroup content
+ *
+ * for each role configured (htpasswdsync_roles array), list users
+ * and update the htgroup accordingly
+ *
+ * @param
+ * @return
+ */
+ function _htpasswdsync_updategroup() {
+
+ $file = _htpasswdsync_grpfilename();
+ $groups = array();
+ _htpasswdsync_read_htfile($groups, $file);
+
+ foreach (_htpasswdsync_roles() as $rid) {
+ // get role name
+ $res = db_fetch_object(db_query('SELECT name FROM {role} WHERE rid = %d', $rid));
+ $name = $res->name;
+
+ //empty group
+ $groups[$name] = "";
+
+ // add members to the group
+ $res = db_query('SELECT name FROM {users} u, {users_roles} ur WHERE ur.rid = %d AND ur.uid = u.uid', $rid);
+ while ($r = db_fetch_object($res)) {
+ $groups[$name] .= $r->name .", ";
+ }
+
+ // get rid of tailing characters
+ $groups[$name] = rtrim($groups[$name], ", ");
+ }
+ _htpasswdsync_write_htfile($groups, $file);
+ }
+
+/* generate the htpasswd content from the database
+ *
+ * update the htpasswd file from the table htpasswdsync_passwd
+ *
+ * @param
+ * @return
+ */
+ function _htpasswdsync_updatepasswd() {
+
+ $file = _htpasswdsync_passfilename();
+ $passwords = array();
+ _htpasswdsync_read_htfile($passwords, $file);
+
+ //get all users
+ $res = db_query('SELECT username, passwd FROM {htpasswdsync_passwd}');
+ while ($r = db_fetch_object($res)) {
+ if ($r->passwd == "****DELETED") {
+ unset($passwords[$r->username]);
+ }
+ else {
+ $passwords[$r->username] = $r->passwd;
+ }
+ }
+ _htpasswdsync_write_htfile($passwords, $file);
+ }
+
+
+/* update htpassword with the new password of the user
+ *
+ * @param $account
+ * account of the user to update
+ * @return
+ */
+ function _htpasswdsync_update($account) {
+
+ // read current file
+ $f = _htpasswdsync_passfilename();
+ $passwds = array();
+ _htpasswdsync_read_htfile($passwds, $f);
+
+ // update with the $account information received
+ // password crypted with the standard crypt (not MD5) function
+ $user = $account['name'];
+ $pass = crypt($account['pass'], chr(rand(65, 122)) . chr(rand(65, 122)));
+ $passwds[$user] = $pass;
+
+ //save file
+ _htpasswdsync_write_htfile($passwds, $f);
+
+ //update table
+ db_query("DELETE FROM {htpasswdsync_passwd} WHERE username = '%s'", $user);
+ db_query("INSERT INTO {htpasswdsync_passwd} (username, passwd) VALUES('%s', '%s')", $user, $pass);
+}
+
+/* remove the user for the htpassword file
+ *
+ * @param $account
+ * array of account to delete
+ * @return
+ */
+function _htpasswdsync_delete($account) {
+ $f = _htpasswdsync_passfilename();
+ $passwds = array();
+ _htpasswdsync_read_htfile($passwds, $f);
+
+ foreach ($account['accounts'] as $a) {
+ $r = db_query("SELECT name FROM {users} WHERE uid = %d", $a);
+ $user = db_fetch_object($r);
+ unset($passwds[$user->name]);
+ db_query("DELETE FROM {htpasswdsync_passwd} WHERE username = '%s'", $user->name);
+ db_query("INSERT INTO {htpasswdsync_passwd} (username, passwd) VALUES('%s', '%s')", $user->name, "****DELETED");
+ }
+ _htpasswdsync_write_htfile($passwds, $f);
+
+ _htpasswdsync_updategroup();
+ }
+
+ /**
+* Display help and module information
+* @param path which path of the site we're displaying help
+* @param arg array that holds the current path as would be returned from arg() function
+* @return help text for the path
+*/
+function htpasswdsync_help($path, $arg) {
+ $output = ''; //declare your output variable
+ switch ($path) {
+ case "admin/help#htpasswdsync":
+ $output = '<p>'. t("synchronize password with a htpasswd file") .'</p>';
+ break;
+ }
+ return $output;
+} // function htpasswdsync_help
+
+/**
+* Valid permissions for this module
+* @return array An array of valid permissions for the htpasswdsync module
+*/
+function htpasswdsync_perm() {
+ return array('administer htpasswdsync');
+} // function htpasswdsync_perm()
+
+/*
+ * Implementation of hook_user()
+ */
+function htpasswdsync_user($op, &$edit, &$account, $category = NULL) {
+ switch ($op) {
+ case "delete":
+ _htpasswdsync_delete($edit);
+ break;
+ case "insert":
+ _htpasswdsync_update($edit);
+ break;
+ case "update":
+ _htpasswdsync_update($edit);
+ break;
+ }
+} // function htpasswdsync_user()
+
+
+function htpasswdsync_admin() {
+
+ $form['htpasswdsync_htpasswd'] = array(
+ '#type' => 'textfield',
+ '#title' => t('htpasswd file'),
+ '#default_value' => _htpasswdsync_passfilename(),
+ '#size' => 100,
+ '#maxlength' => 200,
+ '#description' => t("Where is stored the !file file we want to synchronize with.",
+ array('!file' => 'htpasswd')),
+ '#required' => TRUE,
+ );
+ $form['htpasswdsync_htgroup'] = array(
+ '#type' => 'textfield',
+ '#title' => t('htgroup file'),
+ '#default_value' => _htpasswdsync_grpfilename(),
+ '#size' => 100,
+ '#maxlength' => 200,
+ '#description' => t("Where is stored the !file file we want to synchronize with.",
+ array('!file' => 'htgroup')),
+ '#required' => TRUE,
+ );
+ $form['htpasswdsync_roles'] = array(
+ '#type' => 'checkboxes',
+ '#title' => t('Roles to be exported in htgroup'),
+ '#default_value' => _htpasswdsync_roles(),
+ '#options' => user_roles(TRUE),
+ );
+
+ return system_settings_form($form);
+}
+
+
+/* Test if a file exist and is writable
+ *
+ * @param $filename
+ * name of the file
+ * @return
+ * return false on success, otherwise an error message
+ */
+function _htpasswdsync_check_file($file) {
+ $f = @fopen($file, "r");
+ if (!$f) {
+ return t("File '!file' does not exists, you should create it.",
+ array('!file' => $file));
+ }
+ else {
+ fclose($f);
+ $f = @fopen($file, "a");
+ if (!$f) {
+ return t("cannot write to file '!file'.", array('!file' => $file));
+ }
+ else {
+ fclose($f);
+ }
+ }
+ return FALSE;
+}
+
+function htpasswdsync_admin_validate($form, &$form_state) {
+
+ $file = $form_state['values']['htpasswdsync_htpasswd'];
+ if ($msg = _htpasswdsync_check_file($file)) {
+ form_set_error('htpasswdsync_htpasswd', $msg);
+ }
+
+ $file = $form_state['values']['htpasswdsync_htgroup'];
+ if ($msg = _htpasswdsync_check_file($file)) {
+ form_set_error('htpasswdsync_htpasswd', $msg);
+ }
+}
+
+function htpasswdsync_menu() {
+
+ $items = array();
+
+ $items['admin/user/htpasswdsync'] = array(
+ 'title' => 'HTPassword Sync',
+ 'description' => 'Preferences for the HTPasswd Sync module',
+ 'page callback' => 'drupal_get_form',
+ 'page arguments' => array('htpasswdsync_admin'),
+ 'access arguments' => array('access administration pages'),
+ 'type' => MENU_NORMAL_ITEM,
+ );
+
+ return $items;
+}
+
+function htpasswdsync_requirements($phase) {
+
+ $requirements = array();
+
+ // if not runtime query, exit
+ if ($phase != 'runtime') {
+ return $requirements;
+ }
+
+ $htpasswd_status_msg = _htpasswdsync_check_file(_htpasswdsync_passfilename());
+ $htpasswd_status_val = REQUIREMENT_ERROR;
+ if (!$htpasswd_status_msg) {
+ $htpasswd_status_msg = t('!file file is available', array('!file' => 'htpasswd'));
+ $htpasswd_status_val = REQUIREMENT_OK;
+ }
+
+ $htgroup_status_msg = _htpasswdsync_check_file(_htpasswdsync_grpfilename());
+ $htgroup_status_val = REQUIREMENT_ERROR;
+ if (!$htgroup_status_msg) {
+ $htgroup_status_msg = t('!file file is available', array('!file' => 'htgroup'));
+ $htgroup_status_val = REQUIREMENT_OK;
+ }
+
+ $status = $htpasswd_status_val > $htgroup_status_val ?
+ $htpasswd_status_val : $htgroup_status_val;
+
+ $requirements['htpasswdsync_status'] = array(
+ 'title' => t('HTPasswd Sync Status'),
+ 'value' => $htpasswd_status_msg ."<br>".
+ $htgroup_status_msg ."<br>".
+ t('last update !time ago.', array(
+ '!time' => format_interval(time()-variable_get('htpasswdsync_cron_time', 0)))),
+ 'severity' => $status,
+ );
+ if ($status != REQUIREMENT_OK) {
+ $requirements['htpasswdsync_status']['description'] =
+ t('Cannot access files, please <a href="!url">check configuration</a>.',
+ array('!url' => url('admin/user/htpasswdsync')));
+ }
+
+ return $requirements;
+}
+
+function htpasswdsync_cron() {
+ $time = variable_get('htpasswdsync_cron_time', 0);
+ _htpasswdsync_updatepasswd();
+ _htpasswdsync_updategroup();
+ variable_set('htpasswdsync_cron_time', time());
+}
diff --git a/README.txt b/README.txt
new file mode 100644
index 0000000..9d99291
--- /dev/null
+++ b/README.txt
@@ -0,0 +1,60 @@
+/* $Id$ */
+
+-- SUMMARY --
+
+The HTPasswd Sync module let you synchronize a htpasswd and a
+htgroup file with the user database.
+
+For a full description of the module, visit the project page:
+ http://drupal.org/project/htpasswdsync
+
+To submit bug reports and feature suggestions, or to track changes:
+ http://drupal.org/project/issues/htpasswdsync
+
+
+-- REQUIREMENTS --
+
+The syncrhonization only happen on password change. Hence, this module shall be
+installed before any user creation.
+
+
+-- INSTALLATION --
+
+* Install as usual, see http://drupal.org/node/70151 for further information.
+
+
+-- CONFIGURATION --
+
+* Configure synchronized files in Administer >> User management >> HTPasswd Sync >>
+
+ htpasswdsync module:
+
+ - htpasswd file
+
+ The file that will contain users and password, password are crypted, using
+ the standard crypt function, with a random two charaters seed.
+
+ - htgroup file
+
+ The file that will synchronize the roles.
+
+ - roles
+
+ The roles you want to export in the htgroup file.
+
+
+-- CUSTOMIZATION --
+
+None.
+
+-- TROUBLESHOOTING --
+
+
+
+-- FAQ --
+
+
+-- CONTACT --
+
+Current maintainers:
+* Marc Furrer (m.fu) - http://drupal.org/user/310415

© 2014-2024 Faster IT GmbH | imprint | privacy policy