1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121:
<?php
if (!function_exists('imap_open')) {
die("drivers_imap_pam: php-imap required.");
}
require_once("auth-functions.php");
class imapPamDriver
{
function __construct($imap_url)
{
global $c;
if (empty($imap_url)){
$c->messages[] = sprintf(i18n('drivers_imap_pam : imap_url parameter not configured in /etc/davical/*-conf.php'));
$this->valid=false;
return ;
}
}
}
function IMAP_PAM_check($username, $password ){
global $c;
$imap_username = $username;
if ( function_exists('mb_convert_encoding') ) {
$imap_username = mb_convert_encoding($imap_username, "UTF7-IMAP",mb_detect_encoding($imap_username));
}
else {
$imap_username = imap_utf7_encode($imap_username);
}
$imap_url = $c->authenticate_hook['config']['imap_url'];
$auth_result = "ERR";
$imap_stream = @imap_open($imap_url, $imap_username, $password, OP_HALFOPEN);
if ( $imap_stream ) {
imap_close($imap_stream);
$auth_result = "OK";
}
if ( $auth_result == "OK") {
$principal = new Principal('username',$username);
if ( ! $principal->Exists() ) {
dbg_error_log( "PAM", "Principal '%s' doesn't exist in local DB, we need to create it",$username );
$cmd = "getent passwd '$username'";
$getent_res = exec($cmd);
$getent_arr = explode(":", $getent_res);
$fullname = $getent_arr[4];
if(empty($fullname)) {
$fullname = $username;
}
@list($tmp_user, $tmp_domain) = explode('@', $username);
if( empty($tmp_domain) ) {
$email_address = $username . "@" . $c->authenticate_hook['config']['email_base'];
}
else {
$email_address = $username;
}
$principal->Create( array(
'username' => $username,
'user_active' => true,
'email' => $email_address,
'modified' => date('c'),
'fullname' => $fullname
));
if ( ! $principal->Exists() ) {
dbg_error_log( "PAM", "Unable to create local principal for '%s'", $username );
return false;
}
CreateHomeCollections($username, $c->default_timezone);
CreateDefaultRelationships($username);
}
return $principal;
}
else {
dbg_error_log( "PAM", "User %s is not a valid username (or password was wrong)", $username );
return false;
}
}