r1058: The start of work on the SamLogon call for NETLOGON.
[bbaumbach/samba-autobuild/.git] / source4 / 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_t *auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state)
32 {
33         struct auth_ntlmssp_state *auth_ntlmssp_state = ntlmssp_state->auth_context;
34
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         struct auth_ntlmssp_state *auth_ntlmssp_state = ntlmssp_state->auth_context;
46
47         return auth_ntlmssp_state->auth_context->challenge_may_be_modified;
48 }
49
50 /**
51  * NTLM2 authentication modifies the effective challenge, 
52  * @param challenge The new challenge value
53  */
54 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
55 {
56         struct auth_ntlmssp_state *auth_ntlmssp_state = ntlmssp_state->auth_context;
57         struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
58
59         SMB_ASSERT(challenge->length == 8);
60
61         auth_context->challenge = data_blob_talloc(auth_context->mem_ctx, 
62                                                    challenge->data, challenge->length);
63
64         auth_context->challenge_set_by = "NTLMSSP callback (NTLM2)";
65
66         DEBUG(5, ("auth_context challenge set by %s\n", auth_context->challenge_set_by));
67         DEBUG(5, ("challenge is: \n"));
68         dump_data(5, (const char *)auth_context->challenge.data, auth_context->challenge.length);
69         return NT_STATUS_OK;
70 }
71
72 /**
73  * Check the password on an NTLMSSP login.  
74  *
75  * Return the session keys used on the connection.
76  */
77
78 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key) 
79 {
80         struct auth_ntlmssp_state *auth_ntlmssp_state = ntlmssp_state->auth_context;
81         struct auth_usersupplied_info *user_info = NULL;
82         NTSTATUS nt_status;
83
84 #if 0
85         /* the client has given us its machine name (which we otherwise would not get on port 445).
86            we need to possibly reload smb.conf if smb.conf includes depend on the machine name */
87
88         set_remote_machine_name(auth_ntlmssp_state->ntlmssp_state->workstation, True);
89
90         /* setup the string used by %U */
91         /* sub_set_smb_name checks for weird internally */
92         sub_set_smb_name(auth_ntlmssp_state->ntlmssp_state->user);
93
94         reload_services(True);
95
96 #endif
97         nt_status = make_user_info_map(&user_info, 
98                                        auth_ntlmssp_state->ntlmssp_state->user, 
99                                        auth_ntlmssp_state->ntlmssp_state->domain, 
100                                        auth_ntlmssp_state->ntlmssp_state->workstation, 
101                                        auth_ntlmssp_state->ntlmssp_state->lm_resp.data ? &auth_ntlmssp_state->ntlmssp_state->lm_resp : NULL, 
102                                        auth_ntlmssp_state->ntlmssp_state->nt_resp.data ? &auth_ntlmssp_state->ntlmssp_state->nt_resp : NULL, 
103                                        NULL, NULL, NULL,
104                                        True);
105
106         if (!NT_STATUS_IS_OK(nt_status)) {
107                 return nt_status;
108         }
109
110         nt_status = auth_ntlmssp_state->auth_context->check_ntlm_password(auth_ntlmssp_state->auth_context, 
111                                                                           user_info, &auth_ntlmssp_state->server_info); 
112
113         free_user_info(&user_info);
114
115         if (!NT_STATUS_IS_OK(nt_status)) {
116                 return nt_status;
117         }
118         if (auth_ntlmssp_state->server_info->user_session_key.length) {
119                 DEBUG(10, ("Got NT session key of length %u\n", auth_ntlmssp_state->server_info->user_session_key.length));
120                 *user_session_key = data_blob_talloc(ntlmssp_state->mem_ctx, 
121                                                    auth_ntlmssp_state->server_info->user_session_key.data,
122                                                    auth_ntlmssp_state->server_info->user_session_key.length);
123         }
124         if (auth_ntlmssp_state->server_info->lm_session_key.length) {
125                 DEBUG(10, ("Got LM session key of length %u\n", auth_ntlmssp_state->server_info->lm_session_key.length));
126                 *lm_session_key = data_blob_talloc(ntlmssp_state->mem_ctx, 
127                                                    auth_ntlmssp_state->server_info->lm_session_key.data,
128                                                    auth_ntlmssp_state->server_info->lm_session_key.length);
129         }
130         return nt_status;
131 }
132
133 NTSTATUS auth_ntlmssp_start(struct auth_ntlmssp_state **auth_ntlmssp_state)
134 {
135         NTSTATUS nt_status;
136         TALLOC_CTX *mem_ctx;
137
138         mem_ctx = talloc_init("AUTH NTLMSSP context");
139         
140         *auth_ntlmssp_state = talloc_zero(mem_ctx, sizeof(**auth_ntlmssp_state));
141         if (!*auth_ntlmssp_state) {
142                 DEBUG(0,("auth_ntlmssp_start: talloc failed!\n"));
143                 talloc_destroy(mem_ctx);
144                 return NT_STATUS_NO_MEMORY;
145         }
146
147         ZERO_STRUCTP(*auth_ntlmssp_state);
148
149         (*auth_ntlmssp_state)->mem_ctx = mem_ctx;
150
151         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_server_start(&(*auth_ntlmssp_state)->ntlmssp_state))) {
152                 return nt_status;
153         }
154
155         if (!NT_STATUS_IS_OK(nt_status = make_auth_context_subsystem(&(*auth_ntlmssp_state)->auth_context))) {
156                 return nt_status;
157         }
158
159         (*auth_ntlmssp_state)->ntlmssp_state->auth_context = (*auth_ntlmssp_state);
160         (*auth_ntlmssp_state)->ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
161         (*auth_ntlmssp_state)->ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
162         (*auth_ntlmssp_state)->ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
163         (*auth_ntlmssp_state)->ntlmssp_state->check_password = auth_ntlmssp_check_password;
164         (*auth_ntlmssp_state)->ntlmssp_state->server_role = lp_server_role();
165         
166         return NT_STATUS_OK;
167 }
168
169 void auth_ntlmssp_end(struct auth_ntlmssp_state **auth_ntlmssp_state)
170 {
171         TALLOC_CTX *mem_ctx = (*auth_ntlmssp_state)->mem_ctx;
172
173         if ((*auth_ntlmssp_state)->ntlmssp_state) {
174                 ntlmssp_end(&(*auth_ntlmssp_state)->ntlmssp_state);
175         }
176         if ((*auth_ntlmssp_state)->auth_context) {
177                 free_auth_context(&(*auth_ntlmssp_state)->auth_context);
178         }
179         if ((*auth_ntlmssp_state)->server_info) {
180                 free_server_info(&(*auth_ntlmssp_state)->server_info);
181         }
182         talloc_destroy(mem_ctx);
183         *auth_ntlmssp_state = NULL;
184 }
185
186
187 /**
188  * Next state function for the wrapped NTLMSSP state machine
189  * 
190  * @param auth_ntlmssp_state NTLMSSP State
191  * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
192  * @param in The request, as a DATA_BLOB
193  * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
194  * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent, 
195  *                or NT_STATUS_OK if the user is authenticated. 
196  */
197
198 NTSTATUS auth_ntlmssp_update(struct auth_ntlmssp_state *auth_ntlmssp_state, 
199                              TALLOC_CTX *out_mem_ctx,
200                              const DATA_BLOB in, DATA_BLOB *out) 
201 {
202         return ntlmssp_update(auth_ntlmssp_state->ntlmssp_state, 
203                               out_mem_ctx, 
204                               in, out);
205 }
206
207 /** 
208  * Return the credentials of a logged on user, including session keys
209  * etc.
210  *
211  * Only valid after a successful authentication
212  *
213  * May only be called once per authentication.
214  *
215  */
216
217 NTSTATUS auth_ntlmssp_get_session_info(struct auth_ntlmssp_state *auth_ntlmssp_state, 
218                                        struct auth_session_info **session_info) 
219 {
220         NTSTATUS nt_status;
221         nt_status = make_session_info(auth_ntlmssp_state->server_info, session_info);
222
223         if (!NT_STATUS_IS_OK(nt_status)) {
224                 return nt_status;
225         }
226
227         /* the session_info owns this now */
228         auth_ntlmssp_state->server_info = NULL;
229
230         (*session_info)->session_key = data_blob_talloc((*session_info)->mem_ctx, 
231                                                         auth_ntlmssp_state->ntlmssp_state->session_key.data,
232                                                         auth_ntlmssp_state->ntlmssp_state->session_key.length);
233
234         return NT_STATUS_OK;
235 }