⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smbsession.java

📁 实现网上邻居需要的jar库;可以使用库中的接口实现文件共享的功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }
        return transport;
    }
    void send( ServerMessageBlock request,
                            ServerMessageBlock response ) throws SmbException {
        if( response != null ) {
            response.received = false;
        }

        synchronized(transport.setupDiscoLock) {
            expiration = System.currentTimeMillis() + SmbTransport.SO_TIMEOUT;
            sessionSetup( request, response );
            if( response != null && response.received ) {
                return;
            }
            request.uid = uid;
            request.auth = auth;
            try {
                transport.send( request, response );
            } catch (SmbException se) {
                if (request instanceof SmbComTreeConnectAndX) {
                    logoff(true);
                }
                request.digest = null;
                throw se;
            }
        }
    }
    void sessionSetup( ServerMessageBlock andx,
                            ServerMessageBlock andxResponse ) throws SmbException {
        NtlmContext nctx = null;
        SmbException ex = null;
        SmbComSessionSetupAndX request;
        SmbComSessionSetupAndXResponse response;
        byte[] token = new byte[0];
        int state = 10;

synchronized( transport() ) {
        if( sessionSetup ) {
            return;
        }

        transport.connect();

        /*
         * Session Setup And X Request / Response
         */

        if( transport.log.level >= 4 )
            transport.log.println( "sessionSetup: accountName=" + auth.username + ",primaryDomain=" + auth.domain );

        do {
            switch (state) {
                case 10: /* NTLM */
                    if (auth == NtlmPasswordAuthentication.ANONYMOUS)
                        transport.capabilities &= ~SmbConstants.CAP_EXTENDED_SECURITY;

                    if (transport.hasCapability(SmbConstants.CAP_EXTENDED_SECURITY)) {
                        state = 20; /* NTLMSSP */
                        break;
                    }

                    request = new SmbComSessionSetupAndX( this, andx, auth );
                    response = new SmbComSessionSetupAndXResponse( andxResponse );

                    /* Create SMB signature digest if necessary
                     * Only the first SMB_COM_SESSION_SETUP_ANX with non-null or
                     * blank password initializes signing.
                     */
                    if (transport.isSignatureSetupRequired( auth )) {
                        if( auth.hashesExternal && NtlmPasswordAuthentication.DEFAULT_PASSWORD != NtlmPasswordAuthentication.BLANK ) {
                            /* preauthentication
                             */
                            transport.getSmbSession( NtlmPasswordAuthentication.DEFAULT ).getSmbTree( LOGON_SHARE, null ).treeConnect( null, null );
                        } else {
                            byte[] signingKey = auth.getSigningKey(transport.server.encryptionKey);
                            request.digest = new SigningDigest(signingKey, false);
                        }
                    }

                    request.auth = auth;

                    try {
                        transport.send( request, response );
                    } catch (SmbAuthException sae) {
                        throw sae;
                    } catch (SmbException se) {
                        ex = se;
                    }

                    if( response.isLoggedInAsGuest &&
                                "GUEST".equalsIgnoreCase( auth.username ) == false &&
                                transport.server.security != SmbConstants.SECURITY_SHARE) {
                        throw new SmbAuthException( NtStatus.NT_STATUS_LOGON_FAILURE );
                    }

                    if (ex != null)
                        throw ex;

                    uid = response.uid;

                    if( request.digest != null ) {
                        /* success - install the signing digest */
                        transport.digest = request.digest;
                    }

                    sessionSetup = true;
                    state = 0;

                    break;
                case 20:
                    if (nctx == null) {
                        boolean doSigning = (transport.flags2 & ServerMessageBlock.FLAGS2_SECURITY_SIGNATURES) != 0;
                        nctx = new NtlmContext(auth, doSigning);
                    }

                    if (nctx.isEstablished()) {
                        sessionSetup = true;
                        state = 0;
                        break;
                    }

                    try {
                        token = nctx.initSecContext(token, 0, token.length);
                    } catch (SmbException se) {
                        /* We must close the transport or the server will be expecting a
                         * Type3Message. Otherwise, when we send a Type1Message it will return
                         * "Invalid parameter".
                         */
                        try { transport.disconnect(true); } catch (IOException ioe) {}
                        uid = 0;
                        throw se;
                    }

                    if (token != null) {
                        request = new SmbComSessionSetupAndX(this, null, token);
                        response = new SmbComSessionSetupAndXResponse(null);

                        if (transport.isSignatureSetupRequired( auth )) {
                            byte[] signingKey = nctx.getSigningKey();
                            if (signingKey != null)
                                request.digest = new SigningDigest(signingKey, true);
                        }

                        request.uid = uid;
                        uid = 0;

                        try {
                            transport.send( request, response );
                        } catch (SmbAuthException sae) {
                            throw sae;
                        } catch (SmbException se) {
                            ex = se;
                            /* Apparently once a successfull NTLMSSP login occurs, the
                             * server will return "Access denied" even if a logoff is
                             * sent. Unfortunately calling disconnect() doesn't always
                             * actually shutdown the connection before other threads
                             * have committed themselves (e.g. InterruptTest example).
                             */
                            try { transport.disconnect(true); } catch (Exception e) {}
                        }

                        if( response.isLoggedInAsGuest &&
                                    "GUEST".equalsIgnoreCase( auth.username ) == false) {
                            throw new SmbAuthException( NtStatus.NT_STATUS_LOGON_FAILURE );
                        }

                        if (ex != null)
                            throw ex;

                        uid = response.uid;

                        if (request.digest != null) {
                            /* success - install the signing digest */
                            transport.digest = request.digest;
                        }

                        token = response.blob;
                    }

                    break;
                default:
                    throw new SmbException("Unexpected session setup state: " + state);
            }
        } while (state != 0);
}
    }
    void logoff( boolean inError ) {
synchronized( transport() ) {
        if( sessionSetup == false ) {
            return;
        }

        for( Enumeration e = trees.elements(); e.hasMoreElements(); ) {
            SmbTree t = (SmbTree)e.nextElement();
            t.treeDisconnect( inError );
        }

        if( !inError && transport.server.security != ServerMessageBlock.SECURITY_SHARE ) {
            /*
             * Logoff And X Request / Response
             */
    
            SmbComLogoffAndX request = new SmbComLogoffAndX( null );
            request.uid = uid;
            try {
                transport.send( request, null );
            } catch( SmbException se ) {
            }
            uid = 0;
        }

        sessionSetup = false;
}
    }
    public String toString() {
        return "SmbSession[accountName=" + auth.username +
                ",primaryDomain=" + auth.domain +
                ",uid=" + uid +
                ",sessionSetup=" + sessionSetup + "]";
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -