protected Answer execute(ModifySshKeysCommand cmd) {
File sshKeysDir = new File(_SSHKEYSPATH);
String result = null;
if (!sshKeysDir.exists()) {
// Change permissions for the 700
Script script = new Script("mkdir", _timeout, s_logger);
script.add("-m","700");
script.add(_SSHKEYSPATH);
script.execute();
if(!sshKeysDir.exists()) {
s_logger.debug("failed to create directory " + _SSHKEYSPATH);
}
}
File pubKeyFile = new File(_SSHPUBKEYPATH);
if (!pubKeyFile.exists()) {
try {
pubKeyFile.createNewFile();
} catch (IOException e) {
result = "Failed to create file: " + e.toString();
s_logger.debug(result);
}
}
if (pubKeyFile.exists()) {
String pubKey = cmd.getPubKey();
try {
FileOutputStream pubkStream = new FileOutputStream(pubKeyFile);
pubkStream.write(pubKey.getBytes());
pubkStream.close();
} catch (FileNotFoundException e) {
result = "File" + _SSHPUBKEYPATH + "is not found:"
+ e.toString();
s_logger.debug(result);
} catch (IOException e) {
result = "Write file " + _SSHPUBKEYPATH + ":" + e.toString();
s_logger.debug(result);
}
}
File prvKeyFile = new File(_SSHPRVKEYPATH);
if (!prvKeyFile.exists()) {
try {
prvKeyFile.createNewFile();
} catch (IOException e) {
result = "Failed to create file: " + e.toString();
s_logger.debug(result);
}
}
if (prvKeyFile.exists()) {
String prvKey = cmd.getPrvKey();
try {
FileOutputStream prvKStream = new FileOutputStream(prvKeyFile);
prvKStream.write(prvKey.getBytes());
prvKStream.close();
} catch (FileNotFoundException e) {
result = "File" + _SSHPRVKEYPATH + "is not found:"
+ e.toString();
s_logger.debug(result);
} catch (IOException e) {
result = "Write file " + _SSHPRVKEYPATH + ":" + e.toString();
s_logger.debug(result);
}
Script script = new Script("chmod", _timeout, s_logger);
script.add("600", _SSHPRVKEYPATH);
script.execute();
}
if (result != null) {
return new Answer(cmd, false, result);
} else {