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