X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=servers%2Fslurpd%2Fre.c;h=e0d4ddd8f10ce89c7c5e3e8fa2f67196bc872a44;hb=a6e232e7c1c6815b214e5f920459cd63ceeb54e7;hp=ea2960f67142255b964c733445b79931151422bc;hpb=e9233c923ddb31a1302309e0446a8e8ffacef0f9;p=openldap
diff --git a/servers/slurpd/re.c b/servers/slurpd/re.c
index ea2960f671..e0d4ddd8f1 100644
--- a/servers/slurpd/re.c
+++ b/servers/slurpd/re.c
@@ -1,5 +1,18 @@
-/*
- * Copyright (c) 1996 Regents of the University of Michigan.
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software .
+ *
+ * Copyright 1998-2007 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).
+ */
/*
* re.c - routines which deal with Re (Replication entry) structures.
@@ -18,30 +35,29 @@
*/
+#include "portable.h"
+
#include
-#include
-#include
-#include
+
+#include
+#include
+#include
+#include
+#include
#include "../slapd/slap.h"
+
#include "slurp.h"
#include "globals.h"
-
-/* externs */
-extern char *str_getline( char **next );
-extern void ch_free( char *p );
-
-#ifndef SYSERRLIST_IN_STDIO
-extern char *sys_errlist[];
-#endif /* SYSERRLIST_IN_STDIO */
+#include "lutil.h"
/* Forward references */
-static Rh *get_repl_hosts( char *, int *, char ** );
-static int gettype( char * );
-static int getchangetype( char *);
-static int Re_parse( Re *re, char *replbuf );
-static void Re_dump( Re *re, FILE *fp );
-static void warn_unknown_replica( char *, int port );
+static Rh *get_repl_hosts LDAP_P(( char *, int *, char ** ));
+static int gettype LDAP_P(( char * ));
+static int getchangetype LDAP_P(( char * ));
+static int Re_parse LDAP_P(( Re *re, char *replbuf ));
+static void Re_dump LDAP_P(( Re *re, FILE *fp ));
+static void warn_unknown_replica LDAP_P(( char *, int port ));
/* Globals, scoped within this file */
static int nur = 0; /* Number of unknown replicas */
@@ -64,6 +80,7 @@ Re_getnext(
/*
* Free an Re
+ * ??? Something should apparently return nonzero here, but I dont know what.
*/
static int
Re_free(
@@ -82,11 +99,9 @@ Re_free(
"Warning: freeing re (dn: %s) with nonzero refcnt\n",
re->re_dn, 0, 0 );
}
-#if !defined( THREAD_SUNOS4_LWP )
- /* This seems to have problems under SunOS lwp */
- pthread_mutex_destroy( &re->re_mutex );
-#endif /* THREAD_SUNOS4_LWP */
- ch_free( re->re_timestamp );
+
+ ldap_pvt_thread_mutex_destroy( &re->re_mutex );
+
if (( rh = re->re_replicas ) != NULL ) {
for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
free( rh[ i ].rh_hostname );
@@ -102,6 +117,7 @@ Re_free(
free( mi );
}
free( re );
+ return 0;
}
@@ -127,9 +143,9 @@ Re_parse(
int state;
int nml;
char *buf, *rp, *p;
- long buflen;
+ size_t buflen;
char *type, *value;
- int len;
+ ber_len_t len;
int nreplicas;
if ( re == NULL ) {
@@ -149,7 +165,7 @@ Re_parse(
re->re_refcnt = sglob->num_replicas;
for (;;) {
- if (( state == GOT_ALL ) || ( buf = str_getline( &rp )) == NULL ) {
+ if (( state == GOT_ALL ) || ( buf = ldif_getline( &rp )) == NULL ) {
break;
}
/*
@@ -160,8 +176,8 @@ Re_parse(
if ( strncmp( buf, ERROR_STR, strlen( ERROR_STR )) == 0 ) {
continue;
}
- buflen = ( long ) strlen( buf );
- if ( str_parse_line( buf, &type, &value, &len ) < 0 ) {
+ buflen = strlen( buf );
+ if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
Debug( LDAP_DEBUG_ANY,
"Error: Re_parse: malformed replog file\n",
0, 0, 0 );
@@ -172,20 +188,33 @@ Re_parse(
re->re_changetype = getchangetype( value );
state |= GOT_CHANGETYPE;
break;
- case T_TIME:
+ case T_TIME: {
+ unsigned long t;
+
if (( p = strchr( value, '.' )) != NULL ) {
/* there was a sequence number */
*p++ = '\0';
}
- re->re_timestamp = strdup( value );
- if ( p != NULL && isdigit( *p )) {
- re->re_seq = atoi( p );
+ if ( lutil_atoul( &t, value ) != 0 ) {
+ Debug( LDAP_DEBUG_ANY,
+ "Error: Re_parse: unable to parse timestamp \"%s\"\n",
+ value, 0, 0 );
+ return -1;
+ }
+ re->re_timestamp = (time_t)t;
+ if ( p != NULL && isdigit( (unsigned char) *p )
+ && lutil_atoi( &re->re_seq, p ) != 0 )
+ {
+ Debug( LDAP_DEBUG_ANY,
+ "Error: Re_parse: unable to parse sequence number \"%s\"\n",
+ p, 0, 0 );
+ return -1;
}
state |= GOT_TIME;
- break;
+ } break;
case T_DN:
re->re_dn = ch_malloc( len + 1 );
- memcpy( re->re_dn, value, len );
+ AC_MEMCPY( re->re_dn, value, len );
re->re_dn[ len ]='\0';
state |= GOT_DN;
break;
@@ -194,9 +223,13 @@ Re_parse(
Debug( LDAP_DEBUG_ANY,
"Error: Re_parse: bad type <%s>\n",
type, 0, 0 );
+ free( type );
+ free( value );
return -1;
}
}
+ free( type );
+ free( value );
}
if ( state != GOT_ALL ) {
@@ -207,15 +240,17 @@ Re_parse(
}
for (;;) {
- if (( buf = str_getline( &rp )) == NULL ) {
+ char *const dash = "-";
+
+ if (( buf = ldif_getline( &rp )) == NULL ) {
break;
}
- buflen = ( long ) strlen( buf );
+ buflen = strlen( buf );
if (( buflen == 1 ) && ( buf[ 0 ] == '-' )) {
- type = "-";
+ type = dash;
value = NULL;
} else {
- if ( str_parse_line( buf, &type, &value, &len ) < 0 ) {
+ if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
Debug( LDAP_DEBUG_ANY,
"Error: malformed replog line \"%s\"\n",
buf, 0, 0 );
@@ -227,7 +262,7 @@ Re_parse(
re->re_mods[ nml ].mi_type = strdup( type );
if ( value != NULL ) {
re->re_mods[ nml ].mi_val = ch_malloc( len + 1 );
- memcpy( re->re_mods[ nml ].mi_val, value, len );
+ AC_MEMCPY( re->re_mods[ nml ].mi_val, value, len );
re->re_mods[ nml ].mi_val[ len ] = '\0';
re->re_mods[ nml ].mi_len = len;
} else {
@@ -237,6 +272,11 @@ Re_parse(
re->re_mods[ nml + 1 ].mi_type = NULL;
re->re_mods[ nml + 1 ].mi_val = NULL;
nml++;
+
+ if ( type != dash )
+ free( type );
+ if ( value != NULL )
+ free( value );
}
return 0;
}
@@ -258,10 +298,10 @@ get_repl_hosts(
char **r_rp
)
{
- char buf[ LINE_WIDTH + 1 ];
char *type, *value, *line, *p;
Rh *rh = NULL;
- int nreplicas, len;
+ int nreplicas;
+ ber_len_t len;
int port;
int repl_ok;
int i;
@@ -280,7 +320,7 @@ get_repl_hosts(
for (;;) {
/* If this is a reject log, we need to skip over the ERROR: line */
if ( !strncmp( *r_rp, ERROR_STR, strlen( ERROR_STR ))) {
- line = str_getline( r_rp );
+ line = ldif_getline( r_rp );
if ( line == NULL ) {
break;
}
@@ -288,19 +328,19 @@ get_repl_hosts(
if ( strncasecmp( *r_rp, "replica:", 7 )) {
break;
}
- line = str_getline( r_rp );
+ line = ldif_getline( r_rp );
if ( line == NULL ) {
break;
}
- if ( str_parse_line( line, &type, &value, &len ) < 0 ) {
+ if ( ldif_parse_line( line, &type, &value, &len ) < 0 ) {
return( NULL );
}
port = LDAP_PORT;
if (( p = strchr( value, ':' )) != NULL ) {
*p = '\0';
p++;
- if ( *p != '\0' ) {
- port = atoi( p );
+ if ( *p != '\0' && lutil_atoi( &port, p ) != 0 ) {
+ return( NULL );
}
}
@@ -315,8 +355,10 @@ get_repl_hosts(
break;
}
}
+ free( type );
if ( !repl_ok ) {
warn_unknown_replica( value, port );
+ free( value );
continue;
}
@@ -329,6 +371,8 @@ get_repl_hosts(
rh[ nreplicas ].rh_hostname = strdup( value );
rh[ nreplicas ].rh_port = port;
nreplicas++;
+
+ free( value );
}
if ( nreplicas == 0 ) {
@@ -393,6 +437,7 @@ getchangetype(
+#if 0
/*
* Find the first line which is not a "replica:" line in buf.
* Returns a pointer to the line. Returns NULL if there are
@@ -418,6 +463,7 @@ skip_replica_lines(
}
}
}
+#endif /* 0 */
@@ -441,7 +487,7 @@ Re_dump(
}
fprintf( fp, "Re_dump: ******\n" );
fprintf( fp, "re_refcnt: %d\n", re->re_refcnt );
- fprintf( fp, "re_timestamp: %s\n", re->re_timestamp );
+ fprintf( fp, "re_timestamp: %ld\n", (long) re->re_timestamp );
fprintf( fp, "re_seq: %d\n", re->re_seq );
for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL;
i++ ) {
@@ -500,7 +546,6 @@ Re_write(
int i;
char *s;
int rc = 0;
- Rh *rh;
if ( re == NULL || fp == NULL ) {
Debug( LDAP_DEBUG_ANY, "Internal error: Re_write: NULL argument\n",
@@ -513,13 +558,20 @@ Re_write(
}
if ( ri != NULL ) { /* write a single "replica:" line */
- if ( fprintf( fp, "replica: %s:%d\n", ri->ri_hostname,
- ri->ri_port ) < 0 ) {
+ if ( ri->ri_port != 0 ) {
+ rc = fprintf( fp, "replica: %s:%d\n", ri->ri_hostname,
+ ri->ri_port );
+ } else {
+ rc = fprintf( fp, "replica: %s\n", ri->ri_hostname );
+ }
+ if ( rc < 0 ) {
rc = -1;
goto bad;
}
+ rc = 0;
+
} else { /* write multiple "replica:" lines */
- for ( i = 0; re->re_replicas[ i ].rh_hostname != NULL; i++ ) {
+ for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL; i++ ) {
if ( fprintf( fp, "replica: %s:%d\n",
re->re_replicas[ i ].rh_hostname,
re->re_replicas[ i ].rh_port ) < 0 ) {
@@ -528,7 +580,7 @@ Re_write(
}
}
}
- if ( fprintf( fp, "time: %s.%d\n", re->re_timestamp, re->re_seq ) < 0 ) {
+ if ( fprintf( fp, "time: %ld.%d\n", (long) re->re_timestamp, re->re_seq ) < 0 ) {
rc = -1;
goto bad;
}
@@ -569,7 +621,8 @@ Re_write(
}
} else {
char *obuf;
- obuf = ldif_type_and_value( re->re_mods[ i ].mi_type,
+ obuf = ldif_put( LDIF_PUT_VALUE,
+ re->re_mods[ i ].mi_type,
re->re_mods[ i ].mi_val ? re->re_mods[ i ].mi_val : "",
re->re_mods[ i ].mi_len );
if ( fputs( obuf, fp ) < 0 ) {
@@ -577,7 +630,7 @@ Re_write(
free( obuf );
goto bad;
} else {
- free( obuf );
+ ber_memfree( obuf );
}
}
}
@@ -644,7 +697,7 @@ Re_lock(
Re *re
)
{
- return( pthread_mutex_lock( &re->re_mutex ));
+ return( ldap_pvt_thread_mutex_lock( &re->re_mutex ));
}
@@ -658,7 +711,7 @@ Re_unlock(
Re *re
)
{
- return( pthread_mutex_unlock( &re->re_mutex ));
+ return( ldap_pvt_thread_mutex_unlock( &re->re_mutex ));
}
@@ -691,7 +744,7 @@ Re_init(
/* Initialize private data */
(*re)->re_refcnt = sglob->num_replicas;
- (*re)->re_timestamp = NULL;
+ (*re)->re_timestamp = (time_t) 0L;
(*re)->re_replicas = NULL;
(*re)->re_dn = NULL;
(*re)->re_changetype = 0;
@@ -699,7 +752,7 @@ Re_init(
(*re)->re_mods = NULL;
(*re)->re_next = NULL;
- pthread_mutex_init( &((*re)->re_mutex), pthread_mutexattr_default );
+ ldap_pvt_thread_mutex_init( &((*re)->re_mutex) );
return 0;
}