08e88cc21a436383714c2f58445765ca96667ab2
[kai/samba-autobuild/.git] / source / auth / auth_ntlmssp.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NLTMSSP, server side
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett 2001-2003
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 /**
27  * Return the challenge as determined by the authentication subsystem 
28  * @return an 8 byte random challenge
29  */
30
31 static const uint8 *auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state)
32 {
33         AUTH_NTLMSSP_STATE *auth_ntlmssp_state =
34                 (AUTH_NTLMSSP_STATE *)ntlmssp_state->auth_context;
35         return auth_ntlmssp_state->auth_context->get_ntlm_challenge(auth_ntlmssp_state->auth_context);
36 }
37
38 /**
39  * Some authentication methods 'fix' the challenge, so we may not be able to set it
40  *
41  * @return If the effective challenge used by the auth subsystem may be modified
42  */
43 static BOOL auth_ntlmssp_may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
44 {
45         AUTH_NTLMSSP_STATE *auth_ntlmssp_state =
46                 (AUTH_NTLMSSP_STATE *)ntlmssp_state->auth_context;
47         struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
48
49         return auth_context->challenge_may_be_modified;
50 }
51
52 /**
53  * NTLM2 authentication modifies the effective challenge, 
54  * @param challenge The new challenge value
55  */
56 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
57 {
58         AUTH_NTLMSSP_STATE *auth_ntlmssp_state =
59                 (AUTH_NTLMSSP_STATE *)ntlmssp_state->auth_context;
60         struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
61
62         SMB_ASSERT(challenge->length == 8);
63
64         auth_context->challenge = data_blob_talloc(auth_context->mem_ctx, 
65                                                    challenge->data, challenge->length);
66
67         auth_context->challenge_set_by = "NTLMSSP callback (NTLM2)";
68
69         DEBUG(5, ("auth_context challenge set by %s\n", auth_context->challenge_set_by));
70         DEBUG(5, ("challenge is: \n"));
71         dump_data(5, auth_context->challenge.data, auth_context->challenge.length);
72         return NT_STATUS_OK;
73 }
74
75 /**
76  * Check the password on an NTLMSSP login.  
77  *
78  * Return the session keys used on the connection.
79  */
80
81 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key) 
82 {
83         AUTH_NTLMSSP_STATE *auth_ntlmssp_state =
84                 (AUTH_NTLMSSP_STATE *)ntlmssp_state->auth_context;
85         auth_usersupplied_info *user_info = NULL;
86         NTSTATUS nt_status;
87         BOOL username_was_mapped;
88
89         /* the client has given us its machine name (which we otherwise would not get on port 445).
90            we need to possibly reload smb.conf if smb.conf includes depend on the machine name */
91
92         set_remote_machine_name(auth_ntlmssp_state->ntlmssp_state->workstation, True);
93
94         /* setup the string used by %U */
95         /* sub_set_smb_name checks for weird internally */
96         sub_set_smb_name(auth_ntlmssp_state->ntlmssp_state->user);
97
98         reload_services(True);
99
100         nt_status = make_user_info_map(&user_info, 
101                                        auth_ntlmssp_state->ntlmssp_state->user, 
102                                        auth_ntlmssp_state->ntlmssp_state->domain, 
103                                        auth_ntlmssp_state->ntlmssp_state->workstation, 
104                                        auth_ntlmssp_state->ntlmssp_state->lm_resp.data ? &auth_ntlmssp_state->ntlmssp_state->lm_resp : NULL, 
105                                        auth_ntlmssp_state->ntlmssp_state->nt_resp.data ? &auth_ntlmssp_state->ntlmssp_state->nt_resp : NULL, 
106                                        NULL, NULL, NULL,
107                                        True);
108
109         user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
110
111         if (!NT_STATUS_IS_OK(nt_status)) {
112                 return nt_status;
113         }
114
115         nt_status = auth_ntlmssp_state->auth_context->check_ntlm_password(auth_ntlmssp_state->auth_context, 
116                                                                           user_info, &auth_ntlmssp_state->server_info); 
117
118         username_was_mapped = user_info->was_mapped;
119
120         free_user_info(&user_info);
121
122         if (!NT_STATUS_IS_OK(nt_status)) {
123                 return nt_status;
124         }
125
126         auth_ntlmssp_state->server_info->was_mapped |= username_was_mapped;
127
128         nt_status = create_local_token(auth_ntlmssp_state->server_info);
129
130         if (!NT_STATUS_IS_OK(nt_status)) {
131                 DEBUG(10, ("create_local_token failed\n"));
132                 return nt_status;
133         }
134
135         if (auth_ntlmssp_state->server_info->user_session_key.length) {
136                 DEBUG(10, ("Got NT session key of length %u\n",
137                         (unsigned int)auth_ntlmssp_state->server_info->user_session_key.length));
138                 *user_session_key = data_blob_talloc(auth_ntlmssp_state->mem_ctx, 
139                                                    auth_ntlmssp_state->server_info->user_session_key.data,
140                                                    auth_ntlmssp_state->server_info->user_session_key.length);
141         }
142         if (auth_ntlmssp_state->server_info->lm_session_key.length) {
143                 DEBUG(10, ("Got LM session key of length %u\n",
144                         (unsigned int)auth_ntlmssp_state->server_info->lm_session_key.length));
145                 *lm_session_key = data_blob_talloc(auth_ntlmssp_state->mem_ctx, 
146                                                    auth_ntlmssp_state->server_info->lm_session_key.data,
147                                                    auth_ntlmssp_state->server_info->lm_session_key.length);
148         }
149         return nt_status;
150 }
151
152 NTSTATUS auth_ntlmssp_start(AUTH_NTLMSSP_STATE **auth_ntlmssp_state)
153 {
154         NTSTATUS nt_status;
155         TALLOC_CTX *mem_ctx;
156
157         mem_ctx = talloc_init("AUTH NTLMSSP context");
158         
159         *auth_ntlmssp_state = TALLOC_ZERO_P(mem_ctx, AUTH_NTLMSSP_STATE);
160         if (!*auth_ntlmssp_state) {
161                 DEBUG(0,("auth_ntlmssp_start: talloc failed!\n"));
162                 talloc_destroy(mem_ctx);
163                 return NT_STATUS_NO_MEMORY;
164         }
165
166         ZERO_STRUCTP(*auth_ntlmssp_state);
167
168         (*auth_ntlmssp_state)->mem_ctx = mem_ctx;
169
170         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_server_start(&(*auth_ntlmssp_state)->ntlmssp_state))) {
171                 return nt_status;
172         }
173
174         if (!NT_STATUS_IS_OK(nt_status = make_auth_context_subsystem(&(*auth_ntlmssp_state)->auth_context))) {
175                 return nt_status;
176         }
177
178         (*auth_ntlmssp_state)->ntlmssp_state->auth_context = (*auth_ntlmssp_state);
179         (*auth_ntlmssp_state)->ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
180         (*auth_ntlmssp_state)->ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
181         (*auth_ntlmssp_state)->ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
182         (*auth_ntlmssp_state)->ntlmssp_state->check_password = auth_ntlmssp_check_password;
183         (*auth_ntlmssp_state)->ntlmssp_state->server_role = (enum server_types)lp_server_role();
184
185         return NT_STATUS_OK;
186 }
187
188 void auth_ntlmssp_end(AUTH_NTLMSSP_STATE **auth_ntlmssp_state)
189 {
190         TALLOC_CTX *mem_ctx = (*auth_ntlmssp_state)->mem_ctx;
191
192         if ((*auth_ntlmssp_state)->ntlmssp_state) {
193                 ntlmssp_end(&(*auth_ntlmssp_state)->ntlmssp_state);
194         }
195         if ((*auth_ntlmssp_state)->auth_context) {
196                 ((*auth_ntlmssp_state)->auth_context->free)(&(*auth_ntlmssp_state)->auth_context);
197         }
198         if ((*auth_ntlmssp_state)->server_info) {
199                 TALLOC_FREE((*auth_ntlmssp_state)->server_info);
200         }
201         talloc_destroy(mem_ctx);
202         *auth_ntlmssp_state = NULL;
203 }
204
205 NTSTATUS auth_ntlmssp_update(AUTH_NTLMSSP_STATE *auth_ntlmssp_state, 
206                              const DATA_BLOB request, DATA_BLOB *reply) 
207 {
208         return ntlmssp_update(auth_ntlmssp_state->ntlmssp_state, request, reply);
209 }