X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=clients%2Ftools%2Fldapdelete.c;h=71f13cebe82c6999d7bee226f509ba6f8d53c25f;hb=6911e557deb4ec5061bac1158f6c1e9ecf0de732;hp=041a73d214b61d8ce177cf5f58331ec6facd348b;hpb=e3a4c4ec9c640c03612f90bec1e95bfc55a54947;p=openldap diff --git a/clients/tools/ldapdelete.c b/clients/tools/ldapdelete.c index 041a73d214..71f13cebe8 100644 --- a/clients/tools/ldapdelete.c +++ b/clients/tools/ldapdelete.c @@ -2,10 +2,8 @@ /* $OpenLDAP$ */ /* This work is part of OpenLDAP Software . * - * Copyright 1998-2003 The OpenLDAP Foundation. + * Copyright 1998-2006 The OpenLDAP Foundation. * Portions Copyright 1998-2003 Kurt D. Zeilenga. - * Portions Copyright 1998-2001 Net Boolean Incorporated. - * Portions Copyright 2001-2003 IBM Corporation. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -41,6 +39,8 @@ #include #include #include +#include +#include #include #include "lutil.h" @@ -76,7 +76,7 @@ usage( void ) const char options[] = "r" - "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z"; + "cd:D:e:f:h:H:IkKMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z"; int handle_private_option( int i ) @@ -85,7 +85,7 @@ handle_private_option( int i ) #if 0 int crit; char *control, *cvalue; - case 'E': /* delete controls */ + case 'E': /* delete extensions */ if( protocol == LDAP_VERSION2 ) { fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"), prog, protocol ); @@ -107,7 +107,7 @@ handle_private_option( int i ) if ( (cvalue = strchr( control, '=' )) != NULL ) { *cvalue++ = '\0'; } - fprintf( stderr, _("Invalid delete control name: %s\n"), control ); + fprintf( stderr, _("Invalid delete extension name: %s\n"), control ); usage(); #endif @@ -141,7 +141,7 @@ main( int argc, char **argv ) fp = NULL; - tool_init(); + tool_init( TOOL_DELETE ); prog = lutil_progname( "ldapdelete", argc, argv ); tool_args( argc, argv ); @@ -171,13 +171,11 @@ main( int argc, char **argv ) tool_bind( ld ); - if ( assertion || authzid || manageDSAit || noop ) { - tool_server_controls( ld, NULL, 0 ); - } + tool_server_controls( ld, NULL, 0 ); retval = rc = 0; - if ( fp == NULL ) { + if ( fp == NULL ) { for ( ; optind < argc; ++optind ) { rc = dodelete( ld, argv[ optind ] ); @@ -199,9 +197,9 @@ main( int argc, char **argv ) } } - ldap_unbind( ld ); - - return( retval ); + tool_unbind( ld ); + tool_destroy(); + return retval; } @@ -216,10 +214,10 @@ static int dodelete( if ( verbose ) { printf( _("%sdeleting entry \"%s\"\n"), - (not ? "!" : ""), dn ); + (dont ? "!" : ""), dn ); } - if ( not ) { + if ( dont ) { return LDAP_SUCCESS; } @@ -235,10 +233,25 @@ static int dodelete( return rc; } - rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res ); - if ( rc < 0 ) { - ldap_perror( ld, "ldapdelete: ldap_result" ); - return rc; + for ( ; ; ) { + struct timeval tv; + + if ( tool_check_abandon( ld, id ) ) { + return LDAP_CANCELLED; + } + + tv.tv_sec = 0; + tv.tv_usec = 100000; + + rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); + if ( rc < 0 ) { + tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); + return rc; + } + + if ( rc != 0 ) { + break; + } } rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 ); @@ -249,10 +262,13 @@ static int dodelete( return rc; } - if( verbose || code != LDAP_SUCCESS || - (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) ) + if( code != LDAP_SUCCESS ) { + tool_perror( "ldap_delete", code, NULL, matcheddn, text, refs ); + } else if ( verbose && + ((matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )) { - printf( _("Delete Result: %s (%d)\n"), ldap_err2string( code ), code ); + printf( _("Delete Result: %s (%d)\n"), + ldap_err2string( code ), code ); if( text && *text ) { printf( _("Additional info: %s\n"), text ); @@ -279,7 +295,6 @@ static int dodelete( /* * Delete all the children of an entry recursively until leaf nodes are reached. - * */ static int deletechildren( LDAP *ld, @@ -288,9 +303,13 @@ static int deletechildren( LDAPMessage *res, *e; int entries; int rc; - static char *attrs[] = { "1.1", NULL }; + static char *attrs[] = { LDAP_NO_ATTRS, NULL }; + LDAPControl c, *ctrls[2]; + BerElement *ber = NULL; + LDAPMessage *res_se; if ( verbose ) printf ( _("deleting children of: %s\n"), dn ); + /* * Do a one level search at dn for children. For each, delete its children. */ @@ -298,7 +317,7 @@ static int deletechildren( rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1, NULL, NULL, NULL, -1, &res ); if ( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_search" ); + tool_perror( "ldap_search", rc, NULL, NULL, NULL, NULL ); return( rc ); } @@ -313,15 +332,15 @@ static int deletechildren( char *dn = ldap_get_dn( ld, e ); if( dn == NULL ) { - ldap_perror( ld, "ldap_prune" ); - ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc ); + ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc ); + tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL ); ber_memfree( dn ); return rc; } rc = deletechildren( ld, dn ); if ( rc == -1 ) { - ldap_perror( ld, "ldap_prune" ); + tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL ); ber_memfree( dn ); return rc; } @@ -330,9 +349,9 @@ static int deletechildren( printf( _("\tremoving %s\n"), dn ); } - rc = ldap_delete_s( ld, dn ); + rc = ldap_delete_ext_s( ld, dn, NULL, NULL ); if ( rc == -1 ) { - ldap_perror( ld, "ldap_delete" ); + tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL ); ber_memfree( dn ); return rc; @@ -347,5 +366,73 @@ static int deletechildren( } ldap_msgfree( res ); + + /* + * Do a one level search at dn for subentry children. + */ + + if ((ber = ber_alloc_t(LBER_USE_DER)) == NULL) { + return EXIT_FAILURE; + } + rc = ber_printf( ber, "b", 1 ); + if ( rc == -1 ) { + ber_free( ber, 1 ); + fprintf( stderr, _("Subentries control encoding error!\n")); + return EXIT_FAILURE; + } + if ( ber_flatten2( ber, &c.ldctl_value, 0 ) == -1 ) { + return EXIT_FAILURE; + } + c.ldctl_oid = LDAP_CONTROL_SUBENTRIES; + c.ldctl_iscritical = 1; + ctrls[0] = &c; + ctrls[1] = NULL; + + rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1, + ctrls, NULL, NULL, -1, &res_se ); + if ( rc != LDAP_SUCCESS ) { + tool_perror( "ldap_search", rc, NULL, NULL, NULL, NULL ); + return( rc ); + } + ber_free( ber, 1 ); + + entries = ldap_count_entries( ld, res_se ); + + if ( entries > 0 ) { + int i; + + for (e = ldap_first_entry( ld, res_se ), i = 0; e != NULL; + e = ldap_next_entry( ld, e ), i++ ) + { + char *dn = ldap_get_dn( ld, e ); + + if( dn == NULL ) { + ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc ); + tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL ); + ber_memfree( dn ); + return rc; + } + + if ( verbose ) { + printf( _("\tremoving %s\n"), dn ); + } + + rc = ldap_delete_ext_s( ld, dn, NULL, NULL ); + if ( rc == -1 ) { + tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL ); + ber_memfree( dn ); + return rc; + + } + + if ( verbose ) { + printf( _("\t%s removed\n"), dn ); + } + + ber_memfree( dn ); + } + } + + ldap_msgfree( res_se ); return rc; }