Fix bug # 139535 - ldapsearch: filter patterns that contain %* cause crash.

Avoid calling sprintf() if there is no valid filter pattern.
Also eliminated a few Linux compiler warnings.


git-svn-id: svn://10.0.0.236/trunk@119657 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
mcs%netscape.com 2002-04-24 00:30:20 +00:00
parent 1995ad529b
commit 2fe4ecc1c7
3 changed files with 47 additions and 43 deletions

View File

@ -114,7 +114,7 @@ main( int argc, char **argv )
}
}
} else {
while ( contoper || !LDAPTOOL_RESULT_IS_AN_ERROR( rc ) &&
while (( contoper || !LDAPTOOL_RESULT_IS_AN_ERROR( rc )) &&
fgets(buf, sizeof(buf), ldaptool_fp) != NULL) {
buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
if ( *buf != '\0' ) {

View File

@ -296,8 +296,7 @@ static void
options_callback( int option, char *optarg )
{
char *s, *temp_arg, *ps_ptr, *ps_arg;
int i=0;
switch( option ) {
case 'u': /* include UFN */
++includeufn;
@ -444,7 +443,7 @@ options_callback( int option, char *optarg )
fprintf (stderr, "Invalid argument for -C\n");
usage();
}
if (ps_ptr=strtok(NULL, ":")) {
if (NULL != (ps_ptr=strtok(NULL, ":"))) {
if ( (temp_arg = strdup( ps_ptr )) == NULL ) {
perror ("strdup");
exit (LDAP_NO_MEMORY);
@ -453,13 +452,13 @@ options_callback( int option, char *optarg )
fprintf (stderr, "Invalid argument for -C\n");
usage();
}
if (ps_ptr=strtok(NULL, ":")) {
if (NULL != (ps_ptr=strtok(NULL, ":"))) {
if ( (changesonly = ldaptool_boolean_str2value(ps_ptr, 0)) == -1) {
fprintf(stderr, "Invalid option value: %s\n", ps_ptr);
usage();
}
}
if (ps_ptr=strtok(NULL, ":")) {
if (NULL != (ps_ptr=strtok(NULL, ":"))) {
if ( (return_echg_ctls = ldaptool_boolean_str2value(ps_ptr, 0)) == -1) {
fprintf(stderr, "Invalid option value: %s\n", ps_ptr);
usage();
@ -516,29 +515,39 @@ dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
LDAPVirtualList vlv_data;
int msgid = 0;
int length = 0;
int mallocd_filter = 0;
length = strlen( filtpatt ) + strlen ( value ) +1;
if ( length > BUFSIZ ) {
if ((filterp = (char *)
malloc ( length )) == NULL) {
perror( "filter and/or pattern too long?" );
exit (LDAP_PARAM_ERROR);
}
if ( strstr( filtpatt, "%s" ) == NULL ) { /* no need to sprintf() */
filterp = filtpatt;
} else {
filterp = filter;
}
length = strlen( filtpatt ) + strlen ( value ) +1;
if ( length > BUFSIZ ) {
if ((filterp = (char *)
malloc ( length )) == NULL) {
perror( "filter and/or pattern too long?" );
exit (LDAP_PARAM_ERROR);
}
mallocd_filter = 1;
} else {
filterp = filter;
}
#ifdef HAVE_SNPRINTF
if ( snprintf( filterp, length, filtpatt, value ) < 0 ) {
perror( "snprintf filter (filter and/or pattern too long?)" );
exit( LDAP_PARAM_ERROR );
}
if ( snprintf( filterp, length, filtpatt, value ) < 0 ) {
perror( "snprintf filter (filter and/or pattern too long?)" );
exit( LDAP_PARAM_ERROR );
}
#else
sprintf( filterp, filtpatt, value );
sprintf( filterp, filtpatt, value );
#endif
}
if ( *filterp == '\0' ) { /* treat empty filter is a shortcut for oc=* */
strcpy( filterp, "(objectclass=*)" );
if (mallocd_filter) {
free(filterp);
mallocd_filter = 0;
}
filterp = "(objectclass=*)";
}
if ( ldaptool_verbose ) {
@ -554,8 +563,7 @@ dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
}
if ( ldaptool_not ) {
if (filterp != filter)
free (filterp);
if (mallocd_filter) free(filterp);
return( LDAP_SUCCESS );
}
@ -615,8 +623,7 @@ dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
rc = ldap_create_sort_control(ld,keylist,0,&ldctrl);
ldap_free_sort_keylist(keylist);
if ( rc != LDAP_SUCCESS ) {
if (filterp != filter)
free (filterp);
if (mallocd_filter) free(filterp);
return( ldaptool_print_lderror( ld, "ldap_create_sort_control",
LDAPTOOL_CHECK4SSL_IF_APPROP ));
}
@ -656,16 +663,14 @@ dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
}
if ( rc != LDAP_SUCCESS ) {
if (filterp != filter)
free (filterp);
if (mallocd_filter) free(filterp);
return( ldaptool_print_lderror( ld, "ldap_create_sort_control",
LDAPTOOL_CHECK4SSL_IF_APPROP ));
}
if (LDAP_SUCCESS != (rc = ldap_create_virtuallist_control(ld,
&vlv_data, &ldctrl)))
{
if (filterp != filter)
free (filterp);
if (mallocd_filter) free(filterp);
return( ldaptool_print_lderror( ld,
"ldap_create_virtuallist_control",
LDAPTOOL_CHECK4SSL_IF_APPROP ));
@ -678,8 +683,7 @@ dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
if ( ldap_search_ext( ld, base, scope, filterp, attrs, attrsonly,
ldaptool_request_ctrls, NULL, NULL, -1, &msgid )
!= LDAP_SUCCESS ) {
if (filterp != filter)
free (filterp);
if (mallocd_filter) free(filterp);
return( ldaptool_print_lderror( ld, "ldap_search",
LDAPTOOL_CHECK4SSL_IF_APPROP ));
}
@ -719,8 +723,7 @@ dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
}
}
if ( rc == -1 ) {
if (filterp != filter)
free (filterp);
if (mallocd_filter) free(filterp);
return( ldaptool_print_lderror( ld, "ldap_result",
LDAPTOOL_CHECK4SSL_IF_APPROP ));
}
@ -743,8 +746,7 @@ dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
LDAPTOOL_CHECK4SSL_IF_APPROP );
ldap_controls_free(ctrl_response_array);
ldap_msgfree(res);
if (filterp != filter)
free (filterp);
if (mallocd_filter) free(filterp);
return ( ldap_get_lderrno( ld, NULL, NULL ) );
}
@ -771,8 +773,7 @@ dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
LDAPTOOL_CHECK4SSL_IF_APPROP );
ldap_controls_free(ctrl_response_array);
ldap_msgfree(res);
if (filterp != filter)
free (filterp);
if (mallocd_filter) free(filterp);
return ( ldap_get_lderrno( ld, NULL, NULL ) );
}
@ -819,8 +820,7 @@ dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
ldap_value_free( refs );
}
if (filterp != filter)
free (filterp);
if (mallocd_filter) free(filterp);
ldap_msgfree( res );
return( rc );
@ -858,7 +858,7 @@ print_entry( ld, entry, attrsonly )
LDAPControl **ectrls;
int chgtype, chgnumpresent;
long chgnum;
char *prevdn, intbuf[ 128 ];
char *prevdn, longbuf[ 128 ];
if ( ldap_get_entry_controls( ld, entry, &ectrls ) == LDAP_SUCCESS ) {
if ( ldap_parse_entrychange_control( ld, ectrls, &chgtype,
@ -867,10 +867,10 @@ print_entry( ld, entry, attrsonly )
LDAPTOOL_PSEARCH_ATTR_PREFIX "changeType",
changetype_num2string( chgtype ), 0 );
if ( chgnumpresent ) {
sprintf( intbuf, "%d", chgnum );
sprintf( longbuf, "%ld", chgnum );
write_string_attr_value(
LDAPTOOL_PSEARCH_ATTR_PREFIX "changeNumber",
intbuf, 0 );
longbuf, 0 );
}
if ( NULL != prevdn ) {
write_string_attr_value(

View File

@ -63,6 +63,10 @@ extern int getopt (int argc, char *const *argv, const char *optstring);
#define LDAPTOOL_MKTEMP( p ) mktemp( p )
#endif
#ifdef LINUX
#include <getopt.h> /* not always included from unistd.h */
#endif
#include <ctype.h>
#ifndef SCOOS