r26409: Pass smb ports along.
[jelmer/samba4-debian.git] / source / 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_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 = 6;
57
58         cldap = cldap_socket_init(tmp_ctx, NULL);
59         status = cldap_netlogon(cldap, tmp_ctx, &search);
60         if (!NT_STATUS_IS_OK(status)) {
61                 /*
62                   If cldap_netlogon() returns in error,
63                   default to using Default-First-Site-Name.
64                 */
65                 site_name_str = talloc_asprintf(tmp_ctx, "%s",
66                                                 "Default-First-Site-Name");
67                 if (!site_name_str) {
68                         r->out.error_string = NULL;
69                         talloc_free(tmp_ctx);
70                         return NT_STATUS_NO_MEMORY;
71                 }
72         } else {
73                 site_name_str = talloc_asprintf(tmp_ctx, "%s",
74                                         search.out.netlogon.logon5.client_site);
75                 if (!site_name_str) {
76                         r->out.error_string = NULL;
77                         talloc_free(tmp_ctx);
78                         return NT_STATUS_NO_MEMORY;
79                 }
80         }
81
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);
85         if (!config_dn_str) {
86                 r->out.error_string = NULL;
87                 talloc_free(tmp_ctx);
88                 return NT_STATUS_NO_MEMORY;
89         }
90
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);
94         if (!server_dn_str) {
95                 r->out.error_string = NULL;
96                 talloc_free(tmp_ctx);
97                 return NT_STATUS_NO_MEMORY;
98         }
99
100         r->out.site_name_str = site_name_str;
101         talloc_steal(r, site_name_str);
102
103         r->out.config_dn_str = config_dn_str;
104         talloc_steal(r, config_dn_str);
105
106         r->out.server_dn_str = server_dn_str;
107         talloc_steal(r, server_dn_str);
108
109         talloc_free(tmp_ctx);
110         return NT_STATUS_OK;
111 }
112
113 /*
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>,...
118  */
119 NTSTATUS libnet_JoinSite(struct libnet_context *ctx, 
120                          struct ldb_context *remote_ldb,
121                          struct libnet_JoinDomain *libnet_r)
122 {
123         NTSTATUS status;
124         TALLOC_CTX *tmp_ctx;
125
126         struct libnet_JoinSite *r;
127
128         struct ldb_dn *server_dn;
129         struct ldb_message *msg;
130         int rtn;
131
132         const char *server_dn_str;
133         const char *config_dn_str;
134         struct nbt_name name;
135         const char *dest_addr = NULL;
136
137         tmp_ctx = talloc_named(libnet_r, 0, "libnet_JoinSite temp context");
138         if (!tmp_ctx) {
139                 libnet_r->out.error_string = NULL;
140                 return NT_STATUS_NO_MEMORY;
141         }
142
143         r = talloc(tmp_ctx, struct libnet_JoinSite);
144         if (!r) {
145                 libnet_r->out.error_string = NULL;
146                 talloc_free(tmp_ctx);
147                 return NT_STATUS_NO_MEMORY;
148         }
149
150         make_nbt_name_client(&name, libnet_r->out.samr_binding->host);
151         status = resolve_name(lp_resolve_context(ctx->lp_ctx), &name, r, &dest_addr, NULL);
152         if (!NT_STATUS_IS_OK(status)) {
153                 libnet_r->out.error_string = NULL;
154                 talloc_free(tmp_ctx);
155                 return status;
156         }
157
158         /* Resolve the site name and AD DN's. */
159         r->in.dest_address = dest_addr;
160         r->in.netbios_name = libnet_r->in.netbios_name;
161         r->in.domain_dn_str = libnet_r->out.domain_dn_str;
162         r->in.cldap_port = lp_cldap_port(global_loadparm);
163
164         status = libnet_FindSite(tmp_ctx, r);
165         if (!NT_STATUS_IS_OK(status)) {
166                 libnet_r->out.error_string =
167                         talloc_steal(libnet_r, r->out.error_string);
168                 talloc_free(tmp_ctx);
169                 return status;
170         }
171
172         config_dn_str = r->out.config_dn_str;
173         server_dn_str = r->out.server_dn_str;
174
175         /*
176          Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
177         */
178         msg = ldb_msg_new(tmp_ctx);
179         if (!msg) {
180                 libnet_r->out.error_string = NULL;
181                 talloc_free(tmp_ctx);
182                 return NT_STATUS_NO_MEMORY;
183         }
184
185         rtn = ldb_msg_add_string(msg, "objectClass", "server");
186         if (rtn != 0) {
187                 libnet_r->out.error_string = NULL;
188                 talloc_free(tmp_ctx);
189                 return NT_STATUS_NO_MEMORY;
190         }
191         rtn = ldb_msg_add_string(msg, "systemFlags", "50000000");
192         if (rtn != 0) {
193                 libnet_r->out.error_string = NULL;
194                 talloc_free(tmp_ctx);
195                 return NT_STATUS_NO_MEMORY;
196         }
197         rtn = ldb_msg_add_string(msg, "serverReference", libnet_r->out.account_dn_str);
198         if (rtn != 0) {
199                 libnet_r->out.error_string = NULL;
200                 talloc_free(tmp_ctx);
201                 return NT_STATUS_NO_MEMORY;
202         }
203
204         server_dn = ldb_dn_new(tmp_ctx, remote_ldb, server_dn_str);
205         if ( ! ldb_dn_validate(server_dn)) {
206                 libnet_r->out.error_string = talloc_asprintf(libnet_r,
207                                         "Invalid server dn: %s",
208                                         server_dn_str);
209                 talloc_free(tmp_ctx);
210                 return NT_STATUS_UNSUCCESSFUL;
211         }
212
213         msg->dn = server_dn;
214
215         rtn = ldb_add(remote_ldb, msg);
216         if (rtn == LDB_ERR_ENTRY_ALREADY_EXISTS) {
217                 int i;
218
219                 /* make a 'modify' msg, and only for serverReference */
220                 msg = ldb_msg_new(tmp_ctx);
221                 if (!msg) {
222                         libnet_r->out.error_string = NULL;
223                         talloc_free(tmp_ctx);
224                         return NT_STATUS_NO_MEMORY;
225                 }
226                 msg->dn = server_dn;
227
228                 rtn = ldb_msg_add_string(msg, "serverReference",libnet_r->out.account_dn_str);
229                 if (rtn != 0) {
230                         libnet_r->out.error_string = NULL;
231                         talloc_free(tmp_ctx);
232                         return NT_STATUS_NO_MEMORY;
233                 }
234
235                 /* mark all the message elements (should be just one)
236                    as LDB_FLAG_MOD_REPLACE */
237                 for (i=0;i<msg->num_elements;i++) {
238                         msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
239                 }
240
241                 rtn = ldb_modify(remote_ldb, msg);
242                 if (rtn != 0) {
243                         libnet_r->out.error_string
244                                 = talloc_asprintf(libnet_r,
245                                                   "Failed to modify server entry %s: %s: %d",
246                                                   server_dn_str,
247                                                   ldb_errstring(remote_ldb), rtn);
248                         talloc_free(tmp_ctx);
249                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
250                 }
251         } else if (rtn != 0) {
252                 libnet_r->out.error_string
253                         = talloc_asprintf(libnet_r,
254                                 "Failed to add server entry %s: %s: %d",
255                                 server_dn_str, ldb_errstring(remote_ldb),
256                                 rtn);
257                 talloc_free(tmp_ctx);
258                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
259         }
260         DEBUG(0, ("We still need to perform a DsAddEntry() so that we can create the CN=NTDS Settings container.\n"));
261
262         /* Store the server DN in libnet_r */
263         libnet_r->out.server_dn_str = server_dn_str;
264         talloc_steal(libnet_r, server_dn_str);
265
266         talloc_free(tmp_ctx);
267         return NT_STATUS_OK;
268 }