s4 python: make the function dsdb_get_oid_from_attid reachable from a samDB object
[Samba/ekacnet.git] / source4 / cldap_server / netlogon.c
blob139c1cafdcac45aefd70612b5f40b8b47db327bc
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 *forest_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 parameter could have an optional trailing "." */
77 if (domain && domain[strlen(domain)-1] == '.') {
78 domain = talloc_strndup(mem_ctx, domain, strlen(domain)-1);
79 NT_STATUS_HAVE_NO_MEMORY(domain);
82 /* Lookup using long or short domainname */
83 if (domain && (strcasecmp_m(domain, lp_dnsdomain(lp_ctx)) == 0)) {
84 domain_dn = ldb_get_default_basedn(sam_ctx);
86 if (netbios_domain && (strcasecmp_m(netbios_domain, lp_sam_name(lp_ctx)) == 0)) {
87 domain_dn = ldb_get_default_basedn(sam_ctx);
89 if (domain_dn) {
90 const char *domain_identifier = domain != NULL ? domain
91 : netbios_domain;
92 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
93 domain_dn, LDB_SCOPE_BASE, dom_attrs,
94 "objectClass=domain");
95 if (ret != LDB_SUCCESS) {
96 DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n",
97 domain_identifier,
98 ldb_dn_get_linearized(domain_dn),
99 ldb_errstring(sam_ctx)));
100 return NT_STATUS_NO_SUCH_DOMAIN;
102 if (dom_res->count != 1) {
103 DEBUG(2,("Error finding domain '%s'/'%s' in sam\n",
104 domain_identifier,
105 ldb_dn_get_linearized(domain_dn)));
106 return NT_STATUS_NO_SUCH_DOMAIN;
110 /* Lookup using GUID or SID */
111 if ((dom_res == NULL) && (domain_guid || domain_sid)) {
112 if (domain_guid) {
113 struct GUID binary_guid;
114 struct ldb_val guid_val;
116 /* By this means, we ensure we don't have funny stuff in the GUID */
118 status = GUID_from_string(domain_guid, &binary_guid);
119 if (!NT_STATUS_IS_OK(status)) {
120 return status;
123 /* And this gets the result into the binary format we want anyway */
124 status = GUID_to_ndr_blob(&binary_guid, mem_ctx, &guid_val);
125 if (!NT_STATUS_IS_OK(status)) {
126 return status;
128 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
129 NULL, LDB_SCOPE_SUBTREE,
130 dom_attrs,
131 "(&(objectCategory=DomainDNS)(objectGUID=%s))",
132 ldb_binary_encode(mem_ctx, guid_val));
133 } else { /* domain_sid case */
134 struct dom_sid *sid;
135 struct ldb_val sid_val;
136 enum ndr_err_code ndr_err;
138 /* Rather than go via the string, just push into the NDR form */
139 ndr_err = ndr_push_struct_blob(&sid_val, mem_ctx, NULL, &sid,
140 (ndr_push_flags_fn_t)ndr_push_dom_sid);
141 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
142 return NT_STATUS_INVALID_PARAMETER;
145 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
146 NULL, LDB_SCOPE_SUBTREE,
147 dom_attrs,
148 "(&(objectCategory=DomainDNS)(objectSID=%s))",
149 ldb_binary_encode(mem_ctx, sid_val));
152 if (ret != LDB_SUCCESS) {
153 DEBUG(2,("Unable to find a correct reference to GUID '%s' or SID '%s' in sam: %s\n",
154 domain_guid, dom_sid_string(mem_ctx, domain_sid),
155 ldb_errstring(sam_ctx)));
156 return NT_STATUS_NO_SUCH_DOMAIN;
157 } else if (dom_res->count == 1) {
158 /* Ok, now just check it is our domain */
159 if (ldb_dn_compare(ldb_get_default_basedn(sam_ctx),
160 dom_res->msgs[0]->dn) != 0) {
161 DEBUG(2,("The GUID '%s' or SID '%s' doesn't identify our domain\n",
162 domain_guid,
163 dom_sid_string(mem_ctx, domain_sid)));
164 return NT_STATUS_NO_SUCH_DOMAIN;
166 } else {
167 DEBUG(2,("Unable to find a correct reference to GUID '%s' or SID '%s' in sam\n",
168 domain_guid, dom_sid_string(mem_ctx, domain_sid)));
169 return NT_STATUS_NO_SUCH_DOMAIN;
173 if (dom_res == NULL) {
174 DEBUG(2,("Unable to get domain informations if no parameter of the list [long domainname, short domainname, GUID, SID] was specified!\n"));
175 return NT_STATUS_NO_SUCH_DOMAIN;
178 /* work around different inputs for not-specified users */
179 if (!user) {
180 user = "";
183 /* Enquire about any valid username with just a CLDAP packet -
184 * if kerberos didn't also do this, the security folks would
185 * scream... */
186 if (user[0]) { \
187 /* Only allow some bits to be enquired: [MS-ATDS] 7.3.3.2 */
188 if (acct_control == (uint32_t)-1) {
189 acct_control = 0;
191 acct_control = acct_control & (ACB_TEMPDUP | ACB_NORMAL | ACB_DOMTRUST | ACB_WSTRUST | ACB_SVRTRUST);
193 /* We must exclude disabled accounts, but otherwise do the bitwise match the client asked for */
194 ret = ldb_search(sam_ctx, mem_ctx, &user_res,
195 dom_res->msgs[0]->dn, LDB_SCOPE_SUBTREE,
196 none_attrs,
197 "(&(objectClass=user)(samAccountName=%s)"
198 "(!(userAccountControl:" LDB_OID_COMPARATOR_AND ":=%u))"
199 "(userAccountControl:" LDB_OID_COMPARATOR_OR ":=%u))",
200 ldb_binary_encode_string(mem_ctx, user),
201 UF_ACCOUNTDISABLE, ds_acb2uf(acct_control));
202 if (ret != LDB_SUCCESS) {
203 DEBUG(2,("Unable to find reference to user '%s' with ACB 0x%8x under %s: %s\n",
204 user, acct_control, ldb_dn_get_linearized(dom_res->msgs[0]->dn),
205 ldb_errstring(sam_ctx)));
206 return NT_STATUS_NO_SUCH_USER;
207 } else if (user_res->count == 1) {
208 user_known = true;
209 } else {
210 user_known = false;
213 } else {
214 user_known = true;
217 server_type =
218 DS_SERVER_DS | DS_SERVER_TIMESERV |
219 DS_SERVER_CLOSEST | DS_SERVER_WRITABLE |
220 DS_SERVER_GOOD_TIMESERV;
222 #if 0
223 /* w2k8-r2 as a DC does not claim these */
224 server_type |= DS_DNS_CONTROLLER | DS_DNS_DOMAIN;
225 #endif
227 if (samdb_is_pdc(sam_ctx)) {
228 server_type |= DS_SERVER_PDC;
229 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008) {
230 server_type |= DS_SERVER_FULL_SECRET_DOMAIN_6;
234 if (samdb_is_gc(sam_ctx)) {
235 server_type |= DS_SERVER_GC;
238 if (str_list_check(services, "ldap")) {
239 server_type |= DS_SERVER_LDAP;
242 if (str_list_check(services, "kdc")) {
243 server_type |= DS_SERVER_KDC;
246 #if 0
247 /* w2k8-r2 as a sole DC does not claim this */
248 if (ldb_dn_compare(ldb_get_root_basedn(sam_ctx), ldb_get_default_basedn(sam_ctx)) == 0) {
249 server_type |= DS_DNS_FOREST_ROOT;
251 #endif
253 pdc_name = talloc_asprintf(mem_ctx, "\\\\%s",
254 lp_netbios_name(lp_ctx));
255 NT_STATUS_HAVE_NO_MEMORY(pdc_name);
256 domain_uuid = samdb_result_guid(dom_res->msgs[0], "objectGUID");
257 dns_domain = lp_dnsdomain(lp_ctx);
258 forest_domain = samdb_forest_name(sam_ctx, mem_ctx);
259 NT_STATUS_HAVE_NO_MEMORY(forest_domain);
260 pdc_dns_name = talloc_asprintf(mem_ctx, "%s.%s",
261 strlower_talloc(mem_ctx,
262 lp_netbios_name(lp_ctx)),
263 dns_domain);
264 NT_STATUS_HAVE_NO_MEMORY(pdc_dns_name);
265 flatname = lp_sam_name(lp_ctx);
266 server_site = samdb_server_site_name(sam_ctx, mem_ctx);
267 NT_STATUS_HAVE_NO_MEMORY(server_site);
268 /* FIXME: Hardcoded site name */
269 client_site = "Default-First-Site-Name";
270 load_interfaces(mem_ctx, lp_interfaces(lp_ctx), &ifaces);
271 pdc_ip = iface_best_ip(ifaces, src_address);
273 ZERO_STRUCTP(netlogon);
275 /* check if either of these bits is present */
276 if (version & (NETLOGON_NT_VERSION_5EX|NETLOGON_NT_VERSION_5EX_WITH_IP)) {
277 uint32_t extra_flags = 0;
278 netlogon->ntver = NETLOGON_NT_VERSION_5EX;
280 /* could check if the user exists */
281 if (user_known) {
282 netlogon->data.nt5_ex.command = LOGON_SAM_LOGON_RESPONSE_EX;
283 } else {
284 netlogon->data.nt5_ex.command = LOGON_SAM_LOGON_USER_UNKNOWN_EX;
286 netlogon->data.nt5_ex.server_type = server_type;
287 netlogon->data.nt5_ex.domain_uuid = domain_uuid;
288 netlogon->data.nt5_ex.forest = forest_domain;
289 netlogon->data.nt5_ex.dns_domain = dns_domain;
290 netlogon->data.nt5_ex.pdc_dns_name = pdc_dns_name;
291 netlogon->data.nt5_ex.domain = flatname;
292 netlogon->data.nt5_ex.pdc_name = lp_netbios_name(lp_ctx);
293 netlogon->data.nt5_ex.user_name = user;
294 netlogon->data.nt5_ex.server_site = server_site;
295 netlogon->data.nt5_ex.client_site = client_site;
297 if (version & NETLOGON_NT_VERSION_5EX_WITH_IP) {
298 /* Clearly this needs to be fixed up for IPv6 */
299 extra_flags = NETLOGON_NT_VERSION_5EX_WITH_IP;
300 netlogon->data.nt5_ex.sockaddr.sockaddr_family = 2;
301 netlogon->data.nt5_ex.sockaddr.pdc_ip = pdc_ip;
302 netlogon->data.nt5_ex.sockaddr.remaining = data_blob_talloc_zero(mem_ctx, 8);
304 netlogon->data.nt5_ex.nt_version = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5EX|extra_flags;
305 netlogon->data.nt5_ex.lmnt_token = 0xFFFF;
306 netlogon->data.nt5_ex.lm20_token = 0xFFFF;
308 } else if (version & NETLOGON_NT_VERSION_5) {
309 netlogon->ntver = NETLOGON_NT_VERSION_5;
311 /* could check if the user exists */
312 if (user_known) {
313 netlogon->data.nt5.command = LOGON_SAM_LOGON_RESPONSE;
314 } else {
315 netlogon->data.nt5.command = LOGON_SAM_LOGON_USER_UNKNOWN;
317 netlogon->data.nt5.pdc_name = pdc_name;
318 netlogon->data.nt5.user_name = user;
319 netlogon->data.nt5.domain_name = flatname;
320 netlogon->data.nt5.domain_uuid = domain_uuid;
321 netlogon->data.nt5.forest = forest_domain;
322 netlogon->data.nt5.dns_domain = dns_domain;
323 netlogon->data.nt5.pdc_dns_name = pdc_dns_name;
324 netlogon->data.nt5.pdc_ip = pdc_ip;
325 netlogon->data.nt5.server_type = server_type;
326 netlogon->data.nt5.nt_version = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5;
327 netlogon->data.nt5.lmnt_token = 0xFFFF;
328 netlogon->data.nt5.lm20_token = 0xFFFF;
330 } else /* (version & NETLOGON_NT_VERSION_1) and all other cases */ {
331 netlogon->ntver = NETLOGON_NT_VERSION_1;
332 /* could check if the user exists */
333 if (user_known) {
334 netlogon->data.nt4.command = LOGON_SAM_LOGON_RESPONSE;
335 } else {
336 netlogon->data.nt4.command = LOGON_SAM_LOGON_USER_UNKNOWN;
338 netlogon->data.nt4.server = pdc_name;
339 netlogon->data.nt4.user_name = user;
340 netlogon->data.nt4.domain = flatname;
341 netlogon->data.nt4.nt_version = NETLOGON_NT_VERSION_1;
342 netlogon->data.nt4.lmnt_token = 0xFFFF;
343 netlogon->data.nt4.lm20_token = 0xFFFF;
346 return NT_STATUS_OK;
351 handle incoming cldap requests
353 void cldapd_netlogon_request(struct cldap_socket *cldap,
354 struct cldapd_server *cldapd,
355 TALLOC_CTX *tmp_ctx,
356 uint32_t message_id,
357 struct ldb_parse_tree *tree,
358 struct tsocket_address *src)
360 unsigned int i;
361 const char *domain = NULL;
362 const char *host = NULL;
363 const char *user = NULL;
364 const char *domain_guid = NULL;
365 const char *domain_sid = NULL;
366 int acct_control = -1;
367 int version = -1;
368 struct netlogon_samlogon_response netlogon;
369 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
371 if (tree->operation != LDB_OP_AND) goto failed;
373 /* extract the query elements */
374 for (i=0;i<tree->u.list.num_elements;i++) {
375 struct ldb_parse_tree *t = tree->u.list.elements[i];
376 if (t->operation != LDB_OP_EQUALITY) goto failed;
377 if (strcasecmp(t->u.equality.attr, "DnsDomain") == 0) {
378 domain = talloc_strndup(tmp_ctx,
379 (const char *)t->u.equality.value.data,
380 t->u.equality.value.length);
382 if (strcasecmp(t->u.equality.attr, "Host") == 0) {
383 host = talloc_strndup(tmp_ctx,
384 (const char *)t->u.equality.value.data,
385 t->u.equality.value.length);
387 if (strcasecmp(t->u.equality.attr, "DomainGuid") == 0) {
388 NTSTATUS enc_status;
389 struct GUID guid;
390 enc_status = ldap_decode_ndr_GUID(tmp_ctx,
391 t->u.equality.value, &guid);
392 if (NT_STATUS_IS_OK(enc_status)) {
393 domain_guid = GUID_string(tmp_ctx, &guid);
396 if (strcasecmp(t->u.equality.attr, "DomainSid") == 0) {
397 domain_sid = talloc_strndup(tmp_ctx,
398 (const char *)t->u.equality.value.data,
399 t->u.equality.value.length);
401 if (strcasecmp(t->u.equality.attr, "User") == 0) {
402 user = talloc_strndup(tmp_ctx,
403 (const char *)t->u.equality.value.data,
404 t->u.equality.value.length);
406 if (strcasecmp(t->u.equality.attr, "NtVer") == 0 &&
407 t->u.equality.value.length == 4) {
408 version = IVAL(t->u.equality.value.data, 0);
410 if (strcasecmp(t->u.equality.attr, "AAC") == 0 &&
411 t->u.equality.value.length == 4) {
412 acct_control = IVAL(t->u.equality.value.data, 0);
416 if (domain_guid == NULL && domain == NULL) {
417 domain = lp_dnsdomain(cldapd->task->lp_ctx);
420 if (version == -1) {
421 goto failed;
424 DEBUG(5,("cldap netlogon query domain=%s host=%s user=%s version=%d guid=%s\n",
425 domain, host, user, version, domain_guid));
427 status = fill_netlogon_samlogon_response(cldapd->samctx, tmp_ctx, domain, NULL, NULL, domain_guid,
428 user, acct_control,
429 tsocket_address_inet_addr_string(src, tmp_ctx),
430 version, cldapd->task->lp_ctx, &netlogon);
431 if (!NT_STATUS_IS_OK(status)) {
432 goto failed;
435 status = cldap_netlogon_reply(cldap,
436 lp_iconv_convenience(cldapd->task->lp_ctx),
437 message_id, src, version,
438 &netlogon);
439 if (!NT_STATUS_IS_OK(status)) {
440 goto failed;
443 return;
445 failed:
446 DEBUG(2,("cldap netlogon query failed domain=%s host=%s version=%d - %s\n",
447 domain, host, version, nt_errstr(status)));
448 cldap_empty_reply(cldap, message_id, src);