ldb:ldb tools - remove a superflous "return" (usage internally calls "exit")
[Samba/id10ts.git] / source4 / cldap_server / netlogon.c
blob6e455e46bd7ed76f075a907fc9310fbc987988b2
1 /*
2 Unix SMB/CIFS implementation.
4 CLDAP server - netlogon handling
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include <ldb.h>
25 #include <ldb_errors.h>
26 #include "lib/events/events.h"
27 #include "smbd/service_task.h"
28 #include "cldap_server/cldap_server.h"
29 #include "librpc/gen_ndr/ndr_misc.h"
30 #include "libcli/ldap/ldap_ndr.h"
31 #include "libcli/security/security.h"
32 #include "dsdb/samdb/samdb.h"
33 #include "auth/auth.h"
34 #include "ldb_wrap.h"
35 #include "system/network.h"
36 #include "lib/socket/netif.h"
37 #include "param/param.h"
38 #include "../lib/tsocket/tsocket.h"
39 #include "libds/common/flag_mapping.h"
42 fill in the cldap netlogon union for a given version
44 NTSTATUS fill_netlogon_samlogon_response(struct ldb_context *sam_ctx,
45 TALLOC_CTX *mem_ctx,
46 const char *domain,
47 const char *netbios_domain,
48 struct dom_sid *domain_sid,
49 const char *domain_guid,
50 const char *user,
51 uint32_t acct_control,
52 const char *src_address,
53 uint32_t version,
54 struct loadparm_context *lp_ctx,
55 struct netlogon_samlogon_response *netlogon,
56 bool fill_on_blank_request)
58 const char *dom_attrs[] = {"objectGUID", NULL};
59 const char *none_attrs[] = {NULL};
60 struct ldb_result *dom_res = NULL, *user_res = NULL;
61 int ret;
62 const char **services = lpcfg_server_services(lp_ctx);
63 uint32_t server_type;
64 const char *pdc_name;
65 struct GUID domain_uuid;
66 const char *dns_domain;
67 const char *forest_domain;
68 const char *pdc_dns_name;
69 const char *flatname;
70 const char *server_site;
71 const char *client_site;
72 const char *pdc_ip;
73 struct ldb_dn *domain_dn = NULL;
74 struct interface *ifaces;
75 bool user_known, am_rodc;
76 NTSTATUS status;
78 /* the domain parameter could have an optional trailing "." */
79 if (domain && domain[strlen(domain)-1] == '.') {
80 domain = talloc_strndup(mem_ctx, domain, strlen(domain)-1);
81 NT_STATUS_HAVE_NO_MEMORY(domain);
84 /* Lookup using long or short domainname */
85 if (domain && (strcasecmp_m(domain, lpcfg_dnsdomain(lp_ctx)) == 0)) {
86 domain_dn = ldb_get_default_basedn(sam_ctx);
88 if (netbios_domain && (strcasecmp_m(netbios_domain, lpcfg_sam_name(lp_ctx)) == 0)) {
89 domain_dn = ldb_get_default_basedn(sam_ctx);
91 if (domain_dn) {
92 const char *domain_identifier = domain != NULL ? domain
93 : netbios_domain;
94 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
95 domain_dn, LDB_SCOPE_BASE, dom_attrs,
96 "objectClass=domain");
97 if (ret != LDB_SUCCESS) {
98 DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n",
99 domain_identifier,
100 ldb_dn_get_linearized(domain_dn),
101 ldb_errstring(sam_ctx)));
102 return NT_STATUS_NO_SUCH_DOMAIN;
104 if (dom_res->count != 1) {
105 DEBUG(2,("Error finding domain '%s'/'%s' in sam\n",
106 domain_identifier,
107 ldb_dn_get_linearized(domain_dn)));
108 return NT_STATUS_NO_SUCH_DOMAIN;
112 /* Lookup using GUID or SID */
113 if ((dom_res == NULL) && (domain_guid || domain_sid)) {
114 if (domain_guid) {
115 struct GUID binary_guid;
116 struct ldb_val guid_val;
118 /* By this means, we ensure we don't have funny stuff in the GUID */
120 status = GUID_from_string(domain_guid, &binary_guid);
121 if (!NT_STATUS_IS_OK(status)) {
122 return status;
125 /* And this gets the result into the binary format we want anyway */
126 status = GUID_to_ndr_blob(&binary_guid, mem_ctx, &guid_val);
127 if (!NT_STATUS_IS_OK(status)) {
128 return status;
130 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
131 NULL, LDB_SCOPE_SUBTREE,
132 dom_attrs,
133 "(&(objectCategory=DomainDNS)(objectGUID=%s))",
134 ldb_binary_encode(mem_ctx, guid_val));
135 } else { /* domain_sid case */
136 struct dom_sid *sid;
137 struct ldb_val sid_val;
138 enum ndr_err_code ndr_err;
140 /* Rather than go via the string, just push into the NDR form */
141 ndr_err = ndr_push_struct_blob(&sid_val, mem_ctx, &sid,
142 (ndr_push_flags_fn_t)ndr_push_dom_sid);
143 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
144 return NT_STATUS_INVALID_PARAMETER;
147 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
148 NULL, LDB_SCOPE_SUBTREE,
149 dom_attrs,
150 "(&(objectCategory=DomainDNS)(objectSid=%s))",
151 ldb_binary_encode(mem_ctx, sid_val));
154 if (ret != LDB_SUCCESS) {
155 DEBUG(2,("Unable to find a correct reference to GUID '%s' or SID '%s' in sam: %s\n",
156 domain_guid, dom_sid_string(mem_ctx, domain_sid),
157 ldb_errstring(sam_ctx)));
158 return NT_STATUS_NO_SUCH_DOMAIN;
159 } else if (dom_res->count == 1) {
160 /* Ok, now just check it is our domain */
161 if (ldb_dn_compare(ldb_get_default_basedn(sam_ctx),
162 dom_res->msgs[0]->dn) != 0) {
163 DEBUG(2,("The GUID '%s' or SID '%s' doesn't identify our domain\n",
164 domain_guid,
165 dom_sid_string(mem_ctx, domain_sid)));
166 return NT_STATUS_NO_SUCH_DOMAIN;
168 } else {
169 DEBUG(2,("Unable to find a correct reference to GUID '%s' or SID '%s' in sam\n",
170 domain_guid, dom_sid_string(mem_ctx, domain_sid)));
171 return NT_STATUS_NO_SUCH_DOMAIN;
175 if (dom_res == NULL && fill_on_blank_request) {
176 /* blank inputs gives our domain - tested against
177 w2k8r2. Without this ADUC on Win7 won't start */
178 domain_dn = ldb_get_default_basedn(sam_ctx);
179 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
180 domain_dn, LDB_SCOPE_BASE, dom_attrs,
181 "objectClass=domain");
182 if (ret != LDB_SUCCESS) {
183 DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n",
184 lpcfg_dnsdomain(lp_ctx),
185 ldb_dn_get_linearized(domain_dn),
186 ldb_errstring(sam_ctx)));
187 return NT_STATUS_NO_SUCH_DOMAIN;
191 if (dom_res == NULL) {
192 DEBUG(2,(__location__ ": Unable to get domain information with no inputs\n"));
193 return NT_STATUS_NO_SUCH_DOMAIN;
196 /* work around different inputs for not-specified users */
197 if (!user) {
198 user = "";
201 /* Enquire about any valid username with just a CLDAP packet -
202 * if kerberos didn't also do this, the security folks would
203 * scream... */
204 if (user[0]) { \
205 /* Only allow some bits to be enquired: [MS-ATDS] 7.3.3.2 */
206 if (acct_control == (uint32_t)-1) {
207 acct_control = 0;
209 acct_control = acct_control & (ACB_TEMPDUP | ACB_NORMAL | ACB_DOMTRUST | ACB_WSTRUST | ACB_SVRTRUST);
211 /* We must exclude disabled accounts, but otherwise do the bitwise match the client asked for */
212 ret = ldb_search(sam_ctx, mem_ctx, &user_res,
213 dom_res->msgs[0]->dn, LDB_SCOPE_SUBTREE,
214 none_attrs,
215 "(&(objectClass=user)(samAccountName=%s)"
216 "(!(userAccountControl:" LDB_OID_COMPARATOR_AND ":=%u))"
217 "(userAccountControl:" LDB_OID_COMPARATOR_OR ":=%u))",
218 ldb_binary_encode_string(mem_ctx, user),
219 UF_ACCOUNTDISABLE, ds_acb2uf(acct_control));
220 if (ret != LDB_SUCCESS) {
221 DEBUG(2,("Unable to find reference to user '%s' with ACB 0x%8x under %s: %s\n",
222 user, acct_control, ldb_dn_get_linearized(dom_res->msgs[0]->dn),
223 ldb_errstring(sam_ctx)));
224 return NT_STATUS_NO_SUCH_USER;
225 } else if (user_res->count == 1) {
226 user_known = true;
227 } else {
228 user_known = false;
231 } else {
232 user_known = true;
235 server_type =
236 DS_SERVER_DS | DS_SERVER_TIMESERV |
237 DS_SERVER_CLOSEST |
238 DS_SERVER_GOOD_TIMESERV;
240 #if 0
241 /* w2k8-r2 as a DC does not claim these */
242 server_type |= DS_DNS_CONTROLLER | DS_DNS_DOMAIN;
243 #endif
245 if (samdb_is_pdc(sam_ctx)) {
246 server_type |= DS_SERVER_PDC;
249 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008) {
250 server_type |= DS_SERVER_FULL_SECRET_DOMAIN_6;
253 if (samdb_is_gc(sam_ctx)) {
254 server_type |= DS_SERVER_GC;
257 if (str_list_check(services, "ldap")) {
258 server_type |= DS_SERVER_LDAP;
261 if (str_list_check(services, "kdc")) {
262 server_type |= DS_SERVER_KDC;
265 if (samdb_rodc(sam_ctx, &am_rodc) == LDB_SUCCESS && !am_rodc) {
266 server_type |= DS_SERVER_WRITABLE;
269 #if 0
270 /* w2k8-r2 as a sole DC does not claim this */
271 if (ldb_dn_compare(ldb_get_root_basedn(sam_ctx), ldb_get_default_basedn(sam_ctx)) == 0) {
272 server_type |= DS_DNS_FOREST_ROOT;
274 #endif
276 pdc_name = talloc_asprintf(mem_ctx, "\\\\%s",
277 lpcfg_netbios_name(lp_ctx));
278 NT_STATUS_HAVE_NO_MEMORY(pdc_name);
279 domain_uuid = samdb_result_guid(dom_res->msgs[0], "objectGUID");
280 dns_domain = lpcfg_dnsdomain(lp_ctx);
281 forest_domain = samdb_forest_name(sam_ctx, mem_ctx);
282 NT_STATUS_HAVE_NO_MEMORY(forest_domain);
283 pdc_dns_name = talloc_asprintf(mem_ctx, "%s.%s",
284 strlower_talloc(mem_ctx,
285 lpcfg_netbios_name(lp_ctx)),
286 dns_domain);
287 NT_STATUS_HAVE_NO_MEMORY(pdc_dns_name);
288 flatname = lpcfg_workgroup(lp_ctx);
289 server_site = samdb_server_site_name(sam_ctx, mem_ctx);
290 NT_STATUS_HAVE_NO_MEMORY(server_site);
291 client_site = samdb_client_site_name(sam_ctx, mem_ctx,
292 src_address, NULL);
293 NT_STATUS_HAVE_NO_MEMORY(client_site);
294 load_interfaces(mem_ctx, lpcfg_interfaces(lp_ctx), &ifaces);
296 * TODO: the caller should pass the address which the client
297 * used to trigger this call, as the client is able to reach
298 * this ip.
300 if (src_address) {
301 pdc_ip = iface_best_ip(ifaces, src_address);
302 } else {
303 pdc_ip = iface_n_ip(ifaces, 0);
305 ZERO_STRUCTP(netlogon);
307 /* check if either of these bits is present */
308 if (version & (NETLOGON_NT_VERSION_5EX|NETLOGON_NT_VERSION_5EX_WITH_IP)) {
309 uint32_t extra_flags = 0;
310 netlogon->ntver = NETLOGON_NT_VERSION_5EX;
312 /* could check if the user exists */
313 if (user_known) {
314 netlogon->data.nt5_ex.command = LOGON_SAM_LOGON_RESPONSE_EX;
315 } else {
316 netlogon->data.nt5_ex.command = LOGON_SAM_LOGON_USER_UNKNOWN_EX;
318 netlogon->data.nt5_ex.pdc_name = pdc_name;
319 netlogon->data.nt5_ex.user_name = user;
320 netlogon->data.nt5_ex.domain_name = flatname;
321 netlogon->data.nt5_ex.domain_uuid = domain_uuid;
322 netlogon->data.nt5_ex.forest = forest_domain;
323 netlogon->data.nt5_ex.dns_domain = dns_domain;
324 netlogon->data.nt5_ex.pdc_dns_name = pdc_dns_name;
325 netlogon->data.nt5_ex.server_site = server_site;
326 netlogon->data.nt5_ex.client_site = client_site;
327 if (version & NETLOGON_NT_VERSION_5EX_WITH_IP) {
328 /* Clearly this needs to be fixed up for IPv6 */
329 extra_flags = NETLOGON_NT_VERSION_5EX_WITH_IP;
330 netlogon->data.nt5_ex.sockaddr.sockaddr_family = 2;
331 netlogon->data.nt5_ex.sockaddr.pdc_ip = pdc_ip;
332 netlogon->data.nt5_ex.sockaddr.remaining = data_blob_talloc_zero(mem_ctx, 8);
334 netlogon->data.nt5_ex.server_type = server_type;
335 netlogon->data.nt5_ex.nt_version = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5EX|extra_flags;
336 netlogon->data.nt5_ex.lmnt_token = 0xFFFF;
337 netlogon->data.nt5_ex.lm20_token = 0xFFFF;
339 } else if (version & NETLOGON_NT_VERSION_5) {
340 netlogon->ntver = NETLOGON_NT_VERSION_5;
342 /* could check if the user exists */
343 if (user_known) {
344 netlogon->data.nt5.command = LOGON_SAM_LOGON_RESPONSE;
345 } else {
346 netlogon->data.nt5.command = LOGON_SAM_LOGON_USER_UNKNOWN;
348 netlogon->data.nt5.pdc_name = pdc_name;
349 netlogon->data.nt5.user_name = user;
350 netlogon->data.nt5.domain_name = flatname;
351 netlogon->data.nt5.domain_uuid = domain_uuid;
352 netlogon->data.nt5.forest = forest_domain;
353 netlogon->data.nt5.dns_domain = dns_domain;
354 netlogon->data.nt5.pdc_dns_name = pdc_dns_name;
355 netlogon->data.nt5.pdc_ip = pdc_ip;
356 netlogon->data.nt5.server_type = server_type;
357 netlogon->data.nt5.nt_version = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5;
358 netlogon->data.nt5.lmnt_token = 0xFFFF;
359 netlogon->data.nt5.lm20_token = 0xFFFF;
361 } else /* (version & NETLOGON_NT_VERSION_1) and all other cases */ {
362 netlogon->ntver = NETLOGON_NT_VERSION_1;
363 /* could check if the user exists */
364 if (user_known) {
365 netlogon->data.nt4.command = LOGON_SAM_LOGON_RESPONSE;
366 } else {
367 netlogon->data.nt4.command = LOGON_SAM_LOGON_USER_UNKNOWN;
369 netlogon->data.nt4.pdc_name = pdc_name;
370 netlogon->data.nt4.user_name = user;
371 netlogon->data.nt4.domain_name = flatname;
372 netlogon->data.nt4.nt_version = NETLOGON_NT_VERSION_1;
373 netlogon->data.nt4.lmnt_token = 0xFFFF;
374 netlogon->data.nt4.lm20_token = 0xFFFF;
377 return NT_STATUS_OK;
382 handle incoming cldap requests
384 void cldapd_netlogon_request(struct cldap_socket *cldap,
385 struct cldapd_server *cldapd,
386 TALLOC_CTX *tmp_ctx,
387 uint32_t message_id,
388 struct ldb_parse_tree *tree,
389 struct tsocket_address *src)
391 unsigned int i;
392 const char *domain = NULL;
393 const char *host = NULL;
394 const char *user = NULL;
395 const char *domain_guid = NULL;
396 struct dom_sid *domain_sid = NULL;
397 int acct_control = -1;
398 int version = -1;
399 struct netlogon_samlogon_response netlogon;
400 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
402 if (tree->operation != LDB_OP_AND) goto failed;
404 /* extract the query elements */
405 for (i=0;i<tree->u.list.num_elements;i++) {
406 struct ldb_parse_tree *t = tree->u.list.elements[i];
407 if (t->operation != LDB_OP_EQUALITY) goto failed;
408 if (strcasecmp(t->u.equality.attr, "DnsDomain") == 0) {
409 domain = talloc_strndup(tmp_ctx,
410 (const char *)t->u.equality.value.data,
411 t->u.equality.value.length);
413 if (strcasecmp(t->u.equality.attr, "Host") == 0) {
414 host = talloc_strndup(tmp_ctx,
415 (const char *)t->u.equality.value.data,
416 t->u.equality.value.length);
418 if (strcasecmp(t->u.equality.attr, "DomainGuid") == 0) {
419 NTSTATUS enc_status;
420 struct GUID guid;
421 enc_status = ldap_decode_ndr_GUID(tmp_ctx,
422 t->u.equality.value, &guid);
423 if (NT_STATUS_IS_OK(enc_status)) {
424 domain_guid = GUID_string(tmp_ctx, &guid);
427 if (strcasecmp(t->u.equality.attr, "DomainSid") == 0) {
428 enum ndr_err_code ndr_err;
430 domain_sid = talloc(tmp_ctx, struct dom_sid);
431 if (domain_sid == NULL) {
432 goto failed;
434 ndr_err = ndr_pull_struct_blob(&t->u.equality.value,
435 domain_sid, domain_sid,
436 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
437 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
438 talloc_free(domain_sid);
439 goto failed;
442 if (strcasecmp(t->u.equality.attr, "User") == 0) {
443 user = talloc_strndup(tmp_ctx,
444 (const char *)t->u.equality.value.data,
445 t->u.equality.value.length);
447 if (strcasecmp(t->u.equality.attr, "NtVer") == 0 &&
448 t->u.equality.value.length == 4) {
449 version = IVAL(t->u.equality.value.data, 0);
451 if (strcasecmp(t->u.equality.attr, "AAC") == 0 &&
452 t->u.equality.value.length == 4) {
453 acct_control = IVAL(t->u.equality.value.data, 0);
457 if ((domain == NULL) && (domain_guid == NULL) && (domain_sid == NULL)) {
458 domain = lpcfg_dnsdomain(cldapd->task->lp_ctx);
461 if (version == -1) {
462 goto failed;
465 DEBUG(5,("cldap netlogon query domain=%s host=%s user=%s version=%d guid=%s\n",
466 domain, host, user, version, domain_guid));
468 status = fill_netlogon_samlogon_response(cldapd->samctx, tmp_ctx,
469 domain, NULL, domain_sid,
470 domain_guid,
471 user, acct_control,
472 tsocket_address_inet_addr_string(src, tmp_ctx),
473 version, cldapd->task->lp_ctx,
474 &netlogon, false);
475 if (!NT_STATUS_IS_OK(status)) {
476 goto failed;
479 status = cldap_netlogon_reply(cldap, message_id, src, version, &netlogon);
480 if (!NT_STATUS_IS_OK(status)) {
481 goto failed;
484 return;
486 failed:
487 DEBUG(2,("cldap netlogon query failed domain=%s host=%s version=%d - %s\n",
488 domain, host, version, nt_errstr(status)));
489 cldap_empty_reply(cldap, message_id, src);