3 * Manages PAM repository connection with pwauth
8 * @author Eric Seigne <eric.seigne@ryxeo.com>,
9 * Michael B. Trausch <mike@trausch.us>,
10 * Andrew McMillan <andrew@mcmillan.net.nz>
11 * @copyright Eric Seigne
12 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
14 * Based on drivers_squid_pam.php
17 require_once("auth-functions.php");
19 class pwauthPamDrivers
31 * @param string $config path where pwauth is
33 function __construct($config)
36 if(!file_exists($config)) {
37 $c->messages
[] = sprintf(i18n('drivers_pwauth_pam : Unable to find %s file'), $config);
46 * Check the username / password against the PAM system
48 function PWAUTH_PAM_check($username, $password) {
50 $program = $c->authenticate_hook
['config']['path'];
51 $email_base = $c->authenticate_hook
['config']['email_base'];
53 $pipe = popen(escapeshellarg($program), 'w');
54 $authinfo = sprintf("%s\n%s\n", $username, $password);
55 $written = fwrite($pipe, $authinfo);
56 dbg_error_log('pwauth', 'Bytes written: %d of %d', $written, strlen($authinfo));
57 $return_status = pclose($pipe);
59 switch($return_status) {
61 // STATUS_OK: Authentication succeeded.
62 dbg_error_log('pwauth', 'User %s successfully authenticated', $username);
63 $principal = new Principal('username',$username);
64 if ( !$principal->Exists() ) {
65 dbg_error_log('pwauth', 'User %s does not exist in local db, creating', $username);
66 $pwent = posix_getpwnam($username);
67 $gecos = explode(',',$pwent['gecos']);
68 $fullname = $gecos[0];
69 $principal->Create( array(
70 'username' => $username,
72 'email' => sprintf('%s@%s', $username, $email_base),
73 'fullname' => $fullname
75 if ( ! $principal->Exists() ) {
76 dbg_error_log( "PAM", "Unable to create local principal for '%s'", $username );
79 CreateHomeCalendar($username);
85 * Note that for system configurations using PAM instead of
86 * reading the password database directly, if PAM is unable to
87 * read the password database, pwauth will return status 1.
91 // (1) STATUS_UNKNOWN: Invalid username or password.
92 // (2) STATUS_INVALID: Invalid password.
93 dbg_error_log('pwauth', 'Invalid username or password (username: %s)', $username);
97 // STATUS_BLOCKED: UID for username is < pwauth's MIN_UNIX_UID
98 dbg_error_log('pwauth', 'UID for username %s is < pwauth MIN_UNIX_UID',
103 // STATUS_EXPIRED: The user account has expired.
104 dbg_error_log('pwauth', 'The account for %s has expired', $username);
108 // STATUS_PW_EXPIRED: The user account's password has expired.
109 dbg_error_log('pwauth', 'The account password for user %s has expired',
114 // STATUS_NOLOGIN: Logins to the system are administratively disabled.
115 dbg_error_log('pwauth', 'Logins administratively disabled (%s)', $username);
119 // STATUS_MANYFAILS: Too many login failures for user account.
120 dbg_error_log('pwauth', 'Login rejected for %s, too many failures',
125 // STATUS_INT_USER: Configuration error, Web server cannot use pwauth
126 dbg_error_log('pwauth', 'config error: see pwauth man page (%s)',
131 // STATUS_INT_ARGS: pwauth received no username/passwd to check
132 dbg_error_log('pwauth', 'error: pwauth received no username/password');
136 // STATUS_INT_ERR: unknown error
137 dbg_error_log('pwauth', 'error: see pwauth man page (%s)',
142 // STATUS_INT_NOROOT: pwauth could not read the password database
143 dbg_error_log('pwauth', 'config error: cannot read password database (%s)',
144 'STATUS_INT_NOROOT');
148 // Unknown error code.
149 dbg_error_log('pwauth', 'An unknown error (%d) has occurred',