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