1 19 20 package jcifs.http; 21 22 import java.io.IOException ; 23 24 import java.net.UnknownHostException ; 25 26 import java.util.Enumeration ; 27 28 import javax.servlet.ServletConfig ; 29 import javax.servlet.ServletException ; 30 import javax.servlet.UnavailableException ; 31 32 import javax.servlet.http.HttpSession ; 33 import javax.servlet.http.HttpServlet ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 37 import jcifs.Config; 38 import jcifs.UniAddress; 39 40 import jcifs.smb.NtlmPasswordAuthentication; 41 import jcifs.smb.SmbAuthException; 42 import jcifs.smb.SmbSession; 43 44 import jcifs.util.Base64; 45 46 import jcifs.netbios.NbtAddress; 47 48 59 60 public abstract class NtlmServlet extends HttpServlet { 61 62 private String defaultDomain; 63 64 private String domainController; 65 66 private boolean loadBalance; 67 68 private boolean enableBasic; 69 70 private boolean insecureBasic; 71 72 private String realm; 73 74 public void init(ServletConfig config) throws ServletException { 75 super.init(config); 76 77 79 Config.setProperty( "jcifs.smb.client.soTimeout", "300000" ); 80 Config.setProperty( "jcifs.netbios.cachePolicy", "600" ); 81 82 Enumeration e = config.getInitParameterNames(); 83 String name; 84 while (e.hasMoreElements()) { 85 name = (String ) e.nextElement(); 86 if (name.startsWith("jcifs.")) { 87 Config.setProperty(name, config.getInitParameter(name)); 88 } 89 } 90 defaultDomain = Config.getProperty("jcifs.smb.client.domain"); 91 domainController = Config.getProperty("jcifs.http.domainController"); 92 if( domainController == null ) { 93 domainController = defaultDomain; 94 loadBalance = Config.getBoolean( "jcifs.http.loadBalance", true ); 95 } 96 enableBasic = Boolean.valueOf( 97 Config.getProperty("jcifs.http.enableBasic")).booleanValue(); 98 insecureBasic = Boolean.valueOf( 99 Config.getProperty("jcifs.http.insecureBasic")).booleanValue(); 100 realm = Config.getProperty("jcifs.http.basicRealm"); 101 if (realm == null) realm = "jCIFS"; 102 } 103 104 protected void service(HttpServletRequest request, 105 HttpServletResponse response) throws ServletException , IOException { 106 UniAddress dc; 107 boolean offerBasic = enableBasic && 108 (insecureBasic || request.isSecure()); 109 String msg = request.getHeader("Authorization"); 110 if (msg != null && (msg.startsWith("NTLM ") || 111 (offerBasic && msg.startsWith("Basic ")))) { 112 if( loadBalance ) { 113 dc = new UniAddress( NbtAddress.getByName( domainController, 0x1C, null )); 114 } else { 115 dc = UniAddress.getByName( domainController, true ); 116 } 117 NtlmPasswordAuthentication ntlm; 118 if (msg.startsWith("NTLM ")) { 119 byte[] challenge = SmbSession.getChallenge(dc); 120 ntlm = NtlmSsp.authenticate(request, response, challenge); 121 if (ntlm == null) return; 122 } else { 123 String auth = new String (Base64.decode(msg.substring(6)), 124 "US-ASCII"); 125 int index = auth.indexOf(':'); 126 String user = (index != -1) ? auth.substring(0, index) : auth; 127 String password = (index != -1) ? auth.substring(index + 1) : 128 ""; 129 index = user.indexOf('\\'); 130 if (index == -1) index = user.indexOf('/'); 131 String domain = (index != -1) ? user.substring(0, index) : 132 defaultDomain; 133 user = (index != -1) ? user.substring(index + 1) : user; 134 ntlm = new NtlmPasswordAuthentication(domain, user, password); 135 } 136 try { 137 SmbSession.logon(dc, ntlm); 138 } catch (SmbAuthException sae) { 139 response.setHeader("WWW-Authenticate", "NTLM"); 140 if (offerBasic) { 141 response.addHeader("WWW-Authenticate", "Basic realm=\"" + 142 realm + "\""); 143 } 144 response.setHeader("Connection", "close"); 145 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 146 response.flushBuffer(); 147 return; 148 } 149 HttpSession ssn = request.getSession(); 150 ssn.setAttribute("NtlmHttpAuth", ntlm); 151 ssn.setAttribute( "ntlmdomain", ntlm.getDomain() ); 152 ssn.setAttribute( "ntlmuser", ntlm.getUsername() ); 153 } else { 154 HttpSession ssn = request.getSession(false); 155 if (ssn == null || ssn.getAttribute("NtlmHttpAuth") == null) { 156 response.setHeader("WWW-Authenticate", "NTLM"); 157 if (offerBasic) { 158 response.addHeader("WWW-Authenticate", "Basic realm=\"" + 159 realm + "\""); 160 } 161 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 162 response.flushBuffer(); 163 return; 164 } 165 } 166 super.service(request, response); 167 } 168 } 169 170 | Popular Tags |