/*  Copyright (C) CZ.NIC, z.s.p.o. and contributors
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  For more information, see <https://www.knot-dns.cz/>
 */

#include <assert.h>

#include "knot/common/log.h"
#include "knot/conf/conf.h"
#include "knot/dnssec/key-events.h"
#include "knot/dnssec/zone-events.h"
#include "knot/updates/apply.h"
#include "knot/zone/zone.h"
#include "libknot/errcode.h"

static void log_dnssec_next(const knot_dname_t *zone, knot_time_t refresh_at)
{
	char time_str[64] = { 0 };
	struct tm time_gm = { 0 };
	time_t refresh = refresh_at;
	localtime_r(&refresh, &time_gm);
	strftime(time_str, sizeof(time_str), KNOT_LOG_TIME_FORMAT, &time_gm);
	if (refresh_at == 0) {
		log_zone_warning(zone, "DNSSEC, next signing not scheduled");
	} else {
		log_zone_info(zone, "DNSSEC, next signing at %s", time_str);
	}
}

void event_dnssec_reschedule(conf_t *conf, zone_t *zone,
			     const zone_sign_reschedule_t *refresh, bool zone_changed)
{
	time_t now = time(NULL);
	time_t ignore = -1;
	knot_time_t refresh_at = refresh->next_sign;

	refresh_at = knot_time_min(refresh_at, refresh->next_rollover);
	refresh_at = knot_time_min(refresh_at, refresh->next_nsec3resalt);

	log_dnssec_next(zone->name, (time_t)refresh_at);

	if (refresh->plan_ds_check) {
		zone->timers->next_ds_check = now;
		zone->timers->flags |= TIMERS_MODIFIED;
	}

	unsigned jitter = dnskey_sync_jitter(conf, zone);
	zone_events_schedule_at(zone,
		ZONE_EVENT_DNSSEC, refresh_at ? (time_t)refresh_at : ignore,
		ZONE_EVENT_DS_CHECK, refresh->plan_ds_check ? now : ignore,
		ZONE_EVENT_DNSKEY_SYNC, refresh->plan_dnskey_sync ? now + jitter : ignore
	);
	if (zone_changed) {
		zone_schedule_notify(conf, zone, 0);
	}
}

int event_dnssec(conf_t *conf, zone_t *zone)
{
	assert(zone);

	if (zone_contents_is_empty(zone->contents)) {
		return KNOT_EEMPTYZONE;
	}

	zone_sign_reschedule_t resch = { 0 };
	zone_sign_roll_flags_t r_flags = KEY_ROLL_ALLOW_ALL;
	int sign_flags = 0;
	bool zone_changed = false;

	if (zone_get_flag(zone, ZONE_FORCE_RESIGN, true)) {
		log_zone_info(zone->name, "DNSSEC, dropping previous "
		              "signatures, re-signing zone");
		sign_flags = ZONE_SIGN_DROP_SIGNATURES;
	} else {
		log_zone_info(zone->name, "DNSSEC, signing zone");
		sign_flags = 0;
	}

	if (zone_get_flag(zone, ZONE_FORCE_KSK_ROLL, true)) {
		r_flags |= KEY_ROLL_FORCE_KSK_ROLL;
	}
	if (zone_get_flag(zone, ZONE_FORCE_ZSK_ROLL, true)) {
		r_flags |= KEY_ROLL_FORCE_ZSK_ROLL;
	}

	zone_update_t up;
	int ret = zone_update_init(&up, zone, UPDATE_INCREMENTAL | UPDATE_NO_CHSET | UPDATE_EVREQ);
	if (ret != KNOT_EOK) {
		return ret;
	}

	ret = knot_dnssec_zone_sign(&up, conf, sign_flags, r_flags, 0, &resch);
	if (ret != KNOT_EOK) {
		goto done;
	}

	zone_changed = !zone_update_no_change(&up);

	ret = zone_update_commit(conf, &up);
	if (ret != KNOT_EOK) {
		goto done;
	}

done:
	// Schedule dependent events
	event_dnssec_reschedule(conf, zone, &resch, zone_changed);

	if (ret != KNOT_EOK) {
		zone_update_clear(&up);
		zone_update_error(conf, zone);
	}
	return ret;
}
