2 Unix SMB/CIFS implementation.
4 Get a struct wb_dom_info for a domain using DNS, netbios, possibly cldap
7 Copyright (C) Volker Lendecke 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "libcli/composite/composite.h"
26 #include "libcli/resolve/resolve.h"
27 #include "libcli/security/security.h"
28 #include "winbind/wb_server.h"
29 #include "smbd/service_task.h"
30 #include "librpc/gen_ndr/ndr_irpc.h"
31 #include "librpc/gen_ndr/samr.h"
32 #include "lib/messaging/irpc.h"
34 struct get_dom_info_state
{
35 struct composite_context
*ctx
;
36 struct wbsrv_service
*service
;
37 struct nbtd_getdcname r
;
38 struct wb_dom_info
*info
;
41 static void get_dom_info_recv_addrs(struct composite_context
*ctx
);
42 static void get_dom_info_recv_dcname(struct irpc_request
*ireq
);
44 struct composite_context
*wb_get_dom_info_send(TALLOC_CTX
*mem_ctx
,
45 struct wbsrv_service
*service
,
46 const char *domain_name
,
47 const struct dom_sid
*sid
)
49 struct composite_context
*result
, *ctx
;
50 struct get_dom_info_state
*state
;
53 result
= talloc(mem_ctx
, struct composite_context
);
54 if (result
== NULL
) goto failed
;
55 result
->state
= COMPOSITE_STATE_IN_PROGRESS
;
56 result
->async
.fn
= NULL
;
57 result
->event_ctx
= service
->task
->event_ctx
;
59 state
= talloc(result
, struct get_dom_info_state
);
60 if (state
== NULL
) goto failed
;
62 result
->private_data
= state
;
64 state
->service
= service
;
66 state
->info
= talloc_zero(state
, struct wb_dom_info
);
67 if (state
->info
== NULL
) goto failed
;
69 state
->info
->name
= talloc_strdup(state
->info
, domain_name
);
70 if (state
->info
->name
== NULL
) goto failed
;
71 state
->info
->sid
= dom_sid_dup(state
->info
, sid
);
72 if (state
->info
->sid
== NULL
) goto failed
;
74 make_nbt_name(&name
, state
->info
->name
, NBT_NAME_LOGON
);
76 ctx
= resolve_name_send(&name
, result
->event_ctx
,
77 lp_name_resolve_order());
78 if (ctx
== NULL
) goto failed
;
80 ctx
->async
.fn
= get_dom_info_recv_addrs
;
81 ctx
->async
.private_data
= state
;
89 static void get_dom_info_recv_addrs(struct composite_context
*ctx
)
91 struct get_dom_info_state
*state
=
92 talloc_get_type(ctx
->async
.private_data
,
93 struct get_dom_info_state
);
94 uint32_t *nbt_servers
;
95 struct irpc_request
*ireq
;
97 state
->ctx
->status
= resolve_name_recv(ctx
, state
->info
,
98 &state
->info
->dc_address
);
99 if (!composite_is_ok(state
->ctx
)) return;
101 nbt_servers
= irpc_servers_byname(state
->service
->task
->msg_ctx
,
103 if ((nbt_servers
== NULL
) || (nbt_servers
[0] == 0)) {
104 composite_error(state
->ctx
, NT_STATUS_NO_LOGON_SERVERS
);
108 state
->r
.in
.domainname
= state
->info
->name
;
109 state
->r
.in
.ip_address
= state
->info
->dc_address
;
110 state
->r
.in
.my_computername
= lp_netbios_name();
111 state
->r
.in
.my_accountname
= talloc_asprintf(state
, "%s$",
113 if (composite_nomem(state
->r
.in
.my_accountname
, state
->ctx
)) return;
114 state
->r
.in
.account_control
= ACB_WSTRUST
;
115 state
->r
.in
.domain_sid
= dom_sid_dup(state
, state
->info
->sid
);
116 if (composite_nomem(state
->r
.in
.domain_sid
, state
->ctx
)) return;
118 ireq
= irpc_call_send(state
->service
->task
->msg_ctx
, nbt_servers
[0],
119 &dcerpc_table_irpc
, DCERPC_NBTD_GETDCNAME
,
121 composite_continue_irpc(state
->ctx
, ireq
, get_dom_info_recv_dcname
,
125 static void get_dom_info_recv_dcname(struct irpc_request
*ireq
)
127 struct get_dom_info_state
*state
=
128 talloc_get_type(ireq
->async
.private,
129 struct get_dom_info_state
);
132 state
->ctx
->status
= irpc_call_recv(ireq
);
133 if (!composite_is_ok(state
->ctx
)) return;
135 state
->info
->dc_name
= talloc_steal(state
->info
, state
->r
.out
.dcname
);
136 composite_done(state
->ctx
);
139 NTSTATUS
wb_get_dom_info_recv(struct composite_context
*ctx
,
141 struct wb_dom_info
**result
)
143 NTSTATUS status
= composite_wait(ctx
);
144 if (NT_STATUS_IS_OK(status
)) {
145 struct get_dom_info_state
*state
=
146 talloc_get_type(ctx
->private_data
,
147 struct get_dom_info_state
);
148 *result
= talloc_steal(mem_ctx
, state
->info
);
154 NTSTATUS
wb_get_dom_info(TALLOC_CTX
*mem_ctx
,
155 struct wbsrv_service
*service
,
156 const char *domain_name
,
157 const struct dom_sid
*sid
,
158 struct wb_dom_info
**result
)
160 struct composite_context
*ctx
=
161 wb_get_dom_info_send(mem_ctx
, service
, domain_name
, sid
);
162 return wb_get_dom_info_recv(ctx
, mem_ctx
, result
);