s3-rpc_client: move protos to cli_lsarpc.h
[Samba/ekacnet.git] / source3 / winbindd / winbindd_pam_auth.c
blob94d98ec55b784223ce7770c6b7dcd9d5c9b1e60c
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, mapped_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 didnt' actually do anything,
66 just use the original name */
68 if (NT_STATUS_IS_OK(status)
69 || NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
70 fstrcpy(mapped_user, mapped);
71 } else {
72 fstrcpy(mapped_user, request->data.auth.user);
75 if (!canonicalize_username(mapped_user, name_domain, name_user)) {
76 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
77 return tevent_req_post(req, ev);
80 domain = find_auth_domain(request->flags, name_domain);
81 if (domain == NULL) {
82 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
83 return tevent_req_post(req, ev);
86 subreq = wb_domain_request_send(state, winbind_event_context(), domain,
87 request);
88 if (tevent_req_nomem(subreq, req)) {
89 return tevent_req_post(req, ev);
91 tevent_req_set_callback(subreq, winbindd_pam_auth_done, req);
92 return req;
95 static void winbindd_pam_auth_done(struct tevent_req *subreq)
97 struct tevent_req *req = tevent_req_callback_data(
98 subreq, struct tevent_req);
99 struct winbindd_pam_auth_state *state = tevent_req_data(
100 req, struct winbindd_pam_auth_state);
101 int res, err;
103 res = wb_domain_request_recv(subreq, state, &state->response, &err);
104 TALLOC_FREE(subreq);
105 if (res == -1) {
106 tevent_req_nterror(req, map_nt_error_from_unix(err));
107 return;
109 tevent_req_done(req);
112 NTSTATUS winbindd_pam_auth_recv(struct tevent_req *req,
113 struct winbindd_response *response)
115 struct winbindd_pam_auth_state *state = tevent_req_data(
116 req, struct winbindd_pam_auth_state);
117 NTSTATUS status;
119 if (tevent_req_is_nterror(req, &status)) {
120 set_auth_errors(response, status);
121 return status;
123 *response = *state->response;
124 response->result = WINBINDD_PENDING;
125 state->response = talloc_move(response, &state->response);
127 status = NT_STATUS(response->data.auth.nt_status);
128 if (!NT_STATUS_IS_OK(status)) {
129 return status;
132 if (state->request->flags & WBFLAG_PAM_CACHED_LOGIN) {
134 /* Store in-memory creds for single-signon using ntlm_auth. */
136 status = winbindd_add_memory_creds(
137 state->request->data.auth.user,
138 get_uid_from_request(state->request),
139 state->request->data.auth.pass);
140 DEBUG(10, ("winbindd_add_memory_creds returned: %s\n",
141 nt_errstr(status)));
144 return status;