Fix build with arc4.
[kai/samba.git] / source4 / libnet / libnet_site.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Brad Henry     2005
5
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.
10
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.
15
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/>.
18 */
19
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
29 /**
30  * 1. Setup a CLDAP socket.
31  * 2. Lookup the default Site-Name.
32  */
33 NTSTATUS libnet_FindSite(TALLOC_CTX *ctx, struct libnet_context *lctx, struct libnet_JoinSite *r)
34 {
35         NTSTATUS status;
36         TALLOC_CTX *tmp_ctx;
37
38         char *site_name_str;
39         char *config_dn_str;
40         char *server_dn_str;
41
42         struct cldap_socket *cldap = NULL;
43         struct cldap_netlogon search;
44
45         tmp_ctx = talloc_named(ctx, 0, "libnet_FindSite temp context");
46         if (!tmp_ctx) {
47                 r->out.error_string = NULL;
48                 return NT_STATUS_NO_MEMORY;
49         }
50
51         /* Resolve the site name. */
52         ZERO_STRUCT(search);
53         search.in.dest_address = r->in.dest_address;
54         search.in.dest_port = r->in.cldap_port;
55         search.in.acct_control = -1;
56         search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
57         search.in.map_response = true;
58
59         cldap = cldap_socket_init(tmp_ctx, lctx->event_ctx, lp_iconv_convenience(global_loadparm));
60         status = cldap_netlogon(cldap, tmp_ctx, &search);
61         if (!NT_STATUS_IS_OK(status) || !search.out.netlogon.nt5_ex.client_site) {
62                 /*
63                   If cldap_netlogon() returns in error,
64                   default to using Default-First-Site-Name.
65                 */
66                 site_name_str = talloc_asprintf(tmp_ctx, "%s",
67                                                 "Default-First-Site-Name");
68                 if (!site_name_str) {
69                         r->out.error_string = NULL;
70                         talloc_free(tmp_ctx);
71                         return NT_STATUS_NO_MEMORY;
72                 }
73         } else {
74                 site_name_str = talloc_asprintf(tmp_ctx, "%s",
75                                         search.out.netlogon.nt5_ex.client_site);
76                 if (!site_name_str) {
77                         r->out.error_string = NULL;
78                         talloc_free(tmp_ctx);
79                         return NT_STATUS_NO_MEMORY;
80                 }
81         }
82
83         /* Generate the CN=Configuration,... DN. */
84 /* TODO: look it up! */
85         config_dn_str = talloc_asprintf(tmp_ctx, "CN=Configuration,%s", r->in.domain_dn_str);
86         if (!config_dn_str) {
87                 r->out.error_string = NULL;
88                 talloc_free(tmp_ctx);
89                 return NT_STATUS_NO_MEMORY;
90         }
91
92         /* Generate the CN=Servers,... DN. */
93         server_dn_str = talloc_asprintf(tmp_ctx, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
94                                                  r->in.netbios_name, site_name_str, config_dn_str);
95         if (!server_dn_str) {
96                 r->out.error_string = NULL;
97                 talloc_free(tmp_ctx);
98                 return NT_STATUS_NO_MEMORY;
99         }
100
101         r->out.site_name_str = site_name_str;
102         talloc_steal(r, site_name_str);
103
104         r->out.config_dn_str = config_dn_str;
105         talloc_steal(r, config_dn_str);
106
107         r->out.server_dn_str = server_dn_str;
108         talloc_steal(r, server_dn_str);
109
110         talloc_free(tmp_ctx);
111         return NT_STATUS_OK;
112 }
113
114 /*
115  * find out Site specific stuff:
116  * 1. Lookup the Site name.
117  * 2. Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
118  * TODO: 3.) use DsAddEntry() to create CN=NTDS Settings,CN=<netbios name>,CN=Servers,CN=<site name>,...
119  */
120 NTSTATUS libnet_JoinSite(struct libnet_context *ctx, 
121                          struct ldb_context *remote_ldb,
122                          struct libnet_JoinDomain *libnet_r)
123 {
124         NTSTATUS status;
125         TALLOC_CTX *tmp_ctx;
126
127         struct libnet_JoinSite *r;
128
129         struct ldb_dn *server_dn;
130         struct ldb_message *msg;
131         int rtn;
132
133         const char *server_dn_str;
134         const char *config_dn_str;
135         struct nbt_name name;
136         const char *dest_addr = NULL;
137
138         tmp_ctx = talloc_named(libnet_r, 0, "libnet_JoinSite temp context");
139         if (!tmp_ctx) {
140                 libnet_r->out.error_string = NULL;
141                 return NT_STATUS_NO_MEMORY;
142         }
143
144         r = talloc(tmp_ctx, struct libnet_JoinSite);
145         if (!r) {
146                 libnet_r->out.error_string = NULL;
147                 talloc_free(tmp_ctx);
148                 return NT_STATUS_NO_MEMORY;
149         }
150
151         make_nbt_name_client(&name, libnet_r->out.samr_binding->host);
152         status = resolve_name(lp_resolve_context(ctx->lp_ctx), &name, r, &dest_addr, ctx->event_ctx);
153         if (!NT_STATUS_IS_OK(status)) {
154                 libnet_r->out.error_string = NULL;
155                 talloc_free(tmp_ctx);
156                 return status;
157         }
158
159         /* Resolve the site name and AD DN's. */
160         r->in.dest_address = dest_addr;
161         r->in.netbios_name = libnet_r->in.netbios_name;
162         r->in.domain_dn_str = libnet_r->out.domain_dn_str;
163         r->in.cldap_port = lp_cldap_port(ctx->lp_ctx);
164
165         status = libnet_FindSite(tmp_ctx, ctx, r);
166         if (!NT_STATUS_IS_OK(status)) {
167                 libnet_r->out.error_string =
168                         talloc_steal(libnet_r, r->out.error_string);
169                 talloc_free(tmp_ctx);
170                 return status;
171         }
172
173         config_dn_str = r->out.config_dn_str;
174         server_dn_str = r->out.server_dn_str;
175
176         /*
177          Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
178         */
179         msg = ldb_msg_new(tmp_ctx);
180         if (!msg) {
181                 libnet_r->out.error_string = NULL;
182                 talloc_free(tmp_ctx);
183                 return NT_STATUS_NO_MEMORY;
184         }
185
186         rtn = ldb_msg_add_string(msg, "objectClass", "server");
187         if (rtn != 0) {
188                 libnet_r->out.error_string = NULL;
189                 talloc_free(tmp_ctx);
190                 return NT_STATUS_NO_MEMORY;
191         }
192         rtn = ldb_msg_add_string(msg, "systemFlags", "50000000");
193         if (rtn != 0) {
194                 libnet_r->out.error_string = NULL;
195                 talloc_free(tmp_ctx);
196                 return NT_STATUS_NO_MEMORY;
197         }
198         rtn = ldb_msg_add_string(msg, "serverReference", libnet_r->out.account_dn_str);
199         if (rtn != 0) {
200                 libnet_r->out.error_string = NULL;
201                 talloc_free(tmp_ctx);
202                 return NT_STATUS_NO_MEMORY;
203         }
204
205         server_dn = ldb_dn_new(tmp_ctx, remote_ldb, server_dn_str);
206         if ( ! ldb_dn_validate(server_dn)) {
207                 libnet_r->out.error_string = talloc_asprintf(libnet_r,
208                                         "Invalid server dn: %s",
209                                         server_dn_str);
210                 talloc_free(tmp_ctx);
211                 return NT_STATUS_UNSUCCESSFUL;
212         }
213
214         msg->dn = server_dn;
215
216         rtn = ldb_add(remote_ldb, msg);
217         if (rtn == LDB_ERR_ENTRY_ALREADY_EXISTS) {
218                 int i;
219
220                 /* make a 'modify' msg, and only for serverReference */
221                 msg = ldb_msg_new(tmp_ctx);
222                 if (!msg) {
223                         libnet_r->out.error_string = NULL;
224                         talloc_free(tmp_ctx);
225                         return NT_STATUS_NO_MEMORY;
226                 }
227                 msg->dn = server_dn;
228
229                 rtn = ldb_msg_add_string(msg, "serverReference",libnet_r->out.account_dn_str);
230                 if (rtn != 0) {
231                         libnet_r->out.error_string = NULL;
232                         talloc_free(tmp_ctx);
233                         return NT_STATUS_NO_MEMORY;
234                 }
235
236                 /* mark all the message elements (should be just one)
237                    as LDB_FLAG_MOD_REPLACE */
238                 for (i=0;i<msg->num_elements;i++) {
239                         msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
240                 }
241
242                 rtn = ldb_modify(remote_ldb, msg);
243                 if (rtn != 0) {
244                         libnet_r->out.error_string
245                                 = talloc_asprintf(libnet_r,
246                                                   "Failed to modify server entry %s: %s: %d",
247                                                   server_dn_str,
248                                                   ldb_errstring(remote_ldb), rtn);
249                         talloc_free(tmp_ctx);
250                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
251                 }
252         } else if (rtn != 0) {
253                 libnet_r->out.error_string
254                         = talloc_asprintf(libnet_r,
255                                 "Failed to add server entry %s: %s: %d",
256                                 server_dn_str, ldb_errstring(remote_ldb),
257                                 rtn);
258                 talloc_free(tmp_ctx);
259                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
260         }
261         DEBUG(0, ("We still need to perform a DsAddEntry() so that we can create the CN=NTDS Settings container.\n"));
262
263         /* Store the server DN in libnet_r */
264         libnet_r->out.server_dn_str = server_dn_str;
265         talloc_steal(libnet_r, server_dn_str);
266
267         talloc_free(tmp_ctx);
268         return NT_STATUS_OK;
269 }