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 "librpc/gen_ndr/ndr_winbind_c.h"
29 #include "lib/messaging/irpc.h"
30 #include "param/param.h"
31 #include "nsswitch/libwbclient/wbclient.h"
32 #include "libcli/security/security.h"
34 _PUBLIC_ NTSTATUS auth_winbind_init(void);
36 static NTSTATUS get_info3_from_wbcAuthUserInfo(TALLOC_CTX *mem_ctx,
37 struct wbcAuthUserInfo *info,
38 struct netr_SamInfo3 *info3)
41 struct samr_RidWithAttribute *rids = NULL;
42 struct dom_sid *user_sid;
43 struct dom_sid *group_sid;
45 user_sid = (struct dom_sid *)(void *)&info->sids[0].sid;
46 group_sid = (struct dom_sid *)(void *)&info->sids[1].sid;
48 info3->base.last_logon = info->logon_time;
49 info3->base.last_logoff = info->logoff_time;
50 info3->base.acct_expiry = info->kickoff_time;
51 info3->base.last_password_change = info->pass_last_set_time;
52 info3->base.allow_password_change = info->pass_can_change_time;
53 info3->base.force_password_change = info->pass_must_change_time;
55 info3->base.account_name.string = talloc_strdup(mem_ctx,
57 info3->base.full_name.string = talloc_strdup(mem_ctx,
59 info3->base.logon_script.string = talloc_strdup(mem_ctx,
61 info3->base.profile_path.string = talloc_strdup(mem_ctx,
63 info3->base.home_directory.string = talloc_strdup(mem_ctx,
64 info->home_directory);
65 info3->base.home_drive.string = talloc_strdup(mem_ctx,
67 info3->base.logon_server.string = talloc_strdup(mem_ctx,
69 info3->base.domain.string = talloc_strdup(mem_ctx,
72 info3->base.logon_count = info->logon_count;
73 info3->base.bad_password_count = info->bad_password_count;
74 info3->base.user_flags = info->user_flags;
75 memcpy(info3->base.key.key, info->user_session_key,
76 sizeof(info3->base.key.key));
77 memcpy(info3->base.LMSessKey.key, info->lm_session_key,
78 sizeof(info3->base.LMSessKey.key));
79 info3->base.acct_flags = info->acct_flags;
80 memset(info3->base.unknown, 0, sizeof(info3->base.unknown));
82 if (info->num_sids < 2) {
83 return NT_STATUS_INVALID_PARAMETER;
86 dom_sid_split_rid(mem_ctx, user_sid,
87 &info3->base.domain_sid,
89 dom_sid_split_rid(mem_ctx, group_sid, NULL,
90 &info3->base.primary_gid);
92 /* We already handled the first two, now take care of the rest */
93 info3->base.groups.count = info->num_sids - 2;
95 rids = talloc_array(mem_ctx, struct samr_RidWithAttribute,
96 info3->base.groups.count);
97 NT_STATUS_HAVE_NO_MEMORY(rids);
99 for (i = 2, j = 0; i < info->num_sids; ++i, ++j) {
100 struct dom_sid *tmp_sid;
101 tmp_sid = (struct dom_sid *)(void *)&info->sids[1].sid;
103 rids[j].attributes = info->sids[i].attributes;
104 dom_sid_split_rid(mem_ctx, tmp_sid,
107 info3->base.groups.rids = rids;
113 static NTSTATUS winbind_want_check(struct auth_method_context *ctx,
115 const struct auth_usersupplied_info *user_info)
117 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
118 return NT_STATUS_NOT_IMPLEMENTED;
121 /* TODO: maybe limit the user scope to remote users only */
125 struct winbind_check_password_state {
126 struct winbind_SamLogon req;
130 Authenticate a user with a challenge/response
131 using IRPC to the winbind task
133 static NTSTATUS winbind_check_password(struct auth_method_context *ctx,
135 const struct auth_usersupplied_info *user_info,
136 struct auth_user_info_dc **user_info_dc)
139 struct dcerpc_binding_handle *irpc_handle;
140 struct winbind_check_password_state *s;
141 const struct auth_usersupplied_info *user_info_new;
142 struct netr_IdentityInfo *identity_info;
144 if (!ctx->auth_ctx->msg_ctx) {
145 DEBUG(0,("winbind_check_password: auth_context_create was called with out messaging context\n"));
146 return NT_STATUS_INTERNAL_ERROR;
149 s = talloc(mem_ctx, struct winbind_check_password_state);
150 NT_STATUS_HAVE_NO_MEMORY(s);
152 irpc_handle = irpc_binding_handle_by_name(s, ctx->auth_ctx->msg_ctx,
155 if (irpc_handle == NULL) {
156 DEBUG(0, ("Winbind authentication for [%s]\\[%s] failed, "
157 "no winbind_server running!\n",
158 user_info->client.domain_name, user_info->client.account_name));
159 return NT_STATUS_NO_LOGON_SERVERS;
162 if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
163 struct netr_PasswordInfo *password_info;
165 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_HASH,
166 user_info, &user_info_new);
167 NT_STATUS_NOT_OK_RETURN(status);
168 user_info = user_info_new;
170 password_info = talloc(s, struct netr_PasswordInfo);
171 NT_STATUS_HAVE_NO_MEMORY(password_info);
173 password_info->lmpassword = *user_info->password.hash.lanman;
174 password_info->ntpassword = *user_info->password.hash.nt;
176 identity_info = &password_info->identity_info;
177 s->req.in.logon_level = 1;
178 s->req.in.logon.password= password_info;
180 struct netr_NetworkInfo *network_info;
183 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_RESPONSE,
184 user_info, &user_info_new);
185 NT_STATUS_NOT_OK_RETURN(status);
186 user_info = user_info_new;
188 network_info = talloc(s, struct netr_NetworkInfo);
189 NT_STATUS_HAVE_NO_MEMORY(network_info);
191 status = auth_get_challenge(ctx->auth_ctx, chal);
192 NT_STATUS_NOT_OK_RETURN(status);
194 memcpy(network_info->challenge, chal, sizeof(network_info->challenge));
196 network_info->nt.length = user_info->password.response.nt.length;
197 network_info->nt.data = user_info->password.response.nt.data;
199 network_info->lm.length = user_info->password.response.lanman.length;
200 network_info->lm.data = user_info->password.response.lanman.data;
202 identity_info = &network_info->identity_info;
203 s->req.in.logon_level = 2;
204 s->req.in.logon.network = network_info;
207 identity_info->domain_name.string = user_info->client.domain_name;
208 identity_info->parameter_control = user_info->logon_parameters; /* see MSV1_0_* */
209 identity_info->logon_id_low = 0;
210 identity_info->logon_id_high = 0;
211 identity_info->account_name.string = user_info->client.account_name;
212 identity_info->workstation.string = user_info->workstation_name;
214 s->req.in.validation_level = 3;
216 status = dcerpc_winbind_SamLogon_r(irpc_handle, s, &s->req);
217 NT_STATUS_NOT_OK_RETURN(status);
219 status = make_user_info_dc_netlogon_validation(mem_ctx,
220 user_info->client.account_name,
221 s->req.in.validation_level,
222 &s->req.out.validation,
224 NT_STATUS_NOT_OK_RETURN(status);
230 Authenticate a user with a challenge/response
231 using the samba3 winbind protocol via libwbclient
233 static NTSTATUS winbind_check_password_wbclient(struct auth_method_context *ctx,
235 const struct auth_usersupplied_info *user_info,
236 struct auth_user_info_dc **user_info_dc)
238 struct wbcAuthUserParams params;
239 struct wbcAuthUserInfo *info = NULL;
240 struct wbcAuthErrorInfo *err = NULL;
243 struct netr_SamInfo3 info3;
244 union netr_Validation validation;
247 /* Send off request */
248 const struct auth_usersupplied_info *user_info_temp;
249 nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx,
250 AUTH_PASSWORD_RESPONSE,
251 user_info, &user_info_temp);
252 if (!NT_STATUS_IS_OK(nt_status)) {
255 user_info = user_info_temp;
259 /*params.flags = WBFLAG_PAM_INFO3_NDR;*/
261 params.parameter_control = user_info->logon_parameters;
262 params.parameter_control |= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
263 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
264 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
266 params.account_name = user_info->client.account_name;
267 params.domain_name = user_info->client.domain_name;
268 params.workstation_name = user_info->workstation_name;
270 d_fprintf(stderr, "looking up %s@%s logging in from %s\n",
271 params.account_name, params.domain_name,
272 params.workstation_name);
274 memcpy(params.password.response.challenge,
275 ctx->auth_ctx->challenge.data.data,
276 sizeof(params.password.response.challenge));
278 params.password.response.lm_length =
279 user_info->password.response.lanman.length;
280 params.password.response.nt_length =
281 user_info->password.response.nt.length;
283 params.password.response.lm_data =
284 user_info->password.response.lanman.data;
285 params.password.response.nt_data =
286 user_info->password.response.nt.data;
288 wbc_status = wbcAuthenticateUserEx(¶ms, &info, &err);
289 if (wbc_status == WBC_ERR_AUTH_ERROR) {
290 DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
291 err->nt_string, err->nt_status, err->display_string));
293 nt_status = NT_STATUS(err->nt_status);
295 NT_STATUS_NOT_OK_RETURN(nt_status);
296 } else if (!WBC_ERROR_IS_OK(wbc_status)) {
297 DEBUG(1, ("wbcAuthenticateUserEx: failed with %u - %s\n",
298 wbc_status, wbcErrorString(wbc_status)));
299 return NT_STATUS_LOGON_FAILURE;
301 nt_status = get_info3_from_wbcAuthUserInfo(mem_ctx, info, &info3);
303 NT_STATUS_NOT_OK_RETURN(nt_status);
305 validation.sam3 = &info3;
306 nt_status = make_user_info_dc_netlogon_validation(mem_ctx,
307 user_info->client.account_name,
308 3, &validation, user_info_dc);
313 static const struct auth_operations winbind_ops = {
315 .get_challenge = auth_get_challenge_not_implemented,
316 .want_check = winbind_want_check,
317 .check_password = winbind_check_password
320 static const struct auth_operations winbind_wbclient_ops = {
321 .name = "winbind_wbclient",
322 .get_challenge = auth_get_challenge_not_implemented,
323 .want_check = winbind_want_check,
324 .check_password = winbind_check_password_wbclient
327 _PUBLIC_ NTSTATUS auth_winbind_init(void)
331 ret = auth_register(&winbind_ops);
332 if (!NT_STATUS_IS_OK(ret)) {
333 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));
337 ret = auth_register(&winbind_wbclient_ops);
338 if (!NT_STATUS_IS_OK(ret)) {
339 DEBUG(0,("Failed to register 'winbind_wbclient' auth backend!\n"));