More fixes to CalDAV Scheduling
[davical.git] / inc / drivers_imap_pam.php
blobc8b46dae7cb233b020aa1a2b0fc262e2d20790c8
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 require_once("auth-functions.php");
16 class imapPamDrivers
18 /**#@+
19 * @access private
22 /**#@-*/
25 /**
26 * Constructor.
27 * @param string $imap_url formated for imap_open()
29 function imapPamDrivers($imap_url){
30 $this->__construct($imap_url);
34 /**
35 * The constructor
37 * @param string $imap_url formated for imap_open()
39 function __construct($imap_url)
41 global $c;
42 if (empty($imap_url)){
43 $c->messages[] = sprintf(i18n('drivers_imap_pam : imap_url parameter not configured in /etc/davical/*-conf.php'));
44 $this->valid=false;
45 return ;
51 /**
52 * Check the username / password against the PAM system
54 function IMAP_PAM_check($username, $password ){
55 global $c;
57 $imap_username = $username;
58 if ( function_exists('mb_convert_encoding') ) {
59 $imap_username = mb_convert_encoding($imap_username, "UTF7-IMAP",mb_detect_encoding($imap_username));
61 else {
62 $imap_username = imap_utf7_encode($imap_username);
65 //$imap_url = '{localhost:143/imap/notls}';
66 //$imap_url = '{localhost:993/imap/ssl/novalidate-cert}';
67 $imap_url = $c->authenticate_hook['config']['imap_url'];
68 $auth_result = "ERR";
70 $imap_stream = @imap_open($imap_url, $imap_username, $password, OP_HALFOPEN);
71 //print_r(imap_errors());
72 if ( $imap_stream ) {
73 // disconnect
74 imap_close($imap_stream);
75 // login ok
76 $auth_result = "OK";
79 if ( $auth_result == "OK") {
80 $principal = new Principal('username',$username);
81 if ( ! $principal->Exists() ) {
82 dbg_error_log( "PAM", "Principal '%s' doesn't exist in local DB, we need to create it",$username );
83 $cmd = "getent passwd '$username'";
84 $getent_res = exec($cmd);
85 $getent_arr = explode(":", $getent_res);
86 $fullname = $getent_arr[4];
87 if(empty($fullname)) {
88 $fullname = $username;
91 $principal->Create( array(
92 'username' => $username,
93 'user_active' => true,
94 'email' => $username . "@" . $c->authenticate_hook['config']['email_base'],
95 'modified' => date(),
96 'fullname' => $fullname
97 ));
98 if ( ! $principal->Exists() ) {
99 dbg_error_log( "PAM", "Unable to create local principal for '%s'", $username );
100 return false;
102 CreateHomeCalendar($username);
104 return $principal;
106 else {
107 dbg_error_log( "PAM", "User %s is not a valid username (or password was wrong)", $username );
108 return false;