2 Unix SMB/CIFS implementation.
4 Winbind daemon - pam auth funcions
6 Copyright (C) Andrew Tridgell 2000
7 Copyright (C) Tim Potter 2001
8 Copyright (C) Andrew Bartlett 2001-2002
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.
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.
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.
27 #define DBGC_CLASS DBGC_WINBIND
30 static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx,
31 struct winbindd_cli_state *state,
32 NET_USER_INFO_3 *info3)
36 if (!prs_init(&ps, 256 /* Random, non-zero number */, mem_ctx, MARSHALL)) {
37 return NT_STATUS_NO_MEMORY;
39 if (!net_io_user_info3("", info3, &ps, 1, 3)) {
41 return NT_STATUS_UNSUCCESSFUL;
44 size = prs_data_size(&ps);
45 state->response.extra_data = malloc(size);
46 if (!state->response.extra_data) {
48 return NT_STATUS_NO_MEMORY;
50 prs_copy_all_data_out(state->response.extra_data, &ps);
51 state->response.length += size;
56 /* Return a password structure from a username. */
58 enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state)
61 fstring name_domain, name_user;
62 unsigned char trust_passwd[16];
63 time_t last_change_time;
64 uint32 sec_channel_type;
65 NET_USER_INFO_3 info3;
66 struct cli_state *cli = NULL;
68 TALLOC_CTX *mem_ctx = NULL;
74 /* Ensure null termination */
75 state->request.data.auth.user[sizeof(state->request.data.auth.user)-1]='\0';
77 /* Ensure null termination */
78 state->request.data.auth.pass[sizeof(state->request.data.auth.pass)-1]='\0';
80 DEBUG(3, ("[%5d]: pam auth %s\n", state->pid,
81 state->request.data.auth.user));
83 if (!(mem_ctx = talloc_init("winbind pam auth for %s", state->request.data.auth.user))) {
84 DEBUG(0, ("winbindd_pam_auth: could not talloc_init()!\n"));
85 result = NT_STATUS_NO_MEMORY;
89 /* Parse domain and username */
91 if (!parse_domain_user(state->request.data.auth.user, name_domain,
93 DEBUG(5,("no domain separator (%s) in username (%s) - failing auth\n", lp_winbind_separator(), state->request.data.auth.user));
94 result = NT_STATUS_INVALID_PARAMETER;
99 unsigned char local_lm_response[24];
100 unsigned char local_nt_response[24];
102 generate_random_buffer(chal, 8, False);
103 SMBencrypt(state->request.data.auth.pass, chal, local_lm_response);
105 SMBNTencrypt(state->request.data.auth.pass, chal, local_nt_response);
107 lm_resp = data_blob_talloc(mem_ctx, local_lm_response, sizeof(local_lm_response));
108 nt_resp = data_blob_talloc(mem_ctx, local_nt_response, sizeof(local_nt_response));
112 * Get the machine account password for our primary domain
115 if (!secrets_fetch_trust_account_password(
116 lp_workgroup(), trust_passwd, &last_change_time,
117 &sec_channel_type)) {
118 DEBUG(0, ("winbindd_pam_auth: could not fetch trust account "
119 "password for domain %s\n", lp_workgroup()));
120 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
126 ZERO_STRUCT(ret_creds);
128 /* Don't shut this down - it belongs to the connection cache code */
129 result = cm_get_netlogon_cli(lp_workgroup(), trust_passwd,
130 sec_channel_type, False, &cli);
132 if (!NT_STATUS_IS_OK(result)) {
133 DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
137 result = cli_netlogon_sam_network_logon(cli, mem_ctx,
139 name_user, name_domain,
140 global_myname(), chal,
145 /* We have to try a second time as cm_get_netlogon_cli
146 might not yet have noticed that the DC has killed
149 } while ( (attempts < 2) && (cli->fd == -1) );
152 clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &ret_creds);
154 uni_group_cache_store_netlogon(mem_ctx, &info3);
157 /* give us a more useful (more correct?) error code */
158 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) || (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
159 result = NT_STATUS_NO_LOGON_SERVERS;
162 state->response.data.auth.nt_status = NT_STATUS_V(result);
163 fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
164 fstrcpy(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
165 state->response.data.auth.pam_error = nt_status_to_pam(result);
167 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n",
168 state->request.data.auth.user,
169 state->response.data.auth.nt_status_string,
170 state->response.data.auth.pam_error));
173 talloc_destroy(mem_ctx);
175 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
178 /* Challenge Response Authentication Protocol */
180 enum winbindd_result winbindd_pam_auth_crap(struct winbindd_cli_state *state)
183 unsigned char trust_passwd[16];
184 time_t last_change_time;
185 uint32 sec_channel_type;
186 NET_USER_INFO_3 info3;
187 struct cli_state *cli = NULL;
188 TALLOC_CTX *mem_ctx = NULL;
190 const char *domain = NULL;
191 const char *contact_domain;
192 const char *workstation;
196 DATA_BLOB lm_resp, nt_resp;
198 if (!state->privileged) {
199 DEBUG(2, ("winbindd_pam_auth_crap: non-privileged access denied!\n"));
200 /* send a better message than ACCESS_DENIED */
201 push_utf8_fstring(state->response.data.auth.error_string, "winbind client not authorized to use winbindd_pam_auth_crap");
202 result = NT_STATUS_ACCESS_DENIED;
206 /* Ensure null termination */
207 state->request.data.auth_crap.user[sizeof(state->request.data.auth_crap.user)-1]='\0';
209 /* Ensure null termination */
210 state->request.data.auth_crap.domain[sizeof(state->request.data.auth_crap.domain)-1]='\0';
212 if (!(mem_ctx = talloc_init("winbind pam auth crap for (utf8) %s", state->request.data.auth_crap.user))) {
213 DEBUG(0, ("winbindd_pam_auth_crap: could not talloc_init()!\n"));
214 result = NT_STATUS_NO_MEMORY;
218 if (pull_utf8_talloc(mem_ctx, &user, state->request.data.auth_crap.user) == (size_t)-1) {
219 DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
222 if (*state->request.data.auth_crap.domain) {
224 if (pull_utf8_talloc(mem_ctx, &dom, state->request.data.auth_crap.domain) == (size_t)-1) {
225 DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
228 } else if (lp_winbind_use_default_domain()) {
229 domain = lp_workgroup();
231 DEBUG(5,("no domain specified with username (%s) - failing auth\n",
233 result = NT_STATUS_INVALID_PARAMETER;
237 DEBUG(3, ("[%5d]: pam auth crap domain: %s user: %s\n", state->pid,
240 if (lp_allow_trusted_domains() && (state->request.data.auth_crap.flags & WINBIND_PAM_CONTACT_TRUSTDOM)) {
241 contact_domain = domain;
243 contact_domain = lp_workgroup();
246 if (*state->request.data.auth_crap.workstation) {
248 if (pull_utf8_talloc(mem_ctx, &wrk, state->request.data.auth_crap.workstation) == (size_t)-1) {
249 DEBUG(0, ("winbindd_pam_auth_crap: pull_utf8_talloc failed!\n"));
253 workstation = global_myname();
256 if (state->request.data.auth_crap.lm_resp_len > sizeof(state->request.data.auth_crap.lm_resp)
257 || state->request.data.auth_crap.nt_resp_len > sizeof(state->request.data.auth_crap.nt_resp)) {
258 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n",
259 state->request.data.auth_crap.lm_resp_len,
260 state->request.data.auth_crap.nt_resp_len));
261 result = NT_STATUS_INVALID_PARAMETER;
265 lm_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.lm_resp, state->request.data.auth_crap.lm_resp_len);
266 nt_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.nt_resp, state->request.data.auth_crap.nt_resp_len);
269 * Get the machine account password for the domain to contact.
270 * This is either our own domain for a workstation, or possibly
271 * any domain for a PDC with trusted domains.
274 if (!secrets_fetch_trust_account_password (
275 contact_domain, trust_passwd, &last_change_time,
276 &sec_channel_type)) {
277 DEBUG(0, ("winbindd_pam_auth: could not fetch trust account "
278 "password for domain %s\n", contact_domain));
279 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
285 ZERO_STRUCT(ret_creds);
287 /* Don't shut this down - it belongs to the connection cache code */
288 result = cm_get_netlogon_cli(contact_domain, trust_passwd,
289 sec_channel_type, False, &cli);
291 if (!NT_STATUS_IS_OK(result)) {
292 DEBUG(3, ("could not open handle to NETLOGON pipe (error: %s)\n",
297 result = cli_netlogon_sam_network_logon(cli, mem_ctx,
301 state->request.data.auth_crap.chal,
307 /* We have to try a second time as cm_get_netlogon_cli
308 might not yet have noticed that the DC has killed
311 } while ( (attempts < 2) && (cli->fd == -1) );
313 clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &ret_creds);
315 if (NT_STATUS_IS_OK(result)) {
316 uni_group_cache_store_netlogon(mem_ctx, &info3);
317 if (state->request.data.auth_crap.flags & WINBIND_PAM_INFO3_NDR) {
318 result = append_info3_as_ndr(mem_ctx, state, &info3);
321 if (state->request.data.auth_crap.flags & WINBIND_PAM_NTKEY) {
322 memcpy(state->response.data.auth.nt_session_key, info3.user_sess_key, sizeof(state->response.data.auth.nt_session_key) /* 16 */);
324 if (state->request.data.auth_crap.flags & WINBIND_PAM_LMKEY) {
325 memcpy(state->response.data.auth.first_8_lm_hash, info3.padding, sizeof(state->response.data.auth.first_8_lm_hash) /* 8 */);
331 /* give us a more useful (more correct?) error code */
332 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) || (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
333 result = NT_STATUS_NO_LOGON_SERVERS;
336 state->response.data.auth.nt_status = NT_STATUS_V(result);
337 push_utf8_fstring(state->response.data.auth.nt_status_string, nt_errstr(result));
338 if (!*state->response.data.auth.error_string)
339 push_utf8_fstring(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
340 state->response.data.auth.pam_error = nt_status_to_pam(result);
342 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
343 ("NTLM CRAP authentication for user [%s]\\[%s] returned %s (PAM: %d)\n",
346 state->response.data.auth.nt_status_string,
347 state->response.data.auth.pam_error));
350 talloc_destroy(mem_ctx);
352 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
355 /* Change a user password */
357 enum winbindd_result winbindd_pam_chauthtok(struct winbindd_cli_state *state)
360 char *oldpass, *newpass;
361 fstring domain, user;
364 DEBUG(3, ("[%5d]: pam chauthtok %s\n", state->pid,
365 state->request.data.chauthtok.user));
370 return WINBINDD_ERROR;
372 if (!parse_domain_user(state->request.data.chauthtok.user, domain,
374 result = NT_STATUS_INVALID_PARAMETER;
378 /* Change password */
380 oldpass = state->request.data.chauthtok.oldpass;
381 newpass = state->request.data.chauthtok.newpass;
385 if (!(hnd = cm_get_sam_handle(domain))) {
386 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
387 result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
391 if (!cli_oem_change_password(hnd->cli, user, newpass, oldpass)) {
392 DEBUG(1, ("password change failed for user %s/%s\n", domain,
394 result = NT_STATUS_WRONG_PASSWORD;
396 result = NT_STATUS_OK;
400 state->response.data.auth.nt_status = NT_STATUS_V(result);
401 fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
402 fstrcpy(state->response.data.auth.error_string, nt_errstr(result));
403 state->response.data.auth.pam_error = nt_status_to_pam(result);
405 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
406 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
409 state->response.data.auth.nt_status_string,
410 state->response.data.auth.pam_error));
412 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;