]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
move 'check_for_invalid_chars' helper to common code
authorMichal Rakowski <michal.rakowski@baculasystems.com>
Thu, 18 Nov 2021 12:28:07 +0000 (13:28 +0100)
committerEric Bollengier <eric@baculasystems.com>
Thu, 14 Sep 2023 11:56:56 +0000 (13:56 +0200)
bacula/src/lib/edit.c
bacula/src/lib/protos.h

index 5a19b305c43c28ec7d4f32cbb75b9b636896fb2c..08112582092b81556f3e4972e7fab7d5e6cbabbd 100644 (file)
@@ -546,7 +546,56 @@ bool is_name_valid(const char *name, POOLMEM **msg, const char *accept)
    return true;
 }
 
+/* Small helper method, so far used only by the PostgreSQL and MySQL plugins
+ * to check if name provided by the user can be used.
+ * Checks if given string does not contain suspicious characters, as well as
+ * informs the called if it's already double-quoted or not, to know if it needs
+ * some further processing.
+ * @ret false if no invalid character is found
+ * @ret true if some invalid character is found ('err' is filed with message in that case)
+ */
+bool check_for_invalid_chars(const char *str, POOLMEM **err, bool *quote_needed)
+{
+   *quote_needed = true;
+   int len = strlen(str);
+   for (int i=0; i<len; i++) {
+      switch (str[i]) {
+         case '\\':
+            pm_strcpy(err, "Found invalid \"\\\" character");
+            return true;
+         case '\'':
+            pm_strcpy(err, "Found invalid \"\'\" character");
+            return true;
+         case '$':
+            pm_strcpy(err, "Found invalid \"$\" character");
+            return true;
+         case '\"':
+            /* Only outer double quotes are allowed */
+            if (i == 0 || i == (len-1)) {
+               if (i == 0 && str[len-1] != '\"') {
+                  pm_strcpy(err, "Missing opening double quote");
+                  return true;
+               } else if (i == (len-1) && str[0] != '\"') {
+                  pm_strcpy(err, "Missing closing double quote");
+                  return true;
+               } else {
+                  *quote_needed = false;
+               }
+               break;
+            } else {
+               pm_strcpy(err, "Found invalid \'\"\' character");
+               return true;
+            }
 
+            return true;
+         default:
+            break;
+      }
+   }
+
+   /* No invalid character found */
+   return false;
+}
 
 /*
  * Add commas to a string, which is presumably
index b3bd62c51f8382e488b39272eb4b5a7e788b29ee..8a42cdf8b24df70bd6821e09c60d9040966ca014 100644 (file)
@@ -244,6 +244,8 @@ bool             is_name_valid           (const char *name, POOLMEM **msg);
 bool             is_name_valid           (const char *name, POOLMEM **msg, const char *accept);
 char             *get_next_tag(char **buf);
 
+bool check_for_invalid_chars(const char *str, POOLMEM **err, bool *quote_needed);
+
 /* jcr.c (most definitions are in src/jcr.h) */
 void     init_last_jobs_list();
 void     term_last_jobs_list();