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.
18 #include <sys/types.h>
21 #include <quipu/config.h>
22 #include <quipu/entry.h>
23 #include <quipu/commonarg.h>
24 #include <quipu/attrvalue.h>
27 #define HAVE_FILE_ATTR_DIR
31 #define HAVE_PARSE_ENTRY
34 #define DEF_EDBFILENAME "EDB"
35 #define EDB_ROOT_FILENAME "EDB.root"
37 #define EDBMAP_FILENAME "EDB.map"
38 #define ADDVALS_FILENAME ".add"
40 #define MAX_LINE_SIZE 2048
42 #define VERBOSE_ENTRY_REPORT_THRESHOLD 250
49 struct edbmap *edbm_next;
53 static int edb2ldif( FILE *outfp, char *edbfile, char *basedn, int recurse );
54 static int convert_entry( FILE *fp, char *edbname, FILE *outfp,
55 char *basedn, char *loc_addvals, int loc_addlen, char *linebuf );
56 static int read_edbmap( char *mapfile, struct edbmap **edbmapp );
57 static char *file2rdn( struct edbmap *edbmap, char *filename );
58 static void free_edbmap( struct edbmap *edbmap );
59 static char *read_file( char *filename, int *lenp );
60 static void print_err( char *msg );
67 static int verboseflg;
68 static int override_add = 0;
69 static int entrycount;
70 static char **ignore_attr = NULL;
71 static char *always_addvals = NULL;
72 static int always_addlen;
74 static char *edb_home = ".";
77 int ldap_syslog_level = 0;
84 char *usage = "usage: %s [-d] [-o] [-r] [-v] [-b basedn] [-a addvalsfile] [-f fileattrdir] [-i ignoreattr...] [edbfile...]\n";
85 char edbfile[ MAXNAMLEN ], *basedn;
86 int c, rc, errflg, ignore_count, recurse;
90 #ifdef HAVE_FILE_ATTR_DIR
91 extern char *file_attr_directory;
94 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
100 errflg = recurse = 0;
105 always_addvals = NULL;
108 while (( c = getopt( argc, argv, "dorva:b:f:h:i:" )) != -1 ) {
114 fprintf( stderr, "Ignoring -d: compile with -DLDAP_DEBUG to enable this option.\n" );
131 if ( always_addvals != NULL ) {
133 } else if (( always_addvals = read_file( optarg, &always_addlen ))
141 if ( basedn != NULL ) {
149 #ifdef HAVE_FILE_ATTR_DIR
150 /* add trailing slash to directory name if missing */
151 if ( *( optarg + strlen( optarg ) - 1 ) == '/' ) {
152 file_attr_directory = strdup( optarg );
153 } else if (( file_attr_directory = (char *)malloc( strlen( optarg )
155 sprintf( file_attr_directory, "%s/", optarg );
158 if ( file_attr_directory == NULL ) {
159 print_err( "malloc" );
162 #else /* HAVE_FILE_ATTR_DIR */
163 fprintf( stderr, "Ignoring -f: this option requires a newer version of ISODE.\n" );
164 #endif /* HAVE_FILE_ATTR_DIR */
172 if ( ignore_count == 0 ) {
173 ignore_attr = (char **)malloc( 2 * sizeof( char * ));
175 ignore_attr = (char **)realloc( ignore_attr,
176 ( ignore_count + 2 ) * sizeof( char * ));
178 if ( ignore_attr == NULL ) {
179 print_err( "malloc/realloc" );
182 ignore_attr[ ignore_count ] = optarg;
183 ignore_attr[ ++ignore_count ] = NULL;
192 fprintf( stderr, usage, progname );
196 if ( basedn == NULL ) {
200 /* load & initialize quipu syntax handlers */
203 pp_quipu_init( progname );
205 dsap_init( NULL, NULL );
207 dsa_mode = 1; /* so {CRYPT} is accepted by EDB parse routines */
209 if ( init_syntaxes() < 0 ) {
210 fprintf( stderr, "%s: init_syntaxes failed -- check your oid tables \n",
218 /* process EDB file(s) */
219 if ( optind >= argc ) {
221 rc = edb2ldif( stdout, edbfile, basedn, recurse );
223 for ( rc = 0; rc >= 0 && optind < argc; ++optind ) {
224 if ( argv[ optind ][ 0 ] == '/' ) {
225 strcpy( edbfile, argv[ optind ] );
227 sprintf( edbfile, "%s/%s", edb_home, argv[ optind ] );
229 rc = edb2ldif( stdout, edbfile, basedn, recurse );
233 if ( last_dn != NULL ) {
238 fprintf( stderr, "edb2ldif: exit( %d )\n", ( rc < 0 ) ? 1 : 0 );
241 exit( ( rc < 0 ) ? 1 : 0 );
246 edb2ldif( outfp, edbfile, basedn, recurse )
253 char *addvals, *p, *rdn, line[ MAX_LINE_SIZE + 1 ];
254 char dirname[ MAXNAMLEN ], filename[ MAXNAMLEN ];
255 int err, startcount, addvals_len;
260 fprintf( stderr, "edb2ldif( 0x%X, \"%s\", \"%s\", %d)\n",
261 outfp, edbfile, basedn, recurse );
265 if ( *edbfile == '\0' ) {
266 sprintf( filename, "%s/%s", edb_home, EDB_ROOT_FILENAME );
267 if ( stat( filename, &st ) == 0 ) {
268 if (( err = edb2ldif( outfp, filename, basedn, 0 )) < 0 ) {
271 fprintf( stderr, "edb2ldif: 0 return( %d )\n", err );
276 if (( basedn = strdup( last_dn )) == NULL ) {
277 print_err( "strdup" );
280 fprintf( stderr, "edb2ldif: 1 return( -1 )\n" );
286 sprintf( edbfile, "%s/%s", edb_home, DEF_EDBFILENAME );
290 fprintf( stderr, "%s: converting EDB file: \"%s\"\n\tbasedn: \"%s\"\n",
291 progname, edbfile, basedn );
294 startcount = entrycount;
298 /* construct name of directory we are working in */
299 if (( p = strrchr( edbfile, '/' )) == NULL ) {
303 strncpy( dirname, edbfile, p - edbfile );
304 dirname[ p - edbfile ] = '\0';
307 /* load local ".add" file (if any) */
308 sprintf( filename, "%s/%s", dirname, ADDVALS_FILENAME );
310 addvals = read_file( filename, &addvals_len );
312 /* read and convert this EDB file */
313 if (( fp = fopen( edbfile, "r" )) == NULL ) {
314 print_err( edbfile );
315 if ( addvals != NULL ) {
320 fprintf( stderr, "edb2ldif: 2 return( -1 )\n" );
326 /* skip first two lines (type and timestamp) if they are present */
327 if ( fgets( line, MAX_LINE_SIZE, fp ) == NULL ) {
330 line[ strlen( line ) - 1 ] = '\0';
331 if ( strcmp( line, "MASTER" ) == 0 || strcmp( line, "SLAVE" ) == 0 ||
332 strcmp( line, "CACHE" ) == 0 ) {
333 if ( fgets( line, MAX_LINE_SIZE, fp ) == NULL ) {
342 fprintf( stderr, "%s: skipping empty EDB file %s\n", progname,
344 err = 0; /* treat as a non-fatal error */
346 while ( !feof( fp ) && ( err = convert_entry( fp, edbfile, outfp,
347 basedn, addvals, addvals_len, line )) > 0 ) {
348 if ( verboseflg && (( entrycount - startcount ) %
349 VERBOSE_ENTRY_REPORT_THRESHOLD ) == 0 ) {
350 fprintf( stderr, "\tworking... %d entries done...\n",
351 entrycount - startcount );
357 if ( addvals != NULL ) {
364 fprintf( stderr, "edb2ldif: 3 return( %d )\n", err );
371 fprintf( stderr, "\t%d entries converted\n\n",
372 entrycount - startcount );
375 /* optionally convert EDB file within sub-directories */
380 struct edbmap *edbmap;
382 /* open this directory */
383 if (( dp = opendir( dirname )) == NULL ) {
384 print_err( dirname );
387 fprintf( stderr, "edb2ldif: 4 return( -1 )\n" );
393 /* check for EDB.map file and record contents for future reference */
394 sprintf( filename, "%s/%s", dirname, EDBMAP_FILENAME );
395 if ( read_edbmap( filename, &edbmap ) < 0 ) {
396 print_err( "read_edbmap" );
400 fprintf( stderr, "edb2ldif: 5 return( -1 )\n" );
406 p = dirname + strlen( dirname );
410 /* scan looking for sub-directories w/EDB files in them */
412 while ( err >= 0 && ( dep = readdir( dp )) != NULL ) {
413 if ( dep->d_name[ 0 ] == '.' && ( dep->d_name[ 1 ] == '\0' ||
414 ( dep->d_name[ 1 ] == '.' && dep->d_name[ 2 ] == '\0' ))) {
415 continue; /* skip "." and ".." */
418 strcpy( p, dep->d_name );
421 fprintf( stderr, "edb2ldif: checking directory \"%s\"\n",
426 if ( stat( dirname, &st ) != 0 ) {
427 print_err( dirname );
428 } else if ( S_ISDIR( st.st_mode )) {
429 sprintf( filename, "%s/%s", dirname, DEF_EDBFILENAME );
431 if ( stat( filename, &st ) == 0 && S_ISREG( st.st_mode )) {
432 if (( newbase = malloc( strlen( basedn ) +
433 strlen( dep->d_name ) + 3 )) == NULL ) {
434 print_err( "malloc" );
439 sprintf( newbase, "%s@%s", basedn,
440 file2rdn( edbmap, dep->d_name ));
443 err = edb2ldif( outfp, filename, newbase, recurse );
450 free_edbmap( edbmap );
454 fprintf( stderr, "%s: %d total entries converted under \"%s\"\n\n",
455 progname, entrycount - startcount, basedn );
461 fprintf( stderr, "edb2ldif: 6 return( %d )\n", err );
469 * read one entry from fp and write to outfp.
470 * return > 0 if entry converted, 0 if end of file, < 0 if error occurs
473 convert_entry( fp, edbname, outfp, basedn, loc_addvals, loc_addlen, linebuf )
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( entryas, rdn )
696 Attr_Sequence entryas;
700 * this routine is based on code from the real_unravel_attribute() routine
701 * found in isode-8.0/.dsap/common/attribute.c
707 for (; rdn != NULLRDN; rdn = rdn->rdn_next ) {
708 if (( as = as_find_type( entryas, rdn->rdn_at )) == NULLATTR ) {
709 at = AttrT_cpy( rdn->rdn_at );
710 avs = avs_comp_new( AttrV_cpy(&rdn->rdn_av ));
711 as = as_comp_new( at, avs, NULLACL_INFO );
712 entryas = as_merge( entryas, as );
714 for ( avs = as->attr_value; avs != NULLAV; avs = avs->avseq_next ) {
715 if ( AttrV_cmp( &rdn->rdn_av, &avs->avseq_av ) == 0 ) {
720 if ( avs == NULLAV ) {
721 avs = avs_comp_new( AttrV_cpy( &rdn->rdn_av ));
722 as->attr_value = avs_merge( as->attr_value, avs );
731 /* read the EDB.map file and return a linked list of translations */
733 read_edbmap( mapfile, edbmapp )
735 struct edbmap **edbmapp;
738 char *p, *filename, *rdn, line[ MAX_LINE_SIZE + 1 ];
740 struct edbmap *emp, *tmpemp;
744 fprintf( stderr, "read_edbmap( \"%s\", ...)\n", mapfile );
748 if (( fp = fopen( mapfile, "r" )) == NULL ) {
750 return( 0 ); /* soft error -- no EDB.map file */
756 * read all the lines in the file, looking for lines of the form:
760 while ( err == 0 && fgets( line, MAX_LINE_SIZE, fp ) != NULL ) {
761 line[ strlen( line ) - 1 ] = '\0'; /* remove trailing newline */
762 if (( filename = strchr( line, '#' )) == NULL ) {
767 while ( isspace( *filename )) { /* strip leading whitespace */
771 if ( *filename == '\0' ) {
775 p = filename + strlen( filename ) - 1;
776 while ( isspace( *p )) { /* strip trailing whitespace */
781 while ( isspace( *rdn )) { /* strip leading whitespace */
785 if ( *rdn == '\0' ) {
789 p = rdn + strlen( rdn ) - 1;
790 while ( isspace( *p )) { /* strip trailing whitespace */
794 if (( tmpemp = (struct edbmap *)calloc( 1, sizeof( struct edbmap )))
796 ( tmpemp->edbm_filename = strdup( filename )) == NULL ||
797 ( tmpemp->edbm_rdn = strdup( rdn )) == NULL ) {
800 tmpemp->edbm_next = emp;
818 file2rdn( edbmap, filename )
819 struct edbmap *edbmap;
824 fprintf( stderr, "file2rdn( 0x%X, \"%s\" )\n", edbmap, filename );
828 while ( edbmap != NULL ) {
829 if ( strcmp( filename, edbmap->edbm_filename ) == 0 ) {
832 edbmap = edbmap->edbm_next;
835 return(( edbmap == NULL ) ? filename : edbmap->edbm_rdn );
839 /* free the edbmap list */
841 free_edbmap( edbmap )
842 struct edbmap *edbmap;
848 fprintf( stderr, "free_edbmap( 0x%X )\n", edbmap );
852 while ( edbmap != NULL ) {
853 if ( edbmap->edbm_filename != NULL ) free( edbmap->edbm_filename );
854 if ( edbmap->edbm_rdn != NULL ) free( edbmap->edbm_rdn );
856 edbmap = edbmap->edbm_next;
867 extern char *sys_errlist[];
872 fprintf( stderr, "print_err( \"%s\" )\n", msg );
876 if ( errno > sys_nerr ) {
877 fprintf( stderr, "%s: %s: error %d\n", progname, msg, errno );
879 fprintf( stderr, "%s: %s: %s\n", progname, msg, sys_errlist[ errno ] );
885 read_file( char *filename, int *lenp )
893 fprintf( stderr, "read_file( \"%s\", 0x%X )\n", filename, lenp );
897 if ( stat( filename, &st ) != 0 || !S_ISREG( st.st_mode ) ||
898 ( fp = fopen( filename, "r" )) == NULL ) {
902 if (( buf = (char *)malloc( st.st_size )) == NULL ) {
907 if ( fread( buf, st.st_size, 1, fp ) != 1 ) {