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