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; }
/** * Retrieves an existing DNS zone. * * If no identifier is specified and no body is supplied, all zones will be * retrieved without records. * * ### Response: ### * * ~~~ * [ * { * "name": <string>, * "type": MASTER|SLAVE|NATIVE, * "master": <ipv4 optional>, * "last_check": <int optional>, * "notified_serial": <int optional> * },0..n * ] * ~~~ * * If a query is specified in the URL, all zones matching the given wildcard * are returned. The * wildcard is supported. * * If an identifier is specified, one zone will be retrieved with records. * * ### Response: ### * * ~~~ * { * "name": <string>, * "type": MASTER|SLAVE|NATIVE, * "master": <ipv4>, * "last_check": <int>, * "records": [ { * "name": <string>, * "type": <string>, * "content": <string>, * "ttl": <int optional>, * "priority: <int optional>, * "change_date": <int optional> * },0..n ] * } * ~~~ * * ### Errors (request without identifier): ### * * * 508 - Invalid request, missing required parameters or input validation failed. * * 500 - Failed to connect to database or query execution error. * * ### 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 DNS zone data if successful, error message otherwise. */ public function get($request, $identifier = null) { $response = new FormattedResponse($request); $data = $request->parseData(); if (empty($identifier)) { if ($data == null) { return ZoneFunctions::get_all_zones($response); } else { $validator = new ZoneValidator($data); if (!isset($data->query)) { $response->code = Response::BADREQUEST; $response->error = "Query was missing. Ensure that the body is in valid format and all required parameters are present."; $response->error_detail = "BODY_MALFORMED"; return $response; } if (!$validator->validates()) { $response->code = Response::BADREQUEST; $response->error = $validator->getFormattedErrors(); $response->error_detail = $validator->getErrorDetails(); return $response; } return ZoneFunctions::query_zones($response, $data->query); } } else { $validator = new ZoneValidator(); $validator->identifier = $identifier; if (!$validator->validates()) { $response->code = Response::BADREQUEST; $response->error = $validator->getFormattedErrors(); $response->error_detail = $validator->getErrorDetails(); return $response; } return ZoneFunctions::get_zone($response, $identifier); } }
public function create_arpa($response, $identifier, $data, &$out = null) { ArpaFunctions::get_arpa($response, $identifier, $o); if (!empty($o)) { $response->code = Response::CONFLICT; $response->error = "Resource already exists"; $response->error_detail = "ARPA_ALREADY_EXISTS"; $out = false; return $response; } $response->error = null; unset($o); ZoneFunctions::get_zone($response, $identifier, $o, false); if (empty($o)) { if ($response->code == Response::NOTFOUND) { $response->error = sprintf("Could not find Arpa zone for ip '%s'", $identifier); $response->error_detail = "ARPA_ZONE_NOT_FOUND"; } $out = false; return $response; } $zone = $o['name']; unset($o); $record = new stdClass(); $record->name = HelperFunctions::ip_to_arpa($identifier); $record->type = "PTR"; $record->content = $data; $req = new stdClass(); $req->records = array($record); $response = ZoneFunctions::create_records($response, $zone, $req, $o, true); if (empty($o)) { return $response; } $response->code = Response::OK; $response->body = true; $response->log_message = sprintf("Added '%s' to Arpa zone '%s'", $identifier, $zone); $out = true; return $response; }