LCOV - code coverage report
Current view: top level - lib/ldb/tools - ldbdel.c (source / functions) Hit Total Coverage
Test: coverage report for vadcx-master-patch-75612 fe003de8 Lines: 29 55 52.7 %
Date: 2024-02-29 22:57:05 Functions: 2 4 50.0 %

          Line data    Source code
       1             : /* 
       2             :    ldb database library
       3             : 
       4             :    Copyright (C) Andrew Tridgell  2004
       5             : 
       6             :      ** NOTE! The following LGPL license applies to the ldb
       7             :      ** library. This does NOT imply that all of Samba is released
       8             :      ** under the LGPL
       9             :    
      10             :    This library is free software; you can redistribute it and/or
      11             :    modify it under the terms of the GNU Lesser General Public
      12             :    License as published by the Free Software Foundation; either
      13             :    version 3 of the License, or (at your option) any later version.
      14             : 
      15             :    This library is distributed in the hope that it will be useful,
      16             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      18             :    Lesser General Public License for more details.
      19             : 
      20             :    You should have received a copy of the GNU Lesser General Public
      21             :    License along with this library; if not, see <http://www.gnu.org/licenses/>.
      22             : */
      23             : 
      24             : /*
      25             :  *  Name: ldb
      26             :  *
      27             :  *  Component: ldbdel
      28             :  *
      29             :  *  Description: utility to delete records - modelled on ldapdelete
      30             :  *
      31             :  *  Author: Andrew Tridgell
      32             :  */
      33             : 
      34             : #include "replace.h"
      35             : #include "ldb.h"
      36             : #include "tools/cmdline.h"
      37             : #include "ldbutil.h"
      38             : 
      39           0 : static int dn_cmp(struct ldb_message **msg1, struct ldb_message **msg2)
      40             : {
      41           0 :         return ldb_dn_compare((*msg1)->dn, (*msg2)->dn);
      42             : }
      43             : 
      44           0 : static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn,struct ldb_control **req_ctrls)
      45             : {
      46           0 :         int ret;
      47           0 :         unsigned int i, total=0;
      48           0 :         const char *attrs[] = { NULL };
      49           0 :         struct ldb_result *res;
      50             :         
      51           0 :         ret = ldb_search(ldb, ldb, &res, dn, LDB_SCOPE_SUBTREE, attrs, "distinguishedName=*");
      52           0 :         if (ret != LDB_SUCCESS) return ret;
      53             : 
      54             :         /* sort the DNs, deepest first */
      55           0 :         TYPESAFE_QSORT(res->msgs, res->count, dn_cmp);
      56             : 
      57           0 :         for (i = 0; i < res->count; i++) {
      58           0 :                 if (ldb_delete_ctrl(ldb, res->msgs[i]->dn,req_ctrls) == LDB_SUCCESS) {
      59           0 :                         total++;
      60             :                 } else {
      61           0 :                         printf("Failed to delete '%s' - %s\n",
      62           0 :                                ldb_dn_get_linearized(res->msgs[i]->dn),
      63             :                                ldb_errstring(ldb));
      64             :                 }
      65             :         }
      66             : 
      67           0 :         talloc_free(res);
      68             : 
      69           0 :         if (total == 0) {
      70           0 :                 return LDB_ERR_OPERATIONS_ERROR;
      71             :         }
      72           0 :         printf("Deleted %u records\n", total);
      73           0 :         return LDB_SUCCESS;
      74             : }
      75             : 
      76           1 : static void usage(struct ldb_context *ldb)
      77             : {
      78           1 :         printf("Usage: ldbdel <options> <DN...>\n");
      79           1 :         printf("Deletes records from a ldb\n\n");
      80           1 :         ldb_cmdline_help(ldb, "ldbdel", stdout);
      81           1 :         exit(LDB_ERR_OPERATIONS_ERROR);
      82             : }
      83             : 
      84          21 : int main(int argc, const char **argv)
      85             : {
      86           3 :         struct ldb_control **req_ctrls;
      87           3 :         struct ldb_cmdline *options;
      88           3 :         struct ldb_context *ldb;
      89          21 :         int ret = 0, i;
      90          21 :         TALLOC_CTX *mem_ctx = talloc_new(NULL);
      91             : 
      92          21 :         ldb = ldb_init(mem_ctx, NULL);
      93          21 :         if (ldb == NULL) {
      94           0 :                 return LDB_ERR_OPERATIONS_ERROR;
      95             :         }
      96             : 
      97          21 :         options = ldb_cmdline_process(ldb, argc, argv, usage);
      98             : 
      99          19 :         if (options->argc < 1) {
     100           0 :                 usage(ldb);
     101             :         }
     102             : 
     103          19 :         req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
     104          19 :         if (options->controls != NULL &&  req_ctrls== NULL) {
     105           0 :                 printf("parsing controls failed: %s\n", ldb_errstring(ldb));
     106           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     107             :         }
     108             : 
     109          38 :         for (i=0;i<options->argc;i++) {
     110           3 :                 struct ldb_dn *dn;
     111             : 
     112          19 :                 dn = ldb_dn_new(ldb, ldb, options->argv[i]);
     113          19 :                 if (dn == NULL) {
     114           0 :                         return LDB_ERR_OPERATIONS_ERROR;
     115             :                 }
     116          19 :                 if (options->recursive) {
     117           0 :                         ret = ldb_delete_recursive(ldb, dn,req_ctrls);
     118             :                 } else {
     119          19 :                         ret = ldb_delete_ctrl(ldb, dn,req_ctrls);
     120          19 :                         if (ret == LDB_SUCCESS) {
     121          17 :                                 printf("Deleted 1 record\n");
     122             :                         }
     123             :                 }
     124          19 :                 if (ret != LDB_SUCCESS) {
     125           5 :                         printf("delete of '%s' failed - (%s) %s\n",
     126             :                                ldb_dn_get_linearized(dn),
     127             :                                ldb_strerror(ret),
     128             :                                ldb_errstring(ldb));
     129             :                 }
     130             :         }
     131             : 
     132          19 :         talloc_free(mem_ctx);
     133             : 
     134          19 :         return ret;
     135             : }

Generated by: LCOV version 1.14