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 "nsswitch/winbind_client.h"
27 #include "librpc/gen_ndr/ndr_netlogon.h"
28 #include "librpc/gen_ndr/ndr_winbind.h"
29 #include "lib/messaging/irpc.h"
31 static NTSTATUS get_info3_from_ndr(TALLOC_CTX *mem_ctx, struct winbindd_response *response, struct netr_SamInfo3 *info3)
33 size_t len = response->length - sizeof(struct winbindd_response);
37 blob.length = len - 4;
38 blob.data = (uint8_t *)(((char *)response->extra_data.data) + 4);
40 status = ndr_pull_struct_blob(&blob, mem_ctx, info3,
41 (ndr_pull_flags_fn_t)ndr_pull_netr_SamInfo3);
45 DEBUG(2, ("get_info3_from_ndr: No info3 struct found!\n"));
46 return NT_STATUS_UNSUCCESSFUL;
50 static NTSTATUS winbind_want_check(struct auth_method_context *ctx,
52 const struct auth_usersupplied_info *user_info)
54 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
55 return NT_STATUS_NOT_IMPLEMENTED;
58 /* TODO: maybe limit the user scope to remote users only */
63 Authenticate a user with a challenge/response
64 using the samba3 winbind protocol
66 static NTSTATUS winbind_check_password_samba3(struct auth_method_context *ctx,
68 const struct auth_usersupplied_info *user_info,
69 struct auth_serversupplied_info **server_info)
71 struct winbindd_request request;
72 struct winbindd_response response;
75 struct netr_SamInfo3 info3;
77 /* Send off request */
78 const struct auth_usersupplied_info *user_info_temp;
79 nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx,
80 AUTH_PASSWORD_RESPONSE,
81 user_info, &user_info_temp);
82 if (!NT_STATUS_IS_OK(nt_status)) {
85 user_info = user_info_temp;
88 ZERO_STRUCT(response);
89 request.flags = WBFLAG_PAM_INFO3_NDR;
91 request.data.auth_crap.logon_parameters = user_info->logon_parameters;
93 safe_strcpy(request.data.auth_crap.user,
94 user_info->client.account_name, sizeof(fstring));
95 safe_strcpy(request.data.auth_crap.domain,
96 user_info->client.domain_name, sizeof(fstring));
97 safe_strcpy(request.data.auth_crap.workstation,
98 user_info->workstation_name, sizeof(fstring));
100 memcpy(request.data.auth_crap.chal, ctx->auth_ctx->challenge.data.data, sizeof(request.data.auth_crap.chal));
102 request.data.auth_crap.lm_resp_len = MIN(user_info->password.response.lanman.length,
103 sizeof(request.data.auth_crap.lm_resp));
104 request.data.auth_crap.nt_resp_len = MIN(user_info->password.response.nt.length,
105 sizeof(request.data.auth_crap.nt_resp));
107 memcpy(request.data.auth_crap.lm_resp, user_info->password.response.lanman.data,
108 request.data.auth_crap.lm_resp_len);
109 memcpy(request.data.auth_crap.nt_resp, user_info->password.response.nt.data,
110 request.data.auth_crap.nt_resp_len);
112 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
114 nt_status = NT_STATUS(response.data.auth.nt_status);
115 NT_STATUS_NOT_OK_RETURN(nt_status);
117 if (result == NSS_STATUS_SUCCESS && response.extra_data.data) {
118 union netr_Validation validation;
120 nt_status = get_info3_from_ndr(mem_ctx, &response, &info3);
121 SAFE_FREE(response.extra_data.data);
122 NT_STATUS_NOT_OK_RETURN(nt_status);
124 validation.sam3 = &info3;
125 nt_status = make_server_info_netlogon_validation(mem_ctx,
126 user_info->client.account_name,
130 } else if (result == NSS_STATUS_SUCCESS && !response.extra_data.data) {
131 DEBUG(0, ("Winbindd authenticated the user [%s]\\[%s], "
132 "but did not include the required info3 reply!\n",
133 user_info->client.domain_name, user_info->client.account_name));
134 return NT_STATUS_INSUFFICIENT_LOGON_INFO;
135 } else if (NT_STATUS_IS_OK(nt_status)) {
136 DEBUG(1, ("Winbindd authentication for [%s]\\[%s] failed, "
137 "but no error code is available!\n",
138 user_info->client.domain_name, user_info->client.account_name));
139 return NT_STATUS_NO_LOGON_SERVERS;
145 struct winbind_check_password_state {
146 struct winbind_SamLogon req;
150 Authenticate a user with a challenge/response
151 using IRPC to the winbind task
153 static NTSTATUS winbind_check_password(struct auth_method_context *ctx,
155 const struct auth_usersupplied_info *user_info,
156 struct auth_serversupplied_info **server_info)
159 struct server_id *winbind_servers;
160 struct winbind_check_password_state *s;
161 const struct auth_usersupplied_info *user_info_new;
162 struct netr_IdentityInfo *identity_info;
164 s = talloc(mem_ctx, struct winbind_check_password_state);
165 NT_STATUS_HAVE_NO_MEMORY(s);
167 winbind_servers = irpc_servers_byname(ctx->auth_ctx->msg_ctx, s, "winbind_server");
168 if ((winbind_servers == NULL) || (winbind_servers[0].id == 0)) {
169 DEBUG(0, ("Winbind authentication for [%s]\\[%s] failed, "
170 "no winbind_server running!\n",
171 user_info->client.domain_name, user_info->client.account_name));
172 return NT_STATUS_NO_LOGON_SERVERS;
175 if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
176 struct netr_PasswordInfo *password_info;
178 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_HASH,
179 user_info, &user_info_new);
180 NT_STATUS_NOT_OK_RETURN(status);
181 user_info = user_info_new;
183 password_info = talloc(s, struct netr_PasswordInfo);
184 NT_STATUS_HAVE_NO_MEMORY(password_info);
186 password_info->lmpassword = *user_info->password.hash.lanman;
187 password_info->ntpassword = *user_info->password.hash.nt;
189 identity_info = &password_info->identity_info;
190 s->req.in.logon_level = 1;
191 s->req.in.logon.password= password_info;
193 struct netr_NetworkInfo *network_info;
194 const uint8_t *challenge;
196 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_RESPONSE,
197 user_info, &user_info_new);
198 NT_STATUS_NOT_OK_RETURN(status);
199 user_info = user_info_new;
201 network_info = talloc(s, struct netr_NetworkInfo);
202 NT_STATUS_HAVE_NO_MEMORY(network_info);
204 status = auth_get_challenge(ctx->auth_ctx, &challenge);
205 NT_STATUS_NOT_OK_RETURN(status);
207 memcpy(network_info->challenge, challenge, sizeof(network_info->challenge));
209 network_info->nt.length = user_info->password.response.nt.length;
210 network_info->nt.data = user_info->password.response.nt.data;
212 network_info->lm.length = user_info->password.response.lanman.length;
213 network_info->lm.data = user_info->password.response.lanman.data;
215 identity_info = &network_info->identity_info;
216 s->req.in.logon_level = 2;
217 s->req.in.logon.network = network_info;
220 identity_info->domain_name.string = user_info->client.domain_name;
221 identity_info->parameter_control = user_info->logon_parameters; /* see MSV1_0_* */
222 identity_info->logon_id_low = 0;
223 identity_info->logon_id_high = 0;
224 identity_info->account_name.string = user_info->client.account_name;
225 identity_info->workstation.string = user_info->workstation_name;
227 s->req.in.validation_level = 3;
229 status = IRPC_CALL(ctx->auth_ctx->msg_ctx, winbind_servers[0],
230 winbind, WINBIND_SAMLOGON,
232 NT_STATUS_NOT_OK_RETURN(status);
234 status = make_server_info_netlogon_validation(mem_ctx,
235 user_info->client.account_name,
236 s->req.in.validation_level,
237 &s->req.out.validation,
239 NT_STATUS_NOT_OK_RETURN(status);
244 static const struct auth_operations winbind_samba3_ops = {
245 .name = "winbind_samba3",
246 .get_challenge = auth_get_challenge_not_implemented,
247 .want_check = winbind_want_check,
248 .check_password = winbind_check_password_samba3
251 static const struct auth_operations winbind_ops = {
253 .get_challenge = auth_get_challenge_not_implemented,
254 .want_check = winbind_want_check,
255 .check_password = winbind_check_password
258 NTSTATUS auth_winbind_init(void)
262 ret = auth_register(&winbind_samba3_ops);
263 if (!NT_STATUS_IS_OK(ret)) {
264 DEBUG(0,("Failed to register 'winbind_samba3' auth backend!\n"));
268 ret = auth_register(&winbind_ops);
269 if (!NT_STATUS_IS_OK(ret)) {
270 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));