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