1 18 19 package com.knowgate.jcifs.smb; 20 21 import java.util.Vector ; 22 import java.util.Enumeration ; 23 import java.net.InetAddress ; 24 import java.net.UnknownHostException ; 25 26 import com.knowgate.jcifs.Config; 27 import com.knowgate.jcifs.UniAddress; 28 import com.knowgate.debug.*; 29 30 53 54 public final class SmbSession { 55 56 private static final String LOGON_SHARE = Config.getProperty( "jcifs.smb.client.logonShare", "IPC$" ); 57 58 public static byte[] getChallenge( UniAddress dc ) 59 throws SmbException, UnknownHostException { 60 SmbTransport trans = SmbTransport.getSmbTransport( dc, 0 ); 61 trans.negotiate(); 62 return trans.server.encryptionKey; 63 } 64 75 public static void logon( UniAddress dc, 76 NtlmPasswordAuthentication auth ) throws SmbException { 77 SmbTransport.getSmbTransport( dc, 0 ).getSmbSession( auth ).getSmbTree( LOGON_SHARE, null ).treeConnect( null, null ); 78 } 79 80 private int uid; 81 private Vector trees; 82 private boolean sessionSetup; 83 private UniAddress address; 85 private int port, localPort; 86 private InetAddress localAddr; 87 88 SmbTransport transport = SmbTransport.NULL_TRANSPORT; 89 NtlmPasswordAuthentication auth; 90 91 SmbSession( UniAddress address, int port, 92 InetAddress localAddr, int localPort, 93 NtlmPasswordAuthentication auth ) { 94 this.address = address; 95 this.port = port; 96 this.localAddr = localAddr; 97 this.localPort = localPort; 98 this.auth = auth; 99 trees = new Vector (); 100 } 101 102 synchronized SmbTree getSmbTree( String share, String service ) { 103 SmbTree t; 104 105 if( share == null ) { 106 share = "IPC$"; 107 } 108 for( Enumeration e = trees.elements(); e.hasMoreElements(); ) { 109 t = (SmbTree)e.nextElement(); 110 if( t.matches( share, service )) { 111 return t; 112 } 113 } 114 t = new SmbTree( this, share, service ); 115 trees.addElement( t ); 116 return t; 117 } 118 boolean matches( NtlmPasswordAuthentication auth ) { 119 return this.auth == auth || this.auth.equals( auth ); 120 } 121 synchronized SmbTransport transport() throws SmbException { 122 if( transport == SmbTransport.NULL_TRANSPORT ) { 123 transport = SmbTransport.getSmbTransport( address, port, localAddr, localPort ); 124 } 125 return transport; 126 } 127 void sendTransaction( SmbComTransaction request, 128 SmbComTransactionResponse response ) throws SmbException { 129 sessionSetup( null, null ); 131 request.uid = uid; 132 request.auth = auth; 133 transport().sendTransaction( request, response ); 134 } 135 void send( ServerMessageBlock request, 136 ServerMessageBlock response ) throws SmbException { 137 if( response != null ) { 138 response.received = false; 139 } 140 sessionSetup( request, response ); 141 if( response != null && response.received ) { 142 return; 143 } 144 request.uid = uid; 145 request.auth = auth; 146 transport().send( request, response ); 147 } 148 void sessionSetup( ServerMessageBlock andx, 149 ServerMessageBlock andxResponse ) throws SmbException { 150 151 synchronized( transport() ) { 152 if( sessionSetup ) { 153 return; 154 } 155 156 transport.negotiate(); 157 158 161 162 if( DebugFile.trace ) 163 DebugFile.writeln( "sessionSetup: accountName=" + auth.username + ",primaryDomain=" + auth.domain ); 164 165 SmbComSessionSetupAndX request = new SmbComSessionSetupAndX( this, andx ); 166 SmbComSessionSetupAndXResponse response = new SmbComSessionSetupAndXResponse( andxResponse ); 167 168 171 if( transport.isSignatureSetupRequired( auth )) { 172 if( auth.hashesExternal && NtlmPasswordAuthentication.DEFAULT_PASSWORD != null ) { 173 175 transport.getSmbSession( NtlmPasswordAuthentication.DEFAULT ).getSmbTree( LOGON_SHARE, null ).treeConnect( null, null ); 176 } 177 request.digest = new SigningDigest( transport, auth ); 178 } 179 180 request.auth = auth; 181 transport.send( request, response ); 182 183 if( response.isLoggedInAsGuest && "GUEST".equals( auth.username )) { 184 throw new SmbAuthException( NtStatus.NT_STATUS_LOGON_FAILURE ); 185 } 186 187 uid = response.uid; 188 sessionSetup = true; 189 190 if( request.digest != null ) { 191 192 transport.digest = request.digest; 193 } 194 } 195 } 196 void logoff( boolean inError ) { 197 synchronized( transport ) { 198 try { 199 if( sessionSetup == false ) { 200 return; 201 } 202 203 for( Enumeration e = trees.elements(); e.hasMoreElements(); ) { 204 SmbTree t = (SmbTree)e.nextElement(); 205 t.treeDisconnect( inError ); 206 } 207 208 if( transport.server.security == ServerMessageBlock.SECURITY_SHARE ) { 209 return; 210 } 211 212 if( !inError ) { 213 214 217 218 SmbComLogoffAndX request = new SmbComLogoffAndX( null ); 219 request.uid = uid; 220 try { 221 transport.send( request, null ); 222 } catch( SmbException se ) { 223 } 224 } 225 sessionSetup = false; 226 } finally { 227 transport = SmbTransport.NULL_TRANSPORT; 228 } 229 } 230 } 231 public String toString() { 232 return "SmbSession[accountName=" + auth.username + 233 ",primaryDomain=" + auth.domain + 234 ",uid=" + uid + 235 ",sessionSetup=" + sessionSetup + "]"; 236 } 237 } 238 | Popular Tags |