ntp: check name and return status from NSR_AddSourceByName()

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.
This commit is contained in:
Miroslav Lichvar 2019-12-12 11:58:18 +01:00
parent 2ae008bcee
commit 3763befd62
3 changed files with 22 additions and 6 deletions

View file

@ -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); tx_message->status = htons(STT_INVALIDAF);
break; break;
case NSR_NoSuchSource: case NSR_NoSuchSource:
case NSR_InvalidName:
case NSR_UnresolvedName:
assert(0); assert(0);
break; break;
} }
@ -755,6 +757,8 @@ handle_del_source(CMD_Request *rx_message, CMD_Reply *tx_message)
case NSR_TooManySources: case NSR_TooManySources:
case NSR_AlreadyInUse: case NSR_AlreadyInUse:
case NSR_InvalidAF: case NSR_InvalidAF:
case NSR_InvalidName:
case NSR_UnresolvedName:
assert(0); assert(0);
break; break;
} }

View 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) NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type, SourceParameters *params)
{ {
struct UnresolvedSource *us; struct UnresolvedSource *us;
struct SourcePool *sp; struct SourcePool *sp;
NTP_Remote_Address remote_addr; NTP_Remote_Address remote_addr;
int i;
/* If the name is an IP address, don't bother with full resolving now /* If the name is an IP address, don't bother with full resolving now
or later when trying to replace the source */ or later when trying to replace the source */
if (UTI_StringToIP(name, &remote_addr.ip_addr)) { if (UTI_StringToIP(name, &remote_addr.ip_addr)) {
remote_addr.port = port; remote_addr.port = port;
NSR_AddSource(&remote_addr, type, params); return NSR_AddSource(&remote_addr, type, params);
return; }
/* 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); 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); append_unresolved_source(us);
return NSR_UnresolvedName;
} }
/* ================================================== */ /* ================================================== */

View file

@ -44,7 +44,9 @@ typedef enum {
NSR_NoSuchSource, /* Remove - attempt to remove a source that is not known */ 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_AlreadyInUse, /* AddSource - attempt to add a source that is already known */
NSR_TooManySources, /* AddSource - too many sources already present */ 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; } NSR_Status;
/* Procedure to add a new server or peer source. */ /* 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 /* 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 name instead of address. The name is resolved in exponentially increasing
intervals until it succeeds or fails with a non-temporary error. */ intervals until it succeeds or fails with a non-temporary error. If the
extern void NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type, SourceParameters *params); 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 /* Function type for handlers to be called back when an attempt
* (possibly unsuccessful) to resolve unresolved sources ends */ * (possibly unsuccessful) to resolve unresolved sources ends */