X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=servers%2Fslurpd%2Freject.c;h=4f49f6a4e02e899f231ee5db2c3a40df206f139c;hb=d1824b14ae78b128fb9ff6cf73d2ec4a0e756a90;hp=b87f55145a9a9df1315f240940546f40315df502;hpb=6949ab31ac7c3e35c1d1c82701550fa3a01e35d4;p=openldap diff --git a/servers/slurpd/reject.c b/servers/slurpd/reject.c index b87f55145a..4f49f6a4e0 100644 --- a/servers/slurpd/reject.c +++ b/servers/slurpd/reject.c @@ -1,5 +1,18 @@ -/* - * Copyright (c) 1996 Regents of the University of Michigan. +/* $OpenLDAP$ */ +/* This work is part of OpenLDAP Software . + * + * Copyright 1998-2006 The OpenLDAP Foundation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted only as authorized by the OpenLDAP + * Public License. + * + * A copy of this license is available in file LICENSE in the + * top-level directory of the distribution or, alternatively, at + * . + */ +/* Portions Copyright (c) 1996 Regents of the University of Michigan. * All rights reserved. * * Redistribution and use in source and binary forms are permitted @@ -9,6 +22,10 @@ * software without specific prior written permission. This software * is provided ``as is'' without express or implied warranty. */ +/* ACKNOWLEDGEMENTS: + * This work was originally developed by the University of Michigan + * (as part of U-MICH LDAP). + */ /* @@ -17,22 +34,29 @@ * to a replica LDAP server. */ -#define DISABLE_BRIDGE #include "portable.h" #include -#include + +#include +#include +#include +#include + #include #include -#include #include "slurp.h" #include "globals.h" -#ifndef DECL_SYS_ERRLIST -extern char *sys_errlist[]; -#endif /* DECL_SYS_ERRLIST */ +#include "lber_pvt.h" +#include "lutil.h" +#ifdef _WIN32 +#define PORTSEP "," +#else +#define PORTSEP ":" +#endif /* * Write a replication record to a reject file. The reject file has the @@ -54,19 +78,19 @@ write_reject( FILE *rfp, *lfp; int rc; - pthread_mutex_lock( &sglob->rej_mutex ); - sprintf( rejfile, "%s/%s:%d.rej", sglob->slurpd_rdir, - ri->ri_hostname, ri->ri_port ); + ldap_pvt_thread_mutex_lock( &sglob->rej_mutex ); + snprintf( rejfile, sizeof rejfile, "%s" LDAP_DIRSEP "%s" PORTSEP "%d.rej", + sglob->slurpd_rdir, ri->ri_hostname, ri->ri_port ); if ( access( rejfile, F_OK ) < 0 ) { /* Doesn't exist - try to create */ int rjfd; - if (( rjfd = open( rejfile, O_RDWR | O_APPEND | O_CREAT, + if (( rjfd = open( rejfile, O_RDWR|O_APPEND|O_CREAT|O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP )) < 0 ) { Debug( LDAP_DEBUG_ANY, "Error: write_reject: Cannot create \"%s\": %s\n", rejfile, sys_errlist[ errno ], 0 ); - pthread_mutex_unlock( &sglob->rej_mutex ); + ldap_pvt_thread_mutex_unlock( &sglob->rej_mutex ); return; } else { close( rjfd ); @@ -76,12 +100,45 @@ write_reject( Debug( LDAP_DEBUG_ANY, "Error: cannot open reject file \"%s\"\n", rejfile, 0, 0 ); } else { - fseek( rfp, 0, 2 ); - if ( errmsg != NULL ) { - fprintf( rfp, "%s: %s\n", ERROR_STR, errmsg ); + struct berval bv = BER_BVNULL, + errstrbv, + errmsgbv = BER_BVNULL; + char *ptr; + + ber_str2bv( ldap_err2string( lderr ), 0, 0, &errstrbv ); + if ( errmsg && *errmsg ) { + ber_str2bv( errmsg, 0, 0, &errmsgbv ); + bv.bv_len = errstrbv.bv_len + + STRLENOF( ": " ) + errmsgbv.bv_len; + + ptr = bv.bv_val = ber_memalloc( bv.bv_len + 1 ); + ptr = lutil_strcopy( ptr, errstrbv.bv_val ); + ptr = lutil_strcopy( ptr, ": " ); + ptr = lutil_strcopy( ptr, errmsgbv.bv_val ); + } else { - fprintf( rfp, "%s: %s\n", ERROR_STR, ldap_err2string( lderr )); + bv = errstrbv; + } + + fseek( rfp, 0, 2 ); + + ptr = ldif_put( LDIF_PUT_VALUE, ERROR_STR, bv.bv_val, bv.bv_len ); + if ( bv.bv_val != errstrbv.bv_val ) { + ber_memfree( bv.bv_val ); + } + if ( ptr == NULL ) { + Debug( LDAP_DEBUG_ANY, + "Error: cannot convert error message(s) \"%s%s%s\" " + "into LDIF format\n", + errstrbv.bv_val, + BER_BVISNULL( &errmsgbv ) ? "" : ": ", + BER_BVISNULL( &errmsgbv ) ? "" : errmsgbv.bv_val ); + return; } + + fputs( ptr, rfp ); + ber_memfree( ptr ); + if ((rc = re->re_write( ri, re, rfp )) < 0 ) { Debug( LDAP_DEBUG_ANY, "Error: cannot write reject file \"%s\"\n", @@ -92,7 +149,7 @@ write_reject( "Error: ldap operation failed, data written to \"%s\"\n", rejfile, 0, 0 ); } - pthread_mutex_unlock( &sglob->rej_mutex ); + ldap_pvt_thread_mutex_unlock( &sglob->rej_mutex ); return; }