File pacemaker#3379-0001-Low-tools-Fix-argument-validation-for-crm_attribute-.patch of Package pacemaker.34780

From 88d94635bc1d1387863d7454ec99c6de70abf439 Mon Sep 17 00:00:00 2001
From: Chris Lumens <clumens@redhat.com>
Date: Thu, 29 Feb 2024 15:50:04 -0500
Subject: [PATCH] Low: tools: Fix argument validation for crm_attribute update.

Previously (perhaps as far back as when crm_attribute was introduced) we
haven't done any validation on whether an attribute name was given along
with the -v option specifying the new attribute value.  Instead we just
asserted.

Instead, check that a name or pattern was given and if not, display an
error message.  The other two asserts can go, as well.  options.type
will always be set by the call to set_type(), and options.attr_value
will be set if -v was given on the command line.

Fixes T765
---
 tools/crm_attribute.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

Index: pacemaker-2.1.2+20211124.ada5c3b36/tools/crm_attribute.c
===================================================================
--- pacemaker-2.1.2+20211124.ada5c3b36.orig/tools/crm_attribute.c
+++ pacemaker-2.1.2+20211124.ada5c3b36/tools/crm_attribute.c
@@ -172,7 +172,7 @@ static GOptionEntry selecting_entries[]
 
 static GOptionEntry command_entries[] = {
     { "delete", 'D', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, delete_cb,
-      "Delete the attribute/option",
+      "Delete the attribute/option (with -n or -P)",
       NULL
     },
 
@@ -182,7 +182,7 @@ static GOptionEntry command_entries[] =
     },
 
     { "update", 'v', 0, G_OPTION_ARG_CALLBACK, update_cb,
-      "Update the value of the attribute/option",
+      "Update the value of the attribute/option (with -n or -P)",
       "VALUE"
     },
 
@@ -253,6 +253,14 @@ static GOptionEntry deprecated_entries[]
     { NULL }
 };
 
+static bool
+update_used_correctly(void)
+{
+    return (options.command != 'v')
+           || (options.attr_name != NULL)
+           || (options.attr_pattern != NULL);
+}
+
 static GOptionContext *
 build_arg_context(pcmk__common_args_t *args, GOptionGroup **group) {
     GOptionContext *context = NULL;
@@ -424,6 +432,13 @@ main(int argc, char **argv)
         goto done;
     }
 
+    if (!update_used_correctly()) {
+        exit_code = CRM_EX_USAGE;
+        g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
+                    "Error: must specify attribute name or pattern to update");
+        goto done;
+    }
+
     if (options.attr_pattern) {
         if (((options.command != 'v') && (options.command != 'D'))
             || !pcmk__str_eq(options.type, XML_CIB_TAG_STATUS, pcmk__str_casei)) {
openSUSE Build Service is sponsored by