r10400: commit merge patch from jra
[Samba.git] / source / nsswitch / winbindd_pam.c
blob0b3c119afa02b26019a15f7a5334ecb34d58843c
1 /*
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.
25 #include "includes.h"
26 #include "winbindd.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
31 static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx,
32 struct winbindd_cli_state *state,
33 NET_USER_INFO_3 *info3)
35 prs_struct ps;
36 uint32 size;
37 if (!prs_init(&ps, 256 /* Random, non-zero number */, mem_ctx, MARSHALL)) {
38 return NT_STATUS_NO_MEMORY;
40 if (!net_io_user_info3("", info3, &ps, 1, 3)) {
41 prs_mem_free(&ps);
42 return NT_STATUS_UNSUCCESSFUL;
45 size = prs_data_size(&ps);
46 state->response.extra_data = SMB_MALLOC(size);
47 if (!state->response.extra_data) {
48 prs_mem_free(&ps);
49 return NT_STATUS_NO_MEMORY;
51 memset( state->response.extra_data, '\0', size );
52 prs_copy_all_data_out(state->response.extra_data, &ps);
53 state->response.length += size;
54 prs_mem_free(&ps);
55 return NT_STATUS_OK;
58 static NTSTATUS check_info3_in_group(TALLOC_CTX *mem_ctx,
59 NET_USER_INFO_3 *info3,
60 const char *group_sid)
62 DOM_SID require_membership_of_sid;
63 DOM_SID *all_sids;
64 size_t num_all_sids = (2 + info3->num_groups2 + info3->num_other_sids);
65 size_t i, j = 0;
67 /* Parse the 'required group' SID */
69 if (!group_sid || !group_sid[0]) {
70 /* NO sid supplied, all users may access */
71 return NT_STATUS_OK;
74 if (!string_to_sid(&require_membership_of_sid, group_sid)) {
75 DEBUG(0, ("check_info3_in_group: could not parse %s as a SID!",
76 group_sid));
78 return NT_STATUS_INVALID_PARAMETER;
81 all_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_all_sids);
82 if (!all_sids)
83 return NT_STATUS_NO_MEMORY;
85 /* and create (by appending rids) the 'domain' sids */
87 sid_copy(&all_sids[0], &(info3->dom_sid.sid));
89 if (!sid_append_rid(&all_sids[0], info3->user_rid)) {
90 DEBUG(3,("could not append user's primary RID 0x%x\n",
91 info3->user_rid));
93 return NT_STATUS_INVALID_PARAMETER;
95 j++;
97 sid_copy(&all_sids[1], &(info3->dom_sid.sid));
99 if (!sid_append_rid(&all_sids[1], info3->group_rid)) {
100 DEBUG(3,("could not append additional group rid 0x%x\n",
101 info3->group_rid));
103 return NT_STATUS_INVALID_PARAMETER;
105 j++;
107 for (i = 0; i < info3->num_groups2; i++) {
109 sid_copy(&all_sids[j], &(info3->dom_sid.sid));
111 if (!sid_append_rid(&all_sids[j], info3->gids[i].g_rid)) {
112 DEBUG(3,("could not append additional group rid 0x%x\n",
113 info3->gids[i].g_rid));
115 return NT_STATUS_INVALID_PARAMETER;
117 j++;
120 /* Copy 'other' sids. We need to do sid filtering here to
121 prevent possible elevation of privileges. See:
123 http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
126 for (i = 0; i < info3->num_other_sids; i++) {
127 sid_copy(&all_sids[info3->num_groups2 + i + 2],
128 &info3->other_sids[i].sid);
129 j++;
132 for (i = 0; i < j; i++) {
133 fstring sid1, sid2;
134 DEBUG(10, ("User has SID: %s\n",
135 sid_to_string(sid1, &all_sids[i])));
136 if (sid_equal(&require_membership_of_sid, &all_sids[i])) {
137 DEBUG(10, ("SID %s matches %s - user permitted to authenticate!\n",
138 sid_to_string(sid1, &require_membership_of_sid), sid_to_string(sid2, &all_sids[i])));
139 return NT_STATUS_OK;
143 /* Do not distinguish this error from a wrong username/pw */
145 return NT_STATUS_LOGON_FAILURE;
148 static struct winbindd_domain *find_auth_domain(const char *domain_name)
150 struct winbindd_domain *domain;
152 if (IS_DC) {
153 domain = find_domain_from_name_noinit(domain_name);
154 if (domain == NULL) {
155 DEBUG(3, ("Authentication for domain [%s] "
156 "as it is not a trusted domain\n",
157 domain_name));
159 return domain;
162 if (is_myname(domain_name)) {
163 DEBUG(3, ("Authentication for domain %s (local domain "
164 "to this server) not supported at this "
165 "stage\n", domain_name));
166 return NULL;
169 return find_our_domain();
172 static void set_auth_errors(struct winbindd_response *resp, NTSTATUS result)
174 resp->data.auth.nt_status = NT_STATUS_V(result);
175 fstrcpy(resp->data.auth.nt_status_string, nt_errstr(result));
177 /* we might have given a more useful error above */
178 if (*resp->data.auth.error_string == '\0')
179 fstrcpy(resp->data.auth.error_string,
180 get_friendly_nt_error_msg(result));
181 resp->data.auth.pam_error = nt_status_to_pam(result);
184 /**********************************************************************
185 Authenticate a user with a clear text password
186 **********************************************************************/
188 void winbindd_pam_auth(struct winbindd_cli_state *state)
190 struct winbindd_domain *domain;
191 fstring name_domain, name_user;
193 /* Ensure null termination */
194 state->request.data.auth.user
195 [sizeof(state->request.data.auth.user)-1]='\0';
197 /* Ensure null termination */
198 state->request.data.auth.pass
199 [sizeof(state->request.data.auth.pass)-1]='\0';
201 DEBUG(3, ("[%5lu]: pam auth %s\n", (unsigned long)state->pid,
202 state->request.data.auth.user));
204 /* Parse domain and username */
206 parse_domain_user(state->request.data.auth.user,
207 name_domain, name_user);
209 domain = find_auth_domain(name_domain);
211 if (domain == NULL) {
212 set_auth_errors(&state->response, NT_STATUS_NO_SUCH_USER);
213 DEBUG(5, ("Plain text authentication for %s returned %s "
214 "(PAM: %d)\n",
215 state->request.data.auth.user,
216 state->response.data.auth.nt_status_string,
217 state->response.data.auth.pam_error));
218 request_error(state);
219 return;
222 sendto_domain(state, domain);
225 enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
226 struct winbindd_cli_state *state)
228 NTSTATUS result;
229 fstring name_domain, name_user;
230 const char *srv_name_slash;
231 NET_USER_INFO_3 info3;
232 unsigned char *session_key;
233 struct rpc_pipe_client *pipe_cli;
234 uchar chal[8];
235 DATA_BLOB lm_resp;
236 DATA_BLOB nt_resp;
237 DOM_CRED ret_creds;
238 DOM_CRED *credentials;
239 int attempts = 0;
240 unsigned char local_lm_response[24];
241 unsigned char local_nt_response[24];
242 struct winbindd_domain *contact_domain;
243 BOOL retry;
245 /* Ensure null termination */
246 state->request.data.auth.user[sizeof(state->request.data.auth.user)-1]='\0';
248 /* Ensure null termination */
249 state->request.data.auth.pass[sizeof(state->request.data.auth.pass)-1]='\0';
251 DEBUG(3, ("[%5lu]: pam auth %s\n", (unsigned long)state->pid,
252 state->request.data.auth.user));
254 /* Parse domain and username */
256 parse_domain_user(state->request.data.auth.user, name_domain, name_user);
258 /* do password magic */
261 generate_random_buffer(chal, 8);
262 if (lp_client_ntlmv2_auth()) {
263 DATA_BLOB server_chal;
264 DATA_BLOB names_blob;
265 DATA_BLOB nt_response;
266 DATA_BLOB lm_response;
267 server_chal = data_blob_talloc(state->mem_ctx, chal, 8);
269 /* note that the 'workgroup' here is a best guess - we don't know
270 the server's domain at this point. The 'server name' is also
271 dodgy...
273 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
275 if (!SMBNTLMv2encrypt(name_user, name_domain,
276 state->request.data.auth.pass,
277 &server_chal,
278 &names_blob,
279 &lm_response, &nt_response, NULL)) {
280 data_blob_free(&names_blob);
281 data_blob_free(&server_chal);
282 DEBUG(0, ("winbindd_pam_auth: SMBNTLMv2encrypt() failed!\n"));
283 result = NT_STATUS_NO_MEMORY;
284 goto done;
286 data_blob_free(&names_blob);
287 data_blob_free(&server_chal);
288 lm_resp = data_blob_talloc(state->mem_ctx, lm_response.data,
289 lm_response.length);
290 nt_resp = data_blob_talloc(state->mem_ctx, nt_response.data,
291 nt_response.length);
292 data_blob_free(&lm_response);
293 data_blob_free(&nt_response);
295 } else {
296 if (lp_client_lanman_auth()
297 && SMBencrypt(state->request.data.auth.pass,
298 chal,
299 local_lm_response)) {
300 lm_resp = data_blob_talloc(state->mem_ctx,
301 local_lm_response,
302 sizeof(local_lm_response));
303 } else {
304 lm_resp = data_blob(NULL, 0);
306 SMBNTencrypt(state->request.data.auth.pass,
307 chal,
308 local_nt_response);
310 nt_resp = data_blob_talloc(state->mem_ctx,
311 local_nt_response,
312 sizeof(local_nt_response));
316 /* what domain should we contact? */
318 if ( IS_DC ) {
319 if (!(contact_domain = find_domain_from_name(name_domain))) {
320 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
321 state->request.data.auth.user, name_domain, name_user, name_domain));
322 result = NT_STATUS_NO_SUCH_USER;
323 goto done;
326 } else {
327 if (is_myname(name_domain)) {
328 DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
329 result = NT_STATUS_NO_SUCH_USER;
330 goto done;
333 contact_domain = find_our_domain();
336 srv_name_slash = talloc_asprintf(state->mem_ctx, "\\\\%s",
337 contact_domain->dcname);
338 if (srv_name_slash == NULL) {
339 DEBUG(0, ("talloc_asprintf failed\n"));
340 return WINBINDD_ERROR;
343 /* check authentication loop */
345 do {
346 DOM_CRED clnt_creds;
348 ZERO_STRUCT(info3);
349 ZERO_STRUCT(ret_creds);
350 retry = False;
352 result = cm_connect_netlogon(contact_domain, state->mem_ctx,
353 &pipe_cli, &session_key,
354 &credentials);
356 if (!NT_STATUS_IS_OK(result)) {
357 DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
358 goto done;
361 credentials->timestamp.time = time(NULL);
362 memcpy(&clnt_creds, credentials, sizeof(clnt_creds));
364 /* Calculate the new credentials. */
365 cred_create(session_key, &credentials->challenge,
366 clnt_creds.timestamp, &(clnt_creds.challenge));
368 result = rpccli_netlogon_sam_network_logon(pipe_cli,
369 state->mem_ctx,
370 srv_name_slash,
371 &clnt_creds,
372 &ret_creds,
373 name_user,
374 name_domain,
375 global_myname(),
376 chal, lm_resp,
377 nt_resp, &info3,
378 session_key);
379 attempts += 1;
381 /* We have to try a second time as cm_connect_netlogon
382 might not yet have noticed that the DC has killed
383 our connection. */
385 if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
386 retry = True;
387 continue;
390 /* if we get access denied, a possible cause was that we had
391 and open connection to the DC, but someone changed our
392 machine account password out from underneath us using 'net
393 rpc changetrustpw' */
395 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
396 DEBUG(3,("winbindd_pam_auth: sam_logon returned "
397 "ACCESS_DENIED. Maybe the trust account "
398 "password was changed and we didn't know it. "
399 "Killing connections to domain %s\n",
400 name_domain));
401 invalidate_cm_connection(&contact_domain->conn);
402 retry = True;
405 } while ( (attempts < 2) && retry );
407 /* Only check creds if we got a connection. */
408 if (contact_domain->conn.cli &&
409 !(NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
410 NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
411 if (!clnt_deal_with_creds(session_key, credentials, &ret_creds)) {
412 DEBUG(3, ("DC %s sent wrong credentials\n",
413 pipe_cli->cli->srv_name_slash));
414 result = NT_STATUS_ACCESS_DENIED;
418 if (NT_STATUS_IS_OK(result)) {
419 netsamlogon_cache_store(state->mem_ctx, name_user, &info3);
420 wcache_invalidate_samlogon(find_domain_from_name(name_domain), &info3);
422 /* Check if the user is in the right group */
424 if (!NT_STATUS_IS_OK(result = check_info3_in_group(state->mem_ctx, &info3, state->request.data.auth.require_membership_of_sid))) {
425 DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
426 state->request.data.auth.user,
427 state->request.data.auth.require_membership_of_sid));
431 done:
432 /* give us a more useful (more correct?) error code */
433 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) || (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
434 result = NT_STATUS_NO_LOGON_SERVERS;
437 state->response.data.auth.nt_status = NT_STATUS_V(result);
438 fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
440 /* we might have given a more useful error above */
441 if (!*state->response.data.auth.error_string)
442 fstrcpy(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
443 state->response.data.auth.pam_error = nt_status_to_pam(result);
445 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n",
446 state->request.data.auth.user,
447 state->response.data.auth.nt_status_string,
448 state->response.data.auth.pam_error));
450 if ( NT_STATUS_IS_OK(result) &&
451 (state->request.flags & WBFLAG_PAM_AFS_TOKEN) ) {
453 char *afsname = SMB_STRDUP(lp_afs_username_map());
454 char *cell;
456 if (afsname == NULL) goto no_token;
458 afsname = realloc_string_sub(afsname, "%D", name_domain);
459 afsname = realloc_string_sub(afsname, "%u", name_user);
460 afsname = realloc_string_sub(afsname, "%U", name_user);
463 DOM_SID user_sid;
464 fstring sidstr;
466 sid_copy(&user_sid, &info3.dom_sid.sid);
467 sid_append_rid(&user_sid, info3.user_rid);
468 sid_to_string(sidstr, &user_sid);
469 afsname = realloc_string_sub(afsname, "%s", sidstr);
472 if (afsname == NULL) goto no_token;
474 strlower_m(afsname);
476 DEBUG(10, ("Generating token for user %s\n", afsname));
478 cell = strchr(afsname, '@');
480 if (cell == NULL) goto no_token;
482 *cell = '\0';
483 cell += 1;
485 /* Append an AFS token string */
486 state->response.extra_data =
487 afs_createtoken_str(afsname, cell);
489 if (state->response.extra_data != NULL)
490 state->response.length +=
491 strlen(state->response.extra_data)+1;
493 no_token:
494 SAFE_FREE(afsname);
497 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
500 /**********************************************************************
501 Challenge Response Authentication Protocol
502 **********************************************************************/
504 void winbindd_pam_auth_crap(struct winbindd_cli_state *state)
506 struct winbindd_domain *domain = NULL;
507 const char *domain_name = NULL;
508 NTSTATUS result;
510 if (!state->privileged) {
511 char *error_string = NULL;
512 DEBUG(2, ("winbindd_pam_auth_crap: non-privileged access "
513 "denied. !\n"));
514 DEBUGADD(2, ("winbindd_pam_auth_crap: Ensure permissions "
515 "on %s are set correctly.\n",
516 get_winbind_priv_pipe_dir()));
517 /* send a better message than ACCESS_DENIED */
518 error_string = talloc_asprintf(state->mem_ctx,
519 "winbind client not authorized "
520 "to use winbindd_pam_auth_crap."
521 " Ensure permissions on %s "
522 "are set correctly.",
523 get_winbind_priv_pipe_dir());
524 fstrcpy(state->response.data.auth.error_string, error_string);
525 result = NT_STATUS_ACCESS_DENIED;
526 goto done;
529 /* Ensure null termination */
530 state->request.data.auth_crap.user
531 [sizeof(state->request.data.auth_crap.user)-1]=0;
532 state->request.data.auth_crap.domain
533 [sizeof(state->request.data.auth_crap.domain)-1]=0;
535 DEBUG(3, ("[%5lu]: pam auth crap domain: [%s] user: %s\n",
536 (unsigned long)state->pid,
537 state->request.data.auth_crap.domain,
538 state->request.data.auth_crap.user));
540 if (*state->request.data.auth_crap.domain != '\0') {
541 domain_name = state->request.data.auth_crap.domain;
542 } else if (lp_winbind_use_default_domain()) {
543 domain_name = lp_workgroup();
546 if (domain_name != NULL)
547 domain = find_auth_domain(domain_name);
549 if (domain != NULL) {
550 sendto_domain(state, domain);
551 return;
554 result = NT_STATUS_NO_SUCH_USER;
556 done:
557 set_auth_errors(&state->response, result);
558 DEBUG(5, ("CRAP authentication for %s returned %s (PAM: %d)\n",
559 state->request.data.auth.user,
560 state->response.data.auth.nt_status_string,
561 state->response.data.auth.pam_error));
562 request_error(state);
563 return;
567 enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
568 struct winbindd_cli_state *state)
570 NTSTATUS result;
571 const char *srv_name_slash;
572 NET_USER_INFO_3 info3;
573 unsigned char *session_key;
574 struct rpc_pipe_client *pipe_cli;
575 DOM_CRED *credentials;
576 const char *name_user = NULL;
577 const char *name_domain = NULL;
578 const char *workstation;
579 struct winbindd_domain *contact_domain;
580 DOM_CRED ret_creds;
581 int attempts = 0;
582 BOOL retry;
584 DATA_BLOB lm_resp, nt_resp;
586 /* This is child-only, so no check for privileged access is needed
587 anymore */
589 /* Ensure null termination */
590 state->request.data.auth_crap.user[sizeof(state->request.data.auth_crap.user)-1]=0;
591 state->request.data.auth_crap.domain[sizeof(state->request.data.auth_crap.domain)-1]=0;
593 name_user = state->request.data.auth_crap.user;
595 if (*state->request.data.auth_crap.domain) {
596 name_domain = state->request.data.auth_crap.domain;
597 } else if (lp_winbind_use_default_domain()) {
598 name_domain = lp_workgroup();
599 } else {
600 DEBUG(5,("no domain specified with username (%s) - failing auth\n",
601 name_user));
602 result = NT_STATUS_NO_SUCH_USER;
603 goto done;
606 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state->pid,
607 name_domain, name_user));
609 if (*state->request.data.auth_crap.workstation) {
610 workstation = state->request.data.auth_crap.workstation;
611 } else {
612 workstation = global_myname();
615 if (state->request.data.auth_crap.lm_resp_len > sizeof(state->request.data.auth_crap.lm_resp)
616 || state->request.data.auth_crap.nt_resp_len > sizeof(state->request.data.auth_crap.nt_resp)) {
617 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n",
618 state->request.data.auth_crap.lm_resp_len,
619 state->request.data.auth_crap.nt_resp_len));
620 result = NT_STATUS_INVALID_PARAMETER;
621 goto done;
624 lm_resp = data_blob_talloc(state->mem_ctx, state->request.data.auth_crap.lm_resp, state->request.data.auth_crap.lm_resp_len);
625 nt_resp = data_blob_talloc(state->mem_ctx, state->request.data.auth_crap.nt_resp, state->request.data.auth_crap.nt_resp_len);
628 /* what domain should we contact? */
630 if ( IS_DC ) {
631 if (!(contact_domain = find_domain_from_name(name_domain))) {
632 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
633 state->request.data.auth_crap.user, name_domain, name_user, name_domain));
634 result = NT_STATUS_NO_SUCH_USER;
635 goto done;
638 } else {
639 if (is_myname(name_domain)) {
640 DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
641 result = NT_STATUS_NO_SUCH_USER;
642 goto done;
645 contact_domain = find_our_domain();
648 srv_name_slash = talloc_asprintf(state->mem_ctx, "\\\\%s",
649 contact_domain->dcname);
650 if (srv_name_slash == NULL) {
651 DEBUG(0, ("talloc_asprintf failed\n"));
652 return WINBINDD_ERROR;
655 do {
656 DOM_CRED clnt_creds;
657 ZERO_STRUCT(info3);
658 ZERO_STRUCT(ret_creds);
659 retry = False;
661 result = cm_connect_netlogon(contact_domain, state->mem_ctx,
662 &pipe_cli, &session_key,
663 &credentials);
665 if (!NT_STATUS_IS_OK(result)) {
666 DEBUG(3, ("could not open handle to NETLOGON pipe (error: %s)\n",
667 nt_errstr(result)));
668 goto done;
671 credentials->timestamp.time = time(NULL);
672 memcpy(&clnt_creds, credentials, sizeof(clnt_creds));
674 /* Calculate the new credentials. */
675 cred_create(session_key, &credentials->challenge,
676 clnt_creds.timestamp, &(clnt_creds.challenge));
678 result = rpccli_netlogon_sam_network_logon(pipe_cli,
679 state->mem_ctx,
680 srv_name_slash,
681 &clnt_creds,
682 &ret_creds,
683 name_user,
684 name_domain,
685 global_myname(),
686 state->request.data.auth_crap.chal,
687 lm_resp,
688 nt_resp, &info3,
689 session_key);
691 attempts += 1;
693 /* We have to try a second time as cm_connect_netlogon
694 might not yet have noticed that the DC has killed
695 our connection. */
697 if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
698 retry = True;
699 continue;
702 /* if we get access denied, a possible cause was that we had and open
703 connection to the DC, but someone changed our machine account password
704 out from underneath us using 'net rpc changetrustpw' */
706 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
707 DEBUG(3,("winbindd_pam_auth: sam_logon returned "
708 "ACCESS_DENIED. Maybe the trust account "
709 "password was changed and we didn't know it. "
710 "Killing connections to domain %s\n",
711 name_domain));
712 invalidate_cm_connection(&contact_domain->conn);
713 retry = True;
716 } while ( (attempts < 2) && retry );
718 /* Only check creds if we got a connection. */
719 if (contact_domain->conn.cli &&
720 !(NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
721 (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
722 if (!clnt_deal_with_creds(session_key, credentials, &ret_creds)) {
723 DEBUG(3, ("DC %s sent wrong credentials\n",
724 pipe_cli->cli->srv_name_slash));
725 result = NT_STATUS_ACCESS_DENIED;
729 if (NT_STATUS_IS_OK(result)) {
730 netsamlogon_cache_store( state->mem_ctx, name_user, &info3 );
731 wcache_invalidate_samlogon(find_domain_from_name(name_domain), &info3);
733 if (!NT_STATUS_IS_OK(result = check_info3_in_group(state->mem_ctx, &info3, state->request.data.auth_crap.require_membership_of_sid))) {
734 DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
735 state->request.data.auth_crap.user,
736 state->request.data.auth_crap.require_membership_of_sid));
737 goto done;
740 if (state->request.flags & WBFLAG_PAM_INFO3_NDR) {
741 result = append_info3_as_ndr(state->mem_ctx, state, &info3);
742 } else if (state->request.flags & WBFLAG_PAM_UNIX_NAME) {
743 /* ntlm_auth should return the unix username, per
744 'winbind use default domain' settings and the like */
746 fstring username_out;
747 const char *nt_username, *nt_domain;
748 if (!(nt_username = unistr2_tdup(state->mem_ctx, &(info3.uni_user_name)))) {
749 /* If the server didn't give us one, just use the one we sent them */
750 nt_username = name_user;
753 if (!(nt_domain = unistr2_tdup(state->mem_ctx, &(info3.uni_logon_dom)))) {
754 /* If the server didn't give us one, just use the one we sent them */
755 nt_domain = name_domain;
758 fill_domain_username(username_out, nt_domain, nt_username);
760 DEBUG(5, ("Setting unix username to [%s]\n", username_out));
762 state->response.extra_data = SMB_STRDUP(username_out);
763 if (!state->response.extra_data) {
764 result = NT_STATUS_NO_MEMORY;
765 goto done;
767 state->response.length += strlen(state->response.extra_data)+1;
770 if (state->request.flags & WBFLAG_PAM_USER_SESSION_KEY) {
771 memcpy(state->response.data.auth.user_session_key, info3.user_sess_key, sizeof(state->response.data.auth.user_session_key) /* 16 */);
773 if (state->request.flags & WBFLAG_PAM_LMKEY) {
774 memcpy(state->response.data.auth.first_8_lm_hash, info3.lm_sess_key, sizeof(state->response.data.auth.first_8_lm_hash) /* 8 */);
778 done:
779 /* give us a more useful (more correct?) error code */
780 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) || (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
781 result = NT_STATUS_NO_LOGON_SERVERS;
784 if (state->request.flags & WBFLAG_PAM_NT_STATUS_SQUASH) {
785 result = nt_status_squash(result);
788 state->response.data.auth.nt_status = NT_STATUS_V(result);
789 fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
791 /* we might have given a more useful error above */
792 if (!*state->response.data.auth.error_string)
793 fstrcpy(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
794 state->response.data.auth.pam_error = nt_status_to_pam(result);
796 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
797 ("NTLM CRAP authentication for user [%s]\\[%s] returned %s (PAM: %d)\n",
798 name_domain,
799 name_user,
800 state->response.data.auth.nt_status_string,
801 state->response.data.auth.pam_error));
803 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
806 /* Change a user password */
808 void winbindd_pam_chauthtok(struct winbindd_cli_state *state)
810 NTSTATUS result;
811 char *oldpass, *newpass;
812 fstring domain, user;
813 POLICY_HND dom_pol;
814 struct winbindd_domain *contact_domain;
815 struct rpc_pipe_client *cli;
817 DEBUG(3, ("[%5lu]: pam chauthtok %s\n", (unsigned long)state->pid,
818 state->request.data.chauthtok.user));
820 /* Setup crap */
822 parse_domain_user(state->request.data.chauthtok.user, domain, user);
824 if (!(contact_domain = find_domain_from_name(domain))) {
825 DEBUG(3, ("Cannot change password for [%s] -> [%s]\\[%s] as %s is not a trusted domain\n",
826 state->request.data.chauthtok.user, domain, user, domain));
827 result = NT_STATUS_NO_SUCH_USER;
828 goto done;
831 /* Change password */
833 oldpass = state->request.data.chauthtok.oldpass;
834 newpass = state->request.data.chauthtok.newpass;
836 /* Get sam handle */
838 result = cm_connect_sam(contact_domain, state->mem_ctx, &cli,
839 &dom_pol);
840 if (!NT_STATUS_IS_OK(result)) {
841 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
842 goto done;
845 result = rpccli_samr_chgpasswd_user(cli, state->mem_ctx, user, newpass,
846 oldpass);
848 done:
849 state->response.data.auth.nt_status = NT_STATUS_V(result);
850 fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
851 fstrcpy(state->response.data.auth.error_string, nt_errstr(result));
852 state->response.data.auth.pam_error = nt_status_to_pam(result);
854 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
855 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
856 domain,
857 user,
858 state->response.data.auth.nt_status_string,
859 state->response.data.auth.pam_error));
861 if (NT_STATUS_IS_OK(result))
862 request_ok(state);
863 else
864 request_error(state);