lib:torture: Make variables const
[Samba.git] / source4 / auth / ntlm / auth_winbind.c
blob8b9a1ef3af9a4f11742b3662caf22a8b5aba4a74
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_SamInfo6 *info6 = NULL;
170 union netr_Validation validation;
172 /* Send off request */
173 const struct auth_usersupplied_info *user_info_temp;
174 nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx,
175 AUTH_PASSWORD_RESPONSE,
176 user_info, &user_info_temp);
177 if (!NT_STATUS_IS_OK(nt_status)) {
178 return nt_status;
180 user_info = user_info_temp;
182 ZERO_STRUCT(params);
183 ZERO_STRUCT(validation);
184 /*params.flags = WBFLAG_PAM_INFO3_NDR;*/
186 params.parameter_control = user_info->logon_parameters;
187 params.parameter_control |= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
188 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
189 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
191 params.account_name = user_info->client.account_name;
192 params.domain_name = user_info->client.domain_name;
193 params.workstation_name = user_info->workstation_name;
195 DEBUG(5,("looking up %s@%s logging in from %s\n",
196 params.account_name, params.domain_name,
197 params.workstation_name));
199 memcpy(params.password.response.challenge,
200 ctx->auth_ctx->challenge.data.data,
201 sizeof(params.password.response.challenge));
203 params.password.response.lm_length =
204 user_info->password.response.lanman.length;
205 params.password.response.nt_length =
206 user_info->password.response.nt.length;
208 params.password.response.lm_data =
209 user_info->password.response.lanman.data;
210 params.password.response.nt_data =
211 user_info->password.response.nt.data;
213 wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
214 if (wbc_status == WBC_ERR_AUTH_ERROR) {
215 if (err) {
216 DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
217 err->nt_string, err->nt_status, err->display_string));
218 nt_status = NT_STATUS(err->nt_status);
219 wbcFreeMemory(err);
220 } else {
221 nt_status = NT_STATUS_LOGON_FAILURE;
223 NT_STATUS_NOT_OK_RETURN(nt_status);
224 } else if (!WBC_ERROR_IS_OK(wbc_status)) {
225 DEBUG(1, ("wbcAuthenticateUserEx: failed with %u - %s\n",
226 wbc_status, wbcErrorString(wbc_status)));
227 if (err) {
228 DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
229 err->nt_string, err->nt_status, err->display_string));
231 return NT_STATUS_LOGON_FAILURE;
233 info6 = wbcAuthUserInfo_to_netr_SamInfo6(mem_ctx, info);
234 wbcFreeMemory(info);
235 if (!info6) {
236 DEBUG(1, ("wbcAuthUserInfo_to_netr_SamInfo6 failed\n"));
237 return NT_STATUS_NO_MEMORY;
240 validation.sam6 = info6;
241 nt_status = make_user_info_dc_netlogon_validation(mem_ctx,
242 user_info->client.account_name,
243 6, &validation,
244 true, /* This user was authenticated */
245 user_info_dc);
246 return nt_status;
250 static const struct auth_operations winbind_ops = {
251 .name = "winbind",
252 .want_check = winbind_want_check,
253 .check_password = winbind_check_password
256 static const struct auth_operations winbind_wbclient_ops = {
257 .name = "winbind_wbclient",
258 .want_check = winbind_want_check,
259 .check_password = winbind_check_password_wbclient
262 _PUBLIC_ NTSTATUS auth4_winbind_init(void)
264 NTSTATUS ret;
266 ret = auth_register(&winbind_ops);
267 if (!NT_STATUS_IS_OK(ret)) {
268 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));
269 return ret;
272 ret = auth_register(&winbind_wbclient_ops);
273 if (!NT_STATUS_IS_OK(ret)) {
274 DEBUG(0,("Failed to register 'winbind_wbclient' auth backend!\n"));
275 return ret;
278 return NT_STATUS_OK;