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 "librpc/gen_ndr/ndr_winbind_c.h"
28 #include "lib/messaging/irpc.h"
29 #include "param/param.h"
30 #include "nsswitch/libwbclient/wbclient.h"
31 #include "auth/auth_sam_reply.h"
32 #include "libcli/security/security.h"
33 #include "dsdb/samdb/samdb.h"
35 _PUBLIC_ NTSTATUS auth4_winbind_init(void);
37 static NTSTATUS winbind_want_check(struct auth_method_context *ctx,
39 const struct auth_usersupplied_info *user_info)
41 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
42 return NT_STATUS_NOT_IMPLEMENTED;
45 /* TODO: maybe limit the user scope to remote users only */
49 static NTSTATUS winbind_rodc_want_check(struct auth_method_context *ctx,
51 const struct auth_usersupplied_info *user_info)
56 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
57 return NT_STATUS_NOT_IMPLEMENTED;
60 if (ctx->auth_ctx->sam_ctx == NULL) {
61 DBG_ERR("ctx->auth_ctx->sam_ctx == NULL, don't check.\n");
62 return NT_STATUS_NOT_IMPLEMENTED;
65 ret = samdb_rodc(ctx->auth_ctx->sam_ctx, &am_rodc);
66 if (ret != LDB_SUCCESS) {
67 DBG_ERR("samdb_rodc() failed %d %s, don't check.\n",
68 ret, ldb_errstring(ctx->auth_ctx->sam_ctx));
69 return NT_STATUS_NOT_IMPLEMENTED;
74 * We don't support trusts yet and we
75 * don't want to add them using the
76 * semi-async irpc call that uses
77 * a nested event loop.
79 return NT_STATUS_NOT_IMPLEMENTED;
83 * We're a RODC, so we forward the request to our winbind.
84 * As the RODC is not yet production ready anyway, we keep
85 * the semi-async behavior with nested event loops in order
86 * to keep autobuild happy.
91 struct winbind_check_password_state {
92 struct winbind_SamLogon req;
96 Authenticate a user with a challenge/response
97 using IRPC to the winbind task
99 static NTSTATUS winbind_check_password(struct auth_method_context *ctx,
101 const struct auth_usersupplied_info *user_info,
102 struct auth_user_info_dc **user_info_dc)
105 struct dcerpc_binding_handle *irpc_handle;
106 struct winbind_check_password_state *s;
107 const struct auth_usersupplied_info *user_info_new;
108 struct netr_IdentityInfo *identity_info;
110 if (!ctx->auth_ctx->msg_ctx) {
111 DEBUG(0,("winbind_check_password: auth_context_create was called with out messaging context\n"));
112 return NT_STATUS_INTERNAL_ERROR;
115 s = talloc(mem_ctx, struct winbind_check_password_state);
116 NT_STATUS_HAVE_NO_MEMORY(s);
118 irpc_handle = irpc_binding_handle_by_name(s, ctx->auth_ctx->msg_ctx,
121 if (irpc_handle == NULL) {
122 DEBUG(0, ("Winbind authentication for [%s]\\[%s] failed, "
123 "no winbind_server running!\n",
124 user_info->client.domain_name, user_info->client.account_name));
125 return NT_STATUS_NO_LOGON_SERVERS;
128 if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
129 struct netr_PasswordInfo *password_info;
131 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_HASH,
132 user_info, &user_info_new);
133 NT_STATUS_NOT_OK_RETURN(status);
134 user_info = user_info_new;
136 password_info = talloc(s, struct netr_PasswordInfo);
137 NT_STATUS_HAVE_NO_MEMORY(password_info);
139 password_info->lmpassword = *user_info->password.hash.lanman;
140 password_info->ntpassword = *user_info->password.hash.nt;
142 identity_info = &password_info->identity_info;
143 s->req.in.logon_level = 1;
144 s->req.in.logon.password= password_info;
146 struct netr_NetworkInfo *network_info;
149 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_RESPONSE,
150 user_info, &user_info_new);
151 NT_STATUS_NOT_OK_RETURN(status);
152 user_info = user_info_new;
154 network_info = talloc(s, struct netr_NetworkInfo);
155 NT_STATUS_HAVE_NO_MEMORY(network_info);
157 status = auth_get_challenge(ctx->auth_ctx, chal);
158 NT_STATUS_NOT_OK_RETURN(status);
160 memcpy(network_info->challenge, chal, sizeof(network_info->challenge));
162 network_info->nt.length = user_info->password.response.nt.length;
163 network_info->nt.data = user_info->password.response.nt.data;
165 network_info->lm.length = user_info->password.response.lanman.length;
166 network_info->lm.data = user_info->password.response.lanman.data;
168 identity_info = &network_info->identity_info;
169 s->req.in.logon_level = 2;
170 s->req.in.logon.network = network_info;
173 identity_info->domain_name.string = user_info->client.domain_name;
174 identity_info->parameter_control = user_info->logon_parameters; /* see MSV1_0_* */
175 identity_info->logon_id_low = 0;
176 identity_info->logon_id_high = 0;
177 identity_info->account_name.string = user_info->client.account_name;
178 identity_info->workstation.string = user_info->workstation_name;
180 s->req.in.validation_level = 3;
182 /* Note: this makes use of nested event loops... */
183 dcerpc_binding_handle_set_sync_ev(irpc_handle, ctx->auth_ctx->event_ctx);
184 status = dcerpc_winbind_SamLogon_r(irpc_handle, s, &s->req);
185 NT_STATUS_NOT_OK_RETURN(status);
187 if (NT_STATUS_EQUAL(s->req.out.result, NT_STATUS_NO_SUCH_USER) &&
188 !s->req.out.authoritative) {
189 return NT_STATUS_NOT_IMPLEMENTED;
192 status = make_user_info_dc_netlogon_validation(mem_ctx,
193 user_info->client.account_name,
194 s->req.in.validation_level,
195 &s->req.out.validation,
196 true, /* This user was authenticated */
198 NT_STATUS_NOT_OK_RETURN(status);
204 Authenticate a user with a challenge/response
205 using the samba3 winbind protocol via libwbclient
207 static NTSTATUS winbind_check_password_wbclient(struct auth_method_context *ctx,
209 const struct auth_usersupplied_info *user_info,
210 struct auth_user_info_dc **user_info_dc)
212 struct wbcAuthUserParams params;
213 struct wbcAuthUserInfo *info = NULL;
214 struct wbcAuthErrorInfo *err = NULL;
217 struct netr_SamInfo6 *info6 = NULL;
218 union netr_Validation validation;
220 /* Send off request */
221 const struct auth_usersupplied_info *user_info_temp;
222 nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx,
223 AUTH_PASSWORD_RESPONSE,
224 user_info, &user_info_temp);
225 if (!NT_STATUS_IS_OK(nt_status)) {
228 user_info = user_info_temp;
231 ZERO_STRUCT(validation);
232 /*params.flags = WBFLAG_PAM_INFO3_NDR;*/
234 params.parameter_control = user_info->logon_parameters;
235 params.parameter_control |= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
236 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
237 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
239 params.account_name = user_info->client.account_name;
240 params.domain_name = user_info->client.domain_name;
241 params.workstation_name = user_info->workstation_name;
243 DEBUG(5,("looking up %s@%s logging in from %s\n",
244 params.account_name, params.domain_name,
245 params.workstation_name));
247 memcpy(params.password.response.challenge,
248 ctx->auth_ctx->challenge.data.data,
249 sizeof(params.password.response.challenge));
251 params.password.response.lm_length =
252 user_info->password.response.lanman.length;
253 params.password.response.nt_length =
254 user_info->password.response.nt.length;
256 params.password.response.lm_data =
257 user_info->password.response.lanman.data;
258 params.password.response.nt_data =
259 user_info->password.response.nt.data;
261 wbc_status = wbcAuthenticateUserEx(¶ms, &info, &err);
262 if (wbc_status == WBC_ERR_AUTH_ERROR) {
264 DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
265 err->nt_string, err->nt_status, err->display_string));
266 nt_status = NT_STATUS(err->nt_status);
269 nt_status = NT_STATUS_LOGON_FAILURE;
271 NT_STATUS_NOT_OK_RETURN(nt_status);
272 } else if (!WBC_ERROR_IS_OK(wbc_status)) {
273 DEBUG(1, ("wbcAuthenticateUserEx: failed with %u - %s\n",
274 wbc_status, wbcErrorString(wbc_status)));
276 DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
277 err->nt_string, err->nt_status, err->display_string));
279 return NT_STATUS_LOGON_FAILURE;
281 info6 = wbcAuthUserInfo_to_netr_SamInfo6(mem_ctx, info);
284 DEBUG(1, ("wbcAuthUserInfo_to_netr_SamInfo6 failed\n"));
285 return NT_STATUS_NO_MEMORY;
288 validation.sam6 = info6;
289 nt_status = make_user_info_dc_netlogon_validation(mem_ctx,
290 user_info->client.account_name,
292 true, /* This user was authenticated */
298 static const struct auth_operations winbind_ops = {
300 .want_check = winbind_want_check,
301 .check_password = winbind_check_password
304 static const struct auth_operations winbind_rodc_ops = {
305 .name = "winbind_rodc",
306 .want_check = winbind_rodc_want_check,
307 .check_password = winbind_check_password
310 static const struct auth_operations winbind_wbclient_ops = {
311 .name = "winbind_wbclient",
312 .want_check = winbind_want_check,
313 .check_password = winbind_check_password_wbclient
316 _PUBLIC_ NTSTATUS auth4_winbind_init(void)
320 ret = auth_register(&winbind_ops);
321 if (!NT_STATUS_IS_OK(ret)) {
322 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));
326 ret = auth_register(&winbind_rodc_ops);
327 if (!NT_STATUS_IS_OK(ret)) {
328 DEBUG(0,("Failed to register 'winbind_rodc' auth backend!\n"));
332 ret = auth_register(&winbind_wbclient_ops);
333 if (!NT_STATUS_IS_OK(ret)) {
334 DEBUG(0,("Failed to register 'winbind_wbclient' auth backend!\n"));