Ejemplo n.º 1
0
 public function check_records($records)
 {
     if (!is_array($records)) {
         return "Zone records are not valid. Must be specified as an (empty) array.";
     }
     $errors = array();
     foreach ($records as $entry) {
         $record = new RecordValidator($entry);
         if (!empty($this->mode_override)) {
             $record->mode = $this->mode_override;
         }
         if (!$record->validates()) {
             $errors[] = $record->getFormattedErrors();
         }
     }
     if (!empty($errors)) {
         array_unshift($errors, "Zone records are not valid. The following errors occurred: ");
         return implode("\n", $errors);
     } else {
         return true;
     }
 }
Ejemplo n.º 2
0
 public function test_validate_record()
 {
     echo "\n";
     // Missing content and too long name
     $data = array("name" => "example.toolongtldtoolongtldtoolongtldtoolongtldtoolongtldtoolongtld1", "type" => "A", "content" => "");
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(2, $validator->getErrors());
     // Missing content only
     $data['name'] = "example.longtld";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     // Missing type
     $data['name'] = "example.com";
     $data['type'] = "";
     $data['content'] = "127.0.0.2";
     $validator = new RecordValidator($data);
     // Invalid type
     $data['type'] = "ZZZZ";
     $data['content'] = "127.0.0.2";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     // Record content too long
     $data['type'] = "TXT";
     $data['content'] = '"' . str_repeat("a", 4096) . '"';
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     // Invalid priority for non-MX/SRV record
     $data['content'] = '""';
     $data['priority'] = "a";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     // Valid priority for non-MX/SRV record
     $data['content'] = '""';
     $data['priority'] = 12;
     $validator = new RecordValidator($data);
     $this->assertTrue($validator->validates());
     $this->assertEmpty($validator->getErrors());
     // Invalid AAAA record
     unset($data['priority']);
     $data['type'] = "AAAA";
     $data['content'] = "2001:838:300:417:::";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     // Valid AAAA record
     $data['content'] = "2001:838:300:417::";
     $validator = new RecordValidator($data);
     $this->assertTrue($validator->validates());
     // Invalid NS record
     $data['type'] = "NS";
     $data['content'] = "example.toolongtldtoolongtldtoolongtldtoolongtldtoolongtldtoolongtld1";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     // Invalid PTR record
     $data['type'] = "PTR";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     // NAPTR records
     $data['type'] = "NAPTR";
     $wrong = array('only three parts', 'a 20 "s" "SIP+I2C" "" .', '10 b "s" "SIP+I2C" "" .', '10 20 s "SIP+I2C" "" .', '10 20 "!" "SIP+I2C" "" .', '10 20 "su" "SIP+I2C" "" .', '10 20 "s" SIP+I2C "" .', '10 20 "s" "" "" .', '10 20 "s" "+I2C" "" .', '10 20 "s" "SIP+I2C" A .', '10 20 "s" "SIP+I2C" "iAiAi" .', '10 20 "s" "SIP+I2C" "!A!" .', '10 20 "s" "SIP+I2C" "!A!A!z" .', '10 20 "s" "SIP+I2C" "!A!\\1!z" .', '10 20 "s" "SIP+I2C" "!A!\\1!i" example.com.', '10 20 "s" "SIP+I2C" "!A!\\1!z" example.com.', '10 20 "s" "SIP+I2C" "" ', '10 20 "s" "SIP+I2C" "" x');
     $right = array('10 20 "s" "SIP+I2C" "" .', '10 20 "p" "" "" .', '10 20 "s" "SIP+I2C" "!A!\\3!i" .', '10 20 "u" "SIP+I2C" "" .', '10 20 "u" "SIP+I2C" "" example.com.');
     foreach ($wrong as $w) {
         printf("Testing /%s/\n", $w);
         $data['content'] = $w;
         $validator = new RecordValidator($data);
         $this->assertFalse($validator->validates());
         $this->assertCount(1, $validator->getErrors());
     }
     foreach ($right as $r) {
         printf("Testing /%s/\n", $r);
         $data['content'] = $r;
         $validator = new RecordValidator($data);
         $this->assertTrue($validator->validates());
         $this->assertEmpty($validator->getErrors());
     }
     // Invalid RP record
     $data['type'] = "RP";
     $data['content'] = "1 2 3";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     $data['content'] = "webmaster@example.com example.com";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     $data['content'] = "webmaster.example.com +invalid.example.com";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     // Valid RP record
     $data['content'] = "webmaster.example.com example.com";
     $validator = new RecordValidator($data);
     $this->assertTrue($validator->validates());
     $this->assertEmpty($validator->getErrors());
     // Invalid SOA record
     $data['type'] = "SOA";
     $data['content'] = "1 2";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     $data['content'] = "ns+.example.com hostmaster.example.com 0 86400 3600 86400 86400";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     $data['content'] = "ns.example.com hostmaster+.example.com 0 86400 3600 86400 86400";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     $data['content'] = "ns.example.com hostmaster.example.com a 86400 3600 86400 86400";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     // Valid SOA record
     $data['content'] = "ns.example.com hostmaster.example.com 0 86400 3600 86400 86400";
     $validator = new RecordValidator($data);
     $this->assertTrue($validator->validates());
     $this->assertEmpty($validator->getErrors());
     // Invalid SPF record
     $data['type'] = "SPF";
     $data['content'] = "unquoted string";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     // Invalid TXT record
     $data['type'] = "TXT";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     // Valid TXT/SPF record
     $data['content'] = '"quoted string"';
     $validator = new RecordValidator($data);
     $this->assertTrue($validator->validates());
     $this->assertEmpty($validator->getErrors());
     // Invalid SSHFP record
     $data['type'] = "SSHFP";
     $data['content'] = "1 2 3 4";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     $data['content'] = "1 2 0123456789ABCDEF0123456789ABCDEF01234567";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     $data['content'] = "1 1 0123456789ABCDEF0123456789ABCDEF01234567Z";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     // Valid SSHFP
     $data['content'] = "1 1 0123456789ABCDEF0123456789ABCDEF01234567";
     $validator = new RecordValidator($data);
     $this->assertTrue($validator->validates());
     $this->assertEmpty($validator->getErrors());
     $data['content'] = "3 2 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
     $validator = new RecordValidator($data);
     $this->assertTrue($validator->validates());
     $this->assertEmpty($validator->getErrors());
     // Invalid SRV record
     $data['type'] = "SRV";
     $data['content'] = "1 2 3 4";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     $data['priority'] = 10;
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     $data['content'] = "a 443 service.example.com";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     $data['content'] = "100 a service.example.com";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     $data['content'] = "100 443 invalid+.service.example.com";
     $validator = new RecordValidator($data);
     $this->assertFalse($validator->validates());
     $this->assertCount(1, $validator->getErrors());
     // Valid SRV record
     $data['content'] = "100 443 service.example.com";
     $validator = new RecordValidator($data);
     $this->assertTrue($validator->validates());
     $this->assertEmpty($validator->getErrors());
 }
Ejemplo n.º 3
0
 /**
  * Validates a set of records.
  *
  * ### Request: ###
  *
  * ~~~
  * {
  *      entries: [ {
  *              "name": <string>,
  *              "type": <string>,
  *              "content": <string>,
  *              "ttl": <int optional>,
  *              "priority: <int optional>,
  *              "change_date": <int optional>
  *              "mode": add|delete              # default = add
  *      },0..n ],
  *      records: [ {
  *              "name": <string>,
  *              "type": <string>,
  *              "content": <string>,
  *              "ttl": <int optional>,
  *              "priority: <int optional>,
  *              "change_date": <int optional>
  *              "mode": add|delete              # default = add
  *      },0..n ],
  * }
  * ~~~
  *
  * ### Response: ###
  *
  * ~~~
  * true
  * ~~~
  *
  * ### Errors: ###
  *
  * * 508 - Invalid request, missing required parameters or input validation failed.
  *
  * @access public
  * @param mixed $request Request parameters
  * @return Response True if record is valid, error message with parse errors otherwise.
  */
 public function validate($request)
 {
     $response = new FormattedResponse($request);
     $data = $request->parseData();
     if (empty($data) || !isset($data->entries) && !isset($data->records)) {
         $response->code = Response::BADREQUEST;
         $response->error = "Request body was malformed. Ensure that all mandatory properties have been set.";
         $response->error_detail = "BODY_MALFORMED";
         return $response;
     }
     Validator::resetCounter();
     $details = array();
     if (isset($data->entries) && is_array($data->entries)) {
         $output = array();
         $i = 0;
         foreach ($data->entries as $d) {
             $i++;
             if (!isset($d->name) || !isset($d->type) || !isset($d->content)) {
                 $output[] = sprintf("Missing required parameters in entry %d", $i);
                 continue;
             }
             $validator = new RecordValidator($d);
             $validator->record_type = "TEMPLATE";
             if (!$validator->validates()) {
                 $output[] = sprintf("Validation errors in entry %d:", $i);
                 $output[] = $validator->getFormattedErrors(false);
                 $details[] = $validator->getErrorDetails();
             }
             continue;
         }
     }
     if (isset($data->records) && is_array($data->records)) {
         $output = array();
         $i = 0;
         foreach ($data->records as $d) {
             $i++;
             if (!isset($d->name) || !isset($d->type) || !isset($d->content)) {
                 $output[] = sprintf("Missing required parameters in record %d", $i);
                 continue;
             }
             $validator = new RecordValidator($d);
             if (!$validator->validates()) {
                 $output[] = sprintf("Validation errors in record %d:", $i);
                 $output[] = $validator->getFormattedErrors(false);
                 $details[] = $validator->getErrorDetails();
             }
             continue;
         }
     }
     if (empty($output)) {
         $response->code = Response::OK;
         $response->body = true;
         $response->log_message = "Records were successfully validated.";
     } else {
         $response->code = Response::BADREQUEST;
         $response->error = implode("\n", $output);
         $response->error_detail = $details;
     }
     return $response;
 }