public function delete_records($response, $identifier, $data, &$out = null, $connection = null)
 {
     if (!is_object($data) || !isset($data->records)) {
         $response->code = Response::INTERNALSERVERERROR;
         $response->error = "Deletion data invalid";
         $response->error_detail = "INTERNAL_SERVER_ERROR";
         $out = false;
         return $response;
     }
     ZoneFunctions::get_zone($response, $identifier, $o, false, true, true);
     if (empty($o)) {
         $out = false;
         return $response;
     }
     if ($o['type'] == "SLAVE") {
         $response->code = Response::CONFLICT;
         $response->error = sprintf("Cannot delete records from SLAVE zone %s", $identifier);
         $response->error_detail = "ZONE_IS_SLAVE";
         $out = false;
         return $response;
     }
     $zone_id = $o['z_id'];
     if (empty($zone_id) || !ctype_digit($zone_id)) {
         $response->code = Response::INTERNALSERVERERROR;
         $response->error = "Could not retrieve Zone ID to delete records from" . var_export($o, true);
         $response->error_detail = "INTERNAL_SERVER_ERROR";
         $out = false;
         return $response;
     }
     unset($o);
     $commit = false;
     if ($connection === null) {
         try {
             $connection = Database::getConnection();
         } catch (PDOException $e) {
             $response->code = Response::INTERNALSERVERERROR;
             $response->error = "Could not connect to PowerDNS server.";
             $response->error_detail = "INTERNAL_SERVER_ERROR";
             $out = false;
             return $response;
         }
         $connection->beginTransaction();
         $commit = true;
     }
     $statement = $connection->prepare(sprintf("DELETE FROM `%s` WHERE domain_id = :did AND name = :name AND type = :type AND prio = :priority AND content = :content;", PowerDNSConfig::DB_RECORD_TABLE));
     $statement->bindParam(":did", $r_did);
     $statement->bindParam(":name", $r_name);
     $statement->bindParam(":type", $r_type);
     $statement->bindParam(":content", $r_content);
     $statement->bindParam(":priority", $r_prio);
     $statement_noprio = $connection->prepare(sprintf("DELETE FROM `%s` WHERE domain_id = :did AND name = :name AND type = :type AND content = :content;", PowerDNSConfig::DB_RECORD_TABLE));
     $statement_noprio->bindParam(":did", $r_did);
     $statement_noprio->bindParam(":name", $r_name);
     $statement_noprio->bindParam(":type", $r_type);
     $statement_noprio->bindParam(":content", $r_content);
     $r_did = $zone_id;
     foreach ($data->records as $record) {
         $stmt = $statement_noprio;
         if (!isset($record->name) || !isset($record->type) || !isset($record->content)) {
             continue;
         }
         if ($record->type == "MX" || $record->type == "SRV") {
             if (!isset($record->priority)) {
                 continue;
             } else {
                 $stmt = $statement;
             }
         }
         $r_name = $record->name;
         $r_type = $record->type;
         $r_content = $record->content;
         $r_prio = $record->priority;
         if ($stmt->execute() === false) {
             $response->code = Response::INTERNALSERVERERROR;
             $response->error = sprintf("Rolling back transaction, failed to delete zone record - name: '%s', type: '%s', prio: '%s'", $r_name, $r_type, $r_prio);
             $response->error_detail = "RECORD_DELETE_FAILED";
             if ($commit == true) {
                 $connection->rollback();
             }
             $out = false;
             return $response;
         }
     }
     if ($commit == true) {
         $connection->commit();
     }
     $response->code = Response::OK;
     $response->body = true;
     $response->log_message = sprintf("Zone %s deleted %d records.", $identifier, count($data->records));
     $out = true;
     return $response;
 }
Example #2
0
 /**
  * Delete an existing DNS zone, or delete a record from the zone.
  *
  * If an identifier is specified, the entire zone will be deleted.
  *
  * ### Response: ###
  *
  * ~~~
  * true
  * ~~~
  *
  * If a body is specified, but no identifier, the specified entries will be deleted from the zone.
  *
  * ### Request: ###
  *
  * ~~~
  * {
  *     "name": <string>,
  *     "records": [ {
  *             "name": <string>,
  *             "type": <string>,
  *             "content": <string>,
  *             "priority": <int>
  *     },1..n ]
  * }
  * ~~~
  *
  * ### Response: ###
  *
  * ~~~
  * true
  * ~~~
  *
  * ### Errors (request without identifier): ###
  *
  * * 508 - Invalid request, missing required parameters or input validation failed.
  * * 500 - Failed to connect to database or query execution error.
  * * 409 - Cannot delete records from a SLAVE zone.
  * * 404 - Could not find zone.
  *
  * ### Errors (request with identifier): ###
  *
  * * 508 - Invalid request, missing required parameters or input validation failed.
  * * 500 - Failed to connect to database or query execution error.
  * * 404 - Could not find zone.
  *
  * @access public
  * @param mixed $request Request parameters
  * @param string $identifier Zone identifier
  * @return Response True if zone was deleted, error message otherwise.
  */
 public function delete($request, $identifier = null)
 {
     $response = new FormattedResponse($request);
     $data = $request->parseData();
     if (empty($identifier) && (empty($data) || !isset($data->name) || !isset($data->records) || empty($data->records))) {
         $response->code = Response::BADREQUEST;
         $response->error = "Identifier and/or records were missing or invalid. Ensure that the body is in valid format and all required parameters are present.";
         $response->error_detail = "MISSING_REQUIRED_PARAMETERS";
         return $response;
     }
     $validator = new ZoneValidator($data);
     $validator->mode_override = "delete";
     if (!empty($identifier)) {
         $validator->identifier = $identifier;
     }
     if (!$validator->validates()) {
         $response->code = Response::BADREQUEST;
         $response->error = $validator->getFormattedErrors();
         $response->error_detail = $validator->getErrorDetails();
         return $response;
     }
     if (!empty($identifier)) {
         return ZoneFunctions::delete_zone($response, $identifier);
     } else {
         return ZoneFunctions::delete_records($response, $data->name, $data);
     }
 }
 public function delete_arpa($response, $identifier, &$out = null)
 {
     ArpaFunctions::get_arpa($response, $identifier, $o, true);
     if (empty($o)) {
         $out = false;
         return $response;
     }
     $record = new stdClass();
     $record->name = $o['name'];
     $record->type = "PTR";
     $record->content = $o['reverse_dns'];
     $record->priority = $o['priority'];
     $req = new stdClass();
     $req->records = array($record);
     $response = ZoneFunctions::delete_records($response, $o['arpa_zone'], $req, $o);
     if (empty($o)) {
         $out = false;
         return $response;
     }
     $response->code = Response::OK;
     $response->body = true;
     $response->log_message = sprintf("Deleted Arpa record for IP %s", $identifier);
     $out = true;
     return $response;
 }