1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2001-01-21 01:49:05 +08:00
|
|
|
/*
|
2003-01-04 04:20:47 +08:00
|
|
|
* Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
|
2001-01-21 01:49:05 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1996 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that this notice is preserved and that due credit is given
|
|
|
|
* to the University of Michigan at Ann Arbor. The name of the University
|
|
|
|
* may not be used to endorse or promote products derived from this
|
|
|
|
* software without specific prior written permission. This software
|
|
|
|
* is provided ``as is'' without express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* reject.c - routines to write replication records to reject files.
|
|
|
|
* An Re struct is writted to a reject file if it cannot be propagated
|
|
|
|
* to a replica LDAP server.
|
|
|
|
*/
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
2002-05-16 18:51:45 +08:00
|
|
|
#include <ac/stdlib.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
#include <ac/errno.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "slurp.h"
|
|
|
|
#include "globals.h"
|
|
|
|
|
2003-02-22 09:14:24 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define PORTSEP ","
|
|
|
|
#else
|
|
|
|
#define PORTSEP ":"
|
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* Write a replication record to a reject file. The reject file has the
|
|
|
|
* same name as the replica's private copy of the file but with ".rej"
|
|
|
|
* appended (e.g. "/usr/tmp/<hostname>:<port>.rej")
|
|
|
|
*
|
|
|
|
* If errmsg is non-NULL, use that as the error message in the reject
|
|
|
|
* file. Otherwise, use ldap_err2string( lderr ).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
write_reject(
|
|
|
|
Ri *ri,
|
|
|
|
Re *re,
|
|
|
|
int lderr,
|
|
|
|
char *errmsg
|
|
|
|
)
|
|
|
|
{
|
|
|
|
char rejfile[ MAXPATHLEN ];
|
|
|
|
FILE *rfp, *lfp;
|
|
|
|
int rc;
|
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &sglob->rej_mutex );
|
2003-02-22 09:14:24 +08:00
|
|
|
snprintf( rejfile, sizeof rejfile, "%s" LDAP_DIRSEP "%s" PORTSEP "%d.rej",
|
2002-08-06 03:44:24 +08:00
|
|
|
sglob->slurpd_rdir, ri->ri_hostname, ri->ri_port );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( access( rejfile, F_OK ) < 0 ) {
|
|
|
|
/* Doesn't exist - try to create */
|
|
|
|
int rjfd;
|
2002-08-06 03:43:01 +08:00
|
|
|
if (( rjfd = open( rejfile, O_RDWR|O_APPEND|O_CREAT|O_EXCL,
|
1998-08-09 08:43:13 +08:00
|
|
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP )) < 0 ) {
|
2002-04-16 04:18:47 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR, "write_reject: "
|
2002-04-16 04:18:47 +08:00
|
|
|
"Error: Cannot create \"%s\":%s\n",
|
2002-07-12 04:33:24 +08:00
|
|
|
rejfile, sys_errlist[ errno ], 0 );
|
2002-04-16 04:18:47 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"Error: write_reject: Cannot create \"%s\": %s\n",
|
|
|
|
rejfile, sys_errlist[ errno ], 0 );
|
2002-04-16 04:18:47 +08:00
|
|
|
#endif
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &sglob->rej_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
close( rjfd );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (( rc = acquire_lock( rejfile, &rfp, &lfp )) < 0 ) {
|
2002-04-16 04:18:47 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR, "write_reject: "
|
|
|
|
"Error: Cannot open reject file \"%s\"\n", rejfile, 0, 0 );
|
2002-04-16 04:18:47 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "Error: cannot open reject file \"%s\"\n",
|
|
|
|
rejfile, 0, 0 );
|
2002-04-16 04:18:47 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
|
|
|
fseek( rfp, 0, 2 );
|
|
|
|
if ( errmsg != NULL ) {
|
|
|
|
fprintf( rfp, "%s: %s\n", ERROR_STR, errmsg );
|
|
|
|
} else {
|
|
|
|
fprintf( rfp, "%s: %s\n", ERROR_STR, ldap_err2string( lderr ));
|
|
|
|
}
|
|
|
|
if ((rc = re->re_write( ri, re, rfp )) < 0 ) {
|
2002-04-16 04:18:47 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR, "write_reject: "
|
|
|
|
"Error: Cannot write reject file \"%s\"\n", rejfile, 0, 0 );
|
2002-04-16 04:18:47 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"Error: cannot write reject file \"%s\"\n",
|
|
|
|
rejfile, 0, 0 );
|
2002-04-16 04:18:47 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
(void) relinquish_lock( rejfile, rfp, lfp );
|
2002-04-16 04:18:47 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR, "write_reject: "
|
|
|
|
"Error: ldap operation failed, data written to \"%s\"\n",
|
|
|
|
rejfile, 0, 0 );
|
2002-04-16 04:18:47 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"Error: ldap operation failed, data written to \"%s\"\n",
|
|
|
|
rejfile, 0, 0 );
|
2002-04-16 04:18:47 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &sglob->rej_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|