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