2017-02-26 11:11:41 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								<?php
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								$realm = "MY REALM HERE";
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								$ldaphost = "my.ldap.host";
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-27 20:54:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								$ldaptls = "yes";
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-26 21:28:18 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								$ldapbase = "dc=my,dc=server,dc=com";
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-27 23:29:04 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								$checkuser = "cn";
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-27 20:54:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								function basicAuth() {
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-28 12:25:29 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    global $realm;
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-27 23:29:04 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    header('WWW-Authenticate: Basic realm="'.$realm.'"');
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-26 11:11:41 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    header('HTTP/1.0 401 Unauthorized');
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    exit;
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-27 20:54:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								if (!isset($_SERVER['PHP_AUTH_USER'])) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    basicAuth();
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-26 11:11:41 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								} else {
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-28 12:25:29 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    $tstusername = $_SERVER['PHP_AUTH_USER'];
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-26 11:11:41 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    $password = $_SERVER['PHP_AUTH_PW'];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    $ldapconn = ldap_connect($ldaphost, 389) 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        or error_die("connection to LDAP host failed");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        or error_die("failed to set LDAP protocol version 3");
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-27 20:54:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if ($ldaptls!="no" && $ldaptls!=0 && $ldaptlS)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        ldap_start_tls($ldapconn)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            or error_die($ldapconn, "cannot start LDAP TLS");
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-28 12:25:29 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if ($ldapbind = @ldap_bind($ldapconn, $checkuser.'='.$tstusername.','.$ldapbase, $password)) {
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-01 14:36:08 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        $search = ldap_search($ldapconn, $ldapbase, $checkuser.'='.$tstusername, array('mail', 'cn'));
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-01 09:08:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        $data = ldap_get_entries($ldapconn, $search);
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-28 12:25:29 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ldap_close($ldapconn);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        $username = $tstusername;
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-01 14:36:08 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        $email = array('count' => 1, 0 => $username.'@'.$_SERVER['HTTP_HOST']);
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-01 09:08:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        $fullname = $username;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if ($data['count']==1) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if (isset($data[0]['mail']))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                $email = $data[0]['mail'];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if (isset($data[0]['cn'][0]))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                $fullname = $data[0]['cn'][0];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-28 12:25:29 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    } else {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        error_log("user login failed: ".$checkuser.'='.$tstusername.','.$ldapbase);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        error_log(ldap_error($ldapconn));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        ldap_close($ldapconn);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        basicAuth();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-26 11:11:41 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-01 14:36:08 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								?>
							 |