document closed Debian bug
[davical.git] / inc / drivers_imap_pam.php
blobf32606579e194c60bb7d459780be900306d17b23
1 <?php
2 /**
3 * Manages PAM repository connection with local imap server help
5 * @package davical
6 * @category Technical
7 * @subpackage ldap
8 * @author Oliver Schulze <oliver@samera.com.py>,
9 * Andrew McMillan <andrew@mcmillan.net.nz>
10 * @copyright Based on Eric Seigne script drivers_squid_pam.php
11 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
14 // The PHP interpreter will die quietly unless satisfied. This provides user feedback instead.
15 if (!function_exists('imap_open')) {
16 die("drivers_imap_pam: php5-imap required.");
19 require_once("auth-functions.php");
21 class imapPamDrivers
23 /**#@+
24 * @access private
27 /**#@-*/
30 /**
31 * The constructor
33 * @param string $imap_url formated for imap_open()
35 function __construct($imap_url)
37 global $c;
38 if (empty($imap_url)){
39 $c->messages[] = sprintf(i18n('drivers_imap_pam : imap_url parameter not configured in /etc/davical/*-conf.php'));
40 $this->valid=false;
41 return ;
47 /**
48 * Check the username / password against the PAM system
50 function IMAP_PAM_check($username, $password ){
51 global $c;
53 $imap_username = $username;
54 if ( function_exists('mb_convert_encoding') ) {
55 $imap_username = mb_convert_encoding($imap_username, "UTF7-IMAP",mb_detect_encoding($imap_username));
57 else {
58 $imap_username = imap_utf7_encode($imap_username);
61 //$imap_url = '{localhost:143/imap/notls}';
62 //$imap_url = '{localhost:993/imap/ssl/novalidate-cert}';
63 $imap_url = $c->authenticate_hook['config']['imap_url'];
64 $auth_result = "ERR";
66 $imap_stream = @imap_open($imap_url, $imap_username, $password, OP_HALFOPEN);
67 //print_r(imap_errors());
68 if ( $imap_stream ) {
69 // disconnect
70 imap_close($imap_stream);
71 // login ok
72 $auth_result = "OK";
75 if ( $auth_result == "OK") {
76 $principal = new Principal('username',$username);
77 if ( ! $principal->Exists() ) {
78 dbg_error_log( "PAM", "Principal '%s' doesn't exist in local DB, we need to create it",$username );
79 $cmd = "getent passwd '$username'";
80 $getent_res = exec($cmd);
81 $getent_arr = explode(":", $getent_res);
82 $fullname = $getent_arr[4];
83 if(empty($fullname)) {
84 $fullname = $username;
87 $principal->Create( array(
88 'username' => $username,
89 'user_active' => true,
90 'email' => $username . "@" . $c->authenticate_hook['config']['email_base'],
91 'modified' => date('c'),
92 'fullname' => $fullname
93 ));
94 if ( ! $principal->Exists() ) {
95 dbg_error_log( "PAM", "Unable to create local principal for '%s'", $username );
96 return false;
98 CreateHomeCalendar($username);
100 return $principal;
102 else {
103 dbg_error_log( "PAM", "User %s is not a valid username (or password was wrong)", $username );
104 return false;