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