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.
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
)
41 struct cldap_socket
*cldap
= NULL
;
42 struct cldap_netlogon search
;
44 tmp_ctx
= talloc_named(ctx
, 0, "libnet_FindSite temp context");
46 r
->out
.error_string
= NULL
;
47 return NT_STATUS_NO_MEMORY
;
50 /* Resolve the site name. */
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");
66 r
->out
.error_string
= NULL
;
68 return NT_STATUS_NO_MEMORY
;
71 site_name_str
= talloc_asprintf(tmp_ctx
, "%s",
72 search
.out
.netlogon
.logon5
.site_name
);
74 r
->out
.error_string
= NULL
;
76 return NT_STATUS_NO_MEMORY
;
80 /* Generate the CN=Configuration,... DN. */
81 config_dn_str
= talloc_asprintf(tmp_ctx
, "CN=Configuration,%s", r
->in
.domain_dn_str
);
83 r
->out
.error_string
= NULL
;
85 return NT_STATUS_NO_MEMORY
;
88 /* Generate the CN=Servers,... DN. */
89 server_dn_str
= talloc_asprintf(tmp_ctx
, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
90 r
->in
.netbios_name
, site_name_str
, config_dn_str
);
92 r
->out
.error_string
= NULL
;
94 return NT_STATUS_NO_MEMORY
;
97 r
->out
.site_name_str
= site_name_str
;
98 talloc_steal(r
, site_name_str
);
100 r
->out
.config_dn_str
= config_dn_str
;
101 talloc_steal(r
, config_dn_str
);
103 r
->out
.server_dn_str
= server_dn_str
;
104 talloc_steal(r
, server_dn_str
);
106 talloc_free(tmp_ctx
);
111 * find out Site specific stuff:
112 * 1. Lookup the Site name.
113 * 2. Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
114 * TODO: 3.) use DsAddEntry() to create CN=NTDS Settings,CN=<netbios name>,CN=Servers,CN=<site name>,...
116 NTSTATUS
libnet_JoinSite(struct ldb_context
*remote_ldb
,
117 struct libnet_JoinDomain
*libnet_r
)
122 struct libnet_JoinSite
*r
;
124 struct ldb_dn
*server_dn
;
125 struct ldb_message
*msg
;
128 const char *server_dn_str
;
129 const char *config_dn_str
;
131 tmp_ctx
= talloc_named(libnet_r
, 0, "libnet_JoinSite temp context");
133 libnet_r
->out
.error_string
= NULL
;
134 return NT_STATUS_NO_MEMORY
;
137 r
= talloc(tmp_ctx
, struct libnet_JoinSite
);
139 libnet_r
->out
.error_string
= NULL
;
140 talloc_free(tmp_ctx
);
141 return NT_STATUS_NO_MEMORY
;
144 /* Resolve the site name and AD DN's. */
145 r
->in
.dest_address
= libnet_r
->out
.samr_binding
->host
;
146 r
->in
.netbios_name
= libnet_r
->in
.netbios_name
;
147 r
->in
.domain_dn_str
= libnet_r
->out
.domain_dn_str
;
149 status
= libnet_FindSite(tmp_ctx
, r
);
150 if (!NT_STATUS_IS_OK(status
)) {
151 libnet_r
->out
.error_string
=
152 talloc_steal(libnet_r
, r
->out
.error_string
);
153 talloc_free(tmp_ctx
);
154 return NT_STATUS_NO_MEMORY
;
157 config_dn_str
= r
->out
.config_dn_str
;
158 server_dn_str
= r
->out
.server_dn_str
;
161 Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
163 msg
= ldb_msg_new(tmp_ctx
);
165 libnet_r
->out
.error_string
= NULL
;
166 talloc_free(tmp_ctx
);
167 return NT_STATUS_NO_MEMORY
;
170 rtn
= ldb_msg_add_string(msg
, "objectClass", "server");
172 libnet_r
->out
.error_string
= NULL
;
173 talloc_free(tmp_ctx
);
174 return NT_STATUS_NO_MEMORY
;
176 rtn
= ldb_msg_add_string(msg
, "systemFlags", "50000000");
178 libnet_r
->out
.error_string
= NULL
;
179 talloc_free(tmp_ctx
);
180 return NT_STATUS_NO_MEMORY
;
182 rtn
= ldb_msg_add_string(msg
, "serverReference", libnet_r
->out
.account_dn_str
);
184 libnet_r
->out
.error_string
= NULL
;
185 talloc_free(tmp_ctx
);
186 return NT_STATUS_NO_MEMORY
;
189 server_dn
= ldb_dn_explode(tmp_ctx
, server_dn_str
);
190 if (server_dn
== NULL
) {
191 libnet_r
->out
.error_string
= talloc_asprintf(libnet_r
,
192 "Invalid server dn: %s",
194 talloc_free(tmp_ctx
);
195 return NT_STATUS_UNSUCCESSFUL
;
200 rtn
= ldb_add(remote_ldb
, msg
);
201 if (rtn
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
204 /* make a 'modify' msg, and only for serverReference */
205 msg
= ldb_msg_new(tmp_ctx
);
207 libnet_r
->out
.error_string
= NULL
;
208 talloc_free(tmp_ctx
);
209 return NT_STATUS_NO_MEMORY
;
213 rtn
= ldb_msg_add_string(msg
, "serverReference",libnet_r
->out
.account_dn_str
);
215 libnet_r
->out
.error_string
= NULL
;
216 talloc_free(tmp_ctx
);
217 return NT_STATUS_NO_MEMORY
;
220 /* mark all the message elements (should be just one)
221 as LDB_FLAG_MOD_REPLACE */
222 for (i
=0;i
<msg
->num_elements
;i
++) {
223 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
226 rtn
= ldb_modify(remote_ldb
, msg
);
228 libnet_r
->out
.error_string
229 = talloc_asprintf(libnet_r
,
230 "Failed to modify server entry %s: %s: %d",
232 ldb_errstring(remote_ldb
), rtn
);
233 talloc_free(tmp_ctx
);
234 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
236 } else if (rtn
!= 0) {
237 libnet_r
->out
.error_string
238 = talloc_asprintf(libnet_r
,
239 "Failed to add server entry %s: %s: %d",
240 server_dn_str
, ldb_errstring(remote_ldb
),
242 talloc_free(tmp_ctx
);
243 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
245 DEBUG(0, ("We still need to perform a DsAddEntry() so that we can create the CN=NTDS Settings container.\n"));
247 /* Store the server DN in libnet_r */
248 libnet_r
->out
.server_dn_str
= server_dn_str
;
249 talloc_steal(libnet_r
, server_dn_str
);
251 talloc_free(tmp_ctx
);