Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into 4-0-abartlet
[kai/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 "lib/socket/socket.h"
28 #include "smbd/service_task.h"
29 #include "cldap_server/cldap_server.h"
30 #include "librpc/gen_ndr/ndr_misc.h"
31 #include "libcli/ldap/ldap_ndr.h"
32 #include "libcli/security/security.h"
33 #include "dsdb/samdb/samdb.h"
34 #include "auth/auth.h"
35 #include "ldb_wrap.h"
36 #include "system/network.h"
37 #include "lib/socket/netif.h"
38 #include "param/param.h"
39 /*
40   fill in the cldap netlogon union for a given version
41 */
42 NTSTATUS fill_netlogon_samlogon_response(struct ldb_context *sam_ctx,
43                                          TALLOC_CTX *mem_ctx,
44                                          const char *domain,
45                                          const char *netbios_domain,
46                                          struct dom_sid *domain_sid,
47                                          const char *domain_guid,
48                                          const char *user,
49                                          uint32_t acct_control,
50                                          const char *src_address,
51                                          uint32_t version,
52                                          struct loadparm_context *lp_ctx,
53                                          struct netlogon_samlogon_response *netlogon)
54 {
55         const char *ref_attrs[] = {"nETBIOSName", "dnsRoot", "ncName", NULL};
56         const char *dom_attrs[] = {"objectGUID", NULL};
57         const char *none_attrs[] = {NULL};
58         struct ldb_result *ref_res = NULL, *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 *realm;
65         const char *dns_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 *partitions_basedn;
72         struct interface *ifaces;
73         bool user_known;
74
75         partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
76
77         /* the domain has an optional trailing . */
78         if (domain && domain[strlen(domain)-1] == '.') {
79                 domain = talloc_strndup(mem_ctx, domain, strlen(domain)-1);
80         }
81
82         if (domain) {
83                 struct ldb_dn *dom_dn;
84                 /* try and find the domain */
85
86                 ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &ref_res, 
87                                          partitions_basedn, LDB_SCOPE_ONELEVEL, 
88                                          ref_attrs, 
89                                          "(&(&(objectClass=crossRef)(dnsRoot=%s))(nETBIOSName=*))",
90                                          domain);
91         
92                 if (ret != LDB_SUCCESS) {
93                         DEBUG(2,("Unable to find referece to '%s' in sam: %s\n",
94                                  domain, 
95                                  ldb_errstring(sam_ctx)));
96                         return NT_STATUS_NO_SUCH_DOMAIN;
97                 } else if (ref_res->count == 1) {
98                         talloc_steal(mem_ctx, dom_res);
99                         dom_dn = ldb_msg_find_attr_as_dn(sam_ctx, mem_ctx, ref_res->msgs[0], "ncName");
100                         if (!dom_dn) {
101                                 return NT_STATUS_NO_SUCH_DOMAIN;
102                         }
103                         ret = ldb_search(sam_ctx, dom_dn,
104                                          LDB_SCOPE_BASE, "objectClass=domain", 
105                                          dom_attrs, &dom_res);
106                         if (ret != LDB_SUCCESS) {
107                                 DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n", domain, ldb_dn_get_linearized(dom_dn), ldb_errstring(sam_ctx)));
108                                 return NT_STATUS_NO_SUCH_DOMAIN;
109                         }
110                         talloc_steal(mem_ctx, dom_res);
111                         if (dom_res->count != 1) {
112                                 DEBUG(2,("Error finding domain '%s'/'%s' in sam\n", domain, ldb_dn_get_linearized(dom_dn)));
113                                 return NT_STATUS_NO_SUCH_DOMAIN;
114                         }
115                 } else if (ref_res->count > 1) {
116                         talloc_free(ref_res);
117                         return NT_STATUS_NO_SUCH_DOMAIN;
118                 }
119         }
120
121         if (netbios_domain) {
122                 struct ldb_dn *dom_dn;
123                 /* try and find the domain */
124
125                 ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &ref_res, 
126                                          partitions_basedn, LDB_SCOPE_ONELEVEL, 
127                                          ref_attrs, 
128                                          "(&(objectClass=crossRef)(ncName=*)(nETBIOSName=%s))",
129                                          netbios_domain);
130         
131                 if (ret != LDB_SUCCESS) {
132                         DEBUG(2,("Unable to find referece to '%s' in sam: %s\n",
133                                  netbios_domain, 
134                                  ldb_errstring(sam_ctx)));
135                         return NT_STATUS_NO_SUCH_DOMAIN;
136                 } else if (ref_res->count == 1) {
137                         talloc_steal(mem_ctx, dom_res);
138                         dom_dn = ldb_msg_find_attr_as_dn(sam_ctx, mem_ctx, ref_res->msgs[0], "ncName");
139                         if (!dom_dn) {
140                                 return NT_STATUS_NO_SUCH_DOMAIN;
141                         }
142                         ret = ldb_search(sam_ctx, dom_dn,
143                                          LDB_SCOPE_BASE, "objectClass=domain", 
144                                          dom_attrs, &dom_res);
145                         if (ret != LDB_SUCCESS) {
146                                 DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n", domain, ldb_dn_get_linearized(dom_dn), ldb_errstring(sam_ctx)));
147                                 return NT_STATUS_NO_SUCH_DOMAIN;
148                         }
149                         talloc_steal(mem_ctx, dom_res);
150                         if (dom_res->count != 1) {
151                                 DEBUG(2,("Error finding domain '%s'/'%s' in sam\n", domain, ldb_dn_get_linearized(dom_dn)));
152                                 return NT_STATUS_NO_SUCH_DOMAIN;
153                         }
154                 } else if (ref_res->count > 1) {
155                         talloc_free(ref_res);
156                         return NT_STATUS_NO_SUCH_DOMAIN;
157                 }
158         }
159
160         if ((dom_res == NULL || dom_res->count == 0) && (domain_guid || domain_sid)) {
161                 ref_res = NULL;
162
163                 if (domain_guid) {
164                         ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &dom_res,
165                                                  NULL, LDB_SCOPE_SUBTREE, 
166                                                  dom_attrs, 
167                                                  "(&(objectClass=domainDNS)(objectGUID=%s))", 
168                                                  domain_guid);
169                 } else { /* domain_sid case */
170                         ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &dom_res,
171                                                  NULL, LDB_SCOPE_SUBTREE, 
172                                                  dom_attrs, 
173                                                  "(&(objectClass=domainDNS)(objectSID=%s))", 
174                                                  dom_sid_string(mem_ctx, domain_sid));
175                 }
176                 
177                 if (ret != LDB_SUCCESS) {
178                         DEBUG(2,("Unable to find referece to GUID '%s' or SID %s in sam: %s\n",
179                                  domain_guid, dom_sid_string(mem_ctx, domain_sid),
180                                  ldb_errstring(sam_ctx)));
181                         return NT_STATUS_NO_SUCH_DOMAIN;
182                 } else if (dom_res->count == 1) {
183                         /* try and find the domain */
184                         ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &ref_res,
185                                                  partitions_basedn, LDB_SCOPE_ONELEVEL, 
186                                                  ref_attrs, 
187                                                  "(&(objectClass=crossRef)(ncName=%s))", 
188                                                  ldb_dn_get_linearized(dom_res->msgs[0]->dn));
189                         
190                         if (ret != LDB_SUCCESS) {
191                                 DEBUG(2,("Unable to find referece to '%s' in sam: %s\n",
192                                          ldb_dn_get_linearized(dom_res->msgs[0]->dn), 
193                                          ldb_errstring(sam_ctx)));
194                                 return NT_STATUS_NO_SUCH_DOMAIN;
195                                 
196                         } else if (ref_res->count != 1) {
197                                 DEBUG(2,("Unable to find referece to '%s' in sam\n",
198                                          ldb_dn_get_linearized(dom_res->msgs[0]->dn)));
199                                 return NT_STATUS_NO_SUCH_DOMAIN;
200                         }
201                 } else if (dom_res->count > 1) {
202                         talloc_free(ref_res);
203                         return NT_STATUS_NO_SUCH_DOMAIN;
204                 }
205         }
206
207
208         if ((ref_res == NULL || ref_res->count == 0)) {
209                 DEBUG(2,("Unable to find domain reference with name %s or GUID {%s}\n", domain, domain_guid));
210                 return NT_STATUS_NO_SUCH_DOMAIN;
211         }
212
213         if ((dom_res == NULL || dom_res->count == 0)) {
214                 DEBUG(2,("Unable to find domain with name %s or GUID {%s}\n", domain, domain_guid));
215                 return NT_STATUS_NO_SUCH_DOMAIN;
216         }
217
218         /* work around different inputs for not-specified users */
219         if (!user) {
220                 user = "";
221         }
222
223         /* Enquire about any valid username with just a CLDAP packet -
224          * if kerberos didn't also do this, the security folks would
225          * scream... */
226         if (user[0]) {                                                  \
227                 /* Only allow some bits to be enquired:  [MS-ATDS] 7.3.3.2 */
228                 if (acct_control == (uint32_t)-1) {
229                         acct_control = 0;
230                 }
231                 acct_control = acct_control & (ACB_TEMPDUP | ACB_NORMAL | ACB_DOMTRUST | ACB_WSTRUST | ACB_SVRTRUST);
232
233                 /* We must exclude disabled accounts, but otherwise do the bitwise match the client asked for */
234                 ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &user_res,
235                                          dom_res->msgs[0]->dn, LDB_SCOPE_SUBTREE, 
236                                          none_attrs, 
237                                          "(&(objectClass=user)(samAccountName=%s)"
238                                          "(!(userAccountControl:" LDB_OID_COMPARATOR_AND ":=%u))"
239                                          "(userAccountControl:" LDB_OID_COMPARATOR_OR ":=%u))", 
240                                          user, UF_ACCOUNTDISABLE, samdb_acb2uf(acct_control));
241                 if (ret != LDB_SUCCESS) {
242                         DEBUG(2,("Unable to find referece to user '%s' with ACB 0x%8x under %s: %s\n",
243                                  user, acct_control, ldb_dn_get_linearized(dom_res->msgs[0]->dn),
244                                  ldb_errstring(sam_ctx)));
245                         return NT_STATUS_NO_SUCH_USER;
246                 } else if (user_res->count == 1) {
247                         user_known = true;
248                 } else {
249                         user_known = false;
250                 }
251
252         } else {
253                 user_known = true;
254         }
255                 
256         server_type      = 
257                 NBT_SERVER_DS | NBT_SERVER_TIMESERV |
258                 NBT_SERVER_CLOSEST | NBT_SERVER_WRITABLE | 
259                 NBT_SERVER_GOOD_TIMESERV | NBT_SERVER_DS_DNS_CONTR |
260                 NBT_SERVER_DS_DNS_DOMAIN;
261
262         if (samdb_is_pdc(sam_ctx)) {
263                 server_type |= NBT_SERVER_PDC;
264         }
265
266         if (samdb_is_gc(sam_ctx)) {
267                 server_type |= NBT_SERVER_GC;
268         }
269
270         if (str_list_check(services, "ldap")) {
271                 server_type |= NBT_SERVER_LDAP;
272         }
273
274         if (str_list_check(services, "kdc")) {
275                 server_type |= NBT_SERVER_KDC;
276         }
277
278         if (!ldb_dn_compare_base(ldb_get_root_basedn(sam_ctx), ldb_get_default_basedn(sam_ctx))) {
279                 server_type |= NBT_SERVER_DS_DNS_FOREST;
280         }
281
282         pdc_name         = talloc_asprintf(mem_ctx, "\\\\%s", lp_netbios_name(lp_ctx));
283         domain_uuid      = samdb_result_guid(dom_res->msgs[0], "objectGUID");
284         realm            = samdb_result_string(ref_res->msgs[0], "dnsRoot", lp_realm(lp_ctx));
285         dns_domain       = samdb_result_string(ref_res->msgs[0], "dnsRoot", lp_realm(lp_ctx));
286         pdc_dns_name     = talloc_asprintf(mem_ctx, "%s.%s", 
287                                            strlower_talloc(mem_ctx, 
288                                                            lp_netbios_name(lp_ctx)), 
289                                            dns_domain);
290
291         flatname         = samdb_result_string(ref_res->msgs[0], "nETBIOSName", 
292                                                lp_workgroup(lp_ctx));
293         /* FIXME: Hardcoded site names */
294         server_site      = "Default-First-Site-Name";
295         client_site      = "Default-First-Site-Name";
296         load_interfaces(mem_ctx, lp_interfaces(lp_ctx), &ifaces);
297         pdc_ip           = iface_best_ip(ifaces, src_address);
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->nt5_ex.command      = LOGON_SAM_LOGON_RESPONSE_EX;
309                 } else {
310                         netlogon->nt5_ex.command      = LOGON_SAM_LOGON_USER_UNKNOWN_EX;
311                 }
312                 netlogon->nt5_ex.server_type  = server_type;
313                 netlogon->nt5_ex.domain_uuid  = domain_uuid;
314                 netlogon->nt5_ex.forest       = realm;
315                 netlogon->nt5_ex.dns_domain   = dns_domain;
316                 netlogon->nt5_ex.pdc_dns_name = pdc_dns_name;
317                 netlogon->nt5_ex.domain       = flatname;
318                 netlogon->nt5_ex.pdc_name     = lp_netbios_name(lp_ctx);
319                 netlogon->nt5_ex.user_name    = user;
320                 netlogon->nt5_ex.server_site  = server_site;
321                 netlogon->nt5_ex.client_site  = client_site;
322
323                 if (version & NETLOGON_NT_VERSION_5EX_WITH_IP) {
324                         /* Clearly this needs to be fixed up for IPv6 */
325                         extra_flags = NETLOGON_NT_VERSION_5EX_WITH_IP;
326                         netlogon->nt5_ex.sockaddr.sa_family    = 2;
327                         netlogon->nt5_ex.sockaddr.pdc_ip       = pdc_ip;
328                         netlogon->nt5_ex.sockaddr.remaining = data_blob_talloc_zero(mem_ctx, 8);
329                 }
330                 netlogon->nt5_ex.nt_version   = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5EX|extra_flags;
331                 netlogon->nt5_ex.lmnt_token   = 0xFFFF;
332                 netlogon->nt5_ex.lm20_token   = 0xFFFF;
333
334         } else if (version & NETLOGON_NT_VERSION_5) {
335                 netlogon->ntver = NETLOGON_NT_VERSION_5;
336
337                 /* could check if the user exists */
338                 if (user_known) {
339                         netlogon->nt5.command      = LOGON_SAM_LOGON_RESPONSE;
340                 } else {
341                         netlogon->nt5.command      = LOGON_SAM_LOGON_USER_UNKNOWN;
342                 }
343                 netlogon->nt5.pdc_name     = pdc_name;
344                 netlogon->nt5.user_name    = user;
345                 netlogon->nt5.domain_name  = flatname;
346                 netlogon->nt5.domain_uuid  = domain_uuid;
347                 netlogon->nt5.forest       = realm;
348                 netlogon->nt5.dns_domain   = dns_domain;
349                 netlogon->nt5.pdc_dns_name = pdc_dns_name;
350                 netlogon->nt5.pdc_ip       = pdc_ip;
351                 netlogon->nt5.server_type  = server_type;
352                 netlogon->nt5.nt_version   = NETLOGON_NT_VERSION_1|NETLOGON_NT_VERSION_5;
353                 netlogon->nt5.lmnt_token   = 0xFFFF;
354                 netlogon->nt5.lm20_token   = 0xFFFF;
355
356         } else /* (version & NETLOGON_NT_VERSION_1) and all other cases */ {
357                 netlogon->ntver = NETLOGON_NT_VERSION_1;
358                 /* could check if the user exists */
359                 if (user_known) {
360                         netlogon->nt4.command      = LOGON_SAM_LOGON_RESPONSE;
361                 } else {
362                         netlogon->nt4.command      = LOGON_SAM_LOGON_USER_UNKNOWN;
363                 }
364                 netlogon->nt4.server      = pdc_name;
365                 netlogon->nt4.user_name   = user;
366                 netlogon->nt4.domain      = flatname;
367                 netlogon->nt4.nt_version  = NETLOGON_NT_VERSION_1;
368                 netlogon->nt4.lmnt_token  = 0xFFFF;
369                 netlogon->nt4.lm20_token  = 0xFFFF;
370         }
371
372         return NT_STATUS_OK;
373 }
374
375
376 /*
377   handle incoming cldap requests
378 */
379 void cldapd_netlogon_request(struct cldap_socket *cldap, 
380                              uint32_t message_id,
381                              struct ldb_parse_tree *tree,
382                              struct socket_address *src)
383 {
384         struct cldapd_server *cldapd = talloc_get_type(cldap->incoming.private, struct cldapd_server);
385         int i;
386         const char *domain = NULL;
387         const char *host = NULL;
388         const char *user = NULL;
389         const char *domain_guid = NULL;
390         const char *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         TALLOC_CTX *tmp_ctx = talloc_new(cldap);
397
398         if (tree->operation != LDB_OP_AND) goto failed;
399
400         /* extract the query elements */
401         for (i=0;i<tree->u.list.num_elements;i++) {
402                 struct ldb_parse_tree *t = tree->u.list.elements[i];
403                 if (t->operation != LDB_OP_EQUALITY) goto failed;
404                 if (strcasecmp(t->u.equality.attr, "DnsDomain") == 0) {
405                         domain = talloc_strndup(tmp_ctx, 
406                                                 (const char *)t->u.equality.value.data,
407                                                 t->u.equality.value.length);
408                 }
409                 if (strcasecmp(t->u.equality.attr, "Host") == 0) {
410                         host = talloc_strndup(tmp_ctx, 
411                                               (const char *)t->u.equality.value.data,
412                                               t->u.equality.value.length);
413                 }
414                 if (strcasecmp(t->u.equality.attr, "DomainGuid") == 0) {
415                         NTSTATUS enc_status;
416                         struct GUID guid;
417                         enc_status = ldap_decode_ndr_GUID(tmp_ctx, 
418                                                           t->u.equality.value, &guid);
419                         if (NT_STATUS_IS_OK(enc_status)) {
420                                 domain_guid = GUID_string(tmp_ctx, &guid);
421                         }
422                 }
423                 if (strcasecmp(t->u.equality.attr, "DomainSid") == 0) {
424                         domain_sid = talloc_strndup(tmp_ctx, 
425                                                     (const char *)t->u.equality.value.data,
426                                                     t->u.equality.value.length);
427                 }
428                 if (strcasecmp(t->u.equality.attr, "User") == 0) {
429                         user = talloc_strndup(tmp_ctx, 
430                                               (const char *)t->u.equality.value.data,
431                                               t->u.equality.value.length);
432                 }
433                 if (strcasecmp(t->u.equality.attr, "NtVer") == 0 &&
434                     t->u.equality.value.length == 4) {
435                         version = IVAL(t->u.equality.value.data, 0);
436                 }
437                 if (strcasecmp(t->u.equality.attr, "AAC") == 0 &&
438                     t->u.equality.value.length == 4) {
439                         acct_control = IVAL(t->u.equality.value.data, 0);
440                 }
441         }
442
443         if (domain_guid == NULL && domain == NULL) {
444                 domain = lp_realm(cldapd->task->lp_ctx);
445         }
446
447         if (version == -1) {
448                 goto failed;
449         }
450
451         DEBUG(5,("cldap netlogon query domain=%s host=%s user=%s version=%d guid=%s\n",
452                  domain, host, user, version, domain_guid));
453
454         status = fill_netlogon_samlogon_response(cldapd->samctx, tmp_ctx, domain, NULL, NULL, domain_guid,
455                                                  user, acct_control, src->addr, 
456                                                  version, cldapd->task->lp_ctx, &netlogon);
457         if (!NT_STATUS_IS_OK(status)) {
458                 goto failed;
459         }
460
461         status = cldap_netlogon_reply(cldap, message_id, src, version,
462                                       &netlogon);
463         if (!NT_STATUS_IS_OK(status)) {
464                 goto failed;
465         }
466
467         talloc_free(tmp_ctx);
468         return;
469         
470 failed:
471         DEBUG(2,("cldap netlogon query failed domain=%s host=%s version=%d - %s\n",
472                  domain, host, version, nt_errstr(status)));
473         talloc_free(tmp_ctx);
474         cldap_empty_reply(cldap, message_id, src);      
475 }