<?php
/*
 * Copyright 1999 - 2002 by Gero Kohnert
 *
 * CVS Info:  $Id: auth_pam.pinc,v 1.16 2005/05/03 13:18:49 saraj Exp $
 * $Author: saraj $
 */

require_once 'auth.pinc';

#
# Globally diable PWLOST SUPPORT
#
$tutos[pwlostsupport] = 0;

/**
 * Authentification base class
 *
 * @modulegroup BASE
 * @module auth_pam
 * @package BASE
 */
class auth_pam extends auth{
  /**
   * get the password
   */
  function getpw() {
    if ( isset($_POST['pw']) ) {
      return $_POST['pw'];
    } elseif ( isset($_GET['pw']) ) {
      return $_GET['pw'];
    }
    return;
  }
  /**
   * get the username
   */
  function getuname() {
    if ( isset($_POST['uname']) ) {
      return trim($_POST['uname']);
    } elseif ( isset($_GET['uname']) ) {
      return trim($_GET['uname']);
    }
    return;
  }
  /**
   * Check for permmision
   * using a DB query checked against PAM (Pluggable Auth Module)
   */
  function check() {
    global $current_user,$lang,$tutos;

    $r = array();

    if ( ! function_exists('pam_auth')) {
      $r[0] = "PAM Auth not availabel";
      $r[1] = $r[0];
      return $r;
    }

    $query =
       "SELECT p.*, a.* , p.id as u_id FROM ". $this->dbconn->prefix ."people p, ". $this->dbconn->prefix ."addresses a ".
       "WHERE  login = ". $this->dbconn->String($this->getuname()).
       "       AND p.adr_id = a.id";

    $result = $this->dbconn->Exec($query);

    if ( ! $result ) {
      ReadLang($lang);
      $r[0] = "No Result for ". $this->getuname() ." / ". $this->getpw();
      $r[1] = $lang['Err0026'];
      return $r;
    }

    if ( 1 != $result->numrows()) {
      ReadLang($lang);
      $r[0]  = "not unique ";
      $r[0] .= $result->numrows() ." Result for ". $this->getuname() ." / ". $this->getpw();
      $r[1]  = $lang['Err0027'];
      return $r;
    }

    ini_set("pam_auth.servicename",$tutos[pamservice]);
    if ( ! pam_auth($this->getuname(), $this->getpw(), $error)) {
      ReadLang($lang);
      $r[0]  = $error ." : ". $this->getuname() ;
      $r[1]  = $lang['Err0027'] ." : ". $r[0];
      return $r;
    }

    $current_user= new tutos_user($this->dbconn);
    $current_user->read_result($result,0);
    $result->free();
  }
}
?>