Ignore:
Timestamp:
Nov 27, 2012, 4:43:17 PM (13 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: updated trunk to 3.6.0

Location:
trunk/server
Files:
1 deleted
10 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/server

  • trunk/server/source4/lib/ldb/tools/cmdline.c

    r414 r745  
    2222*/
    2323
    24 #include "ldb_includes.h"
     24#include "replace.h"
     25#include "system/filesys.h"
     26#include "system/time.h"
    2527#include "ldb.h"
     28#include "ldb_module.h"
    2629#include "tools/cmdline.h"
    2730
    28 #if (_SAMBA_BUILD_ >= 4)
    29 #include "includes.h"
    30 #include "lib/cmdline/popt_common.h"
    31 #include "lib/ldb-samba/ldif_handlers.h"
    32 #include "auth/gensec/gensec.h"
    33 #include "auth/auth.h"
    34 #include "ldb_wrap.h"
    35 #include "param/param.h"
    36 #endif
    37 
    3831static struct ldb_cmdline options; /* needs to be static for older compilers */
    3932
    40 static struct poptOption popt_options[] = {
     33enum ldb_cmdline_options { CMDLINE_RELAX=1 };
     34
     35static struct poptOption builtin_popt_options[] = {
    4136        POPT_AUTOHELP
    4237        { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
     
    5449        { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
    5550        { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
    56         { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
    57         { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
    5851        { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
    5952        { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
    6053        { "show-binary", 0, POPT_ARG_NONE, &options.show_binary, 0, "display binary LDIF", NULL },
    61 #if (_SAMBA_BUILD_ >= 4)
    62         POPT_COMMON_SAMBA
    63         POPT_COMMON_CREDENTIALS
    64         POPT_COMMON_CONNECTION
    65         POPT_COMMON_VERSION
    66 #endif
     54        { "paged", 0, POPT_ARG_NONE, NULL, 'P', "use a paged search", NULL },
     55        { "show-deleted", 0, POPT_ARG_NONE, NULL, 'D', "show deleted objects", NULL },
     56        { "show-recycled", 0, POPT_ARG_NONE, NULL, 'R', "show recycled objects", NULL },
     57        { "show-deactivated-link", 0, POPT_ARG_NONE, NULL, 'd', "show deactivated links", NULL },
     58        { "reveal", 0, POPT_ARG_NONE, NULL, 'r', "reveal ldb internals", NULL },
     59        { "relax", 0, POPT_ARG_NONE, NULL, CMDLINE_RELAX, "pass relax control", NULL },
     60        { "cross-ncs", 0, POPT_ARG_NONE, NULL, 'N', "search across NC boundaries", NULL },
     61        { "extended-dn", 0, POPT_ARG_NONE, NULL, 'E', "show extended DNs", NULL },
    6762        { NULL }
    6863};
    6964
    70 void ldb_cmdline_help(const char *cmdname, FILE *f)
     65void ldb_cmdline_help(struct ldb_context *ldb, const char *cmdname, FILE *f)
    7166{
    7267        poptContext pc;
    73         pc = poptGetContext(cmdname, 0, NULL, popt_options,
     68        struct poptOption **popt_options = ldb_module_popt_options(ldb);
     69        pc = poptGetContext(cmdname, 0, NULL, *popt_options,
    7470                            POPT_CONTEXT_KEEP_FIRST);
    7571        poptPrintHelp(pc, f, 0);
     72}
     73
     74/*
     75  add a control to the options structure
     76 */
     77static bool add_control(TALLOC_CTX *mem_ctx, const char *control)
     78{
     79        unsigned int i;
     80
     81        /* count how many controls we already have */
     82        for (i=0; options.controls && options.controls[i]; i++) ;
     83
     84        options.controls = talloc_realloc(mem_ctx, options.controls, const char *, i + 2);
     85        if (options.controls == NULL) {
     86                return false;
     87        }
     88        options.controls[i] = control;
     89        options.controls[i+1] = NULL;
     90        return true;
    7691}
    7792
     
    8196struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb,
    8297                                        int argc, const char **argv,
    83                                         void (*usage)(void))
     98                                        void (*usage)(struct ldb_context *))
    8499{
    85100        struct ldb_cmdline *ret=NULL;
    86101        poptContext pc;
    87 #if (_SAMBA_BUILD_ >= 4)
    88         int r;
    89 #endif
    90102        int num_options = 0;
    91103        int opt;
    92104        int flags = 0;
    93 
    94 #if (_SAMBA_BUILD_ >= 4)
    95         r = ldb_register_samba_handlers(ldb);
    96         if (r != 0) {
    97                 goto failed;
    98         }
    99 
    100 #endif
     105        int rc;
     106        struct poptOption **popt_options;
     107
     108        /* make the ldb utilities line buffered */
     109        setlinebuf(stdout);
    101110
    102111        ret = talloc_zero(ldb, struct ldb_cmdline);
     
    122131        options.scope = LDB_SCOPE_DEFAULT;
    123132
    124         pc = poptGetContext(argv[0], argc, argv, popt_options,
     133        popt_options = ldb_module_popt_options(ldb);
     134        (*popt_options) = builtin_popt_options;
     135
     136        rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_OPTIONS);
     137        if (rc != LDB_SUCCESS) {
     138                fprintf(stderr, "ldb: failed to run command line hooks : %s\n", ldb_strerror(rc));
     139                goto failed;
     140        }
     141
     142        pc = poptGetContext(argv[0], argc, argv, *popt_options,
    125143                            POPT_CONTEXT_KEEP_FIRST);
    126144
     
    160178                case 'c': {
    161179                        const char *cs = poptGetOptArg(pc);
    162                         const char *p, *q;
    163                         int cc;
    164 
    165                         for (p = cs, cc = 1; (q = strchr(p, ',')); cc++, p = q + 1) ;
    166 
    167                         options.controls = talloc_array(ret, char *, cc + 1);
    168                         if (options.controls == NULL) {
    169                                 fprintf(stderr, "Out of memory!\n");
    170                                 goto failed;
    171                         }
    172                         for (p = cs, cc = 0; p != NULL; cc++) {
    173                                 const char *t;
     180                        const char *p;
     181
     182                        for (p = cs; p != NULL; ) {
     183                                const char *t, *c;
    174184
    175185                                t = strchr(p, ',');
    176186                                if (t == NULL) {
    177                                         options.controls[cc] = talloc_strdup(options.controls, p);
     187                                        c = talloc_strdup(options.controls, p);
    178188                                        p = NULL;
    179189                                } else {
    180                                         options.controls[cc] = talloc_strndup(options.controls, p, t-p);
     190                                        c = talloc_strndup(options.controls, p, t-p);
    181191                                        p = t + 1;
    182192                                }
    183                         }
    184                         options.controls[cc] = NULL;
     193                                if (c == NULL || !add_control(ret, c)) {
     194                                        fprintf(stderr, __location__ ": out of memory\n");
     195                                        goto failed;
     196                                }
     197                        }
    185198
    186199                        break;   
    187200                }
     201                case 'P':
     202                        if (!add_control(ret, "paged_results:1:1024")) {
     203                                fprintf(stderr, __location__ ": out of memory\n");
     204                                goto failed;
     205                        }
     206                        break;
     207                case 'D':
     208                        if (!add_control(ret, "show_deleted:1")) {
     209                                fprintf(stderr, __location__ ": out of memory\n");
     210                                goto failed;
     211                        }
     212                        break;
     213                case 'R':
     214                        if (!add_control(ret, "show_recycled:0")) {
     215                                fprintf(stderr, __location__ ": out of memory\n");
     216                                goto failed;
     217                        }
     218                        break;
     219                case 'd':
     220                        if (!add_control(ret, "show_deactivated_link:0")) {
     221                                fprintf(stderr, __location__ ": out of memory\n");
     222                                goto failed;
     223                        }
     224                        break;
     225                case 'r':
     226                        if (!add_control(ret, "reveal_internals:0")) {
     227                                fprintf(stderr, __location__ ": out of memory\n");
     228                                goto failed;
     229                        }
     230                        break;
     231                case CMDLINE_RELAX:
     232                        if (!add_control(ret, "relax:0")) {
     233                                fprintf(stderr, __location__ ": out of memory\n");
     234                                goto failed;
     235                        }
     236                        break;
     237                case 'N':
     238                        if (!add_control(ret, "search_options:1:2")) {
     239                                fprintf(stderr, __location__ ": out of memory\n");
     240                                goto failed;
     241                        }
     242                        break;
     243                case 'E':
     244                        if (!add_control(ret, "extended_dn:1:1")) {
     245                                fprintf(stderr, __location__ ": out of memory\n");
     246                                goto failed;
     247                        }
     248                        break;
    188249                default:
    189250                        fprintf(stderr, "Invalid option %s: %s\n",
    190251                                poptBadOption(pc, 0), poptStrerror(opt));
    191                         if (usage) usage();
     252                        if (usage) usage(ldb);
    192253                        goto failed;
    193254                }
     
    206267        if (ret->url == NULL) {
    207268                fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
    208                 if (usage) usage();
     269                if (usage) usage(ldb);
    209270                goto failed;
    210271        }
     
    226287        }
    227288
    228 #if (_SAMBA_BUILD_ >= 4)
    229         /* Must be after we have processed command line options */
    230         gensec_init(cmdline_lp_ctx);
    231        
    232         if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb, cmdline_lp_ctx))) {
    233                 goto failed;
    234         }
    235         if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
    236                 goto failed;
    237         }
    238         if (ldb_set_opaque(ldb, "loadparm", cmdline_lp_ctx)) {
    239                 goto failed;
    240         }
    241 
    242         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
    243 #endif
    244 
    245289        if (options.modules_path != NULL) {
    246290                ldb_set_modules_dir(ldb, options.modules_path);
    247         } else if (getenv("LDB_MODULES_PATH") != NULL) {
    248                 ldb_set_modules_dir(ldb, getenv("LDB_MODULES_PATH"));
     291        }
     292
     293        rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_PRECONNECT);
     294        if (rc != LDB_SUCCESS) {
     295                fprintf(stderr, "ldb: failed to run preconnect hooks : %s\n", ldb_strerror(rc));
     296                goto failed;
    249297        }
    250298
     
    256304        }
    257305
     306        rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_POSTCONNECT);
     307        if (rc != LDB_SUCCESS) {
     308                fprintf(stderr, "ldb: failed to run post connect hooks : %s\n", ldb_strerror(rc));
     309                goto failed;
     310        }
     311
    258312        return ret;
    259313
    260314failed:
    261315        talloc_free(ret);
    262         exit(1);
     316        exit(LDB_ERR_OPERATIONS_ERROR);
    263317        return NULL;
    264318}
     
    274328int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
    275329{
    276         int i, j;
    277         int ret = 0;
     330        unsigned int i, j;
     331        int ret = 0;
    278332
    279333        if (reply == NULL || request == NULL) return -1;
  • trunk/server/source4/lib/ldb/tools/cmdline.h

    r414 r745  
    4242        int num_searches;
    4343        const char *sasl_mechanism;
    44         const char *input;
    45         const char *output;
    46         char **controls;
     44        const char **controls;
    4745        int show_binary;
    4846        int tracing;
    4947};
    5048
    51 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const char **argv,
    52                                         void (*usage)(void));
     49struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc,
     50                                        const char **argv,
     51                                        void (*usage)(struct ldb_context *));
    5352
    5453
    5554int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request);
    56 void ldb_cmdline_help(const char *cmdname, FILE *f);
     55void ldb_cmdline_help(struct ldb_context *ldb, const char *cmdname, FILE *f);
    5756
  • trunk/server/source4/lib/ldb/tools/ldbadd.c

    r414 r745  
    1 /* 
     1/*
    22   ldb database library
    33
     
    77     ** library. This does NOT imply that all of Samba is released
    88     ** under the LGPL
    9    
     9
    1010   This library is free software; you can redistribute it and/or
    1111   modify it under the terms of the GNU Lesser General Public
     
    3434#include "ldb.h"
    3535#include "tools/cmdline.h"
     36#include "ldbutil.h"
    3637
    37 static int failures;
     38static unsigned int failures;
     39static struct ldb_cmdline *options;
    3840
    39 static void usage(void)
     41static void usage(struct ldb_context *ldb)
    4042{
    41         printf("Usage: ldbadd <options> <ldif...>\n"); 
     43        printf("Usage: ldbadd <options> <ldif...>\n");
    4244        printf("Adds records to a ldb, reading ldif the specified list of files\n\n");
    43         ldb_cmdline_help("ldbadd", stdout);
    44         exit(1);
     45        ldb_cmdline_help(ldb, "ldbadd", stdout);
     46        exit(LDB_ERR_OPERATIONS_ERROR);
    4547}
    4648
     
    4951  add records from an opened file
    5052*/
    51 static int process_file(struct ldb_context *ldb, FILE *f, int *count)
     53static int process_file(struct ldb_context *ldb, FILE *f, unsigned int *count)
    5254{
    5355        struct ldb_ldif *ldif;
    5456        int ret = LDB_SUCCESS;
     57        struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
     58        if (options->controls != NULL &&  req_ctrls== NULL) {
     59                printf("parsing controls failed: %s\n", ldb_errstring(ldb));
     60                return LDB_ERR_OPERATIONS_ERROR;
     61        }
     62
    5563
    5664        while ((ldif = ldb_ldif_read_file(ldb, f))) {
     
    6169                }
    6270
    63                 ldif->msg = ldb_msg_canonicalize(ldb, ldif->msg);
     71                ret = ldb_msg_normalize(ldb, ldif, ldif->msg, &ldif->msg);
     72                if (ret != LDB_SUCCESS) {
     73                        fprintf(stderr,
     74                                "ERR: Message canonicalize failed - %s\n",
     75                                ldb_strerror(ret));
     76                        failures++;
     77                        ldb_ldif_read_free(ldb, ldif);
     78                        continue;
     79                }
    6480
    65                 ret = ldb_add(ldb, ldif->msg);
     81                ret = ldb_add_ctrl(ldb, ldif->msg,req_ctrls);
    6682                if (ret != LDB_SUCCESS) {
    67                         fprintf(stderr, "ERR: \"%s\" on DN %s\n",
    68                                 ldb_errstring(ldb), ldb_dn_get_linearized(ldif->msg->dn));
     83                        fprintf(stderr, "ERR: %s : \"%s\" on DN %s\n",
     84                                ldb_strerror(ret), ldb_errstring(ldb),
     85                                ldb_dn_get_linearized(ldif->msg->dn));
    6986                        failures++;
    7087                } else {
    7188                        (*count)++;
     89                        if (options->verbose) {
     90                                printf("Added %s\n", ldb_dn_get_linearized(ldif->msg->dn));
     91                        }
    7292                }
    7393                ldb_ldif_read_free(ldb, ldif);
     
    82102{
    83103        struct ldb_context *ldb;
    84         int i, ret=0, count=0;
    85         struct ldb_cmdline *options;
     104        unsigned int i, count = 0;
     105        int ret = LDB_SUCCESS;
     106        TALLOC_CTX *mem_ctx = talloc_new(NULL);
    86107
    87         ldb = ldb_init(NULL, NULL);
     108        ldb = ldb_init(mem_ctx, NULL);
     109        if (ldb == NULL) {
     110                return LDB_ERR_OPERATIONS_ERROR;
     111        }
    88112
    89113        options = ldb_cmdline_process(ldb, argc, argv, usage);
    90114
    91         if (ldb_transaction_start(ldb) != 0) {
     115        ret = ldb_transaction_start(ldb);
     116        if (ret != LDB_SUCCESS) {
    92117                printf("Failed to start transaction: %s\n", ldb_errstring(ldb));
    93                 exit(1);
     118                return ret;
    94119        }
    95120
     
    103128                        if (!f) {
    104129                                perror(fname);
    105                                 exit(1);
     130                                return LDB_ERR_OPERATIONS_ERROR;
    106131                        }
    107132                        ret = process_file(ldb, f, &count);
     
    111136
    112137        if (count != 0) {
    113                 if (ldb_transaction_commit(ldb) != 0) {
     138                ret = ldb_transaction_commit(ldb);
     139                if (ret != LDB_SUCCESS) {
    114140                        printf("Failed to commit transaction: %s\n", ldb_errstring(ldb));
    115                         exit(1);
     141                        return ret;
    116142                }
    117143        } else {
     
    119145        }
    120146
    121         talloc_free(ldb);
     147        talloc_free(mem_ctx);
    122148
    123         printf("Added %d records with %d failures\n", count, failures);
    124        
     149        printf("Added %u records with %u failures\n", count, failures);
     150
    125151        return ret;
    126152}
  • trunk/server/source4/lib/ldb/tools/ldbdel.c

    r414 r745  
    3232 */
    3333
     34#include "replace.h"
    3435#include "ldb.h"
    3536#include "tools/cmdline.h"
     37#include "ldbutil.h"
    3638
    37 static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn)
     39static int dn_cmp(struct ldb_message **msg1, struct ldb_message **msg2)
    3840{
    39         int ret, i, total=0;
     41        return ldb_dn_compare((*msg1)->dn, (*msg2)->dn);
     42}
     43
     44static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn,struct ldb_control **req_ctrls)
     45{
     46        int ret;
     47        unsigned int i, total=0;
    4048        const char *attrs[] = { NULL };
    4149        struct ldb_result *res;
    4250       
    4351        ret = ldb_search(ldb, ldb, &res, dn, LDB_SCOPE_SUBTREE, attrs, "distinguishedName=*");
    44         if (ret != LDB_SUCCESS) return -1;
     52        if (ret != LDB_SUCCESS) return ret;
     53
     54        /* sort the DNs, deepest first */
     55        TYPESAFE_QSORT(res->msgs, res->count, dn_cmp);
    4556
    4657        for (i = 0; i < res->count; i++) {
    47                 if (ldb_delete(ldb, res->msgs[i]->dn) == 0) {
     58                if (ldb_delete_ctrl(ldb, res->msgs[i]->dn,req_ctrls) == LDB_SUCCESS) {
    4859                        total++;
     60                } else {
     61                        printf("Failed to delete '%s' - %s\n",
     62                               ldb_dn_get_linearized(res->msgs[i]->dn),
     63                               ldb_errstring(ldb));
    4964                }
    5065        }
     
    5368
    5469        if (total == 0) {
    55                 return -1;
     70                return LDB_ERR_OPERATIONS_ERROR;
    5671        }
    57         printf("Deleted %d records\n", total);
    58         return 0;
     72        printf("Deleted %u records\n", total);
     73        return LDB_SUCCESS;
    5974}
    6075
    61 static void usage(void)
     76static void usage(struct ldb_context *ldb)
    6277{
    6378        printf("Usage: ldbdel <options> <DN...>\n");
    6479        printf("Deletes records from a ldb\n\n");
    65         ldb_cmdline_help("ldbdel", stdout);
    66         exit(1);
     80        ldb_cmdline_help(ldb, "ldbdel", stdout);
     81        exit(LDB_ERR_OPERATIONS_ERROR);
    6782}
    6883
    6984int main(int argc, const char **argv)
    7085{
     86        struct ldb_control **req_ctrls;
     87        struct ldb_cmdline *options;
    7188        struct ldb_context *ldb;
    7289        int ret = 0, i;
    73         struct ldb_cmdline *options;
     90        TALLOC_CTX *mem_ctx = talloc_new(NULL);
    7491
    75         ldb = ldb_init(NULL, NULL);
     92        ldb = ldb_init(mem_ctx, NULL);
     93        if (ldb == NULL) {
     94                return LDB_ERR_OPERATIONS_ERROR;
     95        }
    7696
    7797        options = ldb_cmdline_process(ldb, argc, argv, usage);
    7898
    7999        if (options->argc < 1) {
    80                 usage();
    81                 exit(1);
     100                usage(ldb);
     101        }
     102
     103        req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
     104        if (options->controls != NULL &&  req_ctrls== NULL) {
     105                printf("parsing controls failed: %s\n", ldb_errstring(ldb));
     106                return LDB_ERR_OPERATIONS_ERROR;
    82107        }
    83108
     
    86111
    87112                dn = ldb_dn_new(ldb, ldb, options->argv[i]);
    88                 if ( ! ldb_dn_validate(dn)) {
    89                         printf("Invalid DN format\n");
    90                         exit(1);
     113                if (dn == NULL) {
     114                        return LDB_ERR_OPERATIONS_ERROR;
    91115                }
    92116                if (options->recursive) {
    93                         ret = ldb_delete_recursive(ldb, dn);
     117                        ret = ldb_delete_recursive(ldb, dn,req_ctrls);
    94118                } else {
    95                         ret = ldb_delete(ldb, dn);
    96                         if (ret == 0) {
     119                        ret = ldb_delete_ctrl(ldb, dn,req_ctrls);
     120                        if (ret == LDB_SUCCESS) {
    97121                                printf("Deleted 1 record\n");
    98122                        }
    99123                }
    100                 if (ret != 0) {
    101                         printf("delete of '%s' failed - %s\n",
    102                                 ldb_dn_get_linearized(dn),
    103                                 ldb_errstring(ldb));
     124                if (ret != LDB_SUCCESS) {
     125                        printf("delete of '%s' failed - (%s) %s\n",
     126                               ldb_dn_get_linearized(dn),
     127                               ldb_strerror(ret),
     128                               ldb_errstring(ldb));
    104129                }
    105130        }
    106131
    107         talloc_free(ldb);
     132        talloc_free(mem_ctx);
    108133
    109134        return ret;
  • trunk/server/source4/lib/ldb/tools/ldbedit.c

    r414 r745  
    1 /* 
     1/*
    22   ldb database library
    33
     
    77     ** library. This does NOT imply that all of Samba is released
    88     ** under the LGPL
    9    
     9
    1010   This library is free software; you can redistribute it and/or
    1111   modify it under the terms of the GNU Lesser General Public
     
    3131 *  Author: Andrew Tridgell
    3232 */
    33 #include "ldb_includes.h"
     33
     34#include "replace.h"
     35#include "system/filesys.h"
     36#include "system/time.h"
     37#include "system/filesys.h"
    3438#include "ldb.h"
    3539#include "tools/cmdline.h"
     40#include "tools/ldbutil.h"
    3641
    3742static struct ldb_cmdline *options;
    3843
    3944/*
    40   debug routine 
    41 */
    42 static void ldif_write_msg(struct ldb_context *ldb, 
    43                            FILE *f, 
     45  debug routine
     46*/
     47static void ldif_write_msg(struct ldb_context *ldb,
     48                           FILE *f,
    4449                           enum ldb_changetype changetype,
    4550                           struct ldb_message *msg)
     
    5560  returns the number of modified elements
    5661*/
    57 static int modify_record(struct ldb_context *ldb, 
     62static int modify_record(struct ldb_context *ldb,
    5863                         struct ldb_message *msg1,
    59                          struct ldb_message *msg2)
    60 {
     64                         struct ldb_message *msg2,
     65                         struct ldb_control **req_ctrls)
     66{
     67        int ret;
    6168        struct ldb_message *mod;
    6269
    63         mod = ldb_msg_diff(ldb, msg1, msg2);
    64         if (mod == NULL) {
     70        if (ldb_msg_difference(ldb, ldb, msg1, msg2, &mod) != LDB_SUCCESS) {
    6571                fprintf(stderr, "Failed to calculate message differences\n");
    6672                return -1;
    6773        }
    6874
    69         if (mod->num_elements == 0) {
    70                 return 0;
     75        ret = mod->num_elements;
     76        if (ret == 0) {
     77                goto done;
    7178        }
    7279
     
    7582        }
    7683
    77         if (ldb_modify(ldb, mod) != 0) {
    78                 fprintf(stderr, "failed to modify %s - %s\n", 
     84        if (ldb_modify_ctrl(ldb, mod, req_ctrls) != LDB_SUCCESS) {
     85                fprintf(stderr, "failed to modify %s - %s\n",
    7986                        ldb_dn_get_linearized(msg1->dn), ldb_errstring(ldb));
    80                 return -1;
    81         }
    82 
    83         return mod->num_elements;
     87                ret = -1;
     88                goto done;
     89        }
     90
     91done:
     92        talloc_free(mod);
     93        return ret;
    8494}
    8595
     
    8999static struct ldb_message *msg_find(struct ldb_context *ldb,
    90100                                    struct ldb_message **msgs,
    91                                     int count,
     101                                    unsigned int count,
    92102                                    struct ldb_dn *dn)
    93103{
    94         int i;
     104        unsigned int i;
    95105        for (i=0;i<count;i++) {
    96106                if (ldb_dn_compare(dn, msgs[i]->dn) == 0) {
     
    105115*/
    106116static int merge_edits(struct ldb_context *ldb,
    107                        struct ldb_message **msgs1, int count1,
    108                        struct ldb_message **msgs2, int count2)
    109 {
    110         int i;
     117                       struct ldb_message **msgs1, unsigned int count1,
     118                       struct ldb_message **msgs2, unsigned int count2)
     119{
     120        unsigned int i;
    111121        struct ldb_message *msg;
    112         int ret = 0;
    113         int adds=0, modifies=0, deletes=0;
    114 
    115         if (ldb_transaction_start(ldb) != 0) {
     122        int ret;
     123        unsigned int adds=0, modifies=0, deletes=0;
     124        struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
     125        if (options->controls != NULL && req_ctrls == NULL) {
     126                fprintf(stderr, "parsing controls failed: %s\n", ldb_errstring(ldb));
     127                return -1;
     128        }
     129
     130        if (ldb_transaction_start(ldb) != LDB_SUCCESS) {
    116131                fprintf(stderr, "Failed to start transaction: %s\n", ldb_errstring(ldb));
    117132                return -1;
     
    125140                                ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_ADD, msgs2[i]);
    126141                        }
    127                         if (ldb_add(ldb, msgs2[i]) != 0) {
     142                        if (ldb_add_ctrl(ldb, msgs2[i], req_ctrls) != LDB_SUCCESS) {
    128143                                fprintf(stderr, "failed to add %s - %s\n",
    129144                                        ldb_dn_get_linearized(msgs2[i]->dn),
     
    134149                        adds++;
    135150                } else {
    136                         if (modify_record(ldb, msg, msgs2[i]) > 0) {
    137                                 modifies++;
     151                        ret = modify_record(ldb, msg, msgs2[i], req_ctrls);
     152                        if (ret != -1) {
     153                                modifies += (unsigned int) ret;
     154                        } else {
     155                                return -1;
    138156                        }
    139157                }
     
    147165                                ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_DELETE, msgs1[i]);
    148166                        }
    149                         if (ldb_delete(ldb, msgs1[i]->dn) != 0) {
     167                        if (ldb_delete_ctrl(ldb, msgs1[i]->dn, req_ctrls) != LDB_SUCCESS) {
    150168                                fprintf(stderr, "failed to delete %s - %s\n",
    151169                                        ldb_dn_get_linearized(msgs1[i]->dn),
     
    158176        }
    159177
    160         if (ldb_transaction_commit(ldb) != 0) {
     178        if (ldb_transaction_commit(ldb) != LDB_SUCCESS) {
    161179                fprintf(stderr, "Failed to commit transaction: %s\n", ldb_errstring(ldb));
    162180                return -1;
    163181        }
    164182
    165         printf("# %d adds  %d modifies  %d deletes\n", adds, modifies, deletes);
    166 
    167         return ret;
     183        printf("# %u adds  %u modifies  %u deletes\n", adds, modifies, deletes);
     184
     185        return 0;
    168186}
    169187
     
    171189  save a set of messages as ldif to a file
    172190*/
    173 static int save_ldif(struct ldb_context *ldb, 
    174                      FILE *f, struct ldb_message **msgs, int count)
    175 {
    176         int i;
    177 
    178         fprintf(f, "# editing %d records\n", count);
     191static int save_ldif(struct ldb_context *ldb,
     192                     FILE *f, struct ldb_message **msgs, unsigned int count)
     193{
     194        unsigned int i;
     195
     196        fprintf(f, "# editing %u records\n", count);
    179197
    180198        for (i=0;i<count;i++) {
    181199                struct ldb_ldif ldif;
    182                 fprintf(f, "# record %d\n", i+1);
     200                fprintf(f, "# record %u\n", i+1);
    183201
    184202                ldif.changetype = LDB_CHANGETYPE_NONE;
     
    195213  edit the ldb search results in msgs using the user selected editor
    196214*/
    197 static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int count1,
    198                    const char *editor)
     215static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1,
     216                   unsigned int count1, const char *editor)
    199217{
    200218        int fd, ret;
     
    204222        struct ldb_ldif *ldif;
    205223        struct ldb_message **msgs2 = NULL;
    206         int count2 = 0;
     224        unsigned int count2 = 0;
    207225
    208226        /* write out the original set of messages to a temporary
     
    270288}
    271289
    272 static void usage(void)
     290static void usage(struct ldb_context *ldb)
    273291{
    274292        printf("Usage: ldbedit <options> <expression> <attributes ...>\n");
    275         ldb_cmdline_help("ldbedit", stdout);
    276         exit(1);
     293        ldb_cmdline_help(ldb, "ldbedit", stdout);
     294        exit(LDB_ERR_OPERATIONS_ERROR);
    277295}
    278296
     
    285303        const char *expression = "(|(objectClass=*)(distinguishedName=*))";
    286304        const char * const * attrs = NULL;
    287 
    288         ldb = ldb_init(NULL, NULL);
     305        TALLOC_CTX *mem_ctx = talloc_new(NULL);
     306        struct ldb_control **req_ctrls;
     307
     308        ldb = ldb_init(mem_ctx, NULL);
     309        if (ldb == NULL) {
     310                return LDB_ERR_OPERATIONS_ERROR;
     311        }
    289312
    290313        options = ldb_cmdline_process(ldb, argc, argv, usage);
    291314
    292315        /* the check for '=' is for compatibility with ldapsearch */
    293         if (options->argc > 0 && 
     316        if (options->argc > 0 &&
    294317            strchr(options->argv[0], '=')) {
    295318                expression = options->argv[0];
     
    304327        if (options->basedn != NULL) {
    305328                basedn = ldb_dn_new(ldb, ldb, options->basedn);
    306                 if ( ! ldb_dn_validate(basedn)) {
    307                         printf("Invalid Base DN format\n");
    308                         exit(1);
    309                 }
    310         }
    311 
    312         ret = ldb_search(ldb, ldb, &result, basedn, options->scope, attrs, "%s", expression);
     329                if (basedn == NULL) {
     330                        return LDB_ERR_OPERATIONS_ERROR;
     331                }
     332        }
     333
     334        req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
     335        if (options->controls != NULL &&  req_ctrls== NULL) {
     336                printf("parsing controls failed: %s\n", ldb_errstring(ldb));
     337                return LDB_ERR_OPERATIONS_ERROR;
     338        }
     339
     340        ret = ldb_search_ctrl(ldb, ldb, &result, basedn, options->scope, attrs, req_ctrls, "%s", expression);
    313341        if (ret != LDB_SUCCESS) {
    314342                printf("search failed - %s\n", ldb_errstring(ldb));
    315                 exit(1);
     343                return ret;
    316344        }
    317345
    318346        if (result->count == 0) {
    319347                printf("no matching records - cannot edit\n");
    320                 return 0;
    321         }
    322 
    323         do_edit(ldb, result->msgs, result->count, options->editor);
    324 
    325         if (result) {
    326                 ret = talloc_free(result);
    327                 if (ret == -1) {
    328                         fprintf(stderr, "talloc_free failed\n");
    329                         exit(1);
    330                 }
    331         }
    332 
    333         talloc_free(ldb);
    334         return 0;
    335 }
     348                talloc_free(mem_ctx);
     349                return LDB_SUCCESS;
     350        }
     351
     352        ret = do_edit(ldb, result->msgs, result->count, options->editor);
     353
     354        talloc_free(mem_ctx);
     355
     356        return ret == 0 ? LDB_SUCCESS : LDB_ERR_OPERATIONS_ERROR;
     357}
  • trunk/server/source4/lib/ldb/tools/ldbmodify.c

    r414 r745  
    3434#include "ldb.h"
    3535#include "tools/cmdline.h"
     36#include "ldbutil.h"
    3637
    37 static int failures;
     38static unsigned int failures;
     39static struct ldb_cmdline *options;
    3840
    39 static void usage(void)
     41static void usage(struct ldb_context *ldb)
    4042{
    4143        printf("Usage: ldbmodify <options> <ldif...>\n");
    4244        printf("Modifies a ldb based upon ldif change records\n\n");
    43         ldb_cmdline_help("ldbmodify", stdout);
    44         exit(1);
     45        ldb_cmdline_help(ldb, "ldbmodify", stdout);
     46        exit(LDB_ERR_OPERATIONS_ERROR);
    4547}
    4648
     
    4850  process modifies for one file
    4951*/
    50 static int process_file(struct ldb_context *ldb, FILE *f, int *count)
     52static int process_file(struct ldb_context *ldb, FILE *f, unsigned int *count)
    5153{
    5254        struct ldb_ldif *ldif;
    5355        int ret = LDB_SUCCESS;
    54        
     56        struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
     57
     58        if (options->controls != NULL &&  req_ctrls== NULL) {
     59                printf("parsing controls failed: %s\n", ldb_errstring(ldb));
     60                exit(LDB_ERR_OPERATIONS_ERROR);
     61        }
     62
    5563        while ((ldif = ldb_ldif_read_file(ldb, f))) {
    5664                switch (ldif->changetype) {
    5765                case LDB_CHANGETYPE_NONE:
    5866                case LDB_CHANGETYPE_ADD:
    59                         ret = ldb_add(ldb, ldif->msg);
     67                        ret = ldb_add_ctrl(ldb, ldif->msg,req_ctrls);
    6068                        break;
    6169                case LDB_CHANGETYPE_DELETE:
    62                         ret = ldb_delete(ldb, ldif->msg->dn);
     70                        ret = ldb_delete_ctrl(ldb, ldif->msg->dn,req_ctrls);
    6371                        break;
    6472                case LDB_CHANGETYPE_MODIFY:
    65                         ret = ldb_modify(ldb, ldif->msg);
     73                        ret = ldb_modify_ctrl(ldb, ldif->msg,req_ctrls);
    6674                        break;
    6775                }
    6876                if (ret != LDB_SUCCESS) {
    69                         fprintf(stderr, "ERR: \"%s\" on DN %s\n",
     77                        fprintf(stderr, "ERR: (%s) \"%s\" on DN %s\n",
     78                                ldb_strerror(ret),
    7079                                ldb_errstring(ldb), ldb_dn_get_linearized(ldif->msg->dn));
    7180                        failures++;
    7281                } else {
    7382                        (*count)++;
     83                        if (options->verbose) {
     84                                printf("Modified %s\n", ldb_dn_get_linearized(ldif->msg->dn));
     85                        }
    7486                }
    7587                ldb_ldif_read_free(ldb, ldif);
     
    8294{
    8395        struct ldb_context *ldb;
    84         int count=0;
    85         int i, ret=LDB_SUCCESS;
    86         struct ldb_cmdline *options;
     96        unsigned int i, count = 0;
     97        int ret = LDB_SUCCESS;
     98        TALLOC_CTX *mem_ctx = talloc_new(NULL);
    8799
    88         ldb = ldb_init(NULL, NULL);
     100        ldb = ldb_init(mem_ctx, NULL);
     101        if (ldb == NULL) {
     102                return LDB_ERR_OPERATIONS_ERROR;
     103        }
    89104
    90105        options = ldb_cmdline_process(ldb, argc, argv, usage);
     
    99114                        if (!f) {
    100115                                perror(fname);
    101                                 exit(1);
     116                                return LDB_ERR_OPERATIONS_ERROR;
    102117                        }
    103118                        ret = process_file(ldb, f, &count);
     
    106121        }
    107122
    108         talloc_free(ldb);
     123        talloc_free(mem_ctx);
    109124
    110         printf("Modified %d records with %d failures\n", count, failures);
     125        printf("Modified %u records with %u failures\n", count, failures);
    111126
    112127        return ret;
  • trunk/server/source4/lib/ldb/tools/ldbrename.c

    r414 r745  
    3737#include "tools/cmdline.h"
    3838
    39 static void usage(void)
     39static void usage(struct ldb_context *ldb)
    4040{
    4141        printf("Usage: ldbrename [<options>] <olddn> <newdn>\n");
    4242        printf("Renames records in a ldb\n\n");
    43         ldb_cmdline_help("ldbmodify", stdout);
    44         exit(1);
     43        ldb_cmdline_help(ldb, "ldbmodify", stdout);
     44        exit(LDB_ERR_OPERATIONS_ERROR);
    4545}
    4646
     
    5252        struct ldb_cmdline *options;
    5353        struct ldb_dn *dn1, *dn2;
     54        TALLOC_CTX *mem_ctx = talloc_new(NULL);
    5455
    55         ldb = ldb_init(NULL, NULL);
     56        ldb = ldb_init(mem_ctx, NULL);
     57        if (ldb == NULL) {
     58                return LDB_ERR_OPERATIONS_ERROR;
     59        }
    5660
    5761        options = ldb_cmdline_process(ldb, argc, argv, usage);
    5862
    5963        if (options->argc < 2) {
    60                 usage();
     64                usage(ldb);
    6165        }
    6266
    6367        dn1 = ldb_dn_new(ldb, ldb, options->argv[0]);
    6468        dn2 = ldb_dn_new(ldb, ldb, options->argv[1]);
    65 
    66         if ( ! ldb_dn_validate(dn1)) {
    67                 printf("Invalid DN1: %s\n", options->argv[0]);
    68                 return -1;
    69         }
    70         if ( ! ldb_dn_validate(dn2)) {
    71                 printf("Invalid DN2: %s\n", options->argv[1]);
    72                 return -1;
     69        if ((dn1 == NULL) || (dn2 == NULL)) {
     70                return LDB_ERR_OPERATIONS_ERROR;
    7371        }
    7472
    7573        ret = ldb_rename(ldb, dn1, dn2);
    76         if (ret == 0) {
     74        if (ret == LDB_SUCCESS) {
    7775                printf("Renamed 1 record\n");
    7876        } else  {
     
    8179        }
    8280
    83         talloc_free(ldb);
     81        talloc_free(mem_ctx);
    8482       
    8583        return ret;
  • trunk/server/source4/lib/ldb/tools/ldbsearch.c

    r414 r745  
    3232 */
    3333
    34 #include "ldb_includes.h"
     34#include "replace.h"
     35#include "system/filesys.h"
     36#include "system/time.h"
    3537#include "ldb.h"
    3638#include "tools/cmdline.h"
    3739
    38 static void usage(void)
     40static void usage(struct ldb_context *ldb)
    3941{
    4042        printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
    41         ldb_cmdline_help("ldbsearch", stdout);
    42         exit(1);
     43        ldb_cmdline_help(ldb, "ldbsearch", stdout);
     44        exit(LDB_ERR_OPERATIONS_ERROR);
    4345}
    4446
     
    5557
    5658        int sort;
    57         int num_stored;
     59        unsigned int num_stored;
    5860        struct ldb_message **store;
    59         int refs_stored;
     61        unsigned int refs_stored;
    6062        char **refs_store;
    6163
    62         int entries;
    63         int refs;
    64 
    65         int pending;
     64        unsigned int entries;
     65        unsigned int refs;
     66
     67        unsigned int pending;
    6668        int status;
    6769};
     
    132134{
    133135        struct search_context *sctx;
    134         int ret;
     136        int ret = LDB_SUCCESS;
    135137
    136138        sctx = talloc_get_type(req->context, struct search_context);
     
    173175
    174176        talloc_free(ares);
    175         if (ret) {
     177        if (ret != LDB_SUCCESS) {
    176178                return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    177179        }
     
    192194        req = NULL;
    193195       
    194         sctx = talloc(ldb, struct search_context);
    195         if (!sctx) return -1;
     196        sctx = talloc_zero(ldb, struct search_context);
     197        if (!sctx) return LDB_ERR_OPERATIONS_ERROR;
    196198
    197199        sctx->ldb = ldb;
    198200        sctx->sort = options->sorted;
    199         sctx->num_stored = 0;
    200         sctx->refs_stored = 0;
    201         sctx->store = NULL;
    202201        sctx->req_ctrls = ldb_parse_control_strings(ldb, sctx, (const char **)options->controls);
    203202        if (options->controls != NULL &&  sctx->req_ctrls== NULL) {
    204203                printf("parsing controls failed: %s\n", ldb_errstring(ldb));
    205                 return -1;
    206         }
    207         sctx->entries = 0;
    208         sctx->refs = 0;
     204                return LDB_ERR_OPERATIONS_ERROR;
     205        }
    209206
    210207        if (basedn == NULL) {
     
    225222                talloc_free(sctx);
    226223                printf("allocating request failed: %s\n", ldb_errstring(ldb));
    227                 return -1;
     224                return ret;
    228225        }
    229226
     
    233230        if (ret != LDB_SUCCESS) {
    234231                printf("search failed - %s\n", ldb_errstring(ldb));
    235                 return -1;
     232                return ret;
    236233        }
    237234
    238235        ret = ldb_wait(req->handle, LDB_WAIT_ALL);
    239         if (ret != LDB_SUCCESS) {
     236        if (ret != LDB_SUCCESS) {
    240237                printf("search error - %s\n", ldb_errstring(ldb));
    241                 return -1;
     238                return ret;
    242239        }
    243240
     
    246243
    247244        if (sctx->sort && (sctx->num_stored != 0 || sctx->refs != 0)) {
    248                 int i;
     245                unsigned int i;
    249246
    250247                if (sctx->num_stored) {
    251                         ldb_qsort(sctx->store, sctx->num_stored, sizeof(struct ldb_message *),
    252                                   ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
     248                        LDB_TYPESAFE_QSORT(sctx->store, sctx->num_stored, ldb, do_compare_msg);
    253249                }
    254250                for (i = 0; i < sctx->num_stored; i++) {
     
    261257        }
    262258
    263         printf("# returned %d records\n# %d entries\n# %d referrals\n",
     259        printf("# returned %u records\n# %u entries\n# %u referrals\n",
    264260                sctx->entries + sctx->refs, sctx->entries, sctx->refs);
    265261
     
    267263        talloc_free(req);
    268264
    269         return 0;
     265        return LDB_SUCCESS;
    270266}
    271267
     
    278274        int ret = -1;
    279275        const char *expression = "(|(objectClass=*)(distinguishedName=*))";
    280 
    281         ldb = ldb_init(NULL, NULL);
     276        TALLOC_CTX *mem_ctx = talloc_new(NULL);
     277
     278        ldb = ldb_init(mem_ctx, NULL);
    282279        if (ldb == NULL) {
    283                 return -1;
     280                return LDB_ERR_OPERATIONS_ERROR;
    284281        }
    285282
     
    301298        if (options->basedn != NULL) {
    302299                basedn = ldb_dn_new(ldb, ldb, options->basedn);
    303                 if ( ! ldb_dn_validate(basedn)) {
    304                         fprintf(stderr, "Invalid Base DN format\n");
    305                         exit(1);
     300                if (basedn == NULL) {
     301                        return LDB_ERR_OPERATIONS_ERROR;
    306302                }
    307303        }
     
    310306                char line[1024];
    311307                while (fgets(line, sizeof(line), stdin)) {
    312                         if (do_search(ldb, basedn, options, line, attrs) == -1) {
    313                                 ret = -1;
    314                         }
     308                        ret = do_search(ldb, basedn, options, line, attrs);
    315309                }
    316310        } else {
     
    318312        }
    319313
    320         talloc_free(ldb);
     314        talloc_free(mem_ctx);
     315
    321316        return ret;
    322317}
  • trunk/server/source4/lib/ldb/tools/ldbtest.c

    r414 r745  
    3232 */
    3333
    34 #include "ldb_includes.h"
     34#include "replace.h"
     35#include "system/filesys.h"
     36#include "system/time.h"
    3537#include "ldb.h"
    3638#include "tools/cmdline.h"
    3739
    38 static struct timeval tp1,tp2;
     40static struct timespec tp1,tp2;
    3941static struct ldb_cmdline *options;
    4042
    4143static void _start_timer(void)
    4244{
    43         gettimeofday(&tp1,NULL);
     45        if (clock_gettime(CUSTOM_CLOCK_MONOTONIC, &tp1) != 0) {
     46                clock_gettime(CLOCK_REALTIME, &tp1);
     47        }
    4448}
    4549
    4650static double _end_timer(void)
    4751{
    48         gettimeofday(&tp2,NULL);
     52        if (clock_gettime(CUSTOM_CLOCK_MONOTONIC, &tp2) != 0) {
     53                clock_gettime(CLOCK_REALTIME, &tp2);
     54        }
    4955        return((tp2.tv_sec - tp1.tv_sec) +
    50                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
     56               (tp2.tv_nsec - tp1.tv_nsec)*1.0e-9);
    5157}
    5258
    5359static void add_records(struct ldb_context *ldb,
    5460                        struct ldb_dn *basedn,
    55                         int count)
     61                        unsigned int count)
    5662{
    5763        struct ldb_message msg;
    58         int i;
     64        unsigned int i;
    5965
    6066#if 0
    6167        if (ldb_lock(ldb, "transaction") != 0) {
    6268                printf("transaction lock failed\n");
    63                 exit(1);
     69                exit(LDB_ERR_OPERATIONS_ERROR);
    6470        }
    6571#endif
     
    123129                if (ldb_add(ldb, &msg) != 0) {
    124130                        printf("Add of %s failed - %s\n", name, ldb_errstring(ldb));
    125                         exit(1);
     131                        exit(LDB_ERR_OPERATIONS_ERROR);
    126132                }
    127133
     
    134140        if (ldb_unlock(ldb, "transaction") != 0) {
    135141                printf("transaction unlock failed\n");
    136                 exit(1);
     142                exit(LDB_ERR_OPERATIONS_ERROR);
    137143        }
    138144#endif
     
    142148static void modify_records(struct ldb_context *ldb,
    143149                           struct ldb_dn *basedn,
    144                            int count)
     150                           unsigned int count)
    145151{
    146152        struct ldb_message msg;
    147         int i;
     153        unsigned int i;
    148154
    149155        for (i=0;i<count;i++) {
     
    180186                if (ldb_modify(ldb, &msg) != 0) {
    181187                        printf("Modify of %s failed - %s\n", name, ldb_errstring(ldb));
    182                         exit(1);
     188                        exit(LDB_ERR_OPERATIONS_ERROR);
    183189                }
    184190
     
    195201static void delete_records(struct ldb_context *ldb,
    196202                           struct ldb_dn *basedn,
    197                            int count)
    198 {
    199         int i;
     203                           unsigned int count)
     204{
     205        unsigned int i;
    200206
    201207        for (i=0;i<count;i++) {
     
    210216                if (ldb_delete(ldb, dn) != 0) {
    211217                        printf("Delete of %s failed - %s\n", ldb_dn_get_linearized(dn), ldb_errstring(ldb));
    212                         exit(1);
     218                        exit(LDB_ERR_OPERATIONS_ERROR);
    213219                }
    214220                talloc_free(name);
     
    218224}
    219225
    220 static void search_uid(struct ldb_context *ldb, struct ldb_dn *basedn, int nrecords, int nsearches)
    221 {
    222         int i;
     226static void search_uid(struct ldb_context *ldb, struct ldb_dn *basedn,
     227                       unsigned int nrecords, unsigned int nsearches)
     228{
     229        unsigned int i;
    223230
    224231        for (i=0;i<nsearches;i++) {
     
    233240                if (ret != LDB_SUCCESS || (uid < nrecords && res->count != 1)) {
    234241                        printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
    235                         exit(1);
     242                        exit(LDB_ERR_OPERATIONS_ERROR);
    236243                }
    237244
    238245                if (uid >= nrecords && res->count > 0) {
    239246                        printf("Found %s !? - %d\n", expr, ret);
    240                         exit(1);
     247                        exit(LDB_ERR_OPERATIONS_ERROR);
    241248                }
    242249
    243                 printf("testing uid %d/%d - %d  \r", i, uid, res->count);
     250                printf("Testing uid %d/%d - %d  \r", i, uid, res->count);
    244251                fflush(stdout);
    245252
     
    251258}
    252259
    253 static void start_test(struct ldb_context *ldb, int nrecords, int nsearches)
     260static void start_test(struct ldb_context *ldb, unsigned int nrecords,
     261                       unsigned int nsearches)
    254262{
    255263        struct ldb_dn *basedn;
     
    257265        basedn = ldb_dn_new(ldb, ldb, options->basedn);
    258266        if ( ! ldb_dn_validate(basedn)) {
    259                 printf("Invalid base DN\n");
    260                 exit(1);
     267                printf("Invalid base DN format\n");
     268                exit(LDB_ERR_INVALID_DN_SYNTAX);
    261269        }
    262270
     
    323331        if (ldb_add(*ldb, msg) != 0) {
    324332                printf("Add of %s failed - %s\n", ldb_dn_get_linearized(msg->dn), ldb_errstring(*ldb));
    325                 exit(1);
     333                exit(LDB_ERR_OPERATIONS_ERROR);
    326334        }
    327335
     
    338346        if (ldb_add(*ldb, msg) != 0) {
    339347                printf("Add of %s failed - %s\n", ldb_dn_get_linearized(msg->dn), ldb_errstring(*ldb));
    340                 exit(1);
     348                exit(LDB_ERR_OPERATIONS_ERROR);
    341349        }
    342350
    343351        if (talloc_free(*ldb) != 0) {
    344352                printf("failed to free/close ldb database");
    345                 exit(1);
     353                exit(LDB_ERR_OPERATIONS_ERROR);
    346354        }
    347355
     
    351359        if (ret != 0) {
    352360                printf("failed to connect to %s\n", options->url);
    353                 exit(1);
     361                exit(LDB_ERR_OPERATIONS_ERROR);
    354362        }
    355363
    356364        basedn = ldb_dn_new(*ldb, *ldb, options->basedn);
     365        msg->dn = basedn;
     366        ldb_dn_add_child_fmt(msg->dn, "cn=test");
    357367
    358368        ret = ldb_search(*ldb, *ldb, &res, basedn, LDB_SCOPE_SUBTREE, NULL, "uid=test");
    359369        if (ret != LDB_SUCCESS) {
    360370                printf("Search with (uid=test) filter failed!\n");
    361                 exit(1);
     371                exit(LDB_ERR_OPERATIONS_ERROR);
    362372        }
    363373        if(res->count != 1) {
    364374                printf("Should have found 1 record - found %d\n", res->count);
    365                 exit(1);
     375                exit(LDB_ERR_OPERATIONS_ERROR);
    366376        }
    367377
     
    371381            ldb_delete(*ldb, indexlist) != 0) {
    372382                printf("cleanup failed - %s\n", ldb_errstring(*ldb));
    373                 exit(1);
     383                exit(LDB_ERR_OPERATIONS_ERROR);
    374384        }
    375385
     
    378388
    379389
    380 static void usage(void)
     390static void usage(struct ldb_context *ldb)
    381391{
    382392        printf("Usage: ldbtest <options>\n");
     
    387397        printf("\n");
    388398        printf("tests ldb API\n\n");
    389         exit(1);
     399        exit(LDB_ERR_OPERATIONS_ERROR);
    390400}
    391401
     
    396406
    397407        ldb = ldb_init(mem_ctx, NULL);
     408        if (ldb == NULL) {
     409                return LDB_ERR_OPERATIONS_ERROR;
     410        }
    398411
    399412        options = ldb_cmdline_process(ldb, argc, argv, usage);
     
    410423               options->num_records, options->num_searches);
    411424
    412         start_test(ldb, options->num_records, options->num_searches);
     425        start_test(ldb,
     426                   (unsigned int) options->num_records,
     427                   (unsigned int) options->num_searches);
    413428
    414429        start_test_index(&ldb);
     
    416431        talloc_free(mem_ctx);
    417432
    418         return 0;
    419 }
     433        return LDB_SUCCESS;
     434}
Note: See TracChangeset for help on using the changeset viewer.