gpo: Add CSE for applying smb.conf
[Samba.git] / source3 / winbindd / winbindd_pam_chauthtok.c
blobc802649497d6fcbab7791c46ae227bbc153bd9f5
1 /*
2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_PAM_CHAUTHTOK
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_chauthtok_state {
24 struct winbindd_request *request;
25 struct winbindd_response *response;
28 static void winbindd_pam_chauthtok_done(struct tevent_req *subreq);
30 struct tevent_req *winbindd_pam_chauthtok_send(
31 TALLOC_CTX *mem_ctx,
32 struct tevent_context *ev,
33 struct winbindd_cli_state *cli,
34 struct winbindd_request *request)
36 struct tevent_req *req, *subreq;
37 struct winbindd_pam_chauthtok_state *state;
38 struct winbindd_domain *contact_domain;
39 fstring namespace, domain, user;
40 char *mapped_user;
41 NTSTATUS status;
42 bool ok;
44 req = tevent_req_create(mem_ctx, &state,
45 struct winbindd_pam_chauthtok_state);
46 if (req == NULL) {
47 return NULL;
49 state->request = request;
51 /* Ensure null termination */
52 request->data.chauthtok.user[
53 sizeof(request->data.chauthtok.user)-1]='\0';
55 DEBUG(3, ("[%5lu]: pam chauthtok %s\n", (unsigned long)cli->pid,
56 request->data.chauthtok.user));
58 status = normalize_name_unmap(state, request->data.chauthtok.user,
59 &mapped_user);
61 if (NT_STATUS_IS_OK(status) ||
62 NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
63 fstrcpy(request->data.chauthtok.user, mapped_user);
66 ok = canonicalize_username(request->data.chauthtok.user,
67 namespace,
68 domain,
69 user);
70 if (!ok) {
71 DEBUG(10, ("winbindd_pam_chauthtok: canonicalize_username %s "
72 "failed with\n", request->data.chauthtok.user));
73 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
74 return tevent_req_post(req, ev);
77 contact_domain = find_domain_from_name(namespace);
78 if (contact_domain == NULL) {
79 DEBUG(3, ("Cannot change password for [%s] -> [%s]\\[%s] "
80 "as %s is not a trusted domain\n",
81 request->data.chauthtok.user, domain, user, domain));
82 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
83 return tevent_req_post(req, ev);
86 subreq = wb_domain_request_send(state, global_event_context(),
87 contact_domain, request);
88 if (tevent_req_nomem(subreq, req)) {
89 return tevent_req_post(req, ev);
91 tevent_req_set_callback(subreq, winbindd_pam_chauthtok_done, req);
92 return req;
95 static void winbindd_pam_chauthtok_done(struct tevent_req *subreq)
97 struct tevent_req *req = tevent_req_callback_data(
98 subreq, struct tevent_req);
99 struct winbindd_pam_chauthtok_state *state = tevent_req_data(
100 req, struct winbindd_pam_chauthtok_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_chauthtok_recv(struct tevent_req *req,
113 struct winbindd_response *response)
115 struct winbindd_pam_chauthtok_state *state = tevent_req_data(
116 req, struct winbindd_pam_chauthtok_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 /* Update the single sign-on memory creds. */
135 status = winbindd_replace_memory_creds(
136 state->request->data.chauthtok.user,
137 state->request->data.chauthtok.newpass);
139 DEBUG(10, ("winbindd_replace_memory_creds returned %s\n",
140 nt_errstr(status)));
143 * When we login from gdm or xdm and password expires,
144 * we change password, but there are no memory
145 * crendentials So, winbindd_replace_memory_creds()
146 * returns NT_STATUS_OBJECT_NAME_NOT_FOUND. This is
147 * not a failure. --- BoYang
149 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
150 status = NT_STATUS_OK;
153 return status;