diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 4112f2b403..8e602ed1b2 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -144,6 +144,9 @@ public: BACKUP_POWEROFF = 70 }; + static const int MAX_VNC_PASSWD_LENGTH = 8; + static const int MAX_SPICE_PASSWD_LENGTH = 60; + /** * Functions to convert to/from string the VM states */ diff --git a/src/vm/VirtualMachineParser.cc b/src/vm/VirtualMachineParser.cc index 1b1e4582f3..81116984e1 100644 --- a/src/vm/VirtualMachineParser.cc +++ b/src/vm/VirtualMachineParser.cc @@ -431,20 +431,21 @@ int VirtualMachine::parse_graphics(string& error_str, Template * tmpl) { password = one_util::random_password(); - if ( graphics->vector_value("TYPE") == "SPICE" ) - { - // Spice password must be 60 characters maximum - graphics->replace("PASSWD", password.substr(0, 59)); - } - else if ( graphics->vector_value("TYPE") == "vnc" ) - { - // Vnc password must be 8 characters maximum - graphics->replace("PASSWD", password.substr(0, 7)); - } - else - { - graphics->replace("PASSWD", password); - } + graphics->replace("PASSWD", password); + } + + string type = graphics->vector_value("TYPE"); + one_util::tolower(type); + + if ( type == "spice" && password.size() > MAX_SPICE_PASSWD_LENGTH ) + { + // Spice password must be 60 characters maximum + graphics->replace("PASSWD", password.substr(0, MAX_SPICE_PASSWD_LENGTH)); + } + else if ( type == "vnc" && password.size() > MAX_VNC_PASSWD_LENGTH ) + { + // Vnc password must be 8 characters maximum + graphics->replace("PASSWD", password.substr(0, MAX_VNC_PASSWD_LENGTH)); } return 0; diff --git a/src/vmm/LibVirtDriverKVM.cc b/src/vmm/LibVirtDriverKVM.cc index 2f375f1264..75a4d4c5d6 100644 --- a/src/vmm/LibVirtDriverKVM.cc +++ b/src/vmm/LibVirtDriverKVM.cc @@ -1787,21 +1787,26 @@ int LibVirtDriver::deployment_description_kvm( port = graphics->vector_value("PORT"); + one_util::tolower(type); + if ( random_passwrd && passwd.empty()) { passwd = one_util::random_password(); - if ( graphics->vector_value("TYPE") == "SPICE" ) + if ( type == "spice" ) { // Spice password must be 60 characters maximum - passwd = passwd.substr(0, 59); + passwd = passwd.substr(0, VirtualMachine::MAX_SPICE_PASSWD_LENGTH); + } + else if ( type == "vnc" ) + { + // Vnc password must be 8 characters maximum + passwd = passwd.substr(0, VirtualMachine::MAX_VNC_PASSWD_LENGTH); } const_cast(graphics)->replace("PASSWD", passwd); } - one_util::tolower(type); - if ( type == "vnc" || type == "spice" ) { file << "\t\t