2016-05-18 10:06:12 +02:00
< ? php
2016-08-18 20:28:22 -05:00
if ( ! isset ( $_SESSION [ 'username' ])) {
2016-05-18 10:06:12 +02:00
$_SESSION [ 'username' ] = '' ;
}
// Disable certificate checking before connect if required
if ( isset ( $config [ 'auth_ad_check_certificates' ]) &&
$config [ 'auth_ad_check_certificates' ] == 0 ) {
putenv ( 'LDAPTLS_REQCERT=never' );
};
// Set up connection to LDAP server
2016-06-03 22:16:10 -05:00
$ldap_connection = @ ldap_connect ( $config [ 'auth_ad_url' ]);
if ( ! $ldap_connection ) {
echo '<h2>Fatal error while connecting to AD url ' . $config [ 'auth_ad_url' ] . ': ' . ldap_error ( $ldap_connection ) . '</h2>' ;
2016-05-18 10:06:12 +02:00
exit ;
}
// disable referrals and force ldap version to 3
2016-06-03 22:16:10 -05:00
ldap_set_option ( $ldap_connection , LDAP_OPT_REFERRALS , 0 );
ldap_set_option ( $ldap_connection , LDAP_OPT_PROTOCOL_VERSION , 3 );
2016-05-18 10:06:12 +02:00
// Bind to AD
if ( isset ( $config [ 'auth_ad_binduser' ]) && isset ( $config [ 'auth_ad_bindpassword' ])) {
// With specified bind user
2016-06-03 22:16:10 -05:00
if ( ! ldap_bind ( $ldap_connection , " ${ config['auth_ad_binduser']}@${config['auth_ad_domain'] } " , " ${ config['auth_ad_bindpassword'] } " )) {
echo ldap_error ( $ldap_connection );
2016-05-18 10:06:12 +02:00
}
2016-08-18 20:28:22 -05:00
} else {
2016-05-18 10:06:12 +02:00
// Anonymous
2016-06-03 22:16:10 -05:00
if ( ! ldap_bind ( $ldap_connection )) {
echo ldap_error ( $ldap_connection );
2016-05-18 10:06:12 +02:00
}
}
2016-08-18 20:28:22 -05:00
function authenticate ( $username , $password )
{
2016-05-18 10:06:12 +02:00
global $config ;
2016-08-18 20:28:22 -05:00
if ( isset ( $_SERVER [ 'REMOTE_USER' ])) {
$_SESSION [ 'username' ] = mres ( $_SERVER [ 'REMOTE_USER' ]);
2016-05-18 10:06:12 +02:00
2016-08-18 20:28:22 -05:00
if ( user_exists ( $_SESSION [ 'username' ])) {
2016-05-18 10:06:12 +02:00
adduser ( $username );
return 1 ;
}
$_SESSION [ 'username' ] = $config [ 'http_auth_guest' ];
return 1 ;
}
return 0 ;
}
2016-09-13 15:10:42 +01:00
function reauthenticate ()
{
// not supported so return 0
return 0 ;
}
2016-08-18 20:28:22 -05:00
function passwordscanchange ()
{
2016-05-18 10:06:12 +02:00
// not supported so return 0
return 0 ;
}
2016-08-18 20:28:22 -05:00
function changepassword ()
{
2016-05-18 10:06:12 +02:00
// not supported so return 0
return 0 ;
}
2016-08-18 20:28:22 -05:00
function auth_usermanagement ()
{
2016-05-18 10:06:12 +02:00
// not supported so return 0
return 0 ;
}
2016-08-18 20:28:22 -05:00
function adduser ( $username , $level = 0 , $email = '' , $realname = '' , $can_modify_passwd = 0 , $description = '' , $twofactor = 0 )
{
2016-05-18 10:06:12 +02:00
// Check to see if user is already added in the database
if ( ! user_exists_in_db ( $username )) {
$userid = dbInsert ( array ( 'username' => $username , 'realname' => $realname , 'email' => $email , 'descr' => $description , 'level' => $level , 'can_modify_passwd' => $can_modify_passwd , 'twofactor' => $twofactor , 'user_id' => get_userid ( $username )), 'users' );
if ( $userid == false ) {
return false ;
2016-08-18 20:28:22 -05:00
} else {
foreach ( dbFetchRows ( 'select notifications.* from notifications where not exists( select 1 from notifications_attribs where notifications.notifications_id = notifications_attribs.notifications_id and notifications_attribs.user_id = ?) order by notifications.notifications_id desc' , array ( $userid )) as $notif ) {
dbInsert ( array ( 'notifications_id' => $notif [ 'notifications_id' ], 'user_id' => $userid , 'key' => 'read' , 'value' => 1 ), 'notifications_attribs' );
2016-05-18 10:06:12 +02:00
}
}
return $userid ;
2016-08-18 20:28:22 -05:00
} else {
2016-05-18 10:06:12 +02:00
return false ;
}
}
2016-08-18 20:28:22 -05:00
function user_exists_in_db ( $username )
{
2016-05-18 10:06:12 +02:00
$return = dbFetchCell ( 'SELECT COUNT(*) FROM users WHERE username = ?' , array ( $username ), true );
return $return ;
}
2016-08-18 20:28:22 -05:00
function user_exists ( $username )
{
2016-06-03 10:56:42 -05:00
global $config , $ldap_connection ;
2016-05-18 10:06:12 +02:00
2016-08-18 20:28:22 -05:00
if ( auth_ldap_session_cache_get ( 'user_exists' )) {
2016-05-18 10:06:12 +02:00
return 1 ;
2016-08-18 20:28:22 -05:00
}
2016-05-18 10:06:12 +02:00
2016-08-18 20:28:22 -05:00
$search = ldap_search (
$ldap_connection ,
$config [ 'auth_ad_base_dn' ],
" (samaccountname= ${ username } ) " ,
array ( 'samaccountname' )
);
2016-06-03 10:56:42 -05:00
$entries = ldap_get_entries ( $ldap_connection , $search );
2016-05-18 10:06:12 +02:00
if ( $entries [ 'count' ]) {
/*
* Cache positiv result as this will result in more queries which we
* want to speed up .
*/
2016-08-18 20:28:22 -05:00
auth_ldap_session_cache_set ( 'user_exists' , 1 );
2016-05-18 10:06:12 +02:00
return 1 ;
}
return 0 ;
}
2016-08-18 20:28:22 -05:00
function get_userlevel ( $username )
{
2016-06-03 10:56:42 -05:00
global $config , $ldap_connection ;
2016-05-18 10:06:12 +02:00
2016-08-18 20:28:22 -05:00
$userlevel = auth_ldap_session_cache_get ( 'userlevel' );
2016-05-18 10:06:12 +02:00
if ( $userlevel ) {
return $userlevel ;
2016-08-18 20:28:22 -05:00
} else {
2016-05-18 10:06:12 +02:00
$userlevel = 0 ;
}
// Find all defined groups $username is in
2016-08-18 20:28:22 -05:00
$search = ldap_search (
$ldap_connection ,
$config [ 'auth_ad_base_dn' ],
" (samaccountname= { $username } ) " ,
array ( 'memberOf' )
);
2016-06-03 10:56:42 -05:00
$entries = ldap_get_entries ( $ldap_connection , $search );
2016-05-18 10:06:12 +02:00
// Loop the list and find the highest level
foreach ( $entries [ 0 ][ 'memberof' ] as $entry ) {
$group_cn = get_cn ( $entry );
if ( $config [ 'auth_ad_groups' ][ $group_cn ][ 'level' ] > $userlevel ) {
$userlevel = $config [ 'auth_ad_groups' ][ $group_cn ][ 'level' ];
}
}
2016-08-18 20:28:22 -05:00
auth_ldap_session_cache_set ( 'userlevel' , $userlevel );
2016-05-18 10:06:12 +02:00
return $userlevel ;
}
2016-08-18 20:28:22 -05:00
function get_userid ( $username )
{
2016-06-03 10:56:42 -05:00
global $config , $ldap_connection ;
2016-05-18 10:06:12 +02:00
2016-08-18 20:28:22 -05:00
$user_id = auth_ldap_session_cache_get ( 'userid' );
if ( isset ( $user_id )) {
2016-05-18 10:06:12 +02:00
return $user_id ;
2016-08-18 20:28:22 -05:00
} else {
2016-05-18 10:06:12 +02:00
$user_id = - 1 ;
}
$attributes = array ( 'objectsid' );
2016-08-18 20:28:22 -05:00
$search = ldap_search (
$ldap_connection ,
$config [ 'auth_ad_base_dn' ],
" (samaccountname= { $username } ) " ,
$attributes
);
2016-06-03 10:56:42 -05:00
$entries = ldap_get_entries ( $ldap_connection , $search );
2016-05-18 10:06:12 +02:00
if ( $entries [ 'count' ]) {
2016-08-18 20:28:22 -05:00
$user_id = preg_replace ( '/.*-(\d+)$/' , '$1' , sid_from_ldap ( $entries [ 0 ][ 'objectsid' ][ 0 ]));
2016-05-18 10:06:12 +02:00
}
2016-08-18 20:28:22 -05:00
auth_ldap_session_cache_set ( 'userid' , $user_id );
2016-05-18 10:06:12 +02:00
return $user_id ;
}
2016-08-18 20:28:22 -05:00
function deluser ( $username )
{
2016-05-18 10:06:12 +02:00
dbDelete ( 'bill_perms' , '`user_name` = ?' , array ( $username ));
dbDelete ( 'devices_perms' , '`user_name` = ?' , array ( $username ));
dbDelete ( 'ports_perms' , '`user_name` = ?' , array ( $username ));
dbDelete ( 'users_prefs' , '`user_name` = ?' , array ( $username ));
dbDelete ( 'users' , '`user_name` = ?' , array ( $username ));
return dbDelete ( 'users' , '`username` = ?' , array ( $username ));
}
2016-08-18 20:28:22 -05:00
function get_userlist ()
{
2016-06-03 10:56:42 -05:00
global $config , $ldap_connection ;
2016-05-18 10:06:12 +02:00
$userlist = array ();
$userhash = array ();
$ldap_groups = get_group_list ();
2016-08-18 20:28:22 -05:00
foreach ( $ldap_groups as $ldap_group ) {
2016-05-18 10:06:12 +02:00
$group_cn = get_cn ( $ldap_group );
2016-06-03 10:56:42 -05:00
$search = ldap_search ( $ldap_connection , $config [ 'auth_ad_base_dn' ], " (cn= { $group_cn } ) " , array ( 'member' ));
$entries = ldap_get_entries ( $ldap_connection , $search );
2016-05-18 10:06:12 +02:00
2016-08-18 20:28:22 -05:00
foreach ( $entries [ 0 ][ 'member' ] as $member ) {
2016-05-18 10:06:12 +02:00
$member_cn = get_cn ( $member );
2016-08-18 20:28:22 -05:00
$search = ldap_search (
$ldap_connection ,
$config [ 'auth_ad_base_dn' ],
" (cn= { $member_cn } ) " ,
array ( 'sAMAccountname' , 'displayName' , 'objectSID' , 'mail' )
);
2016-06-03 10:56:42 -05:00
$results = ldap_get_entries ( $ldap_connection , $search );
2016-08-18 20:28:22 -05:00
foreach ( $results as $result ) {
if ( isset ( $result [ 'samaccountname' ][ 0 ])) {
$userid = preg_replace (
'/.*-(\d+)$/' ,
'$1' ,
sid_from_ldap ( $result [ 'objectsid' ][ 0 ])
);
2016-05-18 10:06:12 +02:00
// don't make duplicates, user may be member of more than one group
$userhash [ $result [ 'samaccountname' ][ 0 ]] = array (
'realname' => $result [ 'displayName' ][ 0 ],
'user_id' => $userid ,
'email' => $result [ 'mail' ][ 0 ]
);
}
}
}
}
2016-08-18 20:28:22 -05:00
foreach ( array_keys ( $userhash ) as $key ) {
2016-05-18 10:06:12 +02:00
$userlist [] = array (
'username' => $key ,
'realname' => $userhash [ $key ][ 'realname' ],
'user_id' => $userhash [ $key ][ 'user_id' ],
'email' => $userhash [ $key ][ 'email' ]
);
}
return $userlist ;
}
2016-08-18 20:28:22 -05:00
function can_update_users ()
{
2016-05-18 10:06:12 +02:00
// not supported so return 0
return 0 ;
}
2016-08-18 20:28:22 -05:00
function get_user ( $user_id )
{
2016-05-18 10:06:12 +02:00
// not supported so return 0
return dbFetchRow ( 'SELECT * FROM `users` WHERE `user_id` = ?' , array ( $user_id ), true );
}
2016-08-18 20:28:22 -05:00
function update_user ( $user_id , $realname , $level , $can_modify_passwd , $email )
{
2016-05-18 10:06:12 +02:00
dbUpdate ( array ( 'realname' => $realname , 'can_modify_passwd' => $can_modify_passwd , 'email' => $email ), 'users' , '`user_id` = ?' , array ( $user_id ));
}
2016-08-18 20:28:22 -05:00
function get_fullname ( $username )
{
2016-06-03 10:56:42 -05:00
global $config , $ldap_connection ;
2016-05-18 10:06:12 +02:00
$attributes = array ( 'name' );
2016-08-18 20:28:22 -05:00
$result = ldap_search (
$ldap_connection ,
$config [ 'auth_ad_base_dn' ],
" (samaccountname= { $username } ) " ,
$attributes
);
2016-06-03 10:56:42 -05:00
$entries = ldap_get_entries ( $ldap_connection , $result );
2016-05-18 10:06:12 +02:00
if ( $entries [ 'count' ] > 0 ) {
$membername = $entries [ 0 ][ 'name' ][ 0 ];
2016-08-18 20:28:22 -05:00
} else {
2016-05-18 10:06:12 +02:00
$membername = $username ;
}
return $membername ;
}
2016-08-18 20:28:22 -05:00
function get_group_list ()
{
2016-05-18 10:06:12 +02:00
global $config ;
$ldap_groups = array ();
// show all Active Directory Users by default
$default_group = 'Users' ;
if ( isset ( $config [ 'auth_ad_group' ])) {
if ( $config [ 'auth_ad_group' ] !== $default_group ) {
$ldap_groups [] = $config [ 'auth_ad_group' ];
}
}
if ( ! isset ( $config [ 'auth_ad_groups' ]) && ! isset ( $config [ 'auth_ad_group' ])) {
$ldap_groups [] = get_dn ( $default_group );
}
foreach ( $config [ 'auth_ad_groups' ] as $key => $value ) {
$ldap_groups [] = get_dn ( $key );
}
return $ldap_groups ;
}
2016-08-18 20:28:22 -05:00
function get_dn ( $samaccountname )
{
2016-06-03 10:56:42 -05:00
global $config , $ldap_connection ;
2016-05-18 10:06:12 +02:00
$attributes = array ( 'dn' );
2016-08-18 20:28:22 -05:00
$result = ldap_search (
$ldap_connection ,
$config [ 'auth_ad_base_dn' ],
" (samaccountname= { $samaccountname } ) " ,
$attributes
);
2016-06-03 10:56:42 -05:00
$entries = ldap_get_entries ( $ldap_connection , $result );
2016-05-18 10:06:12 +02:00
if ( $entries [ 'count' ] > 0 ) {
return $entries [ 0 ][ 'dn' ];
2016-08-18 20:28:22 -05:00
} else {
2016-05-18 10:06:12 +02:00
return '' ;
}
}
2016-08-18 20:28:22 -05:00
function get_cn ( $dn )
{
2016-05-18 10:06:12 +02:00
preg_match ( '/[^,]*/' , $dn , $matches , PREG_OFFSET_CAPTURE , 3 );
return $matches [ 0 ][ 0 ];
}
function sid_from_ldap ( $sid )
{
$sidHex = unpack ( 'H*hex' , $sid );
2016-08-18 20:28:22 -05:00
$subAuths = unpack ( 'H2/H2/n/N/V*' , $sid );
2016-05-18 10:06:12 +02:00
$revLevel = hexdec ( substr ( $sidHex , 0 , 2 ));
2016-08-18 20:28:22 -05:00
$authIdent = hexdec ( substr ( $sidHex , 4 , 12 ));
2016-05-18 10:06:12 +02:00
return 'S-' . $revLevel . '-' . $authIdent . '-' . implode ( '-' , $subAuths );
}
2016-08-18 20:28:22 -05:00
function auth_ldap_session_cache_get ( $attr )
{
2016-05-18 10:06:12 +02:00
global $config ;
$ttl = 300 ;
2016-08-18 20:28:22 -05:00
if ( $config [ 'auth_ldap_cache_ttl' ]) {
2016-05-18 10:06:12 +02:00
$ttl = $config [ 'auth_ldap_cache_ttl' ];
2016-08-18 20:28:22 -05:00
}
2016-05-18 10:06:12 +02:00
// auth_ldap cache present in this session?
2016-08-18 20:28:22 -05:00
if ( ! isset ( $_SESSION [ 'auth_ldap' ])) {
return null ;
}
2016-05-18 10:06:12 +02:00
$cache = $_SESSION [ 'auth_ldap' ];
// $attr present in cache?
2016-08-18 20:28:22 -05:00
if ( ! isset ( $cache [ $attr ])) {
return null ;
}
2016-05-18 10:06:12 +02:00
// Value still valid?
2016-08-18 20:28:22 -05:00
if ( time () - $cache [ $attr ][ 'last_updated' ] >= $ttl ) {
return null ;
}
2016-05-18 10:06:12 +02:00
return $cache [ $attr ][ 'value' ];
}
2016-08-18 20:28:22 -05:00
function auth_ldap_session_cache_set ( $attr , $value )
{
2016-05-18 10:06:12 +02:00
$_SESSION [ 'auth_ldap' ][ $attr ][ 'value' ] = $value ;
2016-08-18 20:28:22 -05:00
$_SESSION [ 'auth_ldap' ][ $attr ][ 'last_updated' ] = time ();
2016-05-18 10:06:12 +02:00
}