2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
7 * The contents of this file are subject to the Netscape Public
8 * License Version 1.1 (the "License"); you may not use this file
9 * except in compliance with the License. You may obtain a copy of
10 * the License at http://www.mozilla.org/NPL/
12 * Software distributed under the License is distributed on an "AS
13 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * rights and limitations under the License.
17 * The Original Code is Mozilla Communicator client code, released
20 * The Initial Developer of the Original Code is Netscape
21 * Communications Corporation. Portions created by Netscape are
22 * Copyright (C) 1998-1999 Netscape Communications Corporation. All
28 /* ldapdelete.c - simple program to delete an entry using LDAP */
31 #ifdef SOLARIS_LDAP_CMD
33 #endif /* SOLARIS_LDAP_CMD */
38 static int delbypasscmd
, yestodelete
;
41 #ifndef SOLARIS_LDAP_CMD
48 LDAPMessage
*result
, *e
;
49 char *my_filter
= "(objectclass=*)";
51 static int dodelete( LDAP
*ld
, char *dn
, LDAPControl
**serverctrls
);
52 static void options_callback( int option
, char *optarg
);
57 fprintf( stderr
, gettext("usage: %s [options] [dn...]\n"), ldaptool_progname
);
58 fprintf( stderr
, gettext("options:\n") );
59 ldaptool_common_usage( 0 );
60 fprintf( stderr
, gettext(" -a\t\tBy-pass confirmation question when deleting a branch\n") );
61 fprintf( stderr
, gettext( " -c\t\tcontinuous mode (do not stop on errors)\n") );
62 fprintf( stderr
, gettext( " -f file\tread DNs to delete from file (default: standard input)\n") );
63 exit( LDAP_PARAM_ERROR
);
68 main( int argc
, char **argv
)
71 int rc
, deref
, optind
;
82 #ifdef SOLARIS_LDAP_CMD
83 char *locale
= setlocale(LC_ALL
, "");
84 textdomain(TEXT_DOMAIN
);
85 #endif /* SOLARIS_LDAP_CMD */
92 optind
= ldaptool_process_args( argc
, argv
, "c", 0, options_callback
);
98 if ( ldaptool_fp
== NULL
&& optind
>= argc
) {
102 ld
= ldaptool_ldap_init( 0 );
104 deref
= LDAP_DEREF_NEVER
; /* prudent, but probably unnecessary */
105 ldap_set_option( ld
, LDAP_OPT_DEREF
, &deref
);
109 if (( ldctrl
= ldaptool_create_manage_dsait_control()) != NULL
) {
110 ldaptool_add_control_to_array( ldctrl
, ldaptool_request_ctrls
);
113 if ((ldctrl
= ldaptool_create_proxyauth_control(ld
)) !=NULL
) {
114 ldaptool_add_control_to_array( ldctrl
, ldaptool_request_ctrls
);
117 if ( ldaptool_fp
== NULL
) {
118 for ( ; optind
< argc
; ++optind
) {
121 conv
= ldaptool_local2UTF8( argv
[ optind
] );
122 rc
= dodelete( ld
, conv
, ldaptool_request_ctrls
);
129 while ((rc
== 0 || contoper
) &&
130 fgets(buf
, sizeof(buf
), ldaptool_fp
) != NULL
) {
131 buf
[ strlen( buf
) - 1 ] = '\0'; /* remove trailing newline */
132 if ( *buf
!= '\0' ) {
133 rc
= dodelete( ld
, buf
, ldaptool_request_ctrls
);
138 ldaptool_reset_control_array( ldaptool_request_ctrls
);
139 ldaptool_cleanup( ld
);
141 /* check for and report output error */
143 rc
= ldaptool_check_ferror( stdout
, rc
,
144 gettext("output error (output might be incomplete)") );
149 options_callback( int option
, char *optarg
)
152 case 'c': /* continuous operation mode */
161 dodelete( LDAP
*ld
, char *dn
, LDAPControl
**serverctrls
)
165 if ( ldaptool_verbose
) {
166 printf( gettext("%sdeleting entry %s\n"), ldaptool_not
? "!" : "", dn
);
168 if ( ldaptool_not
) {
170 } else if (( rc
= ldaptool_delete_ext_s( ld
, dn
, serverctrls
, NULL
,
171 "ldap_delete" )) == LDAP_SUCCESS
&& ldaptool_verbose
) {
172 printf( gettext("entry removed\n") );
180 /* This code broke iDS.....it will have to be revisited */
182 dodelete( LDAP
*ld
, char *dn
, LDAPControl
**serverctrls
)
189 if ( ldaptool_verbose
) {
190 printf( gettext("%sdeleting entry %s\n"), ldaptool_not
? "!" : "", dn
);
192 if ( ldaptool_not
) {
198 if ( (rc
= ldap_search_s( ld
, dn
, LDAP_SCOPE_SUBTREE
, my_filter
, NULL
, 0, &result
)) != LDAP_SUCCESS
) {
199 ldaptool_print_lderror( ld
, "ldap_search", LDAPTOOL_CHECK4SSL_IF_APPROP
);
202 for ( e
= ldap_first_entry( ld
, result
); e
!= NULL
; e
= ldap_next_entry( ld
, e
) ) {
203 if ( ( dn
= ldap_get_dn( ld
, e
) ) != NULL
) {
204 datalist
= (Element
*)malloc(sizeof(Element
));
206 L_Insert(datalist
, &HeadNode
);
209 if ( ((Head
*)&HeadNode
)->count
> 1 ) {
211 if ( delbypasscmd
== 0 ) {
212 printf( gettext("Are you sure you want to delete the entire branch rooted at %s? [no]\n"), (char *)((Element
*)(((Head
*)&HeadNode
)->first
))->data
);
214 if ( (ch
== 'Y') || (ch
== 'y')) {
223 if ( yestodelete
== 1 ) {
224 for ( datalist
= ((Head
*)&HeadNode
)->last
; datalist
; datalist
= datalist
->left
) {
226 if ((rc
= ldaptool_delete_ext_s( ld
, (char *)datalist
->data
, serverctrls
, NULL
, "ldap_delete" )) == LDAP_SUCCESS
&& ldaptool_verbose
) {
227 printf( gettext("%s entry removed\n"), (char *)datalist
->data
);
229 L_Remove(datalist
, (Head
*)&HeadNode
);
230 ldap_memfree(datalist
->data
);
234 } /* end if (yestodelete) */
238 } /* function end bracket */