libsmb: Remove unused setup_stat_from_stat_ex()
[Samba.git] / source4 / libnet / libnet_site.c
blob691e4b243acb1f3df5309c28fefed9cbc512f1f7
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 <ldb.h>
24 #include <ldb_errors.h>
25 #include "libcli/resolve/resolve.h"
26 #include "param/param.h"
27 #include "lib/tsocket/tsocket.h"
29 /**
30 * 1. Setup a CLDAP socket.
31 * 2. Lookup the default Site-Name.
33 NTSTATUS libnet_FindSite(TALLOC_CTX *ctx, struct libnet_context *lctx, struct libnet_JoinSite *r)
35 NTSTATUS status;
36 TALLOC_CTX *tmp_ctx;
38 char *site_name_str;
39 char *config_dn_str;
40 char *server_dn_str;
42 struct cldap_socket *cldap = NULL;
43 struct cldap_netlogon search;
44 int ret;
45 struct tsocket_address *dest_address;
47 tmp_ctx = talloc_named(ctx, 0, "libnet_FindSite temp context");
48 if (!tmp_ctx) {
49 r->out.error_string = NULL;
50 return NT_STATUS_NO_MEMORY;
53 /* Resolve the site name. */
54 ZERO_STRUCT(search);
55 search.in.dest_address = NULL;
56 search.in.dest_port = 0;
57 search.in.acct_control = -1;
58 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
59 search.in.map_response = true;
61 ret = tsocket_address_inet_from_strings(tmp_ctx, "ip",
62 r->in.dest_address,
63 r->in.cldap_port,
64 &dest_address);
65 if (ret != 0) {
66 r->out.error_string = NULL;
67 status = map_nt_error_from_unix_common(errno);
68 talloc_free(tmp_ctx);
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, 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, tmp_ctx, &search);
80 if (!NT_STATUS_IS_OK(status)
81 || search.out.netlogon.data.nt5_ex.client_site == NULL
82 || search.out.netlogon.data.nt5_ex.client_site[0] == '\0') {
84 If cldap_netlogon() returns in error,
85 default to using Default-First-Site-Name.
87 site_name_str = talloc_asprintf(tmp_ctx, "%s",
88 "Default-First-Site-Name");
89 if (!site_name_str) {
90 r->out.error_string = NULL;
91 talloc_free(tmp_ctx);
92 return NT_STATUS_NO_MEMORY;
94 } else {
95 site_name_str = talloc_asprintf(tmp_ctx, "%s",
96 search.out.netlogon.data.nt5_ex.client_site);
97 if (!site_name_str) {
98 r->out.error_string = NULL;
99 talloc_free(tmp_ctx);
100 return NT_STATUS_NO_MEMORY;
104 /* Generate the CN=Configuration,... DN. */
105 /* TODO: look it up! */
106 config_dn_str = talloc_asprintf(tmp_ctx, "CN=Configuration,%s", r->in.domain_dn_str);
107 if (!config_dn_str) {
108 r->out.error_string = NULL;
109 talloc_free(tmp_ctx);
110 return NT_STATUS_NO_MEMORY;
113 /* Generate the CN=Servers,... DN. */
114 server_dn_str = talloc_asprintf(tmp_ctx, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
115 r->in.netbios_name, site_name_str, config_dn_str);
116 if (!server_dn_str) {
117 r->out.error_string = NULL;
118 talloc_free(tmp_ctx);
119 return NT_STATUS_NO_MEMORY;
122 r->out.site_name_str = site_name_str;
123 talloc_steal(r, site_name_str);
125 r->out.config_dn_str = config_dn_str;
126 talloc_steal(r, config_dn_str);
128 r->out.server_dn_str = server_dn_str;
129 talloc_steal(r, server_dn_str);
131 talloc_free(tmp_ctx);
132 return NT_STATUS_OK;
136 * find out Site specific stuff:
137 * 1. Lookup the Site name.
138 * 2. Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
139 * TODO: 3.) use DsAddEntry() to create CN=NTDS Settings,CN=<netbios name>,CN=Servers,CN=<site name>,...
141 NTSTATUS libnet_JoinSite(struct libnet_context *ctx,
142 struct ldb_context *remote_ldb,
143 struct libnet_JoinDomain *libnet_r)
145 NTSTATUS status;
146 TALLOC_CTX *tmp_ctx;
148 struct libnet_JoinSite *r;
150 struct ldb_dn *server_dn;
151 struct ldb_message *msg;
152 int rtn;
154 const char *server_dn_str;
155 const char *host;
156 struct nbt_name name;
157 const char *dest_addr = NULL;
159 tmp_ctx = talloc_named(libnet_r, 0, "libnet_JoinSite temp context");
160 if (!tmp_ctx) {
161 libnet_r->out.error_string = NULL;
162 return NT_STATUS_NO_MEMORY;
165 r = talloc(tmp_ctx, struct libnet_JoinSite);
166 if (!r) {
167 libnet_r->out.error_string = NULL;
168 talloc_free(tmp_ctx);
169 return NT_STATUS_NO_MEMORY;
172 host = dcerpc_binding_get_string_option(libnet_r->out.samr_binding, "host");
173 make_nbt_name_client(&name, host);
174 status = resolve_name_ex(lpcfg_resolve_context(ctx->lp_ctx),
175 0, 0,
176 &name, r, &dest_addr, ctx->event_ctx);
177 if (!NT_STATUS_IS_OK(status)) {
178 libnet_r->out.error_string = NULL;
179 talloc_free(tmp_ctx);
180 return status;
183 /* Resolve the site name and AD DN's. */
184 r->in.dest_address = dest_addr;
185 r->in.netbios_name = libnet_r->in.netbios_name;
186 r->in.domain_dn_str = libnet_r->out.domain_dn_str;
187 r->in.cldap_port = lpcfg_cldap_port(ctx->lp_ctx);
189 status = libnet_FindSite(tmp_ctx, ctx, r);
190 if (!NT_STATUS_IS_OK(status)) {
191 libnet_r->out.error_string =
192 talloc_steal(libnet_r, r->out.error_string);
193 talloc_free(tmp_ctx);
194 return status;
197 server_dn_str = r->out.server_dn_str;
200 Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
202 msg = ldb_msg_new(tmp_ctx);
203 if (!msg) {
204 libnet_r->out.error_string = NULL;
205 talloc_free(tmp_ctx);
206 return NT_STATUS_NO_MEMORY;
209 rtn = ldb_msg_add_string(msg, "objectClass", "server");
210 if (rtn != LDB_SUCCESS) {
211 libnet_r->out.error_string = NULL;
212 talloc_free(tmp_ctx);
213 return NT_STATUS_NO_MEMORY;
215 rtn = ldb_msg_add_string(msg, "systemFlags", "50000000");
216 if (rtn != LDB_SUCCESS) {
217 libnet_r->out.error_string = NULL;
218 talloc_free(tmp_ctx);
219 return NT_STATUS_NO_MEMORY;
221 rtn = ldb_msg_add_string(msg, "serverReference", libnet_r->out.account_dn_str);
222 if (rtn != LDB_SUCCESS) {
223 libnet_r->out.error_string = NULL;
224 talloc_free(tmp_ctx);
225 return NT_STATUS_NO_MEMORY;
228 server_dn = ldb_dn_new(tmp_ctx, remote_ldb, server_dn_str);
229 if ( ! ldb_dn_validate(server_dn)) {
230 libnet_r->out.error_string = talloc_asprintf(libnet_r,
231 "Invalid server dn: %s",
232 server_dn_str);
233 talloc_free(tmp_ctx);
234 return NT_STATUS_UNSUCCESSFUL;
237 msg->dn = server_dn;
239 rtn = ldb_add(remote_ldb, msg);
240 if (rtn == LDB_ERR_ENTRY_ALREADY_EXISTS) {
241 unsigned int i;
243 /* make a 'modify' msg, and only for serverReference */
244 msg = ldb_msg_new(tmp_ctx);
245 if (!msg) {
246 libnet_r->out.error_string = NULL;
247 talloc_free(tmp_ctx);
248 return NT_STATUS_NO_MEMORY;
250 msg->dn = server_dn;
252 rtn = ldb_msg_add_string(msg, "serverReference",libnet_r->out.account_dn_str);
253 if (rtn != LDB_SUCCESS) {
254 libnet_r->out.error_string = NULL;
255 talloc_free(tmp_ctx);
256 return NT_STATUS_NO_MEMORY;
259 /* mark all the message elements (should be just one)
260 as LDB_FLAG_MOD_REPLACE */
261 for (i=0;i<msg->num_elements;i++) {
262 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
265 rtn = ldb_modify(remote_ldb, msg);
266 if (rtn != LDB_SUCCESS) {
267 libnet_r->out.error_string
268 = talloc_asprintf(libnet_r,
269 "Failed to modify server entry %s: %s: %d",
270 server_dn_str,
271 ldb_errstring(remote_ldb), rtn);
272 talloc_free(tmp_ctx);
273 return NT_STATUS_INTERNAL_DB_CORRUPTION;
275 } else if (rtn != LDB_SUCCESS) {
276 libnet_r->out.error_string
277 = talloc_asprintf(libnet_r,
278 "Failed to add server entry %s: %s: %d",
279 server_dn_str, ldb_errstring(remote_ldb),
280 rtn);
281 talloc_free(tmp_ctx);
282 return NT_STATUS_INTERNAL_DB_CORRUPTION;
284 DEBUG(0, ("We still need to perform a DsAddEntry() so that we can create the CN=NTDS Settings container.\n"));
286 /* Store the server DN in libnet_r */
287 libnet_r->out.server_dn_str = server_dn_str;
288 talloc_steal(libnet_r, server_dn_str);
290 talloc_free(tmp_ctx);
291 return NT_STATUS_OK;