1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
1998-12-29 04:38:04 +08:00
|
|
|
/*
|
2000-05-13 10:36:07 +08:00
|
|
|
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
|
1998-12-29 04:38:04 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
#include <ac/ctype.h>
|
1998-12-22 09:34:01 +08:00
|
|
|
#include <ac/stdarg.h>
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
#include <ac/string.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
1998-10-26 09:18:41 +08:00
|
|
|
#include "lber-int.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-13 22:10:41 +08:00
|
|
|
typedef void (*BER_LOG_FN) LDAP_P((FILE *file, char *subsys, int level, const char *fmt, va_list vl));
|
|
|
|
|
2001-09-22 01:53:16 +08:00
|
|
|
#define ber_log_check(errlvl, loglvl) (errlvl & loglvl)
|
2000-10-13 22:10:41 +08:00
|
|
|
|
2000-10-13 04:01:12 +08:00
|
|
|
BER_LOG_FN ber_int_log_proc = NULL;
|
|
|
|
|
2000-05-11 17:39:33 +08:00
|
|
|
/*
|
|
|
|
* We don't just set ber_pvt_err_file to stderr here, because in NT,
|
|
|
|
* stderr is a symbol imported from a DLL. As such, the compiler
|
|
|
|
* doesn't recognize the symbol as having a constant address. Thus
|
|
|
|
* we set ber_pvt_err_file to stderr later, when it first gets
|
|
|
|
* referenced.
|
|
|
|
*/
|
2000-10-03 01:53:50 +08:00
|
|
|
FILE *ber_pvt_err_file = NULL;
|
2000-05-11 17:39:33 +08:00
|
|
|
|
1999-11-08 23:38:59 +08:00
|
|
|
/*
|
|
|
|
* ber errno
|
|
|
|
*/
|
2000-10-03 01:53:50 +08:00
|
|
|
BER_ERRNO_FN ber_int_errno_fn = NULL;
|
1999-11-08 23:38:59 +08:00
|
|
|
|
|
|
|
int * ber_errno_addr(void)
|
|
|
|
{
|
|
|
|
static int ber_int_errno = LBER_ERROR_NONE;
|
|
|
|
|
|
|
|
if( ber_int_errno_fn ) {
|
|
|
|
return (*ber_int_errno_fn)();
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ber_int_errno;
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
1998-10-27 15:50:07 +08:00
|
|
|
* Print stuff
|
1998-08-09 08:43:13 +08:00
|
|
|
*/
|
2000-06-11 07:06:08 +08:00
|
|
|
void ber_error_print( LDAP_CONST char *data )
|
1998-10-27 15:50:07 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( data != NULL );
|
|
|
|
|
2000-06-11 07:13:01 +08:00
|
|
|
if (!ber_pvt_err_file) ber_pvt_err_file = stderr;
|
2000-05-11 17:39:33 +08:00
|
|
|
|
|
|
|
fputs( data, ber_pvt_err_file );
|
|
|
|
|
|
|
|
/* Print to both streams */
|
2000-06-11 07:13:01 +08:00
|
|
|
if (ber_pvt_err_file != stderr) {
|
|
|
|
fputs( data, stderr );
|
|
|
|
fflush( stderr );
|
2000-05-11 17:39:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fflush( ber_pvt_err_file );
|
1998-10-27 15:50:07 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print;
|
1999-01-26 06:55:00 +08:00
|
|
|
|
1998-12-22 09:34:01 +08:00
|
|
|
/*
|
|
|
|
* lber log
|
|
|
|
*/
|
|
|
|
|
2000-10-13 04:01:12 +08:00
|
|
|
int ber_pvt_log_output( char *subsystem, int level, const char *fmt, ... )
|
|
|
|
{
|
|
|
|
char buf[ 1024 ];
|
|
|
|
va_list vl;
|
|
|
|
va_start( vl, fmt );
|
|
|
|
|
|
|
|
if ( ber_int_log_proc != NULL )
|
|
|
|
{
|
|
|
|
ber_int_log_proc( ber_pvt_err_file, subsystem, level, fmt, vl );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-10-25 23:33:39 +08:00
|
|
|
int level;
|
|
|
|
ber_get_option( NULL, LBER_OPT_BER_DEBUG, &level );
|
2000-10-13 04:01:12 +08:00
|
|
|
#ifdef HAVE_VSNPRINTF
|
2000-10-25 23:33:39 +08:00
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
vsnprintf( buf, sizeof(buf)-1, fmt, vl );
|
2000-10-13 04:01:12 +08:00
|
|
|
#elif HAVE_VSPRINTF
|
2000-10-25 23:33:39 +08:00
|
|
|
vsprintf( buf, fmt, vl ); /* hope it's not too long */
|
2000-10-13 04:01:12 +08:00
|
|
|
#else
|
2000-10-25 23:33:39 +08:00
|
|
|
/* use doprnt() */
|
|
|
|
#error "vsprintf() required.";
|
2000-10-13 04:01:12 +08:00
|
|
|
#endif
|
2000-10-25 23:33:39 +08:00
|
|
|
if ( ber_log_check( LDAP_DEBUG_BER, level ) )
|
|
|
|
(*ber_pvt_log_print)( buf );
|
|
|
|
}
|
2000-10-13 04:01:12 +08:00
|
|
|
va_end(vl);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-08-24 08:27:53 +08:00
|
|
|
int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... )
|
1998-12-22 09:34:01 +08:00
|
|
|
{
|
|
|
|
char buf[ 1024 ];
|
|
|
|
va_list ap;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( fmt != NULL );
|
|
|
|
|
|
|
|
if ( !ber_log_check( errlvl, loglvl )) {
|
1998-12-22 09:34:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-08-24 08:27:53 +08:00
|
|
|
va_start( ap, fmt );
|
|
|
|
|
1998-12-22 09:34:01 +08:00
|
|
|
#ifdef HAVE_VSNPRINTF
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
vsnprintf( buf, sizeof(buf)-1, fmt, ap );
|
1998-12-24 09:31:40 +08:00
|
|
|
#elif HAVE_VSPRINTF
|
1998-12-22 09:34:01 +08:00
|
|
|
vsprintf( buf, fmt, ap ); /* hope it's not too long */
|
|
|
|
#else
|
|
|
|
/* use doprnt() */
|
1999-05-19 09:12:33 +08:00
|
|
|
#error "vsprintf() required."
|
1998-12-22 09:34:01 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
(*ber_pvt_log_print)( buf );
|
1998-12-22 09:34:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2000-09-16 02:10:52 +08:00
|
|
|
|
2000-09-15 10:51:34 +08:00
|
|
|
#if 0
|
1999-05-19 09:12:33 +08:00
|
|
|
static int ber_log_puts(int errlvl, int loglvl, char *buf)
|
1998-12-22 09:34:01 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( buf != NULL );
|
|
|
|
|
|
|
|
if ( !ber_log_check( errlvl, loglvl )) {
|
1998-12-22 09:34:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
(*ber_pvt_log_print)( buf );
|
1998-12-22 09:34:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2000-09-15 10:51:34 +08:00
|
|
|
#endif
|
2000-09-16 02:10:52 +08:00
|
|
|
|
1998-10-27 15:50:07 +08:00
|
|
|
/*
|
|
|
|
* Print arbitrary stuff, for debugging.
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-12-22 09:34:01 +08:00
|
|
|
int
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_log_bprint(int errlvl,
|
|
|
|
int loglvl,
|
|
|
|
const char *data,
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_len_t len )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( data != NULL );
|
|
|
|
|
|
|
|
if ( !ber_log_check( errlvl, loglvl )) {
|
1998-12-22 09:34:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
1998-10-25 09:41:42 +08:00
|
|
|
|
1998-12-22 09:34:01 +08:00
|
|
|
ber_bprint(data, len);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_bprint(
|
|
|
|
LDAP_CONST char *data,
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_len_t len )
|
1998-12-22 09:34:01 +08:00
|
|
|
{
|
2000-09-15 06:28:45 +08:00
|
|
|
static const char hexdig[] = "0123456789abcdef";
|
2000-09-16 02:59:46 +08:00
|
|
|
#define BP_OFFSET 9
|
2000-09-15 06:28:45 +08:00
|
|
|
#define BP_GRAPH 60
|
|
|
|
#define BP_LEN 80
|
|
|
|
char line[ BP_LEN ];
|
|
|
|
ber_len_t i;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( data != NULL );
|
|
|
|
|
2000-09-15 06:28:45 +08:00
|
|
|
/* in case len is zero */
|
|
|
|
line[0] = '\n';
|
|
|
|
line[1] = '\0';
|
|
|
|
|
|
|
|
for ( i = 0 ; i < len ; i++ ) {
|
|
|
|
int n = i % 16;
|
2000-09-23 06:43:30 +08:00
|
|
|
unsigned off;
|
2000-09-15 06:28:45 +08:00
|
|
|
|
|
|
|
if( !n ) {
|
|
|
|
if( i ) (*ber_pvt_log_print)( line );
|
|
|
|
memset( line, ' ', sizeof(line)-2 );
|
|
|
|
line[sizeof(line)-2] = '\n';
|
|
|
|
line[sizeof(line)-1] = '\0';
|
|
|
|
|
|
|
|
off = i % 0x0ffffU;
|
|
|
|
|
2000-09-23 06:43:30 +08:00
|
|
|
line[ 2 ] = hexdig[ 0x0f & (off >> 12) ];
|
|
|
|
line[ 3 ] = hexdig[ 0x0f & (off >> 8) ];
|
|
|
|
line[ 4 ] = hexdig[ 0x0f & (off >> 4) ];
|
|
|
|
line[ 5 ] = hexdig[ 0x0f & off ];
|
2000-09-15 06:28:45 +08:00
|
|
|
line[ 6 ] = ':';
|
|
|
|
}
|
|
|
|
|
2000-09-16 02:10:52 +08:00
|
|
|
off = BP_OFFSET + n*3 + ((n >= 8)?1:0);
|
2000-09-23 06:43:30 +08:00
|
|
|
line[ off ] = hexdig[ 0x0f & ( data[i] >> 4 ) ];
|
|
|
|
line[ off+1 ] = hexdig[ 0x0f & data[i] ];
|
2000-09-15 06:28:45 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
off = BP_GRAPH + n + ((n >= 8)?1:0);
|
2000-09-15 06:28:45 +08:00
|
|
|
|
|
|
|
if ( isprint( data[i] )) {
|
|
|
|
line[ BP_GRAPH + n ] = data[i];
|
|
|
|
} else {
|
|
|
|
line[ BP_GRAPH + n ] = '.';
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2000-09-15 06:28:45 +08:00
|
|
|
(*ber_pvt_log_print)( line );
|
1998-12-22 09:34:01 +08:00
|
|
|
}
|
|
|
|
|
2000-10-25 23:33:39 +08:00
|
|
|
#ifdef NEW_LOGGING
|
|
|
|
int ber_output_dump(
|
2001-05-07 11:50:53 +08:00
|
|
|
const char *subsys,
|
|
|
|
int level,
|
|
|
|
BerElement *ber,
|
|
|
|
int inout )
|
2000-10-25 23:33:39 +08:00
|
|
|
{
|
|
|
|
static const char hexdig[] = "0123456789abcdef";
|
|
|
|
char buf[132];
|
|
|
|
ber_len_t len;
|
|
|
|
char line[ BP_LEN ];
|
|
|
|
ber_len_t i;
|
|
|
|
char *data = ber->ber_ptr;
|
|
|
|
|
|
|
|
if ( inout == 1 ) {
|
|
|
|
len = ber_pvt_ber_remaining(ber);
|
|
|
|
} else {
|
|
|
|
len = ber_pvt_ber_write(ber);
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf( buf, "ber_dump: buf=0x%08lx ptr=0x%08lx end=0x%08lx len=%ld\n",
|
|
|
|
(long) ber->ber_buf,
|
|
|
|
(long) ber->ber_ptr,
|
|
|
|
(long) ber->ber_end,
|
|
|
|
(long) len );
|
|
|
|
|
|
|
|
ber_pvt_log_output( subsys, level, "%s", buf );
|
|
|
|
|
|
|
|
#define BP_OFFSET 9
|
|
|
|
#define BP_GRAPH 60
|
|
|
|
#define BP_LEN 80
|
|
|
|
|
|
|
|
assert( data != NULL );
|
|
|
|
|
|
|
|
/* in case len is zero */
|
|
|
|
line[0] = '\n';
|
|
|
|
line[1] = '\0';
|
|
|
|
|
|
|
|
for ( i = 0 ; i < len ; i++ ) {
|
|
|
|
int n = i % 16;
|
|
|
|
unsigned off;
|
|
|
|
|
|
|
|
if( !n ) {
|
|
|
|
if( i ) ber_pvt_log_output( subsys, level, "%s", line );
|
|
|
|
memset( line, ' ', sizeof(line)-2 );
|
|
|
|
line[sizeof(line)-2] = '\n';
|
|
|
|
line[sizeof(line)-1] = '\0';
|
|
|
|
|
|
|
|
off = i % 0x0ffffU;
|
|
|
|
|
|
|
|
line[ 2 ] = hexdig[ 0x0f & (off >> 12) ];
|
|
|
|
line[ 3 ] = hexdig[ 0x0f & (off >> 8) ];
|
|
|
|
line[ 4 ] = hexdig[ 0x0f & (off >> 4) ];
|
|
|
|
line[ 5 ] = hexdig[ 0x0f & off ];
|
|
|
|
line[ 6 ] = ':';
|
|
|
|
}
|
|
|
|
|
|
|
|
off = BP_OFFSET + n*3 + ((n >= 8)?1:0);
|
|
|
|
line[ off ] = hexdig[ 0x0f & ( data[i] >> 4 ) ];
|
|
|
|
line[ off+1 ] = hexdig[ 0x0f & data[i] ];
|
|
|
|
|
|
|
|
off = BP_GRAPH + n + ((n >= 8)?1:0);
|
|
|
|
|
|
|
|
if ( isprint( data[i] )) {
|
|
|
|
line[ BP_GRAPH + n ] = data[i];
|
|
|
|
} else {
|
|
|
|
line[ BP_GRAPH + n ] = '.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ber_pvt_log_output( subsys, level, "%s", line );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-12-22 09:34:01 +08:00
|
|
|
int
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_log_dump(
|
|
|
|
int errlvl,
|
|
|
|
int loglvl,
|
1999-10-20 02:57:38 +08:00
|
|
|
BerElement *ber,
|
1999-05-19 09:12:33 +08:00
|
|
|
int inout )
|
1998-12-22 09:34:01 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
|
|
|
if ( !ber_log_check( errlvl, loglvl )) {
|
1998-12-22 09:34:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ber_dump(ber, inout);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_dump(
|
1999-10-20 02:57:38 +08:00
|
|
|
BerElement *ber,
|
1999-05-19 09:12:33 +08:00
|
|
|
int inout )
|
1998-12-22 09:34:01 +08:00
|
|
|
{
|
|
|
|
char buf[132];
|
2000-09-15 13:56:37 +08:00
|
|
|
ber_len_t len;
|
1998-12-22 09:34:01 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
2000-09-15 13:56:37 +08:00
|
|
|
if ( inout == 1 ) {
|
|
|
|
len = ber_pvt_ber_remaining(ber);
|
|
|
|
} else {
|
|
|
|
len = ber_pvt_ber_write(ber);
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf( buf, "ber_dump: buf=0x%08lx ptr=0x%08lx end=0x%08lx len=%ld\n",
|
1998-12-22 09:34:01 +08:00
|
|
|
(long) ber->ber_buf,
|
|
|
|
(long) ber->ber_ptr,
|
2000-09-15 13:56:37 +08:00
|
|
|
(long) ber->ber_end,
|
|
|
|
(long) len );
|
1998-12-22 09:34:01 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
(*ber_pvt_log_print)( buf );
|
1998-12-22 09:34:01 +08:00
|
|
|
|
2000-09-15 13:56:37 +08:00
|
|
|
ber_bprint( ber->ber_ptr, len );
|
1998-12-22 09:34:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-09-06 12:33:41 +08:00
|
|
|
ber_log_sos_dump(
|
1999-05-19 09:12:33 +08:00
|
|
|
int errlvl,
|
|
|
|
int loglvl,
|
1999-10-20 02:57:38 +08:00
|
|
|
Seqorset *sos )
|
1998-12-22 09:34:01 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( sos != NULL );
|
|
|
|
|
|
|
|
if ( !ber_log_check( errlvl, loglvl )) {
|
1998-12-22 09:34:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ber_sos_dump( sos );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_sos_dump(
|
1999-10-20 02:57:38 +08:00
|
|
|
Seqorset *sos )
|
1998-12-22 09:34:01 +08:00
|
|
|
{
|
|
|
|
char buf[132];
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( sos != NULL );
|
|
|
|
|
|
|
|
(*ber_pvt_log_print)( "*** sos dump ***\n" );
|
1998-12-22 09:34:01 +08:00
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
while ( sos != NULL ) {
|
1998-12-22 09:34:01 +08:00
|
|
|
sprintf( buf, "ber_sos_dump: clen %ld first 0x%lx ptr 0x%lx\n",
|
1999-06-19 07:53:05 +08:00
|
|
|
(long) sos->sos_clen,
|
|
|
|
(long) sos->sos_first,
|
|
|
|
(long) sos->sos_ptr );
|
1999-05-19 09:12:33 +08:00
|
|
|
(*ber_pvt_log_print)( buf );
|
1998-12-22 09:34:01 +08:00
|
|
|
|
|
|
|
sprintf( buf, " current len %ld contents:\n",
|
|
|
|
(long) (sos->sos_ptr - sos->sos_first) );
|
1999-05-19 09:12:33 +08:00
|
|
|
(*ber_pvt_log_print)( buf );
|
1998-12-22 09:34:01 +08:00
|
|
|
|
|
|
|
ber_bprint( sos->sos_first, sos->sos_ptr - sos->sos_first );
|
|
|
|
|
|
|
|
sos = sos->sos_next;
|
|
|
|
}
|
1998-10-25 09:41:42 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
(*ber_pvt_log_print)( "*** end dump ***\n" );
|
1999-05-19 14:13:44 +08:00
|
|
|
}
|