LCOV - code coverage report
Current view: top level - dfs_server - dfs_server_ad.c (source / functions) Hit Total Coverage
Test: coverage report for recycleplus df22b230 Lines: 292 405 72.1 %
Date: 2024-02-14 10:14:15 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Copyright Matthieu Patou <mat@matws.net> 2010-2011
       5             :    Copyright Stefan Metzmacher 2011
       6             : 
       7             :    This program is free software; you can redistribute it and/or modify
       8             :    it under the terms of the GNU General Public License as published by
       9             :    the Free Software Foundation; either version 3 of the License, or
      10             :    (at your option) any later version.
      11             : 
      12             :    This program is distributed in the hope that it will be useful,
      13             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :    GNU General Public License for more details.
      16             : 
      17             :    You should have received a copy of the GNU General Public License
      18             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             : */
      20             : 
      21             : #include "includes.h"
      22             : #include "librpc/gen_ndr/dfsblobs.h"
      23             : #include "librpc/gen_ndr/ndr_dfsblobs.h"
      24             : #include "dsdb/samdb/samdb.h"
      25             : #include "auth/session.h"
      26             : #include "param/param.h"
      27             : #include "lib/tsocket/tsocket.h"
      28             : #include "dfs_server/dfs_server_ad.h"
      29             : #include "lib/util/util_net.h"
      30             : #include "libds/common/roles.h"
      31             : 
      32             : #define MAX_DFS_RESPONSE 56*1024 /* 56 Kb */
      33             : 
      34             : #undef strcasecmp
      35             : 
      36             : /* A DC set is a group of DC, they might have been grouped together
      37             :    because they belong to the same site, or to site with same cost ...
      38             : */
      39             : struct dc_set {
      40             :         const char **names;
      41             :         uint32_t count;
      42             : };
      43             : 
      44           8 : static void shuffle_dc_set(struct dc_set *list)
      45             : {
      46             :         uint32_t i;
      47             : 
      48           8 :         for (i = list->count; i > 1; i--) {
      49             :                 uint32_t r;
      50             :                 const char *tmp;
      51             : 
      52           0 :                 r = generate_random() % i;
      53             : 
      54           0 :                 tmp = list->names[i - 1];
      55           0 :                 list->names[i - 1] = list->names[r];
      56           0 :                 list->names[r] = tmp;
      57             :         }
      58           8 : }
      59             : 
      60             : /*
      61             :   fill a referral type structure
      62             :  */
      63           2 : static NTSTATUS fill_normal_dfs_referraltype(TALLOC_CTX *mem_ctx,
      64             :                                              struct dfs_referral_type *ref,
      65             :                                              uint16_t version,
      66             :                                              const char *dfs_path,
      67             :                                              const char *server_path, int isfirstoffset)
      68             : {
      69           2 :         ZERO_STRUCTP(ref);
      70           2 :         switch (version) {
      71           1 :         case 4:
      72           1 :                 ref->version = version;
      73             :                 /* For the moment there is a bug with XP that doesn't
      74             :                  * seem to appreciate much level4 so we return just
      75             :                  * level 3 for everyone
      76             :                  */
      77           1 :                 ref->referral.v4.server_type = DFS_SERVER_NON_ROOT;
      78             :                 /* "normal" referral seems to always include the GUID */
      79           1 :                 ref->referral.v4.size = 34;
      80             : 
      81           1 :                 if (isfirstoffset) {
      82           1 :                         ref->referral.v4.entry_flags =  DFS_HEADER_FLAG_TARGET_BCK;
      83             :                 }
      84           1 :                 ref->referral.v4.ttl = 900; /* As w2k8r2 */
      85           1 :                 ref->referral.v4.referrals.r1.DFS_path = talloc_strdup(mem_ctx, dfs_path);
      86           1 :                 if (ref->referral.v4.referrals.r1.DFS_path == NULL) {
      87           0 :                         return NT_STATUS_NO_MEMORY;
      88             :                 }
      89           1 :                 ref->referral.v4.referrals.r1.DFS_alt_path = talloc_strdup(mem_ctx, dfs_path);
      90           1 :                 if (ref->referral.v4.referrals.r1.DFS_alt_path == NULL) {
      91           0 :                         return NT_STATUS_NO_MEMORY;
      92             :                 }
      93           1 :                 ref->referral.v4.referrals.r1.netw_address = talloc_strdup(mem_ctx, server_path);
      94           1 :                 if (ref->referral.v4.referrals.r1.netw_address == NULL) {
      95           0 :                         return NT_STATUS_NO_MEMORY;
      96             :                 }
      97           1 :                 return NT_STATUS_OK;
      98           1 :         case 3:
      99           1 :                 ref->version = version;
     100           1 :                 ref->referral.v3.server_type = DFS_SERVER_NON_ROOT;
     101             :                 /* "normal" referral seems to always include the GUID */
     102           1 :                 ref->referral.v3.size = 34;
     103             : 
     104           1 :                 ref->referral.v3.entry_flags = 0;
     105           1 :                 ref->referral.v3.ttl = 600; /* As w2k3 */
     106           1 :                 ref->referral.v3.referrals.r1.DFS_path = talloc_strdup(mem_ctx, dfs_path);
     107           1 :                 if (ref->referral.v3.referrals.r1.DFS_path == NULL) {
     108           0 :                         return NT_STATUS_NO_MEMORY;
     109             :                 }
     110           1 :                 ref->referral.v3.referrals.r1.DFS_alt_path = talloc_strdup(mem_ctx, dfs_path);
     111           1 :                 if (ref->referral.v3.referrals.r1.DFS_alt_path == NULL) {
     112           0 :                         return NT_STATUS_NO_MEMORY;
     113             :                 }
     114           1 :                 ref->referral.v3.referrals.r1.netw_address = talloc_strdup(mem_ctx, server_path);
     115           1 :                 if (ref->referral.v3.referrals.r1.netw_address == NULL) {
     116           0 :                         return NT_STATUS_NO_MEMORY;
     117             :                 }
     118           1 :                 return NT_STATUS_OK;
     119             :         }
     120           0 :         return NT_STATUS_INVALID_LEVEL;
     121             : }
     122             : 
     123             : /*
     124             :   fill a domain refererral
     125             :  */
     126          18 : static NTSTATUS fill_domain_dfs_referraltype(TALLOC_CTX *mem_ctx,
     127             :                                              struct dfs_referral_type *ref,
     128             :                                              uint16_t version,
     129             :                                              const char *domain,
     130             :                                              const char **names,
     131             :                                              uint16_t numnames)
     132             : {
     133          18 :         switch (version) {
     134          18 :         case 3:
     135          18 :                 ZERO_STRUCTP(ref);
     136          18 :                 DEBUG(8, ("Called fill_domain_dfs_referraltype\n"));
     137          18 :                 ref->version = version;
     138          18 :                 ref->referral.v3.server_type = DFS_SERVER_NON_ROOT;
     139             : #if 0
     140             :                 /* We use to have variable size, on Windows 2008R2 it's the same
     141             :                  * and it seems that it gives better results so ... let's use the same
     142             :                  * size.
     143             :                  *
     144             :                  * Additional note: XP SP2 will ask for version 3 and SP3 for version 4.
     145             :                  */
     146             :                 /*
     147             :                  * It's hard coded ... don't think it's a good way but the
     148             :                  * sizeof return not the correct values
     149             :                  *
     150             :                  * We have 18 if the GUID is not included 34 otherwise
     151             :                  */
     152             :                 if (numnames == 0) {
     153             :                         /* Windows return without the guid when returning domain list
     154             :                          */
     155             :                         ref->referral.v3.size = 18;
     156             :                 } else {
     157             :                         ref->referral.v3.size = 34;
     158             :                 }
     159             : #endif
     160             :                 /* As seen in w2k8r2 it always return the null GUID */
     161          18 :                 ref->referral.v3.size = 34;
     162          18 :                 ref->referral.v3.entry_flags = DFS_FLAG_REFERRAL_DOMAIN_RESP;
     163          18 :                 ref->referral.v3.ttl = 600; /* As w2k3 and w2k8r2*/
     164          18 :                 ref->referral.v3.referrals.r2.special_name = talloc_strdup(mem_ctx,
     165             :                                                                         domain);
     166          18 :                 if (ref->referral.v3.referrals.r2.special_name == NULL) {
     167           0 :                         return NT_STATUS_NO_MEMORY;
     168             :                 }
     169          18 :                 ref->referral.v3.referrals.r2.nb_expanded_names = numnames;
     170             :                 /* Put the final terminator */
     171          18 :                 if (names) {
     172             :                         int i;
     173           6 :                         const char **names2 = talloc_array(mem_ctx, const char *,
     174             :                                                            numnames+1);
     175           6 :                         NT_STATUS_HAVE_NO_MEMORY(names2);
     176          12 :                         for (i = 0; i<numnames; i++) {
     177           6 :                                 names2[i] = talloc_asprintf(names2, "\\%s", names[i]);
     178           6 :                                 NT_STATUS_HAVE_NO_MEMORY(names2[i]);
     179             :                         }
     180           6 :                         names2[numnames] = NULL;
     181           6 :                         ref->referral.v3.referrals.r2.expanded_names = names2;
     182             :                 }
     183          18 :                 return NT_STATUS_OK;
     184             :         }
     185           0 :         return NT_STATUS_INVALID_LEVEL;
     186             : }
     187             : 
     188             : /*
     189             :   get the DCs list within a site
     190             :  */
     191           8 : static NTSTATUS get_dcs_insite(TALLOC_CTX *ctx, struct ldb_context *ldb,
     192             :                                struct ldb_dn *sitedn, struct dc_set *list,
     193             :                                bool dofqdn)
     194             : {
     195             :         static const char *attrs[] = { "serverReference", NULL };
     196             :         static const char *attrs2[] = { "dNSHostName", "sAMAccountName", NULL };
     197             :         struct ldb_result *r;
     198             :         unsigned int i;
     199             :         int ret;
     200             :         const char **dc_list;
     201             : 
     202           8 :         ret = ldb_search(ldb, ctx, &r, sitedn, LDB_SCOPE_SUBTREE, attrs,
     203             :                          "(&(objectClass=server)(serverReference=*))");
     204           8 :         if (ret != LDB_SUCCESS) {
     205           0 :                 DEBUG(2,(__location__ ": Failed to get list of servers - %s\n",
     206             :                          ldb_errstring(ldb)));
     207           0 :                 return NT_STATUS_INTERNAL_ERROR;
     208             :         }
     209             : 
     210           8 :         if (r->count == 0) {
     211             :                 /* none in this site */
     212           0 :                 talloc_free(r);
     213           0 :                 return NT_STATUS_OK;
     214             :         }
     215             : 
     216             :         /*
     217             :          * need to search for all server object to know the size of the array.
     218             :          * Search all the object of class server in this site
     219             :          */
     220           8 :         dc_list = talloc_array(r, const char *, r->count);
     221           8 :         if (dc_list == NULL) {
     222           0 :                 TALLOC_FREE(r);
     223           0 :                 return NT_STATUS_NO_MEMORY;
     224             :         }
     225             : 
     226             :         /* TODO put some random here in the order */
     227           8 :         list->names = talloc_realloc(list, list->names, const char *, list->count + r->count);
     228           8 :         if (list->names == NULL) {
     229           0 :                 TALLOC_FREE(r);
     230           0 :                 return NT_STATUS_NO_MEMORY;
     231             :         }
     232             : 
     233          16 :         for (i = 0; i<r->count; i++) {
     234             :                 struct ldb_dn  *dn;
     235             :                 struct ldb_message *msg;
     236             : 
     237           8 :                 dn = ldb_msg_find_attr_as_dn(ldb, ctx, r->msgs[i], "serverReference");
     238           8 :                 if (!dn) {
     239           0 :                         return NT_STATUS_INTERNAL_ERROR;
     240             :                 }
     241             : 
     242           8 :                 ret = dsdb_search_one(ldb, r, &msg, dn, LDB_SCOPE_BASE, attrs2, 0, "(objectClass=computer)");
     243           8 :                 if (ret != LDB_SUCCESS) {
     244           0 :                         DEBUG(2,(__location__ ": Search for computer on %s failed - %s\n",
     245             :                                  ldb_dn_get_linearized(dn), ldb_errstring(ldb)));
     246           0 :                         return NT_STATUS_INTERNAL_ERROR;
     247             :                 }
     248             : 
     249           8 :                 if (dofqdn) {
     250           5 :                         const char *dns = ldb_msg_find_attr_as_string(msg, "dNSHostName", NULL);
     251           5 :                         if (dns == NULL) {
     252           0 :                                 DEBUG(2,(__location__ ": dNSHostName missing on %s\n",
     253             :                                          ldb_dn_get_linearized(dn)));
     254           0 :                                 talloc_free(r);
     255           0 :                                 return NT_STATUS_INTERNAL_ERROR;
     256             :                         }
     257             : 
     258           5 :                         list->names[list->count] = talloc_strdup(list->names, dns);
     259           5 :                         if (list->names[list->count] == NULL) {
     260           0 :                                 TALLOC_FREE(r);
     261           0 :                                 return NT_STATUS_NO_MEMORY;
     262             :                         }
     263             :                 } else {
     264             :                         char *tmp;
     265           3 :                         const char *aname = ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL);
     266           3 :                         if (aname == NULL) {
     267           0 :                                 DEBUG(2,(__location__ ": sAMAccountName missing on %s\n",
     268             :                                          ldb_dn_get_linearized(dn)));
     269           0 :                                 talloc_free(r);
     270           0 :                                 return NT_STATUS_INTERNAL_ERROR;
     271             :                         }
     272             : 
     273           3 :                         tmp = talloc_strdup(list->names, aname);
     274           3 :                         if (tmp == NULL) {
     275           0 :                                 TALLOC_FREE(r);
     276           0 :                                 return NT_STATUS_NO_MEMORY;
     277             :                         }
     278             : 
     279             :                         /* Netbios name is also the sAMAccountName for
     280             :                            computer but without the final $ */
     281           3 :                         tmp[strlen(tmp) - 1] = '\0';
     282           3 :                         list->names[list->count] = tmp;
     283             :                 }
     284           8 :                 list->count++;
     285           8 :                 talloc_free(msg);
     286             :         }
     287             : 
     288           8 :         shuffle_dc_set(list);
     289             : 
     290           8 :         talloc_free(r);
     291           8 :         return NT_STATUS_OK;
     292             : }
     293             : 
     294             : 
     295             : /*
     296             :   get all DCs
     297             :  */
     298           8 : static NTSTATUS get_dcs(TALLOC_CTX *ctx, struct ldb_context *ldb,
     299             :                         const char *searched_site, bool need_fqdn,
     300             :                         struct dc_set ***pset_list, uint32_t flags)
     301             : {
     302             :         /*
     303             :          * Flags will be used later to indicate things like least-expensive
     304             :          * or same-site options
     305             :          */
     306           8 :         const char *attrs_none[] = { NULL };
     307           8 :         const char *attrs3[] = { "name", NULL };
     308             :         struct ldb_dn *configdn, *sitedn, *dn, *sitescontainerdn;
     309             :         struct ldb_result *r;
     310           8 :         struct dc_set **set_list = NULL;
     311             :         uint32_t i;
     312             :         int ret;
     313           8 :         uint32_t current_pos = 0;
     314             :         NTSTATUS status;
     315             :         TALLOC_CTX *subctx;
     316             : 
     317           8 :         *pset_list = set_list = NULL;
     318             : 
     319           8 :         subctx = talloc_new(ctx);
     320           8 :         NT_STATUS_HAVE_NO_MEMORY(subctx);
     321             : 
     322           8 :         configdn = ldb_get_config_basedn(ldb);
     323             : 
     324             :         /* Let's search for the Site container */
     325           8 :         ret = ldb_search(ldb, subctx, &r, configdn, LDB_SCOPE_SUBTREE, attrs_none,
     326             :                          "(objectClass=sitesContainer)");
     327           8 :         if (ret != LDB_SUCCESS) {
     328           0 :                 DEBUG(2,(__location__ ": Failed to find sitesContainer within %s - %s\n",
     329             :                          ldb_dn_get_linearized(configdn), ldb_errstring(ldb)));
     330           0 :                 talloc_free(subctx);
     331           0 :                 return NT_STATUS_INTERNAL_ERROR;
     332             :         }
     333           8 :         if (r->count > 1) {
     334           0 :                 DEBUG(2,(__location__ ": Expected 1 sitesContainer - found %u within %s\n",
     335             :                          r->count, ldb_dn_get_linearized(configdn)));
     336           0 :                 talloc_free(subctx);
     337           0 :                 return NT_STATUS_INTERNAL_ERROR;
     338             :         }
     339             : 
     340           8 :         sitescontainerdn = talloc_steal(subctx, r->msgs[0]->dn);
     341           8 :         talloc_free(r);
     342             : 
     343             :         /*
     344             :          * TODO: Here we should have a more subtle handling
     345             :          * for the case "same-site"
     346             :          */
     347           8 :         ret = ldb_search(ldb, subctx, &r, sitescontainerdn, LDB_SCOPE_SUBTREE,
     348             :                          attrs_none, "(objectClass=server)");
     349           8 :         if (ret != LDB_SUCCESS) {
     350           0 :                 DEBUG(2,(__location__ ": Failed to find servers within %s - %s\n",
     351             :                          ldb_dn_get_linearized(sitescontainerdn), ldb_errstring(ldb)));
     352           0 :                 talloc_free(subctx);
     353           0 :                 return NT_STATUS_INTERNAL_ERROR;
     354             :         }
     355           8 :         talloc_free(r);
     356             : 
     357           8 :         if (searched_site != NULL && searched_site[0] != '\0') {
     358           8 :                 ret = ldb_search(ldb, subctx, &r, configdn, LDB_SCOPE_SUBTREE,
     359             :                                  attrs_none, "(&(name=%s)(objectClass=site))", searched_site);
     360           8 :                 if (ret != LDB_SUCCESS) {
     361           0 :                         talloc_free(subctx);
     362           0 :                         return NT_STATUS_FOOBAR;
     363           8 :                 } else if (r->count != 1) {
     364           0 :                         talloc_free(subctx);
     365           0 :                         return NT_STATUS_FOOBAR;
     366             :                 }
     367             : 
     368             :                 /* All of this was to get the DN of the searched_site */
     369           8 :                 sitedn = r->msgs[0]->dn;
     370             : 
     371             :                 /*
     372             :                  * We will realloc + 2 because we will need one additional place
     373             :                  * for element at current_pos + 1 for the NULL element
     374             :                  */
     375           8 :                 set_list = talloc_realloc(subctx, set_list, struct dc_set *, current_pos+2);
     376           8 :                 if (set_list == NULL) {
     377           0 :                         TALLOC_FREE(subctx);
     378           0 :                         return NT_STATUS_NO_MEMORY;
     379             :                 }
     380             : 
     381           8 :                 set_list[current_pos] = talloc(set_list, struct dc_set);
     382           8 :                 if (set_list[current_pos] == NULL) {
     383           0 :                         TALLOC_FREE(subctx);
     384           0 :                         return NT_STATUS_NO_MEMORY;
     385             :                 }
     386             : 
     387           8 :                 set_list[current_pos]->names = NULL;
     388           8 :                 set_list[current_pos]->count = 0;
     389             : 
     390           8 :                 set_list[current_pos+1] = NULL;
     391             : 
     392           8 :                 status = get_dcs_insite(subctx, ldb, sitedn,
     393           8 :                                         set_list[current_pos], need_fqdn);
     394           8 :                 if (!NT_STATUS_IS_OK(status)) {
     395           0 :                         DEBUG(2,(__location__ ": Failed to get DC from site %s - %s\n",
     396             :                                  ldb_dn_get_linearized(sitedn), nt_errstr(status)));
     397           0 :                         talloc_free(subctx);
     398           0 :                         return status;
     399             :                 }
     400           8 :                 talloc_free(r);
     401           8 :                 current_pos++;
     402             :         }
     403             : 
     404             :         /* Let's find all the sites */
     405           8 :         ret = ldb_search(ldb, subctx, &r, configdn, LDB_SCOPE_SUBTREE, attrs3, "(objectClass=site)");
     406           8 :         if (ret != LDB_SUCCESS) {
     407           0 :                 DEBUG(2,(__location__ ": Failed to find any site containers in %s\n",
     408             :                          ldb_dn_get_linearized(configdn)));
     409           0 :                 talloc_free(subctx);
     410           0 :                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
     411             :         }
     412             : 
     413             :         /*
     414             :          * TODO:
     415             :          * We should randomize the order in the main site,
     416             :          * it's mostly needed for sysvol/netlogon referral.
     417             :          * Depending of flag we either randomize order of the
     418             :          * not "in the same site DCs"
     419             :          * or we randomize by group of site that have the same cost
     420             :          * In the long run we want to manipulate an array of site_set
     421             :          * All the site in one set have the same cost (if least-expansive options is selected)
     422             :          * and we will put all the dc related to 1 site set into 1 DCs set.
     423             :          * Within a site set, site order has to be randomized
     424             :          *
     425             :          * But for the moment we just return the list of sites
     426             :          */
     427           8 :         if (r->count) {
     428             :                 /*
     429             :                  * We will realloc + 2 because we will need one additional place
     430             :                  * for element at current_pos + 1 for the NULL element
     431             :                  */
     432           8 :                 set_list = talloc_realloc(subctx, set_list, struct dc_set *,
     433             :                                           current_pos+2);
     434           8 :                 if (set_list == NULL) {
     435           0 :                         TALLOC_FREE(subctx);
     436           0 :                         return NT_STATUS_NO_MEMORY;
     437             :                 }
     438             : 
     439           8 :                 set_list[current_pos] = talloc(ctx, struct dc_set);
     440           8 :                 if (set_list[current_pos] == NULL) {
     441           0 :                         TALLOC_FREE(subctx);
     442           0 :                         return NT_STATUS_NO_MEMORY;
     443             :                 }
     444             : 
     445           8 :                 set_list[current_pos]->names = NULL;
     446           8 :                 set_list[current_pos]->count = 0;
     447             : 
     448           8 :                 set_list[current_pos+1] = NULL;
     449             :         }
     450             : 
     451          16 :         for (i=0; i<r->count; i++) {
     452           8 :                 const char *site_name = ldb_msg_find_attr_as_string(r->msgs[i], "name", NULL);
     453           8 :                 if (site_name == NULL) {
     454           0 :                         DEBUG(2,(__location__ ": Failed to find name attribute in %s\n",
     455             :                                  ldb_dn_get_linearized(r->msgs[i]->dn)));
     456           0 :                         talloc_free(subctx);
     457           0 :                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
     458             :                 }
     459             : 
     460           8 :                 if (searched_site == NULL ||
     461           8 :                     strcmp(searched_site, site_name) != 0) {
     462           0 :                         DEBUG(2,(__location__ ": Site: %s %s\n",
     463             :                                 searched_site, site_name));
     464             : 
     465             :                         /*
     466             :                          * Do all the site but the one of the client
     467             :                          * (because it has already been done ...)
     468             :                          */
     469           0 :                         dn = r->msgs[i]->dn;
     470             : 
     471           0 :                         status = get_dcs_insite(subctx, ldb, dn,
     472           0 :                                                 set_list[current_pos],
     473             :                                                 need_fqdn);
     474           0 :                         if (!NT_STATUS_IS_OK(status)) {
     475           0 :                                 talloc_free(subctx);
     476           0 :                                 return status;
     477             :                         }
     478             :                 }
     479             :         }
     480             : 
     481           8 :         *pset_list = talloc_move(ctx, &set_list);
     482           8 :         talloc_free(subctx);
     483           8 :         return NT_STATUS_OK;
     484             : }
     485             : 
     486           7 : static NTSTATUS dodomain_referral(struct loadparm_context *lp_ctx,
     487             :                                   struct ldb_context *sam_ctx,
     488             :                                   const struct tsocket_address *client,
     489             :                                   struct dfs_GetDFSReferral *r)
     490             : {
     491             :         /*
     492             :          * TODO for the moment we just return the local domain
     493             :          */
     494             :         NTSTATUS status;
     495           7 :         const char *dns_domain = lpcfg_dnsdomain(lp_ctx);
     496           7 :         const char *netbios_domain = lpcfg_workgroup(lp_ctx);
     497             :         struct dfs_referral_type *referrals;
     498             :         const char *referral_str;
     499             :         /* In the future this needs to be fetched from the ldb */
     500           7 :         uint32_t found_domain = 2;
     501             : 
     502           7 :         if (lpcfg_server_role(lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC) {
     503           0 :                 DEBUG(10 ,("Received a domain referral request on a non DC\n"));
     504           0 :                 return NT_STATUS_INVALID_PARAMETER;
     505             :         }
     506             : 
     507           7 :         if (r->in.req.max_referral_level < 3) {
     508           1 :                 DEBUG(2,("invalid max_referral_level %u\n",
     509             :                          r->in.req.max_referral_level));
     510           1 :                 return NT_STATUS_UNSUCCESSFUL;
     511             :         }
     512             : 
     513           6 :         r->out.resp = talloc_zero(r, struct dfs_referral_resp);
     514           6 :         if (r->out.resp == NULL) {
     515           0 :                 return NT_STATUS_NO_MEMORY;
     516             :         }
     517             : 
     518           6 :         r->out.resp->path_consumed = 0;
     519           6 :         r->out.resp->header_flags = 0; /* Do like w2k3 */
     520           6 :         r->out.resp->nb_referrals = found_domain; /* the fqdn one + the NT domain */
     521             : 
     522           6 :         referrals = talloc_zero_array(r->out.resp,
     523             :                                       struct dfs_referral_type,
     524             :                                       r->out.resp->nb_referrals);
     525           6 :         if (referrals == NULL) {
     526           0 :                 return NT_STATUS_NO_MEMORY;
     527             :         }
     528           6 :         r->out.resp->referral_entries = referrals;
     529             : 
     530           6 :         referral_str = talloc_asprintf(r, "\\%s", netbios_domain);
     531           6 :         if (referral_str == NULL) {
     532           0 :                 return NT_STATUS_NO_MEMORY;
     533             :         }
     534             : 
     535           6 :         status = fill_domain_dfs_referraltype(referrals,
     536             :                                               &referrals[0], 3,
     537             :                                               referral_str,
     538             :                                               NULL, 0);
     539           6 :         if (!NT_STATUS_IS_OK(status)) {
     540           0 :                 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
     541             :                          __location__, nt_errstr(status)));
     542           0 :                 return status;
     543             :         }
     544             : 
     545           6 :         referral_str = talloc_asprintf(r, "\\%s", dns_domain);
     546           6 :         if (referral_str == NULL) {
     547           0 :                 return NT_STATUS_NO_MEMORY;
     548             :         }
     549             : 
     550           6 :         status = fill_domain_dfs_referraltype(referrals,
     551             :                                               &referrals[1], 3,
     552             :                                               referral_str,
     553             :                                               NULL, 0);
     554           6 :         if (!NT_STATUS_IS_OK(status)) {
     555           0 :                 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
     556             :                          __location__, nt_errstr(status)));
     557           0 :                 return status;
     558             :         }
     559             : 
     560           6 :         return NT_STATUS_OK;
     561             : }
     562             : 
     563             : /*
     564             :  * Handle the logic for dfs referral request like
     565             :  * \\dns_domain or \\netbios_domain.
     566             :  */
     567           6 : static NTSTATUS dodc_referral(struct loadparm_context *lp_ctx,
     568             :                               struct ldb_context *sam_ctx,
     569             :                               const struct tsocket_address *client,
     570             :                               struct dfs_GetDFSReferral *r,
     571             :                               const char *domain_name)
     572             : {
     573             :         NTSTATUS status;
     574           6 :         const char *site_name = NULL; /* Name of the site where the client is */
     575           6 :         bool need_fqdn = false;
     576             :         unsigned int i;
     577           6 :         const char **dc_list = NULL;
     578           6 :         uint32_t num_dcs = 0;
     579             :         struct dc_set **set;
     580           6 :         char *client_str = NULL;
     581             :         struct dfs_referral_type *referrals;
     582             :         const char *referral_str;
     583             : 
     584           6 :         if (lpcfg_server_role(lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC) {
     585           0 :                 return NT_STATUS_INVALID_PARAMETER;
     586             :         }
     587             : 
     588           6 :         if (r->in.req.max_referral_level < 3) {
     589           0 :                 DEBUG(2,("invalid max_referral_level %u\n",
     590             :                          r->in.req.max_referral_level));
     591           0 :                 return NT_STATUS_UNSUCCESSFUL;
     592             :         }
     593             : 
     594           6 :         DEBUG(10, ("in this we have request for %s requested is %s\n",
     595             :                    domain_name, r->in.req.servername));
     596             : 
     597           6 :         if (strchr(domain_name,'.')) {
     598           3 :                 need_fqdn = 1;
     599             :         }
     600             : 
     601           6 :         if (tsocket_address_is_inet(client, "ip")) {
     602           6 :                 client_str = tsocket_address_inet_addr_string(client, r);
     603           6 :                 if (client_str == NULL) {
     604           0 :                         return NT_STATUS_NO_MEMORY;
     605             :                 }
     606             :         }
     607             : 
     608           6 :         site_name = samdb_client_site_name(sam_ctx, r, client_str, NULL, true);
     609             : 
     610           6 :         status = get_dcs(r, sam_ctx, site_name, need_fqdn, &set, 0);
     611           6 :         if (!NT_STATUS_IS_OK(status)) {
     612           0 :                 DEBUG(3,("Unable to get list of DCs - %s\n",
     613             :                          nt_errstr(status)));
     614           0 :                 return status;
     615             :         }
     616             : 
     617          18 :         for(i=0; set[i]; i++) {
     618             :                 uint32_t j;
     619             : 
     620          12 :                 dc_list = talloc_realloc(r, dc_list, const char*,
     621             :                                          num_dcs + set[i]->count + 1);
     622          12 :                 if (dc_list == NULL) {
     623           0 :                         return NT_STATUS_NO_MEMORY;
     624             :                 }
     625             : 
     626          18 :                 for(j=0; j<set[i]->count; j++) {
     627           6 :                         dc_list[num_dcs + j] = talloc_move(dc_list,
     628             :                                                            &set[i]->names[j]);
     629             :                 }
     630          12 :                 num_dcs = num_dcs + set[i]->count;
     631          12 :                 TALLOC_FREE(set[i]);
     632          12 :                 dc_list[num_dcs] = NULL;
     633             :         }
     634             : 
     635           6 :         r->out.resp = talloc_zero(r, struct dfs_referral_resp);
     636           6 :         if (r->out.resp == NULL) {
     637           0 :                 return NT_STATUS_NO_MEMORY;
     638             :         }
     639             : 
     640           6 :         r->out.resp->path_consumed = 0;
     641           6 :         r->out.resp->header_flags = 0; /* Do like w2k3 */
     642           6 :         r->out.resp->nb_referrals = 1;
     643             : 
     644           6 :         referrals = talloc_zero_array(r->out.resp,
     645             :                                       struct dfs_referral_type,
     646             :                                       r->out.resp->nb_referrals);
     647           6 :         if (referrals == NULL) {
     648           0 :                 return NT_STATUS_NO_MEMORY;
     649             :         }
     650           6 :         r->out.resp->referral_entries = referrals;
     651             : 
     652           6 :         if (r->in.req.servername[0] == '\\') {
     653           4 :                 referral_str = talloc_asprintf(referrals, "%s",
     654             :                                                domain_name);
     655             :         } else {
     656           2 :                 referral_str = talloc_asprintf(referrals, "\\%s",
     657             :                                                domain_name);
     658             :         }
     659           6 :         if (referral_str == NULL) {
     660           0 :                 return NT_STATUS_NO_MEMORY;
     661             :         }
     662             : 
     663           6 :         status = fill_domain_dfs_referraltype(referrals,
     664             :                                               &referrals[0], 3,
     665             :                                               referral_str,
     666             :                                               dc_list, num_dcs);
     667           6 :         if (!NT_STATUS_IS_OK(status)) {
     668           0 :                 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
     669             :                          __location__, nt_errstr(status)));
     670           0 :                 return status;
     671             :         }
     672             : 
     673           6 :         return NT_STATUS_OK;
     674             : }
     675             : 
     676             : /*
     677             :  * Handle the logic for dfs referral request like
     678             :  * \\domain\sysvol or \\domain\netlogon
     679             :  */
     680           2 : static NTSTATUS dosysvol_referral(struct loadparm_context *lp_ctx,
     681             :                                   struct ldb_context *sam_ctx,
     682             :                                   const struct tsocket_address *client,
     683             :                                   struct dfs_GetDFSReferral *r,
     684             :                                   const char *domain_name,
     685             :                                   const char *dfs_name)
     686             : {
     687           2 :         const char *site_name = NULL; /* Name of the site where the client is */
     688           2 :         bool need_fqdn = false;
     689           2 :         unsigned int i, c = 0, nb_entries = 0;
     690             :         struct dc_set **set;
     691           2 :         char *client_str = NULL;
     692             :         NTSTATUS status;
     693             :         struct dfs_referral_type *referrals;
     694             : 
     695           2 :         if (lpcfg_server_role(lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC) {
     696           0 :                 return NT_STATUS_INVALID_PARAMETER;
     697             :         }
     698             : 
     699           2 :         if (r->in.req.max_referral_level < 3) {
     700           0 :                 DEBUG(2,("invalid max_referral_level %u\n",
     701             :                          r->in.req.max_referral_level));
     702           0 :                 return NT_STATUS_UNSUCCESSFUL;
     703             :         }
     704             : 
     705           2 :         DEBUG(10, ("in this we have request for %s and share %s requested is %s\n",
     706             :                    domain_name, dfs_name, r->in.req.servername));
     707             : 
     708           2 :         if (strchr(domain_name,'.')) {
     709           2 :                 need_fqdn = 1;
     710             :         }
     711             : 
     712           2 :         if (tsocket_address_is_inet(client, "ip")) {
     713           2 :                 client_str = tsocket_address_inet_addr_string(client, r);
     714           2 :                 if (client_str == NULL) {
     715           0 :                         return NT_STATUS_NO_MEMORY;
     716             :                 }
     717             :         }
     718             : 
     719           2 :         site_name = samdb_client_site_name(sam_ctx, r, client_str, NULL, true);
     720             : 
     721           2 :         status = get_dcs(r, sam_ctx, site_name, need_fqdn, &set, 0);
     722           2 :         if (!NT_STATUS_IS_OK(status)) {
     723           0 :                 DEBUG(3,("Unable to get list of DCs - %s\n",
     724             :                          nt_errstr(status)));
     725           0 :                 return status;
     726             :         }
     727             : 
     728           6 :         for(i=0; set[i]; i++) {
     729           4 :                 nb_entries = nb_entries + set[i]->count;
     730             :         }
     731             : 
     732           2 :         r->out.resp = talloc_zero(r, struct dfs_referral_resp);
     733           2 :         if (r->out.resp == NULL) {
     734           0 :                 return NT_STATUS_NO_MEMORY;
     735             :         }
     736             : 
     737             :         /* The length is expected in bytes */
     738           2 :         r->out.resp->path_consumed = strlen_m(r->in.req.servername) * 2;
     739             :         /* Do like w2k3 and like in 3.3.5.3 of MS-DFSC*/
     740           2 :         r->out.resp->header_flags = DFS_HEADER_FLAG_STORAGE_SVR;
     741           2 :         r->out.resp->nb_referrals = nb_entries;
     742             : 
     743           2 :         referrals = talloc_zero_array(r->out.resp,
     744             :                                       struct dfs_referral_type,
     745             :                                       r->out.resp->nb_referrals);
     746           2 :         if (referrals == NULL) {
     747           0 :                 return NT_STATUS_NO_MEMORY;
     748             :         }
     749           2 :         r->out.resp->referral_entries = referrals;
     750             : 
     751           2 :         c = 0;
     752           6 :         for(i=0; set[i]; i++) {
     753             :                 uint32_t j;
     754             : 
     755           6 :                 for(j=0; j< set[i]->count; j++) {
     756           2 :                         struct dfs_referral_type *ref = &referrals[c];
     757             :                         const char *referral_str;
     758             : 
     759           2 :                         referral_str = talloc_asprintf(referrals, "\\%s\\%s",
     760           2 :                                                        set[i]->names[j], dfs_name);
     761           2 :                         if (referral_str == NULL) {
     762           0 :                                 return NT_STATUS_NO_MEMORY;
     763             :                         }
     764             : 
     765           2 :                         DEBUG(8,("Doing a dfs referral for %s with this value "
     766             :                                  "%s requested %s\n",
     767             :                                  set[i]->names[j], referral_str,
     768             :                                  r->in.req.servername));
     769             : 
     770           2 :                         status = fill_normal_dfs_referraltype(referrals, ref,
     771           2 :                                         r->in.req.max_referral_level,
     772             :                                         r->in.req.servername,
     773             :                                         referral_str, c==0);
     774             : 
     775             : 
     776           2 :                         if (!NT_STATUS_IS_OK(status)) {
     777           0 :                                 DEBUG(2,("%s: Unable to fill domain referral "
     778             :                                          "structure - %s\n",
     779             :                                          __location__, nt_errstr(status)));
     780           0 :                                 return status;
     781             :                         }
     782             : 
     783           2 :                         c++;
     784             :                 }
     785             :         }
     786             : 
     787           2 :         return NT_STATUS_OK;
     788             : }
     789             : 
     790             : /*
     791             :   trans2 getdfsreferral implementation
     792             : */
     793         215 : NTSTATUS dfs_server_ad_get_referrals(struct loadparm_context *lp_ctx,
     794             :                                      struct ldb_context *sam_ctx,
     795             :                                      const struct tsocket_address *client,
     796             :                                      struct dfs_GetDFSReferral *r)
     797             : {
     798         215 :         char *server_name = NULL;
     799         215 :         char *dfs_name = NULL;
     800         215 :         char *link_path = NULL;
     801             :         const char *netbios_domain;
     802             :         const char *dns_domain;
     803             :         const char *netbios_name;
     804             :         const char *dns_name;
     805             :         const char **netbios_aliases;
     806             :         char path_separator;
     807             : 
     808         215 :         if (!lpcfg_host_msdfs(lp_ctx)) {
     809           0 :                 return NT_STATUS_FS_DRIVER_REQUIRED;
     810             :         }
     811             : 
     812         215 :         if (r->in.req.servername == NULL) {
     813           0 :                 return NT_STATUS_INVALID_PARAMETER;
     814             :         }
     815             : 
     816         215 :         DEBUG(8, ("Requested DFS name: %s length: %u\n",
     817             :                   r->in.req.servername,
     818             :                   (unsigned int)strlen_m(r->in.req.servername)*2));
     819             : 
     820             :         /*
     821             :          * If the servername is "" then we are in a case of domain dfs
     822             :          * and the client just searches for the list of local domain
     823             :          * it is attached and also trusted ones.
     824             :          */
     825         215 :         if (strlen(r->in.req.servername) == 0) {
     826           7 :                 return dodomain_referral(lp_ctx, sam_ctx, client, r);
     827             :         }
     828             : 
     829         208 :         server_name = talloc_strdup(r, r->in.req.servername);
     830         208 :         if (server_name == NULL) {
     831           0 :                 return NT_STATUS_NO_MEMORY;
     832             :         }
     833             : 
     834         208 :         path_separator = (*server_name == '/') ? '/' : '\\';
     835             : 
     836         412 :         while(*server_name && *server_name == path_separator) {
     837         204 :                 server_name++;
     838             :         }
     839             : 
     840         208 :         dfs_name = strchr_m(server_name, path_separator);
     841         208 :         if (dfs_name != NULL) {
     842         201 :                 dfs_name[0] = '\0';
     843         201 :                 dfs_name++;
     844             : 
     845         201 :                 link_path = strchr_m(dfs_name, path_separator);
     846         201 :                 if (link_path != NULL) {
     847           1 :                         link_path[0] = '\0';
     848           1 :                         link_path++;
     849             :                 }
     850             :         }
     851             : 
     852         208 :         if (link_path != NULL) {
     853             :                 /*
     854             :                  * If it is a DFS Link we do not
     855             :                  * handle it here.
     856             :                  */
     857           1 :                 return NT_STATUS_NOT_FOUND;
     858             :         }
     859             : 
     860         207 :         netbios_domain = lpcfg_workgroup(lp_ctx);
     861         207 :         dns_domain = lpcfg_dnsdomain(lp_ctx);
     862         207 :         netbios_name = lpcfg_netbios_name(lp_ctx);
     863         207 :         dns_name = talloc_asprintf(r, "%s.%s", netbios_name, dns_domain);
     864         207 :         if (dns_name == NULL) {
     865           0 :                 return NT_STATUS_NO_MEMORY;
     866             :         }
     867             : 
     868         319 :         if ((strcasecmp_m(server_name, netbios_name) == 0) ||
     869         112 :             (strcasecmp_m(server_name, dns_name) == 0)) {
     870             :                 /*
     871             :                  * If it is not domain related do not
     872             :                  * handle it here.
     873             :                  */
     874         144 :                 return NT_STATUS_NOT_FOUND;
     875             :         }
     876             : 
     877          63 :         if (is_ipaddress(server_name)) {
     878             :                 /*
     879             :                  * If it is not domain related do not
     880             :                  * handle it here.
     881             :                  */
     882          54 :                 return NT_STATUS_NOT_FOUND;
     883             :         }
     884             : 
     885           9 :         netbios_aliases = lpcfg_netbios_aliases(lp_ctx);
     886          18 :         while (netbios_aliases && *netbios_aliases) {
     887           9 :                 const char *netbios_alias = *netbios_aliases;
     888             :                 char *dns_alias;
     889             :                 int cmp;
     890             : 
     891           9 :                 cmp = strcasecmp_m(server_name, netbios_alias);
     892           9 :                 if (cmp == 0) {
     893             :                         /*
     894             :                          * If it is not domain related do not
     895             :                          * handle it here.
     896             :                          */
     897           0 :                         return NT_STATUS_NOT_FOUND;
     898             :                 }
     899             : 
     900           9 :                 dns_alias = talloc_asprintf(r, "%s.%s",
     901             :                                             netbios_alias,
     902             :                                             dns_domain);
     903           9 :                 if (dns_alias == NULL) {
     904           0 :                         return NT_STATUS_NO_MEMORY;
     905             :                 }
     906             : 
     907           9 :                 cmp = strcasecmp_m(server_name, dns_alias);
     908           9 :                 talloc_free(dns_alias);
     909           9 :                 if (cmp == 0) {
     910             :                         /*
     911             :                          * If it is not domain related do not
     912             :                          * handle it here.
     913             :                          */
     914           0 :                         return NT_STATUS_NOT_FOUND;
     915             :                 }
     916           9 :                 netbios_aliases++;
     917             :         }
     918             : 
     919          15 :         if ((strcasecmp_m(server_name, netbios_domain) != 0) &&
     920           6 :             (strcasecmp_m(server_name, dns_domain) != 0)) {
     921             :                 /*
     922             :                  * Not a domain we handle.
     923             :                  */
     924           1 :                 return NT_STATUS_INVALID_PARAMETER;
     925             :         }
     926             : 
     927             :         /*
     928             :          * Here we have filtered the thing the requested name don't contain our DNS name.
     929             :          * So if the share == NULL or if share in ("sysvol", "netlogon")
     930             :          * then we proceed. In the first case it will be a dc refereal in the second it will
     931             :          * be just a sysvol/netlogon referral.
     932             :          */
     933           8 :         if (dfs_name == NULL) {
     934           6 :                 return dodc_referral(lp_ctx, sam_ctx,
     935             :                                      client, r, server_name);
     936             :         }
     937             : 
     938             :         /*
     939             :          * Here we have filtered the thing the requested name don't contain our DNS name.
     940             :          * So if the share == NULL or if share in ("sysvol", "netlogon")
     941             :          * then we proceed. In the first case it will be a dc refereal in the second it will
     942             :          * be just a sysvol/netlogon referral.
     943             :          */
     944           2 :         if (strcasecmp(dfs_name, "sysvol") == 0 ||
     945           0 :             strcasecmp(dfs_name, "netlogon") == 0) {
     946           2 :                 return dosysvol_referral(lp_ctx, sam_ctx, client, r,
     947             :                                          server_name, dfs_name);
     948             :         }
     949             : 
     950             :         /* By default until all the case are handled */
     951           0 :         return NT_STATUS_NOT_FOUND;
     952             : }

Generated by: LCOV version 1.14