use unsigned instead of uint32_t for LDB counters.
[Samba/cd1.git] / source4 / libnet / libnet_site.c
blob410fb4b517ca3b44fc24ab8469210c301d378bd8
1 /*
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/>.
20 #include "includes.h"
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"
28 #include "lib/tsocket/tsocket.h"
30 /**
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)
36 NTSTATUS status;
37 TALLOC_CTX *tmp_ctx;
39 char *site_name_str;
40 char *config_dn_str;
41 char *server_dn_str;
43 struct cldap_socket *cldap = NULL;
44 struct cldap_netlogon search;
45 int ret;
46 struct tsocket_address *dest_address;
48 tmp_ctx = talloc_named(ctx, 0, "libnet_FindSite temp context");
49 if (!tmp_ctx) {
50 r->out.error_string = NULL;
51 return NT_STATUS_NO_MEMORY;
54 /* Resolve the site name. */
55 ZERO_STRUCT(search);
56 search.in.dest_address = NULL;
57 search.in.dest_port = 0;
58 search.in.acct_control = -1;
59 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
60 search.in.map_response = true;
62 ret = tsocket_address_inet_from_strings(tmp_ctx, "ip",
63 r->in.dest_address,
64 r->in.cldap_port,
65 &dest_address);
66 if (ret != 0) {
67 r->out.error_string = NULL;
68 status = map_nt_error_from_unix(errno);
69 return status;
72 /* we want to use non async calls, so we're not passing an event context */
73 status = cldap_socket_init(tmp_ctx, NULL, NULL, dest_address, &cldap);
74 if (!NT_STATUS_IS_OK(status)) {
75 talloc_free(tmp_ctx);
76 r->out.error_string = NULL;
77 return status;
79 status = cldap_netlogon(cldap, lp_iconv_convenience(lctx->lp_ctx), tmp_ctx, &search);
80 if (!NT_STATUS_IS_OK(status)
81 || !search.out.netlogon.data.nt5_ex.client_site) {
83 If cldap_netlogon() returns in error,
84 default to using Default-First-Site-Name.
86 site_name_str = talloc_asprintf(tmp_ctx, "%s",
87 "Default-First-Site-Name");
88 if (!site_name_str) {
89 r->out.error_string = NULL;
90 talloc_free(tmp_ctx);
91 return NT_STATUS_NO_MEMORY;
93 } else {
94 site_name_str = talloc_asprintf(tmp_ctx, "%s",
95 search.out.netlogon.data.nt5_ex.client_site);
96 if (!site_name_str) {
97 r->out.error_string = NULL;
98 talloc_free(tmp_ctx);
99 return NT_STATUS_NO_MEMORY;
103 /* Generate the CN=Configuration,... DN. */
104 /* TODO: look it up! */
105 config_dn_str = talloc_asprintf(tmp_ctx, "CN=Configuration,%s", r->in.domain_dn_str);
106 if (!config_dn_str) {
107 r->out.error_string = NULL;
108 talloc_free(tmp_ctx);
109 return NT_STATUS_NO_MEMORY;
112 /* Generate the CN=Servers,... DN. */
113 server_dn_str = talloc_asprintf(tmp_ctx, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
114 r->in.netbios_name, site_name_str, config_dn_str);
115 if (!server_dn_str) {
116 r->out.error_string = NULL;
117 talloc_free(tmp_ctx);
118 return NT_STATUS_NO_MEMORY;
121 r->out.site_name_str = site_name_str;
122 talloc_steal(r, site_name_str);
124 r->out.config_dn_str = config_dn_str;
125 talloc_steal(r, config_dn_str);
127 r->out.server_dn_str = server_dn_str;
128 talloc_steal(r, server_dn_str);
130 talloc_free(tmp_ctx);
131 return NT_STATUS_OK;
135 * find out Site specific stuff:
136 * 1. Lookup the Site name.
137 * 2. Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
138 * TODO: 3.) use DsAddEntry() to create CN=NTDS Settings,CN=<netbios name>,CN=Servers,CN=<site name>,...
140 NTSTATUS libnet_JoinSite(struct libnet_context *ctx,
141 struct ldb_context *remote_ldb,
142 struct libnet_JoinDomain *libnet_r)
144 NTSTATUS status;
145 TALLOC_CTX *tmp_ctx;
147 struct libnet_JoinSite *r;
149 struct ldb_dn *server_dn;
150 struct ldb_message *msg;
151 int rtn;
153 const char *server_dn_str;
154 const char *config_dn_str;
155 struct nbt_name name;
156 const char *dest_addr = NULL;
158 tmp_ctx = talloc_named(libnet_r, 0, "libnet_JoinSite temp context");
159 if (!tmp_ctx) {
160 libnet_r->out.error_string = NULL;
161 return NT_STATUS_NO_MEMORY;
164 r = talloc(tmp_ctx, struct libnet_JoinSite);
165 if (!r) {
166 libnet_r->out.error_string = NULL;
167 talloc_free(tmp_ctx);
168 return NT_STATUS_NO_MEMORY;
171 make_nbt_name_client(&name, libnet_r->out.samr_binding->host);
172 status = resolve_name(lp_resolve_context(ctx->lp_ctx), &name, r, &dest_addr, ctx->event_ctx);
173 if (!NT_STATUS_IS_OK(status)) {
174 libnet_r->out.error_string = NULL;
175 talloc_free(tmp_ctx);
176 return status;
179 /* Resolve the site name and AD DN's. */
180 r->in.dest_address = dest_addr;
181 r->in.netbios_name = libnet_r->in.netbios_name;
182 r->in.domain_dn_str = libnet_r->out.domain_dn_str;
183 r->in.cldap_port = lp_cldap_port(ctx->lp_ctx);
185 status = libnet_FindSite(tmp_ctx, ctx, r);
186 if (!NT_STATUS_IS_OK(status)) {
187 libnet_r->out.error_string =
188 talloc_steal(libnet_r, r->out.error_string);
189 talloc_free(tmp_ctx);
190 return status;
193 config_dn_str = r->out.config_dn_str;
194 server_dn_str = r->out.server_dn_str;
197 Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
199 msg = ldb_msg_new(tmp_ctx);
200 if (!msg) {
201 libnet_r->out.error_string = NULL;
202 talloc_free(tmp_ctx);
203 return NT_STATUS_NO_MEMORY;
206 rtn = ldb_msg_add_string(msg, "objectClass", "server");
207 if (rtn != 0) {
208 libnet_r->out.error_string = NULL;
209 talloc_free(tmp_ctx);
210 return NT_STATUS_NO_MEMORY;
212 rtn = ldb_msg_add_string(msg, "systemFlags", "50000000");
213 if (rtn != 0) {
214 libnet_r->out.error_string = NULL;
215 talloc_free(tmp_ctx);
216 return NT_STATUS_NO_MEMORY;
218 rtn = ldb_msg_add_string(msg, "serverReference", libnet_r->out.account_dn_str);
219 if (rtn != 0) {
220 libnet_r->out.error_string = NULL;
221 talloc_free(tmp_ctx);
222 return NT_STATUS_NO_MEMORY;
225 server_dn = ldb_dn_new(tmp_ctx, remote_ldb, server_dn_str);
226 if ( ! ldb_dn_validate(server_dn)) {
227 libnet_r->out.error_string = talloc_asprintf(libnet_r,
228 "Invalid server dn: %s",
229 server_dn_str);
230 talloc_free(tmp_ctx);
231 return NT_STATUS_UNSUCCESSFUL;
234 msg->dn = server_dn;
236 rtn = ldb_add(remote_ldb, msg);
237 if (rtn == LDB_ERR_ENTRY_ALREADY_EXISTS) {
238 int i;
240 /* make a 'modify' msg, and only for serverReference */
241 msg = ldb_msg_new(tmp_ctx);
242 if (!msg) {
243 libnet_r->out.error_string = NULL;
244 talloc_free(tmp_ctx);
245 return NT_STATUS_NO_MEMORY;
247 msg->dn = server_dn;
249 rtn = ldb_msg_add_string(msg, "serverReference",libnet_r->out.account_dn_str);
250 if (rtn != 0) {
251 libnet_r->out.error_string = NULL;
252 talloc_free(tmp_ctx);
253 return NT_STATUS_NO_MEMORY;
256 /* mark all the message elements (should be just one)
257 as LDB_FLAG_MOD_REPLACE */
258 for (i=0;i<msg->num_elements;i++) {
259 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
262 rtn = ldb_modify(remote_ldb, msg);
263 if (rtn != 0) {
264 libnet_r->out.error_string
265 = talloc_asprintf(libnet_r,
266 "Failed to modify server entry %s: %s: %d",
267 server_dn_str,
268 ldb_errstring(remote_ldb), rtn);
269 talloc_free(tmp_ctx);
270 return NT_STATUS_INTERNAL_DB_CORRUPTION;
272 } else if (rtn != 0) {
273 libnet_r->out.error_string
274 = talloc_asprintf(libnet_r,
275 "Failed to add server entry %s: %s: %d",
276 server_dn_str, ldb_errstring(remote_ldb),
277 rtn);
278 talloc_free(tmp_ctx);
279 return NT_STATUS_INTERNAL_DB_CORRUPTION;
281 DEBUG(0, ("We still need to perform a DsAddEntry() so that we can create the CN=NTDS Settings container.\n"));
283 /* Store the server DN in libnet_r */
284 libnet_r->out.server_dn_str = server_dn_str;
285 talloc_steal(libnet_r, server_dn_str);
287 talloc_free(tmp_ctx);
288 return NT_STATUS_OK;