001: /* jcifs smb client library in Java
002: * Copyright (C) 2002 "Michael B. Allen" <jcifs at samba dot org>
003: * "Eric Glass" <jcifs at samba dot org>
004: * "Jason Pugsley" <jcifs at samba dot org>
005: * "skeetz" <jcifs at samba dot org>
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: package jcifs.http;
023:
024: import java.io.IOException;
025:
026: import javax.servlet.ServletException;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import jcifs.smb.NtlmPasswordAuthentication;
032:
033: import jcifs.util.Base64;
034:
035: import jcifs.ntlmssp.NtlmFlags;
036: import jcifs.ntlmssp.Type1Message;
037: import jcifs.ntlmssp.Type2Message;
038: import jcifs.ntlmssp.Type3Message;
039:
040: /**
041: * This class is used internally by <tt>NtlmHttpFilter</tt>,
042: * <tt>NtlmServlet</tt>, and <tt>NetworkExplorer</tt> to negiotiate password
043: * hashes via NTLM SSP with MSIE. It might also be used directly by servlet
044: * containers to incorporate similar functionality.
045: * <p>
046: * How NTLMSSP is used in conjunction with HTTP and MSIE clients is
047: * described in an <A HREF="http://www.innovation.ch/java/ntlm.html">NTLM
048: * Authentication Scheme for HTTP</A>. <p> Also, read <a
049: * href="../../../ntlmhttpauth.html">jCIFS NTLM HTTP Authentication and
050: * the Network Explorer Servlet</a> related information.
051: */
052:
053: public class NtlmSsp implements NtlmFlags {
054:
055: /**
056: * Calls the static {@link #authenticate(HttpServletRequest,
057: * HttpServletResponse, byte[])} method to perform NTLM authentication
058: * for the specified servlet request.
059: *
060: * @param req The request being serviced.
061: * @param resp The response.
062: * @param challenge The domain controller challenge.
063: * @throws IOException If an IO error occurs.
064: * @throws ServletException If an error occurs.
065: */
066: public NtlmPasswordAuthentication doAuthentication(
067: HttpServletRequest req, HttpServletResponse resp,
068: byte[] challenge) throws IOException, ServletException {
069: return authenticate(req, resp, challenge);
070: }
071:
072: /**
073: * Performs NTLM authentication for the servlet request.
074: *
075: * @param req The request being serviced.
076: * @param resp The response.
077: * @param challenge The domain controller challenge.
078: * @throws IOException If an IO error occurs.
079: * @throws ServletException If an error occurs.
080: */
081: public static NtlmPasswordAuthentication authenticate(
082: HttpServletRequest req, HttpServletResponse resp,
083: byte[] challenge) throws IOException, ServletException {
084: String msg = req.getHeader("Authorization");
085: if (msg != null && msg.startsWith("NTLM ")) {
086: byte[] src = Base64.decode(msg.substring(5));
087: if (src[8] == 1) {
088: Type1Message type1 = new Type1Message(src);
089: Type2Message type2 = new Type2Message(type1, challenge,
090: null);
091: msg = Base64.encode(type2.toByteArray());
092: resp.setHeader("WWW-Authenticate", "NTLM " + msg);
093: } else if (src[8] == 3) {
094: Type3Message type3 = new Type3Message(src);
095: byte[] lmResponse = type3.getLMResponse();
096: if (lmResponse == null)
097: lmResponse = new byte[0];
098: byte[] ntResponse = type3.getNTResponse();
099: if (ntResponse == null)
100: ntResponse = new byte[0];
101: return new NtlmPasswordAuthentication(
102: type3.getDomain(), type3.getUser(), challenge,
103: lmResponse, ntResponse);
104: }
105: } else {
106: resp.setHeader("WWW-Authenticate", "NTLM");
107: }
108: resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
109: resp.flushBuffer();
110: return null;
111: }
112:
113: }
|