Package org.vngx.jsch.exception

Examples of org.vngx.jsch.exception.JSchException


        int count = 5// Make this configurable, 5 attempts to enter correct passphrase
        while( count-- > 0 ) {
          if( identity.isEncrypted() ) {
            if( _userinfo == null ) {
              throw new JSchException("UserAuth 'publickey' fail: identity is encrypted, no passphrase");
            } else if( !_userinfo.promptPassphrase(String.format(MessageConstants.PROMPT_PASSPHRASE, identity.getName())) ) {
              throw new AuthCancelException("UserAuth 'publickey' canceled by user");
            }
            if( _userinfo.getPassphrase() != null ) {
              passphrase = Util.str2byte(_userinfo.getPassphrase());
View Full Code Here


   */
  public static boolean authenticateUser(Session session, byte[] password) throws Exception {
    // Retrieve list of preferred client user auth methods
    LinkedList<String> clientMethods = new LinkedList<String>(session.getConfig().getList(SessionConfig.PREFFERED_AUTHS));
    if( clientMethods.isEmpty() ) {
      throw new JSchException("UserAuth failure, no client preferred authentication methods in config");
    }
    clientMethods.addFirst(UserAuth.NONE)// Add 'none' first to retrieve available server methods

    // Request User Auth service to being auth process
    sendUserAuthInit(session);

    boolean authCanceled = false;
    Set<String> serverMethods = new HashSet<String>(Arrays.asList(UserAuth.NONE));
    UserAuth userAuth;

    // Attempt to perform user auth using each of the client preferred methods
    for( String userAuthMethod : clientMethods ) {
      // 'none' can always be sent, otherwise check if auth method is supported by server
      // TODO Add config override to allow sending auth methods even if not listed as supported
      if( !UserAuth.NONE.equals(userAuthMethod) && !serverMethods.contains(userAuthMethod) ) {
        continue// Server does not support user auth method, skip
      }
      JSch.getLogger().log(Level.INFO, "Authentication methods that can continue: " + (serverMethods != null ? serverMethods : NONE));
      JSch.getLogger().log(Level.INFO, "Current authentication method: " + userAuthMethod);

      try // Attempt to create UserAuth method instance
        userAuth = session.getConfig().getClassImpl(Algorithms.USERAUTH + userAuthMethod);
      } catch(Exception e) {
        JSch.getLogger().log(Level.WARN, "Failed to load UserAuth method '" + userAuthMethod + "': "+e, e);
        continue// Attempt next user auth method since this one failed/not supported...
      }

      authCanceled = false;
      try {
        // Attempt to authenticate user with method
        if( userAuth.authUser(session, password) ) {
          JSch.getLogger().log(Level.INFO, "Authentication succeeded, method: " + userAuthMethod);
          return true// Return true since user has been authed!
        }
      } catch(AuthCancelException ee) {
        authCanceled = true;
      } catch(PartialAuthException pe) {
        authCanceled = false;
        serverMethods = pe.getUserAuthMethods()// Update server list of user auth methods
      }
    }

    // If not authenticated, throw appropriate exception
    if( authCanceled ) {
      throw new AuthCancelException("User authentication canceled by user");
    }
    throw new JSchException("User authentication failed");
  }
View Full Code Here

    // receive user auth response
    // byte      SSH_MSG_SERVICE_ACCEPT(6)
    // string    service name
    if( session.read(buffer).getCommand() != SSH_MSG_SERVICE_ACCEPT ) {
      throw new JSchException("UserAuth service failed, expected SSH_MSG_SERVICE_ACCEPT(6): "+buffer.getCommand());
    }
    JSch.getLogger().log(Level.INFO, "SSH_MSG_SERVICE_ACCEPT for UserAuth received");
  }
View Full Code Here

      _context.requestConf(true);
      _context.requestInteg(true);             // for MIC
      _context.requestCredDeleg(true);
      _context.requestAnonymity(false);
    } catch(GSSException ex) {
      throw new JSchException("Failed to create GSSContextKrb5: "+ex, ex);
    }
  }
View Full Code Here

      if( $useSubjectCredsOnly == null ) {
        setSystemProperty(USE_SUBJECT_CREDS_ONLY, "false");
      }
      return _context.initSecContext(token, 0, length);
    } catch(GSSException ex) {
      throw new JSchException("Failed to init GSSContextKrb5: "+ex, ex);
    } catch(SecurityException ex) {
      throw new JSchException("Failed to init GSSContextKrb5: "+ex, ex);
    } finally {
      if( $useSubjectCredsOnly == null ) {
        // By default, it must be "true".
        setSystemProperty(USE_SUBJECT_CREDS_ONLY, "true");
      }
View Full Code Here

        byte[] byteAddress = addr.getAddress();
        for( int i = 0; i < byteAddress.length; i++ ) {
          buf[index++] = byteAddress[i];
        }
      } catch(UnknownHostException uhe) {
        throw new JSchException("ProxySOCKS4: " + uhe, uhe);
      }

      if( _user != null ) {
        System.arraycopy(Util.str2byte(_user), 0, buf, index, _user.length());
        index += _user.length();
      }
      buf[index++] = 0;
      _proxyOut.write(buf, 0, index);

      /*
      The SOCKS server checks to see whether such a request should be granted
      based on any combination of source IP address, destination IP address,
      destination port number, the userid, and information it may obtain by
      consulting IDENT, cf. RFC 1413.  If the request is granted, the SOCKS
      server makes a connection to the specified port of the destination host.
      A reply packet is sent to the client when this connection is established,
      or when the request is rejected or the operation fails.

      +----+----+----+----+----+----+----+----+
      | VN | CD | DSTPORT |      DSTIP        |
      +----+----+----+----+----+----+----+----+
      # of bytes:   1    1      2              4

      VN is the version of the reply code and should be 0. CD is the result
      code with one of the following values:

      90: request granted
      91: request rejected or failed
      92: request rejected becasue SOCKS server cannot connect to
      identd on the client
      93: request rejected because the client program and identd
      report different user-ids

      The remaining fields are ignored.
       */

      int len = 8, s = 0, i;
      while( s < len ) {
        if( (i = _proxyIn.read(buf, s, len - s)) <= 0 ) {
          throw new JSchException("ProxySOCKS4: stream is closed");
        }
        s += i;
      }
      if( buf[0] != 0 ) {
        throw new JSchException("ProxySOCKS4: server returns VN " + buf[0]);
      }
      if( buf[1] != 90 ) {
        throw new JSchException("ProxySOCKS4: server returns CD " + buf[1]);
      }
    } catch(JSchException e) {
      close()// If error occured, close all resources!
      throw e;
    } catch(Exception e) {
      close()// If error occured, close all resources!
      throw new JSchException("Failed to connect ProxySOCKS4: " + e, e);
    }
  }
View Full Code Here

          break;
        default:
      }

      if( !check ) {
        throw new JSchException("Failed to connect ProxySOCKS5 (check)");
      }

      /*
      The SOCKS request is formed as follows:

      +----+-----+-------+------+----------+----------+
      |VER | CMD |  RSV  | ATYP | DST.ADDR | DST.PORT |
      +----+-----+-------+------+----------+----------+
      | 1  |  1  | X'00' |  1   | Variable |    2     |
      +----+-----+-------+------+----------+----------+

      Where:

      o  VER    protocol version: X'05'
      o  CMD
      o  CONNECT X'01'
      o  BIND X'02'
      o  UDP ASSOCIATE X'03'
      o  RSV    RESERVED
      o  ATYP   address type of following address
      o  IP V4 address: X'01'
      o  DOMAINNAME: X'03'
      o  IP V6 address: X'04'
      o  DST.ADDR       desired destination address
      o  DST.PORT desired destination port in network octet
      order
       */

      index = 0;
      buf[index++] = 5;
      buf[index++] = 1;       // CONNECT
      buf[index++] = 0;

      byte[] hostb = Util.str2byte(host);
      int len = hostb.length;
      buf[index++] = 3;      // DOMAINNAME
      buf[index++] = (byte) (len);
      System.arraycopy(hostb, 0, buf, index, len);
      index += len;
      buf[index++] = (byte) (port >>> 8);
      buf[index++] = (byte) (port & 0xff);

      _proxyOut.write(buf, 0, index);

      /*
      The SOCKS request information is sent by the client as soon as it has
      established a connection to the SOCKS server, and completed the
      authentication negotiations.  The server evaluates the request, and
      returns a reply formed as follows:

      +----+-----+-------+------+----------+----------+
      |VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
      +----+-----+-------+------+----------+----------+
      | 1  |  1  | X'00' |  1   | Variable |    2     |
      +----+-----+-------+------+----------+----------+

      Where:

      o  VER    protocol version: X'05'
      o  REP    Reply field:
      o  X'00' succeeded
      o  X'01' general SOCKS server failure
      o  X'02' connection not allowed by ruleset
      o  X'03' Network unreachable
      o  X'04' Host unreachable
      o  X'05' Connection refused
      o  X'06' TTL expired
      o  X'07' Command not supported
      o  X'08' Address type not supported
      o  X'09' to X'FF' unassigned
      o  RSV    RESERVED
      o  ATYP   address type of following address
      o  IP V4 address: X'01'
      o  DOMAINNAME: X'03'
      o  IP V6 address: X'04'
      o  BND.ADDR       server bound address
      o  BND.PORT       server bound port in network octet order
       */

      //in.read(buf, 0, 4);
      fill(_proxyIn, buf, 4);

      if( buf[1] != 0 ) {
        throw new JSchException("ProxySOCKS5: server returns " + buf[1]);
      }

      switch(buf[3] & 0xff) {
        case 1:
          //in.read(buf, 0, 6);
          fill(_proxyIn, buf, 6);
          break;
        case 3:
          //in.read(buf, 0, 1);
          fill(_proxyIn, buf, 1);
          //in.read(buf, 0, buf[0]+2);
          fill(_proxyIn, buf, (buf[0] & 0xff) + 2);
          break;
        case 4:
          //in.read(buf, 0, 18);
          fill(_proxyIn, buf, 18);
          break;
        default:
      }
    } catch(JSchException e) {
      close()// If error occured, close all resources!
      throw e;
    } catch(Exception e) {
      close()// If error occured, close all resources!
      throw new JSchException("Failed to connect ProxySOCKS5: "+e, e);
    }
  }
View Full Code Here

          break;
        }
      }
    } catch(Exception e) {
      close()// If error occured, close all resources!
      throw new JSchException("Failed to connect ProxyHTTP: "+e, e);
    }
  }
View Full Code Here

TOP

Related Classes of org.vngx.jsch.exception.JSchException

Copyright © 2018 www.massapicom. 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.