Пример #1
0
  public boolean enablePortForward() {

    /*
     * DynamicPortForwarder dpf = null;
     *
     * try { dpf = connection.createDynamicPortForwarder(new
     * InetSocketAddress( InetAddress.getLocalHost(), 1984)); } catch
     * (Exception e) { Log.e(TAG, "Could not create dynamic port forward",
     * e); return false; }
     */

    // LocalPortForwarder lpf1 = null;
    try {

      dnspf = connection.createLocalPortForwarder(8053, "www.google.com", 80);

      if (profile.isSocks()) {
        dpf = connection.createDynamicPortForwarder(profile.getLocalPort());
      } else {
        lpf =
            connection.createLocalPortForwarder(
                profile.getLocalPort(), profile.getRemoteAddress(), profile.getRemotePort());
      }

    } catch (Exception e) {
      Log.e(TAG, "Could not create local port forward", e);
      if (reason == null) reason = getString(R.string.fail_to_forward);
      return false;
    }

    return true;
  }
Пример #2
0
 /**
  * Start the application
  *
  * @throws IOException if there is a problem with connection
  * @throws InterruptedException if thread was interrupted
  */
 private void start() throws IOException, InterruptedException {
   Connection c = new Connection(myHost.getHostName(), myHost.getPort());
   try {
     configureKnownHosts(c);
     c.connect(new HostKeyVerifier());
     authenticate(c);
     final Session s = c.openSession();
     try {
       s.execCommand(myCommand);
       // Note that stdin is not being waited using semaphore.
       // Instead, the SSH process waits for remote process exit
       // if remote process exited, none is interested in stdin
       // anyway.
       forward("stdin", s.getStdin(), System.in, false);
       forward("stdout", System.out, s.getStdout(), true);
       forward("stderr", System.err, s.getStderr(), true);
       myForwardCompleted.acquire(2); // wait only for stderr and stdout
       s.waitForCondition(ChannelCondition.EXIT_STATUS, Long.MAX_VALUE);
       Integer exitStatus = s.getExitStatus();
       if (exitStatus == null) {
         // broken exit status
         exitStatus = 1;
       }
       System.exit(exitStatus.intValue() == 0 ? myExitCode : exitStatus.intValue());
     } finally {
       s.close();
     }
   } finally {
     c.close();
   }
 }
 private Connection getDropletConnection(String host, int port, PrintStream logger)
     throws IOException {
   logger.println("Connecting to " + host + " on port " + port + ". ");
   Connection conn = new Connection(host, port);
   try {
     conn.connect(null, 10 * 1000, 10 * 1000);
   } catch (SocketTimeoutException e) {
     return null;
   }
   logger.println("Connected via SSH.");
   return conn;
 }
Пример #4
0
  private void onDisconnect() {
    connected = false;

    try {
      if (lpf != null) {
        lpf.close();
        lpf = null;
      }
    } catch (IOException ignore) {
      // Nothing
    }
    try {
      if (dpf != null) {
        dpf.close();
        dpf = null;
      }
    } catch (IOException ignore) {
      // Nothing
    }
    try {
      if (dnspf != null) {
        dnspf.close();
        dnspf = null;
      }
    } catch (IOException ignore) {
      // Nothing
    }

    if (connection != null) {
      connection.close();
      connection = null;
    }
  }
Пример #5
0
 /**
  * Configure known host database for connection
  *
  * @param c a connection
  * @throws IOException if there is a IO problem
  */
 private void configureKnownHosts(Connection c) throws IOException {
   File knownHostFile = new File(knownHostPath);
   if (knownHostFile.exists()) {
     database.addHostkeys(knownHostFile);
   }
   final List<String> algorithms = myHost.getHostKeyAlgorithms();
   c.setServerHostKeyAlgorithms(algorithms.toArray(new String[algorithms.size()]));
 }
Пример #6
0
 /**
  * Try public key
  *
  * @param c a ssh connection
  * @param keyPath a path to key
  * @return true if authentication is successful
  */
 private boolean tryPublicKey(final Connection c, final String keyPath) {
   try {
     final File file = new File(keyPath);
     if (file.exists()) {
       // if encrypted ask user for passphrase
       String passphrase = null;
       char[] text = FileUtil.loadFileText(file);
       if (isEncryptedKey(text)) {
         // need to ask passphrase from user
         int i;
         for (i = myHost.getNumberOfPasswordPrompts(); i > 0; i--) {
           passphrase =
               myXmlRpcClient.askPassphrase(
                   myHandlerNo, getUserHostString(), keyPath, myLastError);
           if (passphrase == null) {
             // if no passphrase was entered, just return false and try something other
             return false;
           } else {
             try {
               PEMDecoder.decode(text, passphrase);
               myLastError = "";
             } catch (IOException e) {
               // decoding failed
               myLastError = GitBundle.message("sshmain.invalidpassphrase", keyPath);
               continue;
             }
             break;
           }
         }
         if (i == 0) {
           myLastError =
               GitBundle.message(
                   "sshmain.too.mush.passphrase.guesses",
                   keyPath,
                   myHost.getNumberOfPasswordPrompts());
           return false;
         }
       }
       // try authentication
       if (c.authenticateWithPublicKey(myHost.getUser(), text, passphrase)) {
         myLastError = "";
         return true;
       } else {
         if (passphrase != null) {
           // mark as failed authentication only if passphrase were asked
           myLastError = GitBundle.message("sshmain.pk.authenitication.failed", keyPath);
         } else {
           myLastError = "";
         }
       }
     }
     return false;
   } catch (Exception e) {
     return false;
   }
 }
 protected int installJava(Connection conn, PrintStream logger, Iterable<String> javaVersion)
     throws IOException, InterruptedException {
   int result = 1;
   for (String version : javaVersion) {
     result = conn.exec(getInstallCommand(version), logger);
     if (result == 0) {
       return result;
     }
   }
   return result;
 }
  private boolean runInitScript(
      final Computer computer, final PrintStream logger, final Connection conn, final SCPClient scp)
      throws IOException, InterruptedException {

    String initScript = Util.fixEmptyAndTrim(computer.getNode().getInitScript());

    if (initScript == null) {
      return true;
    }
    if (conn.exec("test -e ~/.hudson-run-init", logger) == 0) {
      return true;
    }

    logger.println("Executing init script");
    scp.put(initScript.getBytes("UTF-8"), "init.sh", "/tmp", "0700");
    Session session = conn.openSession();
    session.requestDumbPTY(); // so that the remote side bundles stdout and stderr
    session.execCommand(buildUpCommand(computer, "/tmp/init.sh"));

    session.getStdin().close(); // nothing to write here
    session.getStderr().close(); // we are not supposed to get anything from stderr
    IOUtils.copy(session.getStdout(), logger);

    int exitStatus = waitCompletion(session);
    if (exitStatus != 0) {
      logger.println("init script failed: exit code=" + exitStatus);
      return false;
    }
    session.close();

    // Needs a tty to run sudo.
    session = conn.openSession();
    session.requestDumbPTY(); // so that the remote side bundles stdout and stderr
    session.execCommand(buildUpCommand(computer, "touch ~/.hudson-run-init"));
    session.close();

    return true;
  }
Пример #9
0
  private Connection getConnection(
      String servername,
      int serverport,
      String proxyhost,
      int proxyport,
      String proxyusername,
      String proxypassword) {
    /* Create a connection instance */

    Connection connect = new Connection(servername, serverport);

    /* We want to connect through a HTTP proxy */
    if (usehttpproxy) {
      connect.setProxyData(new HTTPProxyData(proxyhost, proxyport));

      /* Now connect */
      // if the proxy requires basic authentication:
      if (useBasicAuthentication) {
        connect.setProxyData(new HTTPProxyData(proxyhost, proxyport, proxyusername, proxypassword));
      }
    }

    return connect;
  }
Пример #10
0
  private void authenticate() {

    try {
      if (connection.authenticateWithNone(profile.getUser())) {
        Log.d(TAG, "Authenticate with none");
        return;
      }
    } catch (Exception e) {
      Log.d(TAG, "Host does not support 'none' authentication.");
    }

    try {
      if (connection.isAuthMethodAvailable(profile.getUser(), AUTH_PUBLICKEY)) {
        File f = new File(profile.getKeyPath());
        if (f.exists()) if (profile.getPassword().equals("")) profile.setPassword(null);
        if (connection.authenticateWithPublicKey(profile.getUser(), f, profile.getPassword())) {
          Log.d(TAG, "Authenticate with public key");
          return;
        }
      }
    } catch (Exception e) {
      Log.d(TAG, "Host does not support 'Public key' authentication.");
    }

    try {
      if (connection.isAuthMethodAvailable(profile.getUser(), AUTH_PASSWORD)) {
        if (connection.authenticateWithPassword(profile.getUser(), profile.getPassword())) {
          Log.d(TAG, "Authenticate with password");
          return;
        }
      }
    } catch (IllegalStateException e) {
      Log.e(TAG, "Connection went away while we were trying to authenticate", e);
    } catch (Exception e) {
      Log.e(TAG, "Problem during handleAuthentication()", e);
    }

    // TODO: Need verification

    try {
      if (connection.isAuthMethodAvailable(profile.getUser(), AUTH_KEYBOARDINTERACTIVE)) {
        if (connection.authenticateWithKeyboardInteractive(profile.getUser(), this)) return;
      }
    } catch (Exception e) {
      Log.d(TAG, "Host does not support 'Keyboard-Interactive' authentication.");
    }
  }
  private boolean installJava(final PrintStream logger, final Connection conn)
      throws IOException, InterruptedException {
    logger.println("Verifying that java exists");
    if (conn.exec("java -fullversion", logger) != 0) {
      logger.println("Try to install one of these Java-versions: " + VALID_VERSIONS);
      // TODO Web UI to let users install a custom java (or any other type of tool) package.
      logger.println("Trying to find a working package manager");
      for (JavaInstaller installer : INSTALLERS) {
        if (!installer.isUsable(conn, logger)) {
          continue;
        }
        if (installer.installJava(conn, logger, VALID_VERSIONS) == 0) {
          return true;
        }
      }

      logger.println("Java could not be installed using any of the supported package managers");
      return false;
    }
    return true;
  }
Пример #12
0
  public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);

    try {

      // Get real variable value
      String realServerName = environmentSubstitute(serverName);
      int realServerPort = Const.toInt(environmentSubstitute(serverPort), 22);
      String realUserName = environmentSubstitute(userName);
      String realServerPassword =
          Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(password));
      // Proxy Host
      String realProxyHost = environmentSubstitute(httpproxyhost);
      int realProxyPort = Const.toInt(environmentSubstitute(httpproxyport), 22);
      String realproxyUserName = environmentSubstitute(httpproxyusername);
      String realProxyPassword =
          Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(httpProxyPassword));
      // Key file
      String realKeyFilename = environmentSubstitute(keyFilename);
      String relKeyFilepass = environmentSubstitute(keyFilePass);
      // Source files
      String realLocalDirectory = environmentSubstitute(localDirectory);
      String realwildcard = environmentSubstitute(wildcard);
      // Remote destination
      String realftpDirectory = environmentSubstitute(ftpDirectory);
      // Destination folder (Move to)
      String realDestinationFolder = environmentSubstitute(destinationfolder);

      try {
        // Remote source
        realftpDirectory = FTPUtils.normalizePath(realftpDirectory);
        // Destination folder (Move to)
        realDestinationFolder = FTPUtils.normalizePath(realDestinationFolder);
      } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.CanNotNormalizePath", e.getMessage()));
        result.setNrErrors(1);
        return result;
      }

      // Check for mandatory fields
      boolean mandatoryok = true;
      if (Const.isEmpty(realServerName)) {
        mandatoryok = false;
        logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.ServernameMissing"));
      }
      if (usehttpproxy) {
        if (Const.isEmpty(realProxyHost)) {
          mandatoryok = false;
          logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.HttpProxyhostMissing"));
        }
      }
      if (publicpublickey) {
        if (Const.isEmpty(realKeyFilename)) {
          mandatoryok = false;
          logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.KeyFileMissing"));
        } else {
          // Let's check if folder exists...
          if (!KettleVFS.fileExists(realKeyFilename, this)) {
            mandatoryok = false;
            logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.KeyFileNotExist"));
          }
        }
      }

      if (Const.isEmpty(realLocalDirectory)) {
        mandatoryok = false;
        logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.LocalFolderMissing"));
      }
      if (afterFtpPut.equals("move_file")) {
        if (Const.isEmpty(realDestinationFolder)) {
          mandatoryok = false;
          logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.DestinatFolderMissing"));
        } else {
          FileObject folder = null;
          try {
            folder = KettleVFS.getFileObject(realDestinationFolder, this);
            // Let's check if folder exists...
            if (!folder.exists()) {
              // Do we need to create it?
              if (createDestinationFolder) {
                folder.createFolder();
              } else {
                logError(
                    BaseMessages.getString(
                        PKG, "JobSSH2PUT.Log.DestinatFolderNotExist", realDestinationFolder));
              }
            }
          } catch (Exception e) {
            throw new KettleException(e);
          } finally {
            if (folder != null) {
              try {
                folder.close();
                folder = null;
              } catch (Exception e) {
                /* Ignore */
              }
            }
          }
        }
      }

      if (mandatoryok) {
        Connection conn = null;
        SFTPv3Client client = null;
        boolean good = true;

        int nbfilestoput = 0;
        int nbput = 0;
        int nbrerror = 0;

        try {
          // Create a connection instance
          conn =
              getConnection(
                  realServerName,
                  realServerPort,
                  realProxyHost,
                  realProxyPort,
                  realproxyUserName,
                  realProxyPassword);

          if (timeout > 0) {
            // Use timeout
            // Cache Host Key
            if (cachehostkey) {
              conn.connect(new SimpleVerifier(database), 0, timeout * 1000);
            } else {
              conn.connect(null, 0, timeout * 1000);
            }

          } else {
            // Cache Host Key
            if (cachehostkey) {
              conn.connect(new SimpleVerifier(database));
            } else {
              conn.connect();
            }
          }

          // Authenticate

          boolean isAuthenticated = false;
          if (publicpublickey) {
            String keyContent =
                KettleVFS.getTextFileContent(realKeyFilename, this, Const.XML_ENCODING);
            isAuthenticated =
                conn.authenticateWithPublicKey(
                    realUserName, keyContent.toCharArray(), relKeyFilepass);
          } else {
            isAuthenticated = conn.authenticateWithPassword(realUserName, realServerPassword);
          }

          // LET'S CHECK AUTHENTICATION ...
          if (isAuthenticated == false) {
            logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.AuthenticationFailed"));
          } else {
            if (log.isBasic()) {
              logBasic(
                  BaseMessages.getString(PKG, "JobSSH2PUT.Log.Connected", serverName, userName));
            }

            client = new SFTPv3Client(conn);

            if (log.isDetailed()) {
              logDetailed(
                  BaseMessages.getString(
                      PKG, "JobSSH2PUT.Log.ProtocolVersion", "" + client.getProtocolVersion()));
            }

            // Check if remote directory exists
            if (!Const.isEmpty(realftpDirectory)) {
              if (!sshDirectoryExists(client, realftpDirectory)) {
                good = false;
                if (createRemoteFolder) {
                  good = CreateRemoteFolder(client, realftpDirectory);
                  if (good) {
                    logBasic(BaseMessages.getString(PKG, "JobSSH2PUT.Log.RemoteDirectoryCreated"));
                  }

                } else {
                  logError(
                      BaseMessages.getString(
                          PKG, "JobSSH2PUT.Log.RemoteDirectoryNotExist", realftpDirectory));
                }
              } else if (log.isDetailed()) {
                logDetailed(
                    BaseMessages.getString(
                        PKG, "JobSSH2PUT.Log.RemoteDirectoryExist", realftpDirectory));
              }
            }

            if (good) {
              // Get files list from local folder (source)
              List<FileObject> myFileList = getFiles(realLocalDirectory);

              // Prepare Pattern for wildcard
              Pattern pattern = null;
              if (!Const.isEmpty(realwildcard)) {
                pattern = Pattern.compile(realwildcard);
              }

              // Let's put files now ...
              // Get the files in the list
              for (int i = 0; i < myFileList.size() && !parentJob.isStopped(); i++) {
                FileObject myFile = myFileList.get(i);
                String localFilename = myFile.toString();
                String remoteFilename = myFile.getName().getBaseName();

                boolean getIt = true;

                // First see if the file matches the regular expression!
                if (pattern != null) {
                  Matcher matcher = pattern.matcher(remoteFilename);
                  getIt = matcher.matches();
                }

                // do we have a target directory?
                if (!Const.isEmpty(realftpDirectory)) {
                  remoteFilename = realftpDirectory + FTPUtils.FILE_SEPARATOR + remoteFilename;
                }

                if (onlyGettingNewFiles) {
                  // We get only new files
                  // ie not exist on the remote server
                  getIt = !sshFileExists(client, remoteFilename);
                }

                if (getIt) {
                  nbfilestoput++;

                  boolean putok = putFile(myFile, remoteFilename, client);
                  if (!putok) {
                    nbrerror++;
                    logError(
                        BaseMessages.getString(
                            PKG, "JobSSH2PUT.Log.Error.CanNotPutFile", localFilename));
                  } else {
                    nbput++;
                  }

                  if (putok && !afterFtpPut.equals("do_nothing")) {
                    deleteOrMoveFiles(myFile, realDestinationFolder);
                  }
                }
              }
              /** ****************************** RESULT ******************* */
              if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Result.JobEntryEnd1"));
                logDetailed(
                    BaseMessages.getString(
                        PKG, "JobSSH2PUT.Log.Result.TotalFiles", "" + nbfilestoput));
                logDetailed(
                    BaseMessages.getString(PKG, "JobSSH2PUT.Log.Result.TotalFilesPut", "" + nbput));
                logDetailed(
                    BaseMessages.getString(
                        PKG, "JobSSH2PUT.Log.Result.TotalFilesError", "" + nbrerror));
                logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Result.JobEntryEnd2"));
              }
              if (nbrerror == 0) {
                result.setResult(true);
                /** ****************************** RESULT ******************* */
              }
            }
          }

        } catch (Exception e) {
          result.setNrErrors(nbrerror);
          logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Error.ErrorFTP", e.getMessage()));
        } finally {
          if (conn != null) {
            conn.close();
          }
          if (client != null) {
            client.close();
          }
        }
      }
    } catch (Exception e) {
      result.setResult(false);
      result.setNrErrors(1L);
      logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Error.UnexpectedError"), e);
    }

    return result;
  }
Пример #13
0
  public boolean connect() {

    // try {
    //
    // connection.setCompression(true);
    // connection.setTCPNoDelay(true);
    //
    // } catch (IOException e) {
    // Log.e(TAG, "Could not enable compression!", e);
    // }

    // initialize the upstream proxy
    if (profile.isUpstreamProxy()) {
      try {
        if (profile.getUpstreamProxy() == null || profile.getUpstreamProxy().equals(""))
          throw new Exception();
        String[] proxyInfo = profile.getUpstreamProxy().split("@");
        if (proxyInfo.length == 1) {
          String[] hostInfo = proxyInfo[0].split(":");
          if (hostInfo.length != 2) throw new Exception();
          proxyData = new HTTPProxyData(hostInfo[0], Integer.valueOf(hostInfo[1]));
        } else if (proxyInfo.length == 2) {
          String[] userInfo = proxyInfo[0].split(":");
          if (userInfo.length != 2) throw new Exception();
          String[] hostInfo = proxyInfo[1].split(":");
          if (hostInfo.length != 2) throw new Exception();
          proxyData =
              new HTTPProxyData(
                  hostInfo[0], Integer.valueOf(hostInfo[1]), userInfo[0], userInfo[1]);
        } else {
          throw new Exception();
        }
      } catch (Exception e) {
        if (reason == null) reason = getString(R.string.upstream_format_error);
        return false;
      }
    }

    // get the host ip address
    if (proxyData != null) {
      try {
        hostAddress = InetAddress.getByName(proxyData.proxyHost).getHostAddress();
      } catch (UnknownHostException e) {
        hostAddress = null;
      }
    } else {
      try {
        hostAddress = InetAddress.getByName(profile.getHost()).getHostAddress();
      } catch (UnknownHostException e) {
        hostAddress = null;
      }
    }

    // fail to connect if the dns lookup failed
    if (hostAddress == null) {
      if (reason == null) reason = getString(R.string.fail_to_connect);
      return false;
    }

    // begin to connect
    try {
      connection = new Connection(profile.getHost(), profile.getPort());

      if (proxyData != null) connection.setProxyData(proxyData);

      connection.addConnectionMonitor(this);

      /*
       * Uncomment when debugging SSH protocol:
       */

      /*
       * DebugLogger logger = new DebugLogger() {
       *
       * public void log(int level, String className, String message) {
       * Log.d("SSH", message); }
       *
       * };
       *
       * Logger.enabled = true; Logger.logger = logger;
       */

      connection.connect(this, 10 * 1000, 20 * 1000);
      connected = true;

    } catch (Exception e) {
      Log.e(TAG, "Problem in SSH connection thread during connecting", e);

      // Display the reason in the text.

      if (reason == null) reason = getString(R.string.fail_to_connect);

      return false;
    }

    try {
      // enter a loop to keep trying until authentication
      int tries = 0;
      while (connected && !connection.isAuthenticationComplete() && tries++ < AUTH_TRIES) {
        authenticate();

        // sleep to make sure we dont kill system
        Thread.sleep(1000);
      }
    } catch (Exception e) {
      Log.e(TAG, "Problem in SSH connection thread during authentication", e);

      if (reason == null) reason = getString(R.string.fail_to_authenticate);
      return false;
    }

    try {
      if (connection.isAuthenticationComplete()) {
        return enablePortForward();
      }
    } catch (Exception e) {
      Log.e(TAG, "Problem in SSH connection thread during enabling port", e);

      if (reason == null) reason = getString(R.string.fail_to_connect);
      return false;
    }

    if (reason == null) reason = getString(R.string.fail_to_authenticate);
    Log.e(TAG, "Cannot authenticate");
    return false;
  }
Пример #14
0
  public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);

    if (log.isRowLevel()) {
      logRowlevel(BaseMessages.getString(PKG, "JobSSH2GET.Log.GettingFieldsValue"));
    }

    // Get real variable value
    String realServerName = environmentSubstitute(serverName);
    int realServerPort = Const.toInt(environmentSubstitute(serverPort), 22);
    String realUserName = environmentSubstitute(userName);
    String realServerPassword =
        Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(password));
    // Proxy Host
    String realProxyHost = environmentSubstitute(httpProxyHost);
    int realProxyPort = Const.toInt(environmentSubstitute(httpproxyport), 22);
    String realproxyUserName = environmentSubstitute(httpproxyusername);
    String realProxyPassword =
        Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(httpProxyPassword));
    // Key file
    String realKeyFilename = environmentSubstitute(keyFilename);
    String relKeyFilepass = environmentSubstitute(keyFilePass);
    // target files
    String realLocalDirectory = environmentSubstitute(localDirectory);
    String realwildcard = environmentSubstitute(wildcard);
    // Remote source
    String realftpDirectory = environmentSubstitute(ftpDirectory);
    // Destination folder (Move to)
    String realDestinationFolder = environmentSubstitute(destinationfolder);

    try {
      // Remote source
      realftpDirectory = FTPUtils.normalizePath(realftpDirectory);
      // Destination folder (Move to)
      realDestinationFolder = FTPUtils.normalizePath(realDestinationFolder);
    } catch (Exception e) {
      logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.CanNotNormalizePath", e.getMessage()));
      result.setNrErrors(1);
      return result;
    }

    // Check for mandatory fields
    if (log.isRowLevel()) {
      logRowlevel(BaseMessages.getString(PKG, "JobSSH2GET.Log.CheckingMandatoryFields"));
    }

    boolean mandatoryok = true;
    if (Const.isEmpty(realServerName)) {
      mandatoryok = false;
      logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.ServernameMissing"));
    }
    if (usehttpproxy) {
      if (Const.isEmpty(realProxyHost)) {
        mandatoryok = false;
        logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.HttpProxyhostMissing"));
      }
    }
    if (publicpublickey) {
      if (Const.isEmpty(realKeyFilename)) {
        mandatoryok = false;
        logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.KeyFileMissing"));
      } else {
        // Let's check if key file exists...
        if (!new File(realKeyFilename).exists()) {
          mandatoryok = false;
          logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.KeyFileNotExist"));
        }
      }
    }

    if (Const.isEmpty(realLocalDirectory)) {
      mandatoryok = false;
      logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.LocalFolderMissing"));
    } else {
      // Check if target folder exists...
      if (!new File(realLocalDirectory).exists()) {

        if (createtargetfolder) {
          // Create Target folder
          if (!CreateFolder(realLocalDirectory)) {
            mandatoryok = false;
          }

        } else {
          mandatoryok = false;
          logError(
              BaseMessages.getString(
                  PKG, "JobSSH2GET.Log.LocalFolderNotExists", realLocalDirectory));
        }
      } else {
        if (!new File(realLocalDirectory).isDirectory()) {
          mandatoryok = false;
          logError(
              BaseMessages.getString(
                  PKG, "JobSSH2GET.Log.LocalFolderNotFolder", realLocalDirectory));
        }
      }
    }
    if (afterFtpPut.equals("move_file")) {
      if (Const.isEmpty(realDestinationFolder)) {
        mandatoryok = false;
        logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.DestinatFolderMissing"));
      }
    }

    if (mandatoryok) {

      Connection conn = null;
      SFTPv3Client client = null;
      boolean good = true;

      try {
        // Create a connection instance
        conn =
            getConnection(
                realServerName,
                realServerPort,
                realProxyHost,
                realProxyPort,
                realproxyUserName,
                realProxyPassword);
        if (log.isDetailed()) {
          logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.ConnectionInstanceCreated"));
        }
        if (timeout > 0) {
          // Use timeout
          // Cache Host Key
          if (cachehostkey) {
            conn.connect(new SimpleVerifier(database), 0, timeout * 1000);
          } else {
            conn.connect(null, 0, timeout * 1000);
          }

        } else {
          // Cache Host Key
          if (cachehostkey) {
            conn.connect(new SimpleVerifier(database));
          } else {
            conn.connect();
          }
        }

        // Authenticate

        boolean isAuthenticated = false;
        if (publicpublickey) {
          isAuthenticated =
              conn.authenticateWithPublicKey(
                  realUserName, new File(realKeyFilename), relKeyFilepass);
        } else {
          isAuthenticated = conn.authenticateWithPassword(realUserName, realServerPassword);
        }

        // LET'S CHECK AUTHENTICATION ...
        if (isAuthenticated == false) {
          logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.AuthenticationFailed"));
        } else {
          if (log.isBasic()) {
            logBasic(BaseMessages.getString(PKG, "JobSSH2GET.Log.Connected", serverName, userName));
          }

          client = new SFTPv3Client(conn);

          if (log.isDetailed()) {
            logDetailed(
                BaseMessages.getString(
                    PKG, "JobSSH2GET.Log.ProtocolVersion", "" + client.getProtocolVersion()));
          }

          // Check if ftp (source) directory exists
          if (!Const.isEmpty(realftpDirectory)) {
            if (!sshDirectoryExists(client, realftpDirectory)) {
              good = false;
              logError(
                  BaseMessages.getString(
                      PKG, "JobSSH2GET.Log.RemoteDirectoryNotExist", realftpDirectory));
            } else if (log.isDetailed()) {
              logDetailed(
                  BaseMessages.getString(
                      PKG, "JobSSH2GET.Log.RemoteDirectoryExist", realftpDirectory));
            }
          }

          if (realDestinationFolder != null) {
            // Check now destination folder
            if (!sshDirectoryExists(client, realDestinationFolder)) {
              if (createdestinationfolder) {
                if (!CreateRemoteFolder(client, realDestinationFolder)) {
                  good = false;
                }
              } else {
                good = false;
                logError(
                    BaseMessages.getString(
                        PKG, "JobSSH2GET.Log.DestinatFolderNotExist", realDestinationFolder));
              }
            }
          }

          if (good) {
            Pattern pattern = null;
            if (!Const.isEmpty(realwildcard)) {
              pattern = Pattern.compile(realwildcard);
            }

            if (includeSubFolders) {
              if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.RecursiveModeOn"));
              }
              copyRecursive(realftpDirectory, realLocalDirectory, client, pattern, parentJob);
            } else {
              if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.RecursiveModeOff"));
              }
              GetFiles(realftpDirectory, realLocalDirectory, client, pattern, parentJob);
            }

            /** ****************************** RESULT ******************* */
            if (log.isDetailed()) {
              logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.Result.JobEntryEnd1"));
              logDetailed(
                  BaseMessages.getString(
                      PKG, "JobSSH2GET.Log.Result.TotalFiles", "" + nbfilestoget));
              logDetailed(
                  BaseMessages.getString(PKG, "JobSSH2GET.Log.Result.TotalFilesPut", "" + nbgot));
              logDetailed(
                  BaseMessages.getString(
                      PKG, "JobSSH2GET.Log.Result.TotalFilesError", "" + nbrerror));
              logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.Result.JobEntryEnd2"));
            }
            if (nbrerror == 0) {
              result.setResult(true);
              /** ****************************** RESULT ******************* */
            }
          }
        }

      } catch (Exception e) {
        result.setNrErrors(nbrerror);
        logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.Error.ErrorFTP", e.getMessage()));
      } finally {
        if (conn != null) {
          conn.close();
        }
        if (client != null) {
          client.close();
        }
      }
    }

    return result;
  }
Пример #15
0
 /**
  * Authenticate using some supported methods. If authentication fails, the method throws {@link
  * IOException}.
  *
  * @param c the connection to use for authentication
  * @throws IOException in case of IO error or authentication failure
  */
 private void authenticate(final Connection c) throws IOException {
   for (String method : myHost.getPreferredMethods()) {
     if (c.isAuthenticationComplete()) {
       return;
     }
     if (PUBLIC_KEY_METHOD.equals(method)) {
       if (!myHost.supportsPubkeyAuthentication()) {
         continue;
       }
       if (!c.isAuthMethodAvailable(myHost.getUser(), PUBLIC_KEY_METHOD)) {
         continue;
       }
       File key = myHost.getIdentityFile();
       if (key == null) {
         for (String a : myHost.getHostKeyAlgorithms()) {
           if (SSH_RSA_ALGORITHM.equals(a)) {
             if (tryPublicKey(c, idRSAPath)) {
               return;
             }
           } else if (SSH_DSS_ALGORITHM.equals(a)) {
             if (tryPublicKey(c, idDSAPath)) {
               return;
             }
           }
         }
       } else {
         if (tryPublicKey(c, key.getPath())) {
           return;
         }
       }
     } else if (KEYBOARD_INTERACTIVE_METHOD.equals(method)) {
       if (!c.isAuthMethodAvailable(myHost.getUser(), KEYBOARD_INTERACTIVE_METHOD)) {
         continue;
       }
       InteractiveSupport interactiveSupport = new InteractiveSupport();
       for (int i = myHost.getNumberOfPasswordPrompts(); i > 0; i--) {
         if (c.isAuthenticationComplete()) {
           return;
         }
         if (c.authenticateWithKeyboardInteractive(myHost.getUser(), interactiveSupport)) {
           myLastError = "";
           return;
         } else {
           myLastError = GitBundle.getString("sshmain.keyboard.interactive.failed");
         }
         if (interactiveSupport.myPromptCount == 0 || interactiveSupport.myCancelled) {
           // the interactive callback has never been asked or it was cancelled, exit the loop
           myLastError = "";
           break;
         }
       }
     } else if (PASSWORD_METHOD.equals(method)) {
       if (!myHost.supportsPasswordAuthentication()) {
         continue;
       }
       if (!c.isAuthMethodAvailable(myHost.getUser(), PASSWORD_METHOD)) {
         continue;
       }
       for (int i = myHost.getNumberOfPasswordPrompts(); i > 0; i--) {
         String password =
             myXmlRpcClient.askPassword(myHandlerNo, getUserHostString(), myLastError);
         if (password == null) {
           break;
         } else {
           if (c.authenticateWithPassword(myHost.getUser(), password)) {
             myLastError = "";
             return;
           } else {
             myLastError = GitBundle.getString("sshmain.password.failed");
           }
         }
       }
     }
   }
   throw new IOException("Authentication failed");
 }
  /**
   * Connects to the given {@link Computer} via SSH and installs Java/Jenkins agent if necessary.
   */
  @Override
  public void launch(SlaveComputer _computer, TaskListener listener) {

    Computer computer = (Computer) _computer;
    PrintStream logger = listener.getLogger();

    Date startDate = new Date();
    logger.println("Start time: " + getUtcDate(startDate));

    final Connection conn;
    Connection cleanupConn = null;
    boolean successful = false;

    try {
      conn = connectToSsh(computer, logger);
      cleanupConn = conn;
      logger.println("Authenticating as " + computer.getRemoteAdmin());
      if (!conn.authenticateWithPublicKey(
          computer.getRemoteAdmin(), computer.getNode().getPrivateKey().toCharArray(), "")) {
        logger.println("Authentication failed");
        throw new Exception("Authentication failed");
      }

      final SCPClient scp = conn.createSCPClient();

      if (!runInitScript(computer, logger, conn, scp)) {
        return;
      }

      if (!installJava(logger, conn)) {
        return;
      }

      logger.println("Copying slave.jar");
      scp.put(Jenkins.getInstance().getJnlpJars("slave.jar").readFully(), "slave.jar", "/tmp");
      String jvmOpts = Util.fixNull(computer.getNode().getJvmOpts());
      String launchString = "java " + jvmOpts + " -jar /tmp/slave.jar";
      logger.println("Launching slave agent: " + launchString);
      final Session sess = conn.openSession();
      sess.execCommand(launchString);
      computer.setChannel(
          sess.getStdout(),
          sess.getStdin(),
          logger,
          new Channel.Listener() {
            @Override
            public void onClosed(Channel channel, IOException cause) {
              sess.close();
              conn.close();
            }
          });

      successful = true;
    } catch (Exception e) {
      LOGGER.log(Level.WARNING, e.getMessage(), e);
      try {
        Jenkins.getInstance().removeNode(computer.getNode());
      } catch (Exception ee) {
        ee.printStackTrace(logger);
      }
      e.printStackTrace(logger);
    } finally {
      Date endDate = new Date();
      logger.println("Done setting up at: " + getUtcDate(endDate));
      logger.println(
          "Done in "
              + TimeUnit2.MILLISECONDS.toSeconds(endDate.getTime() - startDate.getTime())
              + " seconds");
      if (cleanupConn != null && !successful) {
        cleanupConn.close();
      }
    }
  }
 private boolean checkCommand(Connection conn, PrintStream logger, String command)
     throws IOException, InterruptedException {
   logger.println("Checking: " + command);
   return conn.exec(command, logger) == 0;
 }
Пример #18
0
  @Override
  public Map<? extends ServerResource, Map<String, String>> find(
      long dcId,
      Long podId,
      Long clusterId,
      URI uri,
      String username,
      String password,
      List<String> hostTags)
      throws DiscoveryException {

    ClusterVO cluster = _clusterDao.findById(clusterId);
    if (cluster == null || cluster.getHypervisorType() != HypervisorType.KVM) {
      if (s_logger.isInfoEnabled())
        s_logger.info("invalid cluster id or cluster is not for KVM hypervisors");
      return null;
    }

    Map<KvmDummyResourceBase, Map<String, String>> resources =
        new HashMap<KvmDummyResourceBase, Map<String, String>>();
    Map<String, String> details = new HashMap<String, String>();
    if (!uri.getScheme().equals("http")) {
      String msg =
          "urlString is not http so we're not taking care of the discovery for this: " + uri;
      s_logger.debug(msg);
      return null;
    }
    com.trilead.ssh2.Connection sshConnection = null;
    String agentIp = null;
    try {

      String hostname = uri.getHost();
      InetAddress ia = InetAddress.getByName(hostname);
      agentIp = ia.getHostAddress();
      String guid = UUID.nameUUIDFromBytes(agentIp.getBytes()).toString();
      String guidWithTail = guid + "-LibvirtComputingResource"; /*tail added by agent.java*/
      if (_resourceMgr.findHostByGuid(guidWithTail) != null) {
        s_logger.debug(
            "Skipping " + agentIp + " because " + guidWithTail + " is already in the database.");
        return null;
      }

      sshConnection = new com.trilead.ssh2.Connection(agentIp, 22);

      sshConnection.connect(null, 60000, 60000);
      if (!sshConnection.authenticateWithPassword(username, password)) {
        s_logger.debug("Failed to authenticate");
        throw new DiscoveredWithErrorException("Authentication error");
      }

      if (!SSHCmdHelper.sshExecuteCmd(sshConnection, "lsmod|grep kvm", 3)) {
        s_logger.debug("It's not a KVM enabled machine");
        return null;
      }

      List<PhysicalNetworkSetupInfo> netInfos =
          _networkMgr.getPhysicalNetworkInfo(dcId, HypervisorType.KVM);
      String kvmPrivateNic = _kvmPrivateNic;
      String kvmPublicNic = _kvmPublicNic;
      String kvmGuestNic = _kvmGuestNic;

      for (PhysicalNetworkSetupInfo info : netInfos) {
        if (info.getPrivateNetworkName() != null) {
          kvmPrivateNic = info.getPrivateNetworkName();
        }
        if (info.getPublicNetworkName() != null) {
          kvmPublicNic = info.getPublicNetworkName();
        }
        if (info.getGuestNetworkName() != null) {
          kvmGuestNic = info.getGuestNetworkName();
        }
      }

      String parameters =
          " -m " + _hostIp + " -z " + dcId + " -p " + podId + " -c " + clusterId + " -g " + guid
              + " -a";

      if (kvmPublicNic != null) {
        parameters += " --pubNic=" + kvmPublicNic;
      }

      if (kvmPrivateNic != null) {
        parameters += " --prvNic=" + kvmPrivateNic;
      }

      if (kvmGuestNic != null) {
        parameters += " --guestNic=" + kvmGuestNic;
      }

      SSHCmdHelper.sshExecuteCmd(sshConnection, "cloud-setup-agent " + parameters, 3);

      KvmDummyResourceBase kvmResource = new KvmDummyResourceBase();
      Map<String, Object> params = new HashMap<String, Object>();

      params.put("zone", Long.toString(dcId));
      params.put("pod", Long.toString(podId));
      params.put("cluster", Long.toString(clusterId));
      params.put("guid", guid);
      params.put("agentIp", agentIp);
      kvmResource.configure("kvm agent", params);
      resources.put(kvmResource, details);

      HostVO connectedHost = waitForHostConnect(dcId, podId, clusterId, guidWithTail);
      if (connectedHost == null) return null;

      details.put("guid", guidWithTail);

      // place a place holder guid derived from cluster ID
      if (cluster.getGuid() == null) {
        cluster.setGuid(UUID.nameUUIDFromBytes(String.valueOf(clusterId).getBytes()).toString());
        _clusterDao.update(clusterId, cluster);
      }

      // save user name and password
      _hostDao.loadDetails(connectedHost);
      Map<String, String> hostDetails = connectedHost.getDetails();
      hostDetails.put("password", password);
      hostDetails.put("username", username);
      _hostDao.saveDetails(connectedHost);
      return resources;
    } catch (DiscoveredWithErrorException e) {
      throw e;
    } catch (Exception e) {
      String msg = " can't setup agent, due to " + e.toString() + " - " + e.getMessage();
      s_logger.warn(msg);
    } finally {
      if (sshConnection != null) sshConnection.close();
    }

    return null;
  }
  public static void main(String[] args) {
    String hostname = "127.0.0.1";
    String username = "******";
    String password = "******";

    try {
      /* Create a connection instance */

      Connection conn = new Connection(hostname);

      /* Now connect */

      conn.connect();

      /* Authenticate */

      boolean isAuthenticated = conn.authenticateWithPassword(username, password);

      if (isAuthenticated == false) throw new IOException("Authentication failed.");

      /* Create a session */

      Session sess = conn.openSession();

      sess.execCommand("echo \"Text on STDOUT\"; echo \"Text on STDERR\" >&2");

      InputStream stdout = new StreamGobbler(sess.getStdout());
      InputStream stderr = new StreamGobbler(sess.getStderr());

      BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
      BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));

      System.out.println("Here is the output from stdout:");

      while (true) {
        String line = stdoutReader.readLine();
        if (line == null) break;
        System.out.println(line);
      }

      System.out.println("Here is the output from stderr:");

      while (true) {
        String line = stderrReader.readLine();
        if (line == null) break;
        System.out.println(line);
      }

      /* Close this session */

      sess.close();

      /* Close the connection */

      conn.close();

    } catch (IOException e) {
      e.printStackTrace(System.err);
      System.exit(2);
    }
  }