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