From 094e87b0dcaa8d7dde4539b5d2299fe96da4be7c Mon Sep 17 00:00:00 2001
From: Sean Bright <sean.bright@gmail.com>
Date: Mon, 13 Jan 2020 17:37:00 -0500
Subject: [PATCH] res_realtime: Fix 'realtime update2' argument handling
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The change in 9b99ef50b5d01ee8111d26efa7b926bdfaf3f980 updated the
syntax of the 'realtime update2' CLI command but did not correctly
update the calls to ast_update2_realtime().

The issue this addresses was originally opened because we aren't
allowing a SQL NULL to be set as part of the update, but this is a
limitation of the Realtime API and is not a bug.

Additionally, this patch:

* Corrects the example in the command documentation to reflect
  'update2' instead of 'update.'

* Fixes the leading spacing of the command documentation.

* Checks that the required 'NULL' literal argument is present where we
  expect it to be.

ASTERISK-21794 #close
Reported by: Cédric Bassaget

Change-Id: Idda63a5dc50d5f9bcb34c27ea3238d90f733b2cd
---
 res/res_realtime.c | 56 ++++++++++++++++++++++++++++++----------------
 1 file changed, 37 insertions(+), 19 deletions(-)

diff --git a/res/res_realtime.c b/res/res_realtime.c
index 9a8e6482c2..d57c2d8ccc 100644
--- a/res/res_realtime.c
+++ b/res/res_realtime.c
@@ -122,32 +122,50 @@ static char *cli_realtime_update2(struct ast_cli_entry *e, int cmd, struct ast_c
 		e->command = "realtime update2";
 		e->usage =
 			"Usage: realtime update2 <family> <colmatch> <valuematch> [... <colmatch5> <valuematch5>] NULL <colupdate> <newvalue>\n"
-			"   Update a single variable, requiring one or more fields to match using the\n"
-			"   RealTime driver.  You must supply a family name, a column to update, a new\n"
-			"   value, and at least one column and value to match.\n"
-			"   Ex: realtime update sippeers name bobsphone ipaddr 127.0.0.1 NULL port 4343\n"
-			"   will execute SQL as\n"
-			"   UPDATE sippeers SET port='4343' WHERE name='bobsphone' and ipaddr='127.0.0.1'\n";
+			"       Update a single variable, requiring one or more fields to match using the\n"
+			"       RealTime driver. You must supply a family name, a column to update, a new\n"
+			"       value, and at least one column and value to match.\n"
+			"       Ex: realtime update2 sippeers name bobsphone ipaddr 127.0.0.1 NULL port 4343\n"
+			"       will execute SQL as\n"
+			"       UPDATE sippeers SET port='4343' WHERE name='bobsphone' and ipaddr='127.0.0.1'\n";
 		return NULL;
 	case CLI_GENERATE:
 		return NULL;
 	}
 
-	if (a->argc < 7)
+	/* Make sure we have the right number of arguments and that the required literal NULL
+	   is present */
+	if (a->argc < 8 || a->argc > 16 || a->argc % 2
+	   || strcmp(a->argv[a->argc - 3], "NULL")) {
 		return CLI_SHOWUSAGE;
+	}
 
-	if (a->argc == 7) {
-		res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], SENTINEL, a->argv[5], a->argv[6], SENTINEL);
-	} else if (a->argc == 9) {
-		res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL, a->argv[7], a->argv[8], SENTINEL);
-	} else if (a->argc == 11) {
-		res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], SENTINEL, a->argv[9], a->argv[10], SENTINEL);
-	} else if (a->argc == 13) {
-		res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], SENTINEL, a->argv[11], a->argv[12], SENTINEL);
-	} else if (a->argc == 15) {
-		res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], a->argv[11], a->argv[12], SENTINEL, a->argv[13], a->argv[14], SENTINEL);
-	} else {
-		return CLI_SHOWUSAGE;
+	if (a->argc == 8) {
+		res = ast_update2_realtime(
+			a->argv[2], a->argv[3], a->argv[4], SENTINEL,
+			a->argv[6], a->argv[7], SENTINEL);
+	} else if (a->argc == 10) {
+		res = ast_update2_realtime(
+			a->argv[2], a->argv[3], a->argv[4], a->argv[5],
+			a->argv[6], SENTINEL,
+			a->argv[8], a->argv[9], SENTINEL);
+	} else if (a->argc == 12) {
+		res = ast_update2_realtime(
+			a->argv[2], a->argv[3], a->argv[4], a->argv[5],
+			a->argv[6], a->argv[7], a->argv[8], SENTINEL,
+			a->argv[10], a->argv[11], SENTINEL);
+	} else if (a->argc == 14) {
+		res = ast_update2_realtime(
+			a->argv[2], a->argv[3], a->argv[4], a->argv[5],
+			a->argv[6], a->argv[7], a->argv[8], a->argv[9],
+			a->argv[10], SENTINEL,
+			a->argv[12], a->argv[13], SENTINEL);
+	} else if (a->argc == 16) {
+		res = ast_update2_realtime(
+			a->argv[2], a->argv[3], a->argv[4], a->argv[5],
+			a->argv[6], a->argv[7], a->argv[8], a->argv[9],
+			a->argv[10], a->argv[11], a->argv[12], SENTINEL,
+			a->argv[14], a->argv[15], SENTINEL);
 	}
 
 	if (res < 0) {