winbind: Initialize "expired" parameter to idmap_cache_xid2sid
[metze/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 #include "libcli/security/dom_sid.h"
23
24 struct winbindd_pam_auth_state {
25         struct winbindd_request *request;
26         struct winbindd_response *response;
27 };
28
29 static void winbindd_pam_auth_done(struct tevent_req *subreq);
30
31 struct tevent_req *winbindd_pam_auth_send(TALLOC_CTX *mem_ctx,
32                                           struct tevent_context *ev,
33                                           struct winbindd_cli_state *cli,
34                                           struct winbindd_request *request)
35 {
36         struct tevent_req *req, *subreq;
37         struct winbindd_pam_auth_state *state;
38         struct winbindd_domain *domain;
39         fstring name_namespace, name_domain, name_user;
40         char *mapped = NULL;
41         NTSTATUS status;
42         bool ok;
43
44         req = tevent_req_create(mem_ctx, &state,
45                                 struct winbindd_pam_auth_state);
46         if (req == NULL) {
47                 return NULL;
48         }
49         state->request = request;
50
51         /* Ensure null termination */
52         request->data.auth.user[sizeof(request->data.auth.user)-1] = '\0';
53         request->data.auth.pass[sizeof(request->data.auth.pass)-1] = '\0';
54
55         DBG_NOTICE("[%s (%u)]: pam auth %s\n",
56                    cli->client_name,
57                    (unsigned int)cli->pid,
58                    request->data.auth.user);
59
60         if (!check_request_flags(request->flags)) {
61                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
62                 return tevent_req_post(req, ev);
63         }
64
65         /* Parse domain and username */
66
67         status = normalize_name_unmap(state, request->data.auth.user, &mapped);
68
69         /* If the name normalization changed something, copy it over the given
70            name */
71
72         if (NT_STATUS_IS_OK(status)
73             || NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
74                 fstrcpy(request->data.auth.user, mapped);
75         }
76
77         ok = canonicalize_username(request->data.auth.user,
78                                    name_namespace,
79                                    name_domain,
80                                    name_user);
81         if (!ok) {
82                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
83                 return tevent_req_post(req, ev);
84         }
85
86         domain = find_auth_domain(request->flags, name_namespace);
87         if (domain == NULL) {
88                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
89                 return tevent_req_post(req, ev);
90         }
91
92         subreq = wb_domain_request_send(state, global_event_context(), domain,
93                                         request);
94         if (tevent_req_nomem(subreq, req)) {
95                 return tevent_req_post(req, ev);
96         }
97         tevent_req_set_callback(subreq, winbindd_pam_auth_done, req);
98         return req;
99 }
100
101 static void winbindd_pam_auth_done(struct tevent_req *subreq)
102 {
103         struct tevent_req *req = tevent_req_callback_data(
104                 subreq, struct tevent_req);
105         struct winbindd_pam_auth_state *state = tevent_req_data(
106                 req, struct winbindd_pam_auth_state);
107         int res, err;
108
109         res = wb_domain_request_recv(subreq, state, &state->response, &err);
110         TALLOC_FREE(subreq);
111         if (res == -1) {
112                 tevent_req_nterror(req, map_nt_error_from_unix(err));
113                 return;
114         }
115         tevent_req_done(req);
116 }
117
118 NTSTATUS winbindd_pam_auth_recv(struct tevent_req *req,
119                                 struct winbindd_response *response)
120 {
121         struct winbindd_pam_auth_state *state = tevent_req_data(
122                 req, struct winbindd_pam_auth_state);
123         NTSTATUS status;
124
125         if (tevent_req_is_nterror(req, &status)) {
126                 set_auth_errors(response, status);
127                 return status;
128         }
129         *response = *state->response;
130         response->result = WINBINDD_PENDING;
131         state->response = talloc_move(response, &state->response);
132
133         status = NT_STATUS(response->data.auth.nt_status);
134         if (!NT_STATUS_IS_OK(status)) {
135                 return status;
136         }
137
138         if (state->request->flags & WBFLAG_PAM_INFO3_TEXT) {
139                 bool ok;
140
141                 ok = add_trusted_domain_from_auth(
142                         state->response->data.auth.validation_level,
143                         &state->response->data.auth.info3,
144                         &state->response->data.auth.info6);
145                 if (!ok) {
146                         DBG_ERR("add_trusted_domain_from_auth failed\n");
147                         set_auth_errors(response, NT_STATUS_LOGON_FAILURE);
148                         return NT_STATUS_LOGON_FAILURE;
149                 }
150         }
151
152         if (state->request->flags & WBFLAG_PAM_CACHED_LOGIN) {
153
154                 /* Store in-memory creds for single-signon using ntlm_auth. */
155
156                 status = winbindd_add_memory_creds(
157                         state->request->data.auth.user,
158                         get_uid_from_request(state->request),
159                         state->request->data.auth.pass);
160                 DEBUG(10, ("winbindd_add_memory_creds returned: %s\n",
161                            nt_errstr(status)));
162         }
163
164         return status;
165 }