r319: Fix a segfault in winbind. Thanks to Guenther Deschner for his valgrind log
[abartlet/samba.git/.git] / source3 / nsswitch / winbindd_pam.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon - pam auth funcions
5
6    Copyright (C) Andrew Tridgell 2000
7    Copyright (C) Tim Potter 2001
8    Copyright (C) Andrew Bartlett 2001-2002
9    
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.
14    
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.
19    
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.
23 */
24
25 #include "includes.h"
26 #include "winbindd.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
29
30
31 static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx, 
32                                     struct winbindd_cli_state *state, 
33                                     NET_USER_INFO_3 *info3) 
34 {
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;
39         }
40         if (!net_io_user_info3("", info3, &ps, 1, 3)) {
41                 prs_mem_free(&ps);
42                 return NT_STATUS_UNSUCCESSFUL;
43         }
44
45         size = prs_data_size(&ps);
46         state->response.extra_data = malloc(size);
47         if (!state->response.extra_data) {
48                 prs_mem_free(&ps);
49                 return NT_STATUS_NO_MEMORY;
50         }
51         prs_copy_all_data_out(state->response.extra_data, &ps);
52         state->response.length += size;
53         prs_mem_free(&ps);
54         return NT_STATUS_OK;
55 }
56
57 static NTSTATUS check_info3_in_group(TALLOC_CTX *mem_ctx, 
58                                      NET_USER_INFO_3 *info3,
59                                      const char *group_sid) 
60 {
61         DOM_SID required_membership_sid;
62         DOM_SID *all_sids;
63         size_t num_all_sids = (2 + info3->num_groups2 + info3->num_other_sids);
64         size_t i, j = 0;
65
66         /* Parse the 'required group' SID */
67         
68         if (!group_sid || !group_sid[0]) {
69                 /* NO sid supplied, all users may access */
70                 return NT_STATUS_OK;
71         }
72         
73         if (!string_to_sid(&required_membership_sid, group_sid)) {
74                 DEBUG(0, ("winbindd_pam_auth: could not parse %s as a SID!", 
75                           group_sid));
76
77                 return NT_STATUS_INVALID_PARAMETER;
78         }
79
80         all_sids = talloc(mem_ctx, sizeof(DOM_SID) * num_all_sids);
81         if (!all_sids)
82                 return NT_STATUS_NO_MEMORY;
83
84         /* and create (by appending rids) the 'domain' sids */
85         
86         sid_copy(&all_sids[0], &(info3->dom_sid.sid));
87         
88         if (!sid_append_rid(&all_sids[0], info3->user_rid)) {
89                 DEBUG(3,("could not append user's primary RID 0x%x\n",
90                          info3->user_rid));                     
91                 
92                 return NT_STATUS_INVALID_PARAMETER;
93         }
94         j++;
95
96         sid_copy(&all_sids[1], &(info3->dom_sid.sid));
97                 
98         if (!sid_append_rid(&all_sids[1], info3->group_rid)) {
99                 DEBUG(3,("could not append additional group rid 0x%x\n",
100                          info3->group_rid));                    
101                 
102                 return NT_STATUS_INVALID_PARAMETER;
103         }
104         j++;    
105
106         for (i = 0; i < info3->num_groups2; i++) {
107         
108                 sid_copy(&all_sids[j], &(info3->dom_sid.sid));
109                 
110                 if (!sid_append_rid(&all_sids[j], info3->gids[j].g_rid)) {
111                         DEBUG(3,("could not append additional group rid 0x%x\n",
112                                 info3->gids[j].g_rid));                 
113                                 
114                         return NT_STATUS_INVALID_PARAMETER;
115                 }
116                 j++;
117         }
118
119         /* Copy 'other' sids.  We need to do sid filtering here to
120            prevent possible elevation of privileges.  See:
121
122            http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
123          */
124
125         for (i = 0; i < info3->num_other_sids; j++) {
126                 sid_copy(&all_sids[info3->num_groups2 + i + 2],
127                          &info3->other_sids[j].sid);
128                 j++;
129         }
130
131         for (i = 0; i < j; i++) {
132                 fstring sid1, sid2;
133                 DEBUG(10, ("User has SID: %s\n", 
134                            sid_to_string(sid1, &all_sids[i])));
135                 if (sid_equal(&required_membership_sid, &all_sids[i])) {
136                         DEBUG(10, ("SID %s matches %s - user permitted to authenticate!\n", 
137                                    sid_to_string(sid1, &required_membership_sid), sid_to_string(sid2, &all_sids[i])));
138                         return NT_STATUS_OK;
139                 }
140         }
141         
142         /* Do not distinguish this error from a wrong username/pw */
143
144         return NT_STATUS_LOGON_FAILURE;
145 }
146
147 /**********************************************************************
148  Authenticate a user with a clear text password
149 **********************************************************************/
150
151 enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state) 
152 {
153         NTSTATUS result;
154         fstring name_domain, name_user;
155         unsigned char trust_passwd[16];
156         time_t last_change_time;
157         uint32 sec_channel_type;
158         NET_USER_INFO_3 info3;
159         struct cli_state *cli = NULL;
160         uchar chal[8];
161         TALLOC_CTX *mem_ctx = NULL;
162         DATA_BLOB lm_resp;
163         DATA_BLOB nt_resp;
164         DOM_CRED ret_creds;
165         int attempts = 0;
166         unsigned char local_lm_response[24];
167         unsigned char local_nt_response[24];
168         struct winbindd_domain *contact_domain;
169         BOOL retry;
170
171         /* Ensure null termination */
172         state->request.data.auth.user[sizeof(state->request.data.auth.user)-1]='\0';
173
174         /* Ensure null termination */
175         state->request.data.auth.pass[sizeof(state->request.data.auth.pass)-1]='\0';
176
177         DEBUG(3, ("[%5lu]: pam auth %s\n", (unsigned long)state->pid,
178                   state->request.data.auth.user));
179
180         if (!(mem_ctx = talloc_init("winbind pam auth for %s", state->request.data.auth.user))) {
181                 DEBUG(0, ("winbindd_pam_auth: could not talloc_init()!\n"));
182                 result = NT_STATUS_NO_MEMORY;
183                 goto done;
184         }
185
186         /* Parse domain and username */
187         
188         parse_domain_user(state->request.data.auth.user, name_domain, name_user);
189
190         /* do password magic */
191         
192         generate_random_buffer(chal, 8, False);
193         SMBencrypt(state->request.data.auth.pass, chal, local_lm_response);
194                 
195         SMBNTencrypt(state->request.data.auth.pass, chal, local_nt_response);
196
197         lm_resp = data_blob_talloc(mem_ctx, local_lm_response, sizeof(local_lm_response));
198         nt_resp = data_blob_talloc(mem_ctx, local_nt_response, sizeof(local_nt_response));
199         
200         /* what domain should we contact? */
201         
202         if ( IS_DC ) {
203                 if (!(contact_domain = find_domain_from_name(name_domain))) {
204                         DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n", 
205                                   state->request.data.auth.user, name_domain, name_user, name_domain)); 
206                         result = NT_STATUS_NO_SUCH_USER;
207                         goto done;
208                 }
209                 
210         } else {
211                 if (is_myname(name_domain)) {
212                         DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
213                         result =  NT_STATUS_NO_SUCH_USER;
214                         goto done;
215                 }
216
217                 if (!(contact_domain = find_our_domain())) {
218                         DEBUG(1, ("Authentication for [%s] -> [%s]\\[%s] in our domain failed - we can't find our domain!\n", 
219                                   state->request.data.auth.user, name_domain, name_user)); 
220                         result = NT_STATUS_NO_SUCH_USER;
221                         goto done;
222                 }
223         }
224
225         if ( !get_trust_pw(contact_domain->name, trust_passwd, &last_change_time, &sec_channel_type) ) {
226                 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
227                 goto done;
228         }
229
230         /* check authentication loop */
231
232         do {
233                 ZERO_STRUCT(info3);
234                 ZERO_STRUCT(ret_creds);
235                 retry = False;
236         
237                 /* Don't shut this down - it belongs to the connection cache code */
238                 result = cm_get_netlogon_cli(contact_domain, trust_passwd, 
239                                              sec_channel_type, False, &cli);
240
241                 if (!NT_STATUS_IS_OK(result)) {
242                         DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
243                         goto done;
244                 }
245
246                 result = cli_netlogon_sam_network_logon(cli, mem_ctx,
247                                                         &ret_creds,
248                                                         name_user, name_domain, 
249                                                         global_myname(), chal, 
250                                                         lm_resp, nt_resp,
251                                                         &info3);
252                 attempts += 1;
253                 
254                 /* We have to try a second time as cm_get_netlogon_cli
255                    might not yet have noticed that the DC has killed
256                    our connection. */
257
258                 if ( cli->fd == -1 ) {
259                         retry = True;
260                         continue;
261                 } 
262                 
263                 /* if we get access denied, a possible cuase was that we had and open
264                    connection to the DC, but someone changed our machine account password
265                    out from underneath us using 'net rpc changetrustpw' */
266                    
267                 if ( NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED) ) {
268                         DEBUG(3,("winbindd_pam_auth: sam_logon returned ACCESS_DENIED.  Maybe the trust account "
269                                 "password was changed and we didn't know it.  Killing connections to domain %s\n",
270                                 name_domain));
271                         winbindd_cm_flush();
272                         retry = True;
273                         cli = NULL;
274                 } 
275                 
276         } while ( (attempts < 2) && retry );
277
278         if (cli != NULL) {
279                 /* We might have come out of the loop above with cli == NULL,
280                    so don't dereference that. */
281                 clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &ret_creds);
282         }
283         
284         if (NT_STATUS_IS_OK(result)) {
285                 netsamlogon_cache_store( cli->mem_ctx, &info3 );
286                 wcache_invalidate_samlogon(find_domain_from_name(name_domain), &info3);
287
288                 /* Check if the user is in the right group */
289
290                 if (!NT_STATUS_IS_OK(result = check_info3_in_group(mem_ctx, &info3, state->request.data.auth.required_membership_sid))) {
291                         DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
292                                   state->request.data.auth.user, 
293                                   state->request.data.auth.required_membership_sid));
294                 }
295         }
296
297 done:
298         /* give us a more useful (more correct?) error code */
299         if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) || (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
300                 result = NT_STATUS_NO_LOGON_SERVERS;
301         }
302         
303         state->response.data.auth.nt_status = NT_STATUS_V(result);
304         fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
305
306         /* we might have given a more useful error above */
307         if (!*state->response.data.auth.error_string) 
308                 fstrcpy(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
309         state->response.data.auth.pam_error = nt_status_to_pam(result);
310
311         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n", 
312               state->request.data.auth.user, 
313               state->response.data.auth.nt_status_string,
314               state->response.data.auth.pam_error));          
315
316         if ( NT_STATUS_IS_OK(result) &&
317              (state->request.flags & WBFLAG_PAM_AFS_TOKEN) ) {
318
319                 char *afsname = strdup(lp_afs_username_map());
320                 char *cell;
321
322                 if (afsname == NULL) goto no_token;
323
324                 afsname = realloc_string_sub(afsname, "%D", name_domain);
325                 afsname = realloc_string_sub(afsname, "%u", name_user);
326                 afsname = realloc_string_sub(afsname, "%U", name_user);
327
328                 if (afsname == NULL) goto no_token;
329
330                 strlower_m(afsname);
331
332                 cell = strchr(afsname, '@');
333
334                 if (cell == NULL) goto no_token;
335
336                 *cell = '\0';
337                 cell += 1;
338
339                 /* Append an AFS token string */
340                 state->response.extra_data =
341                         afs_createtoken_str(afsname, cell);
342
343                 if (state->response.extra_data != NULL)
344                         state->response.length +=
345                                 strlen(state->response.extra_data)+1;
346
347         no_token:
348                 SAFE_FREE(afsname);
349         }
350                 
351         if (mem_ctx) 
352                 talloc_destroy(mem_ctx);
353         
354         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
355 }
356
357 /**********************************************************************
358  Challenge Response Authentication Protocol 
359 **********************************************************************/
360
361 enum winbindd_result winbindd_pam_auth_crap(struct winbindd_cli_state *state) 
362 {
363         NTSTATUS result;
364         unsigned char trust_passwd[16];
365         time_t last_change_time;
366         uint32 sec_channel_type;
367         NET_USER_INFO_3 info3;
368         struct cli_state *cli = NULL;
369         TALLOC_CTX *mem_ctx = NULL;
370         char *name_user = NULL;
371         const char *name_domain = NULL;
372         const char *workstation;
373         struct winbindd_domain *contact_domain;
374         DOM_CRED ret_creds;
375         int attempts = 0;
376         BOOL retry;
377
378         DATA_BLOB lm_resp, nt_resp;
379
380         if (!state->privileged) {
381                 char *error_string = NULL;
382                 DEBUG(2, ("winbindd_pam_auth_crap: non-privileged access denied.  !\n"));
383                 DEBUGADD(2, ("winbindd_pam_auth_crap: Ensure permissions on %s are set correctly.\n", 
384                              get_winbind_priv_pipe_dir()));
385                 /* send a better message than ACCESS_DENIED */
386                 asprintf(&error_string, "winbind client not authorized to use winbindd_pam_auth_crap.  Ensure permissions on %s are set correctly.",
387                          get_winbind_priv_pipe_dir());
388                 push_utf8_fstring(state->response.data.auth.error_string, error_string);
389                 SAFE_FREE(error_string);
390                 result =  NT_STATUS_ACCESS_DENIED;
391                 goto done;
392         }
393
394         /* Ensure null termination */
395         state->request.data.auth_crap.user[sizeof(state->request.data.auth_crap.user)-1]=0;
396         state->request.data.auth_crap.domain[sizeof(state->request.data.auth_crap.domain)-1]=0;
397
398         if (!(mem_ctx = talloc_init("winbind pam auth crap for (utf8) %s", state->request.data.auth_crap.user))) {
399                 DEBUG(0, ("winbindd_pam_auth_crap: could not talloc_init()!\n"));
400                 result = NT_STATUS_NO_MEMORY;
401                 goto done;
402         }
403
404         if (pull_utf8_talloc(mem_ctx, &name_user, state->request.data.auth_crap.user) == (size_t)-1) {
405                 DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
406                 result = NT_STATUS_UNSUCCESSFUL;
407                 goto done;
408         }
409
410         if (*state->request.data.auth_crap.domain) {
411                 char *dom = NULL;
412                 if (pull_utf8_talloc(mem_ctx, &dom, state->request.data.auth_crap.domain) == (size_t)-1) {
413                         DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
414                         result = NT_STATUS_UNSUCCESSFUL;
415                         goto done;
416                 }
417                 name_domain = dom;
418         } else if (lp_winbind_use_default_domain()) {
419                 name_domain = lp_workgroup();
420         } else {
421                 DEBUG(5,("no domain specified with username (%s) - failing auth\n", 
422                          name_user));
423                 result = NT_STATUS_NO_SUCH_USER;
424                 goto done;
425         }
426
427         DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state->pid,
428                   name_domain, name_user));
429            
430         if (*state->request.data.auth_crap.workstation) {
431                 char *wrk = NULL;
432                 if (pull_utf8_talloc(mem_ctx, &wrk, state->request.data.auth_crap.workstation) == (size_t)-1) {
433                         DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
434                         result = NT_STATUS_UNSUCCESSFUL;
435                         goto done;
436                 }
437                 workstation = wrk;
438         } else {
439                 workstation = global_myname();
440         }
441
442         if (state->request.data.auth_crap.lm_resp_len > sizeof(state->request.data.auth_crap.lm_resp)
443                 || state->request.data.auth_crap.nt_resp_len > sizeof(state->request.data.auth_crap.nt_resp)) {
444                 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n", 
445                           state->request.data.auth_crap.lm_resp_len, 
446                           state->request.data.auth_crap.nt_resp_len));
447                 result = NT_STATUS_INVALID_PARAMETER;
448                 goto done;
449         }
450
451         lm_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.lm_resp, state->request.data.auth_crap.lm_resp_len);
452         nt_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.nt_resp, state->request.data.auth_crap.nt_resp_len);
453         
454
455         /* what domain should we contact? */
456         
457         if ( IS_DC ) {
458                 if (!(contact_domain = find_domain_from_name(name_domain))) {
459                         DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n", 
460                                   state->request.data.auth_crap.user, name_domain, name_user, name_domain)); 
461                         result = NT_STATUS_NO_SUCH_USER;
462                         goto done;
463                 }
464                 
465         } else {
466                 if (is_myname(name_domain)) {
467                         DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
468                         result =  NT_STATUS_NO_SUCH_USER;
469                         goto done;
470                 }
471
472                 if (!(contact_domain = find_our_domain())) {
473                         DEBUG(1, ("Authenticatoin for [%s] -> [%s]\\[%s] in our domain failed - we can't find our domain!\n", 
474                                   state->request.data.auth_crap.user, name_domain, name_user)); 
475                         result = NT_STATUS_NO_SUCH_USER;
476                         goto done;
477                 }
478         }
479                 
480         if ( !get_trust_pw(contact_domain->name, trust_passwd, &last_change_time, &sec_channel_type) ) {
481                 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
482                 goto done;
483         }
484
485         do {
486                 ZERO_STRUCT(info3);
487                 ZERO_STRUCT(ret_creds);
488                 retry = False;
489
490                 /* Don't shut this down - it belongs to the connection cache code */
491                 result = cm_get_netlogon_cli(contact_domain, trust_passwd, sec_channel_type, False, &cli);
492
493                 if (!NT_STATUS_IS_OK(result)) {
494                         DEBUG(3, ("could not open handle to NETLOGON pipe (error: %s)\n",
495                                   nt_errstr(result)));
496                         goto done;
497                 }
498
499                 result = cli_netlogon_sam_network_logon(cli, mem_ctx,
500                                                         &ret_creds,
501                                                         name_user, name_domain,
502                                                         workstation,
503                                                         state->request.data.auth_crap.chal, 
504                                                         lm_resp, nt_resp, 
505                                                         &info3);
506
507                 attempts += 1;
508
509                 /* We have to try a second time as cm_get_netlogon_cli
510                    might not yet have noticed that the DC has killed
511                    our connection. */
512
513                 if ( cli->fd == -1 ) {
514                         retry = True;
515                         continue;
516                 } 
517
518                 /* if we get access denied, a possible cause was that we had and open
519                    connection to the DC, but someone changed our machine account password
520                    out from underneath us using 'net rpc changetrustpw' */
521                    
522                 if ( NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED) ) {
523                         DEBUG(3,("winbindd_pam_auth_crap: sam_logon returned ACCESS_DENIED.  Maybe the trust account "
524                                 "password was changed and we didn't know it.  Killing connections to domain %s\n",
525                                 contact_domain->name));
526                         winbindd_cm_flush();
527                         retry = True;
528                         cli = NULL;
529                 } 
530                 
531         } while ( (attempts < 2) && retry );
532
533         clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &ret_creds);
534         
535         if (NT_STATUS_IS_OK(result)) {
536                 netsamlogon_cache_store( cli->mem_ctx, &info3 );
537                 wcache_invalidate_samlogon(find_domain_from_name(name_domain), &info3);
538                 
539                 if (!NT_STATUS_IS_OK(result = check_info3_in_group(mem_ctx, &info3, state->request.data.auth_crap.required_membership_sid))) {
540                         DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
541                                   state->request.data.auth_crap.user, 
542                                   state->request.data.auth_crap.required_membership_sid));
543                         goto done;
544                 }
545
546                 if (state->request.flags & WBFLAG_PAM_INFO3_NDR) {
547                         result = append_info3_as_ndr(mem_ctx, state, &info3);
548                 } else if (state->request.flags & WBFLAG_PAM_UNIX_NAME) {
549                         /* ntlm_auth should return the unix username, per 
550                            'winbind use default domain' settings and the like */
551                         
552                         fstring username_out;
553                         const char *nt_username, *nt_domain;
554                         if (!(nt_username = unistr2_tdup(mem_ctx, &(info3.uni_user_name)))) {
555                                 /* If the server didn't give us one, just use the one we sent them */
556                                 nt_username = name_user;
557                         }
558                         
559                         if (!(nt_domain = unistr2_tdup(mem_ctx, &(info3.uni_logon_dom)))) {
560                                 /* If the server didn't give us one, just use the one we sent them */
561                                 nt_domain = name_domain;
562                         }
563
564                         fill_domain_username(username_out, nt_domain, nt_username);
565
566                         DEBUG(5, ("Setting unix username to [%s]\n", username_out));
567
568                         /* this interface is in UTF8 */
569                         if (push_utf8_allocate((char **)&state->response.extra_data, username_out) == -1) {
570                                 result = NT_STATUS_NO_MEMORY;
571                                 goto done;
572                         }
573                         state->response.length +=  strlen(state->response.extra_data)+1;
574                 }
575                 
576                 if (state->request.flags & WBFLAG_PAM_USER_SESSION_KEY) {
577                         memcpy(state->response.data.auth.user_session_key, info3.user_sess_key, sizeof(state->response.data.auth.user_session_key) /* 16 */);
578                 }
579                 if (state->request.flags & WBFLAG_PAM_LMKEY) {
580                         memcpy(state->response.data.auth.first_8_lm_hash, info3.padding, sizeof(state->response.data.auth.first_8_lm_hash) /* 8 */);
581                 }
582         }
583
584 done:
585         /* give us a more useful (more correct?) error code */
586         if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) || (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
587                 result = NT_STATUS_NO_LOGON_SERVERS;
588         }
589
590         if (state->request.flags & WBFLAG_PAM_NT_STATUS_SQUASH) {
591                 result = nt_status_squash(result);
592         }
593         
594         state->response.data.auth.nt_status = NT_STATUS_V(result);
595         push_utf8_fstring(state->response.data.auth.nt_status_string, nt_errstr(result));
596         
597         /* we might have given a more useful error above */
598         if (!*state->response.data.auth.error_string) 
599                 push_utf8_fstring(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
600         state->response.data.auth.pam_error = nt_status_to_pam(result);
601
602         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, 
603               ("NTLM CRAP authentication for user [%s]\\[%s] returned %s (PAM: %d)\n", 
604                name_domain,
605                name_user,
606                state->response.data.auth.nt_status_string,
607                state->response.data.auth.pam_error));         
608
609         if (mem_ctx) 
610                 talloc_destroy(mem_ctx);
611         
612         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
613 }
614
615 /* Change a user password */
616
617 enum winbindd_result winbindd_pam_chauthtok(struct winbindd_cli_state *state)
618 {
619         NTSTATUS result;
620         char *oldpass, *newpass;
621         fstring domain, user;
622         CLI_POLICY_HND *hnd;
623         TALLOC_CTX *mem_ctx;
624         struct winbindd_domain *contact_domain;
625
626         DEBUG(3, ("[%5lu]: pam chauthtok %s\n", (unsigned long)state->pid,
627                 state->request.data.chauthtok.user));
628
629         if (!(mem_ctx = talloc_init("winbind password change for (utf8) %s", 
630                                     state->request.data.chauthtok.user))) {
631                 DEBUG(0, ("winbindd_pam_auth_crap: could not talloc_init()!\n"));
632                 result = NT_STATUS_NO_MEMORY;
633                 goto done;
634         }
635
636         /* Setup crap */
637
638         if (state == NULL)
639                 return WINBINDD_ERROR;
640
641         parse_domain_user(state->request.data.chauthtok.user, domain, user);
642
643         if (!(contact_domain = find_domain_from_name(domain))) {
644                 DEBUG(3, ("Cannot change password for [%s] -> [%s]\\[%s] as %s is not a trusted domain\n", 
645                           state->request.data.chauthtok.user, domain, user, domain)); 
646                 result = NT_STATUS_NO_SUCH_USER;
647                 goto done;
648         }
649
650         /* Change password */
651
652         oldpass = state->request.data.chauthtok.oldpass;
653         newpass = state->request.data.chauthtok.newpass;
654
655         /* Get sam handle */
656
657         if (!NT_STATUS_IS_OK(result = cm_get_sam_handle(contact_domain, &hnd)) ) {
658                 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
659                 goto done;
660         }
661
662         result = cli_samr_chgpasswd_user(hnd->cli, mem_ctx, user, newpass, oldpass);
663
664 done:    
665         state->response.data.auth.nt_status = NT_STATUS_V(result);
666         fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
667         fstrcpy(state->response.data.auth.error_string, nt_errstr(result));
668         state->response.data.auth.pam_error = nt_status_to_pam(result);
669
670         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, 
671               ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n", 
672                domain,
673                user,
674                state->response.data.auth.nt_status_string,
675                state->response.data.auth.pam_error));         
676
677         if (mem_ctx)
678                 talloc_destroy(mem_ctx);
679
680         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
681 }