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