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/>.
22 #include "libcli/security/dom_sid.h"
23 #include "lib/util/string_wrappers.h"
24 #include "lib/global_contexts.h"
25 #include "librpc/gen_ndr/ndr_winbind_c.h"
27 static NTSTATUS
fake_password_policy(struct winbindd_response
*r
,
28 uint16_t validation_level
,
29 union netr_Validation
*validation
)
31 const struct netr_SamBaseInfo
*bi
= NULL
;
32 NTTIME min_password_age
;
33 NTTIME max_password_age
;
35 switch (validation_level
) {
37 bi
= &validation
->sam3
->base
;
40 bi
= &validation
->sam6
->base
;
43 return NT_STATUS_INTERNAL_ERROR
;
46 if (bi
->allow_password_change
> bi
->last_password_change
) {
47 min_password_age
= bi
->allow_password_change
-
48 bi
->last_password_change
;
53 if (bi
->force_password_change
> bi
->last_password_change
) {
54 max_password_age
= bi
->force_password_change
-
55 bi
->last_password_change
;
60 r
->data
.auth
.policy
.min_length_password
= 0;
61 r
->data
.auth
.policy
.password_history
= 0;
62 r
->data
.auth
.policy
.password_properties
= 0;
63 r
->data
.auth
.policy
.expire
=
64 nt_time_to_unix_abs(&max_password_age
);
65 r
->data
.auth
.policy
.min_passwordage
=
66 nt_time_to_unix_abs(&min_password_age
);
71 struct winbindd_pam_auth_state
{
72 struct wbint_PamAuth
*r
;
78 static void winbindd_pam_auth_done(struct tevent_req
*subreq
);
80 struct tevent_req
*winbindd_pam_auth_send(TALLOC_CTX
*mem_ctx
,
81 struct tevent_context
*ev
,
82 struct winbindd_cli_state
*cli
,
83 struct winbindd_request
*request
)
85 struct tevent_req
*req
, *subreq
;
86 struct winbindd_pam_auth_state
*state
;
87 struct winbindd_domain
*domain
;
89 char *auth_user
= NULL
;
93 req
= tevent_req_create(mem_ctx
, &state
,
94 struct winbindd_pam_auth_state
);
99 D_NOTICE("[%s (%u)] Winbind external command PAM_AUTH start.\n"
100 "Authenticating user '%s'.\n",
102 (unsigned int)cli
->pid
,
103 request
->data
.auth
.user
);
105 if (!check_request_flags(request
->flags
)) {
106 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
107 return tevent_req_post(req
, ev
);
110 /* Parse domain and username */
112 status
= normalize_name_unmap(state
, request
->data
.auth
.user
, &mapped
);
114 /* If the name normalization changed something, copy it over the given
117 if (NT_STATUS_IS_OK(status
)
118 || NT_STATUS_EQUAL(status
, NT_STATUS_FILE_RENAMED
)) {
119 fstrcpy(request
->data
.auth
.user
, mapped
);
122 auth_user
= request
->data
.auth
.user
;
123 ok
= canonicalize_username(state
,
125 &state
->name_namespace
,
129 tevent_req_nterror(req
, NT_STATUS_NO_SUCH_USER
);
130 return tevent_req_post(req
, ev
);
133 fstrcpy(request
->data
.auth
.user
, auth_user
);
135 domain
= find_auth_domain(request
->flags
, state
->name_namespace
);
136 if (domain
== NULL
) {
137 tevent_req_nterror(req
, NT_STATUS_NO_SUCH_USER
);
138 return tevent_req_post(req
, ev
);
141 state
->r
= talloc_zero(state
, struct wbint_PamAuth
);
142 if (tevent_req_nomem(state
->r
, req
)) {
143 return tevent_req_post(req
, ev
);
146 state
->r
->in
.client_name
= talloc_strdup(
147 state
->r
, request
->client_name
);
148 if (tevent_req_nomem(state
->r
, req
)) {
149 return tevent_req_post(req
, ev
);
152 state
->r
->in
.client_pid
= request
->pid
;
153 state
->r
->in
.flags
= request
->flags
;
155 state
->r
->in
.info
= talloc_zero(state
->r
, struct wbint_AuthUserInfo
);
156 if (tevent_req_nomem(state
->r
, req
)) {
157 return tevent_req_post(req
, ev
);
160 state
->r
->in
.info
->krb5_cc_type
= talloc_strdup(
161 state
->r
, request
->data
.auth
.krb5_cc_type
);
162 if (tevent_req_nomem(state
->r
, req
)) {
163 return tevent_req_post(req
, ev
);
166 state
->r
->in
.info
->password
= talloc_strdup(
167 state
->r
, request
->data
.auth
.pass
);
168 if (tevent_req_nomem(state
->r
, req
)) {
169 return tevent_req_post(req
, ev
);
172 state
->r
->in
.info
->username
= talloc_strdup(
173 state
->r
, request
->data
.auth
.user
);
174 if (tevent_req_nomem(state
->r
, req
)) {
175 return tevent_req_post(req
, ev
);
178 state
->r
->in
.info
->uid
= request
->data
.auth
.uid
;
180 status
= extra_data_to_sid_array(
181 request
->data
.auth
.require_membership_of_sid
,
183 &state
->r
->in
.require_membership_of_sid
);
184 if (tevent_req_nterror(req
, status
)) {
185 return tevent_req_post(req
, ev
);
188 subreq
= dcerpc_wbint_PamAuth_r_send(state
,
189 global_event_context(),
190 dom_child_handle(domain
),
192 if (tevent_req_nomem(subreq
, req
)) {
193 return tevent_req_post(req
, ev
);
195 tevent_req_set_callback(subreq
, winbindd_pam_auth_done
, req
);
199 static void winbindd_pam_auth_done(struct tevent_req
*subreq
)
201 struct tevent_req
*req
= tevent_req_callback_data(
202 subreq
, struct tevent_req
);
203 struct winbindd_pam_auth_state
*state
= tevent_req_data(
204 req
, struct winbindd_pam_auth_state
);
207 status
= dcerpc_wbint_PamAuth_r_recv(subreq
, state
);
209 if (tevent_req_nterror(req
, status
)) {
213 if (tevent_req_nterror(req
, state
->r
->out
.result
)) {
217 tevent_req_done(req
);
220 NTSTATUS
winbindd_pam_auth_recv(struct tevent_req
*req
,
221 struct winbindd_response
*response
)
223 struct winbindd_pam_auth_state
*state
= tevent_req_data(
224 req
, struct winbindd_pam_auth_state
);
227 D_NOTICE("Winbind external command PAM_AUTH end.\n");
228 if (tevent_req_is_nterror(req
, &status
)) {
229 set_auth_errors(response
, status
);
233 response
->result
= WINBINDD_PENDING
;
235 status
= append_auth_data(response
,
238 state
->r
->out
.validation
->level
,
239 state
->r
->out
.validation
->validation
,
242 fstrcpy(response
->data
.auth
.krb5ccname
,
243 state
->r
->out
.validation
->krb5ccname
);
245 if (state
->r
->in
.flags
& WBFLAG_PAM_INFO3_TEXT
) {
248 ok
= add_trusted_domain_from_auth(
249 state
->r
->out
.validation
->level
,
250 &response
->data
.auth
.info3
,
251 &response
->data
.auth
.info6
);
253 DBG_ERR("add_trusted_domain_from_auth failed\n");
254 set_auth_errors(response
, NT_STATUS_LOGON_FAILURE
);
255 return NT_STATUS_LOGON_FAILURE
;
259 if (state
->r
->in
.flags
& WBFLAG_PAM_CACHED_LOGIN
) {
261 /* Store in-memory creds for single-signon using ntlm_auth. */
263 status
= winbindd_add_memory_creds(
264 state
->r
->in
.info
->username
,
265 state
->r
->in
.info
->uid
,
266 state
->r
->in
.info
->password
);
267 D_DEBUG("winbindd_add_memory_creds returned: %s\n",
271 if (state
->r
->in
.flags
& WBFLAG_PAM_GET_PWD_POLICY
) {
273 * WBFLAG_PAM_GET_PWD_POLICY is not used within
274 * any Samba caller anymore.
276 * We just fake this based on the effective values
277 * for the user, for legacy callers.
279 status
= fake_password_policy(response
,
280 state
->r
->out
.validation
->level
,
281 state
->r
->out
.validation
->validation
);
282 if (!NT_STATUS_IS_OK(status
)) {
283 DBG_ERR("Failed to fake password policy: %s\n",
285 set_auth_errors(response
, status
);