summaryrefslogtreecommitdiff
path: root/php/admin/include/auth.class.php.in
blob: 8932a10f339a4d755693f9f185a5b408855871dd (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
<?php
/*
 *  Copyright (c) 2004 Klarälvdalens Datakonsult AB
 *
 *    Written by Steffen Hansen <steffen@klaralvdalens-datakonsult.se>
 *
 *  This  program is free  software; you can redistribute  it and/or
 *  modify it  under the terms of the GNU  General Public License as
 *  published by the  Free Software Foundation; either version 2, or
 *  (at your option) any later version.
 *
 *  This program is  distributed in the hope that it will be useful,
 *  but WITHOUT  ANY WARRANTY; without even the  implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You can view the  GNU General Public License, online, at the GNU
 *  Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.
 */

require_once('ldap.class.php');
require_once('debug.php');
require_once('mysmarty.php');
require_once('locale.php');

class KolabAuth {
    function KolabAuth( $do_auth = true, $params = array() ) {
	    $this->params = $params;
		if( isset( $_GET['logout'] ) || isset( $_POST['logout'] ) ) {
			$this->logout();
		} else if( $do_auth ) {
			$this->authenticate();
		}
	}

	function authenticate() {
		global $ldap;
		$this->error_string = false;
		if( !isset( $ldap ) ) {
			$this->error_string = _("Server error, no ldap object!");
			return false;
		}
		// Anon. bind first
		if( !$ldap->bind( $_SESSION['php_dn'],  $_SESSION['php_pw'] ) ) {
			$this->error_string = _("Could not bind to LDAP server: ").$ldap->error();
			$this->gotoLoginPage(); 
		}
		if( $this->isAuthenticated() ) {
			$bind_result = $ldap->bind( $_SESSION['auth_dn'], $_SESSION['auth_pw'] );
		} else {
			$bind_result = false;
		}
		if( !$bind_result ) {
			// Anon. bind first
			if( !$ldap->bind() ) {
				$this->error_string = _("Could not bind to LDAP server");
				$this->gotoLoginPage(); 
			}
			// User not logged in, check login/password
			if( isset( $_POST['username'] ) && isset( $_POST['password'] ) ) {
				$dn = $ldap->dnForUid( $_POST['username'] );
                if (!$dn) {
					$dn = $ldap->dnForMail( $_POST['username'] ); // try mail attribute
				}
				if( $dn ) {
					$auth_user = $ldap->uidForDn( $dn );
					$auth_group = $ldap->groupForUid( $auth_user );
					$tmp_group = ($auth_user=='manager')?'manager':$auth_group;
					if( !in_array( $tmp_group, $this->params['allow_user_classes'] ) ) {
						$this->error_string = _("User class '$tmp_group' is denied access");
						$this->gotoLoginPage();					  
					}
					$bind_result = $ldap->bind( $dn, $_POST['password'] );
					if( $bind_result ) {
						// All OK!
						$_SESSION['auth_dn'] = $dn;
						$_SESSION['auth_user'] = $auth_user;
						$_SESSION['auth_pw'] = $_POST['password'];
						$_SESSION['auth_group'] = $auth_group;
						$_SESSION['remote_ip'] = $_SERVER['REMOTE_ADDR'];
						return true;
					} else {
						$this->error_string = _("Wrong username or password");
						$this->gotoLoginPage(); 
					}
				} else {
					$this->error_string = _("Wrong username or password");
					//$this->error_string = "Dn not found";
					$this->gotoLoginPage(); 
				}
			} else {
				$this->error_string = _('Please log in as a valid user');
				$this->gotoLoginPage();
			}
		} else {
			// All OK, user already logged in
			return true;
		}
	}
  
	function logout() {
		session_unset();
		session_destroy();
		$this->error_string = "";
		//$this->gotoLoginPage();
		Header("Location: @webserver_web_prefix@/admin/");
	}

	function handleLogin() {
		if( isset( $_POST['login'] ) ) {
			$this->authenticate();
		} else if( isset( $_POST['logout'] ) ) {
			$this->logout();
		}
	}

	function gotoLoginPage() {
		global $topdir;
		$smarty =& new MySmarty();
		$smarty->assign( 'topdir', $topdir );
		$smarty->assign( 'uid', '' );
		$smarty->assign( 'group', '' );
		$smarty->assign( 'page_title', _('Login') );
		$smarty->assign( 'menuitems', array() );
		$smarty->assign( 'errors', array( $this->error() ) );
		$smarty->assign( 'maincontent', 'login.tpl' );
		$smarty->display('page.tpl');
		exit();
	}

	function isAuthenticated() {
		return isset( $_SESSION['auth_dn'] ) && $_SESSION['remote_ip'] == $_SERVER['REMOTE_ADDR'];
	}

	function dn() {
		if( $this->isAuthenticated() ) return $_SESSION['auth_dn'];
		else return false;
	}

	function uid() {
		if( $this->isAuthenticated() ) return $_SESSION['auth_user'];
		else return false;
	}

	function group() {
		if( $this->isAuthenticated() ) return $_SESSION['auth_group'];
	}

	function password() {
		if( $this->isAuthenticated() ) {
			return $_SESSION['auth_pw'];
		}
		else return false;
	}

	function setDn( $dn ) {$_SESSION['auth_dn'] = $dn;}
	function setUid( $uid ) {$_SESSION['auth_user'] = $uid;}
	function setPassword( $pw ) {$_SESSION['auth_pw'] = $pw;}

	function error() {
		return $this->error_string;
	}

	var $error_string = false;
	var $params;
};
/*
  Local variables:
  mode: php
  indent-tabs-mode: t
  tab-width: 4
  buffer-file-coding-system: utf-8
  End:
 */
?>