fix bug 382: make sure to free and reset the cli_state pointer after flushing connect...
[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 "winbindd.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
28
29
30 static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx, 
31                                     struct winbindd_cli_state *state, 
32                                     NET_USER_INFO_3 *info3) 
33 {
34         prs_struct ps;
35         uint32 size;
36         if (!prs_init(&ps, 256 /* Random, non-zero number */, mem_ctx, MARSHALL)) {
37                 return NT_STATUS_NO_MEMORY;
38         }
39         if (!net_io_user_info3("", info3, &ps, 1, 3)) {
40                 prs_mem_free(&ps);
41                 return NT_STATUS_UNSUCCESSFUL;
42         }
43
44         size = prs_data_size(&ps);
45         state->response.extra_data = malloc(size);
46         if (!state->response.extra_data) {
47                 prs_mem_free(&ps);
48                 return NT_STATUS_NO_MEMORY;
49         }
50         prs_copy_all_data_out(state->response.extra_data, &ps);
51         state->response.length += size;
52         prs_mem_free(&ps);
53         return NT_STATUS_OK;
54 }
55
56 /**********************************************************************
57  Authenticate a user with a clear test password
58 **********************************************************************/
59
60 enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state) 
61 {
62         NTSTATUS result;
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 = NULL;
69         uchar chal[8];
70         TALLOC_CTX *mem_ctx = NULL;
71         DATA_BLOB lm_resp;
72         DATA_BLOB nt_resp;
73         DOM_CRED ret_creds;
74         int attempts = 0;
75         unsigned char local_lm_response[24];
76         unsigned char local_nt_response[24];
77         const char *contact_domain;
78
79         /* Ensure null termination */
80         state->request.data.auth.user[sizeof(state->request.data.auth.user)-1]='\0';
81
82         /* Ensure null termination */
83         state->request.data.auth.pass[sizeof(state->request.data.auth.pass)-1]='\0';
84
85         DEBUG(3, ("[%5lu]: pam auth %s\n", (unsigned long)state->pid,
86                   state->request.data.auth.user));
87
88         if (!(mem_ctx = talloc_init("winbind pam auth for %s", state->request.data.auth.user))) {
89                 DEBUG(0, ("winbindd_pam_auth: could not talloc_init()!\n"));
90                 result = NT_STATUS_NO_MEMORY;
91                 goto done;
92         }
93
94         /* Parse domain and username */
95         
96         parse_domain_user(state->request.data.auth.user, name_domain, name_user);
97         if ( !name_domain ) {
98                 DEBUG(5,("no domain separator (%s) in username (%s) - failing auth\n", lp_winbind_separator(), state->request.data.auth.user));
99                 result = NT_STATUS_INVALID_PARAMETER;
100                 goto done;
101         }
102
103         /* do password magic */
104         
105         generate_random_buffer(chal, 8, False);
106         SMBencrypt(state->request.data.auth.pass, chal, local_lm_response);
107                 
108         SMBNTencrypt(state->request.data.auth.pass, chal, local_nt_response);
109
110         lm_resp = data_blob_talloc(mem_ctx, local_lm_response, sizeof(local_lm_response));
111         nt_resp = data_blob_talloc(mem_ctx, local_nt_response, sizeof(local_nt_response));
112         
113         if ( !get_trust_pw(name_domain, trust_passwd, &last_change_time, &sec_channel_type) ) {
114                 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
115                 goto done;
116         }
117
118         /* what domain should we contact? */
119         
120         if ( IS_DC )
121                 contact_domain = name_domain;
122         else
123                 contact_domain = lp_workgroup();
124                 
125         /* check authentication loop */
126
127         do {
128                 ZERO_STRUCT(info3);
129                 ZERO_STRUCT(ret_creds);
130
131                 if ( cli ) 
132                         SAFE_FREE(cli);
133         
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);
137
138                 if (!NT_STATUS_IS_OK(result)) {
139                         DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
140                         goto done;
141                 }
142
143                 result = cli_netlogon_sam_network_logon(cli, mem_ctx,
144                                                         &ret_creds,
145                                                         name_user, name_domain, 
146                                                         global_myname(), chal, 
147                                                         lm_resp, nt_resp,
148                                                         &info3);
149                 attempts += 1;
150                 
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' */
154                    
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",
158                                 name_domain));
159                         winbindd_cm_flush();
160                         cli->fd = -1;
161                 } 
162                 
163                 /* We have to try a second time as cm_get_netlogon_cli
164                    might not yet have noticed that the DC has killed
165                    our connection. */
166
167         } while ( (attempts < 2) && (cli->fd == -1) );
168
169         
170         clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &ret_creds);
171         
172         if (NT_STATUS_IS_OK(result)) {
173                 netsamlogon_cache_store( cli->mem_ctx, &info3 );
174                 wcache_invalidate_samlogon(find_domain_from_name(name_domain), &info3);
175         }
176         
177         
178 done:
179         
180         /* give us a more useful (more correct?) error code */
181         if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) || (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
182                 result = NT_STATUS_NO_LOGON_SERVERS;
183         }
184         
185         state->response.data.auth.nt_status = NT_STATUS_V(result);
186         fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
187         fstrcpy(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
188         state->response.data.auth.pam_error = nt_status_to_pam(result);
189
190         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n", 
191               state->request.data.auth.user, 
192               state->response.data.auth.nt_status_string,
193               state->response.data.auth.pam_error));          
194
195         if (mem_ctx) 
196                 talloc_destroy(mem_ctx);
197         
198         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
199 }
200
201 /**********************************************************************
202  Challenge Response Authentication Protocol 
203 **********************************************************************/
204
205 enum winbindd_result winbindd_pam_auth_crap(struct winbindd_cli_state *state) 
206 {
207         NTSTATUS result;
208         unsigned char trust_passwd[16];
209         time_t last_change_time;
210         uint32 sec_channel_type;
211         NET_USER_INFO_3 info3;
212         struct cli_state *cli = NULL;
213         TALLOC_CTX *mem_ctx = NULL;
214         char *user = NULL;
215         const char *domain = NULL;
216         const char *workstation;
217         const char *contact_domain;
218         DOM_CRED ret_creds;
219         int attempts = 0;
220
221         DATA_BLOB lm_resp, nt_resp;
222
223         if (!state->privileged) {
224                 DEBUG(2, ("winbindd_pam_auth_crap: non-privileged access denied!\n"));
225                 /* send a better message than ACCESS_DENIED */
226                 push_utf8_fstring(state->response.data.auth.error_string, "winbind client not authorized to use winbindd_pam_auth_crap");
227                 result =  NT_STATUS_ACCESS_DENIED;
228                 goto done;
229         }
230
231         /* Ensure null termination */
232         state->request.data.auth_crap.user[sizeof(state->request.data.auth_crap.user)-1]=0;
233         state->request.data.auth_crap.domain[sizeof(state->request.data.auth_crap.domain)-1]=0;
234
235         if (!(mem_ctx = talloc_init("winbind pam auth crap for (utf8) %s", state->request.data.auth_crap.user))) {
236                 DEBUG(0, ("winbindd_pam_auth_crap: could not talloc_init()!\n"));
237                 result = NT_STATUS_NO_MEMORY;
238                 goto done;
239         }
240
241         if (pull_utf8_talloc(mem_ctx, &user, state->request.data.auth_crap.user) == (size_t)-1) {
242                 DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
243                 result = NT_STATUS_UNSUCCESSFUL;
244                 goto done;
245         }
246
247         if (*state->request.data.auth_crap.domain) {
248                 char *dom = NULL;
249                 if (pull_utf8_talloc(mem_ctx, &dom, state->request.data.auth_crap.domain) == (size_t)-1) {
250                         DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
251                         result = NT_STATUS_UNSUCCESSFUL;
252                         goto done;
253                 }
254                 domain = dom;
255         } else if (lp_winbind_use_default_domain()) {
256                 domain = lp_workgroup();
257         } else {
258                 DEBUG(5,("no domain specified with username (%s) - failing auth\n", 
259                          user));
260                 result = NT_STATUS_INVALID_PARAMETER;
261                 goto done;
262         }
263
264         DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state->pid,
265                   domain, user));
266            
267         if ( !get_trust_pw(domain, trust_passwd, &last_change_time, &sec_channel_type) ) {
268                 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
269                 goto done;
270         }
271
272         if (*state->request.data.auth_crap.workstation) {
273                 char *wrk = NULL;
274                 if (pull_utf8_talloc(mem_ctx, &wrk, state->request.data.auth_crap.workstation) == (size_t)-1) {
275                         DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
276                         result = NT_STATUS_UNSUCCESSFUL;
277                         goto done;
278                 }
279                 workstation = wrk;
280         } else {
281                 workstation = global_myname();
282         }
283
284         if (state->request.data.auth_crap.lm_resp_len > sizeof(state->request.data.auth_crap.lm_resp)
285                 || state->request.data.auth_crap.nt_resp_len > sizeof(state->request.data.auth_crap.nt_resp)) {
286                 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n", 
287                           state->request.data.auth_crap.lm_resp_len, 
288                           state->request.data.auth_crap.nt_resp_len));
289                 result = NT_STATUS_INVALID_PARAMETER;
290                 goto done;
291         }
292
293         lm_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.lm_resp, state->request.data.auth_crap.lm_resp_len);
294         nt_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.nt_resp, state->request.data.auth_crap.nt_resp_len);
295         
296         /* what domain should we contact? */
297         
298         if ( IS_DC )
299                 contact_domain = domain;
300         else
301                 contact_domain = lp_workgroup();
302         
303         do {
304                 ZERO_STRUCT(info3);
305                 ZERO_STRUCT(ret_creds);
306
307                 if ( cli ) 
308                         SAFE_FREE( cli );
309
310                 /* Don't shut this down - it belongs to the connection cache code */
311                 result = cm_get_netlogon_cli(contact_domain, trust_passwd, sec_channel_type, False, &cli);
312
313                 if (!NT_STATUS_IS_OK(result)) {
314                         DEBUG(3, ("could not open handle to NETLOGON pipe (error: %s)\n",
315                                   nt_errstr(result)));
316                         goto done;
317                 }
318
319                 result = cli_netlogon_sam_network_logon(cli, mem_ctx,
320                                                         &ret_creds,
321                                                         user, domain,
322                                                         workstation,
323                                                         state->request.data.auth_crap.chal, 
324                                                         lm_resp, nt_resp, 
325                                                         &info3);
326
327                 attempts += 1;
328
329                 /* if we get access denied, a possible cuase was that we had and open
330                    connection to the DC, but someone changed our machine accoutn password
331                    out from underneath us using 'net rpc changetrustpw' */
332                    
333                 if ( NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED) ) {
334                         DEBUG(3,("winbindd_pam_auth_crap: sam_logon returned ACCESS_DENIED.  Maybe the trust account "
335                                 "password was changed and we didn't know it.  Killing connections to domain %s\n",
336                                 domain));
337                         winbindd_cm_flush();
338                         cli->fd = -1;
339                 } 
340                 
341                 /* We have to try a second time as cm_get_netlogon_cli
342                    might not yet have noticed that the DC has killed
343                    our connection. */
344
345         } while ( (attempts < 2) && (cli->fd == -1) );
346
347         clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &ret_creds);
348         
349         if (NT_STATUS_IS_OK(result)) {
350                 netsamlogon_cache_store( cli->mem_ctx, &info3 );
351                 wcache_invalidate_samlogon(find_domain_from_name(domain), &info3);
352                 
353                 if (state->request.flags & WBFLAG_PAM_INFO3_NDR) {
354                         result = append_info3_as_ndr(mem_ctx, state, &info3);
355                 }
356                 
357                 if (state->request.flags & WBFLAG_PAM_NTKEY) {
358                         memcpy(state->response.data.auth.nt_session_key, info3.user_sess_key, sizeof(state->response.data.auth.nt_session_key) /* 16 */);
359                 }
360                 if (state->request.flags & WBFLAG_PAM_LMKEY) {
361                         memcpy(state->response.data.auth.first_8_lm_hash, info3.padding, sizeof(state->response.data.auth.first_8_lm_hash) /* 8 */);
362                 }
363         }
364
365 done:
366
367         /* give us a more useful (more correct?) error code */
368         if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) || (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
369                 result = NT_STATUS_NO_LOGON_SERVERS;
370         }
371         
372         state->response.data.auth.nt_status = NT_STATUS_V(result);
373         push_utf8_fstring(state->response.data.auth.nt_status_string, nt_errstr(result));
374         if (!*state->response.data.auth.error_string) 
375                 push_utf8_fstring(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
376         state->response.data.auth.pam_error = nt_status_to_pam(result);
377
378         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, 
379               ("NTLM CRAP authentication for user [%s]\\[%s] returned %s (PAM: %d)\n", 
380                domain,
381                user,
382                state->response.data.auth.nt_status_string,
383                state->response.data.auth.pam_error));         
384
385         if (mem_ctx) 
386                 talloc_destroy(mem_ctx);
387         
388         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
389 }
390
391 /* Change a user password */
392
393 enum winbindd_result winbindd_pam_chauthtok(struct winbindd_cli_state *state)
394 {
395         NTSTATUS result;
396         char *oldpass, *newpass;
397         fstring domain, user;
398         CLI_POLICY_HND *hnd;
399
400         DEBUG(3, ("[%5lu]: pam chauthtok %s\n", (unsigned long)state->pid,
401                 state->request.data.chauthtok.user));
402
403         /* Setup crap */
404
405         if (state == NULL)
406                 return WINBINDD_ERROR;
407
408         parse_domain_user(state->request.data.chauthtok.user, domain, user);
409         if ( !*domain ) {
410                 result = NT_STATUS_INVALID_PARAMETER;
411                 goto done;
412         }
413
414         /* Change password */
415
416         oldpass = state->request.data.chauthtok.oldpass;
417         newpass = state->request.data.chauthtok.newpass;
418
419         /* Get sam handle */
420
421         if ( NT_STATUS_IS_ERR(result = cm_get_sam_handle(domain, &hnd)) ) {
422                 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
423                 goto done;
424         }
425
426         if (!cli_oem_change_password(hnd->cli, user, newpass, oldpass)) {
427                 DEBUG(1, ("password change failed for user %s/%s\n", domain, 
428                           user));
429                 result = NT_STATUS_WRONG_PASSWORD;
430         } else {
431                 result = NT_STATUS_OK;
432         }
433
434 done:    
435         state->response.data.auth.nt_status = NT_STATUS_V(result);
436         fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
437         fstrcpy(state->response.data.auth.error_string, nt_errstr(result));
438         state->response.data.auth.pam_error = nt_status_to_pam(result);
439
440         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, 
441               ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n", 
442                domain,
443                user,
444                state->response.data.auth.nt_status_string,
445                state->response.data.auth.pam_error));         
446
447         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
448 }