kdc: Add belts-and-braces check that we fail if the hdb version changes
[Samba.git] / source4 / winbind / wb_pam_auth.c
blobc84b51f4fe937c302b61f6f5e80cd7ff697722cb
1 /*
2 Unix SMB/CIFS implementation.
4 Authenticate a user
6 Copyright (C) Volker Lendecke 2005
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
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.
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.
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/>.
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/winbind.h"
31 #include "param/param.h"
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 tevent_context *event_ctx;
37 struct loadparm_context *lp_ctx;
39 struct winbind_SamLogon *req;
40 char *unix_username;
42 struct netr_NetworkInfo ninfo;
43 struct netr_LogonSamLogon r;
45 const char *user_name;
46 const char *domain_name;
48 struct netr_UserSessionKey user_session_key;
49 struct netr_LMSessionKey lm_key;
50 DATA_BLOB info3;
54 * NTLM authentication.
57 static void pam_auth_crap_recv_logon(struct tevent_req *subreq);
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)
69 struct composite_context *result;
70 struct pam_auth_crap_state *state;
71 struct netr_NetworkInfo *ninfo;
72 DATA_BLOB tmp_nt_resp, tmp_lm_resp;
73 struct tevent_req *subreq;
75 result = composite_create(mem_ctx, service->task->event_ctx);
76 if (result == NULL) goto failed;
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;
84 state->req = talloc(state, struct winbind_SamLogon);
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;
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);
98 SMB_ASSERT(chal.length == sizeof(ninfo->challenge));
99 memcpy(ninfo->challenge, chal.data,
100 sizeof(ninfo->challenge));
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;
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;
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;
115 state->unix_username = NULL;
117 subreq = wb_sam_logon_send(state,
118 service->task->event_ctx,
119 service, state->req);
120 if (subreq == NULL) goto failed;
121 tevent_req_set_callback(subreq, pam_auth_crap_recv_logon, state);
122 return result;
124 failed:
125 talloc_free(result);
126 return NULL;
130 NTLM Authentication
132 Send of a SamLogon request to authenticate a user.
134 static void pam_auth_crap_recv_logon(struct tevent_req *subreq)
136 DATA_BLOB tmp_blob;
137 enum ndr_err_code ndr_err;
138 struct netr_SamBaseInfo *base;
139 struct pam_auth_crap_state *state =
140 tevent_req_callback_data(subreq,
141 struct pam_auth_crap_state);
143 state->ctx->status = wb_sam_logon_recv(subreq, state, state->req);
144 TALLOC_FREE(subreq);
145 if (!composite_is_ok(state->ctx)) return;
147 ndr_err = ndr_push_struct_blob(
148 &tmp_blob, state, state->req->out.validation.sam3,
149 (ndr_push_flags_fn_t)ndr_push_netr_SamInfo3);
150 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
151 state->ctx->status = ndr_map_error2ntstatus(ndr_err);
152 if (!composite_is_ok(state->ctx)) return;
155 /* The Samba3 protocol is a bit broken (due to non-IDL
156 * heritage, so for compatability we must add a non-zero 4
157 * bytes to the info3 */
158 state->info3 = data_blob_talloc(state, NULL, tmp_blob.length+4);
159 if (composite_nomem(state->info3.data, state->ctx)) return;
161 SIVAL(state->info3.data, 0, 1);
162 memcpy(state->info3.data+4, tmp_blob.data, tmp_blob.length);
164 base = &state->req->out.validation.sam3->base;
166 state->user_session_key = base->key;
167 state->lm_key = base->LMSessKey;
169 /* Give the caller the most accurate username possible.
170 * Assists where case sensitive comparisons may be done by our
171 * ntlm_auth callers */
172 if (base->account_name.string) {
173 state->user_name = base->account_name.string;
174 talloc_steal(state, base->account_name.string);
176 if (base->logon_domain.string) {
177 state->domain_name = base->logon_domain.string;
178 talloc_steal(state, base->logon_domain.string);
181 state->unix_username = talloc_asprintf(state, "%s%s%s",
182 state->domain_name,
183 lpcfg_winbind_separator(state->lp_ctx),
184 state->user_name);
185 if (composite_nomem(state->unix_username, state->ctx)) return;
187 composite_done(state->ctx);
190 /* Having received a NTLM authentication reply, parse out the useful
191 * reply data for the caller */
192 NTSTATUS wb_cmd_pam_auth_crap_recv(struct composite_context *c,
193 TALLOC_CTX *mem_ctx,
194 DATA_BLOB *info3,
195 struct netr_UserSessionKey *user_session_key,
196 struct netr_LMSessionKey *lm_key,
197 char **unix_username)
199 struct pam_auth_crap_state *state =
200 talloc_get_type(c->private_data, struct pam_auth_crap_state);
201 NTSTATUS status = composite_wait(c);
202 if (NT_STATUS_IS_OK(status)) {
203 info3->length = state->info3.length;
204 info3->data = talloc_steal(mem_ctx, state->info3.data);
205 *user_session_key = state->user_session_key;
206 *lm_key = state->lm_key;
207 *unix_username = talloc_steal(mem_ctx, state->unix_username);
209 talloc_free(state);
210 return status;
213 /* Handle plaintext authentication, by encrypting the password and
214 * then sending via the NTLM calls */
216 struct composite_context *wb_cmd_pam_auth_send(TALLOC_CTX *mem_ctx,
217 struct wbsrv_service *service,
218 struct cli_credentials *credentials)
220 const char *workstation;
221 NTSTATUS status;
222 const char *user, *domain;
223 DATA_BLOB chal, nt_resp, lm_resp, names_blob;
224 int flags = CLI_CRED_NTLM_AUTH;
225 if (lpcfg_client_lanman_auth(service->task->lp_ctx)) {
226 flags |= CLI_CRED_LANMAN_AUTH;
229 if (lpcfg_client_ntlmv2_auth(service->task->lp_ctx)) {
230 flags |= CLI_CRED_NTLMv2_AUTH;
233 DEBUG(5, ("wbsrv_samba3_pam_auth called\n"));
235 chal = data_blob_talloc(mem_ctx, NULL, 8);
236 if (!chal.data) {
237 return NULL;
239 generate_random_buffer(chal.data, chal.length);
240 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx,
241 &user, &domain);
242 /* for best compatability with multiple vitual netbios names
243 * on the host, this should be generated from the
244 * cli_credentials associated with the machine account */
245 workstation = cli_credentials_get_workstation(credentials);
247 names_blob = NTLMv2_generate_names_blob(
248 mem_ctx,
249 cli_credentials_get_workstation(credentials),
250 cli_credentials_get_domain(credentials));
252 status = cli_credentials_get_ntlm_response(
253 credentials, mem_ctx, &flags, chal, names_blob,
254 &lm_resp, &nt_resp, NULL, NULL);
255 if (!NT_STATUS_IS_OK(status)) {
256 return NULL;
258 return wb_cmd_pam_auth_crap_send(mem_ctx, service,
259 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT|MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT /* logon parameters */,
260 domain, user, workstation,
261 chal, nt_resp, lm_resp);
264 NTSTATUS wb_cmd_pam_auth_recv(struct composite_context *c,
265 TALLOC_CTX *mem_ctx,
266 DATA_BLOB *info3,
267 struct netr_UserSessionKey *user_session_key,
268 struct netr_LMSessionKey *lm_key,
269 char **unix_username)
271 struct pam_auth_crap_state *state =
272 talloc_get_type(c->private_data, struct pam_auth_crap_state);
273 NTSTATUS status = composite_wait(c);
274 if (NT_STATUS_IS_OK(status)) {
275 if (info3) {
276 info3->length = state->info3.length;
277 info3->data = talloc_steal(mem_ctx, state->info3.data);
279 if (user_session_key) {
280 *user_session_key = state->user_session_key;
282 if (lm_key) {
283 *lm_key = state->lm_key;
285 if (unix_username) {
286 *unix_username = talloc_steal(mem_ctx, state->unix_username);
289 talloc_free(state);
290 return status;