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 196 197
|
<?php
/*
* HOMER API
* Homer's LDAP auth
*
* Copyright (C) 2011-2014 Alexandr Dubovikov <alexandr.dubovikov@gmail.com>
* Copyright (C) 2011-2012 Lorenzo Mangani <lorenzo.mangani@gmail.com>
*
* The Initial Developers of the Original Code are
*
* Alexandr Dubovikov <alexandr.dubovikov@gmail.com>
* Lorenzo Mangani <lorenzo.mangani@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace Authentication;
defined( '_HOMEREXEC' ) or die( 'Restricted access' );
class LDAP extends Authentication {
private $_instance = null;
function __construct($utable = NULL)
{
if($utable != NULL) $this->user_table = $utable;
}
public function getContainer()
{
if ($this->_instance === null) {
// extract name of the storage class
}
return $this->_instance;
}
function logIn($param) {
$ds=@ldap_connect(LDAP_HOST,LDAP_PORT);
$_SESSION['loggedin'] = "-1";
// Set LDAP Version, Default is Version 2
@ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, ( LDAP_VERSION) ? LDAP_VERSION : 2);
// Referrals are disabled
@ldap_set_option($ds, LDAP_OPT_REFERRALS, 0 );
// Enable TLS Encryption
if(LDAP_ENCRYPTION == "tls") {
// Documentation says - set to never
putenv('LDAPTLS_REQCERT=never') or die('Failed to setup the env');
@ldap_start_tls($ds);
}
if (defined('LDAP_BIND_USER') && defined('LDAP_BIND_PASSWORD')) {
if (!@ldap_bind( $ds, LDAP_BIND_USER, LDAP_BIND_PASSWORD)) {
return array();
}
}
$r=@ldap_search( $ds, LDAP_BASEDN, LDAP_USERNAME_ATTRIBUTE_OPEN .$param['username'].LDAP_USERNAME_ATTRIBUTE_CLOSE);
if ($r) {
$result = @ldap_get_entries( $ds, $r);
if ($result[0]) {
if (@ldap_bind( $ds, $result[0]['dn'], $param['password']) ) {
if($result[0] != NULL) {
if (defined("LDAP_GROUPDN")) {
if (!$this->check_filegroup_membership($ds, (defined("LDAP_GROUP_ARRAY") && LDAP_GROUP_ARRAY) ? $result[0][LDAP_GROUP_USER][0] : $result[0][LDAP_GROUP_USER])) {
return false;
}
}
if(array_key_exists(LDAP_UID, $result[0])) $user['uid'] = $result[0][LDAP_UID][0];
else $user['uid'] = base_convert($param['username'], 16, 10);
if(array_key_exists(LDAP_GID, $result[0])) $user['gid'] = $result[0][LDAP_GID][0];
else $user['gid'] = 10;
if(array_key_exists(LDAP_FIRSTNAME, $result[0])) $user['firstname'] = $result[0][LDAP_FIRSTNAME][0];
else $user['firstname'] = $param['username'];
if(array_key_exists(LDAP_LASTNAME, $result[0])) $user['lastname'] = $result[0][LDAP_LASTNAME][0];
else $user['lastname'] = $param['username'];
if(array_key_exists(LDAP_EMAIL, $result[0])) $user['email'] = $result[0][LDAP_EMAIL][0];
else $user['email'] = "no@exist.com";
$user['username'] = $param['username'];
$user['grp'] = "users";
$user['lastvisit'] = date('c');
$_SESSION['uid'] = $user['uid'];
$_SESSION['loggedin'] = $user['username'];
$_SESSION['userlevel'] = LDAP_USERLEVEL;
$_SESSION['username'] = $user['username'];
$_SESSION['gid'] = $user['gid'];
$_SESSION['grp'] = "users";
$_SESSION['data'] = $user;
// Assigne Admin Privs, should be read from the LDAP Directory in the future
$ADMIN_USER = split(",", LDAP_ADMIN_USER);
foreach($ADMIN_USER as &$value) {
if ($value == $param['username']) {
$_SESSION['userlevel'] = 1; # LDAP_ADMINLEVEL;
$user['grp'] = "users,admins";
$_SESSION["grp"] = "users,admins";
}
}
return $user;
}
}
}
}
return array();
}
/* posixGroup schema, rfc2307 */
function check_filegroup_membership($ds, $uid) {
$dn = LDAP_GROUPDN;
$attr = LDAP_GROUP_ATTRIBUTE;
$result = @ldap_compare($ds, $dn, $attr, $uid);
if ($result === true) return true;
else return false;
}
//logout function
function logOut(){
$_SESSION['loggedin'] = '-1';
session_destroy();
return;
}
function checkSession () {
if(!isset($_SESSION['loggedin'])) $_SESSION['loggedin'] = '-1';
if($_SESSION['loggedin'] == "-1") return false;
return true;
}
function checkAdmin () {
if(preg_match('/admins/',$_SESSION['grp'])) return true;
else return false;
}
function updateUser($param) {
$this->getUser();
}
function getUser() {
if(!isset($_SESSION['loggedin'])) $_SESSION['loggedin'] = '-1';
if($_SESSION['loggedin'] == "-1") return array();
return $_SESSION['data'];
}
//create random password with 8 alphanumerical characters
function createPassword() {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
}
?>
|