document closed Debian bug
[davical.git] / inc / drivers_squid_pam.php
blob2cfab0e5125249fa5ded895f36b4079981ebac06
1 <?php
2 /**
3 * Manages PAM repository connection with SQUID help
5 * @package davical
6 * @category Technical
7 * @subpackage ldap
8 * @author Eric Seigne <eric.seigne@ryxeo.com>,
9 * Andrew McMillan <andrew@mcmillan.net.nz>
10 * @copyright Eric Seigne
11 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
14 require_once("auth-functions.php");
16 class squidPamDrivers
18 /**#@+
19 * @access private
22 /**#@-*/
25 /**
26 * The constructor
28 * @param string $config path where /usr/lib/squid/pam_auth is
30 function __construct($config) {
31 global $c;
32 if (! file_exists($config)){
33 $c->messages[] = sprintf(i18n( 'drivers_squid_pam : Unable to find %s file'), $config );
34 $this->valid=false;
35 return ;
41 /**
42 * Check the username / password against the PAM system
44 function SQUID_PAM_check($username, $password ){
45 global $c;
47 $script = $c->authenticate_hook['config']['script'];
48 if ( empty($script) ) $script = $c->authenticate_hook['config']['path'];
49 $cmd = sprintf( 'echo %s %s | %s -n common-auth', escapeshellarg($username), escapeshellarg($password),
50 $script);
51 $auth_result = exec($cmd);
52 if ( $auth_result == "OK") {
53 dbg_error_log('pwauth', 'User %s successfully authenticated', $username);
54 $principal = new Principal('username',$username);
55 if ( !$principal->Exists() ) {
56 dbg_error_log('pwauth', 'User %s does not exist in local db, creating', $username);
57 $pwent = posix_getpwnam($username);
58 $gecos = explode(',',$pwent['gecos']);
59 $fullname = $gecos[0];
60 $principal->Create( array(
61 'username' => $username,
62 'user_active' => 't',
63 'email' => sprintf('%s@%s', $username, $email_base),
64 'fullname' => $fullname
65 ));
66 if ( ! $principal->Exists() ) {
67 dbg_error_log( "PAM", "Unable to create local principal for '%s'", $username );
68 return false;
70 CreateHomeCalendar($username);
72 return $principal;
74 else {
75 dbg_error_log( "PAM", "User %s is not a valid username (or password was wrong)", $username );
76 return false;