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