2 Unix SMB/CIFS implementation.
4 Copyright (C) Brad Henry 2005
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/>.
21 #include "libnet/libnet.h"
22 #include "libcli/cldap/cldap.h"
23 #include "source3/libads/netlogon_ping.h"
25 #include <ldb_errors.h>
26 #include "libcli/resolve/resolve.h"
27 #include "param/param.h"
28 #include "lib/tsocket/tsocket.h"
31 * 1. Setup a CLDAP socket.
32 * 2. Lookup the default Site-Name.
34 NTSTATUS
libnet_FindSite(TALLOC_CTX
*ctx
, struct libnet_context
*lctx
, struct libnet_JoinSite
*r
)
37 TALLOC_CTX
*tmp_ctx
= NULL
;
39 char *site_name_str
= NULL
;
40 char *config_dn_str
= NULL
;
41 char *server_dn_str
= NULL
;
44 struct tsocket_address
*dest_address
= NULL
;
45 struct netlogon_samlogon_response
**responses
= NULL
;
47 tmp_ctx
= talloc_named(ctx
, 0, "libnet_FindSite temp context");
49 r
->out
.error_string
= NULL
;
53 site_name_str
= talloc_strdup(tmp_ctx
, "Default-First-Site-Name");
54 if (site_name_str
== NULL
) {
55 r
->out
.error_string
= NULL
;
59 ret
= tsocket_address_inet_from_strings(
60 tmp_ctx
, "ip", r
->in
.dest_address
, 389, &dest_address
);
62 r
->out
.error_string
= NULL
;
63 status
= map_nt_error_from_unix_common(errno
);
67 status
= netlogon_pings(tmp_ctx
, /* mem_ctx */
68 lpcfg_client_netlogon_ping_protocol(
69 lctx
->lp_ctx
), /* proto */
70 &dest_address
, /* servers*/
72 (struct netlogon_ping_filter
){
73 .ntversion
= NETLOGON_NT_VERSION_5
|
74 NETLOGON_NT_VERSION_5EX
,
78 tevent_timeval_current_ofs(2, 0), /* timeout */
81 if (NT_STATUS_IS_OK(status
)) {
82 struct netlogon_samlogon_response
*resp
= responses
[0];
83 struct NETLOGON_SAM_LOGON_RESPONSE_EX
84 *nt5ex
= &resp
->data
.nt5_ex
;
86 map_netlogon_samlogon_response(resp
);
88 if ((nt5ex
->client_site
!= NULL
) &&
89 (nt5ex
->client_site
[0] != '\0'))
91 site_name_str
= talloc_strdup(tmp_ctx
,
93 if (site_name_str
== NULL
) {
94 r
->out
.error_string
= NULL
;
100 /* Generate the CN=Configuration,... DN. */
101 /* TODO: look it up! */
102 config_dn_str
= talloc_asprintf(tmp_ctx
, "CN=Configuration,%s", r
->in
.domain_dn_str
);
103 if (!config_dn_str
) {
104 r
->out
.error_string
= NULL
;
108 /* Generate the CN=Servers,... DN. */
109 server_dn_str
= talloc_asprintf(tmp_ctx
, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
110 r
->in
.netbios_name
, site_name_str
, config_dn_str
);
111 if (!server_dn_str
) {
112 r
->out
.error_string
= NULL
;
116 r
->out
.site_name_str
= talloc_move(r
, &site_name_str
);
117 r
->out
.config_dn_str
= talloc_move(r
, &config_dn_str
);
118 r
->out
.server_dn_str
= talloc_move(r
, &server_dn_str
);
120 talloc_free(tmp_ctx
);
123 status
= NT_STATUS_NO_MEMORY
;
125 TALLOC_FREE(tmp_ctx
);
130 * find out Site specific stuff:
131 * 1. Lookup the Site name.
132 * 2. Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
133 * TODO: 3.) use DsAddEntry() to create CN=NTDS Settings,CN=<netbios name>,CN=Servers,CN=<site name>,...
135 NTSTATUS
libnet_JoinSite(struct libnet_context
*ctx
,
136 struct ldb_context
*remote_ldb
,
137 struct libnet_JoinDomain
*libnet_r
)
142 struct libnet_JoinSite
*r
;
144 struct ldb_dn
*server_dn
;
145 struct ldb_message
*msg
;
148 const char *server_dn_str
;
150 struct nbt_name name
;
151 const char *dest_addr
= NULL
;
153 tmp_ctx
= talloc_named(libnet_r
, 0, "libnet_JoinSite temp context");
155 libnet_r
->out
.error_string
= NULL
;
156 return NT_STATUS_NO_MEMORY
;
159 r
= talloc(tmp_ctx
, struct libnet_JoinSite
);
161 libnet_r
->out
.error_string
= NULL
;
162 talloc_free(tmp_ctx
);
163 return NT_STATUS_NO_MEMORY
;
166 host
= dcerpc_binding_get_string_option(libnet_r
->out
.samr_binding
, "host");
167 make_nbt_name_client(&name
, host
);
168 status
= resolve_name_ex(lpcfg_resolve_context(ctx
->lp_ctx
),
170 &name
, r
, &dest_addr
, ctx
->event_ctx
);
171 if (!NT_STATUS_IS_OK(status
)) {
172 libnet_r
->out
.error_string
= NULL
;
173 talloc_free(tmp_ctx
);
177 /* Resolve the site name and AD DN's. */
178 r
->in
.dest_address
= dest_addr
;
179 r
->in
.netbios_name
= libnet_r
->in
.netbios_name
;
180 r
->in
.domain_dn_str
= libnet_r
->out
.domain_dn_str
;
182 status
= libnet_FindSite(tmp_ctx
, ctx
, r
);
183 if (!NT_STATUS_IS_OK(status
)) {
184 libnet_r
->out
.error_string
=
185 talloc_steal(libnet_r
, r
->out
.error_string
);
186 talloc_free(tmp_ctx
);
190 server_dn_str
= r
->out
.server_dn_str
;
193 Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
195 msg
= ldb_msg_new(tmp_ctx
);
197 libnet_r
->out
.error_string
= NULL
;
198 talloc_free(tmp_ctx
);
199 return NT_STATUS_NO_MEMORY
;
202 rtn
= ldb_msg_add_string(msg
, "objectClass", "server");
203 if (rtn
!= LDB_SUCCESS
) {
204 libnet_r
->out
.error_string
= NULL
;
205 talloc_free(tmp_ctx
);
206 return NT_STATUS_NO_MEMORY
;
208 rtn
= ldb_msg_add_string(msg
, "systemFlags", "50000000");
209 if (rtn
!= LDB_SUCCESS
) {
210 libnet_r
->out
.error_string
= NULL
;
211 talloc_free(tmp_ctx
);
212 return NT_STATUS_NO_MEMORY
;
214 rtn
= ldb_msg_add_string(msg
, "serverReference", libnet_r
->out
.account_dn_str
);
215 if (rtn
!= LDB_SUCCESS
) {
216 libnet_r
->out
.error_string
= NULL
;
217 talloc_free(tmp_ctx
);
218 return NT_STATUS_NO_MEMORY
;
221 server_dn
= ldb_dn_new(tmp_ctx
, remote_ldb
, server_dn_str
);
222 if ( ! ldb_dn_validate(server_dn
)) {
223 libnet_r
->out
.error_string
= talloc_asprintf(libnet_r
,
224 "Invalid server dn: %s",
226 talloc_free(tmp_ctx
);
227 return NT_STATUS_UNSUCCESSFUL
;
232 rtn
= ldb_add(remote_ldb
, msg
);
233 if (rtn
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
236 /* make a 'modify' msg, and only for serverReference */
237 msg
= ldb_msg_new(tmp_ctx
);
239 libnet_r
->out
.error_string
= NULL
;
240 talloc_free(tmp_ctx
);
241 return NT_STATUS_NO_MEMORY
;
245 rtn
= ldb_msg_add_string(msg
, "serverReference",libnet_r
->out
.account_dn_str
);
246 if (rtn
!= LDB_SUCCESS
) {
247 libnet_r
->out
.error_string
= NULL
;
248 talloc_free(tmp_ctx
);
249 return NT_STATUS_NO_MEMORY
;
252 /* mark all the message elements (should be just one)
253 as LDB_FLAG_MOD_REPLACE */
254 for (i
=0;i
<msg
->num_elements
;i
++) {
255 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
258 rtn
= ldb_modify(remote_ldb
, msg
);
259 if (rtn
!= LDB_SUCCESS
) {
260 libnet_r
->out
.error_string
261 = talloc_asprintf(libnet_r
,
262 "Failed to modify server entry %s: %s: %d",
264 ldb_errstring(remote_ldb
), rtn
);
265 talloc_free(tmp_ctx
);
266 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
268 } else if (rtn
!= LDB_SUCCESS
) {
269 libnet_r
->out
.error_string
270 = talloc_asprintf(libnet_r
,
271 "Failed to add server entry %s: %s: %d",
272 server_dn_str
, ldb_errstring(remote_ldb
),
274 talloc_free(tmp_ctx
);
275 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
277 DEBUG(0, ("We still need to perform a DsAddEntry() so that we can create the CN=NTDS Settings container.\n"));
279 /* Store the server DN in libnet_r */
280 libnet_r
->out
.server_dn_str
= server_dn_str
;
281 talloc_steal(libnet_r
, server_dn_str
);
283 talloc_free(tmp_ctx
);