2 * Copyright (c) 1990, 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.
14 * chlog2replog - read a quipu-style changelog on stdin and write a
15 * slapd-style replog on stdout, or write to a file, respecting
16 * slapd/slurpd locking conventions.
25 #include <ac/string.h>
27 #include <quipu/commonarg.h>
28 #include <quipu/attrvalue.h>
35 extern FILE *lock_fopen( char *, char *, FILE ** );
36 extern int lock_fclose( FILE *, FILE * );
37 extern void *ch_realloc( void *, unsigned long );
43 int ldap_syslog_level = 0;
55 #define TY_MODIFYTYPE 1
60 #define TY_MODIFYARGS 6
62 #define MOD_ADDVALUES 1
63 #define MOD_ADDATTRIBUTE 2
64 #define MOD_REMOVEATTRIBUTE 3
65 #define MOD_REMOVEVALUES 4
69 dn2ldap( char *edbdn )
75 static int inited = 0;
78 /* load & initialize quipu syntax handlers */
82 pp_quipu_init( progname );
85 dsap_init( NULL, NULL );
87 if (( ldap_dn_syntax = str2syntax( "DN" )) == 0 ) {
93 if (( dn = str2dn( edbdn )) == NULLDN ) {
97 if (( str_ps = ps_alloc( str_open )) == NULLPS ||
98 str_setup( str_ps, NULLCP, 0, 0 ) == NOTOK ) {
103 if ( dn2ldif( str_ps, dn ) != 0 ) {
110 len = ( str_ps->ps_ptr - str_ps->ps_base );
112 if (( ldapdn = malloc( len + 1 )) == NULL ) {
117 memcpy( ldapdn, str_ps->ps_base, len );
118 ldapdn[ len ] = '\0';
124 #define SEPARATOR(c) (c == ',' || c == ';')
125 #define SPACE(c) (c == ' ' || c == '\n')
128 dn2ldif( PS ps, DN dn )
135 if ( dn == NULLDN ) {
139 if ( dn->dn_parent != NULLDN ) {
140 if (( rc = dn2ldif( ps, dn->dn_parent )) != 0 ) {
143 ps_print( ps, ", " );
146 if ( (rps = ps_alloc( str_open )) == NULLPS ||
147 str_setup( rps, NULLCP, 0, 0 ) == NOTOK ) {
152 for ( rdn = dn->dn_rdn; rdn != NULLRDN; rdn = rdn->rdn_next ) {
156 ps_print( ps, " + " );
159 AttrT_print( ps, rdn->rdn_at, EDBOUT );
162 if ( rdn->rdn_at->oa_syntax == ldap_dn_syntax ) {
163 if (( rc = dn2ldif( rps, (DN) rdn->rdn_av.av_struct )) != 0 ) {
167 value = rps->ps_base;
169 AttrV_print( rps, &rdn->rdn_av, EDBOUT );
171 value = rps->ps_base;
176 * ,+="\\\n all go in quotes. " and \\ need to
177 * be preceeded by \\.
180 if ( strpbrk( value, ",+=\"\\\n" ) != NULL || SPACE( value[0] )
181 || SPACE( value[max( strlen(value) - 1, 0 )] ) ) {
185 ps_print( ps, "\"" );
188 for ( p = value; *p != '\0'; p++ ) {
189 if ( *p == '"' || *p == '\\' ) {
193 if ( specialcount > 0 ) {
194 tmp = smalloc( strlen( value ) + specialcount + 1 );
195 for ( p = value, t = tmp; *p != '\0'; p++ ) {
209 ps_print( ps, value );
212 ps_print( ps, "\"" );
214 ps_print( ps, value );
217 rps->ps_ptr = rps->ps_base;
241 if ( strncasecmp( s, T61, T61LEN) == 0 ) {
260 else if ( c >= 'A' && c <= 'F' )
262 else if ( c >= 'a' && c <= 'f' )
272 else if ( c >= 'A' && c <= 'F' )
274 else if ( c >= 'a' && c <= 'f' )
304 static char retbuf[ RBSIZE ];
306 if (( val = strchr( buf, sep )) != NULL ) {
307 strncpy( retbuf, buf, val - buf );
308 retbuf[ val - buf ] = '\0';
320 return( getattr( buf, ':' ));
328 return( getattr( buf, '=' ));
338 if (( val = strchr( buf, sep )) != NULL ) {
339 return( strdup( ++val ));
349 return( getval( buf, ':' ));
357 return( getval( buf, '=' ));
367 oid_table_attr *p, *name2attr();
369 p = name2attr( attr );
370 if ( p == ( oid_table_attr * ) 0 ) {
373 if ( p->oa_syntax == ldap_dn_syntax ) {
383 print_as( as, modtype, ofp )
390 char *attrname, *tmpdn, *obuf;
393 for ( p = as; p != NULLATTR; p = p->attr_link) {
394 rps->ps_ptr = rps->ps_base;
395 AttrT_print( rps, p->attr_type, EDBOUT );
397 attrname = strdup( rps->ps_base );
398 if ( modtype != 0 ) {
401 case MOD_ADDATTRIBUTE:
402 fprintf( ofp, "add: %s\n", attrname );
404 case MOD_REMOVEATTRIBUTE:
405 case MOD_REMOVEVALUES:
406 fprintf( ofp, "delete: %s\n", attrname );
412 for ( av = p->attr_value; av != NULLAV; av = av->avseq_next ) {
413 rps->ps_ptr = rps->ps_base;
414 AttrV_print( rps, &av->avseq_av, EDBOUT );
416 de_t61( rps->ps_base, 0 );
417 if ( isDNsyntax( attrname )) {
418 tmpdn = dn2ldap( rps->ps_base );
419 obuf = ldif_type_and_value( attrname, tmpdn,
423 obuf = ldif_type_and_value( attrname, rps->ps_base,
424 strlen( rps->ps_base ));
426 if ( obuf != NULL ) {
431 if ( modtype != 0 ) {
432 fprintf( ofp, "-\n" );
443 fprintf( stderr, "usage: %s -d dn-suffix -r replica:port ", name );
444 fprintf( stderr, "[-r replica:port...] [-o outputfile]\n" );
449 main( int argc, char **argv )
451 char *ldapdn, nbuf[ 4096 ], *buf, *p;
452 int state, prevstate, modstate, modtype, i;
461 char **replicas = NULL;
463 char *dn_suffix = NULL;
465 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
466 progname = argv[ 0 ];
471 while (( arg = getopt( argc, argv, "o:r:d:" )) != EOF ) {
477 replicas = (char **) ch_realloc( (char *) replicas, (unsigned long)
478 ( nreplicas + 2 ) * sizeof( char * ));
479 replicas[ nreplicas ] = optarg;
480 replicas[ nreplicas + 1 ] = NULL;
492 if (( dn_suffix == NULL ) || ( nreplicas == 0 )) {
497 if ( ofile == NULL ) {
498 /* Just write to stdout */
503 state = prevstate = ST_START;
506 if (( std_ps = ps_alloc( std_open )) == NULLPS ||
507 std_setup( std_ps, ofp ) != OK ) {
508 fprintf( stderr, "std_ps setup failed - help!\n" );
511 if (( rps = ps_alloc( str_open )) == NULLPS ||
512 str_setup( rps, NULLCP, 0, 0 ) != OK ) {
513 fprintf( stderr, "rps setup failed - help!\n" );
518 while ( gets( nbuf ) != NULL ) {
519 if ( nbuf[ 0 ] == '\0' ) {
520 if ( state == ST_NL1 ) {
521 if ( prevstate == ST_ARGS ) {
522 /* We've got an attribute sequence to print */
523 if ( modtype == TY_ADD ) {
524 print_as( as, 0, ofp );
526 print_as( as, modstate, ofp );
528 /* as_print( std_ps, as, EDBOUT ); */
533 fprintf( ofp, "\n" );
535 /* If writing to a file, release the lock */
536 if ( ofile != NULL ) {
537 lock_fclose( ofp, lfp );
546 /* See if we've got a line continuation to deal with */
547 nbuflen = strlen( nbuf );
548 if ( state == ST_CONCAT ) {
549 for ( p = nbuf; isspace( *p ); p++, nbuflen-- ); /* skip space */
550 buf = realloc( buf, buflen + nbuflen + 1 );
552 buflen += ( nbuflen );
557 buf = strdup( nbuf );
560 if ( buf[ buflen - 1 ] == '\\' ) {
561 if ( state != ST_CONCAT ) {
565 buf[ buflen - 1 ] = '\0';
568 } else if ( state == ST_CONCAT ) {
572 if ( state == ST_PUNT ) {
576 if ( state == ST_START ) {
578 * Acquire the file lock if writing to a file.
580 if ( ofile != NULL ) {
581 if (( ofp = lock_fopen( ofile, "a", &lfp )) == NULL ) {
587 * If we have a changelog entry, then go ahead
588 * and write the replica: lines for the replog entry.
590 for ( i = 0; replicas[ i ] != NULL; i++ ) {
591 fprintf( ofp, "replica: %s\n", replicas[ i ] );
593 fprintf( ofp, "time: %ld\n", time( NULL ));
598 if ( state == ST_DN ) {
599 /* Second line - dn (quipu-style) of entry to be modified */
600 if (( ldapdn = dn2ldap( buf )) == NULL ) {
601 fprintf( ofp, "dn: (conversion failed)\n" );
603 fprintf( ofp, "dn: %s%s\n", ldapdn, dn_suffix );
610 if ( state == ST_TYPE ) {
613 if ( !strcmp( buf, "modify" )) {
614 modtype = TY_MODIFYTYPE;
615 fprintf( ofp, "changetype: modify\n" );
616 } else if ( !strcmp( buf, "add" )) {
618 fprintf( ofp, "changetype: add\n" );
620 } else if ( !strcmp( buf, "remove" )) {
622 fprintf( ofp, "changetype: delete\n" );
623 } else if ( !strcmp( buf, "newrdn" )) {
625 fprintf( ofp, "changetype: modrdn\n" );
633 if ( state == ST_ARGS ) {
636 fprintf( ofp, "newrdn: %s\n", buf );
638 case TY_REMOVE: /* No additional args */
641 as = as_combine( as, buf, 0 );
645 if ( buf[ 0 ] == '\0' ) {
648 print_as( as, modstate, ofp);
654 if (!strcmp( buf, "addvalues" )) {
656 print_as( as, modstate, ofp );
660 modstate = MOD_ADDVALUES;
662 } else if (!strcmp( buf, "removevalues" )) {
664 print_as( as, modstate, ofp );
668 modstate = MOD_REMOVEVALUES;
670 } else if (!strcmp( buf, "addattribute" )) {
672 print_as( as, modstate, ofp );
676 modstate = MOD_ADDATTRIBUTE;
678 } else if (!strcmp( buf, "removeattribute" )) {
680 print_as( as, modstate, ofp );
684 modstate = MOD_REMOVEATTRIBUTE;
687 switch ( modstate ) {
689 as = as_combine( as, buf, 0 );
691 case MOD_REMOVEVALUES:
692 as = as_combine( as, buf, 0 );
694 case MOD_ADDATTRIBUTE:
695 as = as_combine( as, buf, 0 );
697 case MOD_REMOVEATTRIBUTE:
698 fprintf( ofp, "delete: %s\n-\n", buf);
706 if ( ofile != NULL ) {
707 lock_fclose( ofp, lfp );
708 sprintf( nbuf, "%s.lock", ofile );
709 (void) unlink( nbuf );