LCOV - code coverage report
Current view: top level - lib/ldb/tools - ldbadd.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 67 84 79.8 %
Date: 2024-02-28 12:06:22 Functions: 3 3 100.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: ldbadd
      28             :  *
      29             :  *  Description: utility to add records - modelled on ldapadd
      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             : #include "include/ldb_private.h"
      39             : 
      40             : static struct ldb_cmdline *options;
      41             : 
      42           1 : static void usage(struct ldb_context *ldb)
      43             : {
      44           1 :         printf("Usage: ldbadd <options> <ldif...>\n");
      45           1 :         printf("Adds records to a ldb, reading ldif the specified list of files\n\n");
      46           1 :         ldb_cmdline_help(ldb, "ldbadd", stdout);
      47           1 :         exit(LDB_ERR_OPERATIONS_ERROR);
      48             : }
      49             : 
      50             : 
      51             : /*
      52             :   add records from an opened file
      53             : */
      54         189 : static int process_file(struct ldb_context *ldb, FILE *f, unsigned int *count)
      55             : {
      56          29 :         struct ldb_ldif *ldif;
      57         189 :         int fun_ret = LDB_SUCCESS, ret;
      58         189 :         struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
      59         189 :         struct ldif_read_file_state state = {
      60             :                 .f = f
      61             :         };
      62             : 
      63         189 :         if (options->controls != NULL &&  req_ctrls== NULL) {
      64           0 :                 printf("parsing controls failed: %s\n", ldb_errstring(ldb));
      65           0 :                 return LDB_ERR_OPERATIONS_ERROR;
      66             :         }
      67             : 
      68         189 :         fun_ret = ldb_transaction_start(ldb);
      69         189 :         if (fun_ret != LDB_SUCCESS) {
      70           0 :                 fprintf(stderr, "ERR: (%s) on transaction start\n",
      71             :                         ldb_errstring(ldb));
      72           0 :                 return fun_ret;
      73             :         }
      74             : 
      75         450 :         while ((ldif = ldb_ldif_read_file_state(ldb, &state))) {
      76         273 :                 if (ldif->changetype != LDB_CHANGETYPE_ADD &&
      77         204 :                     ldif->changetype != LDB_CHANGETYPE_NONE) {
      78           4 :                         fprintf(stderr, "Only CHANGETYPE_ADD records allowed\n");
      79           2 :                         break;
      80             :                 }
      81             : 
      82         269 :                 ret = ldb_msg_normalize(ldb, ldif, ldif->msg, &ldif->msg);
      83         269 :                 if (ret != LDB_SUCCESS) {
      84           0 :                         fprintf(stderr,
      85             :                                 "ERR: Message canonicalize failed - %s\n",
      86             :                                 ldb_strerror(ret));
      87           0 :                         fun_ret = ret;
      88           0 :                         ldb_ldif_read_free(ldb, ldif);
      89           0 :                         continue;
      90             :                 }
      91             : 
      92         269 :                 ret = ldb_add_ctrl(ldb, ldif->msg,req_ctrls);
      93         269 :                 if (ret != LDB_SUCCESS) {
      94           8 :                         fprintf(stderr, "ERR: %s : \"%s\" on DN %s at block before line %llu\n",
      95             :                                 ldb_strerror(ret), ldb_errstring(ldb),
      96           8 :                                 ldb_dn_get_linearized(ldif->msg->dn),
      97           8 :                                 (unsigned long long)state.line_no);
      98           8 :                         fun_ret = ret;
      99             :                 } else {
     100         261 :                         (*count)++;
     101         261 :                         if (options->verbose) {
     102           0 :                                 printf("Added %s\n", ldb_dn_get_linearized(ldif->msg->dn));
     103             :                         }
     104             :                 }
     105         269 :                 ldb_ldif_read_free(ldb, ldif);
     106         269 :                 if (ret) {
     107           4 :                         break;
     108             :                 }
     109             :         }
     110             : 
     111         189 :         if (fun_ret == LDB_SUCCESS && !feof(f)) {
     112           4 :                 fprintf(stderr, "Failed to parse ldif\n");
     113           4 :                 fun_ret = LDB_ERR_OPERATIONS_ERROR;
     114             :         }
     115             : 
     116         189 :         if (fun_ret == LDB_SUCCESS) {
     117         177 :                 fun_ret = ldb_transaction_commit(ldb);
     118         177 :                 if (fun_ret != LDB_SUCCESS) {
     119           0 :                         fprintf(stderr, "ERR: (%s) on transaction commit\n",
     120             :                                 ldb_errstring(ldb));
     121             :                 }
     122             :         } else {
     123          12 :                 ldb_transaction_cancel(ldb);
     124             :         }
     125             : 
     126         160 :         return fun_ret;
     127             : }
     128             : 
     129             : 
     130             : 
     131         191 : int main(int argc, const char **argv)
     132             : {
     133          29 :         struct ldb_context *ldb;
     134         191 :         unsigned int i, count = 0;
     135         191 :         int ret = LDB_SUCCESS;
     136         191 :         TALLOC_CTX *mem_ctx = talloc_new(NULL);
     137             : 
     138         191 :         ldb = ldb_init(mem_ctx, NULL);
     139         191 :         if (ldb == NULL) {
     140           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     141             :         }
     142             : 
     143         191 :         options = ldb_cmdline_process(ldb, argc, argv, usage);
     144             : 
     145         189 :         ret = ldb_transaction_start(ldb);
     146         189 :         if (ret != LDB_SUCCESS) {
     147           0 :                 printf("Failed to start transaction: %s\n", ldb_errstring(ldb));
     148           0 :                 return ret;
     149             :         }
     150             : 
     151         189 :         if (options->argc == 0) {
     152         154 :                 ret = process_file(ldb, stdin, &count);
     153             :         } else {
     154          70 :                 for (i=0;i<options->argc;i++) {
     155          35 :                         const char *fname = options->argv[i];
     156          16 :                         FILE *f;
     157          35 :                         f = fopen(fname, "r");
     158          35 :                         if (!f) {
     159           0 :                                 perror(fname);
     160           0 :                                 return LDB_ERR_OPERATIONS_ERROR;
     161             :                         }
     162          35 :                         ret = process_file(ldb, f, &count);
     163          35 :                         fclose(f);
     164             :                 }
     165             :         }
     166             : 
     167         189 :         if (count != 0) {
     168         177 :                 ret = ldb_transaction_commit(ldb);
     169         177 :                 if (ret != LDB_SUCCESS) {
     170           0 :                         printf("Failed to commit transaction: %s\n", ldb_errstring(ldb));
     171           0 :                         return ret;
     172             :                 }
     173             :         } else {
     174          12 :                 ldb_transaction_cancel(ldb);
     175             :         }
     176             : 
     177         189 :         talloc_free(mem_ctx);
     178             : 
     179         189 :         if (ret) {
     180          12 :                 printf("Add failed after processing %u records\n", count);
     181             :         } else {
     182         177 :                 printf("Added %u records successfully\n", count);
     183             :         }
     184             : 
     185         160 :         return ret;
     186             : }

Generated by: LCOV version 1.14