| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SimpleSAML\Module\monitor\TestCase\AuthSource\Ldap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \SimpleSAML\Module\monitor\State as State; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use \SimpleSAML\Module\monitor\TestData as TestData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use \SimpleSAML\Module\monitor\TestResult as TestResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | final class Search extends \SimpleSAML\Module\monitor\TestCaseFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var \SimpleSAML_Auth_LDAP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $base; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $username; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $password; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $attributes = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param TestData $testData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     protected function initialize($testData) | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $authSourceData = $testData->getInputItem('authSourceData'); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         // Just to be on the safe side, strip off any OU's and search to whole directory | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $base = $authSourceData->getString('search.base', '<< unset >>'); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $base = is_array($base) ? $base[0] : $base; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         if (($i = stripos($base, 'DC=')) > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             $base = substr($base, $i); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $this->base = $base; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $username = $authSourceData->getString('search.username', '<< unset >>'); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $this->setSubject($username); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         if (strpos($username, 'DC=') > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             // We have been given a DN | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             $username = ldap_explode_dn($username, 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             $this->username = $username[0]; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $this->attributes = array('cn'); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             // We have been given a sAMAccountName | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             $this->username = $username; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             $this->attributes = array('sAMAccountName'); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $this->password = $authSourceData->getString('search.password', '<< unset >>'); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         $this->connection = $testData->getInputItem('connection'); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         parent::initialize($testData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function invokeTest() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $this->connection->searchfordn($this->base, $this->attributes, $this->username); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } catch (\SimpleSAML_Error_Error $error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             // Fallthru | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $testResult = new TestResult('LDAP Search', $this->getSubject()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if (isSet($error)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $msg = str_replace('Library - LDAP searchfordn(): ', '', $error->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $testResult->setState(State::ERROR); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $testResult->setMessage($msg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $testResult->setState(State::OK); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $testResult->setMessage('Search succesful'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->setTestResult($testResult); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  |