2 Unix SMB/CIFS implementation.
4 Winbind daemon - pam auth funcions
6 Copyright (C) Andrew Tridgell 2000
7 Copyright (C) Tim Potter 2001
8 Copyright (C) Andrew Bartlett 2001-2002
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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define DBGC_CLASS DBGC_WINBIND
30 static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx,
31 struct winbindd_cli_state *state,
32 NET_USER_INFO_3 *info3)
36 if (!prs_init(&ps, 256 /* Random, non-zero number */, mem_ctx, MARSHALL)) {
37 return NT_STATUS_NO_MEMORY;
39 if (!net_io_user_info3("", info3, &ps, 1, 3)) {
41 return NT_STATUS_UNSUCCESSFUL;
44 size = prs_data_size(&ps);
45 state->response.extra_data = malloc(size);
46 if (!state->response.extra_data) {
48 return NT_STATUS_NO_MEMORY;
50 prs_copy_all_data_out(state->response.extra_data, &ps);
51 state->response.length += size;
56 /**********************************************************************
57 Authenticate a user with a clear test password
58 **********************************************************************/
60 enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state)
63 fstring name_domain, name_user;
64 unsigned char trust_passwd[16];
65 time_t last_change_time;
66 uint32 sec_channel_type;
67 NET_USER_INFO_3 info3;
68 struct cli_state *cli;
70 TALLOC_CTX *mem_ctx = NULL;
75 unsigned char local_lm_response[24];
76 unsigned char local_nt_response[24];
77 const char *contact_domain;
80 /* Ensure null termination */
81 state->request.data.auth.user[sizeof(state->request.data.auth.user)-1]='\0';
83 /* Ensure null termination */
84 state->request.data.auth.pass[sizeof(state->request.data.auth.pass)-1]='\0';
86 DEBUG(3, ("[%5lu]: pam auth %s\n", (unsigned long)state->pid,
87 state->request.data.auth.user));
89 if (!(mem_ctx = talloc_init("winbind pam auth for %s", state->request.data.auth.user))) {
90 DEBUG(0, ("winbindd_pam_auth: could not talloc_init()!\n"));
91 result = NT_STATUS_NO_MEMORY;
95 /* Parse domain and username */
97 parse_domain_user(state->request.data.auth.user, name_domain, name_user);
99 DEBUG(5,("no domain separator (%s) in username (%s) - failing auth\n", lp_winbind_separator(), state->request.data.auth.user));
100 result = NT_STATUS_INVALID_PARAMETER;
104 /* do password magic */
106 generate_random_buffer(chal, 8, False);
107 SMBencrypt(state->request.data.auth.pass, chal, local_lm_response);
109 SMBNTencrypt(state->request.data.auth.pass, chal, local_nt_response);
111 lm_resp = data_blob_talloc(mem_ctx, local_lm_response, sizeof(local_lm_response));
112 nt_resp = data_blob_talloc(mem_ctx, local_nt_response, sizeof(local_nt_response));
114 if ( !get_trust_pw(name_domain, trust_passwd, &last_change_time, &sec_channel_type) ) {
115 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
119 /* what domain should we contact? */
122 contact_domain = name_domain;
124 contact_domain = lp_workgroup();
126 /* check authentication loop */
130 ZERO_STRUCT(ret_creds);
134 /* Don't shut this down - it belongs to the connection cache code */
135 result = cm_get_netlogon_cli(contact_domain, trust_passwd,
136 sec_channel_type, False, &cli);
138 if (!NT_STATUS_IS_OK(result)) {
139 DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
143 result = cli_netlogon_sam_network_logon(cli, mem_ctx,
145 name_user, name_domain,
146 global_myname(), chal,
151 /* if we get access denied, a possible cuase was that we had and open
152 connection to the DC, but someone changed our machine accoutn password
153 out from underneath us using 'net rpc changetrustpw' */
155 if ( NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED) ) {
156 DEBUG(3,("winbindd_pam_auth: sam_logon returned ACCESS_DENIED. Maybe the trust account "
157 "password was changed and we didn't know it. Killing connections to domain %s\n",
163 /* We have to try a second time as cm_get_netlogon_cli
164 might not yet have noticed that the DC has killed
167 } while ( (attempts < 2) && retry );
169 clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &ret_creds);
171 if (NT_STATUS_IS_OK(result)) {
172 netsamlogon_cache_store( cli->mem_ctx, &info3 );
173 wcache_invalidate_samlogon(find_domain_from_name(name_domain), &info3);
177 /* give us a more useful (more correct?) error code */
178 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) || (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
179 result = NT_STATUS_NO_LOGON_SERVERS;
182 state->response.data.auth.nt_status = NT_STATUS_V(result);
183 fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
184 fstrcpy(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
185 state->response.data.auth.pam_error = nt_status_to_pam(result);
187 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n",
188 state->request.data.auth.user,
189 state->response.data.auth.nt_status_string,
190 state->response.data.auth.pam_error));
193 talloc_destroy(mem_ctx);
195 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
198 /**********************************************************************
199 Challenge Response Authentication Protocol
200 **********************************************************************/
202 enum winbindd_result winbindd_pam_auth_crap(struct winbindd_cli_state *state)
205 unsigned char trust_passwd[16];
206 time_t last_change_time;
207 uint32 sec_channel_type;
208 NET_USER_INFO_3 info3;
209 struct cli_state *cli;
210 TALLOC_CTX *mem_ctx = NULL;
212 const char *domain = NULL;
213 const char *workstation;
214 const char *contact_domain;
219 DATA_BLOB lm_resp, nt_resp;
221 if (!state->privileged) {
222 DEBUG(2, ("winbindd_pam_auth_crap: non-privileged access denied!\n"));
223 /* send a better message than ACCESS_DENIED */
224 push_utf8_fstring(state->response.data.auth.error_string, "winbind client not authorized to use winbindd_pam_auth_crap");
225 result = NT_STATUS_ACCESS_DENIED;
229 /* Ensure null termination */
230 state->request.data.auth_crap.user[sizeof(state->request.data.auth_crap.user)-1]=0;
231 state->request.data.auth_crap.domain[sizeof(state->request.data.auth_crap.domain)-1]=0;
233 if (!(mem_ctx = talloc_init("winbind pam auth crap for (utf8) %s", state->request.data.auth_crap.user))) {
234 DEBUG(0, ("winbindd_pam_auth_crap: could not talloc_init()!\n"));
235 result = NT_STATUS_NO_MEMORY;
239 if (pull_utf8_talloc(mem_ctx, &user, state->request.data.auth_crap.user) == (size_t)-1) {
240 DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
241 result = NT_STATUS_UNSUCCESSFUL;
245 if (*state->request.data.auth_crap.domain) {
247 if (pull_utf8_talloc(mem_ctx, &dom, state->request.data.auth_crap.domain) == (size_t)-1) {
248 DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
249 result = NT_STATUS_UNSUCCESSFUL;
253 } else if (lp_winbind_use_default_domain()) {
254 domain = lp_workgroup();
256 DEBUG(5,("no domain specified with username (%s) - failing auth\n",
258 result = NT_STATUS_INVALID_PARAMETER;
262 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state->pid,
265 if ( !get_trust_pw(domain, trust_passwd, &last_change_time, &sec_channel_type) ) {
266 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
270 if (*state->request.data.auth_crap.workstation) {
272 if (pull_utf8_talloc(mem_ctx, &wrk, state->request.data.auth_crap.workstation) == (size_t)-1) {
273 DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
274 result = NT_STATUS_UNSUCCESSFUL;
279 workstation = global_myname();
282 if (state->request.data.auth_crap.lm_resp_len > sizeof(state->request.data.auth_crap.lm_resp)
283 || state->request.data.auth_crap.nt_resp_len > sizeof(state->request.data.auth_crap.nt_resp)) {
284 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n",
285 state->request.data.auth_crap.lm_resp_len,
286 state->request.data.auth_crap.nt_resp_len));
287 result = NT_STATUS_INVALID_PARAMETER;
291 lm_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.lm_resp, state->request.data.auth_crap.lm_resp_len);
292 nt_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.nt_resp, state->request.data.auth_crap.nt_resp_len);
294 /* what domain should we contact? */
297 contact_domain = domain;
299 contact_domain = lp_workgroup();
303 ZERO_STRUCT(ret_creds);
307 /* Don't shut this down - it belongs to the connection cache code */
308 result = cm_get_netlogon_cli(contact_domain, trust_passwd, sec_channel_type, False, &cli);
310 if (!NT_STATUS_IS_OK(result)) {
311 DEBUG(3, ("could not open handle to NETLOGON pipe (error: %s)\n",
316 result = cli_netlogon_sam_network_logon(cli, mem_ctx,
320 state->request.data.auth_crap.chal,
326 /* if we get access denied, a possible cuase was that we had and open
327 connection to the DC, but someone changed our machine accoutn password
328 out from underneath us using 'net rpc changetrustpw' */
330 if ( NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED) ) {
331 DEBUG(3,("winbindd_pam_auth_crap: sam_logon returned ACCESS_DENIED. Maybe the trust account "
332 "password was changed and we didn't know it. Killing connections to domain %s\n",
338 /* We have to try a second time as cm_get_netlogon_cli
339 might not yet have noticed that the DC has killed
342 } while ( (attempts < 2) && retry );
344 clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &ret_creds);
346 if (NT_STATUS_IS_OK(result)) {
347 netsamlogon_cache_store( cli->mem_ctx, &info3 );
348 wcache_invalidate_samlogon(find_domain_from_name(domain), &info3);
350 if (state->request.flags & WBFLAG_PAM_INFO3_NDR) {
351 result = append_info3_as_ndr(mem_ctx, state, &info3);
354 if (state->request.flags & WBFLAG_PAM_NTKEY) {
355 memcpy(state->response.data.auth.nt_session_key, info3.user_sess_key, sizeof(state->response.data.auth.nt_session_key) /* 16 */);
357 if (state->request.flags & WBFLAG_PAM_LMKEY) {
358 memcpy(state->response.data.auth.first_8_lm_hash, info3.padding, sizeof(state->response.data.auth.first_8_lm_hash) /* 8 */);
363 /* give us a more useful (more correct?) error code */
364 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) || (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
365 result = NT_STATUS_NO_LOGON_SERVERS;
368 state->response.data.auth.nt_status = NT_STATUS_V(result);
369 push_utf8_fstring(state->response.data.auth.nt_status_string, nt_errstr(result));
370 if (!*state->response.data.auth.error_string)
371 push_utf8_fstring(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
372 state->response.data.auth.pam_error = nt_status_to_pam(result);
374 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
375 ("NTLM CRAP authentication for user [%s]\\[%s] returned %s (PAM: %d)\n",
378 state->response.data.auth.nt_status_string,
379 state->response.data.auth.pam_error));
382 talloc_destroy(mem_ctx);
384 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
387 /* Change a user password */
389 enum winbindd_result winbindd_pam_chauthtok(struct winbindd_cli_state *state)
392 char *oldpass, *newpass;
393 fstring domain, user;
396 DEBUG(3, ("[%5lu]: pam chauthtok %s\n", (unsigned long)state->pid,
397 state->request.data.chauthtok.user));
402 return WINBINDD_ERROR;
404 parse_domain_user(state->request.data.chauthtok.user, domain, user);
406 result = NT_STATUS_INVALID_PARAMETER;
410 /* Change password */
412 oldpass = state->request.data.chauthtok.oldpass;
413 newpass = state->request.data.chauthtok.newpass;
417 if ( NT_STATUS_IS_ERR(result = cm_get_sam_handle(domain, &hnd)) ) {
418 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
422 if (!cli_oem_change_password(hnd->cli, user, newpass, oldpass)) {
423 DEBUG(1, ("password change failed for user %s/%s\n", domain,
425 result = NT_STATUS_WRONG_PASSWORD;
427 result = NT_STATUS_OK;
431 state->response.data.auth.nt_status = NT_STATUS_V(result);
432 fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
433 fstrcpy(state->response.data.auth.error_string, nt_errstr(result));
434 state->response.data.auth.pam_error = nt_status_to_pam(result);
436 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
437 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
440 state->response.data.auth.nt_status_string,
441 state->response.data.auth.pam_error));
443 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;