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