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.
22 #include <ac/stdlib.h>
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)
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' )
298 getattr(char *buf, char sep)
302 static char retbuf[ RBSIZE ];
304 if (( val = strchr( buf, sep )) != NULL ) {
305 strncpy( retbuf, buf, val - buf );
306 retbuf[ val - buf ] = '\0';
315 getattr_ldif(char *buf)
317 return( getattr( buf, ':' ));
322 getattr_edb(char *buf)
324 return( getattr( buf, '=' ));
328 getval(char *buf, char sep)
332 if (( val = strchr( buf, sep )) != NULL ) {
333 return( strdup( ++val ));
340 getval_ldif(char *buf)
342 return( getval( buf, ':' ));
347 getval_edb(char *buf)
349 return( getval( buf, '=' ));
356 isDNsyntax(char *attr)
358 oid_table_attr *p, *name2attr(char *);
360 p = name2attr( attr );
361 if ( p == ( oid_table_attr * ) 0 ) {
364 if ( p->oa_syntax == ldap_dn_syntax ) {
374 print_as(Attr_Sequence as, int modtype, FILE *ofp)
378 char *attrname, *tmpdn, *obuf;
381 for ( p = as; p != NULLATTR; p = p->attr_link) {
382 rps->ps_ptr = rps->ps_base;
383 AttrT_print( rps, p->attr_type, EDBOUT );
385 attrname = strdup( rps->ps_base );
386 if ( modtype != 0 ) {
389 case MOD_ADDATTRIBUTE:
390 fprintf( ofp, "add: %s\n", attrname );
392 case MOD_REMOVEATTRIBUTE:
393 case MOD_REMOVEVALUES:
394 fprintf( ofp, "delete: %s\n", attrname );
400 for ( av = p->attr_value; av != NULLAV; av = av->avseq_next ) {
401 rps->ps_ptr = rps->ps_base;
402 AttrV_print( rps, &av->avseq_av, EDBOUT );
404 de_t61( rps->ps_base, 0 );
405 if ( isDNsyntax( attrname )) {
406 tmpdn = dn2ldap( rps->ps_base );
407 obuf = ldif_type_and_value( attrname, tmpdn,
411 obuf = ldif_type_and_value( attrname, rps->ps_base,
412 strlen( rps->ps_base ));
414 if ( obuf != NULL ) {
419 if ( modtype != 0 ) {
420 fprintf( ofp, "-\n" );
431 fprintf( stderr, "usage: %s -d dn-suffix -r replica:port ", name );
432 fprintf( stderr, "[-r replica:port...] [-o outputfile]\n" );
437 main( int argc, char **argv )
439 char *ldapdn, nbuf[ 4096 ], *buf, *p;
440 int state, prevstate, modstate, modtype, i;
449 char **replicas = NULL;
451 char *dn_suffix = NULL;
453 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
454 progname = argv[ 0 ];
459 while (( arg = getopt( argc, argv, "o:r:d:" )) != EOF ) {
465 replicas = (char **) ch_realloc( (char *) replicas, (unsigned long)
466 ( nreplicas + 2 ) * sizeof( char * ));
467 replicas[ nreplicas ] = optarg;
468 replicas[ nreplicas + 1 ] = NULL;
480 if (( dn_suffix == NULL ) || ( nreplicas == 0 )) {
485 if ( ofile == NULL ) {
486 /* Just write to stdout */
491 state = prevstate = ST_START;
494 if (( std_ps = ps_alloc( std_open )) == NULLPS ||
495 std_setup( std_ps, ofp ) != OK ) {
496 fprintf( stderr, "std_ps setup failed - help!\n" );
499 if (( rps = ps_alloc( str_open )) == NULLPS ||
500 str_setup( rps, NULLCP, 0, 0 ) != OK ) {
501 fprintf( stderr, "rps setup failed - help!\n" );
506 while ( gets( nbuf ) != NULL ) {
507 if ( nbuf[ 0 ] == '\0' ) {
508 if ( state == ST_NL1 ) {
509 if ( prevstate == ST_ARGS ) {
510 /* We've got an attribute sequence to print */
511 if ( modtype == TY_ADD ) {
512 print_as( as, 0, ofp );
514 print_as( as, modstate, ofp );
516 /* as_print( std_ps, as, EDBOUT ); */
521 fprintf( ofp, "\n" );
523 /* If writing to a file, release the lock */
524 if ( ofile != NULL ) {
525 lock_fclose( ofp, lfp );
534 /* See if we've got a line continuation to deal with */
535 nbuflen = strlen( nbuf );
536 if ( state == ST_CONCAT ) {
537 for ( p = nbuf; isspace( (unsigned char) *p ); p++, nbuflen-- )
539 buf = realloc( buf, buflen + nbuflen + 1 );
541 buflen += ( nbuflen );
546 buf = strdup( nbuf );
549 if ( buf[ buflen - 1 ] == '\\' ) {
550 if ( state != ST_CONCAT ) {
554 buf[ buflen - 1 ] = '\0';
557 } else if ( state == ST_CONCAT ) {
561 if ( state == ST_PUNT ) {
565 if ( state == ST_START ) {
567 * Acquire the file lock if writing to a file.
569 if ( ofile != NULL ) {
570 if (( ofp = lock_fopen( ofile, "a", &lfp )) == NULL ) {
576 * If we have a changelog entry, then go ahead
577 * and write the replica: lines for the replog entry.
579 for ( i = 0; replicas[ i ] != NULL; i++ ) {
580 fprintf( ofp, "replica: %s\n", replicas[ i ] );
582 fprintf( ofp, "time: %ld\n", time( NULL ));
587 if ( state == ST_DN ) {
588 /* Second line - dn (quipu-style) of entry to be modified */
589 if (( ldapdn = dn2ldap( buf )) == NULL ) {
590 fprintf( ofp, "dn: (conversion failed)\n" );
592 fprintf( ofp, "dn: %s%s\n", ldapdn, dn_suffix );
599 if ( state == ST_TYPE ) {
602 if ( !strcmp( buf, "modify" )) {
603 modtype = TY_MODIFYTYPE;
604 fprintf( ofp, "changetype: modify\n" );
605 } else if ( !strcmp( buf, "add" )) {
607 fprintf( ofp, "changetype: add\n" );
609 } else if ( !strcmp( buf, "remove" )) {
611 fprintf( ofp, "changetype: delete\n" );
612 } else if ( !strcmp( buf, "newrdn" )) {
614 fprintf( ofp, "changetype: modrdn\n" );
622 if ( state == ST_ARGS ) {
625 fprintf( ofp, "newrdn: %s\n", buf );
627 case TY_REMOVE: /* No additional args */
630 as = as_combine( as, buf, 0 );
634 if ( buf[ 0 ] == '\0' ) {
637 print_as( as, modstate, ofp);
643 if (!strcmp( buf, "addvalues" )) {
645 print_as( as, modstate, ofp );
649 modstate = MOD_ADDVALUES;
651 } else if (!strcmp( buf, "removevalues" )) {
653 print_as( as, modstate, ofp );
657 modstate = MOD_REMOVEVALUES;
659 } else if (!strcmp( buf, "addattribute" )) {
661 print_as( as, modstate, ofp );
665 modstate = MOD_ADDATTRIBUTE;
667 } else if (!strcmp( buf, "removeattribute" )) {
669 print_as( as, modstate, ofp );
673 modstate = MOD_REMOVEATTRIBUTE;
676 switch ( modstate ) {
678 as = as_combine( as, buf, 0 );
680 case MOD_REMOVEVALUES:
681 as = as_combine( as, buf, 0 );
683 case MOD_ADDATTRIBUTE:
684 as = as_combine( as, buf, 0 );
686 case MOD_REMOVEATTRIBUTE:
687 fprintf( ofp, "delete: %s\n-\n", buf);
695 if ( ofile != NULL ) {
696 lock_fclose( ofp, lfp );
697 sprintf( nbuf, "%s.lock", ofile );
698 (void) unlink( nbuf );