diff --git a/cmdmon.c b/cmdmon.c index 9b7afe1..ca8195c 100644 --- 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; } diff --git a/ntp_sources.c b/ntp_sources.c index 0f9eaba..2b300dc 100644 --- a/ntp_sources.c +++ b/ntp_sources.c @@ -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; } /* ================================================== */ diff --git a/ntp_sources.h b/ntp_sources.h index 16b62be..4214233 100644 --- a/ntp_sources.h +++ b/ntp_sources.h @@ -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 */