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