Examples of JSchException


Examples of com.jcraft.jsch.JSchException

    String remoteFilePath = remoteFileInfo.filePath;
    String tempRemotePath = tempNameForSourceFile(remoteFilePath);
    File localFile = null;
    long bytesCopied = 0L;
    IOException cleanupIOException = null;
    JSchException cleanupJSchException = null;
    boolean success = false;
   
    // Copy file and validate result
    try {
      // Rename the remote file with a temporary name before copying 
      if(logger.isDebugEnabled()) {
        logger.debug("Renaming remote file to: {}", tempRemotePath);
      }
      sshCommand.execShellCommand("mv " + remoteFilePath + " " + tempRemotePath);

      // Copy the (renamed) remote file to local destination
      remoteFileInfo.filePath = tempRemotePath;  // remote file name is needed during validation
      bytesCopied = sshCommand.copyFrom(tempRemotePath, localFilePath);
      localFile = new File(localFilePath);
      long modTime =  (long) (Double.parseDouble(remoteFileInfo.fileModtime) * 1000.0);
      localFile.setLastModified(modTime);
     
      // Check integrity of copy
      success = validateRemoteFileCopy(sshCommand, remoteFileInfo, localFilePath);
      if (!success) {
        logger.warn("Copy of file {} did not pass integrity check!", remoteFilePath);
      }
    } catch (IOException ex) {
      // If there's been an error copying the file, we may be left with a zero-length or incomplete file
      if ((localFile != null) && localFile.exists() && !success) {
        if(logger.isDebugEnabled()) {
          logger.debug("Deleting failed local copy of remote file: {}", localFile.getAbsolutePath());
        }
        localFile.delete();
      }
    } finally {
      // Use a try-block during cleanup, but only throw exception if the
      // main file-copy try-block doesn't
      try {
        if (deleteSource && success) {
          if(logger.isDebugEnabled()) {
            logger.debug("Deleting remote file: {}", tempRemotePath);
          }
          sshCommand.execShellCommand("rm " + tempRemotePath);
        } else {
          // Move source file back from temporary name
          sshCommand.execShellCommand("mv " + tempRemotePath + " " + remoteFilePath);
        }
      } catch (JSchException ex) {
        logger.warn("JSchException during remote file copy cleanup: {}", ex);
        cleanupJSchException = new JSchException(ex.getMessage());       
      } catch (IOException ex) {
        logger.warn("IOException during remote file copy cleanup: {}", ex);
        cleanupIOException = new IOException(ex);
      }
      remoteFileInfo.filePath = remoteFilePath;  // restore original file name in argument
View Full Code Here

Examples of com.jcraft.jsch.JSchException

  }
 
  private static String execTailOnRemoteFile(SSHCommand sshCommand, String remoteFilePath, int numLines) throws JSchException, IOException
  {
    if ((sshCommand == null) || !sshCommand.isConnected()) {
      throw new JSchException("Not connected with a valid SSH session");
    }
    String numLinesArg = String.valueOf(numLines);
    if ((numLines < 0) || (numLinesArg == null) || (numLinesArg.length() == 0) || (remoteFilePath == null) || (remoteFilePath.length() == 0)) {
      return null;
    } else if (numLines == 0) {
View Full Code Here

Examples of com.jcraft.jsch.JSchException

            if (credential == null) {
                try {
                    credential = getCredential();
                } catch (SecurityException t) {
                    System.out.printf("Could not get proxy: %s: %s\n", t.getClass().getSimpleName(), t.getMessage());
                    throw new JSchException(t.toString());
                }
            }

            String cname = host;

            try {
                cname = InetAddress.getByName(cname).getCanonicalHostName();
            } catch (UnknownHostException e) {
            }

            GSSName name = HostAuthorization.getInstance().getExpectedName(credential, cname);

//      context = manager.createContext(name, null, credential, GSSContext.DEFAULT_LIFETIME);
//
//      // RFC4462 3.4. GSS-API Session
//      //
//      // When calling GSS_Init_sec_context(), the client MUST set
//      // integ_req_flag to "true" to request that per-message integrity
//      // protection be supported for this context. In addition,
//      // deleg_req_flag MAY be set to "true" to request access delegation,
//      // if
//      // requested by the user.
//      //
//      // Since the user authentication process by its nature authenticates
//      // only the client, the setting of mutual_req_flag is not needed for
//      // this process. This flag SHOULD be set to "false".
//
//      // TODO: OpenSSH's sshd does accept 'false' for mutual_req_flag
//      // context.requestMutualAuth(false);
//      context.requestMutualAuth(true);
//      context.requestConf(true);
//      context.requestInteg(true); // for MIC
//      context.requestCredDeleg(true);
//      context.requestAnonymity(false);

            context = new BCGSSContextImpl(name, (GlobusGSSCredentialImpl) credential);
            context.requestLifetime(GSSCredential.DEFAULT_LIFETIME);
            context.requestCredDeleg(true);
            context.requestMutualAuth(true);
            context.requestReplayDet(true);
            context.requestSequenceDet(true);
            context.requestConf(false);
            context.requestInteg(true);
            ((ExtendedGSSContext)context).setOption(GSSConstants.DELEGATION_TYPE, GSIConstants.DELEGATION_TYPE_FULL);

            return;
        } catch (GSSException ex) {
            throw new JSchException(ex.toString());
        }
    }
View Full Code Here

Examples of com.jcraft.jsch.JSchException

    public byte[] init(byte[] token, int s, int l) throws JSchException {
        try {
            return context.initSecContext(token, s, l);
        } catch (GSSException ex) {
            throw new JSchException(ex.toString());
        }
    }
View Full Code Here

Examples of com.jcraft.jsch.JSchException

      context.requestAnonymity(false);

      return;
    }
    catch(GSSException ex){
      throw new JSchException(ex.toString());
    }
  }
View Full Code Here

Examples of com.jcraft.jsch.JSchException

        setSystemProperty(pUseSubjectCredsOnly, "false");
      }
      return context.initSecContext(token, 0, l);
    }
    catch(GSSException ex){
      throw new JSchException(ex.toString());
    }
    catch(java.lang.SecurityException ex){
      throw new JSchException(ex.toString());
    }
    finally{
      if(useSubjectCredsOnly==null){
        // By the default, it must be "true".
        setSystemProperty(pUseSubjectCredsOnly, "true");
View Full Code Here

Examples of com.jcraft.jsch.JSchException

              monitor);
        } catch (JSchException e) {
          throw e;
        }
        if (session == null) {
          throw new JSchException(Messages.SSHTunnelSession_0
              + hostname);
        }
        if (session.getTimeout() != DEFAULT_TIMEOUT) {
          session.setTimeout(DEFAULT_TIMEOUT);
        }
View Full Code Here

Examples of org.vngx.jsch.exception.JSchException

  public static KeyPair genKeyPair(KeyType type, int keySize) throws JSchException {
    KeyPair keyPair;
    switch( type ) {
      case SSH_DSS: keyPair = new KeyPairDSA(); break;
      case SSH_RSA: keyPair = new KeyPairRSA(); break;
      default: throw new JSchException("Unsupported KeyType: "+type);
    }
    keyPair.generate(keySize)// Generate keys for the specified size
    return keyPair;        // and return key pair
  }
View Full Code Here

Examples of org.vngx.jsch.exception.JSchException

        }
        System.arraycopy(hn, 0, key, 0, key.length);
        return key;

      default// No support for other vendor types
        throw new JSchException("Unsupported vendor type: "+_vendor);
    }
  }
View Full Code Here

Examples of org.vngx.jsch.exception.JSchException

            type = KeyType.SSH_RSA;
          } else if( buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H' ) { // FSecure
            type = KeyType.UNKNOWN;
            vendor = VENDOR_FSECURE;
          } else {
            throw new JSchException("invalid privatekey: " + prvkey);
          }
          i += 3;
          continue;
        }
        if( buf[i] == 'C' && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf[i + 3] == ',' ) {
          i += 4;
          for( int ii = 0; ii < iv.length; ii++ ) {
            iv[ii] = (byte) (((DataUtil.a2b(buf[i++]) << 4) & 0xf0) + (DataUtil.a2b(buf[i++]) & 0xf));
          }
          continue;
        }
        if( buf[i] == 0x0d
            && i + 1 < buf.length && buf[i + 1] == 0x0a ) {
          i++;
          continue;
        }
        if( buf[i] == 0x0a && i + 1 < buf.length ) {
          if( buf[i + 1] == 0x0a ) {
            i += 2;
            break;
          }
          if( buf[i + 1] == 0x0d
              && i + 2 < buf.length && buf[i + 2] == 0x0a ) {
            i += 3;
            break;
          }
          boolean inheader = false;
          for( int j = i + 1; j < buf.length; j++ ) {
            if( buf[j] == 0x0a ) {
              break;
            }
            //if(buf[j]==0x0d) break;
            if( buf[j] == ':' ) {
              inheader = true;
              break;
            }
          }
          if( !inheader ) {
            i++;
            encrypted = false;    // no passphrase
            break;
          }
        }
        i++;
      }

      if( type == null ) {
        throw new JSchException("invalid privatekey: " + prvkey);
      }

      int start = i;
      while( i < len ) {
        if( buf[i] == 0x0a ) {
          boolean xd = (buf[i - 1] == 0x0d);
          System.arraycopy(buf, i + 1,
              buf,
              i - (xd ? 1 : 0),
              len - i - 1 - (xd ? 1 : 0));
          if( xd ) {
            len--;
          }
          len--;
          continue;
        }
        if( buf[i] == '-' ) {
          break;
        }
        i++;
      }
      data = Util.fromBase64(buf, start, i - start);

      if( data.length > 4 && // FSecure
          data[0] == (byte) 0x3f
          && data[1] == (byte) 0x6f
          && data[2] == (byte) 0xf9
          && data[3] == (byte) 0xeb ) {

        Buffer _buf = new Buffer(data);
        _buf.getInt()// 0x3f6ff9be
        _buf.getInt();
        @SuppressWarnings("unused")
        byte[] _type = _buf.getString();
        byte[] _cipher = _buf.getString();
        String cipher = Util.byte2str(_cipher);
        if( cipher.equals("3des-cbc") ) {
          _buf.getInt();
          byte[] foo = new byte[data.length - _buf.getOffSet()];
          _buf.getBytes(foo);
          data = foo;
          encrypted = true;
          throw new JSchException("unknown privatekey format: " + prvkey);
        } else if( cipher.equals("none") ) {
          _buf.getInt();
          _buf.getInt();
          encrypted = false;
          byte[] foo = new byte[data.length - _buf.getOffSet()];
          _buf.getBytes(foo);
          data = foo;
        }
      }

      if( pubkey != null ) {
        try {
          fc = new FileInputStream(pubkey).getChannel();
          len = (int) fc.size();
          fc.read(ByteBuffer.wrap(buf = new byte[len]));
          fc.close();

          if( buf.length > 4 && // FSecure's public key
              buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-' ) {

            boolean valid = true;
            i = 0;
            do {
              i++;
            } while( buf.length > i && buf[i] != 0x0a );
            if( buf.length <= i ) {
              valid = false;
            }

            while( valid ) {
              if( buf[i] == 0x0a ) {
                boolean inheader = false;
                for( int j = i + 1; j < buf.length; j++ ) {
                  if( buf[j] == 0x0a ) {
                    break;
                  }
                  if( buf[j] == ':' ) {
                    inheader = true;
                    break;
                  }
                }
                if( !inheader ) {
                  i++;
                  break;
                }
              }
              i++;
            }
            if( buf.length <= i ) {
              valid = false;
            }

            start = i;
            while( valid && i < len ) {
              if( buf[i] == 0x0a ) {
                System.arraycopy(buf, i + 1, buf, i, len - i - 1);
                len--;
                continue;
              }
              if( buf[i] == '-' ) {
                break;
              }
              i++;
            }
            if( valid ) {
              publickeyblob = Util.fromBase64(buf, start, i - start);
              if( type == KeyType.UNKNOWN ) {
                if( publickeyblob[8] == 'd' ) {
                  type = KeyType.SSH_DSS;
                } else if( publickeyblob[8] == 'r' ) {
                  type = KeyType.SSH_RSA;
                }
              }
            }
          } else {
            if( buf[0] == 's' && buf[1] == 's' && buf[2] == 'h' && buf[3] == '-' ) {
              i = 0;
              while( i < len ) {
                if( buf[i] == ' ' ) {
                  break;
                }
                i++;
              }
              i++;
              if( i < len ) {
                start = i;
                while( i < len ) {
                  if( buf[i] == ' ' ) {
                    break;
                  }
                  i++;
                }
                publickeyblob = Util.fromBase64(buf, start, i - start);
              }
            }
          }
        } catch(Exception ee) {
          // TODO Error handling???
        }
      }
    } catch(JSchException e) {
      throw e;
    } catch(Exception e) {
      throw new JSchException("Failed to load KeyPair: "+e, e);
    }

    KeyPair keyPair = null;
    switch( type ) {
      case SSH_DSS: keyPair = new KeyPairDSA(); break;
      case SSH_RSA: keyPair = new KeyPairRSA(); break;
      default: throw new JSchException("Unsupported key type: "+type);
    }
    keyPair._encrypted = encrypted;
    keyPair._publicKeyBlob = publickeyblob;
    keyPair._vendor = vendor;

    if( encrypted ) {
      keyPair._iv = iv;
      keyPair._data = data;
    } else if( !keyPair.parse(data) ) {
      throw new JSchException("Invalid private key: " + prvkey);
    }
    return keyPair;
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.