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.
17 #include <ac/stdlib.h>
21 #include <ac/dirent.h>
22 #include <ac/string.h>
23 #include <ac/unistd.h>
27 #include <quipu/config.h>
28 #include <quipu/entry.h>
29 #include <quipu/commonarg.h>
30 #include <quipu/attrvalue.h>
33 #define HAVE_FILE_ATTR_DIR
37 #define HAVE_PARSE_ENTRY
40 #define DEF_EDBFILENAME "EDB"
41 #define EDB_ROOT_FILENAME "EDB.root"
43 #define EDBMAP_FILENAME "EDB.map"
44 #define ADDVALS_FILENAME ".add"
46 #define MAX_LINE_SIZE 2048
48 #define VERBOSE_ENTRY_REPORT_THRESHOLD 250
55 struct edbmap *edbm_next;
59 static int edb2ldif( FILE *outfp, char *edbfile, char *basedn, int recurse );
60 static int convert_entry( FILE *fp, char *edbname, FILE *outfp,
61 char *basedn, char *loc_addvals, int loc_addlen, char *linebuf );
62 static int add_rdn_values (Attr_Sequence entryas, RDN rdn);
63 static int read_edbmap( char *mapfile, struct edbmap **edbmapp );
64 static char *file2rdn( struct edbmap *edbmap, char *filename );
65 static void free_edbmap( struct edbmap *edbmap );
66 static char *read_file( char *filename, int *lenp );
67 static void print_err( char *msg );
74 static int verboseflg;
75 static int override_add = 0;
76 static int entrycount;
77 static char **ignore_attr = NULL;
78 static char *always_addvals = NULL;
79 static int always_addlen;
81 static char *edb_home = ".";
84 int ldap_syslog_level = 0;
88 main( int argc, char **argv )
90 char *usage = "usage: %s [-d] [-o] [-r] [-v] [-b basedn] [-a addvalsfile] [-f fileattrdir] [-i ignoreattr...] [edbfile...]\n";
91 char edbfile[ MAXNAMLEN ], *basedn;
92 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:" )) != EOF ) {
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 ))
140 exit( EXIT_FAILURE );
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" );
164 exit( EXIT_FAILURE );
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" );
184 exit( EXIT_FAILURE );
186 ignore_attr[ ignore_count ] = optarg;
187 ignore_attr[ ++ignore_count ] = NULL;
196 fprintf( stderr, usage, progname );
197 exit( EXIT_FAILURE );
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",
216 exit( EXIT_FAILURE );
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 ) ? EXIT_FAILURE : EXIT_SUCCESS );
250 edb2ldif( FILE *outfp, char *edbfile, char *basedn, int 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
483 Attr_Sequence as, tmpas;
485 PS attrtype_ps, val_ps;
490 extern int parse_status;
491 extern char *parse_file;
492 extern RDN parse_rdn;
493 #ifdef HAVE_PARSE_ENTRY
494 extern char *parse_entry;
495 extern Attr_Sequence fget_attributes();
496 #else /* HAVE_PARSE_ENTRY */
497 extern Attr_Sequence get_attributes();
498 #endif /* HAVE_PARSE_ENTRY */
502 fprintf( stderr, "convert_entry( 0x%X, \"%s\", 0x%X, \"%s\", ...)\n",
503 fp, edbname, outfp, basedn );
507 while (( dnstr = fgets( linebuf, MAX_LINE_SIZE, fp )) != NULL &&
512 if ( dnstr == NULL ) {
513 return( feof( fp ) ? 0 : -1 ); /* end of file or error */
516 linebuf[ strlen( linebuf ) - 1 ] = '\0';
518 if (( dnstr = malloc( strlen( basedn ) + strlen( linebuf ) + 2 ))
520 print_err( "convert_entry" );
523 sprintf( dnstr, "%s@%s", basedn, linebuf );
524 if ( last_dn != NULL ) {
529 if ( entrycount > 0 ) {
530 fputc( '\n', outfp );
534 * parse_entry, parse_file and parse_rdn are needed inside the
535 * libisode decoding routines, so we set it here.
537 parse_file = edbname;
538 #ifdef HAVE_PARSE_ENTRY
541 parse_rdn = rdn = str2rdn( linebuf );
543 if (( val_ps = ps_alloc( str_open )) == NULLPS ||
544 str_setup( val_ps, NULLCP, 0, 0 ) == NOTOK ) {
545 fprintf( stderr, "%s: ps_alloc/setup failed (EDB file %s)\n", progname,
547 if ( rdn != NULLRDN ) {
553 if (( dn = str2dn( dnstr )) == NULLDN || av2ldif( outfp, NULL, dn,
554 0, "dn", val_ps ) < 0 ) {
556 "str2dn or av2ldif of DN failed (EDB file %s, entry %s)\n",
558 print_err( linebuf );
559 if ( dn != NULLDN ) {
563 if ( rdn != NULLRDN ) {
572 if ( always_addvals != NULL && ( loc_addvals == NULL || !override_add )
573 && fwrite( always_addvals, always_addlen, 1, outfp ) != 1 ) {
575 "write of additional values failed (EDB file %s, entry %s)\n",
577 print_err( linebuf );
579 if ( rdn != NULLRDN ) {
585 if ( loc_addvals != NULL && fwrite( loc_addvals, loc_addlen, 1,
588 "write of additional values failed (EDB file %s, entry %s)\n",
590 print_err( linebuf );
592 if ( rdn != NULLRDN ) {
599 #ifdef HAVE_PARSE_ENTRY
600 as = fget_attributes( fp );
601 #else /* HAVE_PARSE_ENTRY */
602 as = get_attributes( fp );
603 #endif /* HAVE_PARSE_ENTRY */
605 if ( parse_status != 0 ) {
606 fprintf( stderr, "%s: problem parsing entry (EDB file %s)\n", progname,
609 if ( as != NULLATTR ) {
612 if ( rdn != NULLRDN ) {
618 if ( add_rdn_values( as, rdn ) != 0 ) {
620 "adding RDN values(s) failed (EDB file %s, entry %s)\n",
622 print_err( linebuf );
623 if ( as != NULLATTR ) {
626 if ( rdn != NULLRDN ) {
632 if (( attrtype_ps = ps_alloc( str_open )) == NULLPS ||
633 str_setup( attrtype_ps, NULLCP, 0, 0 ) == NOTOK ) {
634 fprintf( stderr, "%s: ps_alloc/setup failed (EDB file %s)\n", progname,
636 if ( as != NULLATTR ) {
639 if ( rdn != NULLRDN ) {
645 for ( tmpas = as; tmpas != NULLATTR; tmpas = tmpas->attr_link ) {
646 attrtype_ps->ps_ptr = attrtype_ps->ps_base;
647 AttrT_print( attrtype_ps, tmpas->attr_type, EDBOUT );
648 *attrtype_ps->ps_ptr = '\0';
650 if ( ignore_attr != NULL ) {
653 for ( i = 0; ignore_attr[ i ] != NULL; ++i ) {
654 if ( strcasecmp( attrtype_ps->ps_base, ignore_attr[ i ] )
659 if ( ignore_attr[ i ] != NULL ) {
660 continue; /* skip this attribute */
665 for ( av = tmpas->attr_value; av != NULLAV; av = av->avseq_next ) {
667 if ( av2ldif( outfp, av, NULL, tmpas->attr_type->oa_syntax,
668 attrtype_ps->ps_base, val_ps ) < 0 ) {
670 "av2ldif failed (EDB file %s, entry %s, attribute %s, value no. %d)\n",
671 edbname, dnstr, attrtype_ps->ps_base, valcnt );
672 print_err( linebuf );
673 ps_free( attrtype_ps );
676 if ( rdn != NULLRDN ) {
684 ps_free( attrtype_ps );
687 if ( rdn != NULLRDN ) {
696 add_rdn_values( Attr_Sequence entryas, RDN rdn )
699 * this routine is based on code from the real_unravel_attribute() routine
700 * found in isode-8.0/.dsap/common/attribute.c
706 for (; rdn != NULLRDN; rdn = rdn->rdn_next ) {
707 if (( as = as_find_type( entryas, rdn->rdn_at )) == NULLATTR ) {
708 at = AttrT_cpy( rdn->rdn_at );
709 avs = avs_comp_new( AttrV_cpy(&rdn->rdn_av ));
710 as = as_comp_new( at, avs, NULLACL_INFO );
711 entryas = as_merge( entryas, as );
713 for ( avs = as->attr_value; avs != NULLAV; avs = avs->avseq_next ) {
714 if ( AttrV_cmp( &rdn->rdn_av, &avs->avseq_av ) == 0 ) {
719 if ( avs == NULLAV ) {
720 avs = avs_comp_new( AttrV_cpy( &rdn->rdn_av ));
721 as->attr_value = avs_merge( as->attr_value, avs );
730 /* read the EDB.map file and return a linked list of translations */
732 read_edbmap( char *mapfile, struct edbmap **edbmapp )
735 char *p, *filename, *rdn, line[ MAX_LINE_SIZE + 1 ];
737 struct edbmap *emp, *tmpemp;
741 fprintf( stderr, "read_edbmap( \"%s\", ...)\n", mapfile );
745 if (( fp = fopen( mapfile, "r" )) == NULL ) {
747 return( 0 ); /* soft error -- no EDB.map file */
753 * read all the lines in the file, looking for lines of the form:
757 while ( err == 0 && fgets( line, MAX_LINE_SIZE, fp ) != NULL ) {
758 line[ strlen( line ) - 1 ] = '\0'; /* remove trailing newline */
759 if (( filename = strchr( line, '#' )) == NULL ) {
764 while ( isspace((unsigned char) *filename) ) { /* strip leading whitespace */
768 if ( *filename == '\0' ) {
772 p = filename + strlen( filename ) - 1;
773 while ( isspace((unsigned char) *p) ) { /* strip trailing whitespace */
778 while ( isspace((unsigned char) *rdn)) { /* strip leading whitespace */
782 if ( *rdn == '\0' ) {
786 p = rdn + strlen( rdn ) - 1;
787 while ( isspace((unsigned char) *p)) { /* strip trailing whitespace */
791 if (( tmpemp = (struct edbmap *)calloc( 1, sizeof( struct edbmap )))
793 ( tmpemp->edbm_filename = strdup( filename )) == NULL ||
794 ( tmpemp->edbm_rdn = strdup( rdn )) == NULL ) {
797 tmpemp->edbm_next = emp;
815 file2rdn( struct edbmap *edbmap, char *filename )
819 fprintf( stderr, "file2rdn( 0x%X, \"%s\" )\n", edbmap, filename );
823 while ( edbmap != NULL ) {
824 if ( strcmp( filename, edbmap->edbm_filename ) == 0 ) {
827 edbmap = edbmap->edbm_next;
830 return(( edbmap == NULL ) ? filename : edbmap->edbm_rdn );
834 /* free the edbmap list */
836 free_edbmap( struct edbmap *edbmap )
842 fprintf( stderr, "free_edbmap( 0x%X )\n", edbmap );
846 while ( edbmap != NULL ) {
847 if ( edbmap->edbm_filename != NULL ) free( edbmap->edbm_filename );
848 if ( edbmap->edbm_rdn != NULL ) free( edbmap->edbm_rdn );
850 edbmap = edbmap->edbm_next;
857 print_err( char *msg )
861 fprintf( stderr, "print_err( \"%s\" )\n", msg );
865 if ( errno > sys_nerr ) {
866 fprintf( stderr, "%s: %s: errno=%d\n", progname, msg, errno );
868 fprintf( stderr, "%s: %s: %s\n", progname, msg, sys_errlist[ errno ] );
874 read_file( char *filename, int *lenp )
882 fprintf( stderr, "read_file( \"%s\", 0x%X )\n", filename, lenp );
886 if ( stat( filename, &st ) != 0 || !S_ISREG( st.st_mode ) ||
887 ( fp = fopen( filename, "r" )) == NULL ) {
891 if (( buf = (char *)malloc( st.st_size )) == NULL ) {
896 if ( fread( buf, st.st_size, 1, fp ) != 1 ) {