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 <sys/types.h>
23 #include <quipu/config.h>
24 #include <quipu/entry.h>
25 #include <quipu/commonarg.h>
26 #include <quipu/attrvalue.h>
29 #define HAVE_FILE_ATTR_DIR
33 #define HAVE_PARSE_ENTRY
36 #define DEF_EDBFILENAME "EDB"
37 #define EDB_ROOT_FILENAME "EDB.root"
39 #define EDBMAP_FILENAME "EDB.map"
40 #define ADDVALS_FILENAME ".add"
42 #define MAX_LINE_SIZE 2048
44 #define VERBOSE_ENTRY_REPORT_THRESHOLD 250
51 struct edbmap *edbm_next;
55 static int edb2ldif( FILE *outfp, char *edbfile, char *basedn, int recurse );
56 static int convert_entry( FILE *fp, char *edbname, FILE *outfp,
57 char *basedn, char *loc_addvals, int loc_addlen, char *linebuf );
58 static int read_edbmap( char *mapfile, struct edbmap **edbmapp );
59 static char *file2rdn( struct edbmap *edbmap, char *filename );
60 static void free_edbmap( struct edbmap *edbmap );
61 static char *read_file( char *filename, int *lenp );
62 static void print_err( char *msg );
69 static int verboseflg;
70 static int override_add = 0;
71 static int entrycount;
72 static char **ignore_attr = NULL;
73 static char *always_addvals = NULL;
74 static int always_addlen;
76 static char *edb_home = ".";
79 int ldap_syslog_level = 0;
86 char *usage = "usage: %s [-d] [-o] [-r] [-v] [-b basedn] [-a addvalsfile] [-f fileattrdir] [-i ignoreattr...] [edbfile...]\n";
87 char edbfile[ MAXNAMLEN ], *basedn;
88 int c, rc, errflg, ignore_count, recurse;
92 #ifdef HAVE_FILE_ATTR_DIR
93 extern char *file_attr_directory;
96 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
102 errflg = recurse = 0;
107 always_addvals = NULL;
110 while (( c = getopt( argc, argv, "dorva:b:f:h:i:" )) != -1 ) {
116 fprintf( stderr, "Ignoring -d: compile with -DLDAP_DEBUG to enable this option.\n" );
133 if ( always_addvals != NULL ) {
135 } else if (( always_addvals = read_file( optarg, &always_addlen ))
143 if ( basedn != NULL ) {
151 #ifdef HAVE_FILE_ATTR_DIR
152 /* add trailing slash to directory name if missing */
153 if ( *( optarg + strlen( optarg ) - 1 ) == '/' ) {
154 file_attr_directory = strdup( optarg );
155 } else if (( file_attr_directory = (char *)malloc( strlen( optarg )
157 sprintf( file_attr_directory, "%s/", optarg );
160 if ( file_attr_directory == NULL ) {
161 print_err( "malloc" );
164 #else /* HAVE_FILE_ATTR_DIR */
165 fprintf( stderr, "Ignoring -f: this option requires a newer version of ISODE.\n" );
166 #endif /* HAVE_FILE_ATTR_DIR */
174 if ( ignore_count == 0 ) {
175 ignore_attr = (char **)malloc( 2 * sizeof( char * ));
177 ignore_attr = (char **)realloc( ignore_attr,
178 ( ignore_count + 2 ) * sizeof( char * ));
180 if ( ignore_attr == NULL ) {
181 print_err( "malloc/realloc" );
184 ignore_attr[ ignore_count ] = optarg;
185 ignore_attr[ ++ignore_count ] = NULL;
194 fprintf( stderr, usage, progname );
198 if ( basedn == NULL ) {
202 /* load & initialize quipu syntax handlers */
205 pp_quipu_init( progname );
207 dsap_init( NULL, NULL );
209 dsa_mode = 1; /* so {CRYPT} is accepted by EDB parse routines */
211 if ( init_syntaxes() < 0 ) {
212 fprintf( stderr, "%s: init_syntaxes failed -- check your oid tables \n",
220 /* process EDB file(s) */
221 if ( optind >= argc ) {
223 rc = edb2ldif( stdout, edbfile, basedn, recurse );
225 for ( rc = 0; rc >= 0 && optind < argc; ++optind ) {
226 if ( argv[ optind ][ 0 ] == '/' ) {
227 strcpy( edbfile, argv[ optind ] );
229 sprintf( edbfile, "%s/%s", edb_home, argv[ optind ] );
231 rc = edb2ldif( stdout, edbfile, basedn, recurse );
235 if ( last_dn != NULL ) {
240 fprintf( stderr, "edb2ldif: exit( %d )\n", ( rc < 0 ) ? 1 : 0 );
243 exit( ( rc < 0 ) ? 1 : 0 );
248 edb2ldif( outfp, edbfile, basedn, recurse )
255 char *addvals, *p, *rdn, line[ MAX_LINE_SIZE + 1 ];
256 char dirname[ MAXNAMLEN ], filename[ MAXNAMLEN ];
257 int err, startcount, addvals_len;
262 fprintf( stderr, "edb2ldif( 0x%X, \"%s\", \"%s\", %d)\n",
263 outfp, edbfile, basedn, recurse );
267 if ( *edbfile == '\0' ) {
268 sprintf( filename, "%s/%s", edb_home, EDB_ROOT_FILENAME );
269 if ( stat( filename, &st ) == 0 ) {
270 if (( err = edb2ldif( outfp, filename, basedn, 0 )) < 0 ) {
273 fprintf( stderr, "edb2ldif: 0 return( %d )\n", err );
278 if (( basedn = strdup( last_dn )) == NULL ) {
279 print_err( "strdup" );
282 fprintf( stderr, "edb2ldif: 1 return( -1 )\n" );
288 sprintf( edbfile, "%s/%s", edb_home, DEF_EDBFILENAME );
292 fprintf( stderr, "%s: converting EDB file: \"%s\"\n\tbasedn: \"%s\"\n",
293 progname, edbfile, basedn );
296 startcount = entrycount;
300 /* construct name of directory we are working in */
301 if (( p = strrchr( edbfile, '/' )) == NULL ) {
305 strncpy( dirname, edbfile, p - edbfile );
306 dirname[ p - edbfile ] = '\0';
309 /* load local ".add" file (if any) */
310 sprintf( filename, "%s/%s", dirname, ADDVALS_FILENAME );
312 addvals = read_file( filename, &addvals_len );
314 /* read and convert this EDB file */
315 if (( fp = fopen( edbfile, "r" )) == NULL ) {
316 print_err( edbfile );
317 if ( addvals != NULL ) {
322 fprintf( stderr, "edb2ldif: 2 return( -1 )\n" );
328 /* skip first two lines (type and timestamp) if they are present */
329 if ( fgets( line, MAX_LINE_SIZE, fp ) == NULL ) {
332 line[ strlen( line ) - 1 ] = '\0';
333 if ( strcmp( line, "MASTER" ) == 0 || strcmp( line, "SLAVE" ) == 0 ||
334 strcmp( line, "CACHE" ) == 0 ) {
335 if ( fgets( line, MAX_LINE_SIZE, fp ) == NULL ) {
344 fprintf( stderr, "%s: skipping empty EDB file %s\n", progname,
346 err = 0; /* treat as a non-fatal error */
348 while ( !feof( fp ) && ( err = convert_entry( fp, edbfile, outfp,
349 basedn, addvals, addvals_len, line )) > 0 ) {
350 if ( verboseflg && (( entrycount - startcount ) %
351 VERBOSE_ENTRY_REPORT_THRESHOLD ) == 0 ) {
352 fprintf( stderr, "\tworking... %d entries done...\n",
353 entrycount - startcount );
359 if ( addvals != NULL ) {
366 fprintf( stderr, "edb2ldif: 3 return( %d )\n", err );
373 fprintf( stderr, "\t%d entries converted\n\n",
374 entrycount - startcount );
377 /* optionally convert EDB file within sub-directories */
382 struct edbmap *edbmap;
384 /* open this directory */
385 if (( dp = opendir( dirname )) == NULL ) {
386 print_err( dirname );
389 fprintf( stderr, "edb2ldif: 4 return( -1 )\n" );
395 /* check for EDB.map file and record contents for future reference */
396 sprintf( filename, "%s/%s", dirname, EDBMAP_FILENAME );
397 if ( read_edbmap( filename, &edbmap ) < 0 ) {
398 print_err( "read_edbmap" );
402 fprintf( stderr, "edb2ldif: 5 return( -1 )\n" );
408 p = dirname + strlen( dirname );
412 /* scan looking for sub-directories w/EDB files in them */
414 while ( err >= 0 && ( dep = readdir( dp )) != NULL ) {
415 if ( dep->d_name[ 0 ] == '.' && ( dep->d_name[ 1 ] == '\0' ||
416 ( dep->d_name[ 1 ] == '.' && dep->d_name[ 2 ] == '\0' ))) {
417 continue; /* skip "." and ".." */
420 strcpy( p, dep->d_name );
423 fprintf( stderr, "edb2ldif: checking directory \"%s\"\n",
428 if ( stat( dirname, &st ) != 0 ) {
429 print_err( dirname );
430 } else if ( S_ISDIR( st.st_mode )) {
431 sprintf( filename, "%s/%s", dirname, DEF_EDBFILENAME );
433 if ( stat( filename, &st ) == 0 && S_ISREG( st.st_mode )) {
434 if (( newbase = malloc( strlen( basedn ) +
435 strlen( dep->d_name ) + 3 )) == NULL ) {
436 print_err( "malloc" );
441 sprintf( newbase, "%s@%s", basedn,
442 file2rdn( edbmap, dep->d_name ));
445 err = edb2ldif( outfp, filename, newbase, recurse );
452 free_edbmap( edbmap );
456 fprintf( stderr, "%s: %d total entries converted under \"%s\"\n\n",
457 progname, entrycount - startcount, basedn );
463 fprintf( stderr, "edb2ldif: 6 return( %d )\n", err );
471 * read one entry from fp and write to outfp.
472 * return > 0 if entry converted, 0 if end of file, < 0 if error occurs
475 convert_entry( fp, edbname, outfp, basedn, loc_addvals, loc_addlen, linebuf )
484 Attr_Sequence as, tmpas;
486 PS attrtype_ps, val_ps;
491 extern int parse_status;
492 extern char *parse_file;
493 extern RDN parse_rdn;
494 #ifdef HAVE_PARSE_ENTRY
495 extern char *parse_entry;
496 extern Attr_Sequence fget_attributes();
497 #else /* HAVE_PARSE_ENTRY */
498 extern Attr_Sequence get_attributes();
499 #endif /* HAVE_PARSE_ENTRY */
503 fprintf( stderr, "convert_entry( 0x%X, \"%s\", 0x%X, \"%s\", ...)\n",
504 fp, edbname, outfp, basedn );
508 while (( dnstr = fgets( linebuf, MAX_LINE_SIZE, fp )) != NULL &&
513 if ( dnstr == NULL ) {
514 return( feof( fp ) ? 0 : -1 ); /* end of file or error */
517 linebuf[ strlen( linebuf ) - 1 ] = '\0';
519 if (( dnstr = malloc( strlen( basedn ) + strlen( linebuf ) + 2 ))
521 print_err( "convert_entry" );
524 sprintf( dnstr, "%s@%s", basedn, linebuf );
525 if ( last_dn != NULL ) {
530 if ( entrycount > 0 ) {
531 fputc( '\n', outfp );
535 * parse_entry, parse_file and parse_rdn are needed inside the
536 * libisode decoding routines, so we set it here.
538 parse_file = edbname;
539 #ifdef HAVE_PARSE_ENTRY
542 parse_rdn = rdn = str2rdn( linebuf );
544 if (( val_ps = ps_alloc( str_open )) == NULLPS ||
545 str_setup( val_ps, NULLCP, 0, 0 ) == NOTOK ) {
546 fprintf( stderr, "%s: ps_alloc/setup failed (EDB file %s)\n", progname,
548 if ( rdn != NULLRDN ) {
554 if (( dn = str2dn( dnstr )) == NULLDN || av2ldif( outfp, NULL, dn,
555 0, "dn", val_ps ) < 0 ) {
557 "str2dn or av2ldif of DN failed (EDB file %s, entry %s)\n",
559 print_err( linebuf );
560 if ( dn != NULLDN ) {
564 if ( rdn != NULLRDN ) {
573 if ( always_addvals != NULL && ( loc_addvals == NULL || !override_add )
574 && fwrite( always_addvals, always_addlen, 1, outfp ) != 1 ) {
576 "write of additional values failed (EDB file %s, entry %s)\n",
578 print_err( linebuf );
580 if ( rdn != NULLRDN ) {
586 if ( loc_addvals != NULL && fwrite( loc_addvals, loc_addlen, 1,
589 "write of additional values failed (EDB file %s, entry %s)\n",
591 print_err( linebuf );
593 if ( rdn != NULLRDN ) {
600 #ifdef HAVE_PARSE_ENTRY
601 as = fget_attributes( fp );
602 #else /* HAVE_PARSE_ENTRY */
603 as = get_attributes( fp );
604 #endif /* HAVE_PARSE_ENTRY */
606 if ( parse_status != 0 ) {
607 fprintf( stderr, "%s: problem parsing entry (EDB file %s)\n", progname,
610 if ( as != NULLATTR ) {
613 if ( rdn != NULLRDN ) {
619 if ( add_rdn_values( as, rdn ) != 0 ) {
621 "adding RDN values(s) failed (EDB file %s, entry %s)\n",
623 print_err( linebuf );
624 if ( as != NULLATTR ) {
627 if ( rdn != NULLRDN ) {
633 if (( attrtype_ps = ps_alloc( str_open )) == NULLPS ||
634 str_setup( attrtype_ps, NULLCP, 0, 0 ) == NOTOK ) {
635 fprintf( stderr, "%s: ps_alloc/setup failed (EDB file %s)\n", progname,
637 if ( as != NULLATTR ) {
640 if ( rdn != NULLRDN ) {
646 for ( tmpas = as; tmpas != NULLATTR; tmpas = tmpas->attr_link ) {
647 attrtype_ps->ps_ptr = attrtype_ps->ps_base;
648 AttrT_print( attrtype_ps, tmpas->attr_type, EDBOUT );
649 *attrtype_ps->ps_ptr = '\0';
651 if ( ignore_attr != NULL ) {
654 for ( i = 0; ignore_attr[ i ] != NULL; ++i ) {
655 if ( strcasecmp( attrtype_ps->ps_base, ignore_attr[ i ] )
660 if ( ignore_attr[ i ] != NULL ) {
661 continue; /* skip this attribute */
666 for ( av = tmpas->attr_value; av != NULLAV; av = av->avseq_next ) {
668 if ( av2ldif( outfp, av, NULL, tmpas->attr_type->oa_syntax,
669 attrtype_ps->ps_base, val_ps ) < 0 ) {
671 "av2ldif failed (EDB file %s, entry %s, attribute %s, value no. %d)\n",
672 edbname, dnstr, attrtype_ps->ps_base, valcnt );
673 print_err( linebuf );
674 ps_free( attrtype_ps );
677 if ( rdn != NULLRDN ) {
685 ps_free( attrtype_ps );
688 if ( rdn != NULLRDN ) {
697 add_rdn_values( entryas, rdn )
698 Attr_Sequence entryas;
702 * this routine is based on code from the real_unravel_attribute() routine
703 * found in isode-8.0/.dsap/common/attribute.c
709 for (; rdn != NULLRDN; rdn = rdn->rdn_next ) {
710 if (( as = as_find_type( entryas, rdn->rdn_at )) == NULLATTR ) {
711 at = AttrT_cpy( rdn->rdn_at );
712 avs = avs_comp_new( AttrV_cpy(&rdn->rdn_av ));
713 as = as_comp_new( at, avs, NULLACL_INFO );
714 entryas = as_merge( entryas, as );
716 for ( avs = as->attr_value; avs != NULLAV; avs = avs->avseq_next ) {
717 if ( AttrV_cmp( &rdn->rdn_av, &avs->avseq_av ) == 0 ) {
722 if ( avs == NULLAV ) {
723 avs = avs_comp_new( AttrV_cpy( &rdn->rdn_av ));
724 as->attr_value = avs_merge( as->attr_value, avs );
733 /* read the EDB.map file and return a linked list of translations */
735 read_edbmap( mapfile, edbmapp )
737 struct edbmap **edbmapp;
740 char *p, *filename, *rdn, line[ MAX_LINE_SIZE + 1 ];
742 struct edbmap *emp, *tmpemp;
746 fprintf( stderr, "read_edbmap( \"%s\", ...)\n", mapfile );
750 if (( fp = fopen( mapfile, "r" )) == NULL ) {
752 return( 0 ); /* soft error -- no EDB.map file */
758 * read all the lines in the file, looking for lines of the form:
762 while ( err == 0 && fgets( line, MAX_LINE_SIZE, fp ) != NULL ) {
763 line[ strlen( line ) - 1 ] = '\0'; /* remove trailing newline */
764 if (( filename = strchr( line, '#' )) == NULL ) {
769 while ( isspace( *filename )) { /* strip leading whitespace */
773 if ( *filename == '\0' ) {
777 p = filename + strlen( filename ) - 1;
778 while ( isspace( *p )) { /* strip trailing whitespace */
783 while ( isspace( *rdn )) { /* strip leading whitespace */
787 if ( *rdn == '\0' ) {
791 p = rdn + strlen( rdn ) - 1;
792 while ( isspace( *p )) { /* strip trailing whitespace */
796 if (( tmpemp = (struct edbmap *)calloc( 1, sizeof( struct edbmap )))
798 ( tmpemp->edbm_filename = strdup( filename )) == NULL ||
799 ( tmpemp->edbm_rdn = strdup( rdn )) == NULL ) {
802 tmpemp->edbm_next = emp;
820 file2rdn( edbmap, filename )
821 struct edbmap *edbmap;
826 fprintf( stderr, "file2rdn( 0x%X, \"%s\" )\n", edbmap, filename );
830 while ( edbmap != NULL ) {
831 if ( strcmp( filename, edbmap->edbm_filename ) == 0 ) {
834 edbmap = edbmap->edbm_next;
837 return(( edbmap == NULL ) ? filename : edbmap->edbm_rdn );
841 /* free the edbmap list */
843 free_edbmap( edbmap )
844 struct edbmap *edbmap;
850 fprintf( stderr, "free_edbmap( 0x%X )\n", edbmap );
854 while ( edbmap != NULL ) {
855 if ( edbmap->edbm_filename != NULL ) free( edbmap->edbm_filename );
856 if ( edbmap->edbm_rdn != NULL ) free( edbmap->edbm_rdn );
858 edbmap = edbmap->edbm_next;
869 extern char *sys_errlist[];
874 fprintf( stderr, "print_err( \"%s\" )\n", msg );
878 if ( errno > sys_nerr ) {
879 fprintf( stderr, "%s: %s: error %d\n", progname, msg, errno );
881 fprintf( stderr, "%s: %s: %s\n", progname, msg, sys_errlist[ errno ] );
887 read_file( char *filename, int *lenp )
895 fprintf( stderr, "read_file( \"%s\", 0x%X )\n", filename, lenp );
899 if ( stat( filename, &st ) != 0 || !S_ISREG( st.st_mode ) ||
900 ( fp = fopen( filename, "r" )) == NULL ) {
904 if (( buf = (char *)malloc( st.st_size )) == NULL ) {
909 if ( fread( buf, st.st_size, 1, fp ) != 1 ) {