Winbind cleanup.
[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 "winbindd.h"
26
27 /* Return a password structure from a username.  */
28
29 enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state) 
30 {
31         NTSTATUS result;
32         fstring name_domain, name_user;
33         int passlen;
34         unsigned char trust_passwd[16];
35         time_t last_change_time;
36         uint32 smb_uid_low;
37         NET_USER_INFO_3 info3;
38         struct cli_state *cli = NULL;
39         uchar chal[8];
40         TALLOC_CTX *mem_ctx = NULL;
41         DATA_BLOB lm_resp;
42         DATA_BLOB nt_resp;
43
44         extern pstring global_myname;
45
46         DEBUG(3, ("[%5d]: pam auth %s\n", state->pid,
47                   state->request.data.auth.user));
48
49         if (!(mem_ctx = talloc_init_named("winbind pam auth for %s", state->request.data.auth.user))) {
50                 DEBUG(0, ("winbindd_pam_auth: could not talloc_init()!\n"));
51                 result = NT_STATUS_NO_MEMORY;
52                 goto done;
53         }
54
55         /* Parse domain and username */
56         
57         if (!parse_domain_user(state->request.data.auth.user, name_domain, 
58                                name_user)) {
59                 DEBUG(5,("no domain seperator (%s) in username (%s) - failing auth\n", lp_winbind_separator(), state->request.data.auth.user));
60                 result = NT_STATUS_INVALID_PARAMETER;
61                 goto done;
62         }
63
64         passlen = strlen(state->request.data.auth.pass);
65                 
66         {
67                 unsigned char local_lm_response[24];
68                 unsigned char local_nt_response[24];
69                 
70                 generate_random_buffer(chal, 8, False);
71                 SMBencrypt( (const uchar *)state->request.data.auth.pass, chal, local_lm_response);
72                 
73                 SMBNTencrypt((const uchar *)state->request.data.auth.pass, chal, local_nt_response);
74
75                 lm_resp = data_blob_talloc(mem_ctx, local_lm_response, sizeof(local_lm_response));
76                 nt_resp = data_blob_talloc(mem_ctx, local_nt_response, sizeof(local_nt_response));
77         }
78         
79         /*
80          * Get the machine account password for our primary domain
81          */
82
83         if (!secrets_fetch_trust_account_password(
84                 lp_workgroup(), trust_passwd, &last_change_time)) {
85                 DEBUG(0, ("winbindd_pam_auth: could not fetch trust account "
86                           "password for domain %s\n", lp_workgroup()));
87                 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
88                 goto done;
89         }
90
91         /* We really don't care what LUID we give the user. */
92
93         generate_random_buffer( (unsigned char *)&smb_uid_low, 4, False);
94
95         ZERO_STRUCT(info3);
96         
97         /* Don't shut this down - it belongs to the connection cache code */
98         result = cm_get_netlogon_cli(lp_workgroup(), trust_passwd, &cli);
99
100         if (!NT_STATUS_IS_OK(result)) {
101                 DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
102                 goto done;
103         }
104
105         result = cli_netlogon_sam_network_logon(cli, mem_ctx,
106                                                 name_user, name_domain, 
107                                                 global_myname, chal, 
108                                                 lm_resp, nt_resp, 
109                                                 &info3);
110         
111         uni_group_cache_store_netlogon(mem_ctx, &info3);
112 done:
113
114         state->response.data.auth.nt_status = NT_STATUS_V(result);
115         fstrcpy(state->response.data.auth.nt_status_string, get_nt_error_msg(result));
116         fstrcpy(state->response.data.auth.error_string, get_nt_error_msg(result));
117         state->response.data.auth.pam_error = nt_status_to_pam(result);
118
119         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authenticaion for user %s returned %s (PAM: %d)\n", 
120               state->request.data.auth.user, 
121               state->response.data.auth.nt_status_string,
122               state->response.data.auth.pam_error));          
123
124         if (mem_ctx) 
125                 talloc_destroy(mem_ctx);
126         
127         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
128 }
129         
130 /* Challenge Response Authentication Protocol */
131
132 enum winbindd_result winbindd_pam_auth_crap(struct winbindd_cli_state *state) 
133 {
134         NTSTATUS result;
135         unsigned char trust_passwd[16];
136         time_t last_change_time;
137         NET_USER_INFO_3 info3;
138         struct cli_state *cli = NULL;
139         TALLOC_CTX *mem_ctx = NULL;
140         const char *domain = NULL;
141
142         DATA_BLOB lm_resp, nt_resp;
143
144         extern pstring global_myname;
145
146         DEBUG(3, ("[%5d]: pam auth crap domain: %s user: %s\n", state->pid,
147                   state->request.data.auth_crap.domain, state->request.data.auth_crap.user));
148
149         if (!(mem_ctx = talloc_init_named("winbind pam auth crap for %s", state->request.data.auth.user))) {
150                 DEBUG(0, ("winbindd_pam_auth_crap: could not talloc_init()!\n"));
151                 result = NT_STATUS_NO_MEMORY;
152                 goto done;
153         }
154
155         if (*state->request.data.auth_crap.domain) {
156                 domain = talloc_strdup(mem_ctx, state->request.data.auth_crap.domain);
157         } else if (lp_winbind_use_default_domain()) {
158                 domain = talloc_strdup(mem_ctx, lp_workgroup());
159         } else {
160                 DEBUG(5,("no domain specified with username (%s) - failing auth\n", state->request.data.auth.user));
161                 result = NT_STATUS_INVALID_PARAMETER;
162                 goto done;
163         }
164
165         if (!domain) {
166                 DEBUG(0,("winbindd_pam_auth_crap: talloc_strdup failed!\n"));
167                 result = NT_STATUS_NO_MEMORY;
168                 goto done;
169         }
170
171         lm_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.lm_resp, state->request.data.auth_crap.lm_resp_len);
172         nt_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.nt_resp, state->request.data.auth_crap.nt_resp_len);
173         
174         /*
175          * Get the machine account password for our primary domain
176          */
177
178         if (!secrets_fetch_trust_account_password(
179                 lp_workgroup(), trust_passwd, &last_change_time)) {
180                 DEBUG(0, ("winbindd_pam_auth: could not fetch trust account "
181                           "password for domain %s\n", lp_workgroup()));
182                 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
183                 goto done;
184         }
185
186         ZERO_STRUCT(info3);
187
188         /* Don't shut this down - it belongs to the connection cache code */
189         result = cm_get_netlogon_cli(lp_workgroup(), trust_passwd, &cli);
190
191         if (!NT_STATUS_IS_OK(result)) {
192                 DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
193                 goto done;
194         }
195
196         result = cli_netlogon_sam_network_logon(cli, mem_ctx,
197                                                 state->request.data.auth_crap.user, domain,
198                                                 global_myname, state->request.data.auth_crap.chal, 
199                                                 lm_resp, nt_resp, 
200                                                 &info3);
201         
202         if (NT_STATUS_IS_OK(result)) {
203                 uni_group_cache_store_netlogon(mem_ctx, &info3);
204         }
205
206 done:
207
208         state->response.data.auth.nt_status = NT_STATUS_V(result);
209         fstrcpy(state->response.data.auth.nt_status_string, get_nt_error_msg(result));
210         fstrcpy(state->response.data.auth.error_string, get_nt_error_msg(result));
211         state->response.data.auth.pam_error = nt_status_to_pam(result);
212
213         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("NTLM CRAP authenticaion for user [%s]\\[%s] returned %s (PAM: %d)\n", 
214               state->request.data.auth_crap.domain, 
215               state->request.data.auth_crap.user, 
216               state->response.data.auth.nt_status_string,
217               state->response.data.auth.pam_error));          
218
219         if (mem_ctx) 
220                 talloc_destroy(mem_ctx);
221         
222         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
223 }
224
225 /* Change a user password */
226
227 enum winbindd_result winbindd_pam_chauthtok(struct winbindd_cli_state *state)
228 {
229         NTSTATUS result;
230         char *oldpass, *newpass;
231         fstring domain, user;
232         CLI_POLICY_HND *hnd;
233
234         DEBUG(3, ("[%5d]: pam chauthtok %s\n", state->pid,
235                 state->request.data.chauthtok.user));
236
237         /* Setup crap */
238
239         if (state == NULL)
240                 return WINBINDD_ERROR;
241
242         if (!parse_domain_user(state->request.data.chauthtok.user, domain, 
243                                user)) {
244                 result = NT_STATUS_INVALID_PARAMETER;
245                 goto done;
246         }
247
248         /* Change password */
249
250         oldpass = state->request.data.chauthtok.oldpass;
251         newpass = state->request.data.chauthtok.newpass;
252
253         /* Get sam handle */
254
255         if (!(hnd = cm_get_sam_handle(domain))) {
256                 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
257                 result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
258                 goto done;
259         }
260
261         if (!cli_oem_change_password(hnd->cli, user, newpass, oldpass)) {
262                 DEBUG(1, ("password change failed for user %s/%s\n", domain, 
263                           user));
264                 result = NT_STATUS_WRONG_PASSWORD;
265         } else {
266                 result = NT_STATUS_OK;
267         }
268
269 done:    
270         state->response.data.auth.nt_status = NT_STATUS_V(result);
271         fstrcpy(state->response.data.auth.nt_status_string, get_nt_error_msg(result));
272         fstrcpy(state->response.data.auth.error_string, get_nt_error_msg(result));
273         state->response.data.auth.pam_error = nt_status_to_pam(result);
274
275         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
276 }