Merge winbind from Samba 3.0 onto HEAD.
[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         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 /**********************************************************************
58  Authenticate a user with a clear test password
59 **********************************************************************/
60
61 enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state) 
62 {
63         NTSTATUS result;
64         fstring name_domain, name_user;
65         unsigned char trust_passwd[16];
66         time_t last_change_time;
67         uint32 sec_channel_type;
68         NET_USER_INFO_3 info3;
69         struct cli_state *cli = NULL;
70         uchar chal[8];
71         TALLOC_CTX *mem_ctx = NULL;
72         DATA_BLOB lm_resp;
73         DATA_BLOB nt_resp;
74         DOM_CRED ret_creds;
75         int attempts = 0;
76         unsigned char local_lm_response[24];
77         unsigned char local_nt_response[24];
78         struct winbindd_domain *contact_domain;
79         BOOL retry;
80
81         /* Ensure null termination */
82         state->request.data.auth.user[sizeof(state->request.data.auth.user)-1]='\0';
83
84         /* Ensure null termination */
85         state->request.data.auth.pass[sizeof(state->request.data.auth.pass)-1]='\0';
86
87         DEBUG(3, ("[%5lu]: pam auth %s\n", (unsigned long)state->pid,
88                   state->request.data.auth.user));
89
90         if (!(mem_ctx = talloc_init("winbind pam auth for %s", state->request.data.auth.user))) {
91                 DEBUG(0, ("winbindd_pam_auth: could not talloc_init()!\n"));
92                 result = NT_STATUS_NO_MEMORY;
93                 goto done;
94         }
95
96         /* Parse domain and username */
97         
98         parse_domain_user(state->request.data.auth.user, name_domain, name_user);
99
100         /* do password magic */
101         
102         generate_random_buffer(chal, 8, False);
103         SMBencrypt(state->request.data.auth.pass, chal, local_lm_response);
104                 
105         SMBNTencrypt(state->request.data.auth.pass, chal, local_nt_response);
106
107         lm_resp = data_blob_talloc(mem_ctx, local_lm_response, sizeof(local_lm_response));
108         nt_resp = data_blob_talloc(mem_ctx, local_nt_response, sizeof(local_nt_response));
109         
110         /* what domain should we contact? */
111         
112         if ( IS_DC ) {
113                 if (!(contact_domain = find_domain_from_name(name_domain))) {
114                         DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n", 
115                                   state->request.data.auth.user, name_domain, name_user, name_domain)); 
116                         result = NT_STATUS_NO_SUCH_USER;
117                         goto done;
118                 }
119                 
120         } else {
121                 if (is_myname(name_domain)) {
122                         DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
123                         result =  NT_STATUS_NO_SUCH_USER;
124                         goto done;
125                 }
126
127                 if (!(contact_domain = find_our_domain())) {
128                         DEBUG(1, ("Authenticatoin for [%s] -> [%s]\\[%s] in our domain failed - we can't find our domain!\n", 
129                                   state->request.data.auth.user, name_domain, name_user)); 
130                         result = NT_STATUS_NO_SUCH_USER;
131                         goto done;
132                 }
133         }
134
135         if ( !get_trust_pw(contact_domain->name, trust_passwd, &last_change_time, &sec_channel_type) ) {
136                 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
137                 goto done;
138         }
139
140         /* check authentication loop */
141
142         do {
143                 ZERO_STRUCT(info3);
144                 ZERO_STRUCT(ret_creds);
145                 retry = False;
146         
147                 /* Don't shut this down - it belongs to the connection cache code */
148                 result = cm_get_netlogon_cli(contact_domain, trust_passwd, 
149                                              sec_channel_type, False, &cli);
150
151                 if (!NT_STATUS_IS_OK(result)) {
152                         DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
153                         goto done;
154                 }
155
156                 result = cli_netlogon_sam_network_logon(cli, mem_ctx,
157                                                         &ret_creds,
158                                                         name_user, name_domain, 
159                                                         global_myname(), chal, 
160                                                         lm_resp, nt_resp,
161                                                         &info3);
162                 attempts += 1;
163                 
164                 /* We have to try a second time as cm_get_netlogon_cli
165                    might not yet have noticed that the DC has killed
166                    our connection. */
167
168                 if ( cli->fd == -1 ) {
169                         retry = True;
170                         continue;
171                 } 
172                 
173                 /* if we get access denied, a possible cuase was that we had and open
174                    connection to the DC, but someone changed our machine account password
175                    out from underneath us using 'net rpc changetrustpw' */
176                    
177                 if ( NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED) ) {
178                         DEBUG(3,("winbindd_pam_auth: sam_logon returned ACCESS_DENIED.  Maybe the trust account "
179                                 "password was changed and we didn't know it.  Killing connections to domain %s\n",
180                                 name_domain));
181                         winbindd_cm_flush();
182                         retry = True;
183                         cli = NULL;
184                 } 
185                 
186         } while ( (attempts < 2) && retry );
187         
188         clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &ret_creds);
189         
190         if (NT_STATUS_IS_OK(result)) {
191                 netsamlogon_cache_store( cli->mem_ctx, &info3 );
192                 wcache_invalidate_samlogon(find_domain_from_name(name_domain), &info3);
193         }
194    
195 done:
196         /* give us a more useful (more correct?) error code */
197         if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) || (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
198                 result = NT_STATUS_NO_LOGON_SERVERS;
199         }
200         
201         state->response.data.auth.nt_status = NT_STATUS_V(result);
202         fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
203
204         /* we might have given a more useful error above */
205         if (!*state->response.data.auth.error_string) 
206                 fstrcpy(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
207         state->response.data.auth.pam_error = nt_status_to_pam(result);
208
209         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n", 
210               state->request.data.auth.user, 
211               state->response.data.auth.nt_status_string,
212               state->response.data.auth.pam_error));          
213
214         if (mem_ctx) 
215                 talloc_destroy(mem_ctx);
216         
217         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
218 }
219
220 /**********************************************************************
221  Challenge Response Authentication Protocol 
222 **********************************************************************/
223
224 enum winbindd_result winbindd_pam_auth_crap(struct winbindd_cli_state *state) 
225 {
226         NTSTATUS result;
227         unsigned char trust_passwd[16];
228         time_t last_change_time;
229         uint32 sec_channel_type;
230         NET_USER_INFO_3 info3;
231         struct cli_state *cli = NULL;
232         TALLOC_CTX *mem_ctx = NULL;
233         char *name_user = NULL;
234         const char *name_domain = NULL;
235         const char *workstation;
236         struct winbindd_domain *contact_domain;
237         DOM_CRED ret_creds;
238         int attempts = 0;
239         BOOL retry;
240
241         DATA_BLOB lm_resp, nt_resp;
242
243         if (!state->privileged) {
244                 char *error_string = NULL;
245                 DEBUG(2, ("winbindd_pam_auth_crap: non-privileged access denied.  !\n"));
246                 DEBUGADD(2, ("winbindd_pam_auth_crap: Ensure permissions on %s are set correctly.\n", 
247                              get_winbind_priv_pipe_dir()));
248                 /* send a better message than ACCESS_DENIED */
249                 asprintf(&error_string, "winbind client not authorized to use winbindd_pam_auth_crap.  Ensure permissions on %s are set correctly.",
250                          get_winbind_priv_pipe_dir());
251                 push_utf8_fstring(state->response.data.auth.error_string, error_string);
252                 SAFE_FREE(error_string);
253                 result =  NT_STATUS_ACCESS_DENIED;
254                 goto done;
255         }
256
257         /* Ensure null termination */
258         state->request.data.auth_crap.user[sizeof(state->request.data.auth_crap.user)-1]=0;
259         state->request.data.auth_crap.domain[sizeof(state->request.data.auth_crap.domain)-1]=0;
260
261         if (!(mem_ctx = talloc_init("winbind pam auth crap for (utf8) %s", state->request.data.auth_crap.user))) {
262                 DEBUG(0, ("winbindd_pam_auth_crap: could not talloc_init()!\n"));
263                 result = NT_STATUS_NO_MEMORY;
264                 goto done;
265         }
266
267         if (pull_utf8_talloc(mem_ctx, &name_user, state->request.data.auth_crap.user) == (size_t)-1) {
268                 DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
269                 result = NT_STATUS_UNSUCCESSFUL;
270                 goto done;
271         }
272
273         if (*state->request.data.auth_crap.domain) {
274                 char *dom = NULL;
275                 if (pull_utf8_talloc(mem_ctx, &dom, state->request.data.auth_crap.domain) == (size_t)-1) {
276                         DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
277                         result = NT_STATUS_UNSUCCESSFUL;
278                         goto done;
279                 }
280                 name_domain = dom;
281         } else if (lp_winbind_use_default_domain()) {
282                 name_domain = lp_workgroup();
283         } else {
284                 DEBUG(5,("no domain specified with username (%s) - failing auth\n", 
285                          name_user));
286                 result = NT_STATUS_NO_SUCH_USER;
287                 goto done;
288         }
289
290         DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state->pid,
291                   name_domain, name_user));
292            
293         if (*state->request.data.auth_crap.workstation) {
294                 char *wrk = NULL;
295                 if (pull_utf8_talloc(mem_ctx, &wrk, state->request.data.auth_crap.workstation) == (size_t)-1) {
296                         DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
297                         result = NT_STATUS_UNSUCCESSFUL;
298                         goto done;
299                 }
300                 workstation = wrk;
301         } else {
302                 workstation = global_myname();
303         }
304
305         if (state->request.data.auth_crap.lm_resp_len > sizeof(state->request.data.auth_crap.lm_resp)
306                 || state->request.data.auth_crap.nt_resp_len > sizeof(state->request.data.auth_crap.nt_resp)) {
307                 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n", 
308                           state->request.data.auth_crap.lm_resp_len, 
309                           state->request.data.auth_crap.nt_resp_len));
310                 result = NT_STATUS_INVALID_PARAMETER;
311                 goto done;
312         }
313
314         lm_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.lm_resp, state->request.data.auth_crap.lm_resp_len);
315         nt_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.nt_resp, state->request.data.auth_crap.nt_resp_len);
316         
317         /* what domain should we contact? */
318         
319
320         /* what domain should we contact? */
321         
322         if ( IS_DC ) {
323                 if (!(contact_domain = find_domain_from_name(name_domain))) {
324                         DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n", 
325                                   state->request.data.auth.user, name_domain, name_user, name_domain)); 
326                         result = NT_STATUS_NO_SUCH_USER;
327                         goto done;
328                 }
329                 
330         } else {
331                 if (is_myname(name_domain)) {
332                         DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
333                         result =  NT_STATUS_NO_SUCH_USER;
334                         goto done;
335                 }
336
337                 if (!(contact_domain = find_our_domain())) {
338                         DEBUG(1, ("Authenticatoin for [%s] -> [%s]\\[%s] in our domain failed - we can't find our domain!\n", 
339                                   state->request.data.auth.user, name_domain, name_user)); 
340                         result = NT_STATUS_NO_SUCH_USER;
341                         goto done;
342                 }
343         }
344                 
345         if ( !get_trust_pw(contact_domain->name, trust_passwd, &last_change_time, &sec_channel_type) ) {
346                 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
347                 goto done;
348         }
349
350         do {
351                 ZERO_STRUCT(info3);
352                 ZERO_STRUCT(ret_creds);
353                 retry = False;
354
355                 /* Don't shut this down - it belongs to the connection cache code */
356                 result = cm_get_netlogon_cli(contact_domain, trust_passwd, sec_channel_type, False, &cli);
357
358                 if (!NT_STATUS_IS_OK(result)) {
359                         DEBUG(3, ("could not open handle to NETLOGON pipe (error: %s)\n",
360                                   nt_errstr(result)));
361                         goto done;
362                 }
363
364                 result = cli_netlogon_sam_network_logon(cli, mem_ctx,
365                                                         &ret_creds,
366                                                         name_user, name_domain,
367                                                         workstation,
368                                                         state->request.data.auth_crap.chal, 
369                                                         lm_resp, nt_resp, 
370                                                         &info3);
371
372                 attempts += 1;
373
374                 /* We have to try a second time as cm_get_netlogon_cli
375                    might not yet have noticed that the DC has killed
376                    our connection. */
377
378                 if ( cli->fd == -1 ) {
379                         retry = True;
380                         continue;
381                 } 
382
383                 /* if we get access denied, a possible cause was that we had and open
384                    connection to the DC, but someone changed our machine account password
385                    out from underneath us using 'net rpc changetrustpw' */
386                    
387                 if ( NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED) ) {
388                         DEBUG(3,("winbindd_pam_auth_crap: sam_logon returned ACCESS_DENIED.  Maybe the trust account "
389                                 "password was changed and we didn't know it.  Killing connections to domain %s\n",
390                                 contact_domain->name));
391                         winbindd_cm_flush();
392                         retry = True;
393                         cli = NULL;
394                 } 
395                 
396         } while ( (attempts < 2) && retry );
397
398         clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &ret_creds);
399         
400         if (NT_STATUS_IS_OK(result)) {
401                 netsamlogon_cache_store( cli->mem_ctx, &info3 );
402                 wcache_invalidate_samlogon(find_domain_from_name(name_domain), &info3);
403                 
404                 if (state->request.flags & WBFLAG_PAM_INFO3_NDR) {
405                         result = append_info3_as_ndr(mem_ctx, state, &info3);
406                 } else if (state->request.flags & WBFLAG_PAM_UNIX_NAME) {
407                         /* ntlm_auth should return the unix username, per 
408                            'winbind use default domain' settings and the like */
409                         
410                         fstring username_out;
411                         const char *nt_username, *nt_domain;
412                         if (!(nt_username = unistr2_tdup(mem_ctx, &(info3.uni_user_name)))) {
413                                 /* If the server didn't give us one, just use the one we sent them */
414                                 nt_username = name_user;
415                         }
416                         
417                         if (!(nt_domain = unistr2_tdup(mem_ctx, &(info3.uni_logon_dom)))) {
418                                 /* If the server didn't give us one, just use the one we sent them */
419                                 nt_domain = name_domain;
420                         }
421
422                         fill_domain_username(username_out, nt_domain, nt_username);
423
424                         DEBUG(5, ("Setting unix username to [%s]\n", username_out));
425
426                         /* this interface is in UTF8 */
427                         if (push_utf8_allocate((char **)&state->response.extra_data, username_out) == -1) {
428                                 result = NT_STATUS_NO_MEMORY;
429                                 goto done;
430                         }
431                         state->response.length +=  strlen(state->response.extra_data)+1;
432                 }
433                 
434                 if (state->request.flags & WBFLAG_PAM_NTKEY) {
435                         memcpy(state->response.data.auth.nt_session_key, info3.user_sess_key, sizeof(state->response.data.auth.nt_session_key) /* 16 */);
436                 }
437                 if (state->request.flags & WBFLAG_PAM_LMKEY) {
438                         memcpy(state->response.data.auth.first_8_lm_hash, info3.padding, sizeof(state->response.data.auth.first_8_lm_hash) /* 8 */);
439                 }
440         }
441
442 done:
443         /* give us a more useful (more correct?) error code */
444         if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) || (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
445                 result = NT_STATUS_NO_LOGON_SERVERS;
446         }
447         
448         state->response.data.auth.nt_status = NT_STATUS_V(result);
449         push_utf8_fstring(state->response.data.auth.nt_status_string, nt_errstr(result));
450         
451         /* we might have given a more useful error above */
452         if (!*state->response.data.auth.error_string) 
453                 push_utf8_fstring(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
454         state->response.data.auth.pam_error = nt_status_to_pam(result);
455
456         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, 
457               ("NTLM CRAP authentication for user [%s]\\[%s] returned %s (PAM: %d)\n", 
458                name_domain,
459                name_user,
460                state->response.data.auth.nt_status_string,
461                state->response.data.auth.pam_error));         
462
463         if (mem_ctx) 
464                 talloc_destroy(mem_ctx);
465         
466         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
467 }
468
469 /* Change a user password */
470
471 enum winbindd_result winbindd_pam_chauthtok(struct winbindd_cli_state *state)
472 {
473         NTSTATUS result;
474         char *oldpass, *newpass;
475         fstring domain, user;
476         CLI_POLICY_HND *hnd;
477         TALLOC_CTX *mem_ctx;
478         struct winbindd_domain *contact_domain;
479
480         DEBUG(3, ("[%5lu]: pam chauthtok %s\n", (unsigned long)state->pid,
481                 state->request.data.chauthtok.user));
482
483         if (!(mem_ctx = talloc_init("winbind password change for (utf8) %s", 
484                                     state->request.data.chauthtok.user))) {
485                 DEBUG(0, ("winbindd_pam_auth_crap: could not talloc_init()!\n"));
486                 result = NT_STATUS_NO_MEMORY;
487                 goto done;
488         }
489
490         /* Setup crap */
491
492         if (state == NULL)
493                 return WINBINDD_ERROR;
494
495         parse_domain_user(state->request.data.chauthtok.user, domain, user);
496
497         if (!(contact_domain = find_domain_from_name(domain))) {
498                 DEBUG(3, ("Cannot change password for [%s] -> [%s]\\[%s] as %s is not a trusted domain\n", 
499                           state->request.data.chauthtok.user, domain, user, domain)); 
500                 result = NT_STATUS_NO_SUCH_USER;
501                 goto done;
502         }
503
504         /* Change password */
505
506         oldpass = state->request.data.chauthtok.oldpass;
507         newpass = state->request.data.chauthtok.newpass;
508
509         /* Get sam handle */
510
511         if ( NT_STATUS_IS_ERR(result = cm_get_sam_handle(contact_domain, &hnd)) ) {
512                 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
513                 goto done;
514         }
515
516         if (!cli_oem_change_password(hnd->cli, user, newpass, oldpass)) {
517                 DEBUG(1, ("password change failed for user %s/%s\n", domain, 
518                           user));
519                 result = NT_STATUS_WRONG_PASSWORD;
520         } else {
521                 result = NT_STATUS_OK;
522         }
523
524 done:    
525         state->response.data.auth.nt_status = NT_STATUS_V(result);
526         fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
527         fstrcpy(state->response.data.auth.error_string, nt_errstr(result));
528         state->response.data.auth.pam_error = nt_status_to_pam(result);
529
530         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, 
531               ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n", 
532                domain,
533                user,
534                state->response.data.auth.nt_status_string,
535                state->response.data.auth.pam_error));         
536
537         if (mem_ctx)
538                 talloc_destroy(mem_ctx);
539
540         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
541 }