summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/tools/dbscan.c
blob: 0c36ddceece89a47755a2857f5ce8b63b5de91d5 (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
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
/** 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 <errno.h>
#include "db.h"
#include "nspr.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

#if ( defined( hpux ) )
#ifdef _XOPEN_SOURCE_EXTENDED
#include <arpa/inet.h>    /* for ntohl, et al. */
#endif
#endif

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

/* 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

/* changelog ruv info.  These correspond with some special csn
 * timestamps from cl5_api.c */
#define ENTRY_COUNT_KEY    "0000006f" /* 111 csn timestamp */
#define PURGE_RUV_KEY    "000000de" /* 222 csn timestamp */
#define MAX_RUV_KEY    "0000014d" /* 333 csn timestamp */

#define ONEMEG (1024*1024)

#ifndef DB_BUFFER_SMALL
#define DB_BUFFER_SMALL ENOMEM
#endif

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

typedef PRUint32 ID;

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

#define RDN_BULK_FETCH_BUFFER_SIZE (8*1024)
typedef struct _rdn_elem {
    char rdn_elem_id[sizeof(ID)];
    char rdn_elem_nrdn_len[2]; /* ushort; length including '\0' */
    char rdn_elem_rdn_len[2];  /* ushort; length including '\0' */
    char rdn_elem_nrdn_rdn[1]; /* "normalized rdn" '\0' "rdn" '\0' */
} rdn_elem;


#define RDN_ADDR(elem) \
    ((elem)->rdn_elem_nrdn_rdn + \
     sizeushort_stored_to_internal((elem)->rdn_elem_nrdn_len))

static void display_entryrdn_parent(DB *db, ID id, const char *nrdn, int indent);
static void display_entryrdn_self(DB *db, ID id, const char *nrdn, int indent);
static void display_entryrdn_children(DB *db, ID id, const char *nrdn, int indent);
static void display_entryrdn_item(DB *db, DBC *cursor, DBT *key);

PRUint32 file_type = 0;
PRUint32 min_display = 0;
PRUint32 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);
    va_end(ap);
}

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

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

int MAX_BUFFER = 4096;
int MIN_BUFFER = 20;

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

    if (data->size < 2*sizeof(PRUint32)) {
        idl = (IDL *)malloc(sizeof(IDL) + 64*sizeof(ID));
        if (! idl)
            return NULL;
        idl->max = 64;
        idl->used = 1;
        idl->id[0] = *(ID *)(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, ID id)
{
    if (idl->used >= idl->max) {
        /* must grow */
        idl->max *= 2;
        idl = realloc(idl, sizeof(IDL) + idl->max * sizeof(ID));
        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((char *)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 ID 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) {
        return;
    }
    db_printf("\t");

    if(attrname) {
        db_printf("%s: ", attrname);
    } else {
        db_printf("unknown attribute: ");
    }
    if(val) {
        db_printf("%s\n", val);
        free(val);
    } else {
        db_printf("\n");
    }
}

/*** 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;
    ID i;
    PRUint32 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;
    PRUint32 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 ) {
        (void)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;
}

static size_t sizeushort_stored_to_internal(char* b)
{
    size_t i;
    i = (PRUint16)b[1] & 0x000000ff;
    i |= (((PRUint16)b[0]) << 8) & 0x0000ff00;
    return i;
}

void _cl5ReadMod(char **buff)
{
    char *pos = *buff;
    PRUint32 i;
    PRUint32 val_count;
    char *type = NULL;

    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 (PRUint32);

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

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

/* data format: <value count> <value size> <value> <value size> <value> ..... */
void print_ruv(unsigned char *buff)
{
    char *pos = (char *)buff;
    PRUint32 i;
    PRUint32 val_count;

    /* 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 (PRUint32);

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

/*
   *** Copied from cl5_api:cl5DBData2Entry ***
   Data in db format:
   ------------------
   <1 byte version><1 byte change_type><sizeof PRUint32 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 PRUint32 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:
   -----------
   <0 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;
    PRUint32 thetime32;
    time_t thetime;
    PRUint32 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(PRUint32);
    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);
            db_printf("\toperation: modrdn\n");
            print_attr("newrdn", &pos);
            db_printf("\tdeleteoldrdn: %d\n", (int )(*pos++));
            print_attr("newsuperior", &pos);
            _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, *(PRUint32 *)(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 == NULL) {
        printf("\t(illegal idl)\n");
        return;
    }

    if (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));
            if (strncasecmp((char *)key->data, ENTRY_COUNT_KEY, 8) == 0) {
                printf("\tentry count: %d\n", *(int*)data->data);
            } else if (strncasecmp((char *)key->data, PURGE_RUV_KEY, 8) == 0) {
                printf("\tpurge ruv:\n");
                print_ruv(data->data);
        } else if (strncasecmp((char *)key->data, MAX_RUV_KEY, 8) == 0) {
                printf("\tmax ruv:\n");
                print_ruv(data->data);
            } else {
                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 %u\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;
}

void
_entryrdn_dump_rdn_elem(char *key, rdn_elem *elem, int indent)
{
    char *indentp = (char *)malloc(indent + 1);
    char *p, *endp = indentp + indent;

    for (p = indentp; p < endp; p++) *p = ' ';
    *p = '\0';    
    printf("%s\n", key);
    printf("%sID: %u; RDN: \"%s\"; NRDN: \"%s\"\n",
           indentp, id_stored_to_internal(elem->rdn_elem_id),
           RDN_ADDR(elem), elem->rdn_elem_nrdn_rdn);
    free(indentp);
}

static void
display_entryrdn_self(DB *db, ID id, const char *nrdn, int indent)
{
    DBC *cursor = NULL;
    DBT key, data;
    char *keybuf = NULL;
    int rc = 0;
    rdn_elem *elem;
    char buffer[1024]; 

    rc = db->cursor(db, NULL, &cursor, 0);
    if (rc) {
        printf("Can't create db cursor: %s\n", db_strerror(rc));
        exit(1);
    }
    snprintf(buffer, sizeof(buffer), "%u:%s", id, nrdn);
    keybuf = strdup(buffer);
    key.data = keybuf;
    key.size = key.ulen = strlen(keybuf) + 1;
    key.flags = DB_DBT_USERMEM;

    memset(&data, 0, sizeof(data));

    /* Position cursor at the matching key */
    rc = cursor->c_get(cursor, &key, &data, DB_SET);
    if (rc) {
        fprintf(stderr, "Failed to position cursor at the key: %s: %s "
                            "(%d)\n", (char *)key.data, db_strerror(rc), rc);
        goto bail;
    }

    elem = (rdn_elem *)data.data;
    _entryrdn_dump_rdn_elem(keybuf, elem, indent);
    display_entryrdn_parent(db, id_stored_to_internal(elem->rdn_elem_id),
                            elem->rdn_elem_nrdn_rdn, indent);
    display_entryrdn_children(db, id_stored_to_internal(elem->rdn_elem_id),
                              elem->rdn_elem_nrdn_rdn, indent);
bail:
    if (keybuf) {
        free(keybuf);
    }
    cursor->c_close(cursor);
    return;
}

static void
display_entryrdn_parent(DB *db, ID id, const char *nrdn, int indent)
{
    DBC *cursor = NULL;
    DBT key, data;
    char *keybuf = NULL;
    int rc = 0;
    rdn_elem *elem;
    char buffer[1024]; 

    rc = db->cursor(db, NULL, &cursor, 0);
    if (rc) {
        printf("Can't create db cursor: %s\n", db_strerror(rc));
        exit(1);
    }
    snprintf(buffer, sizeof(buffer), "P%d:%s", id, nrdn);
    keybuf = strdup(buffer);
    key.data = keybuf;
    key.size = key.ulen = strlen(keybuf) + 1;
    key.flags = DB_DBT_USERMEM;

    memset(&data, 0, sizeof(data));

    /* Position cursor at the matching key */
    rc = cursor->c_get(cursor, &key, &data, DB_SET);
    if (rc) {
        fprintf(stderr, "Failed to position cursor at the key: %s: %s "
                            "(%d)\n", (char *)key.data, db_strerror(rc), rc);
        goto bail;
    }

    elem = (rdn_elem *)data.data;
    _entryrdn_dump_rdn_elem(keybuf, elem, indent);
bail:
    if (keybuf) {
        free(keybuf);
    }
    cursor->c_close(cursor);
    return;
}

static void
display_entryrdn_children(DB *db, ID id, const char *nrdn, int indent)
{
    DBC *cursor = NULL;
    DBT key, data;
    char *keybuf = NULL;
    int rc = 0;
    rdn_elem *elem = NULL;;
    char buffer[1024]; 

    rc = db->cursor(db, NULL, &cursor, 0);
    if (rc) {
        printf("Can't create db cursor: %s\n", db_strerror(rc));
        exit(1);
    }
    indent += 2;
    snprintf(buffer, sizeof(buffer), "C%d:%s", id, nrdn);
    keybuf = strdup(buffer);
    key.data = keybuf;
    key.size = key.ulen = strlen(keybuf) + 1;
    key.flags = DB_DBT_USERMEM;

    memset(&data, 0, sizeof(data));
    data.ulen = sizeof(buffer);
    data.size = sizeof(buffer);
    data.data = buffer;
    data.flags = DB_DBT_USERMEM;

    /* Position cursor at the matching key */
    rc = cursor->c_get(cursor, &key, &data, DB_SET);
    if (rc) {
        if (DB_BUFFER_SMALL == rc) {
            fprintf(stderr, "Entryrdn index is corrupt; "
                            "data item for key %s is too large for our "
                            "buffer (need=%d actual=%d)\n",
                            (char *)key.data, data.size, data.ulen);
        } else if (rc != DB_NOTFOUND) {
            fprintf(stderr, "Failed to position cursor at the key: %s: %s "
                            "(%d)\n", (char *)key.data, db_strerror(rc), rc);
        }
        goto bail;
    }

    /* Iterate over the duplicates */
    for (;;) {
        elem = (rdn_elem *)data.data;
        _entryrdn_dump_rdn_elem(keybuf, elem, indent);
        display_entryrdn_self(db, id_stored_to_internal(elem->rdn_elem_id),
                              elem->rdn_elem_nrdn_rdn, indent);
        rc = cursor->c_get(cursor, &key, &data, DB_NEXT_DUP);
        if (rc) {
            break;
        }
    }
    if (rc) {
        if (DB_BUFFER_SMALL == rc) {
            fprintf(stderr, "Entryrdn index is corrupt; "
                            "data item for key %s is too large for our "
                            "buffer (need=%d actual=%d)\n",
                            (char *)key.data, data.size, data.ulen);
        } else if (rc != DB_NOTFOUND) {
            fprintf(stderr, "Failed to position cursor at the key: %s: %s "
                            "(%d)\n", (char *)key.data, db_strerror(rc), rc);
        }
    }
bail:
    if (keybuf) {
        free(keybuf);
    }
    cursor->c_close(cursor);
    return;
}

static void 
display_entryrdn_item(DB *db, DBC *cursor, DBT *key)
{
    rdn_elem *elem = NULL;
    int indent = 2;
    DBT data;
    int rc;

    /* Setting the bulk fetch buffer */
    memset(&data, 0, sizeof(data));
    if (key->data) { /* suffix, if not, dbscan fails. */
        /* Position cursor at the matching key */
        rc = cursor->c_get(cursor, key, &data, DB_SET);
        if (rc) {
            fprintf(stderr, "Failed to position cursor at the key: %s: %s "
                                "(%d)\n", (char *)key->data, db_strerror(rc), rc);
            return;
        }
    
        elem = (rdn_elem *)data.data;
        _entryrdn_dump_rdn_elem((char *)key->data, elem, indent);
        display_entryrdn_children(db, id_stored_to_internal(elem->rdn_elem_id),
                                  elem->rdn_elem_nrdn_rdn, indent);
    } else { /* otherwise, display all from the HEAD to TAIL */
        char buffer[RDN_BULK_FETCH_BUFFER_SIZE]; 
        DBT dataret;
        PRUint32 flags = DB_FIRST|DB_MULTIPLE;

        data.ulen = sizeof(buffer);
        data.size = sizeof(buffer);
        data.data = buffer;
        data.flags = DB_DBT_USERMEM;
next:
        /* Position cursor at the matching key */
        rc = cursor->c_get(cursor, key, &data, flags);
        if (rc) {
            if (DB_BUFFER_SMALL == rc) {
                fprintf(stderr, "Entryrdn index is corrupt; "
                                "data item for key %s is too large for our "
                                "buffer (need=%d actual=%d)\n",
                                (char *)key->data, data.size, data.ulen);
            } else {
                if (rc != DB_NOTFOUND) {
                    fprintf(stderr, "Failed to position cursor "
                                    "at the key: %s: %s (%d)\n",
                                    (char *)key->data, db_strerror(rc), rc);
                }
            }
            goto bail;
        }
    
        /* Iterate over the duplicates */
        for (;;) {
            void *ptr;
            DB_MULTIPLE_INIT(ptr, &data);
            for (;;) {
                memset(&dataret, 0, sizeof(dataret));
                DB_MULTIPLE_NEXT(ptr, &data, dataret.data, dataret.size);
                if (dataret.data == NULL)
                    break;
                if (ptr == NULL)
                    break;
    
                elem = (rdn_elem *)dataret.data;
                _entryrdn_dump_rdn_elem((char *)key->data, elem, indent);
            }
            rc = cursor->c_get(cursor, key, &data, DB_NEXT_DUP|DB_MULTIPLE);
            if (0 != rc) {
                break;
            }
        }
        if (DB_BUFFER_SMALL == rc) {
            fprintf(stderr, "Entryrdn index is corrupt; "
                                "data item for key %s is too large for our "
                                "buffer (need=%d actual=%d)\n",
                                (char *)key->data, data.size, data.ulen);
            goto bail;
        } else if (rc != DB_NOTFOUND) {
            fprintf(stderr, "Failed to position cursor at the key: %s: %s "
                                "(%d)\n", (char *)key->data, db_strerror(rc), rc);
            goto bail;
        }
        flags = DB_NEXT|DB_MULTIPLE;
        goto next;
    }
bail:
    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)
{
    char *copy = strdup(argv0);
    char *p0 = NULL, *p1 = NULL;
    if (NULL != copy) {
        /* the full path is not needed in the usages */
        p0 = strrchr(argv0, '/');
        if (NULL != p0) {
            *p0 = '\0';
            p0++;
        } else {
            p0 = argv0;
        }
        p1 = strrchr(p0, '-'); /* get rid of -bin from the usage */
        if (NULL != p1) {
            *p1 = '\0';
        }
    } 
    if (NULL == p0) {
        p0 = argv0;
    }
    printf("\n%s - scan a db file and dump the contents\n", p0);
    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 ID list lengths\n");
    printf("    -r              display the conents of ID list\n");
    printf("    -s              Summary of index counts\n");
    printf("  sample usages:\n");
    printf("    # dump the entry file\n");
    printf("    %s -f id2entry.db\n", p0);
    printf("    # display index keys in cn.db4\n");
    printf("    %s -f cn.db4\n", p0);
    printf("    # display index keys and the count of entries having the key in mail.db4\n");
    printf("    %s -r -f mail.db4\n", p0);
    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", p0);
    printf("    # display summary of objectclass.db4\n");
    printf("    %s -f objectclass.db4\n", p0);
    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;
    PRUint32 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':
        {
            PRUint32 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 (NULL != strstr(filename, "vlv#")) {
            file_type |= VLVINDEXTYPE;
        } else if (NULL != strstr(filename, "entryrdn.db")) {
            file_type |= ENTRYRDNINDEXTYPE;
        }
    }
        
    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);
            }
        }
        if (file_type & ENTRYRDNINDEXTYPE) {
            display_entryrdn_item(db, cursor, &key);
        } else {
            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 != 0xffffffff) {
        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 {
        if (file_type & ENTRYRDNINDEXTYPE) {
            key.data = NULL;
            display_entryrdn_item(db, cursor, &key);
        } 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;
}