r20949: Looking over some lcov output, try and walk some error paths.
[Samba.git] / source / libnet / libnet_site.c
blob51b13f9838e227521b5ccc666a8ddf604f1c99a6
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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"
29 * 1. Setup a CLDAP socket.
30 * 2. Lookup the default Site-Name.
32 NTSTATUS libnet_FindSite(TALLOC_CTX *ctx, struct libnet_JoinSite *r)
34 NTSTATUS status;
35 TALLOC_CTX *tmp_ctx;
37 char *site_name_str;
38 char *config_dn_str;
39 char *server_dn_str;
41 struct cldap_socket *cldap = NULL;
42 struct cldap_netlogon search;
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;
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;
56 cldap = cldap_socket_init(tmp_ctx, NULL);
57 status = cldap_netlogon(cldap, tmp_ctx, &search);
58 if (!NT_STATUS_IS_OK(status)) {
60 If cldap_netlogon() returns in error,
61 default to using Default-First-Site-Name.
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;
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;
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;
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;
98 r->out.site_name_str = site_name_str;
99 talloc_steal(r, site_name_str);
101 r->out.config_dn_str = config_dn_str;
102 talloc_steal(r, config_dn_str);
104 r->out.server_dn_str = server_dn_str;
105 talloc_steal(r, server_dn_str);
107 talloc_free(tmp_ctx);
108 return NT_STATUS_OK;
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>,...
117 NTSTATUS libnet_JoinSite(struct ldb_context *remote_ldb,
118 struct libnet_JoinDomain *libnet_r)
120 NTSTATUS status;
121 TALLOC_CTX *tmp_ctx;
123 struct libnet_JoinSite *r;
125 struct ldb_dn *server_dn;
126 struct ldb_message *msg;
127 int rtn;
129 const char *server_dn_str;
130 const char *config_dn_str;
132 tmp_ctx = talloc_named(libnet_r, 0, "libnet_JoinSite temp context");
133 if (!tmp_ctx) {
134 libnet_r->out.error_string = NULL;
135 return NT_STATUS_NO_MEMORY;
138 r = talloc(tmp_ctx, struct libnet_JoinSite);
139 if (!r) {
140 libnet_r->out.error_string = NULL;
141 talloc_free(tmp_ctx);
142 return NT_STATUS_NO_MEMORY;
145 /* Resolve the site name and AD DN's. */
146 r->in.dest_address = libnet_r->out.samr_binding->host;
147 r->in.netbios_name = libnet_r->in.netbios_name;
148 r->in.domain_dn_str = libnet_r->out.domain_dn_str;
150 status = libnet_FindSite(tmp_ctx, r);
151 if (!NT_STATUS_IS_OK(status)) {
152 libnet_r->out.error_string =
153 talloc_steal(libnet_r, r->out.error_string);
154 talloc_free(tmp_ctx);
155 return NT_STATUS_NO_MEMORY;
158 config_dn_str = r->out.config_dn_str;
159 server_dn_str = r->out.server_dn_str;
162 Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
164 msg = ldb_msg_new(tmp_ctx);
165 if (!msg) {
166 libnet_r->out.error_string = NULL;
167 talloc_free(tmp_ctx);
168 return NT_STATUS_NO_MEMORY;
171 rtn = ldb_msg_add_string(msg, "objectClass", "server");
172 if (rtn != 0) {
173 libnet_r->out.error_string = NULL;
174 talloc_free(tmp_ctx);
175 return NT_STATUS_NO_MEMORY;
177 rtn = ldb_msg_add_string(msg, "systemFlags", "50000000");
178 if (rtn != 0) {
179 libnet_r->out.error_string = NULL;
180 talloc_free(tmp_ctx);
181 return NT_STATUS_NO_MEMORY;
183 rtn = ldb_msg_add_string(msg, "serverReference", libnet_r->out.account_dn_str);
184 if (rtn != 0) {
185 libnet_r->out.error_string = NULL;
186 talloc_free(tmp_ctx);
187 return NT_STATUS_NO_MEMORY;
190 server_dn = ldb_dn_new(tmp_ctx, remote_ldb, server_dn_str);
191 if ( ! ldb_dn_validate(server_dn)) {
192 libnet_r->out.error_string = talloc_asprintf(libnet_r,
193 "Invalid server dn: %s",
194 server_dn_str);
195 talloc_free(tmp_ctx);
196 return NT_STATUS_UNSUCCESSFUL;
199 msg->dn = server_dn;
201 rtn = ldb_add(remote_ldb, msg);
202 if (rtn == LDB_ERR_ENTRY_ALREADY_EXISTS) {
203 int i;
205 /* make a 'modify' msg, and only for serverReference */
206 msg = ldb_msg_new(tmp_ctx);
207 if (!msg) {
208 libnet_r->out.error_string = NULL;
209 talloc_free(tmp_ctx);
210 return NT_STATUS_NO_MEMORY;
212 msg->dn = server_dn;
214 rtn = ldb_msg_add_string(msg, "serverReference",libnet_r->out.account_dn_str);
215 if (rtn != 0) {
216 libnet_r->out.error_string = NULL;
217 talloc_free(tmp_ctx);
218 return NT_STATUS_NO_MEMORY;
221 /* mark all the message elements (should be just one)
222 as LDB_FLAG_MOD_REPLACE */
223 for (i=0;i<msg->num_elements;i++) {
224 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
227 rtn = ldb_modify(remote_ldb, msg);
228 if (rtn != 0) {
229 libnet_r->out.error_string
230 = talloc_asprintf(libnet_r,
231 "Failed to modify server entry %s: %s: %d",
232 server_dn_str,
233 ldb_errstring(remote_ldb), rtn);
234 talloc_free(tmp_ctx);
235 return NT_STATUS_INTERNAL_DB_CORRUPTION;
237 } else if (rtn != 0) {
238 libnet_r->out.error_string
239 = talloc_asprintf(libnet_r,
240 "Failed to add server entry %s: %s: %d",
241 server_dn_str, ldb_errstring(remote_ldb),
242 rtn);
243 talloc_free(tmp_ctx);
244 return NT_STATUS_INTERNAL_DB_CORRUPTION;
246 DEBUG(0, ("We still need to perform a DsAddEntry() so that we can create the CN=NTDS Settings container.\n"));
248 /* Store the server DN in libnet_r */
249 libnet_r->out.server_dn_str = server_dn_str;
250 talloc_steal(libnet_r, server_dn_str);
252 talloc_free(tmp_ctx);
253 return NT_STATUS_OK;