s4-lsa: fixed breakage of lsa server
[Samba/cd1.git] / source4 / cldap_server / netlogon.c
blobc565f2fb1ad2ac962464a6a7aa550bffff3e28e5
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 *dns_domain;
65 const char *pdc_dns_name;
66 const char *flatname;
67 const char *server_site;
68 const char *client_site;
69 const char *pdc_ip;
70 struct ldb_dn *domain_dn = NULL;
71 struct interface *ifaces;
72 bool user_known;
73 NTSTATUS status;
75 /* the domain has an optional trailing . */
76 if (domain && domain[strlen(domain)-1] == '.') {
77 domain = talloc_strndup(mem_ctx, domain, strlen(domain)-1);
80 if (domain && strcasecmp_m(domain, lp_dnsdomain(lp_ctx)) == 0) {
81 domain_dn = ldb_get_default_basedn(sam_ctx);
84 if (netbios_domain && strcasecmp_m(domain, lp_sam_name(lp_ctx))) {
85 domain_dn = ldb_get_default_basedn(sam_ctx);
88 if (domain_dn) {
89 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
90 domain_dn, LDB_SCOPE_BASE, dom_attrs,
91 "objectClass=domain");
92 if (ret != LDB_SUCCESS) {
93 DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n", domain, ldb_dn_get_linearized(domain_dn), ldb_errstring(sam_ctx)));
94 return NT_STATUS_NO_SUCH_DOMAIN;
96 if (dom_res->count != 1) {
97 DEBUG(2,("Error finding domain '%s'/'%s' in sam\n", domain, ldb_dn_get_linearized(domain_dn)));
98 return NT_STATUS_NO_SUCH_DOMAIN;
102 if ((dom_res == NULL || dom_res->count == 0) && (domain_guid || domain_sid)) {
104 if (domain_guid) {
105 struct GUID binary_guid;
106 struct ldb_val guid_val;
107 enum ndr_err_code ndr_err;
109 /* By this means, we ensure we don't have funny stuff in the GUID */
111 status = GUID_from_string(domain_guid, &binary_guid);
112 if (!NT_STATUS_IS_OK(status)) {
113 return status;
116 /* And this gets the result into the binary format we want anyway */
117 ndr_err = ndr_push_struct_blob(&guid_val, mem_ctx, NULL, &binary_guid,
118 (ndr_push_flags_fn_t)ndr_push_GUID);
119 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
120 return NT_STATUS_INVALID_PARAMETER;
122 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
123 NULL, LDB_SCOPE_SUBTREE,
124 dom_attrs,
125 "(&(objectCategory=DomainDNS)(objectGUID=%s))",
126 ldb_binary_encode(mem_ctx, guid_val));
127 } else { /* domain_sid case */
128 struct dom_sid *sid;
129 struct ldb_val sid_val;
130 enum ndr_err_code ndr_err;
132 /* Rather than go via the string, just push into the NDR form */
133 ndr_err = ndr_push_struct_blob(&sid_val, mem_ctx, NULL, &sid,
134 (ndr_push_flags_fn_t)ndr_push_dom_sid);
135 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
136 return NT_STATUS_INVALID_PARAMETER;
139 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
140 NULL, LDB_SCOPE_SUBTREE,
141 dom_attrs,
142 "(&(objectCategory=DomainDNS)(objectSID=%s))",
143 ldb_binary_encode(mem_ctx, sid_val));
146 if (ret != LDB_SUCCESS) {
147 DEBUG(2,("Unable to find referece to GUID '%s' or SID %s in sam: %s\n",
148 domain_guid, dom_sid_string(mem_ctx, domain_sid),
149 ldb_errstring(sam_ctx)));
150 return NT_STATUS_NO_SUCH_DOMAIN;
151 } else if (dom_res->count == 1) {
152 /* Ok, now just check it is our domain */
154 if (ldb_dn_compare(ldb_get_default_basedn(sam_ctx), dom_res->msgs[0]->dn) != 0) {
155 return NT_STATUS_NO_SUCH_DOMAIN;
157 } else if (dom_res->count > 1) {
158 return NT_STATUS_NO_SUCH_DOMAIN;
163 if ((dom_res == NULL || dom_res->count == 0)) {
164 DEBUG(2,("Unable to find domain with name %s or GUID {%s}\n", domain, domain_guid));
165 return NT_STATUS_NO_SUCH_DOMAIN;
168 /* work around different inputs for not-specified users */
169 if (!user) {
170 user = "";
173 /* Enquire about any valid username with just a CLDAP packet -
174 * if kerberos didn't also do this, the security folks would
175 * scream... */
176 if (user[0]) { \
177 /* Only allow some bits to be enquired: [MS-ATDS] 7.3.3.2 */
178 if (acct_control == (uint32_t)-1) {
179 acct_control = 0;
181 acct_control = acct_control & (ACB_TEMPDUP | ACB_NORMAL | ACB_DOMTRUST | ACB_WSTRUST | ACB_SVRTRUST);
183 /* We must exclude disabled accounts, but otherwise do the bitwise match the client asked for */
184 ret = ldb_search(sam_ctx, mem_ctx, &user_res,
185 dom_res->msgs[0]->dn, LDB_SCOPE_SUBTREE,
186 none_attrs,
187 "(&(objectClass=user)(samAccountName=%s)"
188 "(!(userAccountControl:" LDB_OID_COMPARATOR_AND ":=%u))"
189 "(userAccountControl:" LDB_OID_COMPARATOR_OR ":=%u))",
190 ldb_binary_encode_string(mem_ctx, user),
191 UF_ACCOUNTDISABLE, ds_acb2uf(acct_control));
192 if (ret != LDB_SUCCESS) {
193 DEBUG(2,("Unable to find referece to user '%s' with ACB 0x%8x under %s: %s\n",
194 user, acct_control, ldb_dn_get_linearized(dom_res->msgs[0]->dn),
195 ldb_errstring(sam_ctx)));
196 return NT_STATUS_NO_SUCH_USER;
197 } else if (user_res->count == 1) {
198 user_known = true;
199 } else {
200 user_known = false;
203 } else {
204 user_known = true;
207 server_type =
208 DS_SERVER_DS | DS_SERVER_TIMESERV |
209 DS_SERVER_CLOSEST | DS_SERVER_WRITABLE |
210 DS_SERVER_GOOD_TIMESERV;
212 #if 0
213 /* w2k8-r2 as a DC does not claim these */
214 server_type |= DS_DNS_CONTROLLER | DS_DNS_DOMAIN;
215 #endif
217 if (samdb_is_pdc(sam_ctx)) {
218 int *domainFunctionality;
219 server_type |= DS_SERVER_PDC;
220 domainFunctionality = talloc_get_type(ldb_get_opaque(sam_ctx, "domainFunctionality"), int);
221 if (domainFunctionality && *domainFunctionality >= DS_DOMAIN_FUNCTION_2008) {
222 server_type |= DS_SERVER_FULL_SECRET_DOMAIN_6;
226 if (samdb_is_gc(sam_ctx)) {
227 server_type |= DS_SERVER_GC;
230 if (str_list_check(services, "ldap")) {
231 server_type |= DS_SERVER_LDAP;
234 if (str_list_check(services, "kdc")) {
235 server_type |= DS_SERVER_KDC;
238 #if 0
239 /* w2k8-r2 as a sole DC does not claim this */
240 if (ldb_dn_compare(ldb_get_root_basedn(sam_ctx), ldb_get_default_basedn(sam_ctx)) == 0) {
241 server_type |= DS_DNS_FOREST_ROOT;
243 #endif
245 pdc_name = talloc_asprintf(mem_ctx, "\\\\%s", lp_netbios_name(lp_ctx));
246 domain_uuid = samdb_result_guid(dom_res->msgs[0], "objectGUID");
247 dns_domain = lp_dnsdomain(lp_ctx);
248 pdc_dns_name = talloc_asprintf(mem_ctx, "%s.%s",
249 strlower_talloc(mem_ctx,
250 lp_netbios_name(lp_ctx)),
251 dns_domain);
253 flatname = lp_sam_name(lp_ctx);
254 server_site = samdb_server_site_name(sam_ctx, mem_ctx);
255 /* FIXME: Hardcoded site name */
256 client_site = "Default-First-Site-Name";
257 load_interfaces(mem_ctx, lp_interfaces(lp_ctx), &ifaces);
258 pdc_ip = iface_best_ip(ifaces, src_address);
260 ZERO_STRUCTP(netlogon);
262 /* check if either of these bits is present */
263 if (version & (NETLOGON_NT_VERSION_5EX|NETLOGON_NT_VERSION_5EX_WITH_IP)) {
264 uint32_t extra_flags = 0;
265 netlogon->ntver = NETLOGON_NT_VERSION_5EX;
267 /* could check if the user exists */
268 if (user_known) {
269 netlogon->data.nt5_ex.command = LOGON_SAM_LOGON_RESPONSE_EX;
270 } else {
271 netlogon->data.nt5_ex.command = LOGON_SAM_LOGON_USER_UNKNOWN_EX;
273 netlogon->data.nt5_ex.server_type = server_type;
274 netlogon->data.nt5_ex.domain_uuid = domain_uuid;
275 netlogon->data.nt5_ex.forest = dns_domain;
276 netlogon->data.nt5_ex.dns_domain = dns_domain;
277 netlogon->data.nt5_ex.pdc_dns_name = pdc_dns_name;
278 netlogon->data.nt5_ex.domain = flatname;
279 netlogon->data.nt5_ex.pdc_name = lp_netbios_name(lp_ctx);
280 netlogon->data.nt5_ex.user_name = user;
281 netlogon->data.nt5_ex.server_site = server_site;
282 netlogon->data.nt5_ex.client_site = client_site;
284 if (version & NETLOGON_NT_VERSION_5EX_WITH_IP) {
285 /* Clearly this needs to be fixed up for IPv6 */
286 extra_flags = NETLOGON_NT_VERSION_5EX_WITH_IP;
287 netlogon->data.nt5_ex.sockaddr.sockaddr_family = 2;
288 netlogon->data.nt5_ex.sockaddr.pdc_ip = pdc_ip;
289 netlogon->data.nt5_ex.sockaddr.remaining = data_blob_talloc_zero(mem_ctx, 8);
291 netlogon->data.nt5_ex.nt_version = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5EX|extra_flags;
292 netlogon->data.nt5_ex.lmnt_token = 0xFFFF;
293 netlogon->data.nt5_ex.lm20_token = 0xFFFF;
295 } else if (version & NETLOGON_NT_VERSION_5) {
296 netlogon->ntver = NETLOGON_NT_VERSION_5;
298 /* could check if the user exists */
299 if (user_known) {
300 netlogon->data.nt5.command = LOGON_SAM_LOGON_RESPONSE;
301 } else {
302 netlogon->data.nt5.command = LOGON_SAM_LOGON_USER_UNKNOWN;
304 netlogon->data.nt5.pdc_name = pdc_name;
305 netlogon->data.nt5.user_name = user;
306 netlogon->data.nt5.domain_name = flatname;
307 netlogon->data.nt5.domain_uuid = domain_uuid;
308 netlogon->data.nt5.forest = dns_domain;
309 netlogon->data.nt5.dns_domain = dns_domain;
310 netlogon->data.nt5.pdc_dns_name = pdc_dns_name;
311 netlogon->data.nt5.pdc_ip = pdc_ip;
312 netlogon->data.nt5.server_type = server_type;
313 netlogon->data.nt5.nt_version = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5;
314 netlogon->data.nt5.lmnt_token = 0xFFFF;
315 netlogon->data.nt5.lm20_token = 0xFFFF;
317 } else /* (version & NETLOGON_NT_VERSION_1) and all other cases */ {
318 netlogon->ntver = NETLOGON_NT_VERSION_1;
319 /* could check if the user exists */
320 if (user_known) {
321 netlogon->data.nt4.command = LOGON_SAM_LOGON_RESPONSE;
322 } else {
323 netlogon->data.nt4.command = LOGON_SAM_LOGON_USER_UNKNOWN;
325 netlogon->data.nt4.server = pdc_name;
326 netlogon->data.nt4.user_name = user;
327 netlogon->data.nt4.domain = flatname;
328 netlogon->data.nt4.nt_version = NETLOGON_NT_VERSION_1;
329 netlogon->data.nt4.lmnt_token = 0xFFFF;
330 netlogon->data.nt4.lm20_token = 0xFFFF;
333 return NT_STATUS_OK;
338 handle incoming cldap requests
340 void cldapd_netlogon_request(struct cldap_socket *cldap,
341 struct cldapd_server *cldapd,
342 TALLOC_CTX *tmp_ctx,
343 uint32_t message_id,
344 struct ldb_parse_tree *tree,
345 struct tsocket_address *src)
347 int i;
348 const char *domain = NULL;
349 const char *host = NULL;
350 const char *user = NULL;
351 const char *domain_guid = NULL;
352 const char *domain_sid = NULL;
353 int acct_control = -1;
354 int version = -1;
355 struct netlogon_samlogon_response netlogon;
356 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
358 if (tree->operation != LDB_OP_AND) goto failed;
360 /* extract the query elements */
361 for (i=0;i<tree->u.list.num_elements;i++) {
362 struct ldb_parse_tree *t = tree->u.list.elements[i];
363 if (t->operation != LDB_OP_EQUALITY) goto failed;
364 if (strcasecmp(t->u.equality.attr, "DnsDomain") == 0) {
365 domain = talloc_strndup(tmp_ctx,
366 (const char *)t->u.equality.value.data,
367 t->u.equality.value.length);
369 if (strcasecmp(t->u.equality.attr, "Host") == 0) {
370 host = talloc_strndup(tmp_ctx,
371 (const char *)t->u.equality.value.data,
372 t->u.equality.value.length);
374 if (strcasecmp(t->u.equality.attr, "DomainGuid") == 0) {
375 NTSTATUS enc_status;
376 struct GUID guid;
377 enc_status = ldap_decode_ndr_GUID(tmp_ctx,
378 t->u.equality.value, &guid);
379 if (NT_STATUS_IS_OK(enc_status)) {
380 domain_guid = GUID_string(tmp_ctx, &guid);
383 if (strcasecmp(t->u.equality.attr, "DomainSid") == 0) {
384 domain_sid = talloc_strndup(tmp_ctx,
385 (const char *)t->u.equality.value.data,
386 t->u.equality.value.length);
388 if (strcasecmp(t->u.equality.attr, "User") == 0) {
389 user = talloc_strndup(tmp_ctx,
390 (const char *)t->u.equality.value.data,
391 t->u.equality.value.length);
393 if (strcasecmp(t->u.equality.attr, "NtVer") == 0 &&
394 t->u.equality.value.length == 4) {
395 version = IVAL(t->u.equality.value.data, 0);
397 if (strcasecmp(t->u.equality.attr, "AAC") == 0 &&
398 t->u.equality.value.length == 4) {
399 acct_control = IVAL(t->u.equality.value.data, 0);
403 if (domain_guid == NULL && domain == NULL) {
404 domain = lp_dnsdomain(cldapd->task->lp_ctx);
407 if (version == -1) {
408 goto failed;
411 DEBUG(5,("cldap netlogon query domain=%s host=%s user=%s version=%d guid=%s\n",
412 domain, host, user, version, domain_guid));
414 status = fill_netlogon_samlogon_response(cldapd->samctx, tmp_ctx, domain, NULL, NULL, domain_guid,
415 user, acct_control,
416 tsocket_address_inet_addr_string(src, tmp_ctx),
417 version, cldapd->task->lp_ctx, &netlogon);
418 if (!NT_STATUS_IS_OK(status)) {
419 goto failed;
422 status = cldap_netlogon_reply(cldap,
423 lp_iconv_convenience(cldapd->task->lp_ctx),
424 message_id, src, version,
425 &netlogon);
426 if (!NT_STATUS_IS_OK(status)) {
427 goto failed;
430 return;
432 failed:
433 DEBUG(2,("cldap netlogon query failed domain=%s host=%s version=%d - %s\n",
434 domain, host, version, nt_errstr(status)));
435 cldap_empty_reply(cldap, message_id, src);