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.
19 #include <ac/dirent.h>
20 #include <ac/string.h>
22 #include <sys/types.h>
25 #include <quipu/config.h>
26 #include <quipu/entry.h>
27 #include <quipu/commonarg.h>
28 #include <quipu/attrvalue.h>
31 #define HAVE_FILE_ATTR_DIR
35 #define HAVE_PARSE_ENTRY
38 #define DEF_EDBFILENAME "EDB"
39 #define EDB_ROOT_FILENAME "EDB.root"
41 #define EDBMAP_FILENAME "EDB.map"
42 #define ADDVALS_FILENAME ".add"
44 #define MAX_LINE_SIZE 2048
46 #define VERBOSE_ENTRY_REPORT_THRESHOLD 250
53 struct edbmap *edbm_next;
57 static int edb2ldif( FILE *outfp, char *edbfile, char *basedn, int recurse );
58 static int convert_entry( FILE *fp, char *edbname, FILE *outfp,
59 char *basedn, char *loc_addvals, int loc_addlen, char *linebuf );
60 static int read_edbmap( char *mapfile, struct edbmap **edbmapp );
61 static char *file2rdn( struct edbmap *edbmap, char *filename );
62 static void free_edbmap( struct edbmap *edbmap );
63 static char *read_file( char *filename, int *lenp );
64 static void print_err( char *msg );
71 static int verboseflg;
72 static int override_add = 0;
73 static int entrycount;
74 static char **ignore_attr = NULL;
75 static char *always_addvals = NULL;
76 static int always_addlen;
78 static char *edb_home = ".";
81 int ldap_syslog_level = 0;
88 char *usage = "usage: %s [-d] [-o] [-r] [-v] [-b basedn] [-a addvalsfile] [-f fileattrdir] [-i ignoreattr...] [edbfile...]\n";
89 char edbfile[ MAXNAMLEN ], *basedn;
90 int c, rc, errflg, ignore_count, recurse;
94 #ifdef HAVE_FILE_ATTR_DIR
95 extern char *file_attr_directory;
98 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
104 errflg = recurse = 0;
109 always_addvals = NULL;
112 while (( c = getopt( argc, argv, "dorva:b:f:h:i:" )) != -1 ) {
118 fprintf( stderr, "Ignoring -d: compile with -DLDAP_DEBUG to enable this option.\n" );
135 if ( always_addvals != NULL ) {
137 } else if (( always_addvals = read_file( optarg, &always_addlen ))
145 if ( basedn != NULL ) {
153 #ifdef HAVE_FILE_ATTR_DIR
154 /* add trailing slash to directory name if missing */
155 if ( *( optarg + strlen( optarg ) - 1 ) == '/' ) {
156 file_attr_directory = strdup( optarg );
157 } else if (( file_attr_directory = (char *)malloc( strlen( optarg )
159 sprintf( file_attr_directory, "%s/", optarg );
162 if ( file_attr_directory == NULL ) {
163 print_err( "malloc" );
166 #else /* HAVE_FILE_ATTR_DIR */
167 fprintf( stderr, "Ignoring -f: this option requires a newer version of ISODE.\n" );
168 #endif /* HAVE_FILE_ATTR_DIR */
176 if ( ignore_count == 0 ) {
177 ignore_attr = (char **)malloc( 2 * sizeof( char * ));
179 ignore_attr = (char **)realloc( ignore_attr,
180 ( ignore_count + 2 ) * sizeof( char * ));
182 if ( ignore_attr == NULL ) {
183 print_err( "malloc/realloc" );
186 ignore_attr[ ignore_count ] = optarg;
187 ignore_attr[ ++ignore_count ] = NULL;
196 fprintf( stderr, usage, progname );
200 if ( basedn == NULL ) {
204 /* load & initialize quipu syntax handlers */
207 pp_quipu_init( progname );
209 dsap_init( NULL, NULL );
211 dsa_mode = 1; /* so {CRYPT} is accepted by EDB parse routines */
213 if ( init_syntaxes() < 0 ) {
214 fprintf( stderr, "%s: init_syntaxes failed -- check your oid tables \n",
222 /* process EDB file(s) */
223 if ( optind >= argc ) {
225 rc = edb2ldif( stdout, edbfile, basedn, recurse );
227 for ( rc = 0; rc >= 0 && optind < argc; ++optind ) {
228 if ( argv[ optind ][ 0 ] == '/' ) {
229 strcpy( edbfile, argv[ optind ] );
231 sprintf( edbfile, "%s/%s", edb_home, argv[ optind ] );
233 rc = edb2ldif( stdout, edbfile, basedn, recurse );
237 if ( last_dn != NULL ) {
242 fprintf( stderr, "edb2ldif: exit( %d )\n", ( rc < 0 ) ? 1 : 0 );
245 exit( ( rc < 0 ) ? 1 : 0 );
250 edb2ldif( outfp, edbfile, basedn, recurse )
257 char *addvals, *p, *rdn, line[ MAX_LINE_SIZE + 1 ];
258 char dirname[ MAXNAMLEN ], filename[ MAXNAMLEN ];
259 int err, startcount, addvals_len;
264 fprintf( stderr, "edb2ldif( 0x%X, \"%s\", \"%s\", %d)\n",
265 outfp, edbfile, basedn, recurse );
269 if ( *edbfile == '\0' ) {
270 sprintf( filename, "%s/%s", edb_home, EDB_ROOT_FILENAME );
271 if ( stat( filename, &st ) == 0 ) {
272 if (( err = edb2ldif( outfp, filename, basedn, 0 )) < 0 ) {
275 fprintf( stderr, "edb2ldif: 0 return( %d )\n", err );
280 if (( basedn = strdup( last_dn )) == NULL ) {
281 print_err( "strdup" );
284 fprintf( stderr, "edb2ldif: 1 return( -1 )\n" );
290 sprintf( edbfile, "%s/%s", edb_home, DEF_EDBFILENAME );
294 fprintf( stderr, "%s: converting EDB file: \"%s\"\n\tbasedn: \"%s\"\n",
295 progname, edbfile, basedn );
298 startcount = entrycount;
302 /* construct name of directory we are working in */
303 if (( p = strrchr( edbfile, '/' )) == NULL ) {
307 strncpy( dirname, edbfile, p - edbfile );
308 dirname[ p - edbfile ] = '\0';
311 /* load local ".add" file (if any) */
312 sprintf( filename, "%s/%s", dirname, ADDVALS_FILENAME );
314 addvals = read_file( filename, &addvals_len );
316 /* read and convert this EDB file */
317 if (( fp = fopen( edbfile, "r" )) == NULL ) {
318 print_err( edbfile );
319 if ( addvals != NULL ) {
324 fprintf( stderr, "edb2ldif: 2 return( -1 )\n" );
330 /* skip first two lines (type and timestamp) if they are present */
331 if ( fgets( line, MAX_LINE_SIZE, fp ) == NULL ) {
334 line[ strlen( line ) - 1 ] = '\0';
335 if ( strcmp( line, "MASTER" ) == 0 || strcmp( line, "SLAVE" ) == 0 ||
336 strcmp( line, "CACHE" ) == 0 ) {
337 if ( fgets( line, MAX_LINE_SIZE, fp ) == NULL ) {
346 fprintf( stderr, "%s: skipping empty EDB file %s\n", progname,
348 err = 0; /* treat as a non-fatal error */
350 while ( !feof( fp ) && ( err = convert_entry( fp, edbfile, outfp,
351 basedn, addvals, addvals_len, line )) > 0 ) {
352 if ( verboseflg && (( entrycount - startcount ) %
353 VERBOSE_ENTRY_REPORT_THRESHOLD ) == 0 ) {
354 fprintf( stderr, "\tworking... %d entries done...\n",
355 entrycount - startcount );
361 if ( addvals != NULL ) {
368 fprintf( stderr, "edb2ldif: 3 return( %d )\n", err );
375 fprintf( stderr, "\t%d entries converted\n\n",
376 entrycount - startcount );
379 /* optionally convert EDB file within sub-directories */
384 struct edbmap *edbmap;
386 /* open this directory */
387 if (( dp = opendir( dirname )) == NULL ) {
388 print_err( dirname );
391 fprintf( stderr, "edb2ldif: 4 return( -1 )\n" );
397 /* check for EDB.map file and record contents for future reference */
398 sprintf( filename, "%s/%s", dirname, EDBMAP_FILENAME );
399 if ( read_edbmap( filename, &edbmap ) < 0 ) {
400 print_err( "read_edbmap" );
404 fprintf( stderr, "edb2ldif: 5 return( -1 )\n" );
410 p = dirname + strlen( dirname );
414 /* scan looking for sub-directories w/EDB files in them */
416 while ( err >= 0 && ( dep = readdir( dp )) != NULL ) {
417 if ( dep->d_name[ 0 ] == '.' && ( dep->d_name[ 1 ] == '\0' ||
418 ( dep->d_name[ 1 ] == '.' && dep->d_name[ 2 ] == '\0' ))) {
419 continue; /* skip "." and ".." */
422 strcpy( p, dep->d_name );
425 fprintf( stderr, "edb2ldif: checking directory \"%s\"\n",
430 if ( stat( dirname, &st ) != 0 ) {
431 print_err( dirname );
432 } else if ( S_ISDIR( st.st_mode )) {
433 sprintf( filename, "%s/%s", dirname, DEF_EDBFILENAME );
435 if ( stat( filename, &st ) == 0 && S_ISREG( st.st_mode )) {
436 if (( newbase = malloc( strlen( basedn ) +
437 strlen( dep->d_name ) + 3 )) == NULL ) {
438 print_err( "malloc" );
443 sprintf( newbase, "%s@%s", basedn,
444 file2rdn( edbmap, dep->d_name ));
447 err = edb2ldif( outfp, filename, newbase, recurse );
454 free_edbmap( edbmap );
458 fprintf( stderr, "%s: %d total entries converted under \"%s\"\n\n",
459 progname, entrycount - startcount, basedn );
465 fprintf( stderr, "edb2ldif: 6 return( %d )\n", err );
473 * read one entry from fp and write to outfp.
474 * return > 0 if entry converted, 0 if end of file, < 0 if error occurs
477 convert_entry( fp, edbname, outfp, basedn, loc_addvals, loc_addlen, linebuf )
486 Attr_Sequence as, tmpas;
488 PS attrtype_ps, val_ps;
493 extern int parse_status;
494 extern char *parse_file;
495 extern RDN parse_rdn;
496 #ifdef HAVE_PARSE_ENTRY
497 extern char *parse_entry;
498 extern Attr_Sequence fget_attributes();
499 #else /* HAVE_PARSE_ENTRY */
500 extern Attr_Sequence get_attributes();
501 #endif /* HAVE_PARSE_ENTRY */
505 fprintf( stderr, "convert_entry( 0x%X, \"%s\", 0x%X, \"%s\", ...)\n",
506 fp, edbname, outfp, basedn );
510 while (( dnstr = fgets( linebuf, MAX_LINE_SIZE, fp )) != NULL &&
515 if ( dnstr == NULL ) {
516 return( feof( fp ) ? 0 : -1 ); /* end of file or error */
519 linebuf[ strlen( linebuf ) - 1 ] = '\0';
521 if (( dnstr = malloc( strlen( basedn ) + strlen( linebuf ) + 2 ))
523 print_err( "convert_entry" );
526 sprintf( dnstr, "%s@%s", basedn, linebuf );
527 if ( last_dn != NULL ) {
532 if ( entrycount > 0 ) {
533 fputc( '\n', outfp );
537 * parse_entry, parse_file and parse_rdn are needed inside the
538 * libisode decoding routines, so we set it here.
540 parse_file = edbname;
541 #ifdef HAVE_PARSE_ENTRY
544 parse_rdn = rdn = str2rdn( linebuf );
546 if (( val_ps = ps_alloc( str_open )) == NULLPS ||
547 str_setup( val_ps, NULLCP, 0, 0 ) == NOTOK ) {
548 fprintf( stderr, "%s: ps_alloc/setup failed (EDB file %s)\n", progname,
550 if ( rdn != NULLRDN ) {
556 if (( dn = str2dn( dnstr )) == NULLDN || av2ldif( outfp, NULL, dn,
557 0, "dn", val_ps ) < 0 ) {
559 "str2dn or av2ldif of DN failed (EDB file %s, entry %s)\n",
561 print_err( linebuf );
562 if ( dn != NULLDN ) {
566 if ( rdn != NULLRDN ) {
575 if ( always_addvals != NULL && ( loc_addvals == NULL || !override_add )
576 && fwrite( always_addvals, always_addlen, 1, outfp ) != 1 ) {
578 "write of additional values failed (EDB file %s, entry %s)\n",
580 print_err( linebuf );
582 if ( rdn != NULLRDN ) {
588 if ( loc_addvals != NULL && fwrite( loc_addvals, loc_addlen, 1,
591 "write of additional values failed (EDB file %s, entry %s)\n",
593 print_err( linebuf );
595 if ( rdn != NULLRDN ) {
602 #ifdef HAVE_PARSE_ENTRY
603 as = fget_attributes( fp );
604 #else /* HAVE_PARSE_ENTRY */
605 as = get_attributes( fp );
606 #endif /* HAVE_PARSE_ENTRY */
608 if ( parse_status != 0 ) {
609 fprintf( stderr, "%s: problem parsing entry (EDB file %s)\n", progname,
612 if ( as != NULLATTR ) {
615 if ( rdn != NULLRDN ) {
621 if ( add_rdn_values( as, rdn ) != 0 ) {
623 "adding RDN values(s) failed (EDB file %s, entry %s)\n",
625 print_err( linebuf );
626 if ( as != NULLATTR ) {
629 if ( rdn != NULLRDN ) {
635 if (( attrtype_ps = ps_alloc( str_open )) == NULLPS ||
636 str_setup( attrtype_ps, NULLCP, 0, 0 ) == NOTOK ) {
637 fprintf( stderr, "%s: ps_alloc/setup failed (EDB file %s)\n", progname,
639 if ( as != NULLATTR ) {
642 if ( rdn != NULLRDN ) {
648 for ( tmpas = as; tmpas != NULLATTR; tmpas = tmpas->attr_link ) {
649 attrtype_ps->ps_ptr = attrtype_ps->ps_base;
650 AttrT_print( attrtype_ps, tmpas->attr_type, EDBOUT );
651 *attrtype_ps->ps_ptr = '\0';
653 if ( ignore_attr != NULL ) {
656 for ( i = 0; ignore_attr[ i ] != NULL; ++i ) {
657 if ( strcasecmp( attrtype_ps->ps_base, ignore_attr[ i ] )
662 if ( ignore_attr[ i ] != NULL ) {
663 continue; /* skip this attribute */
668 for ( av = tmpas->attr_value; av != NULLAV; av = av->avseq_next ) {
670 if ( av2ldif( outfp, av, NULL, tmpas->attr_type->oa_syntax,
671 attrtype_ps->ps_base, val_ps ) < 0 ) {
673 "av2ldif failed (EDB file %s, entry %s, attribute %s, value no. %d)\n",
674 edbname, dnstr, attrtype_ps->ps_base, valcnt );
675 print_err( linebuf );
676 ps_free( attrtype_ps );
679 if ( rdn != NULLRDN ) {
687 ps_free( attrtype_ps );
690 if ( rdn != NULLRDN ) {
699 add_rdn_values( entryas, rdn )
700 Attr_Sequence entryas;
704 * this routine is based on code from the real_unravel_attribute() routine
705 * found in isode-8.0/.dsap/common/attribute.c
711 for (; rdn != NULLRDN; rdn = rdn->rdn_next ) {
712 if (( as = as_find_type( entryas, rdn->rdn_at )) == NULLATTR ) {
713 at = AttrT_cpy( rdn->rdn_at );
714 avs = avs_comp_new( AttrV_cpy(&rdn->rdn_av ));
715 as = as_comp_new( at, avs, NULLACL_INFO );
716 entryas = as_merge( entryas, as );
718 for ( avs = as->attr_value; avs != NULLAV; avs = avs->avseq_next ) {
719 if ( AttrV_cmp( &rdn->rdn_av, &avs->avseq_av ) == 0 ) {
724 if ( avs == NULLAV ) {
725 avs = avs_comp_new( AttrV_cpy( &rdn->rdn_av ));
726 as->attr_value = avs_merge( as->attr_value, avs );
735 /* read the EDB.map file and return a linked list of translations */
737 read_edbmap( mapfile, edbmapp )
739 struct edbmap **edbmapp;
742 char *p, *filename, *rdn, line[ MAX_LINE_SIZE + 1 ];
744 struct edbmap *emp, *tmpemp;
748 fprintf( stderr, "read_edbmap( \"%s\", ...)\n", mapfile );
752 if (( fp = fopen( mapfile, "r" )) == NULL ) {
754 return( 0 ); /* soft error -- no EDB.map file */
760 * read all the lines in the file, looking for lines of the form:
764 while ( err == 0 && fgets( line, MAX_LINE_SIZE, fp ) != NULL ) {
765 line[ strlen( line ) - 1 ] = '\0'; /* remove trailing newline */
766 if (( filename = strchr( line, '#' )) == NULL ) {
771 while ( isspace( *filename )) { /* strip leading whitespace */
775 if ( *filename == '\0' ) {
779 p = filename + strlen( filename ) - 1;
780 while ( isspace( *p )) { /* strip trailing whitespace */
785 while ( isspace( *rdn )) { /* strip leading whitespace */
789 if ( *rdn == '\0' ) {
793 p = rdn + strlen( rdn ) - 1;
794 while ( isspace( *p )) { /* strip trailing whitespace */
798 if (( tmpemp = (struct edbmap *)calloc( 1, sizeof( struct edbmap )))
800 ( tmpemp->edbm_filename = strdup( filename )) == NULL ||
801 ( tmpemp->edbm_rdn = strdup( rdn )) == NULL ) {
804 tmpemp->edbm_next = emp;
822 file2rdn( edbmap, filename )
823 struct edbmap *edbmap;
828 fprintf( stderr, "file2rdn( 0x%X, \"%s\" )\n", edbmap, filename );
832 while ( edbmap != NULL ) {
833 if ( strcmp( filename, edbmap->edbm_filename ) == 0 ) {
836 edbmap = edbmap->edbm_next;
839 return(( edbmap == NULL ) ? filename : edbmap->edbm_rdn );
843 /* free the edbmap list */
845 free_edbmap( edbmap )
846 struct edbmap *edbmap;
852 fprintf( stderr, "free_edbmap( 0x%X )\n", edbmap );
856 while ( edbmap != NULL ) {
857 if ( edbmap->edbm_filename != NULL ) free( edbmap->edbm_filename );
858 if ( edbmap->edbm_rdn != NULL ) free( edbmap->edbm_rdn );
860 edbmap = edbmap->edbm_next;
871 extern char *sys_errlist[];
876 fprintf( stderr, "print_err( \"%s\" )\n", msg );
880 if ( errno > sys_nerr ) {
881 fprintf( stderr, "%s: %s: error %d\n", progname, msg, errno );
883 fprintf( stderr, "%s: %s: %s\n", progname, msg, sys_errlist[ errno ] );
889 read_file( char *filename, int *lenp )
897 fprintf( stderr, "read_file( \"%s\", 0x%X )\n", filename, lenp );
901 if ( stat( filename, &st ) != 0 || !S_ISREG( st.st_mode ) ||
902 ( fp = fopen( filename, "r" )) == NULL ) {
906 if (( buf = (char *)malloc( st.st_size )) == NULL ) {
911 if ( fread( buf, st.st_size, 1, fp ) != 1 ) {