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"
29 * 1. Setup a CLDAP socket.
30 * 2. Lookup the default Site-Name.
32 NTSTATUS libnet_FindSite(TALLOC_CTX *ctx, struct libnet_JoinSite *r)
41 struct cldap_socket *cldap = NULL;
42 struct cldap_netlogon search;
44 tmp_ctx = talloc_named(ctx, 0, "libnet_FindSite temp context");
46 r->out.error_string = NULL;
47 return NT_STATUS_NO_MEMORY;
50 /* Resolve the site name. */
52 search.in.dest_address = r->in.dest_address;
53 search.in.acct_control = -1;
54 search.in.version = 6;
56 cldap = cldap_socket_init(tmp_ctx, NULL);
57 status = cldap_netlogon(cldap, tmp_ctx, &search);
58 if (!NT_STATUS_IS_OK(status)) {
60 If cldap_netlogon() returns in error,
61 default to using Default-First-Site-Name.
63 site_name_str = talloc_asprintf(tmp_ctx, "%s",
64 "Default-First-Site-Name");
66 r->out.error_string = NULL;
68 return NT_STATUS_NO_MEMORY;
71 site_name_str = talloc_asprintf(tmp_ctx, "%s",
72 search.out.netlogon.logon5.client_site);
74 r->out.error_string = NULL;
76 return NT_STATUS_NO_MEMORY;
80 /* Generate the CN=Configuration,... DN. */
81 /* TODO: look it up! */
82 config_dn_str = talloc_asprintf(tmp_ctx, "CN=Configuration,%s", r->in.domain_dn_str);
84 r->out.error_string = NULL;
86 return NT_STATUS_NO_MEMORY;
89 /* Generate the CN=Servers,... DN. */
90 server_dn_str = talloc_asprintf(tmp_ctx, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
91 r->in.netbios_name, site_name_str, config_dn_str);
93 r->out.error_string = NULL;
95 return NT_STATUS_NO_MEMORY;
98 r->out.site_name_str = site_name_str;
99 talloc_steal(r, site_name_str);
101 r->out.config_dn_str = config_dn_str;
102 talloc_steal(r, config_dn_str);
104 r->out.server_dn_str = server_dn_str;
105 talloc_steal(r, server_dn_str);
107 talloc_free(tmp_ctx);
112 * find out Site specific stuff:
113 * 1. Lookup the Site name.
114 * 2. Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
115 * TODO: 3.) use DsAddEntry() to create CN=NTDS Settings,CN=<netbios name>,CN=Servers,CN=<site name>,...
117 NTSTATUS libnet_JoinSite(struct ldb_context *remote_ldb,
118 struct libnet_JoinDomain *libnet_r)
123 struct libnet_JoinSite *r;
125 struct ldb_dn *server_dn;
126 struct ldb_message *msg;
129 const char *server_dn_str;
130 const char *config_dn_str;
131 struct nbt_name name;
132 const char *dest_addr = NULL;
134 tmp_ctx = talloc_named(libnet_r, 0, "libnet_JoinSite temp context");
136 libnet_r->out.error_string = NULL;
137 return NT_STATUS_NO_MEMORY;
140 r = talloc(tmp_ctx, struct libnet_JoinSite);
142 libnet_r->out.error_string = NULL;
143 talloc_free(tmp_ctx);
144 return NT_STATUS_NO_MEMORY;
147 make_nbt_name_client(&name, libnet_r->out.samr_binding->host);
148 status = resolve_name(&name, r, &dest_addr, NULL);
149 if (!NT_STATUS_IS_OK(status)) {
150 libnet_r->out.error_string = NULL;
151 talloc_free(tmp_ctx);
155 /* Resolve the site name and AD DN's. */
156 r->in.dest_address = dest_addr;
157 r->in.netbios_name = libnet_r->in.netbios_name;
158 r->in.domain_dn_str = libnet_r->out.domain_dn_str;
160 status = libnet_FindSite(tmp_ctx, r);
161 if (!NT_STATUS_IS_OK(status)) {
162 libnet_r->out.error_string =
163 talloc_steal(libnet_r, r->out.error_string);
164 talloc_free(tmp_ctx);
168 config_dn_str = r->out.config_dn_str;
169 server_dn_str = r->out.server_dn_str;
172 Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
174 msg = ldb_msg_new(tmp_ctx);
176 libnet_r->out.error_string = NULL;
177 talloc_free(tmp_ctx);
178 return NT_STATUS_NO_MEMORY;
181 rtn = ldb_msg_add_string(msg, "objectClass", "server");
183 libnet_r->out.error_string = NULL;
184 talloc_free(tmp_ctx);
185 return NT_STATUS_NO_MEMORY;
187 rtn = ldb_msg_add_string(msg, "systemFlags", "50000000");
189 libnet_r->out.error_string = NULL;
190 talloc_free(tmp_ctx);
191 return NT_STATUS_NO_MEMORY;
193 rtn = ldb_msg_add_string(msg, "serverReference", libnet_r->out.account_dn_str);
195 libnet_r->out.error_string = NULL;
196 talloc_free(tmp_ctx);
197 return NT_STATUS_NO_MEMORY;
200 server_dn = ldb_dn_new(tmp_ctx, remote_ldb, server_dn_str);
201 if ( ! ldb_dn_validate(server_dn)) {
202 libnet_r->out.error_string = talloc_asprintf(libnet_r,
203 "Invalid server dn: %s",
205 talloc_free(tmp_ctx);
206 return NT_STATUS_UNSUCCESSFUL;
211 rtn = ldb_add(remote_ldb, msg);
212 if (rtn == LDB_ERR_ENTRY_ALREADY_EXISTS) {
215 /* make a 'modify' msg, and only for serverReference */
216 msg = ldb_msg_new(tmp_ctx);
218 libnet_r->out.error_string = NULL;
219 talloc_free(tmp_ctx);
220 return NT_STATUS_NO_MEMORY;
224 rtn = ldb_msg_add_string(msg, "serverReference",libnet_r->out.account_dn_str);
226 libnet_r->out.error_string = NULL;
227 talloc_free(tmp_ctx);
228 return NT_STATUS_NO_MEMORY;
231 /* mark all the message elements (should be just one)
232 as LDB_FLAG_MOD_REPLACE */
233 for (i=0;i<msg->num_elements;i++) {
234 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
237 rtn = ldb_modify(remote_ldb, msg);
239 libnet_r->out.error_string
240 = talloc_asprintf(libnet_r,
241 "Failed to modify server entry %s: %s: %d",
243 ldb_errstring(remote_ldb), rtn);
244 talloc_free(tmp_ctx);
245 return NT_STATUS_INTERNAL_DB_CORRUPTION;
247 } else if (rtn != 0) {
248 libnet_r->out.error_string
249 = talloc_asprintf(libnet_r,
250 "Failed to add server entry %s: %s: %d",
251 server_dn_str, ldb_errstring(remote_ldb),
253 talloc_free(tmp_ctx);
254 return NT_STATUS_INTERNAL_DB_CORRUPTION;
256 DEBUG(0, ("We still need to perform a DsAddEntry() so that we can create the CN=NTDS Settings container.\n"));
258 /* Store the server DN in libnet_r */
259 libnet_r->out.server_dn_str = server_dn_str;
260 talloc_steal(libnet_r, server_dn_str);
262 talloc_free(tmp_ctx);