libreplace(samba4): let LIBREPLACE depend on LIBREPLACE_NETWORK for now
[samba.git] / source4 / cldap_server / netlogon.c
index 1dec27990589365f1119e023f860b98d1687737b..32a71e43afb3433bae332a2b4dd4c1f507a7672f 100644 (file)
@@ -20,7 +20,7 @@
 */
 
 #include "includes.h"
-#include "libcli/ldap/ldap.h"
+#include "libcli/ldap/ldap_ndr.h"
 #include "lib/ldb/include/ldb.h"
 #include "lib/ldb/include/ldb_errors.h"
 #include "lib/events/events.h"
 #include "librpc/gen_ndr/ndr_misc.h"
 #include "dsdb/samdb/samdb.h"
 #include "auth/auth.h"
-#include "db_wrap.h"
+#include "ldb_wrap.h"
 #include "system/network.h"
 #include "lib/socket/netif.h"
+#include "param/param.h"
 
 /*
   fill in the cldap netlogon union for a given version
@@ -44,13 +45,14 @@ static NTSTATUS cldapd_netlogon_fill(struct cldapd_server *cldapd,
                                     const char *user,
                                     const char *src_address,
                                     uint32_t version,
+                                    struct loadparm_context *lp_ctx,
                                     union nbt_cldap_netlogon *netlogon)
 {
        const char *ref_attrs[] = {"nETBIOSName", "dnsRoot", "ncName", NULL};
        const char *dom_attrs[] = {"objectGUID", NULL};
-       struct ldb_message **ref_res, **dom_res;
-       int ret, count = 0;
-       const char **services = lp_server_services();
+       struct ldb_result *ref_res = NULL, *dom_res = NULL;
+       int ret;
+       const char **services = lp_server_services(lp_ctx);
        uint32_t server_type;
        const char *pdc_name;
        struct GUID domain_uuid;
@@ -62,6 +64,7 @@ static NTSTATUS cldapd_netlogon_fill(struct cldapd_server *cldapd,
        const char *client_site;
        const char *pdc_ip;
        struct ldb_dn *partitions_basedn;
+       struct interface *ifaces;
 
        partitions_basedn = samdb_partitions_dn(cldapd->samctx, mem_ctx);
 
@@ -71,62 +74,106 @@ static NTSTATUS cldapd_netlogon_fill(struct cldapd_server *cldapd,
        }
 
        if (domain) {
-               struct ldb_result *dom_ldb_result;
                struct ldb_dn *dom_dn;
                /* try and find the domain */
-               count = gendb_search(cldapd->samctx, mem_ctx, partitions_basedn, &ref_res, ref_attrs, 
-                                  "(&(&(objectClass=crossRef)(dnsRoot=%s))(nETBIOSName=*))", 
-                                  domain);
-               if (count == 1) {
-                       dom_dn = samdb_result_dn(cldapd->samctx, mem_ctx, ref_res[0], "ncName", NULL);
+
+               ret = ldb_search_exp_fmt(cldapd->samctx, mem_ctx, &ref_res, 
+                                        partitions_basedn, LDB_SCOPE_ONELEVEL, 
+                                        ref_attrs, 
+                                        "(&(&(objectClass=crossRef)(dnsRoot=%s))(nETBIOSName=*))",
+                                        domain);
+       
+               if (ret != LDB_SUCCESS) {
+                       DEBUG(2,("Unable to find referece to '%s' in sam: %s\n",
+                                domain, 
+                                ldb_errstring(cldapd->samctx)));
+                       return NT_STATUS_NO_SUCH_DOMAIN;
+               } else if (ref_res->count == 1) {
+                       talloc_steal(mem_ctx, dom_res);
+                       dom_dn = ldb_msg_find_attr_as_dn(cldapd->samctx, mem_ctx, ref_res->msgs[0], "ncName");
                        if (!dom_dn) {
                                return NT_STATUS_NO_SUCH_DOMAIN;
                        }
                        ret = ldb_search(cldapd->samctx, dom_dn,
                                         LDB_SCOPE_BASE, "objectClass=domain", 
-                                        dom_attrs, &dom_ldb_result);
+                                        dom_attrs, &dom_res);
                        if (ret != LDB_SUCCESS) {
                                DEBUG(2,("Error finding domain '%s'/'%s' in sam: %s\n", domain, ldb_dn_get_linearized(dom_dn), ldb_errstring(cldapd->samctx)));
                                return NT_STATUS_NO_SUCH_DOMAIN;
                        }
-                       talloc_steal(mem_ctx, dom_ldb_result);
-                       if (dom_ldb_result->count != 1) {
+                       talloc_steal(mem_ctx, dom_res);
+                       if (dom_res->count != 1) {
                                DEBUG(2,("Error finding domain '%s'/'%s' in sam\n", domain, ldb_dn_get_linearized(dom_dn)));
                                return NT_STATUS_NO_SUCH_DOMAIN;
                        }
-                       dom_res = dom_ldb_result->msgs;
+               } else if (ref_res->count > 1) {
+                       talloc_free(ref_res);
+                       return NT_STATUS_NO_SUCH_DOMAIN;
                }
        }
 
-       if (count == 0 && domain_guid) {
-               /* OK, so no dice with the name, try and find the domain with the GUID */
-               count = gendb_search(cldapd->samctx, mem_ctx, NULL, &dom_res, dom_attrs, 
-                                  "(&(objectClass=domainDNS)(objectGUID=%s))", 
-                                  domain_guid);
-               if (count == 1) {
+       if ((dom_res == NULL || dom_res->count == 0) && domain_guid) {
+               ref_res = NULL;
+
+               ret = ldb_search_exp_fmt(cldapd->samctx, mem_ctx, &dom_res,
+                                NULL, LDB_SCOPE_SUBTREE, 
+                                dom_attrs, 
+                                "(&(objectClass=domainDNS)(objectGUID=%s))", 
+                                domain_guid);
+               
+               if (ret != LDB_SUCCESS) {
+                       DEBUG(2,("Unable to find referece to GUID '%s' in sam: %s\n",
+                                domain_guid, 
+                                ldb_errstring(cldapd->samctx)));
+                       return NT_STATUS_NO_SUCH_DOMAIN;
+               } else if (dom_res->count == 1) {
                        /* try and find the domain */
-                       ret = gendb_search(cldapd->samctx, mem_ctx, partitions_basedn, &ref_res, ref_attrs, 
-                                          "(&(objectClass=crossRef)(ncName=%s))", 
-                                          ldb_dn_get_linearized(dom_res[0]->dn));
-                       if (ret != 1) {
+                       ret = ldb_search_exp_fmt(cldapd->samctx, mem_ctx, &ref_res,
+                                                partitions_basedn, LDB_SCOPE_ONELEVEL, 
+                                                ref_attrs, 
+                                                "(&(objectClass=crossRef)(ncName=%s))", 
+                                                ldb_dn_get_linearized(dom_res->msgs[0]->dn));
+                       
+                       if (ret != LDB_SUCCESS) {
+                               DEBUG(2,("Unable to find referece to '%s' in sam: %s\n",
+                                        ldb_dn_get_linearized(dom_res->msgs[0]->dn), 
+                                        ldb_errstring(cldapd->samctx)));
+                               return NT_STATUS_NO_SUCH_DOMAIN;
+                               
+                       } else if (ref_res->count != 1) {
                                DEBUG(2,("Unable to find referece to '%s' in sam\n",
-                                        ldb_dn_get_linearized(dom_res[0]->dn)));
+                                        ldb_dn_get_linearized(dom_res->msgs[0]->dn)));
                                return NT_STATUS_NO_SUCH_DOMAIN;
                        }
+               } else if (dom_res->count > 1) {
+                       talloc_free(ref_res);
+                       return NT_STATUS_NO_SUCH_DOMAIN;
                }
        }
 
-       if (count == 0) {
+       if ((ref_res == NULL || ref_res->count == 0)) {
+               DEBUG(2,("Unable to find domain reference with name %s or GUID {%s}\n", domain, domain_guid));
+               return NT_STATUS_NO_SUCH_DOMAIN;
+       }
+
+       if ((dom_res == NULL || dom_res->count == 0)) {
                DEBUG(2,("Unable to find domain with name %s or GUID {%s}\n", domain, domain_guid));
                return NT_STATUS_NO_SUCH_DOMAIN;
        }
 
        server_type      = 
-               NBT_SERVER_PDC | NBT_SERVER_GC | 
                NBT_SERVER_DS | NBT_SERVER_TIMESERV |
                NBT_SERVER_CLOSEST | NBT_SERVER_WRITABLE | 
                NBT_SERVER_GOOD_TIMESERV;
 
+       if (samdb_is_pdc(cldapd->samctx)) {
+               server_type |= NBT_SERVER_PDC;
+       }
+
+       if (samdb_is_gc(cldapd->samctx)) {
+               server_type |= NBT_SERVER_GC;
+       }
+
        if (str_list_check(services, "ldap")) {
                server_type |= NBT_SERVER_LDAP;
        }
@@ -135,18 +182,21 @@ static NTSTATUS cldapd_netlogon_fill(struct cldapd_server *cldapd,
                server_type |= NBT_SERVER_KDC;
        }
 
-       pdc_name         = talloc_asprintf(mem_ctx, "\\\\%s", lp_netbios_name());
-       domain_uuid      = samdb_result_guid(dom_res[0], "objectGUID");
-       realm            = samdb_result_string(ref_res[0], "dnsRoot", lp_realm());
-       dns_domain       = samdb_result_string(ref_res[0], "dnsRoot", lp_realm());
+       pdc_name         = talloc_asprintf(mem_ctx, "\\\\%s", lp_netbios_name(lp_ctx));
+       domain_uuid      = samdb_result_guid(dom_res->msgs[0], "objectGUID");
+       realm            = samdb_result_string(ref_res->msgs[0], "dnsRoot", lp_realm(lp_ctx));
+       dns_domain       = samdb_result_string(ref_res->msgs[0], "dnsRoot", lp_realm(lp_ctx));
        pdc_dns_name     = talloc_asprintf(mem_ctx, "%s.%s", 
-                                          strlower_talloc(mem_ctx, lp_netbios_name()), 
+                                          strlower_talloc(mem_ctx, 
+                                                          lp_netbios_name(lp_ctx)), 
                                           dns_domain);
 
-       flatname         = samdb_result_string(ref_res[0], "nETBIOSName", lp_workgroup());
+       flatname         = samdb_result_string(ref_res->msgs[0], "nETBIOSName", 
+                                              lp_workgroup(lp_ctx));
        server_site      = "Default-First-Site-Name";
        client_site      = "Default-First-Site-Name";
-       pdc_ip           = iface_best_ip(src_address);
+       load_interfaces(mem_ctx, lp_interfaces(lp_ctx), &ifaces);
+       pdc_ip           = iface_best_ip(ifaces, src_address);
 
        ZERO_STRUCTP(netlogon);
 
@@ -180,14 +230,14 @@ static NTSTATUS cldapd_netlogon_fill(struct cldapd_server *cldapd,
        case 5:
        case 6:
        case 7:
-               netlogon->logon5.type         = (user?23+2:23);
+               netlogon->logon5.type         = (user?NETLOGON_RESPONSE_FROM_PDC_USER:NETLOGON_RESPONSE_FROM_PDC2);
                netlogon->logon5.server_type  = server_type;
                netlogon->logon5.domain_uuid  = domain_uuid;
                netlogon->logon5.forest       = realm;
                netlogon->logon5.dns_domain   = dns_domain;
                netlogon->logon5.pdc_dns_name = pdc_dns_name;
                netlogon->logon5.domain       = flatname;
-               netlogon->logon5.pdc_name     = lp_netbios_name();
+               netlogon->logon5.pdc_name     = lp_netbios_name(lp_ctx);
                netlogon->logon5.user_name    = user;
                netlogon->logon5.server_site  = server_site;
                netlogon->logon5.client_site  = client_site;
@@ -195,14 +245,14 @@ static NTSTATUS cldapd_netlogon_fill(struct cldapd_server *cldapd,
                netlogon->logon5.lm20_token   = 0xFFFF;
                break;
        default:
-               netlogon->logon13.type         = (user?23+2:23);
+               netlogon->logon13.type         = (user?NETLOGON_RESPONSE_FROM_PDC_USER:NETLOGON_RESPONSE_FROM_PDC2);
                netlogon->logon13.server_type  = server_type;
                netlogon->logon13.domain_uuid  = domain_uuid;
                netlogon->logon13.forest       = realm;
                netlogon->logon13.dns_domain   = dns_domain;
                netlogon->logon13.pdc_dns_name = pdc_dns_name;
                netlogon->logon13.domain       = flatname;
-               netlogon->logon13.pdc_name     = lp_netbios_name();
+               netlogon->logon13.pdc_name     = lp_netbios_name(lp_ctx);
                netlogon->logon13.user_name    = user;
                netlogon->logon13.server_site  = server_site;
                netlogon->logon13.client_site  = client_site;
@@ -286,7 +336,7 @@ void cldapd_netlogon_request(struct cldap_socket *cldap,
        }
 
        if (domain_guid == NULL && domain == NULL) {
-               domain = lp_realm();
+               domain = lp_realm(cldapd->task->lp_ctx);
        }
 
        if (version == -1) {
@@ -298,7 +348,7 @@ void cldapd_netlogon_request(struct cldap_socket *cldap,
 
        status = cldapd_netlogon_fill(cldapd, tmp_ctx, domain, domain_guid, 
                                      user, src->addr, 
-                                     version, &netlogon);
+                                     version, cldapd->task->lp_ctx, &netlogon);
        if (!NT_STATUS_IS_OK(status)) {
                goto failed;
        }