2 * Copyright (c) 1995 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
20 #include <ac/dirent.h>
21 #include <ac/string.h>
22 #include <ac/unistd.h>
26 #include <quipu/config.h>
27 #include <quipu/entry.h>
28 #include <quipu/commonarg.h>
29 #include <quipu/attrvalue.h>
32 #define HAVE_FILE_ATTR_DIR
36 #define HAVE_PARSE_ENTRY
39 #define DEF_EDBFILENAME "EDB"
40 #define EDB_ROOT_FILENAME "EDB.root"
42 #define EDBMAP_FILENAME "EDB.map"
43 #define ADDVALS_FILENAME ".add"
45 #define MAX_LINE_SIZE 2048
47 #define VERBOSE_ENTRY_REPORT_THRESHOLD 250
54 struct edbmap *edbm_next;
58 static int edb2ldif( FILE *outfp, char *edbfile, char *basedn, int recurse );
59 static int convert_entry( FILE *fp, char *edbname, FILE *outfp,
60 char *basedn, char *loc_addvals, int loc_addlen, char *linebuf );
61 static int add_rdn_values (Attr_Sequence entryas, RDN rdn);
62 static int read_edbmap( char *mapfile, struct edbmap **edbmapp );
63 static char *file2rdn( struct edbmap *edbmap, char *filename );
64 static void free_edbmap( struct edbmap *edbmap );
65 static char *read_file( char *filename, int *lenp );
66 static void print_err( char *msg );
73 static int verboseflg;
74 static int override_add = 0;
75 static int entrycount;
76 static char **ignore_attr = NULL;
77 static char *always_addvals = NULL;
78 static int always_addlen;
80 static char *edb_home = ".";
83 int ldap_syslog_level = 0;
87 main( int argc, char **argv )
89 char *usage = "usage: %s [-d] [-o] [-r] [-v] [-b basedn] [-a addvalsfile] [-f fileattrdir] [-i ignoreattr...] [edbfile...]\n";
90 char edbfile[ MAXNAMLEN ], *basedn;
91 int c, rc, errflg, ignore_count, recurse;
93 #ifdef HAVE_FILE_ATTR_DIR
94 extern char *file_attr_directory;
97 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
103 errflg = recurse = 0;
108 always_addvals = NULL;
111 while (( c = getopt( argc, argv, "dorva:b:f:h:i:" )) != -1 ) {
117 fprintf( stderr, "Ignoring -d: compile with -DLDAP_DEBUG to enable this option.\n" );
134 if ( always_addvals != NULL ) {
136 } else if (( always_addvals = read_file( optarg, &always_addlen ))
144 if ( basedn != NULL ) {
152 #ifdef HAVE_FILE_ATTR_DIR
153 /* add trailing slash to directory name if missing */
154 if ( *( optarg + strlen( optarg ) - 1 ) == '/' ) {
155 file_attr_directory = strdup( optarg );
156 } else if (( file_attr_directory = (char *)malloc( strlen( optarg )
158 sprintf( file_attr_directory, "%s/", optarg );
161 if ( file_attr_directory == NULL ) {
162 print_err( "malloc" );
165 #else /* HAVE_FILE_ATTR_DIR */
166 fprintf( stderr, "Ignoring -f: this option requires a newer version of ISODE.\n" );
167 #endif /* HAVE_FILE_ATTR_DIR */
175 if ( ignore_count == 0 ) {
176 ignore_attr = (char **)malloc( 2 * sizeof( char * ));
178 ignore_attr = (char **)realloc( ignore_attr,
179 ( ignore_count + 2 ) * sizeof( char * ));
181 if ( ignore_attr == NULL ) {
182 print_err( "malloc/realloc" );
185 ignore_attr[ ignore_count ] = optarg;
186 ignore_attr[ ++ignore_count ] = NULL;
195 fprintf( stderr, usage, progname );
199 if ( basedn == NULL ) {
203 /* load & initialize quipu syntax handlers */
206 pp_quipu_init( progname );
208 dsap_init( NULL, NULL );
210 dsa_mode = 1; /* so {CRYPT} is accepted by EDB parse routines */
212 if ( init_syntaxes() < 0 ) {
213 fprintf( stderr, "%s: init_syntaxes failed -- check your oid tables \n",
221 /* process EDB file(s) */
222 if ( optind >= argc ) {
224 rc = edb2ldif( stdout, edbfile, basedn, recurse );
226 for ( rc = 0; rc >= 0 && optind < argc; ++optind ) {
227 if ( argv[ optind ][ 0 ] == '/' ) {
228 strcpy( edbfile, argv[ optind ] );
230 sprintf( edbfile, "%s/%s", edb_home, argv[ optind ] );
232 rc = edb2ldif( stdout, edbfile, basedn, recurse );
236 if ( last_dn != NULL ) {
241 fprintf( stderr, "edb2ldif: exit( %d )\n", ( rc < 0 ) ? 1 : 0 );
244 exit( ( rc < 0 ) ? 1 : 0 );
249 edb2ldif( FILE *outfp, char *edbfile, char *basedn, int recurse )
252 char *addvals, *p, *rdn, line[ MAX_LINE_SIZE + 1 ];
253 char dirname[ MAXNAMLEN ], filename[ MAXNAMLEN ];
254 int err, startcount, addvals_len;
259 fprintf( stderr, "edb2ldif( 0x%X, \"%s\", \"%s\", %d)\n",
260 outfp, edbfile, basedn, recurse );
264 if ( *edbfile == '\0' ) {
265 sprintf( filename, "%s/%s", edb_home, EDB_ROOT_FILENAME );
266 if ( stat( filename, &st ) == 0 ) {
267 if (( err = edb2ldif( outfp, filename, basedn, 0 )) < 0 ) {
270 fprintf( stderr, "edb2ldif: 0 return( %d )\n", err );
275 if (( basedn = strdup( last_dn )) == NULL ) {
276 print_err( "strdup" );
279 fprintf( stderr, "edb2ldif: 1 return( -1 )\n" );
285 sprintf( edbfile, "%s/%s", edb_home, DEF_EDBFILENAME );
289 fprintf( stderr, "%s: converting EDB file: \"%s\"\n\tbasedn: \"%s\"\n",
290 progname, edbfile, basedn );
293 startcount = entrycount;
297 /* construct name of directory we are working in */
298 if (( p = strrchr( edbfile, '/' )) == NULL ) {
302 strncpy( dirname, edbfile, p - edbfile );
303 dirname[ p - edbfile ] = '\0';
306 /* load local ".add" file (if any) */
307 sprintf( filename, "%s/%s", dirname, ADDVALS_FILENAME );
309 addvals = read_file( filename, &addvals_len );
311 /* read and convert this EDB file */
312 if (( fp = fopen( edbfile, "r" )) == NULL ) {
313 print_err( edbfile );
314 if ( addvals != NULL ) {
319 fprintf( stderr, "edb2ldif: 2 return( -1 )\n" );
325 /* skip first two lines (type and timestamp) if they are present */
326 if ( fgets( line, MAX_LINE_SIZE, fp ) == NULL ) {
329 line[ strlen( line ) - 1 ] = '\0';
330 if ( strcmp( line, "MASTER" ) == 0 || strcmp( line, "SLAVE" ) == 0 ||
331 strcmp( line, "CACHE" ) == 0 ) {
332 if ( fgets( line, MAX_LINE_SIZE, fp ) == NULL ) {
341 fprintf( stderr, "%s: skipping empty EDB file %s\n", progname,
343 err = 0; /* treat as a non-fatal error */
345 while ( !feof( fp ) && ( err = convert_entry( fp, edbfile, outfp,
346 basedn, addvals, addvals_len, line )) > 0 ) {
347 if ( verboseflg && (( entrycount - startcount ) %
348 VERBOSE_ENTRY_REPORT_THRESHOLD ) == 0 ) {
349 fprintf( stderr, "\tworking... %d entries done...\n",
350 entrycount - startcount );
356 if ( addvals != NULL ) {
363 fprintf( stderr, "edb2ldif: 3 return( %d )\n", err );
370 fprintf( stderr, "\t%d entries converted\n\n",
371 entrycount - startcount );
374 /* optionally convert EDB file within sub-directories */
379 struct edbmap *edbmap;
381 /* open this directory */
382 if (( dp = opendir( dirname )) == NULL ) {
383 print_err( dirname );
386 fprintf( stderr, "edb2ldif: 4 return( -1 )\n" );
392 /* check for EDB.map file and record contents for future reference */
393 sprintf( filename, "%s/%s", dirname, EDBMAP_FILENAME );
394 if ( read_edbmap( filename, &edbmap ) < 0 ) {
395 print_err( "read_edbmap" );
399 fprintf( stderr, "edb2ldif: 5 return( -1 )\n" );
405 p = dirname + strlen( dirname );
409 /* scan looking for sub-directories w/EDB files in them */
411 while ( err >= 0 && ( dep = readdir( dp )) != NULL ) {
412 if ( dep->d_name[ 0 ] == '.' && ( dep->d_name[ 1 ] == '\0' ||
413 ( dep->d_name[ 1 ] == '.' && dep->d_name[ 2 ] == '\0' ))) {
414 continue; /* skip "." and ".." */
417 strcpy( p, dep->d_name );
420 fprintf( stderr, "edb2ldif: checking directory \"%s\"\n",
425 if ( stat( dirname, &st ) != 0 ) {
426 print_err( dirname );
427 } else if ( S_ISDIR( st.st_mode )) {
428 sprintf( filename, "%s/%s", dirname, DEF_EDBFILENAME );
430 if ( stat( filename, &st ) == 0 && S_ISREG( st.st_mode )) {
431 if (( newbase = malloc( strlen( basedn ) +
432 strlen( dep->d_name ) + 3 )) == NULL ) {
433 print_err( "malloc" );
438 sprintf( newbase, "%s@%s", basedn,
439 file2rdn( edbmap, dep->d_name ));
442 err = edb2ldif( outfp, filename, newbase, recurse );
449 free_edbmap( edbmap );
453 fprintf( stderr, "%s: %d total entries converted under \"%s\"\n\n",
454 progname, entrycount - startcount, basedn );
460 fprintf( stderr, "edb2ldif: 6 return( %d )\n", err );
468 * read one entry from fp and write to outfp.
469 * return > 0 if entry converted, 0 if end of file, < 0 if error occurs
482 Attr_Sequence as, tmpas;
484 PS attrtype_ps, val_ps;
489 extern int parse_status;
490 extern char *parse_file;
491 extern RDN parse_rdn;
492 #ifdef HAVE_PARSE_ENTRY
493 extern char *parse_entry;
494 extern Attr_Sequence fget_attributes();
495 #else /* HAVE_PARSE_ENTRY */
496 extern Attr_Sequence get_attributes();
497 #endif /* HAVE_PARSE_ENTRY */
501 fprintf( stderr, "convert_entry( 0x%X, \"%s\", 0x%X, \"%s\", ...)\n",
502 fp, edbname, outfp, basedn );
506 while (( dnstr = fgets( linebuf, MAX_LINE_SIZE, fp )) != NULL &&
511 if ( dnstr == NULL ) {
512 return( feof( fp ) ? 0 : -1 ); /* end of file or error */
515 linebuf[ strlen( linebuf ) - 1 ] = '\0';
517 if (( dnstr = malloc( strlen( basedn ) + strlen( linebuf ) + 2 ))
519 print_err( "convert_entry" );
522 sprintf( dnstr, "%s@%s", basedn, linebuf );
523 if ( last_dn != NULL ) {
528 if ( entrycount > 0 ) {
529 fputc( '\n', outfp );
533 * parse_entry, parse_file and parse_rdn are needed inside the
534 * libisode decoding routines, so we set it here.
536 parse_file = edbname;
537 #ifdef HAVE_PARSE_ENTRY
540 parse_rdn = rdn = str2rdn( linebuf );
542 if (( val_ps = ps_alloc( str_open )) == NULLPS ||
543 str_setup( val_ps, NULLCP, 0, 0 ) == NOTOK ) {
544 fprintf( stderr, "%s: ps_alloc/setup failed (EDB file %s)\n", progname,
546 if ( rdn != NULLRDN ) {
552 if (( dn = str2dn( dnstr )) == NULLDN || av2ldif( outfp, NULL, dn,
553 0, "dn", val_ps ) < 0 ) {
555 "str2dn or av2ldif of DN failed (EDB file %s, entry %s)\n",
557 print_err( linebuf );
558 if ( dn != NULLDN ) {
562 if ( rdn != NULLRDN ) {
571 if ( always_addvals != NULL && ( loc_addvals == NULL || !override_add )
572 && fwrite( always_addvals, always_addlen, 1, outfp ) != 1 ) {
574 "write of additional values failed (EDB file %s, entry %s)\n",
576 print_err( linebuf );
578 if ( rdn != NULLRDN ) {
584 if ( loc_addvals != NULL && fwrite( loc_addvals, loc_addlen, 1,
587 "write of additional values failed (EDB file %s, entry %s)\n",
589 print_err( linebuf );
591 if ( rdn != NULLRDN ) {
598 #ifdef HAVE_PARSE_ENTRY
599 as = fget_attributes( fp );
600 #else /* HAVE_PARSE_ENTRY */
601 as = get_attributes( fp );
602 #endif /* HAVE_PARSE_ENTRY */
604 if ( parse_status != 0 ) {
605 fprintf( stderr, "%s: problem parsing entry (EDB file %s)\n", progname,
608 if ( as != NULLATTR ) {
611 if ( rdn != NULLRDN ) {
617 if ( add_rdn_values( as, rdn ) != 0 ) {
619 "adding RDN values(s) failed (EDB file %s, entry %s)\n",
621 print_err( linebuf );
622 if ( as != NULLATTR ) {
625 if ( rdn != NULLRDN ) {
631 if (( attrtype_ps = ps_alloc( str_open )) == NULLPS ||
632 str_setup( attrtype_ps, NULLCP, 0, 0 ) == NOTOK ) {
633 fprintf( stderr, "%s: ps_alloc/setup failed (EDB file %s)\n", progname,
635 if ( as != NULLATTR ) {
638 if ( rdn != NULLRDN ) {
644 for ( tmpas = as; tmpas != NULLATTR; tmpas = tmpas->attr_link ) {
645 attrtype_ps->ps_ptr = attrtype_ps->ps_base;
646 AttrT_print( attrtype_ps, tmpas->attr_type, EDBOUT );
647 *attrtype_ps->ps_ptr = '\0';
649 if ( ignore_attr != NULL ) {
652 for ( i = 0; ignore_attr[ i ] != NULL; ++i ) {
653 if ( strcasecmp( attrtype_ps->ps_base, ignore_attr[ i ] )
658 if ( ignore_attr[ i ] != NULL ) {
659 continue; /* skip this attribute */
664 for ( av = tmpas->attr_value; av != NULLAV; av = av->avseq_next ) {
666 if ( av2ldif( outfp, av, NULL, tmpas->attr_type->oa_syntax,
667 attrtype_ps->ps_base, val_ps ) < 0 ) {
669 "av2ldif failed (EDB file %s, entry %s, attribute %s, value no. %d)\n",
670 edbname, dnstr, attrtype_ps->ps_base, valcnt );
671 print_err( linebuf );
672 ps_free( attrtype_ps );
675 if ( rdn != NULLRDN ) {
683 ps_free( attrtype_ps );
686 if ( rdn != NULLRDN ) {
695 add_rdn_values( Attr_Sequence entryas, RDN rdn )
698 * this routine is based on code from the real_unravel_attribute() routine
699 * found in isode-8.0/.dsap/common/attribute.c
705 for (; rdn != NULLRDN; rdn = rdn->rdn_next ) {
706 if (( as = as_find_type( entryas, rdn->rdn_at )) == NULLATTR ) {
707 at = AttrT_cpy( rdn->rdn_at );
708 avs = avs_comp_new( AttrV_cpy(&rdn->rdn_av ));
709 as = as_comp_new( at, avs, NULLACL_INFO );
710 entryas = as_merge( entryas, as );
712 for ( avs = as->attr_value; avs != NULLAV; avs = avs->avseq_next ) {
713 if ( AttrV_cmp( &rdn->rdn_av, &avs->avseq_av ) == 0 ) {
718 if ( avs == NULLAV ) {
719 avs = avs_comp_new( AttrV_cpy( &rdn->rdn_av ));
720 as->attr_value = avs_merge( as->attr_value, avs );
729 /* read the EDB.map file and return a linked list of translations */
731 read_edbmap( char *mapfile, struct edbmap **edbmapp )
734 char *p, *filename, *rdn, line[ MAX_LINE_SIZE + 1 ];
736 struct edbmap *emp, *tmpemp;
740 fprintf( stderr, "read_edbmap( \"%s\", ...)\n", mapfile );
744 if (( fp = fopen( mapfile, "r" )) == NULL ) {
746 return( 0 ); /* soft error -- no EDB.map file */
752 * read all the lines in the file, looking for lines of the form:
756 while ( err == 0 && fgets( line, MAX_LINE_SIZE, fp ) != NULL ) {
757 line[ strlen( line ) - 1 ] = '\0'; /* remove trailing newline */
758 if (( filename = strchr( line, '#' )) == NULL ) {
763 while ( isspace((unsigned char) *filename) ) { /* strip leading whitespace */
767 if ( *filename == '\0' ) {
771 p = filename + strlen( filename ) - 1;
772 while ( isspace((unsigned char) *p) ) { /* strip trailing whitespace */
777 while ( isspace((unsigned char) *rdn)) { /* strip leading whitespace */
781 if ( *rdn == '\0' ) {
785 p = rdn + strlen( rdn ) - 1;
786 while ( isspace((unsigned char) *p)) { /* strip trailing whitespace */
790 if (( tmpemp = (struct edbmap *)calloc( 1, sizeof( struct edbmap )))
792 ( tmpemp->edbm_filename = strdup( filename )) == NULL ||
793 ( tmpemp->edbm_rdn = strdup( rdn )) == NULL ) {
796 tmpemp->edbm_next = emp;
814 file2rdn( struct edbmap *edbmap, char *filename )
818 fprintf( stderr, "file2rdn( 0x%X, \"%s\" )\n", edbmap, filename );
822 while ( edbmap != NULL ) {
823 if ( strcmp( filename, edbmap->edbm_filename ) == 0 ) {
826 edbmap = edbmap->edbm_next;
829 return(( edbmap == NULL ) ? filename : edbmap->edbm_rdn );
833 /* free the edbmap list */
835 free_edbmap( struct edbmap *edbmap )
841 fprintf( stderr, "free_edbmap( 0x%X )\n", edbmap );
845 while ( edbmap != NULL ) {
846 if ( edbmap->edbm_filename != NULL ) free( edbmap->edbm_filename );
847 if ( edbmap->edbm_rdn != NULL ) free( edbmap->edbm_rdn );
849 edbmap = edbmap->edbm_next;
856 print_err( char *msg )
860 fprintf( stderr, "print_err( \"%s\" )\n", msg );
864 if ( errno > sys_nerr ) {
865 fprintf( stderr, "%s: %s: error %d\n", progname, msg, errno );
867 fprintf( stderr, "%s: %s: %s\n", progname, msg, sys_errlist[ errno ] );
873 read_file( char *filename, int *lenp )
881 fprintf( stderr, "read_file( \"%s\", 0x%X )\n", filename, lenp );
885 if ( stat( filename, &st ) != 0 || !S_ISREG( st.st_mode ) ||
886 ( fp = fopen( filename, "r" )) == NULL ) {
890 if (( buf = (char *)malloc( st.st_size )) == NULL ) {
895 if ( fread( buf, st.st_size, 1, fp ) != 1 ) {