KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > http > NtlmServlet


1 /* jcifs smb client library in Java
2  * Copyright (C) 2002 "Michael B. Allen" <jcifs at samba dot org>
3  * "Eric Glass" <jcifs at samba dot org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package jcifs.http;
21
22 import java.io.IOException JavaDoc;
23
24 import java.net.UnknownHostException JavaDoc;
25
26 import java.util.Enumeration JavaDoc;
27
28 import javax.servlet.ServletConfig JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.UnavailableException JavaDoc;
31
32 import javax.servlet.http.HttpSession JavaDoc;
33 import javax.servlet.http.HttpServlet JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
49  * This servlet may be used with pre-2.3 servlet containers
50  * to protect content with NTLM HTTP Authentication. Servlets that
51  * extend this abstract base class may be authenticatied against an SMB
52  * server or domain controller depending on how the
53  * <tt>jcifs.smb.client.domain</tt> or <tt>jcifs.http.domainController</tt>
54  * properties are be specified. <b>With later containers the
55  * <tt>NtlmHttpFilter</tt> should be used/b>. For custom NTLM HTTP Authentication schemes the <tt>NtlmSsp</tt> may be used.
56  * <p>
57  * Read <a HREF="../../../ntlmhttpauth.html">jCIFS NTLM HTTP Authentication and the Network Explorer Servlet</a> related information.
58  */

59
60 public abstract class NtlmServlet extends HttpServlet JavaDoc {
61
62     private String JavaDoc defaultDomain;
63
64     private String JavaDoc domainController;
65
66     private boolean loadBalance;
67
68     private boolean enableBasic;
69
70     private boolean insecureBasic;
71
72     private String JavaDoc realm;
73
74     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
75         super.init(config);
76
77         /* Set jcifs properties we know we want; soTimeout and cachePolicy to 10min.
78          */

79         Config.setProperty( "jcifs.smb.client.soTimeout", "300000" );
80         Config.setProperty( "jcifs.netbios.cachePolicy", "600" );
81
82         Enumeration JavaDoc e = config.getInitParameterNames();
83         String JavaDoc name;
84         while (e.hasMoreElements()) {
85             name = (String JavaDoc) 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 JavaDoc request,
105             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
106         UniAddress dc;
107         boolean offerBasic = enableBasic &&
108                 (insecureBasic || request.isSecure());
109         String JavaDoc 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 JavaDoc auth = new String JavaDoc(Base64.decode(msg.substring(6)),
124                         "US-ASCII");
125                 int index = auth.indexOf(':');
126                 String JavaDoc user = (index != -1) ? auth.substring(0, index) : auth;
127                 String JavaDoc password = (index != -1) ? auth.substring(index + 1) :
128                         "";
129                 index = user.indexOf('\\');
130                 if (index == -1) index = user.indexOf('/');
131                 String JavaDoc 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 JavaDoc ssn = request.getSession();
150             ssn.setAttribute("NtlmHttpAuth", ntlm);
151             ssn.setAttribute( "ntlmdomain", ntlm.getDomain() );
152             ssn.setAttribute( "ntlmuser", ntlm.getUsername() );
153         } else {
154             HttpSession JavaDoc 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