2 fixes for NTLM authentication
[mono.git] / mcs / class / System / Mono.Http / NtlmClient.cs
1 //
2 // Mono.Http.NtlmClient
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2003 Novell, Inc. (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if SECURITY_DEP
32 using System;
33 using System.Collections;
34 using System.Net;
35 using Mono.Security.Protocol.Ntlm;
36
37 namespace Mono.Http
38 {
39         class NtlmSession
40         {
41                 MessageBase message;
42
43                 public NtlmSession () 
44                 {
45                 }
46
47                 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials) 
48                 {
49                         HttpWebRequest request = webRequest as HttpWebRequest;
50                         if (request == null)
51                                 return null;
52         
53                         NetworkCredential cred = credentials.GetCredential (request.RequestUri, "NTLM");
54                         if (cred == null)
55                                 return null;
56
57                         string userName = cred.UserName;
58                         string domain = cred.Domain;
59                         string password = cred.Password;
60                         if (userName == null || userName == "")
61                                 return null;
62
63                         if (String.IsNullOrEmpty (domain)) {
64                                 int idx = userName.IndexOf ('\\');
65                                 if (idx == -1) {
66                                         idx = userName.IndexOf ('/');
67                                 }
68                                 if (idx >= 0) {
69                                         domain = userName.Substring (0, idx);
70                                         userName = userName.Substring (idx + 1);
71                                 }
72                         }
73
74                         bool completed = false;
75                         if (message == null) {
76                                 Type1Message type1 = new Type1Message ();
77                                 type1.Domain = domain;
78                                 type1.Host = ""; // MS does not send it
79                                 message = type1;
80                         } else if (message.Type == 1) {
81                                 // Should I check the credentials?
82                                 if (challenge == null) {
83                                         message = null;
84                                         return null;
85                                 }
86
87                                 Type2Message type2 = new Type2Message (Convert.FromBase64String (challenge));
88                                 if (password == null)
89                                         password = "";
90
91                                 Type3Message type3 = new Type3Message ();
92                                 type3.Domain = domain;
93                                 // type3.Host = ""; MS sends the machine name for type 3 packets
94                                 type3.Username = userName;
95                                 type3.Challenge = type2.Nonce;
96                                 type3.Password = password;
97                                 message = type3;
98                                 completed = true;
99                         } else {
100                                 // Should I check the credentials?
101                                 // type must be 3 here
102                                 if (challenge == null || challenge == String.Empty) {
103                                         Type1Message type1 = new Type1Message ();
104                                         type1.Domain = domain;
105                                         type1.Host = ""; // MS does not send it
106                                         message = type1;
107                                 } else {
108                                         completed = true;
109                                 }
110                         }
111                         
112                         string token = "NTLM " + Convert.ToBase64String (message.GetBytes ());
113                         return new Authorization (token, completed);
114                 }
115         }
116
117         class NtlmClient : IAuthenticationModule
118         {
119                 static Hashtable cache;
120
121                 static NtlmClient () 
122                 {
123                         cache = new Hashtable ();
124                 }
125         
126                 public NtlmClient () {}
127         
128                 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials) 
129                 {
130                         if (credentials == null || challenge == null)
131                                 return null;
132         
133                         string header = challenge.Trim ();
134                         int idx = header.ToLower ().IndexOf ("ntlm");
135                         if (idx == -1)
136                                 return null;
137
138                         idx = header.IndexOfAny (new char [] {' ', '\t'});
139                         if (idx != -1) {
140                                 header = header.Substring (idx).Trim ();
141                         } else {
142                                 header = null;
143                         }
144
145                         HttpWebRequest request = webRequest as HttpWebRequest;
146                         if (request == null)
147                                 return null;
148
149                         lock (cache) {
150                                 NtlmSession ds = (NtlmSession) cache [request];
151                                 if (ds == null) {
152                                         ds = new NtlmSession ();
153                                         cache.Add (request, ds);
154                                 }
155
156                                 return ds.Authenticate (header, webRequest, credentials);
157                         }
158                 }
159
160                 public Authorization PreAuthenticate (WebRequest webRequest, ICredentials credentials) 
161                 {
162                         return null;
163                 }
164         
165                 public string AuthenticationType { 
166                         get { return "NTLM"; }
167                 }
168         
169                 public bool CanPreAuthenticate { 
170                         get { return false; }
171                 }
172         }
173 }
174 #endif