80599b8a2a079f523f6980cee57685fae8ab6400
[samba.git] / source4 / dsdb / samdb / ldb_modules / 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 "librpc/gen_ndr/ndr_misc.h"
29 #include "libcli/ldap/ldap_ndr.h"
30 #include "libcli/security/security.h"
31 #include "dsdb/samdb/samdb.h"
32 #include "dsdb/samdb/ldb_modules/util.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;
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 = false, am_rodc = false;
77         uint32_t uac = 0;
78         NTSTATUS status;
79
80         /* the domain parameter could have an optional trailing "." */
81         if (domain && domain[strlen(domain)-1] == '.') {
82                 domain = talloc_strndup(mem_ctx, domain, strlen(domain)-1);
83                 NT_STATUS_HAVE_NO_MEMORY(domain);
84         }
85
86         /* Lookup using long or short domainname */
87         if (domain && (strcasecmp_m(domain, lpcfg_dnsdomain(lp_ctx)) == 0)) {
88                 domain_dn = ldb_get_default_basedn(sam_ctx);
89         }
90         if (netbios_domain && (strcasecmp_m(netbios_domain, lpcfg_sam_name(lp_ctx)) == 0)) {
91                 domain_dn = ldb_get_default_basedn(sam_ctx);
92         }
93         if (domain_dn) {
94                 const char *domain_identifier = domain != NULL ? domain
95                                                         : netbios_domain;
96                 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
97                                  domain_dn, LDB_SCOPE_BASE, dom_attrs,
98                                  "objectClass=domain");
99                 if (ret != LDB_SUCCESS) {
100                         DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n",
101                                  domain_identifier,
102                                  ldb_dn_get_linearized(domain_dn),
103                                  ldb_errstring(sam_ctx)));
104                         return NT_STATUS_NO_SUCH_DOMAIN;
105                 }
106                 if (dom_res->count != 1) {
107                         DEBUG(2,("Error finding domain '%s'/'%s' in sam\n",
108                                  domain_identifier,
109                                  ldb_dn_get_linearized(domain_dn)));
110                         return NT_STATUS_NO_SUCH_DOMAIN;
111                 }
112         }
113
114         /* Lookup using GUID or SID */
115         if ((dom_res == NULL) && (domain_guid || domain_sid)) {
116                 if (domain_guid) {
117                         struct GUID binary_guid;
118                         struct ldb_val guid_val;
119
120                         /* By this means, we ensure we don't have funny stuff in the GUID */
121
122                         status = GUID_from_string(domain_guid, &binary_guid);
123                         if (!NT_STATUS_IS_OK(status)) {
124                                 return status;
125                         }
126
127                         /* And this gets the result into the binary format we want anyway */
128                         status = GUID_to_ndr_blob(&binary_guid, mem_ctx, &guid_val);
129                         if (!NT_STATUS_IS_OK(status)) {
130                                 return status;
131                         }
132                         ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
133                                                  NULL, LDB_SCOPE_SUBTREE, 
134                                                  dom_attrs, 
135                                                  "(&(objectCategory=DomainDNS)(objectGUID=%s))", 
136                                                  ldb_binary_encode(mem_ctx, guid_val));
137                 } else { /* domain_sid case */
138                         ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
139                                          NULL, LDB_SCOPE_SUBTREE,
140                                          dom_attrs,
141                                          "(&(objectCategory=DomainDNS)(objectSid=%s))",
142                                          dom_sid_string(mem_ctx, domain_sid));
143                 }
144                 
145                 if (ret != LDB_SUCCESS) {
146                         DEBUG(2,("Unable to find a correct reference to GUID '%s' or SID '%s' in sam: %s\n",
147                                  domain_guid, dom_sid_string(mem_ctx, domain_sid),
148                                  ldb_errstring(sam_ctx)));
149                         return NT_STATUS_NO_SUCH_DOMAIN;
150                 } else if (dom_res->count == 1) {
151                         /* Ok, now just check it is our domain */
152                         if (ldb_dn_compare(ldb_get_default_basedn(sam_ctx),
153                                            dom_res->msgs[0]->dn) != 0) {
154                                 DEBUG(2,("The GUID '%s' or SID '%s' doesn't identify our domain\n",
155                                          domain_guid,
156                                          dom_sid_string(mem_ctx, domain_sid)));
157                                 return NT_STATUS_NO_SUCH_DOMAIN;
158                         }
159                 } else {
160                         DEBUG(2,("Unable to find a correct reference to GUID '%s' or SID '%s' in sam\n",
161                                  domain_guid, dom_sid_string(mem_ctx, domain_sid)));
162                         return NT_STATUS_NO_SUCH_DOMAIN;
163                 }
164         }
165
166         if (dom_res == NULL && fill_on_blank_request) {
167                 /* blank inputs gives our domain - tested against
168                    w2k8r2. Without this ADUC on Win7 won't start */
169                 domain_dn = ldb_get_default_basedn(sam_ctx);
170                 ret = ldb_search(sam_ctx, mem_ctx, &dom_res,
171                                  domain_dn, LDB_SCOPE_BASE, dom_attrs,
172                                  "objectClass=domain");
173                 if (ret != LDB_SUCCESS) {
174                         DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n",
175                                  lpcfg_dnsdomain(lp_ctx),
176                                  ldb_dn_get_linearized(domain_dn),
177                                  ldb_errstring(sam_ctx)));
178                         return NT_STATUS_NO_SUCH_DOMAIN;
179                 }
180         }
181
182         if (dom_res == NULL) {
183                 DEBUG(2,(__location__ ": Unable to get domain information with no inputs\n"));
184                 return NT_STATUS_NO_SUCH_DOMAIN;
185         }
186
187         /* work around different inputs for not-specified users */
188         if (!user) {
189                 user = "";
190         }
191
192         /* Enquire about any valid username with just a CLDAP packet -
193          * if kerberos didn't also do this, the security folks would
194          * scream... */
195         if (user[0]) {
196                 /* Only allow some bits to be enquired:  [MS-ATDS] 7.3.3.2 */
197                 if (acct_control == (uint32_t)-1) {
198                         acct_control = 0;
199                 }
200                 /*
201                  * ACB_AUTOLOCK/UF_LOCKOUT seems to be a special
202                  * hack for SEC_CHAN_DNS_DOMAIN.
203                  *
204                  * It's used together with user = "example.com."
205                  */
206                 if (acct_control != ACB_AUTOLOCK) {
207                         acct_control &= (ACB_TEMPDUP | ACB_NORMAL | ACB_DOMTRUST | ACB_WSTRUST | ACB_SVRTRUST);
208                 }
209                 uac = ds_acb2uf(acct_control);
210         }
211
212         if (uac == UF_LOCKOUT) {
213                 struct ldb_message *tdo_msg = NULL;
214
215                 /*
216                  * ACB_AUTOLOCK/UF_LOCKOUT seems to be a special
217                  * hack for SEC_CHAN_DNS_DOMAIN.
218                  *
219                  * It's used together with user = "example.com."
220                  */
221                 status = dsdb_trust_search_tdo_by_type(sam_ctx,
222                                                        SEC_CHAN_DNS_DOMAIN,
223                                                        user, none_attrs,
224                                                        mem_ctx, &tdo_msg);
225                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
226                         user_known = false;
227                 } else if (NT_STATUS_IS_OK(status)) {
228                         TALLOC_FREE(tdo_msg);
229                         user_known = true;
230                 } else {
231                         DEBUG(2,("Unable to find reference to TDO '%s' - %s\n",
232                                  user, nt_errstr(status)));
233                         return status;
234                 }
235         } else if (user[0]) {
236                 struct ldb_result *user_res = NULL;
237
238                 /* We must exclude disabled accounts, but otherwise do the bitwise match the client asked for */
239                 ret = ldb_search(sam_ctx, mem_ctx, &user_res,
240                                          dom_res->msgs[0]->dn, LDB_SCOPE_SUBTREE, 
241                                          none_attrs, 
242                                          "(&(objectClass=user)(samAccountName=%s)"
243                                          "(!(userAccountControl:" LDB_OID_COMPARATOR_AND ":=%u))"
244                                          "(userAccountControl:" LDB_OID_COMPARATOR_OR ":=%u))", 
245                                          ldb_binary_encode_string(mem_ctx, user),
246                                          UF_ACCOUNTDISABLE, uac);
247                 if (ret != LDB_SUCCESS) {
248                         DEBUG(2,("Unable to find reference to user '%s' with ACB 0x%8x under %s: %s\n",
249                                  user, acct_control, ldb_dn_get_linearized(dom_res->msgs[0]->dn),
250                                  ldb_errstring(sam_ctx)));
251                         return NT_STATUS_NO_SUCH_USER;
252                 } else if (user_res->count == 1) {
253                         user_known = true;
254                 } else {
255                         user_known = false;
256                 }
257                 TALLOC_FREE(user_res);
258         } else {
259                 user_known = true;
260         }
261
262         server_type = DS_SERVER_DS;
263
264         if (samdb_is_pdc(sam_ctx)) {
265                 server_type |= DS_SERVER_PDC;
266         }
267
268         if (samdb_is_gc(sam_ctx)) {
269                 server_type |= DS_SERVER_GC;
270         }
271
272         if (str_list_check(services, "ldap")) {
273                 server_type |= DS_SERVER_LDAP;
274         }
275
276         if (str_list_check(services, "kdc")) {
277                 server_type |= DS_SERVER_KDC;
278         }
279
280         if (str_list_check(services, "ntp_signd")) {
281                 server_type |= DS_SERVER_TIMESERV | DS_SERVER_GOOD_TIMESERV;
282         }
283
284         if (samdb_rodc(sam_ctx, &am_rodc) == LDB_SUCCESS && !am_rodc) {
285                 server_type |= DS_SERVER_WRITABLE;
286         }
287
288         if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008) {
289                 if (server_type & DS_SERVER_WRITABLE) {
290                         server_type |= DS_SERVER_FULL_SECRET_DOMAIN_6;
291                 } else {
292                         server_type |= DS_SERVER_SELECT_SECRET_DOMAIN_6;
293                 }
294         }
295
296         if (version & (NETLOGON_NT_VERSION_5EX|NETLOGON_NT_VERSION_5EX_WITH_IP)) {
297                 pdc_name = lpcfg_netbios_name(lp_ctx);
298         } else {
299                 pdc_name = talloc_asprintf(mem_ctx, "\\\\%s",
300                                            lpcfg_netbios_name(lp_ctx));
301                 NT_STATUS_HAVE_NO_MEMORY(pdc_name);
302         }
303         domain_uuid      = samdb_result_guid(dom_res->msgs[0], "objectGUID");
304         dns_domain       = lpcfg_dnsdomain(lp_ctx);
305         forest_domain    = samdb_forest_name(sam_ctx, mem_ctx);
306         NT_STATUS_HAVE_NO_MEMORY(forest_domain);
307         pdc_dns_name     = talloc_asprintf(mem_ctx, "%s.%s", 
308                                            strlower_talloc(mem_ctx, 
309                                                            lpcfg_netbios_name(lp_ctx)),
310                                            dns_domain);
311         NT_STATUS_HAVE_NO_MEMORY(pdc_dns_name);
312         flatname         = lpcfg_workgroup(lp_ctx);
313
314         server_site      = samdb_server_site_name(sam_ctx, mem_ctx);
315         NT_STATUS_HAVE_NO_MEMORY(server_site);
316         client_site      = samdb_client_site_name(sam_ctx, mem_ctx,
317                                                   src_address, NULL);
318         NT_STATUS_HAVE_NO_MEMORY(client_site);
319         if (strcasecmp(server_site, client_site) == 0) {
320                 server_type |= DS_SERVER_CLOSEST;
321         }
322
323         load_interface_list(mem_ctx, lp_ctx, &ifaces);
324         if (src_address) {
325                 pdc_ip = iface_list_best_ip(ifaces, src_address);
326         } else {
327                 pdc_ip = iface_list_first_v4(ifaces);
328         }
329         if (pdc_ip == NULL || !is_ipaddress_v4(pdc_ip)) {
330                 /* this matches windows behaviour */
331                 pdc_ip = "127.0.0.1";
332         }
333
334         ZERO_STRUCTP(netlogon);
335
336         /* check if either of these bits is present */
337         if (version & (NETLOGON_NT_VERSION_5EX|NETLOGON_NT_VERSION_5EX_WITH_IP)) {
338                 uint32_t extra_flags = 0;
339                 netlogon->ntver = NETLOGON_NT_VERSION_5EX;
340
341                 /* could check if the user exists */
342                 if (user_known) {
343                         netlogon->data.nt5_ex.command      = LOGON_SAM_LOGON_RESPONSE_EX;
344                 } else {
345                         netlogon->data.nt5_ex.command      = LOGON_SAM_LOGON_USER_UNKNOWN_EX;
346                 }
347                 netlogon->data.nt5_ex.pdc_name     = pdc_name;
348                 netlogon->data.nt5_ex.user_name    = user;
349                 netlogon->data.nt5_ex.domain_name  = flatname;
350                 netlogon->data.nt5_ex.domain_uuid  = domain_uuid;
351                 netlogon->data.nt5_ex.forest       = forest_domain;
352                 netlogon->data.nt5_ex.dns_domain   = dns_domain;
353                 netlogon->data.nt5_ex.pdc_dns_name = pdc_dns_name;
354                 netlogon->data.nt5_ex.server_site  = server_site;
355                 netlogon->data.nt5_ex.client_site  = client_site;
356                 if (version & NETLOGON_NT_VERSION_5EX_WITH_IP) {
357                         /* note that this is always a IPV4 address */
358                         extra_flags = NETLOGON_NT_VERSION_5EX_WITH_IP;
359                         netlogon->data.nt5_ex.sockaddr.sockaddr_family    = 2;
360                         netlogon->data.nt5_ex.sockaddr.pdc_ip       = pdc_ip;
361                         netlogon->data.nt5_ex.sockaddr.remaining = data_blob_talloc_zero(mem_ctx, 8);
362                 }
363                 netlogon->data.nt5_ex.server_type  = server_type;
364                 netlogon->data.nt5_ex.nt_version   = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5EX|extra_flags;
365                 netlogon->data.nt5_ex.lmnt_token   = 0xFFFF;
366                 netlogon->data.nt5_ex.lm20_token   = 0xFFFF;
367
368         } else if (version & NETLOGON_NT_VERSION_5) {
369                 netlogon->ntver = NETLOGON_NT_VERSION_5;
370
371                 /* could check if the user exists */
372                 if (user_known) {
373                         netlogon->data.nt5.command      = LOGON_SAM_LOGON_RESPONSE;
374                 } else {
375                         netlogon->data.nt5.command      = LOGON_SAM_LOGON_USER_UNKNOWN;
376                 }
377                 netlogon->data.nt5.pdc_name     = pdc_name;
378                 netlogon->data.nt5.user_name    = user;
379                 netlogon->data.nt5.domain_name  = flatname;
380                 netlogon->data.nt5.domain_uuid  = domain_uuid;
381                 netlogon->data.nt5.forest       = forest_domain;
382                 netlogon->data.nt5.dns_domain   = dns_domain;
383                 netlogon->data.nt5.pdc_dns_name = pdc_dns_name;
384                 netlogon->data.nt5.pdc_ip       = pdc_ip;
385                 netlogon->data.nt5.server_type  = server_type;
386                 netlogon->data.nt5.nt_version   = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5;
387                 netlogon->data.nt5.lmnt_token   = 0xFFFF;
388                 netlogon->data.nt5.lm20_token   = 0xFFFF;
389
390         } else /* (version & NETLOGON_NT_VERSION_1) and all other cases */ {
391                 netlogon->ntver = NETLOGON_NT_VERSION_1;
392                 /* could check if the user exists */
393                 if (user_known) {
394                         netlogon->data.nt4.command      = LOGON_SAM_LOGON_RESPONSE;
395                 } else {
396                         netlogon->data.nt4.command      = LOGON_SAM_LOGON_USER_UNKNOWN;
397                 }
398                 netlogon->data.nt4.pdc_name    = pdc_name;
399                 netlogon->data.nt4.user_name   = user;
400                 netlogon->data.nt4.domain_name = flatname;
401                 netlogon->data.nt4.nt_version  = NETLOGON_NT_VERSION_1;
402                 netlogon->data.nt4.lmnt_token  = 0xFFFF;
403                 netlogon->data.nt4.lm20_token  = 0xFFFF;
404         }
405
406         return NT_STATUS_OK;
407 }
408
409 NTSTATUS parse_netlogon_request(struct ldb_parse_tree *tree,
410                                 struct loadparm_context *lp_ctx,
411                                 TALLOC_CTX *tmp_ctx,
412                                 const char **domain,
413                                 const char **host,
414                                 const char **user,
415                                 const char **domain_guid,
416                                 struct dom_sid **domain_sid,
417                                 int *acct_control,
418                                 int *version)
419 {
420         unsigned int i;
421
422         *domain = NULL;
423         *host = NULL;
424         *user = NULL;
425         *domain_guid = NULL;
426         *domain_sid = NULL;
427         *acct_control = -1;
428         *version = NETLOGON_NT_VERSION_5;
429
430         if (tree->operation != LDB_OP_AND) goto failed;
431
432         /* extract the query elements */
433         for (i=0;i<tree->u.list.num_elements;i++) {
434                 struct ldb_parse_tree *t = tree->u.list.elements[i];
435                 if (t->operation != LDB_OP_EQUALITY) goto failed;
436                 if (strcasecmp(t->u.equality.attr, "DnsDomain") == 0) {
437                         *domain = 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, "Host") == 0) {
442                         *host = talloc_strndup(tmp_ctx,
443                                               (const char *)t->u.equality.value.data,
444                                               t->u.equality.value.length);
445                 }
446                 if (strcasecmp(t->u.equality.attr, "DomainGuid") == 0) {
447                         NTSTATUS enc_status;
448                         struct GUID guid;
449                         enc_status = ldap_decode_ndr_GUID(tmp_ctx, 
450                                                           t->u.equality.value, &guid);
451                         if (NT_STATUS_IS_OK(enc_status)) {
452                                 *domain_guid = GUID_string(tmp_ctx, &guid);
453                         }
454                 }
455                 if (strcasecmp(t->u.equality.attr, "DomainSid") == 0) {
456                         enum ndr_err_code ndr_err;
457
458                         *domain_sid = talloc(tmp_ctx, struct dom_sid);
459                         if (*domain_sid == NULL) {
460                                 goto failed;
461                         }
462                         ndr_err = ndr_pull_struct_blob(&t->u.equality.value,
463                                                        *domain_sid, *domain_sid,
464                                                        (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
465                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
466                                 talloc_free(*domain_sid);
467                                 goto failed;
468                         }
469                 }
470                 if (strcasecmp(t->u.equality.attr, "User") == 0) {
471                         *user = talloc_strndup(tmp_ctx,
472                                                (const char *)t->u.equality.value.data,
473                                                t->u.equality.value.length);
474                 }
475                 if (strcasecmp(t->u.equality.attr, "NtVer") == 0 &&
476                     t->u.equality.value.length == 4) {
477                         *version = IVAL(t->u.equality.value.data, 0);
478                 }
479                 if (strcasecmp(t->u.equality.attr, "AAC") == 0 &&
480                     t->u.equality.value.length == 4) {
481                         *acct_control = IVAL(t->u.equality.value.data, 0);
482                 }
483         }
484
485         if ((*domain == NULL) && (*domain_guid == NULL) && (*domain_sid == NULL)) {
486                 *domain = lpcfg_dnsdomain(lp_ctx);
487         }
488
489         return NT_STATUS_OK;
490
491 failed:
492         return NT_STATUS_UNSUCCESSFUL;
493 }