summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/tools/dbscan.c
blob: 7f9433c2fc196c0f09e31c16ca15d905a242c5ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif


/*
 * small program to scan a Directory Server db file and dump the contents
 *
 * TODO: indirect indexes
 */

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include "db.h"

#ifdef _WIN32
#include <windows.h>
#include <winsock.h>
extern int getopt();
extern char *optarg; 
typedef unsigned char uint8_t;
#else
#include <netinet/in.h>
#include <inttypes.h>
#endif

/* file type */
#define ENTRYTYPE     0x1
#define INDEXTYPE     0x2
#define VLVINDEXTYPE  0x4
#define CHANGELOGTYPE 0x8

/* display mode */
#define RAWDATA     0x1
#define SHOWCOUNT   0x2
#define SHOWDATA    0x4
#define SHOWSUMMARY 0x8

/* stolen from slapi-plugin.h */
#define SLAPI_OPERATION_BIND     0x00000001UL
#define SLAPI_OPERATION_UNBIND   0x00000002UL
#define SLAPI_OPERATION_SEARCH   0x00000004UL
#define SLAPI_OPERATION_MODIFY   0x00000008UL
#define SLAPI_OPERATION_ADD      0x00000010UL
#define SLAPI_OPERATION_DELETE   0x00000020UL
#define SLAPI_OPERATION_MODDN    0x00000040UL
#define SLAPI_OPERATION_MODRDN   SLAPI_OPERATION_MODDN
#define SLAPI_OPERATION_COMPARE  0x00000080UL
#define SLAPI_OPERATION_ABANDON  0x00000100UL
#define SLAPI_OPERATION_EXTENDED 0x00000200UL
#define SLAPI_OPERATION_ANY      0xFFFFFFFFUL
#define SLAPI_OPERATION_NONE     0x00000000UL

#define ONEMEG (1024*1024)

#if defined(linux)
#include <getopt.h>
#endif

typedef u_int32_t        ID;

typedef unsigned int uint32;

typedef struct {
    uint32 max;
    uint32 used;
    uint32 id[1];
} IDL;

uint32 file_type = 0;
uint32 min_display = 0;
uint32 display_mode = 0;
int truncatesiz = 0;
long pres_cnt = 0;
long eq_cnt = 0;
long app_cnt = 0;
long sub_cnt = 0;
long match_cnt = 0;
long ind_cnt = 0;
long allids_cnt = 0;
long other_cnt = 0;

/** db_printf - functioning same as printf but a place for manipluating output.
*/
void db_printf(char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    vfprintf(stdout, fmt, ap);
}

void db_printfln(char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    vfprintf(stdout, fmt, ap);
    vfprintf(stdout, "\n", NULL);
}

int MAX_BUFFER = 4096;
int MIN_BUFFER = 20;

static IDL *idl_make(DBT *data)
{
    IDL *idl = NULL, *xidl;

    if (data->size < 2*sizeof(uint32)) {
        idl = (IDL *)malloc(sizeof(IDL) + 64*sizeof(uint32));
        if (! idl)
            return NULL;
        idl->max = 64;
        idl->used = 1;
        idl->id[0] = *(uint32 *)(data->data);
        return idl;
    }

    xidl = (IDL *)(data->data);
    idl = (IDL *)malloc(data->size);
    if (! idl)
        return NULL;

    memcpy(idl, xidl, data->size);
    return idl;
}

static void idl_free(IDL *idl)
{
    idl->max = 0;
    idl->used = 0;
    free(idl);
}

static IDL *idl_append(IDL *idl, uint32 id)
{
    if (idl->used >= idl->max) {
        /* must grow */
        idl->max *= 2;
        idl = realloc(idl, sizeof(IDL) + idl->max * sizeof(uint32));
        if (! idl)
            return NULL;
    }
    idl->id[idl->used++] = id;
    return idl;
}


/* format a string for easy printing */
#define FMT_LF_OK       1
#define FMT_SP_OK       2
static char *format_raw(unsigned char *s, int len, int flags,
                        unsigned char *buf, int buflen)
{
    static char hex[] = "0123456789ABCDEF";
    unsigned char *p, *o, *bufend = buf + buflen - 1;
    int i;

    if (NULL == buf || buflen <= 0)
        return NULL;

    for (p = s, o = buf, i = 0; i < len && o < bufend; p++, i++) {
        if ((*p == '%') || (*p <= ' ') || (*p >= 126)) {
            /* index keys are stored with their trailing NUL */
            if ((*p == 0) && (i == len-1))
                continue;
            if ((flags & FMT_LF_OK) && (*p == '\n')) {
                *o++ = '\n';
                *o++ = '\t';
            } else if ((flags && FMT_SP_OK) && (*p == ' ')) {
                *o++ = ' ';
            } else {
                *o++ = '%';
                *o++ = hex[*p / 16];
                *o++ = hex[*p % 16];
            }
        } else {
            *o++ = *p;
        }
        if (truncatesiz > 0 && o > bufend - 5) {
            /* truncate it */
            strcpy(o, " ...");
            i = len;
            o += 4;
        }
    }
    *o = 0;
    return (char *)buf;
}

static char *format(unsigned char *s, int len, unsigned char *buf, int buflen)
{
    return format_raw(s, len, 0, buf, buflen);
}

static char *format_entry(unsigned char *s, int len,
                          unsigned char *buf, int buflen)
{
    return format_raw(s, len, FMT_LF_OK | FMT_SP_OK, buf, buflen);
}

static char *idl_format(IDL *idl, int isfirsttime, int *done)
{
    static char *buf = NULL;
    static uint32 i = 0;
    
    if (buf == NULL) {
        buf = (char *)malloc(MAX_BUFFER);
        if (buf == NULL)
            return "?";
    }

    buf[0] = 0;
    if (0 != isfirsttime) {
        i = 0;
    }
    for (; i < idl->used; i++) {
        sprintf((char *)buf + strlen(buf), "%d ", idl->id[i]);

        if (strlen(buf) > (size_t)MAX_BUFFER-MIN_BUFFER) {
            i++;
            done = 0;
            return (char *)buf;
        }
    }
    *done = 1;
    return (char *)buf;
}


/*** Copied from cl5_api.c: _cl5ReadString ***/
void _cl5ReadString (char **str, char **buff)
{
    if (str)
    {
        int len = strlen (*buff);
        
        if (len)
        { 
            *str = strdup(*buff);
            (*buff) += len + 1;
        }
        else /* just null char - skip it */
        {
            *str = NULL;
            (*buff) ++;
        }
    }
    else /* just skip this string */
    {
        (*buff) += strlen (*buff) + 1;        
    }
}

/** print_attr - print attribute name followed by one value.
    assume the value stored as null terminated string.
*/
void print_attr(char *attrname, char **buff)
{
    char *val = NULL;

    _cl5ReadString(&val, buff);
    if(attrname != NULL || val != NULL) {
        db_printf("\t");
    }

    if(attrname) {
        db_printf("%s: ", attrname);
    }
    if(val != NULL) {
        db_printf("%s\n", val);
        free(val);
    }
}

/*** Copied from cl5_api.c: _cl5ReadMods ***/
/* mods format:
   -----------
   <4 byte mods count><mod1><mod2>...

   mod format:
   -----------
   <1 byte modop><null terminated attr name><4 byte count>
   {<4 byte size><value1><4 byte size><value2>... || 
        <null terminated str1> <null terminated str2>...}
 */
void _cl5ReadMod(char **buff);

void _cl5ReadMods(char **buff)
{
    char *pos = *buff;
    uint32 i;
    uint32 mod_count;

    /* need to copy first, to skirt around alignment problems on certain
       architectures */
    memcpy((char *)&mod_count, *buff, sizeof(mod_count));
    mod_count = ntohl(mod_count);
    pos += sizeof (mod_count);
    

    for (i = 0; i < mod_count; i++)
    {        
        _cl5ReadMod (&pos);
    }
 
    *buff = pos;
}


/** print_ber_attr - print one line of attribute, the value was stored
                     in ber format, length followed by string.
*/
void print_ber_attr(char* attrname, char** buff)
{
    char *val = NULL;
    uint32 bv_len;

    memcpy((char *)&bv_len, *buff, sizeof(bv_len));
    bv_len = ntohl(bv_len);
    *buff += sizeof (uint32);
    if (bv_len > 0) {

        db_printf("\t\t");

        if(attrname != NULL) {
            db_printf("%s: ", attrname);
        }

        val = malloc(bv_len + 1);
        memcpy (val, *buff, bv_len);
        val[bv_len] = 0;
        *buff += bv_len;
        db_printf("%s\n", val);
        free(val);
    }
}

/*
 *  Conversion routines between host byte order and
 *  big-endian (which is how we store data in the db). 
 */
static ID id_stored_to_internal(char* b)
{
    ID i;
    i = (ID)b[3] & 0x000000ff;
    i |= (((ID)b[2]) << 8) & 0x0000ff00;
    i |= (((ID)b[1]) << 16) & 0x00ff0000;
    i |= ((ID)b[0]) << 24;
    return i;
}

static void id_internal_to_stored(ID i,char *b)
{
    if ( sizeof(ID) > 4 ) {
        memset (b+4, 0, sizeof(ID)-4);
    }

    b[0] = (char)(i >> 24);
    b[1] = (char)(i >> 16);
    b[2] = (char)(i >> 8);
    b[3] = (char)i;
}

void _cl5ReadMod(char **buff)
{
    char *pos = *buff;
    uint32 i;
    uint32 val_count;
    char *type = NULL;
    int op;

    op = (*pos) & 0x000000FF;
    pos ++;
    _cl5ReadString (&type, &pos);

    /* need to do the copy first, to skirt around alignment problems on
       certain architectures */
    memcpy((char *)&val_count, pos, sizeof(val_count));
    val_count = ntohl(val_count);
    pos += sizeof (uint32);

    for (i = 0; i < val_count; i++)
    {
        print_ber_attr(type, &pos);
    }

    (*buff) = pos;
    free(type);
}

/*
   *** Copied from cl5_api:cl5DBData2Entry ***
   Data in db format:
   ------------------
   <1 byte version><1 byte change_type><sizeof uint32 time><null terminated dbid>
   <null terminated csn><null terminated uniqueid><null terminated targetdn>
   [<null terminated newrdn><1 byte deleteoldrdn>][<4 byte mod count><mod1><mod2>....]

Note: the length of time is set uint32 instead of time_t. Regardless of the
width of long (32-bit or 64-bit), it's stored using 4bytes by the server [153306].

   mod format:
   -----------
   <1 byte modop><null terminated attr name><4 byte value count>
   <4 byte value size><value1><4 byte value size><value2>
*/
void print_changelog(unsigned char *data, int len)
{
    uint8_t version;
    unsigned long operation_type;
    char *pos = (char *)data;
    uint32 thetime32;
    time_t thetime;
    uint32 replgen;

    /* read byte of version */
    version = *((uint8_t *)pos);
    if (version != 5)
    {
        db_printf("Invalid changelog db version %i\nWorks for version 5 only.\n", version);
        exit(1);
    }
    pos += sizeof(version);

    /* read change type */
    operation_type = (unsigned long)(*(uint8_t *)pos);
    pos ++;
    
    /* need to do the copy first, to skirt around alignment problems on
       certain architectures */
    memcpy((char *)&thetime32, pos, sizeof(thetime32));

    replgen = ntohl(thetime32);
    pos += sizeof(uint32);
    thetime = (time_t)replgen;
    db_printf("\treplgen: %ld %s", replgen, ctime((time_t *)&thetime));

    /* read csn */
    print_attr("csn", &pos);
    /* read UniqueID */
    print_attr("uniqueid", &pos);    
    
    /* figure out what else we need to read depending on the operation type */
    switch (operation_type)
    {
        case SLAPI_OPERATION_ADD:        
            print_attr("parentuniqueid", &pos);
            print_attr("dn", &pos);
            /* convert mods to entry */
            db_printf("\toperation: add\n");
            _cl5ReadMods(&pos);
            break;

        case SLAPI_OPERATION_MODIFY:    
            print_attr("dn", &pos);
            db_printf("\toperation: modify\n");
            _cl5ReadMods(&pos);
            break;

        case SLAPI_OPERATION_MODRDN:    
            print_attr("dn", &pos);
            print_attr("newrdn", &pos);
            pos ++;
            print_attr("dn", &pos);
            print_attr("uniqueid", &pos);
            db_printf("\toperation: modrdn\n");
            _cl5ReadMods(&pos);
            break;

        case SLAPI_OPERATION_DELETE:    
            print_attr("dn", &pos);
            db_printf("\toperation: delete\n");
            break;

        default:                            
            db_printf("Failed to format entry\n");
            break;
    }
}

static void display_index_item(DBC *cursor, DBT *key, DBT *data,
                               unsigned char *buf, int buflen)
{
    IDL *idl = NULL;
    int ret = 0;

    idl = idl_make(data);
    if (idl == NULL) {
        printf("\t(illegal idl)\n");
        return;
    }

    if (file_type & VLVINDEXTYPE) {  /* vlv index file */
        if (1 > min_display) { /* recno is always 1 */
            if (display_mode & SHOWCOUNT) {  /* key  size=1 */
                printf("%-40s         1\n",
                       format(key->data, key->size, buf, buflen));
            } else {
                printf("%-40s\n", format(key->data, key->size, buf, buflen));
            }
            if (display_mode & SHOWDATA) {
                cursor->c_get(cursor, key, data, DB_GET_RECNO);
                printf("\t%5d\n", *(db_recno_t *)(data->data));
            }
        }
        goto index_done;
    }

    /* ordinary index file */
    /* fetch all other id's too */
    while (ret == 0) {
        ret = cursor->c_get(cursor, key, data, DB_NEXT_DUP);
        if (ret == 0)
            idl = idl_append(idl, *(uint32 *)(data->data));
    }
    if (ret == DB_NOTFOUND)
        ret = 0;
    if (ret != 0) {
        printf("Failure while looping dupes: %s\n", db_strerror(ret));
        exit(1);
    }

    if (idl && idl->max == 0) {
        /* allids; should not exist in the new idl world */
        if ( allids_cnt == 0 && (display_mode & SHOWSUMMARY)) {
            printf("The following index keys reached allids:\n");
        }
        printf("%-40s(allids)\n", format(key->data, key->size, buf, buflen));
        allids_cnt++;
    } else {
        if (idl->used < min_display) {
            goto index_done;  /* less than minimum display count */
        } else if (display_mode & SHOWCOUNT) {  /* key  size */
            printf("%-40s%d\n", 
                   format(key->data, key->size, buf, buflen), idl->used);
        } else if (!(display_mode & SHOWSUMMARY) || (display_mode & SHOWDATA)) {
            /* show keys only if show summary is not set or 
                         * even if it's set, but with show data */
            printf("%-40s\n", format(key->data, key->size, buf, buflen));
        }
        if (display_mode & SHOWDATA) {
            char *formatted_idl = NULL;
            int done = 0;
            int isfirsttime = 1;
            while (0 == done) {
                formatted_idl = idl_format(idl, isfirsttime, &done);
                if (NULL == formatted_idl) {
                    done = 1; /* no more idl */
                } else {
                    if (1 == isfirsttime) {
                        printf("\t%s", formatted_idl);
                        isfirsttime = 0;
                    } else {
                        printf("%s", formatted_idl);
                    }
                }
            }
            printf("\n");
        }
    } 
index_done:
    if ( display_mode & SHOWSUMMARY ) {
        char firstchar;

        firstchar = ((char*)key->data)[0];

        switch ( firstchar ) {
            case '+':
                pres_cnt += idl->used;
                break;

            case '=':
                eq_cnt += idl->used;
                break;

            case '~':
                app_cnt += idl->used;
                break;

            case '*':
                sub_cnt += idl->used;
                break;
            
            case ':':
                match_cnt += idl->used;
                break;

            case '\\':
                ind_cnt += idl->used;
                break;

            default:
                other_cnt += idl->used;
                break;
        }
    }
    idl_free(idl);
    return;
}

static void display_item(DBC *cursor, DBT *key, DBT *data)
{
    static unsigned char *buf = NULL;
    static int buflen = 0;
    int tmpbuflen;

    if (truncatesiz > 0) {
        tmpbuflen = truncatesiz;
    } else if (file_type & INDEXTYPE) {
        /* +256: extra buffer for '\t' and '%##' */
        tmpbuflen = key->size + 256;
    } else {
        /* +1024: extra buffer for '\t' and '%##' */
        tmpbuflen = (key->size > data->size ? key->size : data->size) + 1024;
    }
    if (buflen < tmpbuflen) {
        buflen = tmpbuflen;
        buf = (unsigned char *)realloc(buf, buflen);
        if (NULL == buf) {
            printf("\t(malloc failed -- %d bytes)\n", buflen);
            return;
        }
    }

    if (display_mode & RAWDATA) {
        printf("%s\n", format(key->data, key->size, buf, buflen));
        printf("\t%s\n", format(data->data, data->size, buf, buflen));
    } else {
        if (file_type & INDEXTYPE) {
            display_index_item(cursor, key, data, buf, buflen);
        } else if (file_type & CHANGELOGTYPE) {
            /* changelog db file */
            printf("\ndbid: %s\n", format(key->data, key->size, buf, buflen));
            print_changelog(data->data, data->size);
            return;
        } else if (file_type & ENTRYTYPE) {
            /* id2entry file */
            ID entry_id = id_stored_to_internal(key->data);
            printf("id %d\n", entry_id);
            printf("\t%s\n", format_entry(data->data, data->size, buf, buflen));
        } else {
            /* user didn't tell us what kind of file, dump it raw */
            printf("%s\n", format(key->data, key->size, buf, buflen));
            printf("\t%s\n", format(data->data, data->size, buf, buflen));
        }
    }
    return;
}

static int
is_changelog(char *filename)
{
    char *ptr = NULL;
    int dashes = 0;
    int underscore = 0;
    if (NULL == (ptr = strrchr(filename, '/'))) {
        if (NULL == (ptr = strrchr(filename, '\\'))) {
            ptr = filename;
        } else {
            ptr++;
        }
    } else {
        ptr++;
    }
    for (; ptr && *ptr; ptr++) {
        if ('.' == *ptr) {
            if (0 == strncmp(ptr, ".db", 3)) {
                if (3 == dashes && 1 == underscore) {
                    return 1;
                } else {
                    return 0;
                }
            }
        } else if ('-' == *ptr) {
            if (underscore > 0) {
                return 0;
            }
            dashes++;
        } else if ('_' == *ptr) {
            if (dashes < 3) {
                return 0;
            }
            underscore++;
        } else if (!isxdigit(*ptr)) {
            return 0;
        }
    }
    return 0;
}

static void usage(char *argv0)
{
    printf("\n%s - scan a db file and dump the contents\n", argv0);
    printf("  common options:\n");
    printf("    -f <filename>   specify db file\n");
    printf("    -R              dump as raw data\n");
    printf("    -t <size>       entry truncate size (bytes)\n");
    printf("  entry file options:\n");
    printf("    -K <entry_id>   lookup only a specific entry id\n");
    printf("  index file options:\n");
    printf("    -k <key>        lookup only a specific key\n");
    printf("    -l <size>       max length of dumped id list\n");
    printf("                    (default %d; 40 bytes <= size <= 1048576 bytes)\n",
           MAX_BUFFER);
    printf("    -G <n>          only display index entries with more than <n> ids\n");
    printf("    -n              display idl lengths\n");
    printf("    -r              display the conents of idl\n");
    printf("    -s              Summary of index counts\n");
    printf("  sample usages:\n");
    printf("    # set <prefix>/usr/lib/<brand-ds>:<prefix>/usr/lib:/usr/lib in the library path\n");
    printf("    # dump the entry file\n");
    printf("    %s -f id2entry.db\n", argv0);
    printf("    # display index keys in cn.db4\n");
    printf("    %s -f cn.db4\n", argv0);
    printf("    # display index keys and the count of entries having the key in mail.db4\n");
    printf("    %s -r -f mail.db4\n", argv0);
    printf("    # display index keys and the IDs having more than 20 IDs in sn.db4\n");
    printf("    %s -r -G 20 -f sn.db4\n", argv0);
    printf("    # display summary of objectclass.db4\n");
    printf("    %s -f objectclass.db4\n", argv0);
    printf("\n");
    exit(1);
}

int main(int argc, char **argv)
{
    DB_ENV *env = NULL;
    DB *db = NULL;
    DBC *cursor = NULL;
    char *filename = NULL;
    DBT key = {0}, data = {0};
    int ret;
    char *find_key = NULL;
    uint32 entry_id = 0xffffffff;
    int c;

    key.flags = DB_DBT_REALLOC;
    data.flags = DB_DBT_REALLOC;
    while ((c = getopt(argc, argv, "f:Rl:nG:srk:K:hvt:")) != EOF) {
        switch (c) {
        case 'f':
            filename = optarg;
            break;
        case 'R':
            display_mode |= RAWDATA;
            break;
        case 'l':
        {
            uint32 tmpmaxbufsz = atoi(optarg);
            if (tmpmaxbufsz > ONEMEG) {
                tmpmaxbufsz = ONEMEG;
                printf("WARNING: max length of dumped id list too long, "
                       "reduced to %d\n", tmpmaxbufsz);
            } else if (tmpmaxbufsz < MIN_BUFFER * 2) {
                tmpmaxbufsz = MIN_BUFFER * 2;
                printf("WARNING: max length of dumped id list too short, "
                       "increased to %d\n", tmpmaxbufsz);
            }
            MAX_BUFFER = tmpmaxbufsz;
            break;
        }
        case 'n':
            display_mode |= SHOWCOUNT;
            break;
        case 'G':
            min_display = atoi(optarg)+1;
            break;
        case 'r':
            display_mode |= SHOWDATA;
            break;
        case 's':
            display_mode |= SHOWSUMMARY;
            break;
        case 'k':
            find_key = optarg;
            break;
        case 'K':
            id_internal_to_stored((ID)atoi(optarg), (char *)&entry_id);
            break;
        case 't':
            truncatesiz = atoi(optarg);
            break;
        case 'h':
        default:
            usage(argv[0]);
        }
    }

    if(filename == NULL) {
        usage(argv[0]);
    }
    if (NULL != strstr(filename, "id2entry.db")) {
        file_type |= ENTRYTYPE;
    } else if (is_changelog(filename)) {
        file_type |= CHANGELOGTYPE;
    } else {
        file_type |= INDEXTYPE;
        if (0 == strncmp(filename, "vlv#", 4)) {
            file_type |= VLVINDEXTYPE;
        } 
    }
        
    ret = db_env_create(&env, 0);
    if (ret != 0) {
        printf("Can't create dbenv: %s\n", db_strerror(ret));
        exit(1);
    }
    ret = env->open(env, NULL, DB_CREATE|DB_INIT_MPOOL|DB_PRIVATE, 0);
    if (ret != 0) {
        printf("Can't open dbenv: %s\n", db_strerror(ret));
        exit(1);
    }

    ret = db_create(&db, env, 0);
    if (ret != 0) {
        printf("Can't create db handle: %d\n", ret);
        exit(1);
    }
    ret = db->open(db, NULL, filename, NULL, DB_UNKNOWN, DB_RDONLY, 0);
    if (ret != 0) {
        printf("Can't open db file '%s': %s\n", filename, db_strerror(ret));
        exit(1);
    }

    /* cursor through the db */

    ret = db->cursor(db, NULL, &cursor, 0);
    if (ret != 0) {
        printf("Can't create db cursor: %s\n", db_strerror(ret));
        exit(1);
    }
    ret = cursor->c_get(cursor, &key, &data, DB_FIRST);
    if (ret == DB_NOTFOUND) {
        printf("Empty database!\n");
        exit(0);
    }
    if (ret != 0) {
        printf("Can't get first cursor: %s\n", db_strerror(ret));
        exit(1);
    }

    if (find_key) {
        key.size = strlen(find_key)+1;
        key.data = find_key;
        ret = db->get(db, NULL, &key, &data, 0);
        if (ret != 0) {
            /* could be a key that doesn't have the trailing null? */
            key.size--;
            ret = db->get(db, NULL, &key, &data, 0);
            if (ret != 0) {
                printf("Can't find key '%s'\n", find_key);
                exit(1);
            }
        }
        ret = cursor->c_get(cursor, &key, &data, DB_SET);
        if (ret != 0) {
            printf("Can't set cursor to returned item: %s\n",
                   db_strerror(ret));
            exit(1);
        }
        do {
            display_item(cursor, &key, &data);
            ret = cursor->c_get(cursor, &key, &data, DB_NEXT_DUP);
        } while (0 == ret);
        key.size = 0;
        key.data = NULL;
    } else if (entry_id != -1) {
        key.size = sizeof(entry_id);
        key.data = &entry_id;
        ret = db->get(db, NULL, &key, &data, 0);
        if (ret != 0) {
            printf("Can't set cursor to returned item: %s\n",
                   db_strerror(ret));
            exit(1);
        }
        display_item(cursor, &key, &data);
        key.size = 0;
        key.data = NULL;
    } else {
        while (ret == 0) {
            /* display */
            display_item(cursor, &key, &data);

            ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
            if ((ret != 0) && (ret != DB_NOTFOUND)) {
                printf("Bizarre error: %s\n", db_strerror(ret));
                exit(1);
            }
        }
    }

    ret = cursor->c_close(cursor);
    if (ret != 0) {
        printf("Can't close the cursor (?!): %s\n", db_strerror(ret));
        exit(1);
    }

    ret = db->close(db, 0);
    if (ret != 0) {
        printf("Unable to close db file: %s\n", db_strerror(ret));
        exit(1);
    }

    if ( display_mode & SHOWSUMMARY) {

        if ( allids_cnt > 0 ) {
            printf("Index keys that reached ALLIDs threshold: %ld\n", allids_cnt);
        }

        if ( pres_cnt > 0 ) {
            printf("Presence index keys: %ld\n", pres_cnt);
        }

        if ( eq_cnt > 0 ) {
            printf("Equality index keys: %ld\n", eq_cnt);
        }

        if ( app_cnt > 0 ) {
            printf("Approximate index keys: %ld\n", app_cnt);
        }

        if ( sub_cnt > 0 ) {
            printf("Substring index keys: %ld\n", sub_cnt);
        }

        if ( match_cnt > 0 ) {
            printf("Match index keys: %ld\n", match_cnt);
        }

        if ( ind_cnt > 0 ) {
            printf("Indirect index keys: %ld\n", ind_cnt);
        }

        if ( other_cnt > 0 ) {
            printf("This file contains %ld number of unknown type ( possible corruption)\n",other_cnt);
        }

    }

    ret = env->close(env, 0);
    if (ret != 0) {
        printf("Unable to shutdown libdb: %s\n", db_strerror(ret));
        exit(1);
    }

    return 0;
}