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