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 "lib/ldb/include/ldb.h"
24 #include "lib/ldb/include/ldb_errors.h"
25 #include "librpc/rpc/dcerpc.h"
26 #include "libcli/resolve/resolve.h"
27 #include "param/param.h"
30 * 1. Setup a CLDAP socket.
31 * 2. Lookup the default Site-Name.
33 NTSTATUS libnet_FindSite(TALLOC_CTX *ctx, struct libnet_JoinSite *r)
42 struct cldap_socket *cldap = NULL;
43 struct cldap_netlogon search;
45 tmp_ctx = talloc_named(ctx, 0, "libnet_FindSite temp context");
47 r->out.error_string = NULL;
48 return NT_STATUS_NO_MEMORY;
51 /* Resolve the site name. */
53 search.in.dest_address = r->in.dest_address;
54 search.in.dest_port = lp_cldap_port(global_loadparm);
55 search.in.acct_control = -1;
56 search.in.version = 6;
58 cldap = cldap_socket_init(tmp_ctx, NULL);
59 status = cldap_netlogon(cldap, tmp_ctx, &search);
60 if (!NT_STATUS_IS_OK(status)) {
62 If cldap_netlogon() returns in error,
63 default to using Default-First-Site-Name.
65 site_name_str = talloc_asprintf(tmp_ctx, "%s",
66 "Default-First-Site-Name");
68 r->out.error_string = NULL;
70 return NT_STATUS_NO_MEMORY;
73 site_name_str = talloc_asprintf(tmp_ctx, "%s",
74 search.out.netlogon.logon5.client_site);
76 r->out.error_string = NULL;
78 return NT_STATUS_NO_MEMORY;
82 /* Generate the CN=Configuration,... DN. */
83 /* TODO: look it up! */
84 config_dn_str = talloc_asprintf(tmp_ctx, "CN=Configuration,%s", r->in.domain_dn_str);
86 r->out.error_string = NULL;
88 return NT_STATUS_NO_MEMORY;
91 /* Generate the CN=Servers,... DN. */
92 server_dn_str = talloc_asprintf(tmp_ctx, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
93 r->in.netbios_name, site_name_str, config_dn_str);
95 r->out.error_string = NULL;
97 return NT_STATUS_NO_MEMORY;
100 r->out.site_name_str = site_name_str;
101 talloc_steal(r, site_name_str);
103 r->out.config_dn_str = config_dn_str;
104 talloc_steal(r, config_dn_str);
106 r->out.server_dn_str = server_dn_str;
107 talloc_steal(r, server_dn_str);
109 talloc_free(tmp_ctx);
114 * find out Site specific stuff:
115 * 1. Lookup the Site name.
116 * 2. Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
117 * TODO: 3.) use DsAddEntry() to create CN=NTDS Settings,CN=<netbios name>,CN=Servers,CN=<site name>,...
119 NTSTATUS libnet_JoinSite(struct ldb_context *remote_ldb,
120 struct libnet_JoinDomain *libnet_r)
125 struct libnet_JoinSite *r;
127 struct ldb_dn *server_dn;
128 struct ldb_message *msg;
131 const char *server_dn_str;
132 const char *config_dn_str;
133 struct nbt_name name;
134 const char *dest_addr = NULL;
136 tmp_ctx = talloc_named(libnet_r, 0, "libnet_JoinSite temp context");
138 libnet_r->out.error_string = NULL;
139 return NT_STATUS_NO_MEMORY;
142 r = talloc(tmp_ctx, struct libnet_JoinSite);
144 libnet_r->out.error_string = NULL;
145 talloc_free(tmp_ctx);
146 return NT_STATUS_NO_MEMORY;
149 make_nbt_name_client(&name, libnet_r->out.samr_binding->host);
150 status = resolve_name(lp_resolve_context(global_loadparm), &name, r, &dest_addr, NULL);
151 if (!NT_STATUS_IS_OK(status)) {
152 libnet_r->out.error_string = NULL;
153 talloc_free(tmp_ctx);
157 /* Resolve the site name and AD DN's. */
158 r->in.dest_address = dest_addr;
159 r->in.netbios_name = libnet_r->in.netbios_name;
160 r->in.domain_dn_str = libnet_r->out.domain_dn_str;
162 status = libnet_FindSite(tmp_ctx, r);
163 if (!NT_STATUS_IS_OK(status)) {
164 libnet_r->out.error_string =
165 talloc_steal(libnet_r, r->out.error_string);
166 talloc_free(tmp_ctx);
170 config_dn_str = r->out.config_dn_str;
171 server_dn_str = r->out.server_dn_str;
174 Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
176 msg = ldb_msg_new(tmp_ctx);
178 libnet_r->out.error_string = NULL;
179 talloc_free(tmp_ctx);
180 return NT_STATUS_NO_MEMORY;
183 rtn = ldb_msg_add_string(msg, "objectClass", "server");
185 libnet_r->out.error_string = NULL;
186 talloc_free(tmp_ctx);
187 return NT_STATUS_NO_MEMORY;
189 rtn = ldb_msg_add_string(msg, "systemFlags", "50000000");
191 libnet_r->out.error_string = NULL;
192 talloc_free(tmp_ctx);
193 return NT_STATUS_NO_MEMORY;
195 rtn = ldb_msg_add_string(msg, "serverReference", libnet_r->out.account_dn_str);
197 libnet_r->out.error_string = NULL;
198 talloc_free(tmp_ctx);
199 return NT_STATUS_NO_MEMORY;
202 server_dn = ldb_dn_new(tmp_ctx, remote_ldb, server_dn_str);
203 if ( ! ldb_dn_validate(server_dn)) {
204 libnet_r->out.error_string = talloc_asprintf(libnet_r,
205 "Invalid server dn: %s",
207 talloc_free(tmp_ctx);
208 return NT_STATUS_UNSUCCESSFUL;
213 rtn = ldb_add(remote_ldb, msg);
214 if (rtn == LDB_ERR_ENTRY_ALREADY_EXISTS) {
217 /* make a 'modify' msg, and only for serverReference */
218 msg = ldb_msg_new(tmp_ctx);
220 libnet_r->out.error_string = NULL;
221 talloc_free(tmp_ctx);
222 return NT_STATUS_NO_MEMORY;
226 rtn = ldb_msg_add_string(msg, "serverReference",libnet_r->out.account_dn_str);
228 libnet_r->out.error_string = NULL;
229 talloc_free(tmp_ctx);
230 return NT_STATUS_NO_MEMORY;
233 /* mark all the message elements (should be just one)
234 as LDB_FLAG_MOD_REPLACE */
235 for (i=0;i<msg->num_elements;i++) {
236 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
239 rtn = ldb_modify(remote_ldb, msg);
241 libnet_r->out.error_string
242 = talloc_asprintf(libnet_r,
243 "Failed to modify server entry %s: %s: %d",
245 ldb_errstring(remote_ldb), rtn);
246 talloc_free(tmp_ctx);
247 return NT_STATUS_INTERNAL_DB_CORRUPTION;
249 } else if (rtn != 0) {
250 libnet_r->out.error_string
251 = talloc_asprintf(libnet_r,
252 "Failed to add server entry %s: %s: %d",
253 server_dn_str, ldb_errstring(remote_ldb),
255 talloc_free(tmp_ctx);
256 return NT_STATUS_INTERNAL_DB_CORRUPTION;
258 DEBUG(0, ("We still need to perform a DsAddEntry() so that we can create the CN=NTDS Settings container.\n"));
260 /* Store the server DN in libnet_r */
261 libnet_r->out.server_dn_str = server_dn_str;
262 talloc_steal(libnet_r, server_dn_str);
264 talloc_free(tmp_ctx);