pyldb: avoid segfault when adding an element with no name
[kai/samba-autobuild/.git] / source3 / winbindd / winbindd_pam_chng_pswd_auth_crap.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
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_chng_pswd_auth_crap_state {
24         struct winbindd_request *request;
25         struct winbindd_response *response;
26 };
27
28 static void winbindd_pam_chng_pswd_auth_crap_done(struct tevent_req *subreq);
29
30 struct tevent_req *winbindd_pam_chng_pswd_auth_crap_send(
31         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_chng_pswd_auth_crap_state *state;
38         struct winbindd_domain *domain;
39         const char *domain_name;
40
41         req = tevent_req_create(mem_ctx, &state,
42                                 struct winbindd_pam_chng_pswd_auth_crap_state);
43         if (req == NULL) {
44                 return NULL;
45         }
46         state->request = request;
47
48         /* Ensure null termination */
49         request->data.chng_pswd_auth_crap.user[
50                 sizeof(request->data.chng_pswd_auth_crap.user)-1]='\0';
51         request->data.chng_pswd_auth_crap.domain[
52                 sizeof(request->data.chng_pswd_auth_crap.domain)-1]=0;
53
54         DEBUG(3, ("[%5lu]: pam change pswd auth crap domain: %s user: %s\n",
55                   (unsigned long)cli->pid,
56                   request->data.chng_pswd_auth_crap.domain,
57                   request->data.chng_pswd_auth_crap.user));
58
59         domain_name = NULL;
60         if (*state->request->data.chng_pswd_auth_crap.domain != '\0') {
61                 domain_name = state->request->data.chng_pswd_auth_crap.domain;
62         } else if (lp_winbind_use_default_domain()) {
63                 domain_name = lp_workgroup();
64         }
65
66         domain = NULL;
67         if (domain_name != NULL) {
68                 domain = find_domain_from_name(domain_name);
69         }
70
71         if (domain == NULL) {
72                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
73                 return tevent_req_post(req, ev);
74         }
75
76         subreq = wb_domain_request_send(state, global_event_context(),
77                                         domain, request);
78         if (tevent_req_nomem(subreq, req)) {
79                 return tevent_req_post(req, ev);
80         }
81         tevent_req_set_callback(subreq, winbindd_pam_chng_pswd_auth_crap_done,
82                                 req);
83         return req;
84 }
85
86 static void winbindd_pam_chng_pswd_auth_crap_done(struct tevent_req *subreq)
87 {
88         struct tevent_req *req = tevent_req_callback_data(
89                 subreq, struct tevent_req);
90         struct winbindd_pam_chng_pswd_auth_crap_state *state = tevent_req_data(
91                 req, struct winbindd_pam_chng_pswd_auth_crap_state);
92         int res, err;
93
94         res = wb_domain_request_recv(subreq, state, &state->response, &err);
95         TALLOC_FREE(subreq);
96         if (res == -1) {
97                 tevent_req_nterror(req, map_nt_error_from_unix(err));
98                 return;
99         }
100         tevent_req_done(req);
101 }
102
103 NTSTATUS winbindd_pam_chng_pswd_auth_crap_recv(
104         struct tevent_req *req,
105         struct winbindd_response *response)
106 {
107         struct winbindd_pam_chng_pswd_auth_crap_state *state = tevent_req_data(
108                 req, struct winbindd_pam_chng_pswd_auth_crap_state);
109         NTSTATUS status;
110
111         if (tevent_req_is_nterror(req, &status)) {
112                 set_auth_errors(response, status);
113                 return status;
114         }
115         *response = *state->response;
116         response->result = WINBINDD_PENDING;
117         state->response = talloc_move(response, &state->response);
118
119         return NT_STATUS(response->data.auth.nt_status);
120 }