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.
24 #include <quipu/commonarg.h>
25 #include <quipu/attrvalue.h>
32 extern FILE *lock_fopen( char *, char *, FILE ** );
33 extern int lock_fclose( FILE *, FILE * );
34 extern char *ch_realloc( char *, unsigned long );
40 int ldap_syslog_level = 0;
52 #define TY_MODIFYTYPE 1
57 #define TY_MODIFYARGS 6
59 #define MOD_ADDVALUES 1
60 #define MOD_ADDATTRIBUTE 2
61 #define MOD_REMOVEATTRIBUTE 3
62 #define MOD_REMOVEVALUES 4
66 dn2ldap( char *edbdn )
72 static int inited = 0;
75 /* load & initialize quipu syntax handlers */
79 pp_quipu_init( progname );
82 dsap_init( NULL, NULL );
84 if (( ldap_dn_syntax = str2syntax( "DN" )) == 0 ) {
90 if (( dn = str2dn( edbdn )) == NULLDN ) {
94 if (( str_ps = ps_alloc( str_open )) == NULLPS ||
95 str_setup( str_ps, NULLCP, 0, 0 ) == NOTOK ) {
100 if ( dn2ldif( str_ps, dn ) != 0 ) {
107 len = ( str_ps->ps_ptr - str_ps->ps_base );
109 if (( ldapdn = malloc( len + 1 )) == NULL ) {
114 memcpy( ldapdn, str_ps->ps_base, len );
115 ldapdn[ len ] = '\0';
121 #define SEPARATOR(c) (c == ',' || c == ';')
122 #define SPACE(c) (c == ' ' || c == '\n')
125 dn2ldif( PS ps, DN dn )
132 if ( dn == NULLDN ) {
136 if ( dn->dn_parent != NULLDN ) {
137 if (( rc = dn2ldif( ps, dn->dn_parent )) != 0 ) {
140 ps_print( ps, ", " );
143 if ( (rps = ps_alloc( str_open )) == NULLPS ||
144 str_setup( rps, NULLCP, 0, 0 ) == NOTOK ) {
149 for ( rdn = dn->dn_rdn; rdn != NULLRDN; rdn = rdn->rdn_next ) {
153 ps_print( ps, " + " );
156 AttrT_print( ps, rdn->rdn_at, EDBOUT );
159 if ( rdn->rdn_at->oa_syntax == ldap_dn_syntax ) {
160 if (( rc = dn2ldif( rps, (DN) rdn->rdn_av.av_struct )) != 0 ) {
164 value = rps->ps_base;
166 AttrV_print( rps, &rdn->rdn_av, EDBOUT );
168 value = rps->ps_base;
173 * ,+="\\\n all go in quotes. " and \\ need to
174 * be preceeded by \\.
177 if ( strpbrk( value, ",+=\"\\\n" ) != NULL || SPACE( value[0] )
178 || SPACE( value[max( strlen(value) - 1, 0 )] ) ) {
182 ps_print( ps, "\"" );
185 for ( p = value; *p != '\0'; p++ ) {
186 if ( *p == '"' || *p == '\\' ) {
190 if ( specialcount > 0 ) {
191 tmp = smalloc( strlen( value ) + specialcount + 1 );
192 for ( p = value, t = tmp; *p != '\0'; p++ ) {
206 ps_print( ps, value );
209 ps_print( ps, "\"" );
211 ps_print( ps, value );
214 rps->ps_ptr = rps->ps_base;
238 if ( strncasecmp( s, T61, T61LEN) == 0 ) {
257 else if ( c >= 'A' && c <= 'F' )
259 else if ( c >= 'a' && c <= 'f' )
269 else if ( c >= 'A' && c <= 'F' )
271 else if ( c >= 'a' && c <= 'f' )
301 static char retbuf[ RBSIZE ];
303 if (( val = strchr( buf, sep )) != NULL ) {
304 strncpy( retbuf, buf, val - buf );
305 retbuf[ val - buf ] = '\0';
317 return( getattr( buf, ':' ));
325 return( getattr( buf, '=' ));
335 if (( val = strchr( buf, sep )) != NULL ) {
336 return( strdup( ++val ));
346 return( getval( buf, ':' ));
354 return( getval( buf, '=' ));
364 oid_table_attr *p, *name2attr();
366 p = name2attr( attr );
367 if ( p == ( oid_table_attr * ) 0 ) {
370 if ( p->oa_syntax == ldap_dn_syntax ) {
380 print_as( as, modtype, ofp )
387 char *attrname, *tmpdn, *obuf;
390 for ( p = as; p != NULLATTR; p = p->attr_link) {
391 rps->ps_ptr = rps->ps_base;
392 AttrT_print( rps, p->attr_type, EDBOUT );
394 attrname = strdup( rps->ps_base );
395 if ( modtype != 0 ) {
398 case MOD_ADDATTRIBUTE:
399 fprintf( ofp, "add: %s\n", attrname );
401 case MOD_REMOVEATTRIBUTE:
402 case MOD_REMOVEVALUES:
403 fprintf( ofp, "delete: %s\n", attrname );
409 for ( av = p->attr_value; av != NULLAV; av = av->avseq_next ) {
410 rps->ps_ptr = rps->ps_base;
411 AttrV_print( rps, &av->avseq_av, EDBOUT );
413 de_t61( rps->ps_base, 0 );
414 if ( isDNsyntax( attrname )) {
415 tmpdn = dn2ldap( rps->ps_base );
416 obuf = ldif_type_and_value( attrname, tmpdn,
420 obuf = ldif_type_and_value( attrname, rps->ps_base,
421 strlen( rps->ps_base ));
423 if ( obuf != NULL ) {
428 if ( modtype != 0 ) {
429 fprintf( ofp, "-\n" );
440 fprintf( stderr, "usage: %s -d dn-suffix -r replica:port ", name );
441 fprintf( stderr, "[-r replica:port...] [-o outputfile]\n" );
446 main( int argc, char **argv )
448 char *ldapdn, nbuf[ 4096 ], *buf, *p;
449 int state, prevstate, modstate, modtype, i;
458 char **replicas = NULL;
460 char *dn_suffix = NULL;
462 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
463 progname = argv[ 0 ];
468 while (( arg = getopt( argc, argv, "o:r:d:" )) != EOF ) {
474 replicas = (char **) ch_realloc( (char *) replicas, (unsigned long)
475 ( nreplicas + 2 ) * sizeof( char * ));
476 replicas[ nreplicas ] = optarg;
477 replicas[ nreplicas + 1 ] = NULL;
489 if (( dn_suffix == NULL ) || ( nreplicas == 0 )) {
494 if ( ofile == NULL ) {
495 /* Just write to stdout */
500 state = prevstate = ST_START;
503 if (( std_ps = ps_alloc( std_open )) == NULLPS ||
504 std_setup( std_ps, ofp ) != OK ) {
505 fprintf( stderr, "std_ps setup failed - help!\n" );
508 if (( rps = ps_alloc( str_open )) == NULLPS ||
509 str_setup( rps, NULLCP, 0, 0 ) != OK ) {
510 fprintf( stderr, "rps setup failed - help!\n" );
515 while ( gets( nbuf ) != NULL ) {
516 if ( nbuf[ 0 ] == '\0' ) {
517 if ( state == ST_NL1 ) {
518 if ( prevstate == ST_ARGS ) {
519 /* We've got an attribute sequence to print */
520 if ( modtype == TY_ADD ) {
521 print_as( as, 0, ofp );
523 print_as( as, modstate, ofp );
525 /* as_print( std_ps, as, EDBOUT ); */
530 fprintf( ofp, "\n" );
532 /* If writing to a file, release the lock */
533 if ( ofile != NULL ) {
534 lock_fclose( ofp, lfp );
543 /* See if we've got a line continuation to deal with */
544 nbuflen = strlen( nbuf );
545 if ( state == ST_CONCAT ) {
546 for ( p = nbuf; isspace( *p ); p++, nbuflen-- ); /* skip space */
547 buf = realloc( buf, buflen + nbuflen + 1 );
549 buflen += ( nbuflen );
554 buf = strdup( nbuf );
557 if ( buf[ buflen - 1 ] == '\\' ) {
558 if ( state != ST_CONCAT ) {
562 buf[ buflen - 1 ] = '\0';
565 } else if ( state == ST_CONCAT ) {
569 if ( state == ST_PUNT ) {
573 if ( state == ST_START ) {
575 * Acquire the file lock if writing to a file.
577 if ( ofile != NULL ) {
578 if (( ofp = lock_fopen( ofile, "a", &lfp )) == NULL ) {
584 * If we have a changelog entry, then go ahead
585 * and write the replica: lines for the replog entry.
587 for ( i = 0; replicas[ i ] != NULL; i++ ) {
588 fprintf( ofp, "replica: %s\n", replicas[ i ] );
590 fprintf( ofp, "time: %ld\n", time( NULL ));
595 if ( state == ST_DN ) {
596 /* Second line - dn (quipu-style) of entry to be modified */
597 if (( ldapdn = dn2ldap( buf )) == NULL ) {
598 fprintf( ofp, "dn: (conversion failed)\n" );
600 fprintf( ofp, "dn: %s%s\n", ldapdn, dn_suffix );
607 if ( state == ST_TYPE ) {
610 if ( !strcmp( buf, "modify" )) {
611 modtype = TY_MODIFYTYPE;
612 fprintf( ofp, "changetype: modify\n" );
613 } else if ( !strcmp( buf, "add" )) {
615 fprintf( ofp, "changetype: add\n" );
617 } else if ( !strcmp( buf, "remove" )) {
619 fprintf( ofp, "changetype: delete\n" );
620 } else if ( !strcmp( buf, "newrdn" )) {
622 fprintf( ofp, "changetype: modrdn\n" );
630 if ( state == ST_ARGS ) {
633 fprintf( ofp, "newrdn: %s\n", buf );
635 case TY_REMOVE: /* No additional args */
638 as = as_combine( as, buf, 0 );
642 if ( buf[ 0 ] == '\0' ) {
645 print_as( as, modstate, ofp);
651 if (!strcmp( buf, "addvalues" )) {
653 print_as( as, modstate, ofp );
657 modstate = MOD_ADDVALUES;
659 } else if (!strcmp( buf, "removevalues" )) {
661 print_as( as, modstate, ofp );
665 modstate = MOD_REMOVEVALUES;
667 } else if (!strcmp( buf, "addattribute" )) {
669 print_as( as, modstate, ofp );
673 modstate = MOD_ADDATTRIBUTE;
675 } else if (!strcmp( buf, "removeattribute" )) {
677 print_as( as, modstate, ofp );
681 modstate = MOD_REMOVEATTRIBUTE;
684 switch ( modstate ) {
686 as = as_combine( as, buf, 0 );
688 case MOD_REMOVEVALUES:
689 as = as_combine( as, buf, 0 );
691 case MOD_ADDATTRIBUTE:
692 as = as_combine( as, buf, 0 );
694 case MOD_REMOVEATTRIBUTE:
695 fprintf( ofp, "delete: %s\n-\n", buf);
703 if ( ofile != NULL ) {
704 lock_fclose( ofp, lfp );
705 sprintf( nbuf, "%s.lock", ofile );
706 (void) unlink( nbuf );