summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.15/sites/all/modules/ldap/ldap_sso/ldap_sso.module
blob: 784daa941aabf9f22e82e85ff3c5a21a4504174e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php

/**
 * @file
 * This module injects itself into Drupal's Authentication stack.
 */

/**
 * Implements hook_menu().
 */
function ldap_sso_menu() {
  $items = array();

  $items['user/login/sso'] = array(
    'title' => 'Log In',
    'page callback' => 'ldap_sso_user_login_sso',
    'access callback' => '_ldap_authentication_user_access',
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}


/**
 * Implements hook_user_logout().
 *
 * The user just logged out.
 *
 */

function ldap_sso_user_logout($account) {
  $auth_conf = ldap_authentication_get_valid_conf();
  if ($auth_conf->seamlessLogin == 1) {
    setcookie("seamless_login", 'do not auto login', time() + (int)$auth_conf->cookieExpire, base_path(), "");
    $_SESSION['seamless_login'] = $_COOKIE['seamless_login'];
  }
}

/**
 * Implements hook_boot().
 *  Perform setup tasks. This entry point is used because hook_user_load no
 *  longer runs on anonymous users, and hook_boot is guaranteed to run,
 *  regardless of cache
 */
function ldap_sso_boot() {
  if (!drupal_is_cli() && $GLOBALS['user']->uid == 0) {
    if (!(isset($_COOKIE['seamless_login'])) || $_COOKIE['seamless_login'] == 'auto login') {
      if ((arg(0) == 'user' && !(is_numeric(arg(1)))) || arg(0) == 'logout' ) {
        return;
      }
      else {
        if (isset($_COOKIE['seamless_login_attempted']))
          $login_attempted = $_COOKIE['seamless_login_attempted'];
        else {
          $login_attempted = FALSE;
        }
        require_once(DRUPAL_ROOT . '/includes/common.inc');
        require_once(DRUPAL_ROOT . '/includes/path.inc');
        $ldap_authentication_conf = variable_get('ldap_authentication_conf', array());
        if (isset($ldap_authentication_conf['seamlessLogin']) && $ldap_authentication_conf['seamlessLogin'] == 1 && ($login_attempted != 'true')) {
          setcookie("seamless_login_attempted", 'true', time() + (int)$ldap_authentication_conf['cookieExpire'], base_path(), "");
          $_SESSION['seamless_login_attempted'] = $login_attempted;
          // removed with http://drupal.org/node/1485118 patch
          //$ldap_sso_q = (!isset($_GET['q']) || $_GET['q'] == '') ? 'user' : $_GET['q'];
          //drupal_goto('user/login/sso', array('query' => array('destination' => rawurlencode($ldap_sso_q))));
          drupal_goto('user/login/sso', array('query' => drupal_get_destination()));
        }
        else {
          return;
        }
      }
    }
  }
}

/**
 * A proxy function for the actual authentication routine. This is in place
 * so various implementations of grabbing NTLM credentials can be used and
 * selected from an administration page. This is the real gatekeeper since
 * this assumes that any NTLM authentication from the underlying web server
 * is good enough, and only checks that there are values in place for the
 * user name, and anything else that is set for a particular implementation. In
 * the case that there is no credentials set by the underlying web server, the
 * user is redirected to the normal user login form.
 *
 * @return false
 */
function ldap_sso_user_login_sso() {
  $auth_conf = ldap_authentication_get_valid_conf();
  $implementation = $auth_conf->ldapImplementation;
  $enabled = $auth_conf->ssoEnabled;


  if ($enabled == TRUE) {
    switch ($implementation) {

      case 'mod_auth_sspi' :
        $remote_user = FALSE;
        if (isset($_SERVER['REMOTE_USER'])) {
          $remote_user = $_SERVER['REMOTE_USER'];
        }
        elseif (isset($_SERVER['REDIRECT_REMOTE_USER'])) {
          $remote_user = $_SERVER['REDIRECT_REMOTE_USER'];
        }
        break;

      case 'mod_auth_kerb' :
        $remote_user = FALSE;
        if (isset($_SERVER['REMOTE_USER'])) {
          $remote_user = $_SERVER['REMOTE_USER'];
        }
        elseif (isset($_SERVER['REDIRECT_REMOTE_USER'])) {
          $remote_user = $_SERVER['REDIRECT_REMOTE_USER'];
        }

        if ($remote_user && preg_match('/^([A-Za-z0-9_\-\.]+)@([A-Za-z0-9_\-.]+)$/',
          $remote_user,
          $matches)) {
          $remote_user = $matches[1];
          $realm = $matches[2]; // This can be used later if realms is ever supported properly
        }
        break;
      }

    if ($remote_user) {
      if ($auth_conf->ssoRemoteUserStripDomainName) {
        // might be in form <remote_user>@<domain> or <domain>\<remote_user>
        $domain = NULL;
        $exploded = preg_split('/[\@\\\\]/', $remote_user);
        if (count($exploded) == 2) {
          if (strpos($remote_user, '@') !== FALSE) {
            $remote_user = $exploded[0];
            $domain = $exploded[1];
          }
          else {
            $domain = $exploded[0];
            $remote_user = $exploded[1];
          }
        }
      }
      watchdog('ldap_authentication', '%username : $_SERVER[\'REMOTE_USER\'] found',
              array('%username' => $remote_user), WATCHDOG_DEBUG);
      $fake_form_state = array(
        'values' => array(
          'name' => check_plain($remote_user),
         'pass' => user_password(20),
        ),
        'sso_login' => TRUE,
      );
      $user = ldap_authentication_user_login_authenticate_validate(array(), $fake_form_state);
      if ($user && $user->uid > 0) {
        if ($auth_conf->seamlessLogin == 1) {
          setcookie("seamless_login", 'auto login', time() + $auth_conf->cookieExpire, base_path(), "");
          $_SESSION['seamless_login'] = 'auto login';
          setcookie("seamless_login_attempted", '');
          unset($_SESSION['seamless_login_attempted']);

      }
      drupal_set_message(theme('ldap_authentication_login_message',
                                  array('message' => t('You have been successfully authenticated'))));
      drupal_goto('<front>');
      }
      else {
        if ($auth_conf->seamlessLogin == 1) {
          setcookie("seamless_login", 'do not auto login', time() + $auth_conf->cookieExpire, base_path(), "");
          $_SESSION['seamless_login'] = 'do not auto login';
      }
      drupal_set_message(theme('ldap_authentication_message_not_found',
          array('message' => t('Sorry, your LDAP credentials were not found, ' .
          'or the LDAP server is not available. You may log in ' .
           'with other credentials on the !user_login_form.',
            array('!user_login_form' => l(t('user login form'), 'user/login'))))
        ), 'error');
      drupal_goto('user/login');
      }
    }
    else {
      watchdog('ldap_authentication', '$_SERVER[\'REMOTE_USER\'] not found', array(), WATCHDOG_DEBUG);
      if ($auth_conf->seamlessLogin == 1) {
        setcookie("seamless_login", 'do not auto login', time() + $auth_conf->cookieExpire, base_path(), "");
        $_SESSION['seamless_login'] = 'do not auto login';
      }
      drupal_set_message(theme('ldap_authentication_message_not_authenticated',
                                 array('message' =>
                                     t('You were not authenticated by the server.
                                       You may log in with your credentials below.')
                                   )
                                ), 'error');
      drupal_goto('user/login');
    }
  }
  else
    drupal_goto('user/login');
}