Fix the build of nfs4_acls.c
[Samba.git] / source4 / cldap_server / netlogon.c
blob8a21ea55c9ad727a1a1ea79a108de0495a4456f3
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 "lib/ldb/include/ldb.h"
25 #include "lib/ldb/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"
41 fill in the cldap netlogon union for a given version
43 NTSTATUS fill_netlogon_samlogon_response(struct ldb_context *sam_ctx,
44 TALLOC_CTX *mem_ctx,
45 const char *domain,
46 const char *netbios_domain,
47 struct dom_sid *domain_sid,
48 const char *domain_guid,
49 const char *user,
50 uint32_t acct_control,
51 const char *src_address,
52 uint32_t version,
53 struct loadparm_context *lp_ctx,
54 struct netlogon_samlogon_response *netlogon)
56 const char *dom_attrs[] = {"objectGUID", NULL};
57 const char *none_attrs[] = {NULL};
58 struct ldb_result *dom_res = NULL, *user_res = NULL;
59 int ret;
60 const char **services = lp_server_services(lp_ctx);
61 uint32_t server_type;
62 const char *pdc_name;
63 struct GUID domain_uuid;
64 const char *realm;
65 const char *dns_domain;
66 const char *pdc_dns_name;
67 const char *flatname;
68 const char *server_site;
69 const char *client_site;
70 const char *pdc_ip;
71 struct ldb_dn *domain_dn = NULL;
72 struct interface *ifaces;
73 bool user_known;
74 NTSTATUS status;
76 /* the domain has an optional trailing . */
77 if (domain && domain[strlen(domain)-1] == '.') {
78 domain = talloc_strndup(mem_ctx, domain, strlen(domain)-1);
81 if (domain && strcasecmp_m(domain, lp_realm(lp_ctx)) == 0) {
82 domain_dn = ldb_get_default_basedn(sam_ctx);
85 if (netbios_domain && strcasecmp_m(domain, lp_sam_name(lp_ctx))) {
86 domain_dn = ldb_get_default_basedn(sam_ctx);
89 if (domain_dn) {
90 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
91 domain_dn, LDB_SCOPE_BASE, dom_attrs,
92 "objectClass=domain");
93 if (ret != LDB_SUCCESS) {
94 DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n", domain, ldb_dn_get_linearized(domain_dn), ldb_errstring(sam_ctx)));
95 return NT_STATUS_NO_SUCH_DOMAIN;
97 if (dom_res->count != 1) {
98 DEBUG(2,("Error finding domain '%s'/'%s' in sam\n", domain, ldb_dn_get_linearized(domain_dn)));
99 return NT_STATUS_NO_SUCH_DOMAIN;
103 if ((dom_res == NULL || dom_res->count == 0) && (domain_guid || domain_sid)) {
105 if (domain_guid) {
106 struct GUID binary_guid;
107 struct ldb_val guid_val;
108 enum ndr_err_code ndr_err;
110 /* By this means, we ensure we don't have funny stuff in the GUID */
112 status = GUID_from_string(domain_guid, &binary_guid);
113 if (!NT_STATUS_IS_OK(status)) {
114 return status;
117 /* And this gets the result into the binary format we want anyway */
118 ndr_err = ndr_push_struct_blob(&guid_val, mem_ctx, NULL, &binary_guid,
119 (ndr_push_flags_fn_t)ndr_push_GUID);
120 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
121 return NT_STATUS_INVALID_PARAMETER;
123 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
124 NULL, LDB_SCOPE_SUBTREE,
125 dom_attrs,
126 "(&(objectCategory=DomainDNS)(objectGUID=%s))",
127 ldb_binary_encode(mem_ctx, guid_val));
128 } else { /* domain_sid case */
129 struct dom_sid *sid;
130 struct ldb_val sid_val;
131 enum ndr_err_code ndr_err;
133 /* Rather than go via the string, just push into the NDR form */
134 ndr_err = ndr_push_struct_blob(&sid_val, mem_ctx, NULL, &sid,
135 (ndr_push_flags_fn_t)ndr_push_dom_sid);
136 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
137 return NT_STATUS_INVALID_PARAMETER;
140 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
141 NULL, LDB_SCOPE_SUBTREE,
142 dom_attrs,
143 "(&(objectCategory=DomainDNS)(objectSID=%s))",
144 ldb_binary_encode(mem_ctx, sid_val));
147 if (ret != LDB_SUCCESS) {
148 DEBUG(2,("Unable to find referece to GUID '%s' or SID %s in sam: %s\n",
149 domain_guid, dom_sid_string(mem_ctx, domain_sid),
150 ldb_errstring(sam_ctx)));
151 return NT_STATUS_NO_SUCH_DOMAIN;
152 } else if (dom_res->count == 1) {
153 /* Ok, now just check it is our domain */
155 if (ldb_dn_compare(ldb_get_default_basedn(sam_ctx), dom_res->msgs[0]->dn) != 0) {
156 return NT_STATUS_NO_SUCH_DOMAIN;
158 } else if (dom_res->count > 1) {
159 return NT_STATUS_NO_SUCH_DOMAIN;
164 if ((dom_res == NULL || dom_res->count == 0)) {
165 DEBUG(2,("Unable to find domain with name %s or GUID {%s}\n", domain, domain_guid));
166 return NT_STATUS_NO_SUCH_DOMAIN;
169 /* work around different inputs for not-specified users */
170 if (!user) {
171 user = "";
174 /* Enquire about any valid username with just a CLDAP packet -
175 * if kerberos didn't also do this, the security folks would
176 * scream... */
177 if (user[0]) { \
178 /* Only allow some bits to be enquired: [MS-ATDS] 7.3.3.2 */
179 if (acct_control == (uint32_t)-1) {
180 acct_control = 0;
182 acct_control = acct_control & (ACB_TEMPDUP | ACB_NORMAL | ACB_DOMTRUST | ACB_WSTRUST | ACB_SVRTRUST);
184 /* We must exclude disabled accounts, but otherwise do the bitwise match the client asked for */
185 ret = ldb_search(sam_ctx, mem_ctx, &user_res,
186 dom_res->msgs[0]->dn, LDB_SCOPE_SUBTREE,
187 none_attrs,
188 "(&(objectClass=user)(samAccountName=%s)"
189 "(!(userAccountControl:" LDB_OID_COMPARATOR_AND ":=%u))"
190 "(userAccountControl:" LDB_OID_COMPARATOR_OR ":=%u))",
191 ldb_binary_encode_string(mem_ctx, user),
192 UF_ACCOUNTDISABLE, samdb_acb2uf(acct_control));
193 if (ret != LDB_SUCCESS) {
194 DEBUG(2,("Unable to find referece to user '%s' with ACB 0x%8x under %s: %s\n",
195 user, acct_control, ldb_dn_get_linearized(dom_res->msgs[0]->dn),
196 ldb_errstring(sam_ctx)));
197 return NT_STATUS_NO_SUCH_USER;
198 } else if (user_res->count == 1) {
199 user_known = true;
200 } else {
201 user_known = false;
204 } else {
205 user_known = true;
208 server_type =
209 NBT_SERVER_DS | NBT_SERVER_TIMESERV |
210 NBT_SERVER_CLOSEST | NBT_SERVER_WRITABLE |
211 NBT_SERVER_GOOD_TIMESERV | DS_DNS_CONTROLLER |
212 DS_DNS_DOMAIN;
214 if (samdb_is_pdc(sam_ctx)) {
215 server_type |= NBT_SERVER_PDC;
218 if (samdb_is_gc(sam_ctx)) {
219 server_type |= NBT_SERVER_GC;
222 if (str_list_check(services, "ldap")) {
223 server_type |= NBT_SERVER_LDAP;
226 if (str_list_check(services, "kdc")) {
227 server_type |= NBT_SERVER_KDC;
230 if (ldb_dn_compare(ldb_get_root_basedn(sam_ctx), ldb_get_default_basedn(sam_ctx)) == 0) {
231 server_type |= DS_DNS_FOREST;
234 pdc_name = talloc_asprintf(mem_ctx, "\\\\%s", lp_netbios_name(lp_ctx));
235 domain_uuid = samdb_result_guid(dom_res->msgs[0], "objectGUID");
236 realm = lp_realm(lp_ctx);
237 dns_domain = lp_realm(lp_ctx);
238 pdc_dns_name = talloc_asprintf(mem_ctx, "%s.%s",
239 strlower_talloc(mem_ctx,
240 lp_netbios_name(lp_ctx)),
241 dns_domain);
243 flatname = lp_sam_name(lp_ctx);
244 /* FIXME: Hardcoded site names */
245 server_site = "Default-First-Site-Name";
246 client_site = "Default-First-Site-Name";
247 load_interfaces(mem_ctx, lp_interfaces(lp_ctx), &ifaces);
248 pdc_ip = iface_best_ip(ifaces, src_address);
250 ZERO_STRUCTP(netlogon);
252 /* check if either of these bits is present */
253 if (version & (NETLOGON_NT_VERSION_5EX|NETLOGON_NT_VERSION_5EX_WITH_IP)) {
254 uint32_t extra_flags = 0;
255 netlogon->ntver = NETLOGON_NT_VERSION_5EX;
257 /* could check if the user exists */
258 if (user_known) {
259 netlogon->data.nt5_ex.command = LOGON_SAM_LOGON_RESPONSE_EX;
260 } else {
261 netlogon->data.nt5_ex.command = LOGON_SAM_LOGON_USER_UNKNOWN_EX;
263 netlogon->data.nt5_ex.server_type = server_type;
264 netlogon->data.nt5_ex.domain_uuid = domain_uuid;
265 netlogon->data.nt5_ex.forest = realm;
266 netlogon->data.nt5_ex.dns_domain = dns_domain;
267 netlogon->data.nt5_ex.pdc_dns_name = pdc_dns_name;
268 netlogon->data.nt5_ex.domain = flatname;
269 netlogon->data.nt5_ex.pdc_name = lp_netbios_name(lp_ctx);
270 netlogon->data.nt5_ex.user_name = user;
271 netlogon->data.nt5_ex.server_site = server_site;
272 netlogon->data.nt5_ex.client_site = client_site;
274 if (version & NETLOGON_NT_VERSION_5EX_WITH_IP) {
275 /* Clearly this needs to be fixed up for IPv6 */
276 extra_flags = NETLOGON_NT_VERSION_5EX_WITH_IP;
277 netlogon->data.nt5_ex.sockaddr.sockaddr_family = 2;
278 netlogon->data.nt5_ex.sockaddr.pdc_ip = pdc_ip;
279 netlogon->data.nt5_ex.sockaddr.remaining = data_blob_talloc_zero(mem_ctx, 8);
281 netlogon->data.nt5_ex.nt_version = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5EX|extra_flags;
282 netlogon->data.nt5_ex.lmnt_token = 0xFFFF;
283 netlogon->data.nt5_ex.lm20_token = 0xFFFF;
285 } else if (version & NETLOGON_NT_VERSION_5) {
286 netlogon->ntver = NETLOGON_NT_VERSION_5;
288 /* could check if the user exists */
289 if (user_known) {
290 netlogon->data.nt5.command = LOGON_SAM_LOGON_RESPONSE;
291 } else {
292 netlogon->data.nt5.command = LOGON_SAM_LOGON_USER_UNKNOWN;
294 netlogon->data.nt5.pdc_name = pdc_name;
295 netlogon->data.nt5.user_name = user;
296 netlogon->data.nt5.domain_name = flatname;
297 netlogon->data.nt5.domain_uuid = domain_uuid;
298 netlogon->data.nt5.forest = realm;
299 netlogon->data.nt5.dns_domain = dns_domain;
300 netlogon->data.nt5.pdc_dns_name = pdc_dns_name;
301 netlogon->data.nt5.pdc_ip = pdc_ip;
302 netlogon->data.nt5.server_type = server_type;
303 netlogon->data.nt5.nt_version = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5;
304 netlogon->data.nt5.lmnt_token = 0xFFFF;
305 netlogon->data.nt5.lm20_token = 0xFFFF;
307 } else /* (version & NETLOGON_NT_VERSION_1) and all other cases */ {
308 netlogon->ntver = NETLOGON_NT_VERSION_1;
309 /* could check if the user exists */
310 if (user_known) {
311 netlogon->data.nt4.command = LOGON_SAM_LOGON_RESPONSE;
312 } else {
313 netlogon->data.nt4.command = LOGON_SAM_LOGON_USER_UNKNOWN;
315 netlogon->data.nt4.server = pdc_name;
316 netlogon->data.nt4.user_name = user;
317 netlogon->data.nt4.domain = flatname;
318 netlogon->data.nt4.nt_version = NETLOGON_NT_VERSION_1;
319 netlogon->data.nt4.lmnt_token = 0xFFFF;
320 netlogon->data.nt4.lm20_token = 0xFFFF;
323 return NT_STATUS_OK;
328 handle incoming cldap requests
330 void cldapd_netlogon_request(struct cldap_socket *cldap,
331 struct cldapd_server *cldapd,
332 TALLOC_CTX *tmp_ctx,
333 uint32_t message_id,
334 struct ldb_parse_tree *tree,
335 struct tsocket_address *src)
337 int i;
338 const char *domain = NULL;
339 const char *host = NULL;
340 const char *user = NULL;
341 const char *domain_guid = NULL;
342 const char *domain_sid = NULL;
343 int acct_control = -1;
344 int version = -1;
345 struct netlogon_samlogon_response netlogon;
346 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
348 if (tree->operation != LDB_OP_AND) goto failed;
350 /* extract the query elements */
351 for (i=0;i<tree->u.list.num_elements;i++) {
352 struct ldb_parse_tree *t = tree->u.list.elements[i];
353 if (t->operation != LDB_OP_EQUALITY) goto failed;
354 if (strcasecmp(t->u.equality.attr, "DnsDomain") == 0) {
355 domain = talloc_strndup(tmp_ctx,
356 (const char *)t->u.equality.value.data,
357 t->u.equality.value.length);
359 if (strcasecmp(t->u.equality.attr, "Host") == 0) {
360 host = talloc_strndup(tmp_ctx,
361 (const char *)t->u.equality.value.data,
362 t->u.equality.value.length);
364 if (strcasecmp(t->u.equality.attr, "DomainGuid") == 0) {
365 NTSTATUS enc_status;
366 struct GUID guid;
367 enc_status = ldap_decode_ndr_GUID(tmp_ctx,
368 t->u.equality.value, &guid);
369 if (NT_STATUS_IS_OK(enc_status)) {
370 domain_guid = GUID_string(tmp_ctx, &guid);
373 if (strcasecmp(t->u.equality.attr, "DomainSid") == 0) {
374 domain_sid = talloc_strndup(tmp_ctx,
375 (const char *)t->u.equality.value.data,
376 t->u.equality.value.length);
378 if (strcasecmp(t->u.equality.attr, "User") == 0) {
379 user = talloc_strndup(tmp_ctx,
380 (const char *)t->u.equality.value.data,
381 t->u.equality.value.length);
383 if (strcasecmp(t->u.equality.attr, "NtVer") == 0 &&
384 t->u.equality.value.length == 4) {
385 version = IVAL(t->u.equality.value.data, 0);
387 if (strcasecmp(t->u.equality.attr, "AAC") == 0 &&
388 t->u.equality.value.length == 4) {
389 acct_control = IVAL(t->u.equality.value.data, 0);
393 if (domain_guid == NULL && domain == NULL) {
394 domain = lp_realm(cldapd->task->lp_ctx);
397 if (version == -1) {
398 goto failed;
401 DEBUG(5,("cldap netlogon query domain=%s host=%s user=%s version=%d guid=%s\n",
402 domain, host, user, version, domain_guid));
404 status = fill_netlogon_samlogon_response(cldapd->samctx, tmp_ctx, domain, NULL, NULL, domain_guid,
405 user, acct_control,
406 tsocket_address_inet_addr_string(src, tmp_ctx),
407 version, cldapd->task->lp_ctx, &netlogon);
408 if (!NT_STATUS_IS_OK(status)) {
409 goto failed;
412 status = cldap_netlogon_reply(cldap,
413 lp_iconv_convenience(cldapd->task->lp_ctx),
414 message_id, src, version,
415 &netlogon);
416 if (!NT_STATUS_IS_OK(status)) {
417 goto failed;
420 return;
422 failed:
423 DEBUG(2,("cldap netlogon query failed domain=%s host=%s version=%d - %s\n",
424 domain, host, version, nt_errstr(status)));
425 cldap_empty_reply(cldap, message_id, src);