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