Skip to content

Instantly share code, notes, and snippets.

@anjackson
Created August 20, 2014 10:00
Show Gist options
  • Save anjackson/7664b2acffbcc77a2905 to your computer and use it in GitHub Desktop.
Save anjackson/7664b2acffbcc77a2905 to your computer and use it in GitHub Desktop.
Script to bootstrap and authenticate against Drupal, for Apache mod_auth_external.
#!/usr/bin/php
<?php
# Globals to hold status:
$drupal_bootstrapped = FALSE;
$authenticated = FALSE;
# Use a shutdown function to ensure that authentication does not go through
# if bootstrapping Drupal fails (because PHP's die() returns exit(0)):
function shutdown() {
global $drupal_bootstrapped, $authenticated;
if( $drupal_bootstrapped == FALSE ) exit(1);
if( $authenticated == TRUE ) exit(0);
exit(1);
}
register_shutdown_function( 'shutdown' );
# Check this is a shell call, not a server call:
empty($_SERVER['SHELL']) && die('shells only please');
# Add some mock variables to make Drupal happy:
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
# Bootstrap Drupal:
define('DRUPAL_ROOT', '/var/www/html/act');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$drupal_bootstrapped = TRUE;
# Get the credentials from STDIN:
$username = trim(fgets(STDIN));
$password = trim(fgets(STDIN));
# Authenticate using Drupal:
$auth = user_authenticate($username, $password);
# Record the result in the global scope:
if($auth) {
$authenticated = TRUE;
}
# Exit, passing control to the shutdown function, using code 0 as 1 apparently cannot be overridden on shutdown:
exit(0);
?>
@nbrys
Copy link

nbrys commented Jan 15, 2015

Could you please confirm this is still working with drupal 7.32? We keep getting password mismatch error logs and our users can't log in using this system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment