0c4abebf8354a0da73d386325e0650e2b34ecf7d
[samba.git] / source4 / cldap_server / netlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    CLDAP server - netlogon handling
5
6    Copyright (C) Andrew Tridgell        2005
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008
8    
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.
13    
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.
18    
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/>.
21 */
22
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"
40 #include "lib/util/util_net.h"
41
42 /*
43   fill in the cldap netlogon union for a given version
44 */
45 NTSTATUS fill_netlogon_samlogon_response(struct ldb_context *sam_ctx,
46                                          TALLOC_CTX *mem_ctx,
47                                          const char *domain,
48                                          const char *netbios_domain,
49                                          struct dom_sid *domain_sid,
50                                          const char *domain_guid,
51                                          const char *user,
52                                          uint32_t acct_control,
53                                          const char *src_address,
54                                          uint32_t version,
55                                          struct loadparm_context *lp_ctx,
56                                          struct netlogon_samlogon_response *netlogon,
57                                          bool fill_on_blank_request)
58 {
59         const char *dom_attrs[] = {"objectGUID", NULL};
60         const char *none_attrs[] = {NULL};
61         struct ldb_result *dom_res = NULL, *user_res = NULL;
62         int ret;
63         const char **services = lpcfg_server_services(lp_ctx);
64         uint32_t server_type;
65         const char *pdc_name;
66         struct GUID domain_uuid;
67         const char *dns_domain;
68         const char *forest_domain;
69         const char *pdc_dns_name;
70         const char *flatname;
71         const char *server_site;
72         const char *client_site;
73         const char *pdc_ip;
74         struct ldb_dn *domain_dn = NULL;
75         struct interface *ifaces;
76         bool user_known, am_rodc;
77         NTSTATUS status;
78
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);
83         }
84
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);
88         }
89         if (netbios_domain && (strcasecmp_m(netbios_domain, lpcfg_sam_name(lp_ctx)) == 0)) {
90                 domain_dn = ldb_get_default_basedn(sam_ctx);
91         }
92         if (domain_dn) {
93                 const char *domain_identifier = domain != NULL ? domain
94                                                         : netbios_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",
100                                  domain_identifier,
101                                  ldb_dn_get_linearized(domain_dn),
102                                  ldb_errstring(sam_ctx)));
103                         return NT_STATUS_NO_SUCH_DOMAIN;
104                 }
105                 if (dom_res->count != 1) {
106                         DEBUG(2,("Error finding domain '%s'/'%s' in sam\n",
107                                  domain_identifier,
108                                  ldb_dn_get_linearized(domain_dn)));
109                         return NT_STATUS_NO_SUCH_DOMAIN;
110                 }
111         }
112
113         /* Lookup using GUID or SID */
114         if ((dom_res == NULL) && (domain_guid || domain_sid)) {
115                 if (domain_guid) {
116                         struct GUID binary_guid;
117                         struct ldb_val guid_val;
118
119                         /* By this means, we ensure we don't have funny stuff in the GUID */
120
121                         status = GUID_from_string(domain_guid, &binary_guid);
122                         if (!NT_STATUS_IS_OK(status)) {
123                                 return status;
124                         }
125
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)) {
129                                 return status;
130                         }
131                         ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
132                                                  NULL, LDB_SCOPE_SUBTREE, 
133                                                  dom_attrs, 
134                                                  "(&(objectCategory=DomainDNS)(objectGUID=%s))", 
135                                                  ldb_binary_encode(mem_ctx, guid_val));
136                 } else { /* domain_sid case */
137                         ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
138                                          NULL, LDB_SCOPE_SUBTREE,
139                                          dom_attrs,
140                                          "(&(objectCategory=DomainDNS)(objectSid=%s))",
141                                          dom_sid_string(mem_ctx, domain_sid));
142                 }
143                 
144                 if (ret != LDB_SUCCESS) {
145                         DEBUG(2,("Unable to find a correct reference to GUID '%s' or SID '%s' in sam: %s\n",
146                                  domain_guid, dom_sid_string(mem_ctx, domain_sid),
147                                  ldb_errstring(sam_ctx)));
148                         return NT_STATUS_NO_SUCH_DOMAIN;
149                 } else if (dom_res->count == 1) {
150                         /* Ok, now just check it is our domain */
151                         if (ldb_dn_compare(ldb_get_default_basedn(sam_ctx),
152                                            dom_res->msgs[0]->dn) != 0) {
153                                 DEBUG(2,("The GUID '%s' or SID '%s' doesn't identify our domain\n",
154                                          domain_guid,
155                                          dom_sid_string(mem_ctx, domain_sid)));
156                                 return NT_STATUS_NO_SUCH_DOMAIN;
157                         }
158                 } else {
159                         DEBUG(2,("Unable to find a correct reference to GUID '%s' or SID '%s' in sam\n",
160                                  domain_guid, dom_sid_string(mem_ctx, domain_sid)));
161                         return NT_STATUS_NO_SUCH_DOMAIN;
162                 }
163         }
164
165         if (dom_res == NULL && fill_on_blank_request) {
166                 /* blank inputs gives our domain - tested against
167                    w2k8r2. Without this ADUC on Win7 won't start */
168                 domain_dn = ldb_get_default_basedn(sam_ctx);
169                 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
170                                  domain_dn, LDB_SCOPE_BASE, dom_attrs,
171                                  "objectClass=domain");
172                 if (ret != LDB_SUCCESS) {
173                         DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n",
174                                  lpcfg_dnsdomain(lp_ctx),
175                                  ldb_dn_get_linearized(domain_dn),
176                                  ldb_errstring(sam_ctx)));
177                         return NT_STATUS_NO_SUCH_DOMAIN;
178                 }
179         }
180
181         if (dom_res == NULL) {
182                 DEBUG(2,(__location__ ": Unable to get domain information with no inputs\n"));
183                 return NT_STATUS_NO_SUCH_DOMAIN;
184         }
185
186         /* work around different inputs for not-specified users */
187         if (!user) {
188                 user = "";
189         }
190
191         /* Enquire about any valid username with just a CLDAP packet -
192          * if kerberos didn't also do this, the security folks would
193          * scream... */
194         if (user[0]) {                                                  \
195                 /* Only allow some bits to be enquired:  [MS-ATDS] 7.3.3.2 */
196                 if (acct_control == (uint32_t)-1) {
197                         acct_control = 0;
198                 }
199                 acct_control = acct_control & (ACB_TEMPDUP | ACB_NORMAL | ACB_DOMTRUST | ACB_WSTRUST | ACB_SVRTRUST);
200
201                 /* We must exclude disabled accounts, but otherwise do the bitwise match the client asked for */
202                 ret = ldb_search(sam_ctx, mem_ctx, &user_res,
203                                          dom_res->msgs[0]->dn, LDB_SCOPE_SUBTREE, 
204                                          none_attrs, 
205                                          "(&(objectClass=user)(samAccountName=%s)"
206                                          "(!(userAccountControl:" LDB_OID_COMPARATOR_AND ":=%u))"
207                                          "(userAccountControl:" LDB_OID_COMPARATOR_OR ":=%u))", 
208                                          ldb_binary_encode_string(mem_ctx, user),
209                                          UF_ACCOUNTDISABLE, ds_acb2uf(acct_control));
210                 if (ret != LDB_SUCCESS) {
211                         DEBUG(2,("Unable to find reference to user '%s' with ACB 0x%8x under %s: %s\n",
212                                  user, acct_control, ldb_dn_get_linearized(dom_res->msgs[0]->dn),
213                                  ldb_errstring(sam_ctx)));
214                         return NT_STATUS_NO_SUCH_USER;
215                 } else if (user_res->count == 1) {
216                         user_known = true;
217                 } else {
218                         user_known = false;
219                 }
220
221         } else {
222                 user_known = true;
223         }
224                 
225         server_type      = 
226                 DS_SERVER_DS | DS_SERVER_TIMESERV |
227                 DS_SERVER_GOOD_TIMESERV;
228
229 #if 0
230         /* w2k8-r2 as a DC does not claim these */
231         server_type |= DS_DNS_CONTROLLER | DS_DNS_DOMAIN;
232 #endif
233
234         if (samdb_is_pdc(sam_ctx)) {
235                 server_type |= DS_SERVER_PDC;
236         }
237
238         if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008) {
239                 server_type |= DS_SERVER_FULL_SECRET_DOMAIN_6;
240         }
241
242         if (samdb_is_gc(sam_ctx)) {
243                 server_type |= DS_SERVER_GC;
244         }
245
246         if (str_list_check(services, "ldap")) {
247                 server_type |= DS_SERVER_LDAP;
248         }
249
250         if (str_list_check(services, "kdc")) {
251                 server_type |= DS_SERVER_KDC;
252         }
253
254         if (samdb_rodc(sam_ctx, &am_rodc) == LDB_SUCCESS && !am_rodc) {
255                 server_type |= DS_SERVER_WRITABLE;
256         }
257
258 #if 0
259         /* w2k8-r2 as a sole DC does not claim this */
260         if (ldb_dn_compare(ldb_get_root_basedn(sam_ctx), ldb_get_default_basedn(sam_ctx)) == 0) {
261                 server_type |= DS_DNS_FOREST_ROOT;
262         }
263 #endif
264
265         pdc_name         = talloc_asprintf(mem_ctx, "\\\\%s",
266                                            lpcfg_netbios_name(lp_ctx));
267         NT_STATUS_HAVE_NO_MEMORY(pdc_name);
268         domain_uuid      = samdb_result_guid(dom_res->msgs[0], "objectGUID");
269         dns_domain       = lpcfg_dnsdomain(lp_ctx);
270         forest_domain    = samdb_forest_name(sam_ctx, mem_ctx);
271         NT_STATUS_HAVE_NO_MEMORY(forest_domain);
272         pdc_dns_name     = talloc_asprintf(mem_ctx, "%s.%s", 
273                                            strlower_talloc(mem_ctx, 
274                                                            lpcfg_netbios_name(lp_ctx)),
275                                            dns_domain);
276         NT_STATUS_HAVE_NO_MEMORY(pdc_dns_name);
277         flatname         = lpcfg_workgroup(lp_ctx);
278
279         server_site      = samdb_server_site_name(sam_ctx, mem_ctx);
280         NT_STATUS_HAVE_NO_MEMORY(server_site);
281         client_site      = samdb_client_site_name(sam_ctx, mem_ctx,
282                                                   src_address, NULL);
283         NT_STATUS_HAVE_NO_MEMORY(client_site);
284         if (strcasecmp(server_site, client_site) == 0) {
285                 server_type |= DS_SERVER_CLOSEST;
286         }
287
288         load_interface_list(mem_ctx, lp_ctx, &ifaces);
289         if (src_address) {
290                 pdc_ip = iface_list_best_ip(ifaces, src_address);
291         } else {
292                 pdc_ip = iface_list_first_v4(ifaces);
293         }
294         if (pdc_ip == NULL || !is_ipaddress_v4(pdc_ip)) {
295                 /* this matches windows behaviour */
296                 pdc_ip = "127.0.0.1";
297         }
298
299         ZERO_STRUCTP(netlogon);
300
301         /* check if either of these bits is present */
302         if (version & (NETLOGON_NT_VERSION_5EX|NETLOGON_NT_VERSION_5EX_WITH_IP)) {
303                 uint32_t extra_flags = 0;
304                 netlogon->ntver = NETLOGON_NT_VERSION_5EX;
305
306                 /* could check if the user exists */
307                 if (user_known) {
308                         netlogon->data.nt5_ex.command      = LOGON_SAM_LOGON_RESPONSE_EX;
309                 } else {
310                         netlogon->data.nt5_ex.command      = LOGON_SAM_LOGON_USER_UNKNOWN_EX;
311                 }
312                 netlogon->data.nt5_ex.pdc_name     = pdc_name;
313                 netlogon->data.nt5_ex.user_name    = user;
314                 netlogon->data.nt5_ex.domain_name  = flatname;
315                 netlogon->data.nt5_ex.domain_uuid  = domain_uuid;
316                 netlogon->data.nt5_ex.forest       = forest_domain;
317                 netlogon->data.nt5_ex.dns_domain   = dns_domain;
318                 netlogon->data.nt5_ex.pdc_dns_name = pdc_dns_name;
319                 netlogon->data.nt5_ex.server_site  = server_site;
320                 netlogon->data.nt5_ex.client_site  = client_site;
321                 if (version & NETLOGON_NT_VERSION_5EX_WITH_IP) {
322                         /* note that this is always a IPV4 address */
323                         extra_flags = NETLOGON_NT_VERSION_5EX_WITH_IP;
324                         netlogon->data.nt5_ex.sockaddr.sockaddr_family    = 2;
325                         netlogon->data.nt5_ex.sockaddr.pdc_ip       = pdc_ip;
326                         netlogon->data.nt5_ex.sockaddr.remaining = data_blob_talloc_zero(mem_ctx, 8);
327                 }
328                 netlogon->data.nt5_ex.server_type  = server_type;
329                 netlogon->data.nt5_ex.nt_version   = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5EX|extra_flags;
330                 netlogon->data.nt5_ex.lmnt_token   = 0xFFFF;
331                 netlogon->data.nt5_ex.lm20_token   = 0xFFFF;
332
333         } else if (version & NETLOGON_NT_VERSION_5) {
334                 netlogon->ntver = NETLOGON_NT_VERSION_5;
335
336                 /* could check if the user exists */
337                 if (user_known) {
338                         netlogon->data.nt5.command      = LOGON_SAM_LOGON_RESPONSE;
339                 } else {
340                         netlogon->data.nt5.command      = LOGON_SAM_LOGON_USER_UNKNOWN;
341                 }
342                 netlogon->data.nt5.pdc_name     = pdc_name;
343                 netlogon->data.nt5.user_name    = user;
344                 netlogon->data.nt5.domain_name  = flatname;
345                 netlogon->data.nt5.domain_uuid  = domain_uuid;
346                 netlogon->data.nt5.forest       = forest_domain;
347                 netlogon->data.nt5.dns_domain   = dns_domain;
348                 netlogon->data.nt5.pdc_dns_name = pdc_dns_name;
349                 netlogon->data.nt5.pdc_ip       = pdc_ip;
350                 netlogon->data.nt5.server_type  = server_type;
351                 netlogon->data.nt5.nt_version   = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5;
352                 netlogon->data.nt5.lmnt_token   = 0xFFFF;
353                 netlogon->data.nt5.lm20_token   = 0xFFFF;
354
355         } else /* (version & NETLOGON_NT_VERSION_1) and all other cases */ {
356                 netlogon->ntver = NETLOGON_NT_VERSION_1;
357                 /* could check if the user exists */
358                 if (user_known) {
359                         netlogon->data.nt4.command      = LOGON_SAM_LOGON_RESPONSE;
360                 } else {
361                         netlogon->data.nt4.command      = LOGON_SAM_LOGON_USER_UNKNOWN;
362                 }
363                 netlogon->data.nt4.pdc_name    = pdc_name;
364                 netlogon->data.nt4.user_name   = user;
365                 netlogon->data.nt4.domain_name = flatname;
366                 netlogon->data.nt4.nt_version  = NETLOGON_NT_VERSION_1;
367                 netlogon->data.nt4.lmnt_token  = 0xFFFF;
368                 netlogon->data.nt4.lm20_token  = 0xFFFF;
369         }
370
371         return NT_STATUS_OK;
372 }
373
374
375 /*
376   handle incoming cldap requests
377 */
378 void cldapd_netlogon_request(struct cldap_socket *cldap,
379                              struct cldapd_server *cldapd,
380                              TALLOC_CTX *tmp_ctx,
381                              uint32_t message_id,
382                              struct ldb_parse_tree *tree,
383                              struct tsocket_address *src)
384 {
385         unsigned int i;
386         const char *domain = NULL;
387         const char *host = NULL;
388         const char *user = NULL;
389         const char *domain_guid = NULL;
390         struct dom_sid *domain_sid = NULL;
391         int acct_control = -1;
392         int version = -1;
393         struct netlogon_samlogon_response netlogon;
394         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
395
396         if (tree->operation != LDB_OP_AND) goto failed;
397
398         /* extract the query elements */
399         for (i=0;i<tree->u.list.num_elements;i++) {
400                 struct ldb_parse_tree *t = tree->u.list.elements[i];
401                 if (t->operation != LDB_OP_EQUALITY) goto failed;
402                 if (strcasecmp(t->u.equality.attr, "DnsDomain") == 0) {
403                         domain = talloc_strndup(tmp_ctx, 
404                                                 (const char *)t->u.equality.value.data,
405                                                 t->u.equality.value.length);
406                 }
407                 if (strcasecmp(t->u.equality.attr, "Host") == 0) {
408                         host = talloc_strndup(tmp_ctx, 
409                                               (const char *)t->u.equality.value.data,
410                                               t->u.equality.value.length);
411                 }
412                 if (strcasecmp(t->u.equality.attr, "DomainGuid") == 0) {
413                         NTSTATUS enc_status;
414                         struct GUID guid;
415                         enc_status = ldap_decode_ndr_GUID(tmp_ctx, 
416                                                           t->u.equality.value, &guid);
417                         if (NT_STATUS_IS_OK(enc_status)) {
418                                 domain_guid = GUID_string(tmp_ctx, &guid);
419                         }
420                 }
421                 if (strcasecmp(t->u.equality.attr, "DomainSid") == 0) {
422                         enum ndr_err_code ndr_err;
423
424                         domain_sid = talloc(tmp_ctx, struct dom_sid);
425                         if (domain_sid == NULL) {
426                                 goto failed;
427                         }
428                         ndr_err = ndr_pull_struct_blob(&t->u.equality.value,
429                                                        domain_sid, domain_sid,
430                                                        (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
431                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
432                                 talloc_free(domain_sid);
433                                 goto failed;
434                         }
435                 }
436                 if (strcasecmp(t->u.equality.attr, "User") == 0) {
437                         user = talloc_strndup(tmp_ctx, 
438                                               (const char *)t->u.equality.value.data,
439                                               t->u.equality.value.length);
440                 }
441                 if (strcasecmp(t->u.equality.attr, "NtVer") == 0 &&
442                     t->u.equality.value.length == 4) {
443                         version = IVAL(t->u.equality.value.data, 0);
444                 }
445                 if (strcasecmp(t->u.equality.attr, "AAC") == 0 &&
446                     t->u.equality.value.length == 4) {
447                         acct_control = IVAL(t->u.equality.value.data, 0);
448                 }
449         }
450
451         if ((domain == NULL) && (domain_guid == NULL) && (domain_sid == NULL)) {
452                 domain = lpcfg_dnsdomain(cldapd->task->lp_ctx);
453         }
454
455         if (version == -1) {
456                 goto failed;
457         }
458
459         DEBUG(5,("cldap netlogon query domain=%s host=%s user=%s version=%d guid=%s\n",
460                  domain, host, user, version, domain_guid));
461
462         status = fill_netlogon_samlogon_response(cldapd->samctx, tmp_ctx,
463                                                  domain, NULL, domain_sid,
464                                                  domain_guid,
465                                                  user, acct_control,
466                                                  tsocket_address_inet_addr_string(src, tmp_ctx),
467                                                  version, cldapd->task->lp_ctx,
468                                                  &netlogon, false);
469         if (!NT_STATUS_IS_OK(status)) {
470                 goto failed;
471         }
472
473         status = cldap_netlogon_reply(cldap, message_id, src, version, &netlogon);
474         if (!NT_STATUS_IS_OK(status)) {
475                 goto failed;
476         }
477
478         return;
479         
480 failed:
481         DEBUG(2,("cldap netlogon query failed domain=%s host=%s version=%d - %s\n",
482                  domain, host, version, nt_errstr(status)));
483         cldap_empty_reply(cldap, message_id, src);
484 }