diff --git a/src/DhcpBundle/Controller/DhcpController.php b/src/DhcpBundle/Controller/DhcpController.php index 4156ea8..a65e318 100644 --- a/src/DhcpBundle/Controller/DhcpController.php +++ b/src/DhcpBundle/Controller/DhcpController.php @@ -140,7 +140,7 @@ class DhcpController $output = shell_exec("df -h " . realpath($this->backup_dir) . " | tail -1 | awk '{print $2, $3, $4, $5}'"); if (!$output) { $this->logger->error("Failed to execute disk usage command"); - return null; + return []; } list($total, $used, $available, $percentage) = explode(' ', $output); @@ -217,6 +217,8 @@ class DhcpController * @OA\Property(property="subnet", type="string", description="The name of the subnet"), * @OA\Property(property="next-server", type="string", description="The next server in the subnet"), * @OA\Property(property="boot-file-name", type="string", description="The boot file name for the subnet"), + * @OA\Property(property="DNS", type="string", description="DNS subnet"), + * @OA\Property(property="subnetName", type="string", description="subnetName subnet"), * @OA\Property( * property="reservations", * type="array", @@ -242,6 +244,7 @@ class DhcpController * @OA\Property(property="id", type="integer", example=1), * @OA\Property(property="subnet", type="string", example="192.168.1.0/24"), * @OA\Property(property="next-server", type="string", example="192.168.1.1"), + * @OA\Property(property="DNS", type="string", example="192.168.1.1"), * @OA\Property(property="boot-file-name", type="string", example="pxelinux.0"), * @OA\Property(property="pools", type="array", * @OA\Items( @@ -394,6 +397,8 @@ class DhcpController * @OA\Property(property="mask", type="string", example="255.255.255.0"), * @OA\Property(property="address", type="string", example="192.168.1.0"), * @OA\Property(property="nextServer", type="string", example="192.168.1.1"), + * @OA\Property(property="subnetName", type="string", example="Aula informatica"), + * @OA\Property(property="DNS", type="string", example="192.168.1.1"), * @OA\Property(property="bootFileName", type="string", example="pxelinux.0"), * @OA\Property(property="router", type="string", example="192.168.1.254") * ) @@ -467,6 +472,8 @@ public function addDhcpSubnet(Request $request): JsonResponse $nextServer = isset($input->nextServer) ? htmlspecialchars($input->nextServer) : null; $bootFileName = isset($input->bootFileName) ? htmlspecialchars($input->bootFileName) : null; $router = isset($input->router) ? htmlspecialchars($input->router) : null; + $DNS = isset($input->DNS) ? htmlspecialchars($input->DNS) : null; + $subnetName = isset($input->subnetName) ? htmlspecialchars($input->subnetName) : null; // Calcular el gatewayIP si no se proporciona el router $gatewayIP = $router ?: substr($address, 0, strrpos($address, '.')) . '.1'; @@ -491,21 +498,38 @@ public function addDhcpSubnet(Request $request): JsonResponse try { $response = $this->curlKeaService->executeCurlCommand('config-get'); - $subnetName = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask); + $subnetCIDR = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask); $newSubnet = [ "id" => $subnetId, - "subnet" => $subnetName, + "subnet" => $subnetCIDR, "reservations" => [], "option-data" => [ - [ - "name" => "routers", - "code" => 3, - "data" => $gatewayIP - ] + [ + "name" => "routers", + "code" => 3, + "data" => $gatewayIP + ] ] ]; + // Agregar "user-context" solo si $subnetName tiene valor + if (!empty($subnetName)) { + $newSubnet["user-context"] = [ + "subnetName" => $subnetName + ]; + } + + if (!empty($DNS)) { + $newSubnet["option-data"][] = [ + "name" => "domain-name-servers", + "code" => 6, + "space" => "dhcp4", + "csv-format" => true, + "data" => $DNS + ]; + } + if ($nextServer) { $newSubnet["next-server"] = $nextServer; } @@ -520,7 +544,7 @@ public function addDhcpSubnet(Request $request): JsonResponse $subnets = $response[0]['arguments']['Dhcp4']['subnet4']; - $subnetNameExists = array_reduce($subnets, fn($exists, $subnetElement) => $exists || ($subnetElement['subnet'] === $subnetName), false); + $subnetNameExists = array_reduce($subnets, fn($exists, $subnetElement) => $exists || ($subnetElement['subnet'] === $subnetCIDR), false); $subnetIdExists = array_reduce($subnets, fn($exists, $subnetElement) => $exists || ($subnetElement['id'] === $subnetId), false); if ($subnetNameExists) { @@ -529,10 +553,10 @@ public function addDhcpSubnet(Request $request): JsonResponse 'operation' => $operation, 'component' => $component, 'http_code' => Response::HTTP_BAD_REQUEST, - 'desc' => "La subred con la IP '$subnetName' ya existe." + 'desc' => "La subred con la IP '$subnetCIDR' ya existe." ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); - return new JsonResponse(['error' => "La subred con la ip '$subnetName' ya existe."], Response::HTTP_BAD_REQUEST); + return new JsonResponse(['error' => "La subred con la ip '$subnetCIDR' ya existe."], Response::HTTP_BAD_REQUEST); } elseif ($subnetIdExists) { $this->logger->error(json_encode([ 'severity' => 'ERROR', @@ -866,6 +890,8 @@ public function addDhcpSubnet(Request $request): JsonResponse * type="object", * @OA\Property(property="mask", type="string"), * @OA\Property(property="address", type="string"), + * @OA\Property(property="DNS", type="string"), + * @OA\Property(property="subnetName", type="string"), * @OA\Property(property="router", type="string", example="192.168.1.254"), * @OA\Property(property="nextServer", type="string"), * @OA\Property(property="bootFileName", type="string") @@ -920,6 +946,8 @@ public function modifyDhcpSubnet(Request $request): JsonResponse $nextServer = isset($input->nextServer) ? htmlspecialchars($input->nextServer) : null; $bootFileName = isset($input->bootFileName) ? htmlspecialchars($input->bootFileName) : null; $router = isset($input->router) ? htmlspecialchars($input->router) : null; + $DNS = isset($input->DNS) ? htmlspecialchars($input->DNS) : null; + $subnetName = isset($input->subnetName) ? htmlspecialchars($input->subnetName) : null; // Calcular el gateway añadiendo .1 al final de la subred si no se proporciona el router $gateway = $router ?: preg_replace('/\d+$/', '1', $address); @@ -948,7 +976,7 @@ public function modifyDhcpSubnet(Request $request): JsonResponse try { $response = $this->curlKeaService->executeCurlCommand('config-get'); - $subnetName = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask); + $subnetCIDR = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask); if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) { $errorText = "No hay subredes definidas"; @@ -982,7 +1010,7 @@ public function modifyDhcpSubnet(Request $request): JsonResponse // Modificar la subred existente $newSubnet = [ "id" => $subnetId, - "subnet" => $subnetName, + "subnet" => $subnetCIDR, "reservations" => $existingSubnet['reservations'], // Mantener las reservas existentes "option-data" => [ [ @@ -993,6 +1021,24 @@ public function modifyDhcpSubnet(Request $request): JsonResponse ] ]; + // Agregar "user-context" solo si $subnetName tiene valor + if (!empty($subnetName)) { + $newSubnet["user-context"] = [ + "subnetName" => $subnetName + ]; + } + + // Agregar DNS si está presente + if (!empty($DNS)) { + $newSubnet["option-data"][] = [ + "name" => "domain-name-servers", + "code" => 23, + "space" => "dhcp4", + "csv-format" => true, + "data" => $DNS + ]; + } + // Añadir next-server si está presente, o eliminarlo de las reservas si no está if ($nextServer) { $newSubnet["next-server"] = $nextServer; @@ -1964,6 +2010,8 @@ public function updateDhcpHost(Request $request, $subnetId): JsonResponse ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); return new JsonResponse(['error' => $errorText], Response::HTTP_INTERNAL_SERVER_ERROR); } + + return new JsonResponse(['error' => 'Unexpected error occurred'], Response::HTTP_INTERNAL_SERVER_ERROR); }