r26295: Remove use of global_loadparm for net and wb_pam_auth.
[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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "libcli/composite/composite.h"
25 #include "winbind/wb_server.h"
26 #include "smbd/service_task.h"
27 #include "auth/credentials/credentials.h"
28 #include "libcli/auth/libcli_auth.h"
29 #include "librpc/gen_ndr/ndr_netlogon.h"
30 #include "librpc/gen_ndr/ndr_netlogon_c.h"
31 #include "librpc/gen_ndr/winbind.h"
32 #include "param/param.h"
33
34 /* Oh, there is so much to keep an eye on when authenticating a user.  Oh my! */
35 struct pam_auth_crap_state {
36         struct composite_context *ctx;
37         struct event_context *event_ctx;
38         struct loadparm_context *lp_ctx;
39
40         struct winbind_SamLogon *req;
41         char *unix_username;
42
43         struct netr_NetworkInfo ninfo;
44         struct netr_LogonSamLogon r;
45
46         const char *user_name;
47         const char *domain_name;
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_logon(struct composite_context *ctx);
59
60 struct composite_context *wb_cmd_pam_auth_crap_send(TALLOC_CTX *mem_ctx,
61                                                     struct wbsrv_service *service,
62                                                     uint32_t logon_parameters,
63                                                     const char *domain,
64                                                     const char *user,
65                                                     const char *workstation,
66                                                     DATA_BLOB chal,
67                                                     DATA_BLOB nt_resp,
68                                                     DATA_BLOB lm_resp)
69 {
70         struct composite_context *result, *ctx;
71         struct pam_auth_crap_state *state;
72         struct netr_NetworkInfo *ninfo;
73         DATA_BLOB tmp_nt_resp, tmp_lm_resp;
74
75         result = composite_create(mem_ctx, service->task->event_ctx);
76         if (result == NULL) goto failed;
77
78         state = talloc(result, struct pam_auth_crap_state);
79         if (state == NULL) goto failed;
80         state->ctx = result;
81         state->lp_ctx = service->task->lp_ctx;
82         result->private_data = state;
83
84         state->req = talloc(state, struct winbind_SamLogon);
85
86         state->req->in.logon_level = 2;
87         state->req->in.validation_level = 3;
88         ninfo = state->req->in.logon.network = talloc(state, struct netr_NetworkInfo);
89         if (ninfo == NULL) goto failed;
90         
91         ninfo->identity_info.account_name.string =  talloc_strdup(state, user);
92         ninfo->identity_info.domain_name.string =  talloc_strdup(state, domain);
93         ninfo->identity_info.parameter_control = logon_parameters;
94         ninfo->identity_info.logon_id_low = 0;
95         ninfo->identity_info.logon_id_high = 0;
96         ninfo->identity_info.workstation.string = talloc_strdup(state, workstation);
97
98         SMB_ASSERT(chal.length == sizeof(ninfo->challenge));
99         memcpy(ninfo->challenge, chal.data,
100                sizeof(ninfo->challenge));
101
102         tmp_nt_resp = data_blob_talloc(ninfo, nt_resp.data, nt_resp.length);
103         if ((nt_resp.data != NULL) &&
104             (tmp_nt_resp.data == NULL)) goto failed;
105
106         tmp_lm_resp = data_blob_talloc(ninfo, lm_resp.data, lm_resp.length);
107         if ((lm_resp.data != NULL) &&
108             (tmp_lm_resp.data == NULL)) goto failed;
109
110         ninfo->nt.length = tmp_nt_resp.length;
111         ninfo->nt.data = tmp_nt_resp.data;
112         ninfo->lm.length = tmp_lm_resp.length;
113         ninfo->lm.data = tmp_lm_resp.data;
114
115         state->unix_username = NULL;
116
117         ctx = wb_sam_logon_send(mem_ctx, service, state->req);
118         if (ctx == NULL) goto failed;
119
120         composite_continue(result, ctx, pam_auth_crap_recv_logon, state);
121         return result;
122
123  failed:
124         talloc_free(result);
125         return NULL;
126 }
127
128 /*  
129     NTLM Authentication
130
131     Send of a SamLogon request to authenticate a user.
132 */
133 static void pam_auth_crap_recv_logon(struct composite_context *ctx)
134 {
135         DATA_BLOB tmp_blob;
136         enum ndr_err_code ndr_err;
137         struct netr_SamBaseInfo *base;
138         struct pam_auth_crap_state *state =
139                 talloc_get_type(ctx->async.private_data,
140                                 struct pam_auth_crap_state);
141
142         state->ctx->status = wb_sam_logon_recv(ctx, state, state->req);
143         if (!composite_is_ok(state->ctx)) return;
144
145         ndr_err = ndr_push_struct_blob(
146                 &tmp_blob, state, state->req->out.validation.sam3,
147                 (ndr_push_flags_fn_t)ndr_push_netr_SamInfo3);
148         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
149                 state->ctx->status = ndr_map_error2ntstatus(ndr_err);
150                 if (!composite_is_ok(state->ctx)) return;
151         }
152
153         /* The Samba3 protocol is a bit broken (due to non-IDL
154          * heritage, so for compatability we must add a non-zero 4
155          * bytes to the info3 */
156         state->info3 = data_blob_talloc(state, NULL, tmp_blob.length+4);
157         if (composite_nomem(state->info3.data, state->ctx)) return;
158
159         SIVAL(state->info3.data, 0, 1);
160         memcpy(state->info3.data+4, tmp_blob.data, tmp_blob.length);
161
162         base = &state->req->out.validation.sam3->base;
163
164         state->user_session_key = base->key;
165         state->lm_key = base->LMSessKey;
166
167         /* Give the caller the most accurate username possible.
168          * Assists where case sensitive comparisons may be done by our
169          * ntlm_auth callers */
170         if (base->account_name.string) {
171                 state->user_name = base->account_name.string;
172                 talloc_steal(state, base->account_name.string);
173         }
174         if (base->domain.string) {
175                 state->domain_name = base->domain.string;
176                 talloc_steal(state, base->domain.string);
177         }
178
179         state->unix_username = talloc_asprintf(state, "%s%s%s", 
180                                                state->domain_name,
181                                                lp_winbind_separator(state->lp_ctx),
182                                                state->user_name);
183         if (composite_nomem(state->unix_username, state->ctx)) return;
184
185         composite_done(state->ctx);
186 }
187
188 /* Having received a NTLM authentication reply, parse out the useful
189  * reply data for the caller */
190 NTSTATUS wb_cmd_pam_auth_crap_recv(struct composite_context *c,
191                                    TALLOC_CTX *mem_ctx,
192                                    DATA_BLOB *info3,
193                                    struct netr_UserSessionKey *user_session_key,
194                                    struct netr_LMSessionKey *lm_key,
195                                    char **unix_username)
196 {
197         struct pam_auth_crap_state *state =
198                 talloc_get_type(c->private_data, struct pam_auth_crap_state);
199         NTSTATUS status = composite_wait(c);
200         if (NT_STATUS_IS_OK(status)) {
201                 info3->length = state->info3.length;
202                 info3->data = talloc_steal(mem_ctx, state->info3.data);
203                 *user_session_key = state->user_session_key;
204                 *lm_key = state->lm_key;
205                 *unix_username = talloc_steal(mem_ctx, state->unix_username);
206         }
207         talloc_free(state);
208         return status;
209 }
210
211 /* Handle plaintext authentication, by encrypting the password and
212  * then sending via the NTLM calls */
213
214 struct composite_context *wb_cmd_pam_auth_send(TALLOC_CTX *mem_ctx,
215                                                struct wbsrv_service *service,
216                                                const char *domain,
217                                                const char *user,
218                                                const char *password)
219 {
220         struct cli_credentials *credentials;
221         const char *workstation;
222         NTSTATUS status;
223
224         DATA_BLOB chal, nt_resp, lm_resp, names_blob;
225         int flags = CLI_CRED_NTLM_AUTH;
226         if (lp_client_lanman_auth(service->task->lp_ctx)) {
227                 flags |= CLI_CRED_LANMAN_AUTH;
228         }
229
230         if (lp_client_ntlmv2_auth(service->task->lp_ctx)) {
231                 flags |= CLI_CRED_NTLMv2_AUTH;
232         }
233
234         DEBUG(5, ("wbsrv_samba3_pam_auth called\n"));
235
236         credentials = cli_credentials_init(mem_ctx);
237         if (!credentials) {
238                 return NULL;
239         }
240         cli_credentials_set_conf(credentials, service->task->lp_ctx);
241         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
242         cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
243
244         cli_credentials_set_password(credentials, password, CRED_SPECIFIED);
245
246         chal = data_blob_talloc(mem_ctx, NULL, 8);
247         if (!chal.data) {
248                 return NULL;
249         }
250         generate_random_buffer(chal.data, chal.length);
251         cli_credentials_get_ntlm_username_domain(credentials, mem_ctx,
252                                                  &user, &domain);
253         /* for best compatability with multiple vitual netbios names
254          * on the host, this should be generated from the
255          * cli_credentials associated with the machine account */
256         workstation = cli_credentials_get_workstation(credentials);
257
258         names_blob = NTLMv2_generate_names_blob(
259                 mem_ctx,
260                 cli_credentials_get_workstation(credentials), 
261                 cli_credentials_get_domain(credentials));
262
263         status = cli_credentials_get_ntlm_response(
264                 credentials, mem_ctx, &flags, chal, names_blob,
265                 &lm_resp, &nt_resp, NULL, NULL);
266         if (!NT_STATUS_IS_OK(status)) {
267                 return NULL;
268         }
269         return wb_cmd_pam_auth_crap_send(mem_ctx, service,
270                                          0 /* logon parameters */, 
271                                          domain, user, workstation,
272                                          chal, nt_resp, lm_resp);
273 }
274
275 NTSTATUS wb_cmd_pam_auth_recv(struct composite_context *c)
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        talloc_free(state);
281        return status;
282 }