Program: 28272
Project: 28272
Buggy source:
/** 
 * Setting up the key involves the following steps: -If a key exists and we can connect using the key, do nothing. -Generate a key pair if there isn't one -Connect to remote host using password auth and do the following: 1. create .ssh directory if it doesn't exist 2. copy over the key as key.tmp 3. Append the key to authorized_keys file 4. Remove the temporary key file key.tmp 5. Fix permissions for home, .ssh and authorized_keys
 * @param node        - remote host
 * @param pubKeyFile  - .pub file
 * @param generateKey - flag to indicate if key needs to be generated or not
 * @param passwd      - ssh user password
 * @throws IOException
 * @throws InterruptedException
 */
public void setupKey(String node,String pubKeyFile,boolean generateKey,String passwd) throws IOException, InterruptedException {
  boolean connected=false;
  File key=new File(keyFile);
  if (logger.isLoggable(Level.FINER))   logger.finer("String_Node_Str" + keyFile);
  if (key.exists()) {
    if (checkConnection()) {
      throw new IOException("String_Node_Str" + userName + "String_Node_Str"+ node);
    }
  }
 else {
    if (generateKey) {
      if (!generateKeyPair()) {
        throw new IOException("String_Node_Str");
      }
    }
 else {
      throw new IOException("String_Node_Str");
    }
  }
  if (passwd == null) {
    throw new IOException("String_Node_Str");
  }
  connection=new Connection(node,port);
  connection.connect();
  connected=connection.authenticateWithPassword(userName,passwd);
  if (!connected) {
    throw new IOException("String_Node_Str" + userName + "String_Node_Str"+ node);
  }
  Connection conn=new Connection(node,port);
  conn.connect();
  boolean ret=conn.authenticateWithPassword(userName,passwd);
  if (!ret) {
    throw new IOException("String_Node_Str" + userName + "String_Node_Str"+ node);
  }
  SCPClient scp=new SCPClient(conn);
  SFTPClient sftp=new SFTPClient(connection);
  if (key.exists()) {
    setupSSHDir();
    if (pubKeyFile == null) {
      pubKeyFile=keyFile + "String_Node_Str";
    }
    File pubKey=new File(pubKeyFile);
    if (!pubKey.exists()) {
      throw new IOException("String_Node_Str" + pubKeyFile + "String_Node_Str");
    }
    try {
      if (!sftp.exists(SSH_DIR)) {
        if (logger.isLoggable(Level.FINER)) {
          logger.fine(SSH_DIR + "String_Node_Str");
        }
        sftp.mkdirs("String_Node_Str",0700);
      }
    }
 catch (    Exception e) {
      if (logger.isLoggable(Level.FINER)) {
        e.printStackTrace();
      }
      throw new IOException("String_Node_Str" + e.getMessage());
    }
    scp.put(pubKey.getAbsolutePath(),"String_Node_Str","String_Node_Str","String_Node_Str");
    String mergeCommand="String_Node_Str" + AUTH_KEY_FILE;
    if (logger.isLoggable(Level.FINER)) {
      logger.finer("String_Node_Str" + mergeCommand);
    }
    if (conn.exec(mergeCommand,new ByteArrayOutputStream()) != 0) {
      throw new IOException("String_Node_Str" + pubKeyFile + "String_Node_Str"+ host);
    }
    logger.info("String_Node_Str" + pubKeyFile + "String_Node_Str"+ userName+ "String_Node_Str"+ host);
    if (conn.exec("String_Node_Str",new ByteArrayOutputStream()) != 0) {
      logger.warning("String_Node_Str" + host);
    }
    if (logger.isLoggable(Level.FINER)) {
      logger.finer("String_Node_Str");
    }
    logger.info("String_Node_Str");
    sftp.chmod("String_Node_Str",0755);
    sftp.chmod(SSH_DIR,0700);
    sftp.chmod(SSH_DIR + AUTH_KEY_FILE,0644);
    sftp.close();
    conn.close();
  }
}

Fixed version (ground truth):
/** 
 * Setting up the key involves the following steps: -If a key exists and we can connect using the key, do nothing. -Generate a key pair if there isn't one -Connect to remote host using password auth and do the following: 1. create .ssh directory if it doesn't exist 2. copy over the key as key.tmp 3. Append the key to authorized_keys file 4. Remove the temporary key file key.tmp 5. Fix permissions for home, .ssh and authorized_keys
 * @param node        - remote host
 * @param pubKeyFile  - .pub file
 * @param generateKey - flag to indicate if key needs to be generated or not
 * @param passwd      - ssh user password
 * @throws IOException
 * @throws InterruptedException
 */
public void setupKey(String node,String pubKeyFile,boolean generateKey,String passwd) throws IOException, InterruptedException {
  boolean connected=false;
  File key=new File(keyFile);
  if (logger.isLoggable(Level.FINER))   logger.finer("String_Node_Str" + keyFile);
  if (key.exists()) {
    if (checkConnection()) {
      throw new IOException("String_Node_Str" + userName + "String_Node_Str"+ node);
    }
  }
 else {
    if (generateKey) {
      if (!generateKeyPair()) {
        throw new IOException("String_Node_Str");
      }
    }
 else {
      throw new IOException("String_Node_Str");
    }
  }
  if (passwd == null) {
    throw new IOException("String_Node_Str");
  }
  connection=new Connection(node,port);
  connection.connect();
  connected=connection.authenticateWithPassword(userName,passwd);
  if (!connected) {
    throw new IOException("String_Node_Str" + userName + "String_Node_Str"+ node);
  }
  Connection conn=new Connection(node,port);
  conn.connect();
  boolean ret=conn.authenticateWithPassword(userName,passwd);
  if (!ret) {
    throw new IOException("String_Node_Str" + userName + "String_Node_Str"+ node);
  }
  SCPClient scp=new SCPClient(conn);
  SFTPClient sftp=new SFTPClient(connection);
  if (key.exists()) {
    setupSSHDir();
    if (pubKeyFile == null) {
      pubKeyFile=keyFile + "String_Node_Str";
    }
    File pubKey=new File(pubKeyFile);
    if (!pubKey.exists()) {
      throw new IOException("String_Node_Str" + pubKeyFile + "String_Node_Str");
    }
    try {
      if (!sftp.exists(SSH_DIR)) {
        if (logger.isLoggable(Level.FINE)) {
          logger.fine(SSH_DIR + "String_Node_Str");
        }
        sftp.mkdirs("String_Node_Str",0700);
      }
    }
 catch (    Exception e) {
      if (logger.isLoggable(Level.FINER)) {
        e.printStackTrace();
      }
      throw new IOException("String_Node_Str" + e.getMessage());
    }
    scp.put(pubKey.getAbsolutePath(),"String_Node_Str","String_Node_Str","String_Node_Str");
    String mergeCommand="String_Node_Str" + AUTH_KEY_FILE;
    if (logger.isLoggable(Level.FINER)) {
      logger.finer("String_Node_Str" + mergeCommand);
    }
    if (conn.exec(mergeCommand,new ByteArrayOutputStream()) != 0) {
      throw new IOException("String_Node_Str" + pubKeyFile + "String_Node_Str"+ host);
    }
    logger.info("String_Node_Str" + pubKeyFile + "String_Node_Str"+ userName+ "String_Node_Str"+ host);
    if (conn.exec("String_Node_Str",new ByteArrayOutputStream()) != 0) {
      logger.warning("String_Node_Str" + host);
    }
    if (logger.isLoggable(Level.FINER)) {
      logger.finer("String_Node_Str");
    }
    logger.info("String_Node_Str");
    sftp.chmod("String_Node_Str",0755);
    sftp.chmod(SSH_DIR,0700);
    sftp.chmod(SSH_DIR + AUTH_KEY_FILE,0644);
    sftp.close();
    conn.close();
  }
}