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