s4:auth_winbind: explicitly use dcerpc_binding_handle_set_sync_ev() for irpc
[Samba/wip.git] / source4 / auth / ntlm / auth_winbind.c
blob26d2d079b126e7f3e6bd72a9cd3eff080f590de2
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 "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"
34 _PUBLIC_ NTSTATUS auth4_winbind_init(void);
36 static NTSTATUS winbind_want_check(struct auth_method_context *ctx,
37 TALLOC_CTX *mem_ctx,
38 const struct auth_usersupplied_info *user_info)
40 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
41 return NT_STATUS_NOT_IMPLEMENTED;
44 /* TODO: maybe limit the user scope to remote users only */
45 return NT_STATUS_OK;
48 struct winbind_check_password_state {
49 struct winbind_SamLogon req;
53 Authenticate a user with a challenge/response
54 using IRPC to the winbind task
56 static NTSTATUS winbind_check_password(struct auth_method_context *ctx,
57 TALLOC_CTX *mem_ctx,
58 const struct auth_usersupplied_info *user_info,
59 struct auth_user_info_dc **user_info_dc)
61 NTSTATUS status;
62 struct dcerpc_binding_handle *irpc_handle;
63 struct winbind_check_password_state *s;
64 const struct auth_usersupplied_info *user_info_new;
65 struct netr_IdentityInfo *identity_info;
67 if (!ctx->auth_ctx->msg_ctx) {
68 DEBUG(0,("winbind_check_password: auth_context_create was called with out messaging context\n"));
69 return NT_STATUS_INTERNAL_ERROR;
72 s = talloc(mem_ctx, struct winbind_check_password_state);
73 NT_STATUS_HAVE_NO_MEMORY(s);
75 irpc_handle = irpc_binding_handle_by_name(s, ctx->auth_ctx->msg_ctx,
76 "winbind_server",
77 &ndr_table_winbind);
78 if (irpc_handle == NULL) {
79 DEBUG(0, ("Winbind authentication for [%s]\\[%s] failed, "
80 "no winbind_server running!\n",
81 user_info->client.domain_name, user_info->client.account_name));
82 return NT_STATUS_NO_LOGON_SERVERS;
85 if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
86 struct netr_PasswordInfo *password_info;
88 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_HASH,
89 user_info, &user_info_new);
90 NT_STATUS_NOT_OK_RETURN(status);
91 user_info = user_info_new;
93 password_info = talloc(s, struct netr_PasswordInfo);
94 NT_STATUS_HAVE_NO_MEMORY(password_info);
96 password_info->lmpassword = *user_info->password.hash.lanman;
97 password_info->ntpassword = *user_info->password.hash.nt;
99 identity_info = &password_info->identity_info;
100 s->req.in.logon_level = 1;
101 s->req.in.logon.password= password_info;
102 } else {
103 struct netr_NetworkInfo *network_info;
104 uint8_t chal[8];
106 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_RESPONSE,
107 user_info, &user_info_new);
108 NT_STATUS_NOT_OK_RETURN(status);
109 user_info = user_info_new;
111 network_info = talloc(s, struct netr_NetworkInfo);
112 NT_STATUS_HAVE_NO_MEMORY(network_info);
114 status = auth_get_challenge(ctx->auth_ctx, chal);
115 NT_STATUS_NOT_OK_RETURN(status);
117 memcpy(network_info->challenge, chal, sizeof(network_info->challenge));
119 network_info->nt.length = user_info->password.response.nt.length;
120 network_info->nt.data = user_info->password.response.nt.data;
122 network_info->lm.length = user_info->password.response.lanman.length;
123 network_info->lm.data = user_info->password.response.lanman.data;
125 identity_info = &network_info->identity_info;
126 s->req.in.logon_level = 2;
127 s->req.in.logon.network = network_info;
130 identity_info->domain_name.string = user_info->client.domain_name;
131 identity_info->parameter_control = user_info->logon_parameters; /* see MSV1_0_* */
132 identity_info->logon_id_low = 0;
133 identity_info->logon_id_high = 0;
134 identity_info->account_name.string = user_info->client.account_name;
135 identity_info->workstation.string = user_info->workstation_name;
137 s->req.in.validation_level = 3;
139 /* Note: this makes use of nested event loops... */
140 dcerpc_binding_handle_set_sync_ev(irpc_handle, ctx->auth_ctx->event_ctx);
141 status = dcerpc_winbind_SamLogon_r(irpc_handle, s, &s->req);
142 NT_STATUS_NOT_OK_RETURN(status);
144 status = make_user_info_dc_netlogon_validation(mem_ctx,
145 user_info->client.account_name,
146 s->req.in.validation_level,
147 &s->req.out.validation,
148 true, /* This user was authenticated */
149 user_info_dc);
150 NT_STATUS_NOT_OK_RETURN(status);
152 return NT_STATUS_OK;
156 Authenticate a user with a challenge/response
157 using the samba3 winbind protocol via libwbclient
159 static NTSTATUS winbind_check_password_wbclient(struct auth_method_context *ctx,
160 TALLOC_CTX *mem_ctx,
161 const struct auth_usersupplied_info *user_info,
162 struct auth_user_info_dc **user_info_dc)
164 struct wbcAuthUserParams params;
165 struct wbcAuthUserInfo *info = NULL;
166 struct wbcAuthErrorInfo *err = NULL;
167 wbcErr wbc_status;
168 NTSTATUS nt_status;
169 struct netr_SamInfo3 *info3;
170 union netr_Validation validation;
173 /* Send off request */
174 const struct auth_usersupplied_info *user_info_temp;
175 nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx,
176 AUTH_PASSWORD_RESPONSE,
177 user_info, &user_info_temp);
178 if (!NT_STATUS_IS_OK(nt_status)) {
179 return nt_status;
181 user_info = user_info_temp;
183 ZERO_STRUCT(params);
184 ZERO_STRUCT(info3);
185 /*params.flags = WBFLAG_PAM_INFO3_NDR;*/
187 params.parameter_control = user_info->logon_parameters;
188 params.parameter_control |= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
189 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
190 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
192 params.account_name = user_info->client.account_name;
193 params.domain_name = user_info->client.domain_name;
194 params.workstation_name = user_info->workstation_name;
196 d_fprintf(stderr, "looking up %s@%s logging in from %s\n",
197 params.account_name, params.domain_name,
198 params.workstation_name);
200 memcpy(params.password.response.challenge,
201 ctx->auth_ctx->challenge.data.data,
202 sizeof(params.password.response.challenge));
204 params.password.response.lm_length =
205 user_info->password.response.lanman.length;
206 params.password.response.nt_length =
207 user_info->password.response.nt.length;
209 params.password.response.lm_data =
210 user_info->password.response.lanman.data;
211 params.password.response.nt_data =
212 user_info->password.response.nt.data;
214 wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
215 if (wbc_status == WBC_ERR_AUTH_ERROR) {
216 DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
217 err->nt_string, err->nt_status, err->display_string));
219 nt_status = NT_STATUS(err->nt_status);
220 wbcFreeMemory(err);
221 NT_STATUS_NOT_OK_RETURN(nt_status);
222 } else if (!WBC_ERROR_IS_OK(wbc_status)) {
223 DEBUG(1, ("wbcAuthenticateUserEx: failed with %u - %s\n",
224 wbc_status, wbcErrorString(wbc_status)));
225 return NT_STATUS_LOGON_FAILURE;
227 info3 = wbcAuthUserInfo_to_netr_SamInfo3(mem_ctx, info);
228 wbcFreeMemory(info);
229 if (!info3) {
230 DEBUG(1, ("wbcAuthUserInfo_to_netr_SamInfo3 failed\n"));
231 return NT_STATUS_NO_MEMORY;
234 validation.sam3 = info3;
235 nt_status = make_user_info_dc_netlogon_validation(mem_ctx,
236 user_info->client.account_name,
237 3, &validation,
238 true, /* This user was authenticated */
239 user_info_dc);
240 return nt_status;
244 static const struct auth_operations winbind_ops = {
245 .name = "winbind",
246 .want_check = winbind_want_check,
247 .check_password = winbind_check_password
250 static const struct auth_operations winbind_wbclient_ops = {
251 .name = "winbind_wbclient",
252 .want_check = winbind_want_check,
253 .check_password = winbind_check_password_wbclient
256 _PUBLIC_ NTSTATUS auth4_winbind_init(void)
258 NTSTATUS ret;
260 ret = auth_register(&winbind_ops);
261 if (!NT_STATUS_IS_OK(ret)) {
262 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));
263 return ret;
266 ret = auth_register(&winbind_wbclient_ops);
267 if (!NT_STATUS_IS_OK(ret)) {
268 DEBUG(0,("Failed to register 'winbind_wbclient' auth backend!\n"));
269 return ret;
272 return NT_STATUS_OK;