Updates to winbind's PAM client and server - make the debug logs
[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 = memdup(prs_data_p(&ps), size);
46         if (!state->response.extra_data) {
47                 prs_mem_free(&ps);
48                 return NT_STATUS_NO_MEMORY;
49         }
50         state->response.length += size;
51         prs_mem_free(&ps);
52         return NT_STATUS_OK;
53 }
54
55 /* Return a password structure from a username.  */
56
57 enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state) 
58 {
59         NTSTATUS result;
60         fstring name_domain, name_user;
61         unsigned char trust_passwd[16];
62         time_t last_change_time;
63         uint32 smb_uid_low;
64         NET_USER_INFO_3 info3;
65         struct cli_state *cli = NULL;
66         uchar chal[8];
67         TALLOC_CTX *mem_ctx = NULL;
68         DATA_BLOB lm_resp;
69         DATA_BLOB nt_resp;
70
71         extern pstring global_myname;
72
73         /* Ensure null termination */
74         state->request.data.auth.user[sizeof(state->request.data.auth.user)-1]='\0';
75
76         /* Ensure null termination */
77         state->request.data.auth.pass[sizeof(state->request.data.auth.pass)-1]='\0';
78
79         DEBUG(3, ("[%5d]: pam auth %s\n", state->pid,
80                   state->request.data.auth.user));
81
82         if (!(mem_ctx = talloc_init_named("winbind pam auth for %s", state->request.data.auth.user))) {
83                 DEBUG(0, ("winbindd_pam_auth: could not talloc_init()!\n"));
84                 result = NT_STATUS_NO_MEMORY;
85                 goto done;
86         }
87
88         /* Parse domain and username */
89         
90         if (!parse_domain_user(state->request.data.auth.user, name_domain, 
91                                name_user)) {
92                 DEBUG(5,("no domain separator (%s) in username (%s) - failing auth\n", lp_winbind_separator(), state->request.data.auth.user));
93                 result = NT_STATUS_INVALID_PARAMETER;
94                 goto done;
95         }
96
97         {
98                 unsigned char local_lm_response[24];
99                 unsigned char local_nt_response[24];
100                 
101                 generate_random_buffer(chal, 8, False);
102                 SMBencrypt(state->request.data.auth.pass, chal, local_lm_response);
103                 
104                 SMBNTencrypt(state->request.data.auth.pass, chal, local_nt_response);
105
106                 lm_resp = data_blob_talloc(mem_ctx, local_lm_response, sizeof(local_lm_response));
107                 nt_resp = data_blob_talloc(mem_ctx, local_nt_response, sizeof(local_nt_response));
108         }
109         
110         /*
111          * Get the machine account password for our primary domain
112          */
113
114         if (!secrets_fetch_trust_account_password(
115                 lp_workgroup(), trust_passwd, &last_change_time)) {
116                 DEBUG(0, ("winbindd_pam_auth: could not fetch trust account "
117                           "password for domain %s\n", lp_workgroup()));
118                 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
119                 goto done;
120         }
121
122         /* We really don't care what LUID we give the user. */
123
124         generate_random_buffer( (unsigned char *)&smb_uid_low, 4, False);
125
126         ZERO_STRUCT(info3);
127         
128         /* Don't shut this down - it belongs to the connection cache code */
129         result = cm_get_netlogon_cli(lp_workgroup(), trust_passwd, &cli);
130
131         if (!NT_STATUS_IS_OK(result)) {
132                 DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
133                 goto done;
134         }
135
136         result = cli_netlogon_sam_network_logon(cli, mem_ctx,
137                                                 name_user, name_domain, 
138                                                 global_myname, chal, 
139                                                 lm_resp, nt_resp, 
140                                                 &info3);
141         
142         uni_group_cache_store_netlogon(mem_ctx, &info3);
143 done:
144
145         state->response.data.auth.nt_status = NT_STATUS_V(result);
146         fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
147         fstrcpy(state->response.data.auth.error_string, nt_errstr(result));
148         state->response.data.auth.pam_error = nt_status_to_pam(result);
149
150         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n", 
151               state->request.data.auth.user, 
152               state->response.data.auth.nt_status_string,
153               state->response.data.auth.pam_error));          
154
155         if (mem_ctx) 
156                 talloc_destroy(mem_ctx);
157         
158         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
159 }
160         
161 /* Challenge Response Authentication Protocol */
162
163 enum winbindd_result winbindd_pam_auth_crap(struct winbindd_cli_state *state) 
164 {
165         NTSTATUS result;
166         unsigned char trust_passwd[16];
167         time_t last_change_time;
168         NET_USER_INFO_3 info3;
169         struct cli_state *cli = NULL;
170         TALLOC_CTX *mem_ctx = NULL;
171         char *user = NULL;
172         char *domain = NULL;
173         char *contact_domain;
174         char *workstation;
175
176         DATA_BLOB lm_resp, nt_resp;
177
178         extern pstring global_myname;
179
180         /* Ensure null termination */
181         state->request.data.auth_crap.user[sizeof(state->request.data.auth_crap.user)-1]='\0';
182
183         /* Ensure null termination */
184         state->request.data.auth_crap.domain[sizeof(state->request.data.auth_crap.domain)-1]='\0';
185
186         if (!(mem_ctx = talloc_init_named("winbind pam auth crap for (utf8) %s", state->request.data.auth_crap.user))) {
187                 DEBUG(0, ("winbindd_pam_auth_crap: could not talloc_init()!\n"));
188                 result = NT_STATUS_NO_MEMORY;
189                 goto done;
190         }
191
192         if (pull_utf8_talloc(mem_ctx, &user, state->request.data.auth_crap.user) < 0) {
193                 DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
194         }
195
196         if (*state->request.data.auth_crap.domain) {
197                 if (pull_utf8_talloc(mem_ctx, &domain, state->request.data.auth_crap.domain) < 0) {
198                         DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
199                 }
200         } else if (lp_winbind_use_default_domain()) {
201                 domain = lp_workgroup();
202         } else {
203                 DEBUG(5,("no domain specified with username (%s) - failing auth\n", 
204                          user));
205                 result = NT_STATUS_INVALID_PARAMETER;
206                 goto done;
207         }
208
209         DEBUG(3, ("[%5d]: pam auth crap domain: %s user: %s\n", state->pid,
210                   domain, user));
211
212         if (lp_allow_trusted_domains() && (state->request.data.auth_crap.flags & WINBIND_PAM_CONTACT_TRUSTDOM)) {
213                 contact_domain = domain;
214         } else {
215                 contact_domain = lp_workgroup();
216         }
217
218         if (*state->request.data.auth_crap.workstation) {
219                 if (pull_utf8_talloc(mem_ctx, &workstation, state->request.data.auth_crap.workstation) < 0) {
220                         DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
221                 }
222         } else {
223                 workstation = global_myname;
224         }
225
226         if (state->request.data.auth_crap.lm_resp_len > sizeof(state->request.data.auth_crap.lm_resp)
227                 || state->request.data.auth_crap.nt_resp_len > sizeof(state->request.data.auth_crap.nt_resp)) {
228                 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n", 
229                           state->request.data.auth_crap.lm_resp_len, 
230                           state->request.data.auth_crap.nt_resp_len));
231                 result = NT_STATUS_INVALID_PARAMETER;
232                 goto done;
233         }
234
235         lm_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.lm_resp, state->request.data.auth_crap.lm_resp_len);
236         nt_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.nt_resp, state->request.data.auth_crap.nt_resp_len);
237         
238         /*
239          * Get the machine account password for the domain to contact.
240          * This is either our own domain for a workstation, or possibly
241          * any domain for a PDC with trusted domains.
242          */
243
244         if (!secrets_fetch_trust_account_password (
245                 contact_domain, trust_passwd, &last_change_time)) {
246                 DEBUG(0, ("winbindd_pam_auth: could not fetch trust account "
247                           "password for domain %s\n", contact_domain));
248                 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
249                 goto done;
250         }
251
252         ZERO_STRUCT(info3);
253
254         /* Don't shut this down - it belongs to the connection cache code */
255         result = cm_get_netlogon_cli(contact_domain, trust_passwd, &cli);
256
257         if (!NT_STATUS_IS_OK(result)) {
258                 DEBUG(3, ("could not open handle to NETLOGON pipe (error: %s)\n", nt_errstr(result)));
259                 goto done;
260         }
261
262         result = cli_netlogon_sam_network_logon(cli, mem_ctx,
263                                                 user, domain,
264                                                 workstation, state->request.data.auth_crap.chal, 
265                                                 lm_resp, nt_resp, 
266                                                 &info3);
267         
268         if (NT_STATUS_IS_OK(result)) {
269                 uni_group_cache_store_netlogon(mem_ctx, &info3);
270                 if (state->request.data.auth_crap.flags & WINBIND_PAM_INFO3_NDR) {
271                         result = append_info3_as_ndr(mem_ctx, state, &info3);
272                 }
273
274 #if 0
275                 /* we don't currently do this stuff right */
276                 if (state->request.data.auth_crap.flags & WINBIND_PAM_NTKEY) {
277                         SMB_ASSERT(sizeof(state->response.data.auth.nt_session_key) == sizeof(info3.user_sess_key)); 
278                         memcpy(state->response.data.auth.nt_session_key, info3.user_sess_key, sizeof(state->response.data.auth.nt_session_key) /* 16 */);
279                 }
280                 if (state->request.data.auth_crap.flags & WINBIND_PAM_LMKEY) {
281                         SMB_ASSERT(sizeof(state->response.data.auth.nt_session_key) <= sizeof(info3.user_sess_key)); 
282                         memcpy(state->response.data.auth.first_8_lm_hash, info3.padding, sizeof(state->response.data.auth.nt_session_key) /* 16 */);
283                 }
284 #endif
285         }
286
287 done:
288
289         state->response.data.auth.nt_status = NT_STATUS_V(result);
290         push_utf8_fstring(state->response.data.auth.nt_status_string, nt_errstr(result));
291         push_utf8_fstring(state->response.data.auth.error_string, nt_errstr(result));
292         state->response.data.auth.pam_error = nt_status_to_pam(result);
293
294         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, 
295               ("NTLM CRAP authentication for user [%s]\\[%s] returned %s (PAM: %d)\n", 
296                domain,
297                user,
298                state->response.data.auth.nt_status_string,
299                state->response.data.auth.pam_error));         
300
301         if (mem_ctx) 
302                 talloc_destroy(mem_ctx);
303         
304         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
305 }
306
307 /* Change a user password */
308
309 enum winbindd_result winbindd_pam_chauthtok(struct winbindd_cli_state *state)
310 {
311         NTSTATUS result;
312         char *oldpass, *newpass;
313         fstring domain, user;
314         CLI_POLICY_HND *hnd;
315
316         DEBUG(3, ("[%5d]: pam chauthtok %s\n", state->pid,
317                 state->request.data.chauthtok.user));
318
319         /* Setup crap */
320
321         if (state == NULL)
322                 return WINBINDD_ERROR;
323
324         if (!parse_domain_user(state->request.data.chauthtok.user, domain, 
325                                user)) {
326                 result = NT_STATUS_INVALID_PARAMETER;
327                 goto done;
328         }
329
330         /* Change password */
331
332         oldpass = state->request.data.chauthtok.oldpass;
333         newpass = state->request.data.chauthtok.newpass;
334
335         /* Get sam handle */
336
337         if (!(hnd = cm_get_sam_handle(domain))) {
338                 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
339                 result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
340                 goto done;
341         }
342
343         if (!cli_oem_change_password(hnd->cli, user, newpass, oldpass)) {
344                 DEBUG(1, ("password change failed for user %s/%s\n", domain, 
345                           user));
346                 result = NT_STATUS_WRONG_PASSWORD;
347         } else {
348                 result = NT_STATUS_OK;
349         }
350
351 done:    
352         state->response.data.auth.nt_status = NT_STATUS_V(result);
353         fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
354         fstrcpy(state->response.data.auth.error_string, nt_errstr(result));
355         state->response.data.auth.pam_error = nt_status_to_pam(result);
356
357         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, 
358               ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n", 
359                domain,
360                user,
361                state->response.data.auth.nt_status_string,
362                state->response.data.auth.pam_error));         
363
364         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
365 }