Verzeichnisstruktur phpBB-3.0.0


Veröffentlicht
12.12.2007

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

auth_ldap.php

Zuletzt modifiziert: 09.10.2024, 12:51 - Dateigröße: 8.92 KiB


 001  <?php
 002  /**
 003  *
 004  * LDAP auth plug-in for phpBB3
 005  *
 006  * Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
 007  *
 008  * @package login
 009  * @version $Id$
 010  * @copyright (c) 2005 phpBB Group
 011  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 012  *
 013  */
 014   
 015  /**
 016  * @ignore
 017  */
 018  if (!defined('IN_PHPBB'))
 019  {
 020      exit;
 021  }
 022   
 023  /**
 024  * Connect to ldap server
 025  * Only allow changing authentication to ldap if we can connect to the ldap server
 026  * Called in acp_board while setting authentication plugins
 027  */
 028  function init_ldap()
 029  {
 030      global $config, $user;
 031   
 032      if (!@extension_loaded('ldap'))
 033      {
 034          return $user->lang['LDAP_NO_LDAP_EXTENSION'];
 035      }
 036   
 037      $config['ldap_port'] = (int) $config['ldap_port'];
 038      if ($config['ldap_port'])
 039      {
 040          $ldap = @ldap_connect($config['ldap_server'], $config['ldap_port']);
 041      }
 042      else
 043      {
 044          $ldap = @ldap_connect($config['ldap_server']);
 045      }
 046   
 047      if (!$ldap)
 048      {
 049          return $user->lang['LDAP_NO_SERVER_CONNECTION'];
 050      }
 051   
 052      @ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
 053      @ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
 054   
 055      if ($config['ldap_user'] || $config['ldap_password'])
 056      {
 057          if (!@ldap_bind($ldap, htmlspecialchars_decode($config['ldap_user']), htmlspecialchars_decode($config['ldap_password'])))
 058          {
 059              return $user->lang['LDAP_INCORRECT_USER_PASSWORD'];
 060          }
 061      }
 062   
 063      // ldap_connect only checks whether the specified server is valid, so the connection might still fail
 064      $search = @ldap_search(
 065          $ldap,
 066          $config['ldap_base_dn'],
 067          ldap_user_filter($user->data['username']),
 068          (empty($config['ldap_email'])) ? array($config['ldap_uid']) : array($config['ldap_uid'], $config['ldap_email']),
 069          0,
 070          1
 071      );
 072   
 073      if ($search === false)
 074      {
 075          return $user->lang['LDAP_NO_SERVER_CONNECTION'];
 076      }
 077   
 078      $result = @ldap_get_entries($ldap, $search);
 079   
 080      @ldap_close($ldap);
 081   
 082   
 083      if (!is_array($result) || sizeof($result) < 2)
 084      {
 085          return sprintf($user->lang['LDAP_NO_IDENTITY'], $user->data['username']);
 086      }
 087   
 088      if (!empty($config['ldap_email']) && !isset($result[0][$config['ldap_email']]))
 089      {
 090          return $user->lang['LDAP_NO_EMAIL'];
 091      }
 092   
 093      return false;
 094  }
 095   
 096  /**
 097  * Login function
 098  */
 099  function login_ldap(&$username, &$password)
 100  {
 101      global $db, $config, $user;
 102   
 103      // do not allow empty password
 104      if (!$password)
 105      {
 106          return array(
 107              'status'    => LOGIN_BREAK,
 108              'error_msg'    => 'NO_PASSWORD_SUPPLIED',
 109          );
 110      }
 111   
 112      if (!@extension_loaded('ldap'))
 113      {
 114          return array(
 115              'status'        => LOGIN_ERROR_EXTERNAL_AUTH,
 116              'error_msg'        => 'LDAP_NO_LDAP_EXTENSION',
 117              'user_row'        => array('user_id' => ANONYMOUS),
 118          );
 119      }
 120   
 121      $config['ldap_port'] = (int) $config['ldap_port'];
 122      if ($config['ldap_port'])
 123      {
 124          $ldap = @ldap_connect($config['ldap_server'], $config['ldap_port']);
 125      }
 126      else
 127      {
 128          $ldap = @ldap_connect($config['ldap_server']);
 129      }
 130   
 131      if (!$ldap)
 132      {
 133          return array(
 134              'status'        => LOGIN_ERROR_EXTERNAL_AUTH,
 135              'error_msg'        => 'LDAP_NO_SERVER_CONNECTION',
 136              'user_row'        => array('user_id' => ANONYMOUS),
 137          );
 138      }
 139   
 140      @ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
 141      @ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
 142   
 143      if ($config['ldap_user'] || $config['ldap_password'])
 144      {
 145          if (!@ldap_bind($ldap, $config['ldap_user'], htmlspecialchars_decode($config['ldap_password'])))
 146          {
 147              return $user->lang['LDAP_NO_SERVER_CONNECTION'];
 148          }
 149      }
 150   
 151      $search = @ldap_search(
 152          $ldap,
 153          $config['ldap_base_dn'],
 154          ldap_user_filter($username),
 155          (empty($config['ldap_email'])) ? array($config['ldap_uid']) : array($config['ldap_uid'], $config['ldap_email']),
 156          0,
 157          1
 158      );
 159   
 160      $ldap_result = @ldap_get_entries($ldap, $search);
 161   
 162      if (is_array($ldap_result) && sizeof($ldap_result) > 1)
 163      {
 164          if (@ldap_bind($ldap, $ldap_result[0]['dn'], htmlspecialchars_decode($password)))
 165          {
 166              @ldap_close($ldap);
 167   
 168              $sql ='SELECT user_id, username, user_password, user_passchg, user_email, user_type
 169                  FROM ' . USERS_TABLE . "
 170                  WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
 171              $result = $db->sql_query($sql);
 172              $row = $db->sql_fetchrow($result);
 173              $db->sql_freeresult($result);
 174   
 175              if ($row)
 176              {
 177                  unset($ldap_result);
 178   
 179                  // User inactive...
 180                  if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE)
 181                  {
 182                      return array(
 183                          'status'        => LOGIN_ERROR_ACTIVE,
 184                          'error_msg'        => 'ACTIVE_ERROR',
 185                          'user_row'        => $row,
 186                      );
 187                  }
 188   
 189                  // Successful login... set user_login_attempts to zero...
 190                  return array(
 191                      'status'        => LOGIN_SUCCESS,
 192                      'error_msg'        => false,
 193                      'user_row'        => $row,
 194                  );
 195              }
 196              else
 197              {
 198                  // retrieve default group id
 199                  $sql = 'SELECT group_id
 200                      FROM ' . GROUPS_TABLE . "
 201                      WHERE group_name = '" . $db->sql_escape('REGISTERED') . "'
 202                          AND group_type = " . GROUP_SPECIAL;
 203                  $result = $db->sql_query($sql);
 204                  $row = $db->sql_fetchrow($result);
 205                  $db->sql_freeresult($result);
 206   
 207                  if (!$row)
 208                  {
 209                      trigger_error('NO_GROUP');
 210                  }
 211   
 212                  // generate user account data
 213                  $ldap_user_row = array(
 214                      'username'        => $username,
 215                      'user_password'    => phpbb_hash($password),
 216                      'user_email'    => (!empty($config['ldap_email'])) ? $ldap_result[0][$config['ldap_email']][0] : '',
 217                      'group_id'        => (int) $row['group_id'],
 218                      'user_type'        => USER_NORMAL,
 219                      'user_ip'        => $user->ip,
 220                  );
 221   
 222                  unset($ldap_result);
 223   
 224                  // this is the user's first login so create an empty profile
 225                  return array(
 226                      'status'        => LOGIN_SUCCESS_CREATE_PROFILE,
 227                      'error_msg'        => false,
 228                      'user_row'        => $ldap_user_row,
 229                  );
 230              }
 231          }
 232          else
 233          {
 234              unset($ldap_result);
 235              @ldap_close($ldap);
 236   
 237              // Give status about wrong password...
 238              return array(
 239                  'status'        => LOGIN_ERROR_PASSWORD,
 240                  'error_msg'        => 'LOGIN_ERROR_PASSWORD',
 241                  'user_row'        => array('user_id' => ANONYMOUS),
 242              );
 243          }
 244      }
 245   
 246      @ldap_close($ldap);
 247   
 248      return array(
 249          'status'    => LOGIN_ERROR_USERNAME,
 250          'error_msg'    => 'LOGIN_ERROR_USERNAME',
 251          'user_row'    => array('user_id' => ANONYMOUS),
 252      );
 253  }
 254   
 255  /**
 256  * Generates a filter string for ldap_search to find a user
 257  *
 258  * @param    $username    string    Username identifying the searched user
 259  *
 260  * @return                string    A filter string for ldap_search
 261  */
 262  function ldap_user_filter($username)
 263  {
 264      global $config;
 265   
 266      $filter = '(' . $config['ldap_uid'] . '=' . ldap_escape(htmlspecialchars_decode($username)) . ')';
 267      if ($config['ldap_user_filter'])
 268      {
 269          $filter = "(&$filter({$config['ldap_user_filter']}))";
 270      }
 271      return $filter;
 272  }
 273   
 274  /**
 275  * Escapes an LDAP AttributeValue
 276  */
 277  function ldap_escape($string)
 278  {
 279      return str_replace(array('*', '\\', '(', ')'), array('\\*', '\\\\', '\\(', '\\)'), $string);
 280  }
 281   
 282  /**
 283  * This function is used to output any required fields in the authentication
 284  * admin panel. It also defines any required configuration table fields.
 285  */
 286  function acp_ldap(&$new)
 287  {
 288      global $user;
 289   
 290      $tpl = '
 291   
 292      <dl>
 293          <dt><label for="ldap_server">' . $user->lang['LDAP_SERVER'] . ':</label><br /><span>' . $user->lang['LDAP_SERVER_EXPLAIN'] . '</span></dt>
 294          <dd><input type="text" id="ldap_server" size="40" name="config[ldap_server]" value="' . $new['ldap_server'] . '" /></dd>
 295      </dl>
 296      <dl>
 297          <dt><label for="ldap_port">' . $user->lang['LDAP_PORT'] . ':</label><br /><span>' . $user->lang['LDAP_PORT_EXPLAIN'] . '</span></dt>
 298          <dd><input type="text" id="ldap_port" size="40" name="config[ldap_port]" value="' . $new['ldap_port'] . '" /></dd>
 299      </dl>
 300      <dl>
 301          <dt><label for="ldap_dn">' . $user->lang['LDAP_DN'] . ':</label><br /><span>' . $user->lang['LDAP_DN_EXPLAIN'] . '</span></dt>
 302          <dd><input type="text" id="ldap_dn" size="40" name="config[ldap_base_dn]" value="' . $new['ldap_base_dn'] . '" /></dd>
 303      </dl>
 304      <dl>
 305          <dt><label for="ldap_uid">' . $user->lang['LDAP_UID'] . ':</label><br /><span>' . $user->lang['LDAP_UID_EXPLAIN'] . '</span></dt>
 306          <dd><input type="text" id="ldap_uid" size="40" name="config[ldap_uid]" value="' . $new['ldap_uid'] . '" /></dd>
 307      </dl>
 308      <dl>
 309          <dt><label for="ldap_user_filter">' . $user->lang['LDAP_USER_FILTER'] . ':</label><br /><span>' . $user->lang['LDAP_USER_FILTER_EXPLAIN'] . '</span></dt>
 310          <dd><input type="text" id="ldap_user_filter" size="40" name="config[ldap_user_filter]" value="' . $new['ldap_user_filter'] . '" /></dd>
 311      </dl>
 312      <dl>
 313          <dt><label for="ldap_email">' . $user->lang['LDAP_EMAIL'] . ':</label><br /><span>' . $user->lang['LDAP_EMAIL_EXPLAIN'] . '</span></dt>
 314          <dd><input type="text" id="ldap_email" size="40" name="config[ldap_email]" value="' . $new['ldap_email'] . '" /></dd>
 315      </dl>
 316      <dl>
 317          <dt><label for="ldap_user">' . $user->lang['LDAP_USER'] . ':</label><br /><span>' . $user->lang['LDAP_USER_EXPLAIN'] . '</span></dt>
 318          <dd><input type="text" id="ldap_user" size="40" name="config[ldap_user]" value="' . $new['ldap_user'] . '" /></dd>
 319      </dl>
 320      <dl>
 321          <dt><label for="ldap_password">' . $user->lang['LDAP_PASSWORD'] . ':</label><br /><span>' . $user->lang['LDAP_PASSWORD_EXPLAIN'] . '</span></dt>
 322          <dd><input type="password" id="ldap_password" size="40" name="config[ldap_password]" value="' . $new['ldap_password'] . '" /></dd>
 323      </dl>
 324      ';
 325   
 326      // These are fields required in the config table
 327      return array(
 328          'tpl'        => $tpl,
 329          'config'    => array('ldap_server', 'ldap_port', 'ldap_base_dn', 'ldap_uid', 'ldap_user_filter', 'ldap_email', 'ldap_user', 'ldap_password')
 330      );
 331  }
 332   
 333  ?>