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/>.
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"
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"
40 #include "lib/util/util_net.h"
43 fill in the cldap netlogon union for a given version
45 NTSTATUS fill_netlogon_samlogon_response(struct ldb_context *sam_ctx,
48 const char *netbios_domain,
49 struct dom_sid *domain_sid,
50 const char *domain_guid,
52 uint32_t acct_control,
53 const char *src_address,
55 struct loadparm_context *lp_ctx,
56 struct netlogon_samlogon_response *netlogon,
57 bool fill_on_blank_request)
59 const char *dom_attrs[] = {"objectGUID", NULL};
60 const char *none_attrs[] = {NULL};
61 struct ldb_result *dom_res = NULL, *user_res = NULL;
63 const char **services = lpcfg_server_services(lp_ctx);
66 struct GUID domain_uuid;
67 const char *dns_domain;
68 const char *forest_domain;
69 const char *pdc_dns_name;
71 const char *server_site;
72 const char *client_site;
74 struct ldb_dn *domain_dn = NULL;
75 struct interface *ifaces;
76 bool user_known, am_rodc;
79 /* the domain parameter could have an optional trailing "." */
80 if (domain && domain[strlen(domain)-1] == '.') {
81 domain = talloc_strndup(mem_ctx, domain, strlen(domain)-1);
82 NT_STATUS_HAVE_NO_MEMORY(domain);
85 /* Lookup using long or short domainname */
86 if (domain && (strcasecmp_m(domain, lpcfg_dnsdomain(lp_ctx)) == 0)) {
87 domain_dn = ldb_get_default_basedn(sam_ctx);
89 if (netbios_domain && (strcasecmp_m(netbios_domain, lpcfg_sam_name(lp_ctx)) == 0)) {
90 domain_dn = ldb_get_default_basedn(sam_ctx);
93 const char *domain_identifier = domain != NULL ? domain
95 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
96 domain_dn, LDB_SCOPE_BASE, dom_attrs,
97 "objectClass=domain");
98 if (ret != LDB_SUCCESS) {
99 DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n",
101 ldb_dn_get_linearized(domain_dn),
102 ldb_errstring(sam_ctx)));
103 return NT_STATUS_NO_SUCH_DOMAIN;
105 if (dom_res->count != 1) {
106 DEBUG(2,("Error finding domain '%s'/'%s' in sam\n",
108 ldb_dn_get_linearized(domain_dn)));
109 return NT_STATUS_NO_SUCH_DOMAIN;
113 /* Lookup using GUID or SID */
114 if ((dom_res == NULL) && (domain_guid || domain_sid)) {
116 struct GUID binary_guid;
117 struct ldb_val guid_val;
119 /* By this means, we ensure we don't have funny stuff in the GUID */
121 status = GUID_from_string(domain_guid, &binary_guid);
122 if (!NT_STATUS_IS_OK(status)) {
126 /* And this gets the result into the binary format we want anyway */
127 status = GUID_to_ndr_blob(&binary_guid, mem_ctx, &guid_val);
128 if (!NT_STATUS_IS_OK(status)) {
131 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
132 NULL, LDB_SCOPE_SUBTREE,
134 "(&(objectCategory=DomainDNS)(objectGUID=%s))",
135 ldb_binary_encode(mem_ctx, guid_val));
136 } else { /* domain_sid case */
138 struct ldb_val sid_val;
139 enum ndr_err_code ndr_err;
141 /* Rather than go via the string, just push into the NDR form */
142 ndr_err = ndr_push_struct_blob(&sid_val, mem_ctx, &sid,
143 (ndr_push_flags_fn_t)ndr_push_dom_sid);
144 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
145 return NT_STATUS_INVALID_PARAMETER;
148 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
149 NULL, LDB_SCOPE_SUBTREE,
151 "(&(objectCategory=DomainDNS)(objectSid=%s))",
152 ldb_binary_encode(mem_ctx, sid_val));
155 if (ret != LDB_SUCCESS) {
156 DEBUG(2,("Unable to find a correct reference to GUID '%s' or SID '%s' in sam: %s\n",
157 domain_guid, dom_sid_string(mem_ctx, domain_sid),
158 ldb_errstring(sam_ctx)));
159 return NT_STATUS_NO_SUCH_DOMAIN;
160 } else if (dom_res->count == 1) {
161 /* Ok, now just check it is our domain */
162 if (ldb_dn_compare(ldb_get_default_basedn(sam_ctx),
163 dom_res->msgs[0]->dn) != 0) {
164 DEBUG(2,("The GUID '%s' or SID '%s' doesn't identify our domain\n",
166 dom_sid_string(mem_ctx, domain_sid)));
167 return NT_STATUS_NO_SUCH_DOMAIN;
170 DEBUG(2,("Unable to find a correct reference to GUID '%s' or SID '%s' in sam\n",
171 domain_guid, dom_sid_string(mem_ctx, domain_sid)));
172 return NT_STATUS_NO_SUCH_DOMAIN;
176 if (dom_res == NULL && fill_on_blank_request) {
177 /* blank inputs gives our domain - tested against
178 w2k8r2. Without this ADUC on Win7 won't start */
179 domain_dn = ldb_get_default_basedn(sam_ctx);
180 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
181 domain_dn, LDB_SCOPE_BASE, dom_attrs,
182 "objectClass=domain");
183 if (ret != LDB_SUCCESS) {
184 DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n",
185 lpcfg_dnsdomain(lp_ctx),
186 ldb_dn_get_linearized(domain_dn),
187 ldb_errstring(sam_ctx)));
188 return NT_STATUS_NO_SUCH_DOMAIN;
192 if (dom_res == NULL) {
193 DEBUG(2,(__location__ ": Unable to get domain information with no inputs\n"));
194 return NT_STATUS_NO_SUCH_DOMAIN;
197 /* work around different inputs for not-specified users */
202 /* Enquire about any valid username with just a CLDAP packet -
203 * if kerberos didn't also do this, the security folks would
206 /* Only allow some bits to be enquired: [MS-ATDS] 7.3.3.2 */
207 if (acct_control == (uint32_t)-1) {
210 acct_control = acct_control & (ACB_TEMPDUP | ACB_NORMAL | ACB_DOMTRUST | ACB_WSTRUST | ACB_SVRTRUST);
212 /* We must exclude disabled accounts, but otherwise do the bitwise match the client asked for */
213 ret = ldb_search(sam_ctx, mem_ctx, &user_res,
214 dom_res->msgs[0]->dn, LDB_SCOPE_SUBTREE,
216 "(&(objectClass=user)(samAccountName=%s)"
217 "(!(userAccountControl:" LDB_OID_COMPARATOR_AND ":=%u))"
218 "(userAccountControl:" LDB_OID_COMPARATOR_OR ":=%u))",
219 ldb_binary_encode_string(mem_ctx, user),
220 UF_ACCOUNTDISABLE, ds_acb2uf(acct_control));
221 if (ret != LDB_SUCCESS) {
222 DEBUG(2,("Unable to find reference to user '%s' with ACB 0x%8x under %s: %s\n",
223 user, acct_control, ldb_dn_get_linearized(dom_res->msgs[0]->dn),
224 ldb_errstring(sam_ctx)));
225 return NT_STATUS_NO_SUCH_USER;
226 } else if (user_res->count == 1) {
237 DS_SERVER_DS | DS_SERVER_TIMESERV |
239 DS_SERVER_GOOD_TIMESERV;
242 /* w2k8-r2 as a DC does not claim these */
243 server_type |= DS_DNS_CONTROLLER | DS_DNS_DOMAIN;
246 if (samdb_is_pdc(sam_ctx)) {
247 server_type |= DS_SERVER_PDC;
250 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008) {
251 server_type |= DS_SERVER_FULL_SECRET_DOMAIN_6;
254 if (samdb_is_gc(sam_ctx)) {
255 server_type |= DS_SERVER_GC;
258 if (str_list_check(services, "ldap")) {
259 server_type |= DS_SERVER_LDAP;
262 if (str_list_check(services, "kdc")) {
263 server_type |= DS_SERVER_KDC;
266 if (samdb_rodc(sam_ctx, &am_rodc) == LDB_SUCCESS && !am_rodc) {
267 server_type |= DS_SERVER_WRITABLE;
271 /* w2k8-r2 as a sole DC does not claim this */
272 if (ldb_dn_compare(ldb_get_root_basedn(sam_ctx), ldb_get_default_basedn(sam_ctx)) == 0) {
273 server_type |= DS_DNS_FOREST_ROOT;
277 pdc_name = talloc_asprintf(mem_ctx, "\\\\%s",
278 lpcfg_netbios_name(lp_ctx));
279 NT_STATUS_HAVE_NO_MEMORY(pdc_name);
280 domain_uuid = samdb_result_guid(dom_res->msgs[0], "objectGUID");
281 dns_domain = lpcfg_dnsdomain(lp_ctx);
282 forest_domain = samdb_forest_name(sam_ctx, mem_ctx);
283 NT_STATUS_HAVE_NO_MEMORY(forest_domain);
284 pdc_dns_name = talloc_asprintf(mem_ctx, "%s.%s",
285 strlower_talloc(mem_ctx,
286 lpcfg_netbios_name(lp_ctx)),
288 NT_STATUS_HAVE_NO_MEMORY(pdc_dns_name);
289 flatname = lpcfg_workgroup(lp_ctx);
290 server_site = samdb_server_site_name(sam_ctx, mem_ctx);
291 NT_STATUS_HAVE_NO_MEMORY(server_site);
292 client_site = samdb_client_site_name(sam_ctx, mem_ctx,
294 NT_STATUS_HAVE_NO_MEMORY(client_site);
295 load_interface_list(mem_ctx, lp_ctx, &ifaces);
298 pdc_ip = iface_list_best_ip(ifaces, src_address);
300 pdc_ip = iface_list_first_v4(ifaces);
302 if (pdc_ip == NULL || !is_ipaddress_v4(pdc_ip)) {
303 /* this matches windows behaviour */
304 pdc_ip = "127.0.0.1";
307 ZERO_STRUCTP(netlogon);
309 /* check if either of these bits is present */
310 if (version & (NETLOGON_NT_VERSION_5EX|NETLOGON_NT_VERSION_5EX_WITH_IP)) {
311 uint32_t extra_flags = 0;
312 netlogon->ntver = NETLOGON_NT_VERSION_5EX;
314 /* could check if the user exists */
316 netlogon->data.nt5_ex.command = LOGON_SAM_LOGON_RESPONSE_EX;
318 netlogon->data.nt5_ex.command = LOGON_SAM_LOGON_USER_UNKNOWN_EX;
320 netlogon->data.nt5_ex.pdc_name = pdc_name;
321 netlogon->data.nt5_ex.user_name = user;
322 netlogon->data.nt5_ex.domain_name = flatname;
323 netlogon->data.nt5_ex.domain_uuid = domain_uuid;
324 netlogon->data.nt5_ex.forest = forest_domain;
325 netlogon->data.nt5_ex.dns_domain = dns_domain;
326 netlogon->data.nt5_ex.pdc_dns_name = pdc_dns_name;
327 netlogon->data.nt5_ex.server_site = server_site;
328 netlogon->data.nt5_ex.client_site = client_site;
329 if (version & NETLOGON_NT_VERSION_5EX_WITH_IP) {
330 /* note that this is always a IPV4 address */
331 extra_flags = NETLOGON_NT_VERSION_5EX_WITH_IP;
332 netlogon->data.nt5_ex.sockaddr.sockaddr_family = 2;
333 netlogon->data.nt5_ex.sockaddr.pdc_ip = pdc_ip;
334 netlogon->data.nt5_ex.sockaddr.remaining = data_blob_talloc_zero(mem_ctx, 8);
336 netlogon->data.nt5_ex.server_type = server_type;
337 netlogon->data.nt5_ex.nt_version = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5EX|extra_flags;
338 netlogon->data.nt5_ex.lmnt_token = 0xFFFF;
339 netlogon->data.nt5_ex.lm20_token = 0xFFFF;
341 } else if (version & NETLOGON_NT_VERSION_5) {
342 netlogon->ntver = NETLOGON_NT_VERSION_5;
344 /* could check if the user exists */
346 netlogon->data.nt5.command = LOGON_SAM_LOGON_RESPONSE;
348 netlogon->data.nt5.command = LOGON_SAM_LOGON_USER_UNKNOWN;
350 netlogon->data.nt5.pdc_name = pdc_name;
351 netlogon->data.nt5.user_name = user;
352 netlogon->data.nt5.domain_name = flatname;
353 netlogon->data.nt5.domain_uuid = domain_uuid;
354 netlogon->data.nt5.forest = forest_domain;
355 netlogon->data.nt5.dns_domain = dns_domain;
356 netlogon->data.nt5.pdc_dns_name = pdc_dns_name;
357 netlogon->data.nt5.pdc_ip = pdc_ip;
358 netlogon->data.nt5.server_type = server_type;
359 netlogon->data.nt5.nt_version = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5;
360 netlogon->data.nt5.lmnt_token = 0xFFFF;
361 netlogon->data.nt5.lm20_token = 0xFFFF;
363 } else /* (version & NETLOGON_NT_VERSION_1) and all other cases */ {
364 netlogon->ntver = NETLOGON_NT_VERSION_1;
365 /* could check if the user exists */
367 netlogon->data.nt4.command = LOGON_SAM_LOGON_RESPONSE;
369 netlogon->data.nt4.command = LOGON_SAM_LOGON_USER_UNKNOWN;
371 netlogon->data.nt4.pdc_name = pdc_name;
372 netlogon->data.nt4.user_name = user;
373 netlogon->data.nt4.domain_name = flatname;
374 netlogon->data.nt4.nt_version = NETLOGON_NT_VERSION_1;
375 netlogon->data.nt4.lmnt_token = 0xFFFF;
376 netlogon->data.nt4.lm20_token = 0xFFFF;
384 handle incoming cldap requests
386 void cldapd_netlogon_request(struct cldap_socket *cldap,
387 struct cldapd_server *cldapd,
390 struct ldb_parse_tree *tree,
391 struct tsocket_address *src)
394 const char *domain = NULL;
395 const char *host = NULL;
396 const char *user = NULL;
397 const char *domain_guid = NULL;
398 struct dom_sid *domain_sid = NULL;
399 int acct_control = -1;
401 struct netlogon_samlogon_response netlogon;
402 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
404 if (tree->operation != LDB_OP_AND) goto failed;
406 /* extract the query elements */
407 for (i=0;i<tree->u.list.num_elements;i++) {
408 struct ldb_parse_tree *t = tree->u.list.elements[i];
409 if (t->operation != LDB_OP_EQUALITY) goto failed;
410 if (strcasecmp(t->u.equality.attr, "DnsDomain") == 0) {
411 domain = talloc_strndup(tmp_ctx,
412 (const char *)t->u.equality.value.data,
413 t->u.equality.value.length);
415 if (strcasecmp(t->u.equality.attr, "Host") == 0) {
416 host = talloc_strndup(tmp_ctx,
417 (const char *)t->u.equality.value.data,
418 t->u.equality.value.length);
420 if (strcasecmp(t->u.equality.attr, "DomainGuid") == 0) {
423 enc_status = ldap_decode_ndr_GUID(tmp_ctx,
424 t->u.equality.value, &guid);
425 if (NT_STATUS_IS_OK(enc_status)) {
426 domain_guid = GUID_string(tmp_ctx, &guid);
429 if (strcasecmp(t->u.equality.attr, "DomainSid") == 0) {
430 enum ndr_err_code ndr_err;
432 domain_sid = talloc(tmp_ctx, struct dom_sid);
433 if (domain_sid == NULL) {
436 ndr_err = ndr_pull_struct_blob(&t->u.equality.value,
437 domain_sid, domain_sid,
438 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
439 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
440 talloc_free(domain_sid);
444 if (strcasecmp(t->u.equality.attr, "User") == 0) {
445 user = talloc_strndup(tmp_ctx,
446 (const char *)t->u.equality.value.data,
447 t->u.equality.value.length);
449 if (strcasecmp(t->u.equality.attr, "NtVer") == 0 &&
450 t->u.equality.value.length == 4) {
451 version = IVAL(t->u.equality.value.data, 0);
453 if (strcasecmp(t->u.equality.attr, "AAC") == 0 &&
454 t->u.equality.value.length == 4) {
455 acct_control = IVAL(t->u.equality.value.data, 0);
459 if ((domain == NULL) && (domain_guid == NULL) && (domain_sid == NULL)) {
460 domain = lpcfg_dnsdomain(cldapd->task->lp_ctx);
467 DEBUG(5,("cldap netlogon query domain=%s host=%s user=%s version=%d guid=%s\n",
468 domain, host, user, version, domain_guid));
470 status = fill_netlogon_samlogon_response(cldapd->samctx, tmp_ctx,
471 domain, NULL, domain_sid,
474 tsocket_address_inet_addr_string(src, tmp_ctx),
475 version, cldapd->task->lp_ctx,
477 if (!NT_STATUS_IS_OK(status)) {
481 status = cldap_netlogon_reply(cldap, message_id, src, version, &netlogon);
482 if (!NT_STATUS_IS_OK(status)) {
489 DEBUG(2,("cldap netlogon query failed domain=%s host=%s version=%d - %s\n",
490 domain, host, version, nt_errstr(status)));
491 cldap_empty_reply(cldap, message_id, src);