2 Unix SMB/CIFS implementation.
4 Winbind authentication mechnism
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Andrew Bartlett 2001 - 2002
8 Copyright (C) Stefan Metzmacher 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "auth/auth.h"
26 #include "auth/ntlm/auth_proto.h"
27 #include "auth/auth_sam_reply.h"
28 #include "nsswitch/winbind_client.h"
29 #include "librpc/gen_ndr/ndr_netlogon.h"
30 #include "librpc/gen_ndr/ndr_winbind.h"
31 #include "lib/messaging/irpc.h"
32 #include "param/param.h"
33 #include "nsswitch/libwbclient/wbclient.h"
34 #include "libcli/security/dom_sid.h"
36 static NTSTATUS get_info3_from_ndr(TALLOC_CTX *mem_ctx, struct winbindd_response *response, struct netr_SamInfo3 *info3)
38 size_t len = response->length - sizeof(struct winbindd_response);
40 enum ndr_err_code ndr_err;
42 blob.length = len - 4;
43 blob.data = (uint8_t *)(((char *)response->extra_data.data) + 4);
45 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx,
47 (ndr_pull_flags_fn_t)ndr_pull_netr_SamInfo3);
48 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
49 return ndr_map_error2ntstatus(ndr_err);
54 DEBUG(2, ("get_info3_from_ndr: No info3 struct found!\n"));
55 return NT_STATUS_UNSUCCESSFUL;
59 static NTSTATUS get_info3_from_wbcAuthUserInfo(TALLOC_CTX *mem_ctx,
60 struct wbcAuthUserInfo *info,
61 struct netr_SamInfo3 *info3)
64 struct samr_RidWithAttribute *rids = NULL;
66 info3->base.last_logon = info->logon_time;
67 info3->base.last_logoff = info->logoff_time;
68 info3->base.acct_expiry = info->kickoff_time;
69 info3->base.last_password_change = info->pass_last_set_time;
70 info3->base.allow_password_change = info->pass_can_change_time;
71 info3->base.force_password_change = info->pass_must_change_time;
73 info3->base.account_name.string = talloc_strdup(mem_ctx,
75 info3->base.full_name.string = talloc_strdup(mem_ctx,
77 info3->base.logon_script.string = talloc_strdup(mem_ctx,
79 info3->base.profile_path.string = talloc_strdup(mem_ctx,
81 info3->base.home_directory.string = talloc_strdup(mem_ctx,
82 info->home_directory);
83 info3->base.home_drive.string = talloc_strdup(mem_ctx,
85 info3->base.logon_server.string = talloc_strdup(mem_ctx,
87 info3->base.domain.string = talloc_strdup(mem_ctx,
90 info3->base.logon_count = info->logon_count;
91 info3->base.bad_password_count = info->bad_password_count;
92 info3->base.user_flags = info->user_flags;
93 memcpy(info3->base.key.key, info->user_session_key,
94 sizeof(info3->base.key.key));
95 memcpy(info3->base.LMSessKey.key, info->lm_session_key,
96 sizeof(info3->base.LMSessKey.key));
97 info3->base.acct_flags = info->acct_flags;
98 memset(info3->base.unknown, 0, sizeof(info3->base.unknown));
100 if (info->num_sids < 2) {
101 return NT_STATUS_INVALID_PARAMETER;
104 dom_sid_split_rid(mem_ctx, (struct dom_sid2 *) &info->sids[0].sid,
105 &info3->base.domain_sid,
107 dom_sid_split_rid(mem_ctx, (struct dom_sid2 *) &info->sids[1].sid, NULL,
108 &info3->base.primary_gid);
110 /* We already handled the first two, now take care of the rest */
111 info3->base.groups.count = info->num_sids - 2;
113 rids = talloc_array(mem_ctx, struct samr_RidWithAttribute,
114 info3->base.groups.count);
115 NT_STATUS_HAVE_NO_MEMORY(rids);
117 for (i = 2, j = 0; i < info->num_sids; ++i, ++j) {
118 rids[j].attributes = info->sids[i].attributes;
119 dom_sid_split_rid(mem_ctx,
120 (struct dom_sid2 *) &info->sids[i].sid,
123 info3->base.groups.rids = rids;
129 static NTSTATUS winbind_want_check(struct auth_method_context *ctx,
131 const struct auth_usersupplied_info *user_info)
133 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
134 return NT_STATUS_NOT_IMPLEMENTED;
137 /* TODO: maybe limit the user scope to remote users only */
142 Authenticate a user with a challenge/response
143 using the samba3 winbind protocol
145 static NTSTATUS winbind_check_password_samba3(struct auth_method_context *ctx,
147 const struct auth_usersupplied_info *user_info,
148 struct auth_serversupplied_info **server_info)
150 struct winbindd_request request;
151 struct winbindd_response response;
154 struct netr_SamInfo3 info3;
156 /* Send off request */
157 const struct auth_usersupplied_info *user_info_temp;
158 nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx,
159 AUTH_PASSWORD_RESPONSE,
160 user_info, &user_info_temp);
161 if (!NT_STATUS_IS_OK(nt_status)) {
164 user_info = user_info_temp;
166 ZERO_STRUCT(request);
167 ZERO_STRUCT(response);
168 request.flags = WBFLAG_PAM_INFO3_NDR;
170 request.data.auth_crap.logon_parameters = user_info->logon_parameters;
172 safe_strcpy(request.data.auth_crap.user,
173 user_info->client.account_name, sizeof(fstring));
174 safe_strcpy(request.data.auth_crap.domain,
175 user_info->client.domain_name, sizeof(fstring));
176 safe_strcpy(request.data.auth_crap.workstation,
177 user_info->workstation_name, sizeof(fstring));
179 memcpy(request.data.auth_crap.chal, ctx->auth_ctx->challenge.data.data, sizeof(request.data.auth_crap.chal));
181 request.data.auth_crap.lm_resp_len = MIN(user_info->password.response.lanman.length,
182 sizeof(request.data.auth_crap.lm_resp));
183 request.data.auth_crap.nt_resp_len = MIN(user_info->password.response.nt.length,
184 sizeof(request.data.auth_crap.nt_resp));
186 memcpy(request.data.auth_crap.lm_resp, user_info->password.response.lanman.data,
187 request.data.auth_crap.lm_resp_len);
188 memcpy(request.data.auth_crap.nt_resp, user_info->password.response.nt.data,
189 request.data.auth_crap.nt_resp_len);
191 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
193 nt_status = NT_STATUS(response.data.auth.nt_status);
194 NT_STATUS_NOT_OK_RETURN(nt_status);
196 if (result == NSS_STATUS_SUCCESS && response.extra_data.data) {
197 union netr_Validation validation;
199 nt_status = get_info3_from_ndr(mem_ctx, &response, &info3);
200 SAFE_FREE(response.extra_data.data);
201 NT_STATUS_NOT_OK_RETURN(nt_status);
203 validation.sam3 = &info3;
204 nt_status = make_server_info_netlogon_validation(mem_ctx,
205 user_info->client.account_name,
209 } else if (result == NSS_STATUS_SUCCESS && !response.extra_data.data) {
210 DEBUG(0, ("Winbindd authenticated the user [%s]\\[%s], "
211 "but did not include the required info3 reply!\n",
212 user_info->client.domain_name, user_info->client.account_name));
213 return NT_STATUS_INSUFFICIENT_LOGON_INFO;
214 } else if (NT_STATUS_IS_OK(nt_status)) {
215 DEBUG(1, ("Winbindd authentication for [%s]\\[%s] failed, "
216 "but no error code is available!\n",
217 user_info->client.domain_name, user_info->client.account_name));
218 return NT_STATUS_NO_LOGON_SERVERS;
224 struct winbind_check_password_state {
225 struct winbind_SamLogon req;
229 Authenticate a user with a challenge/response
230 using IRPC to the winbind task
232 static NTSTATUS winbind_check_password(struct auth_method_context *ctx,
234 const struct auth_usersupplied_info *user_info,
235 struct auth_serversupplied_info **server_info)
238 struct server_id *winbind_servers;
239 struct winbind_check_password_state *s;
240 const struct auth_usersupplied_info *user_info_new;
241 struct netr_IdentityInfo *identity_info;
243 s = talloc(mem_ctx, struct winbind_check_password_state);
244 NT_STATUS_HAVE_NO_MEMORY(s);
246 winbind_servers = irpc_servers_byname(ctx->auth_ctx->msg_ctx, s, "winbind_server");
247 if ((winbind_servers == NULL) || (winbind_servers[0].id == 0)) {
248 DEBUG(0, ("Winbind authentication for [%s]\\[%s] failed, "
249 "no winbind_server running!\n",
250 user_info->client.domain_name, user_info->client.account_name));
251 return NT_STATUS_NO_LOGON_SERVERS;
254 if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
255 struct netr_PasswordInfo *password_info;
257 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_HASH,
258 user_info, &user_info_new);
259 NT_STATUS_NOT_OK_RETURN(status);
260 user_info = user_info_new;
262 password_info = talloc(s, struct netr_PasswordInfo);
263 NT_STATUS_HAVE_NO_MEMORY(password_info);
265 password_info->lmpassword = *user_info->password.hash.lanman;
266 password_info->ntpassword = *user_info->password.hash.nt;
268 identity_info = &password_info->identity_info;
269 s->req.in.logon_level = 1;
270 s->req.in.logon.password= password_info;
272 struct netr_NetworkInfo *network_info;
275 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_RESPONSE,
276 user_info, &user_info_new);
277 NT_STATUS_NOT_OK_RETURN(status);
278 user_info = user_info_new;
280 network_info = talloc(s, struct netr_NetworkInfo);
281 NT_STATUS_HAVE_NO_MEMORY(network_info);
283 status = auth_get_challenge(ctx->auth_ctx, chal);
284 NT_STATUS_NOT_OK_RETURN(status);
286 memcpy(network_info->challenge, chal, sizeof(network_info->challenge));
288 network_info->nt.length = user_info->password.response.nt.length;
289 network_info->nt.data = user_info->password.response.nt.data;
291 network_info->lm.length = user_info->password.response.lanman.length;
292 network_info->lm.data = user_info->password.response.lanman.data;
294 identity_info = &network_info->identity_info;
295 s->req.in.logon_level = 2;
296 s->req.in.logon.network = network_info;
299 identity_info->domain_name.string = user_info->client.domain_name;
300 identity_info->parameter_control = user_info->logon_parameters; /* see MSV1_0_* */
301 identity_info->logon_id_low = 0;
302 identity_info->logon_id_high = 0;
303 identity_info->account_name.string = user_info->client.account_name;
304 identity_info->workstation.string = user_info->workstation_name;
306 s->req.in.validation_level = 3;
308 status = IRPC_CALL(ctx->auth_ctx->msg_ctx, winbind_servers[0],
309 winbind, WINBIND_SAMLOGON,
311 NT_STATUS_NOT_OK_RETURN(status);
313 status = make_server_info_netlogon_validation(mem_ctx,
314 user_info->client.account_name,
315 s->req.in.validation_level,
316 &s->req.out.validation,
318 NT_STATUS_NOT_OK_RETURN(status);
324 Authenticate a user with a challenge/response
325 using the samba3 winbind protocol via libwbclient
327 static NTSTATUS winbind_check_password_wbclient(struct auth_method_context *ctx,
329 const struct auth_usersupplied_info *user_info,
330 struct auth_serversupplied_info **server_info)
332 struct wbcAuthUserParams params;
333 struct wbcAuthUserInfo *info = NULL;
334 struct wbcAuthErrorInfo *err = NULL;
337 struct netr_SamInfo3 info3;
338 union netr_Validation validation;
341 /* Send off request */
342 const struct auth_usersupplied_info *user_info_temp;
343 nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx,
344 AUTH_PASSWORD_RESPONSE,
345 user_info, &user_info_temp);
346 if (!NT_STATUS_IS_OK(nt_status)) {
349 user_info = user_info_temp;
353 /*params.flags = WBFLAG_PAM_INFO3_NDR;*/
355 params.parameter_control = user_info->logon_parameters;
356 params.parameter_control |= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
357 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
358 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
360 params.account_name = user_info->client.account_name;
361 params.domain_name = user_info->client.domain_name;
362 params.workstation_name = user_info->workstation_name;
364 d_fprintf(stderr, "looking up %s@%s logging in from %s\n",
365 params.account_name, params.domain_name,
366 params.workstation_name);
368 memcpy(params.password.response.challenge,
369 ctx->auth_ctx->challenge.data.data,
370 sizeof(params.password.response.challenge));
372 params.password.response.lm_length =
373 user_info->password.response.lanman.length;
374 params.password.response.nt_length =
375 user_info->password.response.nt.length;
377 params.password.response.lm_data =
378 user_info->password.response.lanman.data;
379 params.password.response.nt_data =
380 user_info->password.response.nt.data;
382 wbc_status = wbcAuthenticateUserEx(¶ms, &info, &err);
383 if (!WBC_ERROR_IS_OK(wbc_status)) {
384 DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
385 err->nt_string, err->nt_status, err->display_string));
387 nt_status = NT_STATUS(err->nt_status);
389 NT_STATUS_NOT_OK_RETURN(nt_status);
391 nt_status = get_info3_from_wbcAuthUserInfo(mem_ctx, info, &info3);
393 NT_STATUS_NOT_OK_RETURN(nt_status);
395 validation.sam3 = &info3;
396 nt_status = make_server_info_netlogon_validation(mem_ctx,
397 user_info->client.account_name,
398 3, &validation, server_info);
403 static const struct auth_operations winbind_samba3_ops = {
404 .name = "winbind_samba3",
405 .get_challenge = auth_get_challenge_not_implemented,
406 .want_check = winbind_want_check,
407 .check_password = winbind_check_password_samba3
410 static const struct auth_operations winbind_ops = {
412 .get_challenge = auth_get_challenge_not_implemented,
413 .want_check = winbind_want_check,
414 .check_password = winbind_check_password
417 static const struct auth_operations winbind_wbclient_ops = {
418 .name = "winbind_wbclient",
419 .get_challenge = auth_get_challenge_not_implemented,
420 .want_check = winbind_want_check,
421 .check_password = winbind_check_password_wbclient
424 _PUBLIC_ NTSTATUS auth_winbind_init(void)
428 ret = auth_register(&winbind_samba3_ops);
429 if (!NT_STATUS_IS_OK(ret)) {
430 DEBUG(0,("Failed to register 'winbind_samba3' auth backend!\n"));
434 ret = auth_register(&winbind_ops);
435 if (!NT_STATUS_IS_OK(ret)) {
436 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));
440 ret = auth_register(&winbind_wbclient_ops);
441 if (!NT_STATUS_IS_OK(ret)) {
442 DEBUG(0,("Failed to register 'winbind_wbclient' auth backend!\n"));