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>
26 #include <ac/unistd.h>
28 #include <quipu/commonarg.h>
29 #include <quipu/attrvalue.h>
33 static int dn2ldif(PS ps, DN dn);
34 static void de_t61(char *s, int t61mark);
36 extern FILE *lock_fopen( char *, char *, FILE ** );
37 extern int lock_fclose( FILE *, FILE * );
38 extern void *ch_realloc( void *, unsigned long );
44 int ldap_syslog_level = 0;
56 #define TY_MODIFYTYPE 1
61 #define TY_MODIFYARGS 6
63 #define MOD_ADDVALUES 1
64 #define MOD_ADDATTRIBUTE 2
65 #define MOD_REMOVEATTRIBUTE 3
66 #define MOD_REMOVEVALUES 4
70 dn2ldap( char *edbdn )
76 static int inited = 0;
79 /* load & initialize quipu syntax handlers */
83 pp_quipu_init( progname );
86 dsap_init( NULL, NULL );
88 if (( ldap_dn_syntax = str2syntax( "DN" )) == 0 ) {
94 if (( dn = str2dn( edbdn )) == NULLDN ) {
98 if (( str_ps = ps_alloc( str_open )) == NULLPS ||
99 str_setup( str_ps, NULLCP, 0, 0 ) == NOTOK ) {
104 if ( dn2ldif( str_ps, dn ) != 0 ) {
111 len = ( str_ps->ps_ptr - str_ps->ps_base );
113 if (( ldapdn = malloc( len + 1 )) == NULL ) {
118 memcpy( ldapdn, str_ps->ps_base, len );
119 ldapdn[ len ] = '\0';
125 #define SEPARATOR(c) (c == ',' || c == ';')
126 #define SPACE(c) (c == ' ' || c == '\n')
129 dn2ldif( PS ps, DN dn )
136 if ( dn == NULLDN ) {
140 if ( dn->dn_parent != NULLDN ) {
141 if (( rc = dn2ldif( ps, dn->dn_parent )) != 0 ) {
144 ps_print( ps, ", " );
147 if ( (rps = ps_alloc( str_open )) == NULLPS ||
148 str_setup( rps, NULLCP, 0, 0 ) == NOTOK ) {
153 for ( rdn = dn->dn_rdn; rdn != NULLRDN; rdn = rdn->rdn_next ) {
157 ps_print( ps, " + " );
160 AttrT_print( ps, rdn->rdn_at, EDBOUT );
163 if ( rdn->rdn_at->oa_syntax == ldap_dn_syntax ) {
164 if (( rc = dn2ldif( rps, (DN) rdn->rdn_av.av_struct )) != 0 ) {
168 value = rps->ps_base;
170 AttrV_print( rps, &rdn->rdn_av, EDBOUT );
172 value = rps->ps_base;
177 * ,+="\\\n all go in quotes. " and \\ need to
178 * be preceeded by \\.
181 if ( strpbrk( value, ",+=\"\\\n" ) != NULL || SPACE( value[0] )
182 || SPACE( value[max( strlen(value) - 1, 0 )] ) ) {
186 ps_print( ps, "\"" );
189 for ( p = value; *p != '\0'; p++ ) {
190 if ( *p == '"' || *p == '\\' ) {
194 if ( specialcount > 0 ) {
195 tmp = smalloc( strlen( value ) + specialcount + 1 );
196 for ( p = value, t = tmp; *p != '\0'; p++ ) {
210 ps_print( ps, value );
213 ps_print( ps, "\"" );
215 ps_print( ps, value );
218 rps->ps_ptr = rps->ps_base;
232 de_t61(char *s, int t61mark)
240 if ( strncasecmp( s, T61, T61LEN) == 0 ) {
259 else if ( c >= 'A' && c <= 'F' )
261 else if ( c >= 'a' && c <= 'f' )
271 else if ( c >= 'A' && c <= 'F' )
273 else if ( c >= 'a' && c <= 'f' )
297 getattr(char *buf, char sep)
301 static char retbuf[ RBSIZE ];
303 if (( val = strchr( buf, sep )) != NULL ) {
304 strncpy( retbuf, buf, val - buf );
305 retbuf[ val - buf ] = '\0';
314 getattr_ldif(char *buf)
316 return( getattr( buf, ':' ));
321 getattr_edb(char *buf)
323 return( getattr( buf, '=' ));
327 getval(char *buf, char sep)
331 if (( val = strchr( buf, sep )) != NULL ) {
332 return( strdup( ++val ));
339 getval_ldif(char *buf)
341 return( getval( buf, ':' ));
346 getval_edb(char *buf)
348 return( getval( buf, '=' ));
355 isDNsyntax(char *attr)
357 oid_table_attr *p, *name2attr(char *);
359 p = name2attr( attr );
360 if ( p == ( oid_table_attr * ) 0 ) {
363 if ( p->oa_syntax == ldap_dn_syntax ) {
373 print_as(Attr_Sequence as, int modtype, FILE *ofp)
377 char *attrname, *tmpdn, *obuf;
380 for ( p = as; p != NULLATTR; p = p->attr_link) {
381 rps->ps_ptr = rps->ps_base;
382 AttrT_print( rps, p->attr_type, EDBOUT );
384 attrname = strdup( rps->ps_base );
385 if ( modtype != 0 ) {
388 case MOD_ADDATTRIBUTE:
389 fprintf( ofp, "add: %s\n", attrname );
391 case MOD_REMOVEATTRIBUTE:
392 case MOD_REMOVEVALUES:
393 fprintf( ofp, "delete: %s\n", attrname );
399 for ( av = p->attr_value; av != NULLAV; av = av->avseq_next ) {
400 rps->ps_ptr = rps->ps_base;
401 AttrV_print( rps, &av->avseq_av, EDBOUT );
403 de_t61( rps->ps_base, 0 );
404 if ( isDNsyntax( attrname )) {
405 tmpdn = dn2ldap( rps->ps_base );
406 obuf = ldif_type_and_value( attrname, tmpdn,
410 obuf = ldif_type_and_value( attrname, rps->ps_base,
411 strlen( rps->ps_base ));
413 if ( obuf != NULL ) {
418 if ( modtype != 0 ) {
419 fprintf( ofp, "-\n" );
430 fprintf( stderr, "usage: %s -d dn-suffix -r replica:port ", name );
431 fprintf( stderr, "[-r replica:port...] [-o outputfile]\n" );
436 main( int argc, char **argv )
438 char *ldapdn, nbuf[ 4096 ], *buf, *p;
439 int state, prevstate, modstate, modtype, i;
448 char **replicas = NULL;
450 char *dn_suffix = NULL;
452 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
453 progname = argv[ 0 ];
458 while (( arg = getopt( argc, argv, "o:r:d:" )) != EOF ) {
464 replicas = (char **) ch_realloc( (char *) replicas, (unsigned long)
465 ( nreplicas + 2 ) * sizeof( char * ));
466 replicas[ nreplicas ] = optarg;
467 replicas[ nreplicas + 1 ] = NULL;
479 if (( dn_suffix == NULL ) || ( nreplicas == 0 )) {
484 if ( ofile == NULL ) {
485 /* Just write to stdout */
490 state = prevstate = ST_START;
493 if (( std_ps = ps_alloc( std_open )) == NULLPS ||
494 std_setup( std_ps, ofp ) != OK ) {
495 fprintf( stderr, "std_ps setup failed - help!\n" );
498 if (( rps = ps_alloc( str_open )) == NULLPS ||
499 str_setup( rps, NULLCP, 0, 0 ) != OK ) {
500 fprintf( stderr, "rps setup failed - help!\n" );
505 while ( gets( nbuf ) != NULL ) {
506 if ( nbuf[ 0 ] == '\0' ) {
507 if ( state == ST_NL1 ) {
508 if ( prevstate == ST_ARGS ) {
509 /* We've got an attribute sequence to print */
510 if ( modtype == TY_ADD ) {
511 print_as( as, 0, ofp );
513 print_as( as, modstate, ofp );
515 /* as_print( std_ps, as, EDBOUT ); */
520 fprintf( ofp, "\n" );
522 /* If writing to a file, release the lock */
523 if ( ofile != NULL ) {
524 lock_fclose( ofp, lfp );
533 /* See if we've got a line continuation to deal with */
534 nbuflen = strlen( nbuf );
535 if ( state == ST_CONCAT ) {
536 for ( p = nbuf; isspace( *p ); p++, nbuflen-- ); /* skip space */
537 buf = realloc( buf, buflen + nbuflen + 1 );
539 buflen += ( nbuflen );
544 buf = strdup( nbuf );
547 if ( buf[ buflen - 1 ] == '\\' ) {
548 if ( state != ST_CONCAT ) {
552 buf[ buflen - 1 ] = '\0';
555 } else if ( state == ST_CONCAT ) {
559 if ( state == ST_PUNT ) {
563 if ( state == ST_START ) {
565 * Acquire the file lock if writing to a file.
567 if ( ofile != NULL ) {
568 if (( ofp = lock_fopen( ofile, "a", &lfp )) == NULL ) {
574 * If we have a changelog entry, then go ahead
575 * and write the replica: lines for the replog entry.
577 for ( i = 0; replicas[ i ] != NULL; i++ ) {
578 fprintf( ofp, "replica: %s\n", replicas[ i ] );
580 fprintf( ofp, "time: %ld\n", time( NULL ));
585 if ( state == ST_DN ) {
586 /* Second line - dn (quipu-style) of entry to be modified */
587 if (( ldapdn = dn2ldap( buf )) == NULL ) {
588 fprintf( ofp, "dn: (conversion failed)\n" );
590 fprintf( ofp, "dn: %s%s\n", ldapdn, dn_suffix );
597 if ( state == ST_TYPE ) {
600 if ( !strcmp( buf, "modify" )) {
601 modtype = TY_MODIFYTYPE;
602 fprintf( ofp, "changetype: modify\n" );
603 } else if ( !strcmp( buf, "add" )) {
605 fprintf( ofp, "changetype: add\n" );
607 } else if ( !strcmp( buf, "remove" )) {
609 fprintf( ofp, "changetype: delete\n" );
610 } else if ( !strcmp( buf, "newrdn" )) {
612 fprintf( ofp, "changetype: modrdn\n" );
620 if ( state == ST_ARGS ) {
623 fprintf( ofp, "newrdn: %s\n", buf );
625 case TY_REMOVE: /* No additional args */
628 as = as_combine( as, buf, 0 );
632 if ( buf[ 0 ] == '\0' ) {
635 print_as( as, modstate, ofp);
641 if (!strcmp( buf, "addvalues" )) {
643 print_as( as, modstate, ofp );
647 modstate = MOD_ADDVALUES;
649 } else if (!strcmp( buf, "removevalues" )) {
651 print_as( as, modstate, ofp );
655 modstate = MOD_REMOVEVALUES;
657 } else if (!strcmp( buf, "addattribute" )) {
659 print_as( as, modstate, ofp );
663 modstate = MOD_ADDATTRIBUTE;
665 } else if (!strcmp( buf, "removeattribute" )) {
667 print_as( as, modstate, ofp );
671 modstate = MOD_REMOVEATTRIBUTE;
674 switch ( modstate ) {
676 as = as_combine( as, buf, 0 );
678 case MOD_REMOVEVALUES:
679 as = as_combine( as, buf, 0 );
681 case MOD_ADDATTRIBUTE:
682 as = as_combine( as, buf, 0 );
684 case MOD_REMOVEATTRIBUTE:
685 fprintf( ofp, "delete: %s\n-\n", buf);
693 if ( ofile != NULL ) {
694 lock_fclose( ofp, lfp );
695 sprintf( nbuf, "%s.lock", ofile );
696 (void) unlink( nbuf );