[icinga-checkins] icinga.org: icinga2/feature/configconvert-2743: configconvert: properly escape strings with double quotes

git at icinga.org git at icinga.org
Thu May 2 22:17:32 CEST 2013


Module: icinga2
Branch: feature/configconvert-2743
Commit: 0475c5971e3fecab45ed5bfeba2451219f7fa31d
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=0475c5971e3fecab45ed5bfeba2451219f7fa31d

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Mon Apr 29 18:49:25 2013 +0200

configconvert: properly escape strings with double quotes

* command_line and command_args
* notification commands
* action_url, notes_url
* notes
* icon_image, icon_image_alt

fixes #3931

---

 contrib/configconvert/Icinga2/Convert.pm |   46 +++++++++++++++++++++++++++--
 contrib/configconvert/Icinga2/Utils.pm   |    8 +++++
 2 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/contrib/configconvert/Icinga2/Convert.pm b/contrib/configconvert/Icinga2/Convert.pm
index 505a42c..a16bf68 100644
--- a/contrib/configconvert/Icinga2/Convert.pm
+++ b/contrib/configconvert/Icinga2/Convert.pm
@@ -402,7 +402,7 @@ sub convert_notificationcommand {
             foreach my $command_1x_key (keys %{$commands_1x}) {
                 chomp $obj_notification_command; # remove trailing spaces
                 if ($commands_1x->{$command_1x_key}->{'command_name'} eq $obj_notification_command) {
-                    $commands_2x->{notification_command} = $commands_1x->{$command_1x_key}->{'command_line'};
+                    $commands_2x->{notification_command} = Icinga2::Utils::escape_str($commands_1x->{$command_1x_key}->{'command_line'});
                 }
             }
 
@@ -446,13 +446,13 @@ sub convert_checkcommand {
             #debug("Found: $real_command_name_1x");
 
             # save the command_line and the $ARGn$ macros
-            $command_2x->{'check_command'} = $commands_1x->{$command_1x_key}->{'command_line'};
+            $command_2x->{'check_command'} = Icinga2::Utils::escape_str($commands_1x->{$command_1x_key}->{'command_line'});
             $command_2x->{'check_command_name_1x'} = $real_command_name_1x;
             #debug("2x Command: $command_2x->{'check_command'}");
 
             # detect $USERn$ macros and replace them too XXX - this should be a global macro?
             if ($commands_1x->{$command_1x_key}->{'command_line'} =~/\$(USER\d)\$/) {
-                $command_2x->{'command_macros'}->{$1} = $user_macros_1x->{$1};
+                $command_2x->{'command_macros'}->{$1} = Icinga2::Utils::escape_str($user_macros_1x->{$1});
                 #debug("\$$1\$=$command_2x->{'macros'}->{$1}");
             }
 
@@ -460,7 +460,7 @@ sub convert_checkcommand {
             my $arg_cnt = 1;
             foreach my $command_arg_1x (@command_args_1x) {
                 my $macro_name_2x = "ARG" . $arg_cnt;
-                $command_2x->{'command_macros'}->{$macro_name_2x} = $command_arg_1x;
+                $command_2x->{'command_macros'}->{$macro_name_2x} = Icinga2::Utils::escape_str($command_arg_1x);
                 #debug("\$$macro_name_2x\$=$command_2x->{'macros'}->{$macro_name_2x}");
                 $arg_cnt++;
             }
@@ -586,6 +586,25 @@ sub convert_2x {
             ####################################################
 
             ##########################################
+            # escape strings in attributes
+            ##########################################
+            if(defined($cfg_obj_2x->{'service'}->{$service_cnt}->{'action_url'})) {
+                $cfg_obj_2x->{'service'}->{$service_cnt}->{'action_url'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'service'}->{$service_cnt}->{'action_url'});
+            }
+            if(defined($cfg_obj_2x->{'service'}->{$service_cnt}->{'notes_url'})) {
+                $cfg_obj_2x->{'service'}->{$service_cnt}->{'notes_url'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'service'}->{$service_cnt}->{'notes_url'});
+            }
+            if(defined($cfg_obj_2x->{'service'}->{$service_cnt}->{'notes'})) {
+                $cfg_obj_2x->{'service'}->{$service_cnt}->{'notes'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'service'}->{$service_cnt}->{'notes'});
+            }
+            if(defined($cfg_obj_2x->{'service'}->{$service_cnt}->{'icon_image'})) {
+                $cfg_obj_2x->{'service'}->{$service_cnt}->{'icon_image'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'service'}->{$service_cnt}->{'icon_image'});
+            }
+            if(defined($cfg_obj_2x->{'service'}->{$service_cnt}->{'icon_image_alt'})) {
+                $cfg_obj_2x->{'service'}->{$service_cnt}->{'icon_image_alt'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'service'}->{$service_cnt}->{'icon_image_alt'});
+            }
+
+            ##########################################
             # servicegroups
             ##########################################
             delete($cfg_obj_2x->{'service'}->{$service_cnt}->{'servicegroups'});
@@ -719,6 +738,25 @@ sub convert_2x {
         # - check_command
         ####################################################
 
+        ##########################################
+        # escape strings in attributes
+        ##########################################
+        if(defined($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'action_url'})) {
+            $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'action_url'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'action_url'});
+        }
+        if(defined($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'notes_url'})) {
+            $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'notes_url'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'notes_url'});
+        }
+        if(defined($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'notes'})) {
+            $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'notes'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'notes'});
+        }
+        if(defined($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'icon_image'})) {
+            $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'icon_image'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'icon_image'});
+        }
+        if(defined($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'icon_image_alt'})) {
+            $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'icon_image_alt'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'icon_image_alt'});
+        }
+
         ####################################################
         # display_name -> alias mapping
         ####################################################
diff --git a/contrib/configconvert/Icinga2/Utils.pm b/contrib/configconvert/Icinga2/Utils.pm
index 30d3f33..7d9b437 100644
--- a/contrib/configconvert/Icinga2/Utils.pm
+++ b/contrib/configconvert/Icinga2/Utils.pm
@@ -54,6 +54,14 @@ sub errlog {
     
 }
 
+sub escape_str {
+    my $str = shift;
+
+    $str =~ s/"/\\"/g;
+
+    return $str;
+}
+
 sub debug {
     my $dbg_str = shift;
     our $dbg_lvl;





More information about the icinga-checkins mailing list