LCOV - code coverage report
Current view: top level - source4/libnet - libnet_site.c (source / functions) Hit Total Coverage
Test: coverage report for master 469b22b8 Lines: 85 159 53.5 %
Date: 2024-06-10 12:05:21 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Copyright (C) Brad Henry     2005
       5             : 
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "includes.h"
      21             : #include "libnet/libnet.h"
      22             : #include "libcli/cldap/cldap.h"
      23             : #include <ldb.h>
      24             : #include <ldb_errors.h>
      25             : #include "libcli/resolve/resolve.h"
      26             : #include "param/param.h"
      27             : #include "lib/tsocket/tsocket.h"
      28             : 
      29             : /**
      30             :  * 1. Setup a CLDAP socket.
      31             :  * 2. Lookup the default Site-Name.
      32             :  */
      33         239 : NTSTATUS libnet_FindSite(TALLOC_CTX *ctx, struct libnet_context *lctx, struct libnet_JoinSite *r)
      34             : {
      35          36 :         NTSTATUS status;
      36          36 :         TALLOC_CTX *tmp_ctx;
      37             : 
      38          36 :         char *site_name_str;
      39          36 :         char *config_dn_str;
      40          36 :         char *server_dn_str;
      41             : 
      42         239 :         struct cldap_socket *cldap = NULL;
      43          36 :         struct cldap_netlogon search;
      44          36 :         int ret;
      45          36 :         struct tsocket_address *dest_address;
      46             : 
      47         239 :         tmp_ctx = talloc_named(ctx, 0, "libnet_FindSite temp context");
      48         239 :         if (!tmp_ctx) {
      49           0 :                 r->out.error_string = NULL;
      50           0 :                 return NT_STATUS_NO_MEMORY;
      51             :         }
      52             : 
      53             :         /* Resolve the site name. */
      54         239 :         ZERO_STRUCT(search);
      55         239 :         search.in.dest_address = NULL;
      56         239 :         search.in.dest_port = 0;
      57         239 :         search.in.acct_control = -1;
      58         239 :         search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
      59         239 :         search.in.map_response = true;
      60             : 
      61         239 :         ret = tsocket_address_inet_from_strings(tmp_ctx, "ip",
      62             :                                                 r->in.dest_address,
      63             :                                                 r->in.cldap_port,
      64             :                                                 &dest_address);
      65         239 :         if (ret != 0) {
      66           0 :                 r->out.error_string = NULL;
      67           0 :                 status = map_nt_error_from_unix_common(errno);
      68           0 :                 talloc_free(tmp_ctx);
      69           0 :                 return status;
      70             :         }
      71             : 
      72             :         /* we want to use non async calls, so we're not passing an event context */
      73         239 :         status = cldap_socket_init(tmp_ctx, NULL, dest_address, &cldap);
      74         239 :         if (!NT_STATUS_IS_OK(status)) {
      75           0 :                 talloc_free(tmp_ctx);
      76           0 :                 r->out.error_string = NULL;
      77           0 :                 return status;
      78             :         }
      79         239 :         status = cldap_netlogon(cldap, tmp_ctx, &search);
      80         239 :         if (!NT_STATUS_IS_OK(status)
      81         239 :             || search.out.netlogon.data.nt5_ex.client_site == NULL
      82         239 :             || search.out.netlogon.data.nt5_ex.client_site[0] == '\0') {
      83             :                 /*
      84             :                   If cldap_netlogon() returns in error,
      85             :                   default to using Default-First-Site-Name.
      86             :                 */
      87           0 :                 site_name_str = talloc_asprintf(tmp_ctx, "%s",
      88             :                                                 "Default-First-Site-Name");
      89           0 :                 if (!site_name_str) {
      90           0 :                         r->out.error_string = NULL;
      91           0 :                         talloc_free(tmp_ctx);
      92           0 :                         return NT_STATUS_NO_MEMORY;
      93             :                 }
      94             :         } else {
      95         239 :                 site_name_str = talloc_asprintf(tmp_ctx, "%s",
      96             :                                         search.out.netlogon.data.nt5_ex.client_site);
      97         239 :                 if (!site_name_str) {
      98           0 :                         r->out.error_string = NULL;
      99           0 :                         talloc_free(tmp_ctx);
     100           0 :                         return NT_STATUS_NO_MEMORY;
     101             :                 }
     102             :         }
     103             : 
     104             :         /* Generate the CN=Configuration,... DN. */
     105             : /* TODO: look it up! */
     106         239 :         config_dn_str = talloc_asprintf(tmp_ctx, "CN=Configuration,%s", r->in.domain_dn_str);
     107         239 :         if (!config_dn_str) {
     108           0 :                 r->out.error_string = NULL;
     109           0 :                 talloc_free(tmp_ctx);
     110           0 :                 return NT_STATUS_NO_MEMORY;
     111             :         }
     112             : 
     113             :         /* Generate the CN=Servers,... DN. */
     114         239 :         server_dn_str = talloc_asprintf(tmp_ctx, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
     115             :                                                  r->in.netbios_name, site_name_str, config_dn_str);
     116         239 :         if (!server_dn_str) {
     117           0 :                 r->out.error_string = NULL;
     118           0 :                 talloc_free(tmp_ctx);
     119           0 :                 return NT_STATUS_NO_MEMORY;
     120             :         }
     121             : 
     122         239 :         r->out.site_name_str = site_name_str;
     123         239 :         talloc_steal(r, site_name_str);
     124             : 
     125         239 :         r->out.config_dn_str = config_dn_str;
     126         239 :         talloc_steal(r, config_dn_str);
     127             : 
     128         239 :         r->out.server_dn_str = server_dn_str;
     129         239 :         talloc_steal(r, server_dn_str);
     130             : 
     131         239 :         talloc_free(tmp_ctx);
     132         239 :         return NT_STATUS_OK;
     133             : }
     134             : 
     135             : /*
     136             :  * find out Site specific stuff:
     137             :  * 1. Lookup the Site name.
     138             :  * 2. Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
     139             :  * TODO: 3.) use DsAddEntry() to create CN=NTDS Settings,CN=<netbios name>,CN=Servers,CN=<site name>,...
     140             :  */
     141         239 : NTSTATUS libnet_JoinSite(struct libnet_context *ctx, 
     142             :                          struct ldb_context *remote_ldb,
     143             :                          struct libnet_JoinDomain *libnet_r)
     144             : {
     145          36 :         NTSTATUS status;
     146          36 :         TALLOC_CTX *tmp_ctx;
     147             : 
     148          36 :         struct libnet_JoinSite *r;
     149             : 
     150          36 :         struct ldb_dn *server_dn;
     151          36 :         struct ldb_message *msg;
     152          36 :         int rtn;
     153             : 
     154          36 :         const char *server_dn_str;
     155          36 :         const char *host;
     156          36 :         struct nbt_name name;
     157         239 :         const char *dest_addr = NULL;
     158             : 
     159         239 :         tmp_ctx = talloc_named(libnet_r, 0, "libnet_JoinSite temp context");
     160         239 :         if (!tmp_ctx) {
     161           0 :                 libnet_r->out.error_string = NULL;
     162           0 :                 return NT_STATUS_NO_MEMORY;
     163             :         }
     164             : 
     165         239 :         r = talloc(tmp_ctx, struct libnet_JoinSite);
     166         239 :         if (!r) {
     167           0 :                 libnet_r->out.error_string = NULL;
     168           0 :                 talloc_free(tmp_ctx);
     169           0 :                 return NT_STATUS_NO_MEMORY;
     170             :         }
     171             : 
     172         239 :         host = dcerpc_binding_get_string_option(libnet_r->out.samr_binding, "host");
     173         239 :         make_nbt_name_client(&name, host);
     174         239 :         status = resolve_name_ex(lpcfg_resolve_context(ctx->lp_ctx),
     175             :                                  0, 0,
     176             :                                  &name, r, &dest_addr, ctx->event_ctx);
     177         239 :         if (!NT_STATUS_IS_OK(status)) {
     178           0 :                 libnet_r->out.error_string = NULL;
     179           0 :                 talloc_free(tmp_ctx);
     180           0 :                 return status;
     181             :         }
     182             : 
     183             :         /* Resolve the site name and AD DN's. */
     184         239 :         r->in.dest_address = dest_addr;
     185         239 :         r->in.netbios_name = libnet_r->in.netbios_name;
     186         239 :         r->in.domain_dn_str = libnet_r->out.domain_dn_str;
     187         239 :         r->in.cldap_port = lpcfg_cldap_port(ctx->lp_ctx);
     188             : 
     189         239 :         status = libnet_FindSite(tmp_ctx, ctx, r);
     190         239 :         if (!NT_STATUS_IS_OK(status)) {
     191           0 :                 libnet_r->out.error_string =
     192           0 :                         talloc_steal(libnet_r, r->out.error_string);
     193           0 :                 talloc_free(tmp_ctx);
     194           0 :                 return status;
     195             :         }
     196             : 
     197         239 :         server_dn_str = r->out.server_dn_str;
     198             : 
     199             :         /*
     200             :          Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
     201             :         */
     202         239 :         msg = ldb_msg_new(tmp_ctx);
     203         239 :         if (!msg) {
     204           0 :                 libnet_r->out.error_string = NULL;
     205           0 :                 talloc_free(tmp_ctx);
     206           0 :                 return NT_STATUS_NO_MEMORY;
     207             :         }
     208             : 
     209         239 :         rtn = ldb_msg_add_string(msg, "objectClass", "server");
     210         239 :         if (rtn != LDB_SUCCESS) {
     211           0 :                 libnet_r->out.error_string = NULL;
     212           0 :                 talloc_free(tmp_ctx);
     213           0 :                 return NT_STATUS_NO_MEMORY;
     214             :         }
     215         239 :         rtn = ldb_msg_add_string(msg, "systemFlags", "50000000");
     216         239 :         if (rtn != LDB_SUCCESS) {
     217           0 :                 libnet_r->out.error_string = NULL;
     218           0 :                 talloc_free(tmp_ctx);
     219           0 :                 return NT_STATUS_NO_MEMORY;
     220             :         }
     221         239 :         rtn = ldb_msg_add_string(msg, "serverReference", libnet_r->out.account_dn_str);
     222         239 :         if (rtn != LDB_SUCCESS) {
     223           0 :                 libnet_r->out.error_string = NULL;
     224           0 :                 talloc_free(tmp_ctx);
     225           0 :                 return NT_STATUS_NO_MEMORY;
     226             :         }
     227             : 
     228         239 :         server_dn = ldb_dn_new(tmp_ctx, remote_ldb, server_dn_str);
     229         239 :         if ( ! ldb_dn_validate(server_dn)) {
     230           0 :                 libnet_r->out.error_string = talloc_asprintf(libnet_r,
     231             :                                         "Invalid server dn: %s",
     232             :                                         server_dn_str);
     233           0 :                 talloc_free(tmp_ctx);
     234           0 :                 return NT_STATUS_UNSUCCESSFUL;
     235             :         }
     236             : 
     237         239 :         msg->dn = server_dn;
     238             : 
     239         239 :         rtn = ldb_add(remote_ldb, msg);
     240         239 :         if (rtn == LDB_ERR_ENTRY_ALREADY_EXISTS) {
     241           0 :                 unsigned int i;
     242             : 
     243             :                 /* make a 'modify' msg, and only for serverReference */
     244           0 :                 msg = ldb_msg_new(tmp_ctx);
     245           0 :                 if (!msg) {
     246           0 :                         libnet_r->out.error_string = NULL;
     247           0 :                         talloc_free(tmp_ctx);
     248           0 :                         return NT_STATUS_NO_MEMORY;
     249             :                 }
     250           0 :                 msg->dn = server_dn;
     251             : 
     252           0 :                 rtn = ldb_msg_add_string(msg, "serverReference",libnet_r->out.account_dn_str);
     253           0 :                 if (rtn != LDB_SUCCESS) {
     254           0 :                         libnet_r->out.error_string = NULL;
     255           0 :                         talloc_free(tmp_ctx);
     256           0 :                         return NT_STATUS_NO_MEMORY;
     257             :                 }
     258             : 
     259             :                 /* mark all the message elements (should be just one)
     260             :                    as LDB_FLAG_MOD_REPLACE */
     261           0 :                 for (i=0;i<msg->num_elements;i++) {
     262           0 :                         msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
     263             :                 }
     264             : 
     265           0 :                 rtn = ldb_modify(remote_ldb, msg);
     266           0 :                 if (rtn != LDB_SUCCESS) {
     267           0 :                         libnet_r->out.error_string
     268           0 :                                 = talloc_asprintf(libnet_r,
     269             :                                                   "Failed to modify server entry %s: %s: %d",
     270             :                                                   server_dn_str,
     271             :                                                   ldb_errstring(remote_ldb), rtn);
     272           0 :                         talloc_free(tmp_ctx);
     273           0 :                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
     274             :                 }
     275         239 :         } else if (rtn != LDB_SUCCESS) {
     276           0 :                 libnet_r->out.error_string
     277           0 :                         = talloc_asprintf(libnet_r,
     278             :                                 "Failed to add server entry %s: %s: %d",
     279             :                                 server_dn_str, ldb_errstring(remote_ldb),
     280             :                                 rtn);
     281           0 :                 talloc_free(tmp_ctx);
     282           0 :                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
     283             :         }
     284         239 :         DEBUG(0, ("We still need to perform a DsAddEntry() so that we can create the CN=NTDS Settings container.\n"));
     285             : 
     286             :         /* Store the server DN in libnet_r */
     287         239 :         libnet_r->out.server_dn_str = server_dn_str;
     288         239 :         talloc_steal(libnet_r, server_dn_str);
     289             : 
     290         239 :         talloc_free(tmp_ctx);
     291         239 :         return NT_STATUS_OK;
     292             : }

Generated by: LCOV version 1.14