]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
cmdmon: merge add server/peer requests
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 4 Dec 2019 10:16:26 +0000 (11:16 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 12 Dec 2019 13:44:03 +0000 (14:44 +0100)
Instead of having two separate requests in the protocol for adding a
server and peer, specify the type of the new source in the request data.

candm.h
client.c
cmdmon.c
pktlength.c

diff --git a/candm.h b/candm.h
index cb7973928996fbe124f2f02a5713317bab21d754..d62875cce1aaddbe70a2a7b6d75dd65d10aeb5c2 100644 (file)
--- a/candm.h
+++ b/candm.h
 #define REQ_ADD_PEER3 61
 #define REQ_SHUTDOWN 62
 #define REQ_ONOFFLINE 63
-#define N_REQUEST_TYPES 64
+#define REQ_ADD_SOURCE 64
+#define N_REQUEST_TYPES 65
 
 /* Structure used to exchange timespecs independent of time_t size */
 typedef struct {
@@ -245,6 +246,10 @@ typedef struct {
   int32_t EOR;
 } REQ_Ac_Check;
 
+/* Source types in NTP source requests */
+#define REQ_ADDSRC_SERVER 1
+#define REQ_ADDSRC_PEER 2
+
 /* Flags used in NTP source requests */
 #define REQ_ADDSRC_ONLINE 0x1
 #define REQ_ADDSRC_AUTOOFFLINE 0x2
@@ -257,6 +262,7 @@ typedef struct {
 #define REQ_ADDSRC_BURST 0x100
 
 typedef struct {
+  uint32_t type;
   IPAddr ip_addr;
   uint32_t port;
   int32_t minpoll;
index 99aaa82a05fab141d7f1a83771f4d420abae179d..bd716a9d2c45109103fd3876cf3cf9eb9a62295f 100644 (file)
--- a/client.c
+++ b/client.c
@@ -1053,13 +1053,27 @@ process_cmd_doffset(CMD_Request *msg, char *line)
 /* ================================================== */
 
 static int
-process_cmd_add_server_or_peer(CMD_Request *msg, char *line)
+process_cmd_add_source(CMD_Request *msg, char *line)
 {
   CPS_NTP_Source data;
   IPAddr ip_addr;
-  int result = 0, status;
-  const char *opt_name;
+  int result = 0, status, type;
+  const char *opt_name, *word;
   
+  msg->command = htons(REQ_ADD_SOURCE);
+
+  word = line;
+  line = CPS_SplitWord(line);
+
+  if (!strcmp(word, "server")) {
+    type = REQ_ADDSRC_SERVER;
+  } else if (!strcmp(word, "peer")) {
+    type = REQ_ADDSRC_PEER;
+  } else {
+    LOG(LOGS_ERR, "Invalid syntax for add command");
+    return 0;
+  }
+
   status = CPS_ParseNTPSourceAdd(line, &data);
   switch (status) {
     case 0:
@@ -1077,6 +1091,7 @@ process_cmd_add_server_or_peer(CMD_Request *msg, char *line)
         break;
       }
 
+      msg->data.ntp_source.type = htonl(type);
       msg->data.ntp_source.port = htonl((unsigned long) data.port);
       UTI_IPHostToNetwork(&ip_addr, &msg->data.ntp_source.ip_addr);
       msg->data.ntp_source.minpoll = htonl(data.params.minpoll);
@@ -1119,24 +1134,6 @@ process_cmd_add_server_or_peer(CMD_Request *msg, char *line)
 
 /* ================================================== */
 
-static int
-process_cmd_add_server(CMD_Request *msg, char *line)
-{
-  msg->command = htons(REQ_ADD_SERVER3);
-  return process_cmd_add_server_or_peer(msg, line);
-}
-
-/* ================================================== */
-
-static int
-process_cmd_add_peer(CMD_Request *msg, char *line)
-{
-  msg->command = htons(REQ_ADD_PEER3);
-  return process_cmd_add_server_or_peer(msg, line);
-}
-
-/* ================================================== */
-
 static int
 process_cmd_delete(CMD_Request *msg, char *line)
 {
@@ -2927,10 +2924,8 @@ process_line(char *line)
   } else if (!strcmp(command, "activity")) {
     do_normal_submit = 0;
     ret = process_cmd_activity(line);
-  } else if (!strcmp(command, "add") && !strncmp(line, "peer", 4)) {
-    do_normal_submit = process_cmd_add_peer(&tx_message, CPS_SplitWord(line));
-  } else if (!strcmp(command, "add") && !strncmp(line, "server", 6)) {
-    do_normal_submit = process_cmd_add_server(&tx_message, CPS_SplitWord(line));
+  } else if (!strcmp(command, "add")) {
+    do_normal_submit = process_cmd_add_source(&tx_message, line);
   } else if (!strcmp(command, "allow")) {
     if (!strncmp(line, "all", 3)) {
       do_normal_submit = process_cmd_allowall(&tx_message, CPS_SplitWord(line));
index ca8195c9ed6d676931febea44fd41227a5b339d6..453d1e1c284685fd60670db18a980e827c5b2e87 100644 (file)
--- a/cmdmon.c
+++ b/cmdmon.c
@@ -132,6 +132,7 @@ static const char permissions[] = {
   PERMIT_AUTH, /* ADD_PEER3 */
   PERMIT_AUTH, /* SHUTDOWN */
   PERMIT_AUTH, /* ONOFFLINE */
+  PERMIT_AUTH, /* ADD_SOURCE */
 };
 
 /* ================================================== */
@@ -675,12 +676,25 @@ handle_cmdaccheck(CMD_Request *rx_message, CMD_Reply *tx_message)
 /* ================================================== */
 
 static void
-handle_add_source(NTP_Source_Type type, CMD_Request *rx_message, CMD_Reply *tx_message)
+handle_add_source(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
   NTP_Remote_Address rem_addr;
+  NTP_Source_Type type;
   SourceParameters params;
   NSR_Status status;
   
+  switch (ntohl(rx_message->data.ntp_source.type)) {
+    case REQ_ADDSRC_SERVER:
+      type = NTP_SERVER;
+      break;
+    case REQ_ADDSRC_PEER:
+      type = NTP_PEER;
+      break;
+    default:
+      tx_message->status = htons(STT_INVALID);
+      return;
+  }
+
   UTI_IPNetworkToHost(&rx_message->data.ntp_source.ip_addr, &rem_addr.ip_addr);
   rem_addr.port = (unsigned short)(ntohl(rx_message->data.ntp_source.port));
   params.minpoll = ntohl(rx_message->data.ntp_source.minpoll);
@@ -1426,12 +1440,8 @@ read_from_cmd_socket(int sock_fd, int event, void *anything)
           handle_cmdaccheck(&rx_message, &tx_message);
           break;
 
-        case REQ_ADD_SERVER3:
-          handle_add_source(NTP_SERVER, &rx_message, &tx_message);
-          break;
-
-        case REQ_ADD_PEER3:
-          handle_add_source(NTP_PEER, &rx_message, &tx_message);
+        case REQ_ADD_SOURCE:
+          handle_add_source(&rx_message, &tx_message);
           break;
 
         case REQ_DEL_SOURCE:
index 3fd4a0b3322b409bfa0db4df91ea45f8d38fc34a..bb2058c5408e6faf640467e55e7224555fa49fbd 100644 (file)
@@ -116,10 +116,11 @@ static const struct request_length request_lengths[] = {
   REQ_LENGTH_ENTRY(ntp_data, ntp_data),         /* NTP_DATA */
   { 0, 0 },                                     /* ADD_SERVER2 */
   { 0, 0 },                                     /* ADD_PEER2 */
-  REQ_LENGTH_ENTRY(ntp_source, null),           /* ADD_SERVER3 */
-  REQ_LENGTH_ENTRY(ntp_source, null),           /* ADD_PEER3 */
+  { 0, 0 },                                     /* ADD_SERVER3 */
+  { 0, 0 },                                     /* ADD_PEER3 */
   REQ_LENGTH_ENTRY(null, null),                 /* SHUTDOWN */
   REQ_LENGTH_ENTRY(null, null),                 /* ONOFFLINE */
+  REQ_LENGTH_ENTRY(ntp_source, null),           /* ADD_SOURCE */
 };
 
 static const uint16_t reply_lengths[] = {