Merge pull request #649 from DavidS/feature/implement-additional-reference-path
[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
33 #if MONOTOUCH
34 using Mono.Security.Protocol.Ntlm;
35 #else
36 extern alias MonoSecurity;
37 using MonoSecurity::Mono.Security.Protocol.Ntlm;
38 #endif
39
40 using System;
41 using System.Collections;
42 using System.Net;
43
44 namespace Mono.Http
45 {
46         class NtlmSession
47         {
48                 MessageBase message;
49
50                 public NtlmSession () 
51                 {
52                 }
53
54                 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials) 
55                 {
56                         HttpWebRequest request = webRequest as HttpWebRequest;
57                         if (request == null)
58                                 return null;
59         
60                         NetworkCredential cred = credentials.GetCredential (request.RequestUri, "NTLM");
61                         if (cred == null)
62                                 return null;
63
64                         string userName = cred.UserName;
65                         string domain = cred.Domain;
66                         string password = cred.Password;
67                         if (userName == null || userName == "")
68                                 return null;
69
70                         if (String.IsNullOrEmpty (domain)) {
71                                 int idx = userName.IndexOf ('\\');
72                                 if (idx == -1) {
73                                         idx = userName.IndexOf ('/');
74                                 }
75                                 if (idx >= 0) {
76                                         domain = userName.Substring (0, idx);
77                                         userName = userName.Substring (idx + 1);
78                                 }
79                         }
80
81                         bool completed = false;
82                         if (message == null) {
83                                 Type1Message type1 = new Type1Message ();
84                                 type1.Domain = domain;
85                                 type1.Host = ""; // MS does not send it
86                                 type1.Flags |= NtlmFlags.NegotiateNtlm2Key;
87                                 message = type1;
88                         } else if (message.Type == 1) {
89                                 // Should I check the credentials?
90                                 if (challenge == null) {
91                                         message = null;
92                                         return null;
93                                 }
94
95                                 Type2Message type2 = new Type2Message (Convert.FromBase64String (challenge));
96                                 if (password == null)
97                                         password = "";
98
99                                 Type3Message type3 = new Type3Message (type2);
100                                 type3.Username = userName;
101                                 type3.Password = password;
102                                 type3.Domain = domain;
103                                 message = type3;
104                                 completed = true;
105                         } else {
106                                 // Should I check the credentials?
107                                 // type must be 3 here
108                                 if (challenge == null || challenge == String.Empty) {
109                                         Type1Message type1 = new Type1Message ();
110                                         type1.Domain = domain;
111                                         type1.Host = ""; // MS does not send it
112                                         message = type1;
113                                 } else {
114                                         completed = true;
115                                 }
116                         }
117                         
118                         string token = "NTLM " + Convert.ToBase64String (message.GetBytes ());
119                         return new Authorization (token, completed);
120                 }
121         }
122
123         class NtlmClient : IAuthenticationModule
124         {
125                 static Hashtable cache;
126
127                 static NtlmClient () 
128                 {
129                         cache = new Hashtable ();
130                 }
131         
132                 public NtlmClient () {}
133         
134                 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials) 
135                 {
136                         if (credentials == null || challenge == null)
137                                 return null;
138         
139                         string header = challenge.Trim ();
140                         int idx = header.ToLower ().IndexOf ("ntlm");
141                         if (idx == -1)
142                                 return null;
143
144                         idx = header.IndexOfAny (new char [] {' ', '\t'});
145                         if (idx != -1) {
146                                 header = header.Substring (idx).Trim ();
147                         } else {
148                                 header = null;
149                         }
150
151                         HttpWebRequest request = webRequest as HttpWebRequest;
152                         if (request == null)
153                                 return null;
154
155                         lock (cache) {
156                                 NtlmSession ds = (NtlmSession) cache [request];
157                                 if (ds == null) {
158                                         ds = new NtlmSession ();
159                                         cache.Add (request, ds);
160                                 }
161
162                                 return ds.Authenticate (header, webRequest, credentials);
163                         }
164                 }
165
166                 public Authorization PreAuthenticate (WebRequest webRequest, ICredentials credentials) 
167                 {
168                         return null;
169                 }
170         
171                 public string AuthenticationType { 
172                         get { return "NTLM"; }
173                 }
174         
175                 public bool CanPreAuthenticate { 
176                         get { return false; }
177                 }
178         }
179 }
180 #endif