winbind: Fix an error path memleak
[samba.git] / source3 / winbindd / wb_dsgetdcname.c
1 /*
2    Unix SMB/CIFS implementation.
3    async dsgetdcname
4    Copyright (C) Volker Lendecke 2009
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 "librpc/gen_ndr/ndr_winbind_c.h"
23 #include "librpc/gen_ndr/ndr_netlogon.h"
24 #include "lib/gencache.h"
25
26 struct wb_dsgetdcname_state {
27         struct netr_DsRGetDCNameInfo *dcinfo;
28 };
29
30 static void wb_dsgetdcname_done(struct tevent_req *subreq);
31
32 struct tevent_req *wb_dsgetdcname_send(TALLOC_CTX *mem_ctx,
33                                        struct tevent_context *ev,
34                                        const char *domain_name,
35                                        const struct GUID *domain_guid,
36                                        const char *site_name,
37                                        uint32_t flags)
38 {
39         struct tevent_req *req, *subreq;
40         struct wb_dsgetdcname_state *state;
41         struct dcerpc_binding_handle *child_binding_handle = NULL;
42         struct GUID guid;
43         struct GUID *guid_ptr = NULL;
44
45         req = tevent_req_create(mem_ctx, &state, struct wb_dsgetdcname_state);
46         if (req == NULL) {
47                 return NULL;
48         }
49
50         if (strequal(domain_name, "BUILTIN")) {
51                 /*
52                  * This makes no sense
53                  */
54                 tevent_req_nterror(req, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND);
55                 return tevent_req_post(req, ev);
56         }
57
58         if (strequal(domain_name, get_global_sam_name())) {
59                 int role = lp_server_role();
60                 if ( role != ROLE_ACTIVE_DIRECTORY_DC ) {
61                         /*
62                          * Two options here: Give back our own address, or say there's
63                          * nobody around. Right now opting for the latter, one measure
64                          * to prevent the loopback connects. This might change if
65                          * needed.
66                          */
67                         tevent_req_nterror(req, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND);
68                         return tevent_req_post(req, ev);
69                 }
70         }
71
72         if (IS_DC) {
73                 /*
74                  * We have to figure out the DC ourselves
75                  */
76                 child_binding_handle = locator_child_handle();
77         } else {
78                 struct winbindd_domain *domain = find_our_domain();
79                 child_binding_handle = dom_child_handle(domain);
80         }
81
82         if (domain_guid != NULL) {
83                 /* work around a const issue in rpccli_ autogenerated code */
84                 guid = *domain_guid;
85                 guid_ptr = &guid;
86         }
87
88         subreq = dcerpc_wbint_DsGetDcName_send(
89                 state, ev, child_binding_handle, domain_name, guid_ptr, site_name,
90                 flags, &state->dcinfo);
91         if (tevent_req_nomem(subreq, req)) {
92                 return tevent_req_post(req, ev);
93         }
94         tevent_req_set_callback(subreq, wb_dsgetdcname_done, req);
95         return req;
96 }
97
98 static void wb_dsgetdcname_done(struct tevent_req *subreq)
99 {
100         struct tevent_req *req = tevent_req_callback_data(
101                 subreq, struct tevent_req);
102         struct wb_dsgetdcname_state *state = tevent_req_data(
103                 req, struct wb_dsgetdcname_state);
104         NTSTATUS status, result;
105
106         status = dcerpc_wbint_DsGetDcName_recv(subreq, state, &result);
107         TALLOC_FREE(subreq);
108         if (any_nt_status_not_ok(status, result, &status)) {
109                 tevent_req_nterror(req, status);
110                 return;
111         }
112         tevent_req_done(req);
113 }
114
115 NTSTATUS wb_dsgetdcname_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
116                              struct netr_DsRGetDCNameInfo **pdcinfo)
117 {
118         struct wb_dsgetdcname_state *state = tevent_req_data(
119                 req, struct wb_dsgetdcname_state);
120         NTSTATUS status;
121
122         if (tevent_req_is_nterror(req, &status)) {
123                 return status;
124         }
125         *pdcinfo = talloc_move(mem_ctx, &state->dcinfo);
126         return NT_STATUS_OK;
127 }
128
129 NTSTATUS wb_dsgetdcname_gencache_set(const char *domname,
130                                      struct netr_DsRGetDCNameInfo *dcinfo)
131 {
132         DATA_BLOB blob;
133         enum ndr_err_code ndr_err;
134         char *key;
135         bool ok;
136
137         key = talloc_asprintf_strupper_m(talloc_tos(), "DCINFO/%s", domname);
138         if (key == NULL) {
139                 return NT_STATUS_NO_MEMORY;
140         }
141
142         if (DEBUGLEVEL >= 10) {
143                 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo, dcinfo);
144         }
145
146         ndr_err = ndr_push_struct_blob(
147                 &blob, key, dcinfo,
148                 (ndr_push_flags_fn_t)ndr_push_netr_DsRGetDCNameInfo);
149         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
150                 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
151                 DBG_WARNING("ndr_push_struct_blob failed: %s\n",
152                             ndr_errstr(ndr_err));
153                 TALLOC_FREE(key);
154                 return status;
155         }
156
157         ok = gencache_set_data_blob(key, blob, time(NULL)+3600);
158
159         if (!ok) {
160                 DBG_WARNING("gencache_set_data_blob for key %s failed\n", key);
161                 TALLOC_FREE(key);
162                 return NT_STATUS_UNSUCCESSFUL;
163         }
164
165         TALLOC_FREE(key);
166         return NT_STATUS_OK;
167 }
168
169 struct dcinfo_parser_state {
170         NTSTATUS status;
171         TALLOC_CTX *mem_ctx;
172         struct netr_DsRGetDCNameInfo *dcinfo;
173 };
174
175 static void dcinfo_parser(const struct gencache_timeout *timeout,
176                           DATA_BLOB blob,
177                           void *private_data)
178 {
179         struct dcinfo_parser_state *state = private_data;
180         enum ndr_err_code ndr_err;
181
182         if (gencache_timeout_expired(timeout)) {
183                 return;
184         }
185
186         state->dcinfo = talloc(state->mem_ctx, struct netr_DsRGetDCNameInfo);
187         if (state->dcinfo == NULL) {
188                 state->status = NT_STATUS_NO_MEMORY;
189                 return;
190         }
191
192         ndr_err = ndr_pull_struct_blob_all(
193                 &blob, state->dcinfo, state->dcinfo,
194                 (ndr_pull_flags_fn_t)ndr_pull_netr_DsRGetDCNameInfo);
195
196         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
197                 DBG_ERR("ndr_pull_struct_blob failed\n");
198                 state->status = ndr_map_error2ntstatus(ndr_err);
199                 TALLOC_FREE(state->dcinfo);
200                 return;
201         }
202
203         state->status = NT_STATUS_OK;
204 }
205
206 NTSTATUS wb_dsgetdcname_gencache_get(TALLOC_CTX *mem_ctx,
207                                      const char *domname,
208                                      struct netr_DsRGetDCNameInfo **dcinfo)
209 {
210         struct dcinfo_parser_state state;
211         char *key;
212         bool ok;
213
214         key = talloc_asprintf_strupper_m(mem_ctx, "DCINFO/%s", domname);
215         if (key == NULL) {
216                 return NT_STATUS_NO_MEMORY;
217         }
218
219         state = (struct dcinfo_parser_state) {
220                 .status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND,
221                 .mem_ctx = mem_ctx,
222         };
223
224         ok = gencache_parse(key, dcinfo_parser, &state);
225         TALLOC_FREE(key);
226         if (!ok) {
227                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
228         }
229
230         if (!NT_STATUS_IS_OK(state.status)) {
231                 return state.status;
232         }
233
234         if (DEBUGLEVEL >= 10) {
235                 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo, state.dcinfo);
236         }
237
238         *dcinfo = state.dcinfo;
239         return NT_STATUS_OK;
240 }