LCOV - code coverage report
Current view: top level - source4/dsdb/repl - drepl_out_pull.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 93 114 81.6 %
Date: 2024-02-28 12:06:22 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS Implementation.
       3             :    DSDB replication service outgoing Pull-Replication
       4             :    
       5             :    Copyright (C) Stefan Metzmacher 2007
       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             : 
      22             : #include "includes.h"
      23             : #include "dsdb/samdb/samdb.h"
      24             : #include "auth/auth.h"
      25             : #include "samba/service.h"
      26             : #include "lib/events/events.h"
      27             : #include "dsdb/repl/drepl_service.h"
      28             : #include <ldb_errors.h>
      29             : #include "../lib/util/dlinklist.h"
      30             : #include "librpc/gen_ndr/ndr_misc.h"
      31             : #include "librpc/gen_ndr/ndr_drsuapi.h"
      32             : #include "librpc/gen_ndr/ndr_drsblobs.h"
      33             : #include "libcli/composite/composite.h"
      34             : #include "libcli/security/security.h"
      35             : 
      36             : #undef DBGC_CLASS
      37             : #define DBGC_CLASS            DBGC_DRS_REPL
      38             : 
      39             : /*
      40             :   update repsFrom/repsTo error information
      41             :  */
      42        8795 : void drepl_reps_update(struct dreplsrv_service *s, const char *reps_attr,
      43             :                        struct ldb_dn *dn,
      44             :                        struct GUID *source_dsa_obj_guid, WERROR status)
      45             : {
      46           0 :         struct repsFromToBlob *reps;
      47           0 :         uint32_t count, i;
      48           0 :         WERROR werr;
      49        8795 :         TALLOC_CTX *tmp_ctx = talloc_new(s);
      50           0 :         time_t t;
      51           0 :         NTTIME now;
      52             : 
      53        8795 :         t = time(NULL);
      54        8795 :         unix_to_nt_time(&now, t);
      55             : 
      56        8795 :         werr = dsdb_loadreps(s->samdb, tmp_ctx, dn, reps_attr, &reps, &count);
      57        8795 :         if (!W_ERROR_IS_OK(werr)) {
      58           0 :                 talloc_free(tmp_ctx);
      59          32 :                 return;
      60             :         }
      61             : 
      62       16533 :         for (i=0; i<count; i++) {
      63       16501 :                 if (GUID_equal(source_dsa_obj_guid,
      64       16501 :                                &reps[i].ctr.ctr1.source_dsa_obj_guid)) {
      65        8763 :                         break;
      66             :                 }
      67             :         }
      68             : 
      69        8795 :         if (i == count) {
      70             :                 /* no record to update */
      71          32 :                 talloc_free(tmp_ctx);
      72          32 :                 return;
      73             :         }
      74             : 
      75             :         /* only update the status fields */
      76        8763 :         reps[i].ctr.ctr1.last_attempt = now;
      77        8763 :         reps[i].ctr.ctr1.result_last_attempt = status;
      78        8763 :         if (W_ERROR_IS_OK(status)) {
      79        2886 :                 reps[i].ctr.ctr1.last_success = now;
      80        2886 :                 reps[i].ctr.ctr1.consecutive_sync_failures = 0;
      81             :         } else {
      82        5877 :                 reps[i].ctr.ctr1.consecutive_sync_failures++;
      83             :         }
      84             : 
      85        8763 :         werr = dsdb_savereps(s->samdb, tmp_ctx, dn, reps_attr, reps, count);
      86        8763 :         if (!W_ERROR_IS_OK(werr)) {
      87           0 :                 DEBUG(2,("drepl_reps_update: Failed to save %s for %s: %s\n",
      88             :                          reps_attr, ldb_dn_get_linearized(dn), win_errstr(werr)));
      89             :         }
      90        8763 :         talloc_free(tmp_ctx);
      91             : }
      92             : 
      93        4119 : WERROR dreplsrv_schedule_partition_pull_source(struct dreplsrv_service *s,
      94             :                                                struct dreplsrv_partition_source_dsa *source,
      95             :                                                uint32_t options,
      96             :                                                enum drsuapi_DsExtendedOperation extended_op,
      97             :                                                uint64_t fsmo_info,
      98             :                                                dreplsrv_extended_callback_t callback,
      99             :                                                void *cb_data)
     100             : {
     101           0 :         struct dreplsrv_out_operation *op;
     102             : 
     103        4119 :         op = talloc_zero(s, struct dreplsrv_out_operation);
     104        4119 :         W_ERROR_HAVE_NO_MEMORY(op);
     105             : 
     106        4119 :         op->service  = s;
     107             :         /*
     108             :          * source may either be the long-term list of partners, or
     109             :          * from dreplsrv_partition_source_dsa_temporary().  Because it
     110             :          * can be either, we can't talloc_steal() it here, so we
     111             :          * instead we reference it.
     112             :          *
     113             :          * We never talloc_free() the p->sources pointers - indeed we
     114             :          * never remove them - and the temp source will otherwise go
     115             :          * away with the msg it is allocated on.
     116             :          *
     117             :          * Finally the pointer created in drepl_request_extended_op()
     118             :          * is removed with talloc_unlink().
     119             :          *
     120             :          */
     121        4119 :         op->source_dsa       = talloc_reference(op, source);
     122        4119 :         if (!op->source_dsa) {
     123           0 :                 return WERR_NOT_ENOUGH_MEMORY;
     124             :         }
     125             : 
     126        4119 :         op->options  = options;
     127        4119 :         op->extended_op = extended_op;
     128        4119 :         op->fsmo_info   = fsmo_info;
     129        4119 :         op->callback    = callback;
     130        4119 :         op->cb_data  = cb_data;
     131        4119 :         op->schedule_time = time(NULL);
     132        4119 :         op->more_flags       = 0;
     133             : 
     134        4119 :         DLIST_ADD_END(s->ops.pending, op);
     135             : 
     136        4119 :         return WERR_OK;
     137             : }
     138             : 
     139         989 : static WERROR dreplsrv_schedule_partition_pull(struct dreplsrv_service *s,
     140             :                                                struct dreplsrv_partition *p,
     141             :                                                TALLOC_CTX *mem_ctx)
     142             : {
     143          10 :         WERROR status;
     144          10 :         struct dreplsrv_partition_source_dsa *cur;
     145             : 
     146        1175 :         for (cur = p->sources; cur; cur = cur->next) {
     147         186 :                 status = dreplsrv_schedule_partition_pull_source(s, cur,
     148             :                                                                  0, DRSUAPI_EXOP_NONE, 0,
     149             :                                                                  NULL, NULL);
     150         186 :                 W_ERROR_NOT_OK_RETURN(status);
     151             :         }
     152             : 
     153         989 :         return WERR_OK;
     154             : }
     155             : 
     156         203 : WERROR dreplsrv_schedule_pull_replication(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
     157             : {
     158           2 :         WERROR status;
     159           2 :         struct dreplsrv_partition *p;
     160             : 
     161        1192 :         for (p = s->partitions; p; p = p->next) {
     162         989 :                 status = dreplsrv_schedule_partition_pull(s, p, mem_ctx);
     163         989 :                 W_ERROR_NOT_OK_RETURN(status);
     164             :         }
     165             : 
     166         203 :         return WERR_OK;
     167             : }
     168             : 
     169             : 
     170        3843 : static void dreplsrv_pending_op_callback(struct tevent_req *subreq)
     171             : {
     172        3843 :         struct dreplsrv_out_operation *op = tevent_req_callback_data(subreq,
     173             :                                             struct dreplsrv_out_operation);
     174        3843 :         struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
     175        3843 :         struct dreplsrv_service *s = op->service;
     176           0 :         WERROR werr;
     177             : 
     178        3843 :         werr = dreplsrv_op_pull_source_recv(subreq);
     179        3843 :         TALLOC_FREE(subreq);
     180             : 
     181        3843 :         DEBUG(4,("dreplsrv_op_pull_source(%s) for %s\n", win_errstr(werr),
     182             :                  ldb_dn_get_linearized(op->source_dsa->partition->dn)));
     183             : 
     184        3843 :         if (op->extended_op == DRSUAPI_EXOP_NONE) {
     185        1970 :                 drepl_reps_update(s, "repsFrom", op->source_dsa->partition->dn,
     186             :                                   &rf->source_dsa_obj_guid, werr);
     187             :         }
     188             : 
     189        3843 :         if (op->callback) {
     190        2836 :                 op->callback(s, werr, op->extended_ret, op->cb_data);
     191             :         }
     192        3843 :         talloc_free(op);
     193        3843 :         s->ops.current = NULL;
     194        3843 :         dreplsrv_run_pending_ops(s);
     195        3843 : }
     196             : 
     197        4543 : void dreplsrv_run_pull_ops(struct dreplsrv_service *s)
     198             : {
     199           0 :         struct dreplsrv_out_operation *op;
     200           0 :         time_t t;
     201           0 :         NTTIME now;
     202           0 :         struct tevent_req *subreq;
     203           0 :         WERROR werr;
     204             : 
     205        4543 :         if (s->ops.n_current || s->ops.current) {
     206             :                 /* if there's still one running, we're done */
     207        4267 :                 return;
     208             :         }
     209             : 
     210        4119 :         if (!s->ops.pending) {
     211             :                 /* if there're no pending operations, we're done */
     212           0 :                 return;
     213             :         }
     214             : 
     215        4119 :         t = time(NULL);
     216        4119 :         unix_to_nt_time(&now, t);
     217             : 
     218        4119 :         op = s->ops.pending;
     219        4119 :         s->ops.current = op;
     220        4119 :         DLIST_REMOVE(s->ops.pending, op);
     221             : 
     222        4119 :         op->source_dsa->repsFrom1->last_attempt = now;
     223             : 
     224             :         /* check if inbound replication is enabled */
     225        4119 :         if (!(op->options & DRSUAPI_DRS_SYNC_FORCED)) {
     226           0 :                 uint32_t rep_options;
     227        3202 :                 if (samdb_ntds_options(op->service->samdb, &rep_options) != LDB_SUCCESS) {
     228           0 :                         werr = WERR_DS_DRA_INTERNAL_ERROR;
     229           0 :                         goto failed;
     230             :                 }
     231             : 
     232        3202 :                 if ((rep_options & DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL)) {
     233         276 :                         werr = WERR_DS_DRA_SINK_DISABLED;
     234         276 :                         goto failed;
     235             :                 }
     236             :         }
     237             : 
     238        3843 :         subreq = dreplsrv_op_pull_source_send(op, s->task->event_ctx, op);
     239        3843 :         if (!subreq) {
     240           0 :                 werr = WERR_NOT_ENOUGH_MEMORY;
     241           0 :                 goto failed;
     242             :         }
     243             : 
     244        3843 :         tevent_req_set_callback(subreq, dreplsrv_pending_op_callback, op);
     245        3843 :         return;
     246             : 
     247         276 : failed:
     248         276 :         if (op->extended_op == DRSUAPI_EXOP_NONE) {
     249         276 :                 drepl_reps_update(s, "repsFrom", op->source_dsa->partition->dn,
     250         276 :                                   &op->source_dsa->repsFrom1->source_dsa_obj_guid, werr);
     251             :         }
     252             :         /* unblock queue processing */
     253         276 :         s->ops.current = NULL;
     254             :         /*
     255             :          * let the callback do its job just like in any other failure situation
     256             :          */
     257         276 :         if (op->callback) {
     258           2 :                 op->callback(s, werr, op->extended_ret, op->cb_data);
     259             :         }
     260             : }

Generated by: LCOV version 1.14