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