]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
nts: rework update of NTP server address
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 9 Feb 2021 16:40:17 +0000 (17:40 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 11 Feb 2021 08:52:57 +0000 (09:52 +0100)
In the NTS-NTP client instance, maintain a local copy of the NTP address
instead of using a pointer to the NCR's address, which may change at
unexpected times.

Also, change the NNC_CreateInstance() to accept only the NTP port to
make it clear the initial NTP address is the same as the NTS-KE address
and to make it consistent with NNC_ChangeAddress(), which accepts only
one address.

ntp_auth.c
ntp_auth.h
ntp_core.c
nts_ntp_client.c
nts_ntp_client.h
stubs.c
test/unit/ntp_auth.c
test/unit/nts_ntp_client.c

index ee0611cc41cf883f670e903b639f53b8fdce3f22..a5da2f3a7c68f5a83b3ddab0036c68b945d40736 100644 (file)
@@ -161,11 +161,11 @@ NAU_CreateSymmetricInstance(uint32_t key_id)
 /* ================================================== */
 
 NAU_Instance
-NAU_CreateNtsInstance(IPSockAddr *nts_address, const char *name, const IPSockAddr *ntp_address)
+NAU_CreateNtsInstance(IPSockAddr *nts_address, const char *name, uint16_t ntp_port)
 {
   NAU_Instance instance = create_instance(NTP_AUTH_NTS);
 
-  instance->nts = NNC_CreateInstance(nts_address, name, ntp_address);
+  instance->nts = NNC_CreateInstance(nts_address, name, ntp_port);
 
   return instance;
 }
index 4a5deb4e95e13551651001c482a185777e5e885c..9d6c512f980a42164115d6dfd3ab3c4b78772692 100644 (file)
@@ -37,7 +37,7 @@ typedef struct NAU_Instance_Record *NAU_Instance;
 extern NAU_Instance NAU_CreateNoneInstance(void);
 extern NAU_Instance NAU_CreateSymmetricInstance(uint32_t key_id);
 extern NAU_Instance NAU_CreateNtsInstance(IPSockAddr *nts_address, const char *name,
-                                          const IPSockAddr *ntp_address);
+                                          uint16_t ntp_port);
 
 /* Destroy an instance */
 extern void NAU_DestroyInstance(NAU_Instance instance);
index 50146ef4c770ac36721448b3171aabd26a50b7f3..4e5fc59d0eab3fd6f8541f1dc76015d9224a3af1 100644 (file)
@@ -571,7 +571,7 @@ NCR_CreateInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type,
     nts_address.ip_addr = remote_addr->ip_addr;
     nts_address.port = params->nts_port;
 
-    result->auth = NAU_CreateNtsInstance(&nts_address, name, &result->remote_addr);
+    result->auth = NAU_CreateNtsInstance(&nts_address, name, result->remote_addr.port);
   } else if (params->authkey != INACTIVE_AUTHKEY) {
     result->auth = NAU_CreateSymmetricInstance(params->authkey);
   } else {
@@ -703,7 +703,6 @@ NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr, int
   memset(&inst->report, 0, sizeof (inst->report));
   NCR_ResetInstance(inst);
 
-  /* Update the authentication-specific address before NTP address */
   if (!ntp_only)
     NAU_ChangeAddress(inst->auth, &remote_addr->ip_addr);
 
index cbedf0beb93cc482d1a9b4c5f8c4c0b69455d665..ed89edd9f764677f3da16899666c808f6bec0498 100644 (file)
 #define DUMP_IDENTIFIER "NNC0\n"
 
 struct NNC_Instance_Record {
-  /* Pointer to current address of NTP server */
-  const IPSockAddr *ntp_address;
   /* Address of NTS-KE server */
   IPSockAddr nts_address;
   /* Hostname or IP address for certificate verification */
   char *name;
+  /* Address of NTP server (can be negotiated in NTS-KE) */
+  IPSockAddr ntp_address;
 
   NKC_Instance nke;
   SIV_Instance siv;
@@ -114,15 +114,16 @@ reset_instance(NNC_Instance inst)
 /* ================================================== */
 
 NNC_Instance
-NNC_CreateInstance(IPSockAddr *nts_address, const char *name, const IPSockAddr *ntp_address)
+NNC_CreateInstance(IPSockAddr *nts_address, const char *name, uint16_t ntp_port)
 {
   NNC_Instance inst;
 
   inst = MallocNew(struct NNC_Instance_Record);
 
-  inst->ntp_address = ntp_address;
   inst->nts_address = *nts_address;
   inst->name = Strdup(name);
+  inst->ntp_address.ip_addr = nts_address->ip_addr;
+  inst->ntp_address.port = ntp_port;
   inst->siv = NULL;
   inst->nke = NULL;
 
@@ -168,7 +169,7 @@ set_ntp_address(NNC_Instance inst, NTP_Remote_Address *negotiated_address)
 {
   NTP_Remote_Address old_address, new_address;
 
-  old_address = *inst->ntp_address;
+  old_address = inst->ntp_address;
   new_address = *negotiated_address;
 
   if (new_address.ip_addr.family == IPADDR_UNSPEC)
@@ -187,6 +188,8 @@ set_ntp_address(NNC_Instance inst, NTP_Remote_Address *negotiated_address)
     return 0;
   }
 
+  inst->ntp_address = new_address;
+
   return 1;
 }
 
@@ -521,6 +524,7 @@ NNC_ChangeAddress(NNC_Instance inst, IPAddr *address)
   save_cookies(inst);
 
   inst->nts_address.ip_addr = *address;
+  inst->ntp_address.ip_addr = *address;
 
   reset_instance(inst);
 
@@ -557,7 +561,7 @@ save_cookies(NNC_Instance inst)
 
   if (fprintf(f, "%s%s\n%.1f\n%s %d\n%u %d ",
               DUMP_IDENTIFIER, inst->name, context_time,
-              UTI_IPToString(&inst->ntp_address->ip_addr), inst->ntp_address->port,
+              UTI_IPToString(&inst->ntp_address.ip_addr), inst->ntp_address.port,
               inst->context_id, (int)inst->context.algorithm) < 0 ||
       !UTI_BytesToHex(inst->context.s2c.key, inst->context.s2c.length, buf, sizeof (buf)) ||
       fprintf(f, "%s ", buf) < 0 ||
index 18e3357d1d4081dfa6d9bb1f954a4e0206e73214..88287f135e3bcf1c8727b8ffd3e0957cafd6c6f6 100644 (file)
@@ -34,7 +34,7 @@
 typedef struct NNC_Instance_Record *NNC_Instance;
 
 extern NNC_Instance NNC_CreateInstance(IPSockAddr *nts_address, const char *name,
-                                                   const IPSockAddr *ntp_address);
+                                       uint16_t ntp_port);
 extern void NNC_DestroyInstance(NNC_Instance inst);
 extern int NNC_PrepareForAuth(NNC_Instance inst);
 extern int NNC_GenerateRequestAuth(NNC_Instance inst, NTP_Packet *packet,
diff --git a/stubs.c b/stubs.c
index ae41a8390a662788a6d62792006dcff36b30b4b1..02e863620dd7a6919f0388479c54b2a1196b31f6 100644 (file)
--- a/stubs.c
+++ b/stubs.c
@@ -491,7 +491,7 @@ NNS_GenerateResponseAuth(NTP_Packet *request, NTP_PacketInfo *req_info,
 }
 
 NNC_Instance
-NNC_CreateInstance(IPSockAddr *nts_address, const char *name, const IPSockAddr *ntp_address)
+NNC_CreateInstance(IPSockAddr *nts_address, const char *name, uint16_t ntp_port)
 {
   return NULL;
 }
index c83cf29d29f689b7017c6f6211b87cd92692c655..a1a2d537641ae018a09efa932602f1a5992b88a2 100644 (file)
@@ -177,7 +177,7 @@ test_unit(void)
         can_auth_res = can_auth_req;
         break;
       case 2:
-        inst = NAU_CreateNtsInstance(&nts_addr, "test", &nts_addr);
+        inst = NAU_CreateNtsInstance(&nts_addr, "test", 0);
         TEST_CHECK(NAU_IsAuthEnabled(inst));
         TEST_CHECK(NAU_GetSuggestedNtpVersion(inst) == 4);
         mode = NTP_AUTH_NTS;
index 4ee33b0589c3cb6c49b4025c42181ebf88ababa8..2b1e5a7563d946ad1bf967f2cfb96bb433fbb001 100644 (file)
@@ -227,7 +227,7 @@ test_unit(void)
   SCK_GetLoopbackIPAddress(AF_INET, &addr.ip_addr);
   addr.port = 0;
 
-  inst = NNC_CreateInstance(&addr, "test", &addr);
+  inst = NNC_CreateInstance(&addr, "test", 0);
   TEST_CHECK(inst);
 
   for (i = 0; i < 100000; i++) {