s3: Fix a winbind race leading to 100% CPU
[Samba.git] / source3 / winbindd / winbindd_pam_auth.c
blob4f963a32818f4af7b675b932cca8519ac80bfa9e
1 /*
2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_PAM_AUTH
4 Copyright (C) Volker Lendecke 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "winbindd.h"
23 struct winbindd_pam_auth_state {
24 struct winbindd_request *request;
25 struct winbindd_response *response;
28 static void winbindd_pam_auth_done(struct tevent_req *subreq);
30 struct tevent_req *winbindd_pam_auth_send(TALLOC_CTX *mem_ctx,
31 struct tevent_context *ev,
32 struct winbindd_cli_state *cli,
33 struct winbindd_request *request)
35 struct tevent_req *req, *subreq;
36 struct winbindd_pam_auth_state *state;
37 struct winbindd_domain *domain;
38 fstring name_domain, name_user;
39 char *mapped = NULL;
40 NTSTATUS status;
42 req = tevent_req_create(mem_ctx, &state,
43 struct winbindd_pam_auth_state);
44 if (req == NULL) {
45 return NULL;
47 state->request = request;
49 /* Ensure null termination */
50 request->data.auth.user[sizeof(request->data.auth.user)-1] = '\0';
51 request->data.auth.pass[sizeof(request->data.auth.pass)-1] = '\0';
53 DEBUG(3, ("[%5lu]: pam auth %s\n", (unsigned long)cli->pid,
54 request->data.auth.user));
56 if (!check_request_flags(request->flags)) {
57 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
58 return tevent_req_post(req, ev);
61 /* Parse domain and username */
63 status = normalize_name_unmap(state, request->data.auth.user, &mapped);
65 /* If the name normalization changed something, copy it over the given
66 name */
68 if (NT_STATUS_IS_OK(status)
69 || NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
70 fstrcpy(request->data.auth.user, mapped);
73 if (!canonicalize_username(request->data.auth.user, name_domain, name_user)) {
74 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
75 return tevent_req_post(req, ev);
78 domain = find_auth_domain(request->flags, name_domain);
79 if (domain == NULL) {
80 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
81 return tevent_req_post(req, ev);
84 subreq = wb_domain_request_send(state, winbind_event_context(), domain,
85 request);
86 if (tevent_req_nomem(subreq, req)) {
87 return tevent_req_post(req, ev);
89 tevent_req_set_callback(subreq, winbindd_pam_auth_done, req);
90 return req;
93 static void winbindd_pam_auth_done(struct tevent_req *subreq)
95 struct tevent_req *req = tevent_req_callback_data(
96 subreq, struct tevent_req);
97 struct winbindd_pam_auth_state *state = tevent_req_data(
98 req, struct winbindd_pam_auth_state);
99 int res, err;
101 res = wb_domain_request_recv(subreq, state, &state->response, &err);
102 TALLOC_FREE(subreq);
103 if (res == -1) {
104 tevent_req_nterror(req, map_nt_error_from_unix(err));
105 return;
107 tevent_req_done(req);
110 NTSTATUS winbindd_pam_auth_recv(struct tevent_req *req,
111 struct winbindd_response *response)
113 struct winbindd_pam_auth_state *state = tevent_req_data(
114 req, struct winbindd_pam_auth_state);
115 NTSTATUS status;
117 if (tevent_req_is_nterror(req, &status)) {
118 set_auth_errors(response, status);
119 return status;
121 *response = *state->response;
122 response->result = WINBINDD_PENDING;
123 state->response = talloc_move(response, &state->response);
125 status = NT_STATUS(response->data.auth.nt_status);
126 if (!NT_STATUS_IS_OK(status)) {
127 return status;
130 if (state->request->flags & WBFLAG_PAM_CACHED_LOGIN) {
132 /* Store in-memory creds for single-signon using ntlm_auth. */
134 status = winbindd_add_memory_creds(
135 state->request->data.auth.user,
136 get_uid_from_request(state->request),
137 state->request->data.auth.pass);
138 DEBUG(10, ("winbindd_add_memory_creds returned: %s\n",
139 nt_errstr(status)));
142 return status;