Fix bug #7589 - ntlm_auth fails to use cached credentials.
[bbaumbach/samba-autobuild/.git] / source3 / winbindd / winbindd_pam_auth.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_PAM_AUTH
4    Copyright (C) Volker Lendecke 2010
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "winbindd.h"
22
23 struct winbindd_pam_auth_state {
24         struct winbindd_request *request;
25         struct winbindd_response *response;
26 };
27
28 static void winbindd_pam_auth_done(struct tevent_req *subreq);
29
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)
34 {
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;
41
42         req = tevent_req_create(mem_ctx, &state,
43                                 struct winbindd_pam_auth_state);
44         if (req == NULL) {
45                 return NULL;
46         }
47         state->request = request;
48
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';
52
53         DEBUG(3, ("[%5lu]: pam auth %s\n", (unsigned long)cli->pid,
54                   request->data.auth.user));
55
56         if (!check_request_flags(request->flags)) {
57                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
58                 return tevent_req_post(req, ev);
59         }
60
61         /* Parse domain and username */
62
63         status = normalize_name_unmap(state, request->data.auth.user, &mapped);
64
65         /* If the name normalization changed something, copy it over the given
66            name */
67
68         if (NT_STATUS_IS_OK(status)
69             || NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
70                 fstrcpy(request->data.auth.user, mapped);
71         }
72
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);
76         }
77
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);
82         }
83
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);
88         }
89         tevent_req_set_callback(subreq, winbindd_pam_auth_done, req);
90         return req;
91 }
92
93 static void winbindd_pam_auth_done(struct tevent_req *subreq)
94 {
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;
100
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;
106         }
107         tevent_req_done(req);
108 }
109
110 NTSTATUS winbindd_pam_auth_recv(struct tevent_req *req,
111                                 struct winbindd_response *response)
112 {
113         struct winbindd_pam_auth_state *state = tevent_req_data(
114                 req, struct winbindd_pam_auth_state);
115         NTSTATUS status;
116
117         if (tevent_req_is_nterror(req, &status)) {
118                 set_auth_errors(response, status);
119                 return status;
120         }
121         *response = *state->response;
122         response->result = WINBINDD_PENDING;
123         state->response = talloc_move(response, &state->response);
124
125         status = NT_STATUS(response->data.auth.nt_status);
126         if (!NT_STATUS_IS_OK(status)) {
127                 return status;
128         }
129
130         if (state->request->flags & WBFLAG_PAM_CACHED_LOGIN) {
131
132                 /* Store in-memory creds for single-signon using ntlm_auth. */
133
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)));
140         }
141
142         return status;
143 }