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