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