r19598: Ahead of a merge to current lorikeet-heimdal:
[samba.git] / source4 / winbind / wb_pam_auth.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Authenticate a user
5
6    Copyright (C) Volker Lendecke 2005
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
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 #include "libcli/composite/composite.h"
26 #include "winbind/wb_server.h"
27 #include "smbd/service_task.h"
28 #include "auth/credentials/credentials.h"
29 #include "libcli/auth/libcli_auth.h"
30 #include "librpc/gen_ndr/ndr_netlogon.h"
31 #include "librpc/gen_ndr/ndr_netlogon_c.h"
32
33 /* Oh, there is so much to keep an eye on when authenticating a user.  Oh my! */
34 struct pam_auth_crap_state {
35         struct composite_context *ctx;
36         struct event_context *event_ctx;
37         uint32_t logon_parameters;
38         const char *domain_name;
39         const char *user_name;
40         char *unix_username;
41         const char *workstation;
42         DATA_BLOB chal, nt_resp, lm_resp;
43
44         struct creds_CredentialState *creds_state;
45         struct netr_Authenticator auth, auth2;
46         struct netr_NetworkInfo ninfo;
47         struct netr_LogonSamLogon r;
48
49         struct netr_UserSessionKey user_session_key;
50         struct netr_LMSessionKey lm_key;
51         DATA_BLOB info3;
52 };
53
54 /*
55  * NTLM authentication.
56 */
57
58 static void pam_auth_crap_recv_domain(struct composite_context *ctx);
59 static void pam_auth_crap_recv_samlogon(struct rpc_request *req);
60
61 struct composite_context *wb_cmd_pam_auth_crap_send(TALLOC_CTX *mem_ctx,
62                                                     struct wbsrv_service *service,
63                                                     uint32_t logon_parameters,
64                                                     const char *domain,
65                                                     const char *user,
66                                                     const char *workstation,
67                                                     DATA_BLOB chal,
68                                                     DATA_BLOB nt_resp,
69                                                     DATA_BLOB lm_resp)
70 {
71         struct composite_context *result, *ctx;
72         struct pam_auth_crap_state *state;
73
74         result = talloc(mem_ctx, struct composite_context);
75         if (result == NULL) goto failed;
76         result->state = COMPOSITE_STATE_IN_PROGRESS;
77         result->async.fn = NULL;
78         result->event_ctx = service->task->event_ctx;
79
80         state = talloc(result, struct pam_auth_crap_state);
81         if (state == NULL) goto failed;
82         state->ctx = result;
83         result->private_data = state;
84
85         state->logon_parameters = logon_parameters;
86
87         state->domain_name = talloc_strdup(state, domain);
88         if (state->domain_name == NULL) goto failed;
89
90         state->user_name = talloc_strdup(state, user);
91         if (state->user_name == NULL) goto failed;
92
93         state->unix_username = NULL;
94
95         state->workstation = talloc_strdup(state, workstation);
96         if (state->workstation == NULL) goto failed;
97
98         state->chal = data_blob_talloc(state, chal.data, chal.length);
99         if ((chal.data != NULL) && (state->chal.data == NULL)) goto failed;
100
101         state->nt_resp = data_blob_talloc(state, nt_resp.data, nt_resp.length);
102         if ((nt_resp.data != NULL) &&
103             (state->nt_resp.data == NULL)) goto failed;
104
105         state->lm_resp = data_blob_talloc(state, lm_resp.data, lm_resp.length);
106         if ((lm_resp.data != NULL) &&
107             (state->lm_resp.data == NULL)) goto failed;
108
109         ctx = wb_sid2domain_send(state, service, service->primary_sid);
110         if (ctx == NULL) goto failed;
111
112         ctx->async.fn = pam_auth_crap_recv_domain;
113         ctx->async.private_data = state;
114         return result;
115
116  failed:
117         talloc_free(result);
118         return NULL;
119 }
120
121 /*  
122     NTLM Authentication
123
124     Send of a SamLogon request to authenticate a user.
125 */
126 static void pam_auth_crap_recv_domain(struct composite_context *ctx)
127 {
128         struct pam_auth_crap_state *state =
129                 talloc_get_type(ctx->async.private_data,
130                                 struct pam_auth_crap_state);
131         struct rpc_request *req;
132         struct wbsrv_domain *domain;
133
134         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
135         state->creds_state =
136                 cli_credentials_get_netlogon_creds(domain->schannel_creds);
137
138         creds_client_authenticator(state->creds_state, &state->auth);
139
140         state->ninfo.identity_info.account_name.string = state->user_name;
141         state->ninfo.identity_info.domain_name.string =  state->domain_name;
142         state->ninfo.identity_info.parameter_control = state->logon_parameters;
143         state->ninfo.identity_info.logon_id_low = 0;
144         state->ninfo.identity_info.logon_id_high = 0;
145         state->ninfo.identity_info.workstation.string = state->workstation;
146
147         SMB_ASSERT(state->chal.length == sizeof(state->ninfo.challenge));
148         memcpy(state->ninfo.challenge, state->chal.data,
149                sizeof(state->ninfo.challenge));
150
151         state->ninfo.nt.length = state->nt_resp.length;
152         state->ninfo.nt.data = state->nt_resp.data;
153         state->ninfo.lm.length = state->lm_resp.length;
154         state->ninfo.lm.data = state->lm_resp.data;
155
156         state->r.in.server_name = talloc_asprintf(
157                 state, "\\\\%s", dcerpc_server_name(domain->netlogon_pipe));
158         if (composite_nomem(state->r.in.server_name, state->ctx)) return;
159
160         ZERO_STRUCT(state->auth2);
161
162         state->r.in.computer_name =
163                 cli_credentials_get_workstation(domain->schannel_creds);
164         state->r.in.credential = &state->auth;
165         state->r.in.return_authenticator = &state->auth2;
166         state->r.in.logon_level = 2;
167         state->r.in.validation_level = 3;
168         state->r.in.logon.network = &state->ninfo;
169         state->r.out.return_authenticator = NULL;
170
171         req = dcerpc_netr_LogonSamLogon_send(domain->netlogon_pipe, state,
172                                              &state->r);
173         composite_continue_rpc(state->ctx, req, pam_auth_crap_recv_samlogon,
174                                state);
175 }
176
177 /* 
178    NTLM Authentication 
179    
180    Check the SamLogon reply, decrypt and parse out the session keys and the
181    info3 structure.
182 */
183 static void pam_auth_crap_recv_samlogon(struct rpc_request *req)
184 {
185         struct pam_auth_crap_state *state =
186                 talloc_get_type(req->async.private,
187                                 struct pam_auth_crap_state);
188         struct netr_SamBaseInfo *base;
189         DATA_BLOB tmp_blob;
190
191         state->ctx->status = dcerpc_ndr_request_recv(req);
192         if (!composite_is_ok(state->ctx)) return;
193
194         if ((state->r.out.return_authenticator == NULL) ||
195             (!creds_client_check(state->creds_state,
196                                  &state->r.out.return_authenticator->cred))) {
197                 DEBUG(0, ("Credentials check failed!\n"));
198                 composite_error(state->ctx, NT_STATUS_ACCESS_DENIED);
199                 return;
200         }
201
202         state->ctx->status = state->r.out.result;
203         if (!composite_is_ok(state->ctx)) return;
204
205         /* Decrypt the session keys before we reform the info3, so the
206          * person on the other end of winbindd pipe doesn't have to.
207          * They won't have the encryption key anyway */
208         creds_decrypt_samlogon(state->creds_state,
209                                state->r.in.validation_level,
210                                &state->r.out.validation);
211
212         state->ctx->status = ndr_push_struct_blob(
213                 &tmp_blob, state, state->r.out.validation.sam3,
214                 (ndr_push_flags_fn_t)ndr_push_netr_SamInfo3);
215         if (!composite_is_ok(state->ctx)) return;
216
217         /* The Samba3 protocol is a bit broken (due to non-IDL
218          * heritage, so for compatability we must add a non-zero 4
219          * bytes to the info3 */
220         state->info3 = data_blob_talloc(state, NULL, tmp_blob.length+4);
221         if (composite_nomem(state->info3.data, state->ctx)) return;
222
223         SIVAL(state->info3.data, 0, 1);
224         memcpy(state->info3.data+4, tmp_blob.data, tmp_blob.length);
225
226         /* We actually only ask for level 3, and assume it above, but 
227          * anyway... */
228
229         base = NULL;
230         switch(state->r.in.validation_level) {
231         case 2:
232                 base = &state->r.out.validation.sam2->base;
233                 break;
234         case 3:
235                 base = &state->r.out.validation.sam3->base;
236                 break;
237         case 6:
238                 base = &state->r.out.validation.sam6->base;
239                 break;
240         }
241         if (base == NULL) {
242                 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
243                 return;
244         }
245
246         state->user_session_key = base->key;
247         state->lm_key = base->LMSessKey;
248
249         /* Give the caller the most accurate username possible.
250          * Assists where case sensitive comparisons may be done by our
251          * ntlm_auth callers */
252         if (base->account_name.string) {
253                 state->user_name = base->account_name.string;
254                 talloc_steal(state, base->account_name.string);
255         }
256         if (base->domain.string) {
257                 state->domain_name = base->domain.string;
258                 talloc_steal(state, base->domain.string);
259         }
260
261         state->unix_username = talloc_asprintf(state, "%s%s%s", 
262                                                state->domain_name,
263                                                lp_winbind_separator(),
264                                                state->user_name);
265         if (composite_nomem(state->unix_username, state->ctx)) return;
266
267         composite_done(state->ctx);
268 }
269
270 NTSTATUS wb_cmd_pam_auth_crap_recv(struct composite_context *c,
271                                    TALLOC_CTX *mem_ctx,
272                                    DATA_BLOB *info3,
273                                    struct netr_UserSessionKey *user_session_key,
274                                    struct netr_LMSessionKey *lm_key,
275                                    char **unix_username)
276 {
277         struct pam_auth_crap_state *state =
278                 talloc_get_type(c->private_data, struct pam_auth_crap_state);
279         NTSTATUS status = composite_wait(c);
280         if (NT_STATUS_IS_OK(status)) {
281                 info3->length = state->info3.length;
282                 info3->data = talloc_steal(mem_ctx, state->info3.data);
283                 *user_session_key = state->user_session_key;
284                 *lm_key = state->lm_key;
285                 *unix_username = talloc_steal(mem_ctx, state->unix_username);
286         }
287         talloc_free(state);
288         return status;
289 }
290
291 NTSTATUS wb_cmd_pam_auth_crap(TALLOC_CTX *mem_ctx,
292                               struct wbsrv_service *service,
293                               uint32_t logon_parameters,
294                               const char *domain, const char *user,
295                               const char *workstation,
296                               DATA_BLOB chal, DATA_BLOB nt_resp,
297                               DATA_BLOB lm_resp,
298                               DATA_BLOB *info3,
299                               struct netr_UserSessionKey *user_session_key,
300                               struct netr_LMSessionKey *lm_key,
301                               char **unix_username)
302 {
303         struct composite_context *c =
304                 wb_cmd_pam_auth_crap_send(mem_ctx, service, logon_parameters, 
305                                           domain, user, workstation,
306                                           chal, nt_resp, lm_resp);
307         return wb_cmd_pam_auth_crap_recv(c, mem_ctx, info3, user_session_key,
308                                          lm_key, unix_username);
309 }
310
311 struct composite_context *wb_cmd_pam_auth_send(TALLOC_CTX *mem_ctx,
312                                                struct wbsrv_service *service,
313                                                const char *domain,
314                                                const char *user,
315                                                const char *password)
316 {
317         struct cli_credentials *credentials;
318         const char *workstation;
319         NTSTATUS status;
320
321         DATA_BLOB chal, nt_resp, lm_resp, names_blob;
322         int flags = CLI_CRED_NTLM_AUTH;
323         if (lp_client_lanman_auth()) {
324                 flags |= CLI_CRED_LANMAN_AUTH;
325         }
326
327         if (lp_client_ntlmv2_auth()) {
328                 flags |= CLI_CRED_NTLMv2_AUTH;
329         }
330
331         DEBUG(5, ("wbsrv_samba3_pam_auth called\n"));
332
333         credentials = cli_credentials_init(mem_ctx);
334         if (!credentials) {
335                 return NULL;
336         }
337         cli_credentials_set_conf(credentials);
338         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
339         cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
340
341         cli_credentials_set_password(credentials, password, CRED_SPECIFIED);
342
343         chal = data_blob_talloc(mem_ctx, NULL, 8);
344         if (!chal.data) {
345                 return NULL;
346         }
347         generate_random_buffer(chal.data, chal.length);
348         cli_credentials_get_ntlm_username_domain(credentials, mem_ctx,
349                                                  &user, &domain);
350         /* for best compatability with multiple vitual netbios names
351          * on the host, this should be generated from the
352          * cli_credentials associated with the machine account */
353         workstation = cli_credentials_get_workstation(credentials);
354
355         names_blob = NTLMv2_generate_names_blob(
356                 mem_ctx,
357                 cli_credentials_get_workstation(credentials), 
358                 cli_credentials_get_domain(credentials));
359
360         status = cli_credentials_get_ntlm_response(
361                 credentials, mem_ctx, &flags, chal, names_blob,
362                 &lm_resp, &nt_resp, NULL, NULL);
363         if (!NT_STATUS_IS_OK(status)) {
364                 return NULL;
365         }
366         return wb_cmd_pam_auth_crap_send(mem_ctx, service,
367                                          0 /* logon parameters */, 
368                                          domain, user, workstation,
369                                          chal, nt_resp, lm_resp);
370 }
371
372 NTSTATUS wb_cmd_pam_auth_recv(struct composite_context *c)
373 {
374         struct pam_auth_crap_state *state =
375                 talloc_get_type(c->private_data, struct pam_auth_crap_state);
376         NTSTATUS status = composite_wait(c);
377         talloc_free(state);
378         return status;
379 }
380
381 NTSTATUS wb_cmd_pam_auth(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
382                          const char *domain, const char *user,
383                          const char *password)
384 {
385         struct composite_context *c =
386                 wb_cmd_pam_auth_send(mem_ctx, service, domain, user, password);
387         return wb_cmd_pam_auth_recv(c);
388 }