15if (!function_exists(
'imap_open')) {
16 die(
"drivers_imap_pam: php-imap required.");
19require_once(
"auth-functions.php");
41 if (empty($imap_url)){
42 $c->messages[] = sprintf(i18n(
'drivers_imap_pam : imap_url parameter not configured in /etc/davical/*-conf.php'));
53function IMAP_PAM_check($username, $password ){
56 $imap_username = $username;
57 if ( function_exists(
'mb_convert_encoding') ) {
58 $imap_username = mb_convert_encoding($imap_username,
"UTF7-IMAP",mb_detect_encoding($imap_username));
61 $imap_username = imap_utf7_encode($imap_username);
66 $imap_url = $c->authenticate_hook[
'config'][
'imap_url'];
69 $imap_stream = @imap_open($imap_url, $imap_username, $password, OP_HALFOPEN);
73 imap_close($imap_stream);
78 if ( $auth_result ==
"OK") {
79 $principal =
new Principal(
'username',$username);
80 if ( ! $principal->Exists() ) {
81 dbg_error_log(
"PAM",
"Principal '%s' doesn't exist in local DB, we need to create it",$username );
82 $cmd =
"getent passwd '$username'";
83 $getent_res = exec($cmd);
84 $getent_arr = explode(
":", $getent_res);
85 $fullname = $getent_arr[4];
86 if(empty($fullname)) {
87 $fullname = $username;
91 @list($tmp_user, $tmp_domain) = explode(
'@', $username);
92 if( empty($tmp_domain) ) {
93 $email_address = $username .
"@" . $c->authenticate_hook[
'config'][
'email_base'];
96 $email_address = $username;
99 $principal->Create( array(
100 'username' => $username,
101 'user_active' =>
true,
102 'email' => $email_address,
103 'modified' => date(
'c'),
104 'fullname' => $fullname
106 if ( ! $principal->Exists() ) {
107 dbg_error_log(
"PAM",
"Unable to create local principal for '%s'", $username );
110 CreateHomeCollections($username);
111 CreateDefaultRelationships($username);
116 dbg_error_log(
"PAM",
"User %s is not a valid username (or password was wrong)", $username );