bindcar 0.6.0

HTTP REST API for managing BIND9 zones via rndc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Copyright (c) 2025 Erick Bourgeois, firestoned
// SPDX-License-Identifier: MIT

//! DNS record management API handlers
//!
//! This module implements HTTP handlers for individual DNS record operations:
//! - Adding records to existing zones
//! - Removing records from existing zones
//! - Updating existing records
//!
//! All operations use nsupdate for dynamic DNS updates with TSIG authentication.

use axum::{
    extract::{Path, State},
    http::StatusCode,
    Json,
};
use serde::{Deserialize, Serialize};
use tracing::{debug, error, info};
use utoipa::ToSchema;

use crate::{
    metrics, rndc_parser, rndc_types,
    types::{ApiError, AppState},
};

/// Request to add a new DNS record
#[derive(Debug, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct AddRecordRequest {
    /// Record name (e.g., "www", "@" for apex)
    pub name: String,

    /// Record type (e.g., "A", "AAAA", "CNAME", "MX", "TXT")
    #[serde(rename = "type")]
    pub record_type: String,

    /// Record value (e.g., "192.0.2.1" for A record)
    pub value: String,

    /// TTL in seconds (default: 3600)
    #[serde(default = "default_ttl")]
    pub ttl: u32,

    /// Priority (for MX and SRV records)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub priority: Option<u16>,
}

/// Request to remove a DNS record
#[derive(Debug, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct RemoveRecordRequest {
    /// Record name (e.g., "www", "@" for apex)
    pub name: String,

    /// Record type (e.g., "A", "AAAA", "CNAME")
    #[serde(rename = "type")]
    pub record_type: String,

    /// Record value to remove (optional - if omitted, removes all records of this type)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
}

/// Request to update a DNS record
#[derive(Debug, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct UpdateRecordRequest {
    /// Record name (e.g., "www", "@" for apex)
    pub name: String,

    /// Record type (e.g., "A", "AAAA", "CNAME")
    #[serde(rename = "type")]
    pub record_type: String,

    /// Current record value
    pub current_value: String,

    /// New record value
    pub new_value: String,

    /// TTL in seconds (default: 3600)
    #[serde(default = "default_ttl")]
    pub ttl: u32,

    /// Priority (for MX and SRV records)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub priority: Option<u16>,
}

/// Response from record operations
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct RecordResponse {
    pub success: bool,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<serde_json::Value>,
}

fn default_ttl() -> u32 {
    3600
}

/// Supported DNS record types
const VALID_RECORD_TYPES: &[&str] = &["A", "AAAA", "CNAME", "MX", "TXT", "NS", "PTR", "SRV", "CAA"];

/// Validate that a zone exists and supports dynamic updates
///
/// # Arguments
///
/// * `state` - Application state
/// * `zone_name` - Zone name to validate
///
/// # Returns
///
/// Ok if zone exists and has allow-update configured, Err otherwise
async fn validate_zone_for_updates(state: &AppState, zone_name: &str) -> Result<(), ApiError> {
    // Validate zone name is not empty
    if zone_name.is_empty() {
        return Err(ApiError::InvalidRequest(
            "Zone name cannot be empty".to_string(),
        ));
    }

    // Get zone configuration via RNDC
    let zone_config_output = state.rndc.showzone(zone_name).await.map_err(|e| {
        if e.to_string().contains("not found") {
            ApiError::ZoneNotFound(zone_name.to_string())
        } else {
            ApiError::RndcError(e.to_string())
        }
    })?;

    // Parse zone configuration
    let zone_config = rndc_parser::parse_showzone(&zone_config_output).map_err(|e| {
        ApiError::InternalError(format!("Failed to parse zone configuration: {}", e))
    })?;

    // Zone must be primary type
    if zone_config.zone_type != rndc_types::ZoneType::Primary {
        return Err(ApiError::DynamicUpdatesNotEnabled(format!(
            "Zone {} is {} type. Dynamic updates only supported on primary zones",
            zone_name,
            zone_config.zone_type.as_str()
        )));
    }

    // Zone must have allow-update configured
    if zone_config.allow_update.is_none() && zone_config.allow_update_raw.is_none() {
        return Err(ApiError::DynamicUpdatesNotEnabled(format!(
            "Zone {} does not have allow-update configured. \
            Create zone with updateKeyName or modify zone to enable dynamic updates",
            zone_name
        )));
    }

    Ok(())
}

/// Validate DNS record type
fn validate_record_type(record_type: &str) -> Result<(), ApiError> {
    let upper = record_type.to_uppercase();

    if !VALID_RECORD_TYPES.contains(&upper.as_str()) {
        return Err(ApiError::InvalidRecord(format!(
            "Invalid record type: {}. Supported types: {:?}",
            record_type, VALID_RECORD_TYPES
        )));
    }

    Ok(())
}

/// Validate DNS record value based on type
fn validate_record_value(record_type: &str, value: &str) -> Result<(), ApiError> {
    // Value cannot be empty
    if value.is_empty() {
        return Err(ApiError::InvalidRecord(
            "Record value cannot be empty".to_string(),
        ));
    }

    match record_type.to_uppercase().as_str() {
        "A" => {
            // Validate IPv4 address
            value
                .parse::<std::net::Ipv4Addr>()
                .map_err(|_| ApiError::InvalidRecord(format!("Invalid IPv4 address: {}", value)))?;
        }
        "AAAA" => {
            // Validate IPv6 address
            value
                .parse::<std::net::Ipv6Addr>()
                .map_err(|_| ApiError::InvalidRecord(format!("Invalid IPv6 address: {}", value)))?;
        }
        "CNAME" | "NS" | "PTR" | "MX" => {
            // Must be FQDN with trailing dot
            if !value.ends_with('.') {
                return Err(ApiError::InvalidRecord(format!(
                    "{} record value must be a fully qualified domain name ending with '.': {}",
                    record_type, value
                )));
            }
        }
        "TXT" | "CAA" | "SRV" => {
            // Any non-empty string is valid
        }
        _ => {}
    }

    Ok(())
}

/// Normalize record name to FQDN
///
/// # Arguments
///
/// * `name` - Record name (may be relative, @, or FQDN)
/// * `zone` - Zone name
///
/// # Returns
///
/// Fully qualified domain name with trailing dot
fn normalize_record_name(name: &str, zone: &str) -> String {
    if name == "@" {
        // Apex record - use zone name
        format!("{}.", zone)
    } else if name.ends_with('.') {
        // Already a FQDN
        name.to_string()
    } else if name.contains('.') && name.ends_with(zone) {
        // FQDN without trailing dot
        format!("{}.", name)
    } else {
        // Relative name - append zone
        format!("{}.{}.", name, zone)
    }
}

/// Add a DNS record to an existing zone
#[utoipa::path(
    post,
    path = "/api/v1/zones/{zone_name}/records",
    request_body = AddRecordRequest,
    params(
        ("zone_name" = String, Path, description = "Zone name")
    ),
    responses(
        (status = 201, description = "Record added successfully", body = RecordResponse),
        (status = 400, description = "Invalid request or zone not configured for updates"),
        (status = 404, description = "Zone not found"),
        (status = 500, description = "Update failed"),
    ),
    tag = "records"
)]
pub async fn add_record(
    State(state): State<AppState>,
    Path(zone_name): Path<String>,
    Json(request): Json<AddRecordRequest>,
) -> Result<(StatusCode, Json<RecordResponse>), ApiError> {
    info!(
        "Adding record to zone {}: {} {} {} (TTL: {})",
        zone_name, request.name, request.record_type, request.value, request.ttl
    );

    // Early return pattern: validate all prerequisites
    validate_zone_for_updates(&state, &zone_name).await?;
    validate_record_type(&request.record_type)?;
    validate_record_value(&request.record_type, &request.value)?;

    // Normalize record name to FQDN
    let fqdn = normalize_record_name(&request.name, &zone_name);

    debug!("Normalized record name: {} -> {}", request.name, fqdn);

    // For MX and SRV records, prepend priority to value
    let value_with_priority = if let Some(priority) = request.priority {
        if request.record_type.to_uppercase() == "MX" || request.record_type.to_uppercase() == "SRV"
        {
            format!("{} {}", priority, request.value)
        } else {
            request.value.clone()
        }
    } else {
        request.value.clone()
    };

    // Execute nsupdate
    let _output = state
        .nsupdate
        .add_record(
            &zone_name,
            &fqdn,
            request.ttl,
            &request.record_type,
            &value_with_priority,
        )
        .await
        .map_err(|e| {
            error!("nsupdate add failed: {}", e);
            metrics::record_record_operation("add", false);
            ApiError::NsupdateError(format!("Failed to add record: {}", e))
        })?;

    info!("Record added successfully to zone {}", zone_name);
    metrics::record_record_operation("add", true);

    Ok((
        StatusCode::CREATED,
        Json(RecordResponse {
            success: true,
            message: format!("Record added to zone {}", zone_name),
            details: Some(serde_json::json!({
                "zone": zone_name,
                "record": {
                    "name": request.name,
                    "type": request.record_type,
                    "value": request.value,
                    "ttl": request.ttl,
                }
            })),
        }),
    ))
}

/// Remove a DNS record from an existing zone
#[utoipa::path(
    delete,
    path = "/api/v1/zones/{zone_name}/records",
    request_body = RemoveRecordRequest,
    params(
        ("zone_name" = String, Path, description = "Zone name")
    ),
    responses(
        (status = 200, description = "Record removed successfully", body = RecordResponse),
        (status = 400, description = "Invalid request or zone not configured for updates"),
        (status = 404, description = "Zone not found"),
        (status = 500, description = "Update failed"),
    ),
    tag = "records"
)]
pub async fn remove_record(
    State(state): State<AppState>,
    Path(zone_name): Path<String>,
    Json(request): Json<RemoveRecordRequest>,
) -> Result<Json<RecordResponse>, ApiError> {
    info!(
        "Removing record from zone {}: {} {} {:?}",
        zone_name, request.name, request.record_type, request.value
    );

    // Early return pattern: validate all prerequisites
    validate_zone_for_updates(&state, &zone_name).await?;
    validate_record_type(&request.record_type)?;

    // Validate value if provided
    if let Some(ref value) = request.value {
        validate_record_value(&request.record_type, value)?;
    }

    // Normalize record name to FQDN
    let fqdn = normalize_record_name(&request.name, &zone_name);

    debug!("Normalized record name: {} -> {}", request.name, fqdn);

    // Execute nsupdate
    let value_str = request.value.as_deref().unwrap_or("");
    let _output = state
        .nsupdate
        .remove_record(&zone_name, &fqdn, &request.record_type, value_str)
        .await
        .map_err(|e| {
            error!("nsupdate remove failed: {}", e);
            metrics::record_record_operation("remove", false);
            ApiError::NsupdateError(format!("Failed to remove record: {}", e))
        })?;

    info!("Record removed successfully from zone {}", zone_name);
    metrics::record_record_operation("remove", true);

    Ok(Json(RecordResponse {
        success: true,
        message: format!("Record removed from zone {}", zone_name),
        details: Some(serde_json::json!({
            "zone": zone_name,
            "record": {
                "name": request.name,
                "type": request.record_type,
                "value": request.value,
            }
        })),
    }))
}

/// Update a DNS record in an existing zone
#[utoipa::path(
    put,
    path = "/api/v1/zones/{zone_name}/records",
    request_body = UpdateRecordRequest,
    params(
        ("zone_name" = String, Path, description = "Zone name")
    ),
    responses(
        (status = 200, description = "Record updated successfully", body = RecordResponse),
        (status = 400, description = "Invalid request or zone not configured for updates"),
        (status = 404, description = "Zone not found"),
        (status = 500, description = "Update failed"),
    ),
    tag = "records"
)]
pub async fn update_record(
    State(state): State<AppState>,
    Path(zone_name): Path<String>,
    Json(request): Json<UpdateRecordRequest>,
) -> Result<Json<RecordResponse>, ApiError> {
    info!(
        "Updating record in zone {}: {} {} from {} to {} (TTL: {})",
        zone_name,
        request.name,
        request.record_type,
        request.current_value,
        request.new_value,
        request.ttl
    );

    // Early return pattern: validate all prerequisites
    validate_zone_for_updates(&state, &zone_name).await?;
    validate_record_type(&request.record_type)?;
    validate_record_value(&request.record_type, &request.current_value)?;
    validate_record_value(&request.record_type, &request.new_value)?;

    // Normalize record name to FQDN
    let fqdn = normalize_record_name(&request.name, &zone_name);

    debug!("Normalized record name: {} -> {}", request.name, fqdn);

    // For MX and SRV records, prepend priority to values
    let (current_with_priority, new_with_priority) = if let Some(priority) = request.priority {
        if request.record_type.to_uppercase() == "MX" || request.record_type.to_uppercase() == "SRV"
        {
            (
                format!("{} {}", priority, request.current_value),
                format!("{} {}", priority, request.new_value),
            )
        } else {
            (request.current_value.clone(), request.new_value.clone())
        }
    } else {
        (request.current_value.clone(), request.new_value.clone())
    };

    // Execute nsupdate
    let _output = state
        .nsupdate
        .update_record(
            &zone_name,
            &fqdn,
            request.ttl,
            &request.record_type,
            &current_with_priority,
            &new_with_priority,
        )
        .await
        .map_err(|e| {
            error!("nsupdate update failed: {}", e);
            metrics::record_record_operation("update", false);
            ApiError::NsupdateError(format!("Failed to update record: {}", e))
        })?;

    info!("Record updated successfully in zone {}", zone_name);
    metrics::record_record_operation("update", true);

    Ok(Json(RecordResponse {
        success: true,
        message: format!("Record updated in zone {}", zone_name),
        details: Some(serde_json::json!({
            "zone": zone_name,
            "record": {
                "name": request.name,
                "type": request.record_type,
                "currentValue": request.current_value,
                "newValue": request.new_value,
                "ttl": request.ttl,
            }
        })),
    }))
}