2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2009
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/>.
22 #include "librpc/gen_ndr/ndr_winbind_c.h"
23 #include "librpc/gen_ndr/ndr_netlogon.h"
24 #include "lib/gencache.h"
26 struct wb_dsgetdcname_state
{
27 const char *domain_name
;
28 struct GUID domain_guid
;
29 struct netr_DsRGetDCNameInfo
*dcinfo
;
32 static void wb_dsgetdcname_done(struct tevent_req
*subreq
);
34 struct tevent_req
*wb_dsgetdcname_send(TALLOC_CTX
*mem_ctx
,
35 struct tevent_context
*ev
,
36 const char *domain_name
,
37 const struct GUID
*domain_guid
,
38 const char *site_name
,
41 struct tevent_req
*req
, *subreq
;
42 struct wb_dsgetdcname_state
*state
;
43 struct dcerpc_binding_handle
*child_binding_handle
= NULL
;
44 struct GUID
*guid_ptr
= NULL
;
46 req
= tevent_req_create(mem_ctx
, &state
, struct wb_dsgetdcname_state
);
51 D_INFO("WB command dsgetdcname start.\n"
52 "Search domain name %s and site name %s.\n",
55 if (strequal(domain_name
, "BUILTIN")) {
59 tevent_req_nterror(req
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
);
60 return tevent_req_post(req
, ev
);
63 if (strequal(domain_name
, get_global_sam_name())) {
64 int role
= lp_server_role();
65 if ( role
!= ROLE_ACTIVE_DIRECTORY_DC
) {
67 * Two options here: Give back our own address, or say there's
68 * nobody around. Right now opting for the latter, one measure
69 * to prevent the loopback connects. This might change if
72 tevent_req_nterror(req
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
);
73 return tevent_req_post(req
, ev
);
79 * We have to figure out the DC ourselves
81 child_binding_handle
= locator_child_handle();
83 struct winbindd_domain
*domain
= find_our_domain();
84 child_binding_handle
= dom_child_handle(domain
);
87 if (domain_guid
!= NULL
) {
88 /* work around a const issue in rpccli_ autogenerated code */
89 state
->domain_guid
= *domain_guid
;
90 guid_ptr
= &state
->domain_guid
;
93 state
->domain_name
= talloc_strdup(state
, domain_name
);
94 if (tevent_req_nomem(state
->domain_name
, req
)) {
95 return tevent_req_post(req
, ev
);
98 subreq
= dcerpc_wbint_DsGetDcName_send(
99 state
, ev
, child_binding_handle
, domain_name
, guid_ptr
, site_name
,
100 flags
, &state
->dcinfo
);
101 if (tevent_req_nomem(subreq
, req
)) {
102 return tevent_req_post(req
, ev
);
104 tevent_req_set_callback(subreq
, wb_dsgetdcname_done
, req
);
108 static void wb_dsgetdcname_done(struct tevent_req
*subreq
)
110 struct tevent_req
*req
= tevent_req_callback_data(
111 subreq
, struct tevent_req
);
112 struct wb_dsgetdcname_state
*state
= tevent_req_data(
113 req
, struct wb_dsgetdcname_state
);
114 NTSTATUS status
, result
;
116 status
= dcerpc_wbint_DsGetDcName_recv(subreq
, state
, &result
);
118 if (any_nt_status_not_ok(status
, result
, &status
)) {
119 tevent_req_nterror(req
, status
);
122 tevent_req_done(req
);
125 NTSTATUS
wb_dsgetdcname_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
126 struct netr_DsRGetDCNameInfo
**pdcinfo
)
128 struct wb_dsgetdcname_state
*state
= tevent_req_data(
129 req
, struct wb_dsgetdcname_state
);
132 D_INFO("WB command dsgetdcname for %s end.\n",
134 if (tevent_req_is_nterror(req
, &status
)) {
135 D_NOTICE("Failed for %s with %s.\n",
140 *pdcinfo
= talloc_move(mem_ctx
, &state
->dcinfo
);
144 NTSTATUS
wb_dsgetdcname_gencache_set(const char *domname
,
145 struct netr_DsRGetDCNameInfo
*dcinfo
)
148 enum ndr_err_code ndr_err
;
152 key
= talloc_asprintf_strupper_m(talloc_tos(), "DCINFO/%s", domname
);
154 return NT_STATUS_NO_MEMORY
;
157 if (DEBUGLEVEL
>= DBGLVL_DEBUG
) {
158 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo
, dcinfo
);
161 ndr_err
= ndr_push_struct_blob(
163 (ndr_push_flags_fn_t
)ndr_push_netr_DsRGetDCNameInfo
);
164 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
165 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
166 DBG_WARNING("ndr_push_struct_blob failed: %s\n",
167 ndr_errstr(ndr_err
));
172 ok
= gencache_set_data_blob(key
, blob
, time(NULL
)+3600);
175 DBG_WARNING("gencache_set_data_blob for key %s failed\n", key
);
177 return NT_STATUS_UNSUCCESSFUL
;
184 struct dcinfo_parser_state
{
187 struct netr_DsRGetDCNameInfo
*dcinfo
;
190 static void dcinfo_parser(const struct gencache_timeout
*timeout
,
194 struct dcinfo_parser_state
*state
= private_data
;
195 enum ndr_err_code ndr_err
;
197 if (gencache_timeout_expired(timeout
)) {
201 state
->dcinfo
= talloc(state
->mem_ctx
, struct netr_DsRGetDCNameInfo
);
202 if (state
->dcinfo
== NULL
) {
203 state
->status
= NT_STATUS_NO_MEMORY
;
207 ndr_err
= ndr_pull_struct_blob_all(
208 &blob
, state
->dcinfo
, state
->dcinfo
,
209 (ndr_pull_flags_fn_t
)ndr_pull_netr_DsRGetDCNameInfo
);
211 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
212 DBG_ERR("ndr_pull_struct_blob failed\n");
213 state
->status
= ndr_map_error2ntstatus(ndr_err
);
214 TALLOC_FREE(state
->dcinfo
);
218 state
->status
= NT_STATUS_OK
;
221 NTSTATUS
wb_dsgetdcname_gencache_get(TALLOC_CTX
*mem_ctx
,
223 struct netr_DsRGetDCNameInfo
**dcinfo
)
225 struct dcinfo_parser_state state
;
229 key
= talloc_asprintf_strupper_m(mem_ctx
, "DCINFO/%s", domname
);
231 return NT_STATUS_NO_MEMORY
;
234 state
= (struct dcinfo_parser_state
) {
235 .status
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
,
239 ok
= gencache_parse(key
, dcinfo_parser
, &state
);
242 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
245 if (!NT_STATUS_IS_OK(state
.status
)) {
249 if (DEBUGLEVEL
>= DBGLVL_DEBUG
) {
250 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo
, state
.dcinfo
);
253 *dcinfo
= state
.dcinfo
;