]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
ntp: check name and return status from NSR_AddSourceByName()
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 12 Dec 2019 10:58:18 +0000 (11:58 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 12 Dec 2019 13:44:03 +0000 (14:44 +0100)
Return an error status when the name is not printable or contains a
space (don't bother with full hostname validation). If the name is an
address, return the same status as NSR_AddSource(). Otherwise, return a
"not resolved yet" status.

cmdmon.c
ntp_sources.c
ntp_sources.h

index 9b7afe11f34eca53a490baa384312ecdb0afe383..ca8195c9ed6d676931febea44fd41227a5b339d6 100644 (file)
--- a/cmdmon.c
+++ b/cmdmon.c
@@ -729,6 +729,8 @@ handle_add_source(NTP_Source_Type type, CMD_Request *rx_message, CMD_Reply *tx_m
       tx_message->status = htons(STT_INVALIDAF);
       break;
     case NSR_NoSuchSource:
+    case NSR_InvalidName:
+    case NSR_UnresolvedName:
       assert(0);
       break;
   }
@@ -755,6 +757,8 @@ handle_del_source(CMD_Request *rx_message, CMD_Reply *tx_message)
     case NSR_TooManySources:
     case NSR_AlreadyInUse:
     case NSR_InvalidAF:
+    case NSR_InvalidName:
+    case NSR_UnresolvedName:
       assert(0);
       break;
   }
index 0f9eabaaef06701b349ab726ca5fbdeaea3f588d..2b300dcc8af8401de85d7215c30edff81dd58cdb 100644 (file)
@@ -505,19 +505,25 @@ NSR_AddSource(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourceParam
 
 /* ================================================== */
 
-void
+NSR_Status
 NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type, SourceParameters *params)
 {
   struct UnresolvedSource *us;
   struct SourcePool *sp;
   NTP_Remote_Address remote_addr;
+  int i;
 
   /* If the name is an IP address, don't bother with full resolving now
      or later when trying to replace the source */
   if (UTI_StringToIP(name, &remote_addr.ip_addr)) {
     remote_addr.port = port;
-    NSR_AddSource(&remote_addr, type, params);
-    return;
+    return NSR_AddSource(&remote_addr, type, params);
+  }
+
+  /* Make sure the name is at least printable and has no spaces */
+  for (i = 0; name[i] != '\0'; i++) {
+    if (!isgraph(name[i]))
+      return NSR_InvalidName;
   }
 
   us = MallocNew(struct UnresolvedSource);
@@ -540,6 +546,8 @@ NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type, Source
   }
 
   append_unresolved_source(us);
+
+  return NSR_UnresolvedName;
 }
 
 /* ================================================== */
index 16b62be1c79ada8ea2c9612bcd25f2114a65206b..4214233b29484b34585634ab1e62625d6a6b5455 100644 (file)
@@ -44,7 +44,9 @@ typedef enum {
   NSR_NoSuchSource, /* Remove - attempt to remove a source that is not known */
   NSR_AlreadyInUse, /* AddSource - attempt to add a source that is already known */ 
   NSR_TooManySources, /* AddSource - too many sources already present */
-  NSR_InvalidAF /* AddSource - attempt to add a source with invalid address family */
+  NSR_InvalidAF, /* AddSource - attempt to add a source with invalid address family */
+  NSR_InvalidName, /* AddSourceByName - attempt to add a source with invalid name */
+  NSR_UnresolvedName, /* AddSourceByName - name will be resolved later */
 } NSR_Status;
 
 /* Procedure to add a new server or peer source. */
@@ -52,8 +54,10 @@ extern NSR_Status NSR_AddSource(NTP_Remote_Address *remote_addr, NTP_Source_Type
 
 /* Procedure to add a new server, peer source, or pool of servers specified by
    name instead of address.  The name is resolved in exponentially increasing
-   intervals until it succeeds or fails with a non-temporary error. */
-extern void NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type, SourceParameters *params);
+   intervals until it succeeds or fails with a non-temporary error.  If the
+   name is an address, it is equivalent to NSR_AddSource(). */
+extern NSR_Status NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type,
+                                      SourceParameters *params);
 
 /* Function type for handlers to be called back when an attempt
  * (possibly unsuccessful) to resolve unresolved sources ends */