s3: Fill in workstation in winbindd_pam_auth_crap_send
[bbaumbach/samba-autobuild/.git] / source3 / winbindd / winbindd_pam_auth_crap.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_PAM_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_auth_crap_state {
24         struct winbindd_response *response;
25 };
26
27 static void winbindd_pam_auth_crap_done(struct tevent_req *subreq);
28
29 struct tevent_req *winbindd_pam_auth_crap_send(
30         TALLOC_CTX *mem_ctx,
31         struct tevent_context *ev,
32         struct winbindd_cli_state *cli,
33         struct winbindd_request *request)
34 {
35         struct tevent_req *req, *subreq;
36         struct winbindd_pam_auth_crap_state *state;
37         struct winbindd_domain *domain;
38         const char *domain_name;
39
40         req = tevent_req_create(mem_ctx, &state,
41                                 struct winbindd_pam_auth_crap_state);
42         if (req == NULL) {
43                 return NULL;
44         }
45
46         /* Ensure null termination */
47         request->data.auth_crap.user[
48                 sizeof(request->data.auth_crap.user)-1] = '\0';
49         request->data.auth_crap.domain[
50                 sizeof(request->data.auth_crap.domain)-1] = '\0';
51
52         DEBUG(3, ("[%5lu]: pam auth crap domain: [%s] user: %s\n",
53                   (unsigned long)cli->pid,
54                   request->data.auth_crap.domain,
55                   request->data.auth_crap.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         domain_name = NULL;
63
64         if (request->data.auth_crap.domain[0] != '\0') {
65                 domain_name = request->data.auth_crap.domain;
66         } else if (lp_winbind_use_default_domain()) {
67                 domain_name = lp_workgroup();
68         }
69
70         domain = NULL;
71
72         if (domain_name != NULL) {
73                 domain = find_auth_domain(request->flags, domain_name);
74         }
75
76         if (domain == NULL) {
77                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
78                 return tevent_req_post(req, ev);
79         }
80
81         fstrcpy(request->data.auth_crap.domain, domain->name);
82
83         if (request->data.auth_crap.workstation[0] == '\0') {
84                 fstrcpy(request->data.auth_crap.workstation, global_myname());
85         }
86
87         subreq = wb_domain_request_send(state, winbind_event_context(), domain,
88                                         request);
89         if (tevent_req_nomem(subreq, req)) {
90                 return tevent_req_post(req, ev);
91         }
92         tevent_req_set_callback(subreq, winbindd_pam_auth_crap_done, req);
93         return req;
94 }
95
96 static void winbindd_pam_auth_crap_done(struct tevent_req *subreq)
97 {
98         struct tevent_req *req = tevent_req_callback_data(
99                 subreq, struct tevent_req);
100         struct winbindd_pam_auth_crap_state *state = tevent_req_data(
101                 req, struct winbindd_pam_auth_crap_state);
102         int res, err;
103
104         res = wb_domain_request_recv(subreq, state, &state->response, &err);
105         TALLOC_FREE(subreq);
106         if (res == -1) {
107                 tevent_req_nterror(req, map_nt_error_from_unix(err));
108                 return;
109         }
110         tevent_req_done(req);
111 }
112
113 NTSTATUS winbindd_pam_auth_crap_recv(struct tevent_req *req,
114                                      struct winbindd_response *response)
115 {
116         struct winbindd_pam_auth_crap_state *state = tevent_req_data(
117                 req, struct winbindd_pam_auth_crap_state);
118         NTSTATUS status;
119
120         if (tevent_req_is_nterror(req, &status)) {
121                 set_auth_errors(response, status);
122                 return status;
123         }
124         *response = *state->response;
125         response->result = WINBINDD_PENDING;
126         state->response = talloc_move(response, &state->response);
127         return NT_STATUS(response->data.auth.nt_status);
128 }