Fix smbclient/tarmode panic on connecting to Windows 2000 clients.
[Samba.git] / source4 / auth / ntlm / auth_winbind.c
blob30a2f01585cb3fdeab5118b915816f2a083fab1b
1 /*
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/>.
24 #include "includes.h"
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 static NTSTATUS get_info3_from_wbcAuthUserInfo(TALLOC_CTX *mem_ctx,
35 struct wbcAuthUserInfo *info,
36 struct netr_SamInfo3 *info3)
38 int i, j;
39 struct samr_RidWithAttribute *rids = NULL;
40 struct dom_sid *user_sid;
41 struct dom_sid *group_sid;
43 user_sid = (struct dom_sid *)(void *)&info->sids[0].sid;
44 group_sid = (struct dom_sid *)(void *)&info->sids[1].sid;
46 info3->base.last_logon = info->logon_time;
47 info3->base.last_logoff = info->logoff_time;
48 info3->base.acct_expiry = info->kickoff_time;
49 info3->base.last_password_change = info->pass_last_set_time;
50 info3->base.allow_password_change = info->pass_can_change_time;
51 info3->base.force_password_change = info->pass_must_change_time;
53 info3->base.account_name.string = talloc_strdup(mem_ctx,
54 info->account_name);
55 info3->base.full_name.string = talloc_strdup(mem_ctx,
56 info->full_name);
57 info3->base.logon_script.string = talloc_strdup(mem_ctx,
58 info->logon_script);
59 info3->base.profile_path.string = talloc_strdup(mem_ctx,
60 info->profile_path);
61 info3->base.home_directory.string = talloc_strdup(mem_ctx,
62 info->home_directory);
63 info3->base.home_drive.string = talloc_strdup(mem_ctx,
64 info->home_drive);
65 info3->base.logon_server.string = talloc_strdup(mem_ctx,
66 info->logon_server);
67 info3->base.domain.string = talloc_strdup(mem_ctx,
68 info->domain_name);
70 info3->base.logon_count = info->logon_count;
71 info3->base.bad_password_count = info->bad_password_count;
72 info3->base.user_flags = info->user_flags;
73 memcpy(info3->base.key.key, info->user_session_key,
74 sizeof(info3->base.key.key));
75 memcpy(info3->base.LMSessKey.key, info->lm_session_key,
76 sizeof(info3->base.LMSessKey.key));
77 info3->base.acct_flags = info->acct_flags;
78 memset(info3->base.unknown, 0, sizeof(info3->base.unknown));
80 if (info->num_sids < 2) {
81 return NT_STATUS_INVALID_PARAMETER;
84 dom_sid_split_rid(mem_ctx, user_sid,
85 &info3->base.domain_sid,
86 &info3->base.rid);
87 dom_sid_split_rid(mem_ctx, group_sid, NULL,
88 &info3->base.primary_gid);
90 /* We already handled the first two, now take care of the rest */
91 info3->base.groups.count = info->num_sids - 2;
93 rids = talloc_array(mem_ctx, struct samr_RidWithAttribute,
94 info3->base.groups.count);
95 NT_STATUS_HAVE_NO_MEMORY(rids);
97 for (i = 2, j = 0; i < info->num_sids; ++i, ++j) {
98 struct dom_sid *tmp_sid;
99 tmp_sid = (struct dom_sid *)(void *)&info->sids[1].sid;
101 rids[j].attributes = info->sids[i].attributes;
102 dom_sid_split_rid(mem_ctx, tmp_sid,
103 NULL, &rids[j].rid);
105 info3->base.groups.rids = rids;
107 return NT_STATUS_OK;
111 static NTSTATUS winbind_want_check(struct auth_method_context *ctx,
112 TALLOC_CTX *mem_ctx,
113 const struct auth_usersupplied_info *user_info)
115 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
116 return NT_STATUS_NOT_IMPLEMENTED;
119 /* TODO: maybe limit the user scope to remote users only */
120 return NT_STATUS_OK;
123 struct winbind_check_password_state {
124 struct winbind_SamLogon req;
128 Authenticate a user with a challenge/response
129 using IRPC to the winbind task
131 static NTSTATUS winbind_check_password(struct auth_method_context *ctx,
132 TALLOC_CTX *mem_ctx,
133 const struct auth_usersupplied_info *user_info,
134 struct auth_user_info_dc **user_info_dc)
136 NTSTATUS status;
137 struct dcerpc_binding_handle *irpc_handle;
138 struct winbind_check_password_state *s;
139 const struct auth_usersupplied_info *user_info_new;
140 struct netr_IdentityInfo *identity_info;
142 if (!ctx->auth_ctx->msg_ctx) {
143 DEBUG(0,("winbind_check_password: auth_context_create was called with out messaging context\n"));
144 return NT_STATUS_INTERNAL_ERROR;
147 s = talloc(mem_ctx, struct winbind_check_password_state);
148 NT_STATUS_HAVE_NO_MEMORY(s);
150 irpc_handle = irpc_binding_handle_by_name(s, ctx->auth_ctx->msg_ctx,
151 "winbind_server",
152 &ndr_table_winbind);
153 if (irpc_handle == NULL) {
154 DEBUG(0, ("Winbind authentication for [%s]\\[%s] failed, "
155 "no winbind_server running!\n",
156 user_info->client.domain_name, user_info->client.account_name));
157 return NT_STATUS_NO_LOGON_SERVERS;
160 if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
161 struct netr_PasswordInfo *password_info;
163 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_HASH,
164 user_info, &user_info_new);
165 NT_STATUS_NOT_OK_RETURN(status);
166 user_info = user_info_new;
168 password_info = talloc(s, struct netr_PasswordInfo);
169 NT_STATUS_HAVE_NO_MEMORY(password_info);
171 password_info->lmpassword = *user_info->password.hash.lanman;
172 password_info->ntpassword = *user_info->password.hash.nt;
174 identity_info = &password_info->identity_info;
175 s->req.in.logon_level = 1;
176 s->req.in.logon.password= password_info;
177 } else {
178 struct netr_NetworkInfo *network_info;
179 uint8_t chal[8];
181 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_RESPONSE,
182 user_info, &user_info_new);
183 NT_STATUS_NOT_OK_RETURN(status);
184 user_info = user_info_new;
186 network_info = talloc(s, struct netr_NetworkInfo);
187 NT_STATUS_HAVE_NO_MEMORY(network_info);
189 status = auth_get_challenge(ctx->auth_ctx, chal);
190 NT_STATUS_NOT_OK_RETURN(status);
192 memcpy(network_info->challenge, chal, sizeof(network_info->challenge));
194 network_info->nt.length = user_info->password.response.nt.length;
195 network_info->nt.data = user_info->password.response.nt.data;
197 network_info->lm.length = user_info->password.response.lanman.length;
198 network_info->lm.data = user_info->password.response.lanman.data;
200 identity_info = &network_info->identity_info;
201 s->req.in.logon_level = 2;
202 s->req.in.logon.network = network_info;
205 identity_info->domain_name.string = user_info->client.domain_name;
206 identity_info->parameter_control = user_info->logon_parameters; /* see MSV1_0_* */
207 identity_info->logon_id_low = 0;
208 identity_info->logon_id_high = 0;
209 identity_info->account_name.string = user_info->client.account_name;
210 identity_info->workstation.string = user_info->workstation_name;
212 s->req.in.validation_level = 3;
214 status = dcerpc_winbind_SamLogon_r(irpc_handle, s, &s->req);
215 NT_STATUS_NOT_OK_RETURN(status);
217 status = make_user_info_dc_netlogon_validation(mem_ctx,
218 user_info->client.account_name,
219 s->req.in.validation_level,
220 &s->req.out.validation,
221 user_info_dc);
222 NT_STATUS_NOT_OK_RETURN(status);
224 return NT_STATUS_OK;
228 Authenticate a user with a challenge/response
229 using the samba3 winbind protocol via libwbclient
231 static NTSTATUS winbind_check_password_wbclient(struct auth_method_context *ctx,
232 TALLOC_CTX *mem_ctx,
233 const struct auth_usersupplied_info *user_info,
234 struct auth_user_info_dc **user_info_dc)
236 struct wbcAuthUserParams params;
237 struct wbcAuthUserInfo *info = NULL;
238 struct wbcAuthErrorInfo *err = NULL;
239 wbcErr wbc_status;
240 NTSTATUS nt_status;
241 struct netr_SamInfo3 info3;
242 union netr_Validation validation;
245 /* Send off request */
246 const struct auth_usersupplied_info *user_info_temp;
247 nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx,
248 AUTH_PASSWORD_RESPONSE,
249 user_info, &user_info_temp);
250 if (!NT_STATUS_IS_OK(nt_status)) {
251 return nt_status;
253 user_info = user_info_temp;
255 ZERO_STRUCT(params);
256 ZERO_STRUCT(info3);
257 /*params.flags = WBFLAG_PAM_INFO3_NDR;*/
259 params.parameter_control = user_info->logon_parameters;
260 params.parameter_control |= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
261 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
262 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
264 params.account_name = user_info->client.account_name;
265 params.domain_name = user_info->client.domain_name;
266 params.workstation_name = user_info->workstation_name;
268 d_fprintf(stderr, "looking up %s@%s logging in from %s\n",
269 params.account_name, params.domain_name,
270 params.workstation_name);
272 memcpy(params.password.response.challenge,
273 ctx->auth_ctx->challenge.data.data,
274 sizeof(params.password.response.challenge));
276 params.password.response.lm_length =
277 user_info->password.response.lanman.length;
278 params.password.response.nt_length =
279 user_info->password.response.nt.length;
281 params.password.response.lm_data =
282 user_info->password.response.lanman.data;
283 params.password.response.nt_data =
284 user_info->password.response.nt.data;
286 wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
287 if (wbc_status == WBC_ERR_AUTH_ERROR) {
288 DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
289 err->nt_string, err->nt_status, err->display_string));
291 nt_status = NT_STATUS(err->nt_status);
292 wbcFreeMemory(err);
293 NT_STATUS_NOT_OK_RETURN(nt_status);
294 } else if (!WBC_ERROR_IS_OK(wbc_status)) {
295 DEBUG(1, ("wbcAuthenticateUserEx: failed with %u - %s\n",
296 wbc_status, wbcErrorString(wbc_status)));
297 return NT_STATUS_LOGON_FAILURE;
299 nt_status = get_info3_from_wbcAuthUserInfo(mem_ctx, info, &info3);
300 wbcFreeMemory(info);
301 NT_STATUS_NOT_OK_RETURN(nt_status);
303 validation.sam3 = &info3;
304 nt_status = make_user_info_dc_netlogon_validation(mem_ctx,
305 user_info->client.account_name,
306 3, &validation, user_info_dc);
307 return nt_status;
311 static const struct auth_operations winbind_ops = {
312 .name = "winbind",
313 .get_challenge = auth_get_challenge_not_implemented,
314 .want_check = winbind_want_check,
315 .check_password = winbind_check_password
318 static const struct auth_operations winbind_wbclient_ops = {
319 .name = "winbind_wbclient",
320 .get_challenge = auth_get_challenge_not_implemented,
321 .want_check = winbind_want_check,
322 .check_password = winbind_check_password_wbclient
325 _PUBLIC_ NTSTATUS auth_winbind_init(void)
327 NTSTATUS ret;
329 ret = auth_register(&winbind_ops);
330 if (!NT_STATUS_IS_OK(ret)) {
331 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));
332 return ret;
335 ret = auth_register(&winbind_wbclient_ops);
336 if (!NT_STATUS_IS_OK(ret)) {
337 DEBUG(0,("Failed to register 'winbind_wbclient' auth backend!\n"));
338 return ret;
341 return NT_STATUS_OK;