1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
10*0Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
11*0Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
12*0Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
15*0Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
16*0Sstevel@tonic-gate  * implied. See the License for the specific language governing
17*0Sstevel@tonic-gate  * rights and limitations under the License.
18*0Sstevel@tonic-gate  *
19*0Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
20*0Sstevel@tonic-gate  * March 31, 1998.
21*0Sstevel@tonic-gate  *
22*0Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
23*0Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
24*0Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25*0Sstevel@tonic-gate  * Rights Reserved.
26*0Sstevel@tonic-gate  *
27*0Sstevel@tonic-gate  * Contributor(s):
28*0Sstevel@tonic-gate  */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /* ldapdelete.c - simple program to delete an entry using LDAP */
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include "ldaptool.h"
33*0Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
34*0Sstevel@tonic-gate #include <locale.h>
35*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate static int	contoper;
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #ifdef later
40*0Sstevel@tonic-gate static int	delbypasscmd, yestodelete;
41*0Sstevel@tonic-gate #endif
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
44*0Sstevel@tonic-gate #define gettext(s) s
45*0Sstevel@tonic-gate #endif
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static LDAP	*ld;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate LDAPMessage *result, *e;
51*0Sstevel@tonic-gate char *my_filter = "(objectclass=*)";
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate static int dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls );
54*0Sstevel@tonic-gate static void options_callback( int option, char *optarg );
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate static void
57*0Sstevel@tonic-gate usage( void )
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate     fprintf( stderr, gettext("usage: %s [options] [dn...]\n"), ldaptool_progname );
60*0Sstevel@tonic-gate     fprintf( stderr, gettext("options:\n") );
61*0Sstevel@tonic-gate     ldaptool_common_usage( 0 );
62*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -a\t\tBy-pass confirmation question when deleting a branch\n") );
63*0Sstevel@tonic-gate     fprintf( stderr, gettext( "    -c\t\tcontinuous mode (do not stop on errors)\n") );
64*0Sstevel@tonic-gate     fprintf( stderr, gettext( "    -f file\tread DNs to delete from file (default: standard input)\n") );
65*0Sstevel@tonic-gate     exit( LDAP_PARAM_ERROR );
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate int
70*0Sstevel@tonic-gate main( int argc, char **argv )
71*0Sstevel@tonic-gate {
72*0Sstevel@tonic-gate     char	buf[ 4096 ];
73*0Sstevel@tonic-gate     int		rc, deref, optind;
74*0Sstevel@tonic-gate     LDAPControl	*ldctrl;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate #ifdef notdef
77*0Sstevel@tonic-gate #ifdef HPUX11
78*0Sstevel@tonic-gate #ifndef __LP64__
79*0Sstevel@tonic-gate 	_main( argc, argv);
80*0Sstevel@tonic-gate #endif /* __LP64_ */
81*0Sstevel@tonic-gate #endif /* HPUX11 */
82*0Sstevel@tonic-gate #endif
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
85*0Sstevel@tonic-gate     char *locale = setlocale(LC_ALL, "");
86*0Sstevel@tonic-gate     textdomain(TEXT_DOMAIN);
87*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
88*0Sstevel@tonic-gate     contoper = 0;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate #ifdef later
91*0Sstevel@tonic-gate     delbypasscmd = 0;
92*0Sstevel@tonic-gate #endif
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate     optind = ldaptool_process_args( argc, argv, "c", 0, options_callback );
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate     if ( optind == -1 ) {
97*0Sstevel@tonic-gate 	usage();
98*0Sstevel@tonic-gate     }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate     if ( ldaptool_fp == NULL && optind >= argc ) {
101*0Sstevel@tonic-gate 	ldaptool_fp = stdin;
102*0Sstevel@tonic-gate     }
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate     ld = ldaptool_ldap_init( 0 );
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate     deref = LDAP_DEREF_NEVER;	/* prudent, but probably unnecessary */
107*0Sstevel@tonic-gate     ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate     ldaptool_bind( ld );
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate     if (( ldctrl = ldaptool_create_manage_dsait_control()) != NULL ) {
112*0Sstevel@tonic-gate 	ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
113*0Sstevel@tonic-gate     }
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate     if ((ldctrl = ldaptool_create_proxyauth_control(ld)) !=NULL) {
116*0Sstevel@tonic-gate 	ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
117*0Sstevel@tonic-gate     }
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate     if ( ldaptool_fp == NULL ) {
120*0Sstevel@tonic-gate 	for ( ; optind < argc; ++optind ) {
121*0Sstevel@tonic-gate             char *conv;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate             conv = ldaptool_local2UTF8( argv[ optind ] );
124*0Sstevel@tonic-gate 	    rc = dodelete( ld, conv, ldaptool_request_ctrls );
125*0Sstevel@tonic-gate             if( conv != NULL ) {
126*0Sstevel@tonic-gate                 free( conv );
127*0Sstevel@tonic-gate             }
128*0Sstevel@tonic-gate 	}
129*0Sstevel@tonic-gate     } else {
130*0Sstevel@tonic-gate 	rc = 0;
131*0Sstevel@tonic-gate 	while ((rc == 0 || contoper) &&
132*0Sstevel@tonic-gate 		fgets(buf, sizeof(buf), ldaptool_fp) != NULL) {
133*0Sstevel@tonic-gate 	    buf[ strlen( buf ) - 1 ] = '\0';	/* remove trailing newline */
134*0Sstevel@tonic-gate 	    if ( *buf != '\0' ) {
135*0Sstevel@tonic-gate 	          rc = dodelete( ld, buf, ldaptool_request_ctrls );
136*0Sstevel@tonic-gate 	    }
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate     }
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate     ldaptool_reset_control_array( ldaptool_request_ctrls );
141*0Sstevel@tonic-gate     ldaptool_cleanup( ld );
142*0Sstevel@tonic-gate     return( rc );
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate static void
146*0Sstevel@tonic-gate options_callback( int option, char *optarg )
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate     switch( option ) {
149*0Sstevel@tonic-gate     case 'c':	/* continuous operation mode */
150*0Sstevel@tonic-gate 	++contoper;
151*0Sstevel@tonic-gate 	break;
152*0Sstevel@tonic-gate     default:
153*0Sstevel@tonic-gate 	usage();
154*0Sstevel@tonic-gate     }
155*0Sstevel@tonic-gate }
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate static int
158*0Sstevel@tonic-gate dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls )
159*0Sstevel@tonic-gate {
160*0Sstevel@tonic-gate     int         rc;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate     if ( ldaptool_verbose ) {
163*0Sstevel@tonic-gate         printf( gettext("%sdeleting entry %s\n"), ldaptool_not ? "!" : "", dn );
164*0Sstevel@tonic-gate     }
165*0Sstevel@tonic-gate     if ( ldaptool_not ) {
166*0Sstevel@tonic-gate         rc = LDAP_SUCCESS;
167*0Sstevel@tonic-gate     } else if (( rc = ldaptool_delete_ext_s( ld, dn, serverctrls, NULL,
168*0Sstevel@tonic-gate             "ldap_delete" )) == LDAP_SUCCESS && ldaptool_verbose ) {
169*0Sstevel@tonic-gate         printf( gettext("entry removed\n") );
170*0Sstevel@tonic-gate     }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate     return( rc );
173*0Sstevel@tonic-gate }
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate #ifdef later
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate /* This code broke iDS.....it will have to be revisited */
178*0Sstevel@tonic-gate static int
179*0Sstevel@tonic-gate dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls )
180*0Sstevel@tonic-gate {
181*0Sstevel@tonic-gate     int rc;
182*0Sstevel@tonic-gate     Head HeadNode;
183*0Sstevel@tonic-gate     Element *datalist;
184*0Sstevel@tonic-gate     char ch;
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate     if ( ldaptool_verbose ) {
187*0Sstevel@tonic-gate         printf( gettext("%sdeleting entry %s\n"), ldaptool_not ? "!" : "", dn );
188*0Sstevel@tonic-gate     }
189*0Sstevel@tonic-gate     if ( ldaptool_not ) {
190*0Sstevel@tonic-gate         rc = LDAP_SUCCESS;
191*0Sstevel@tonic-gate     }
192*0Sstevel@tonic-gate     else { /* else 1 */
193*0Sstevel@tonic-gate        L_Init(&HeadNode);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate        if ( (rc = ldap_search_s( ld, dn, LDAP_SCOPE_SUBTREE, my_filter, NULL, 0, &result )) != LDAP_SUCCESS ) {
196*0Sstevel@tonic-gate           ldaptool_print_lderror( ld, "ldap_search", LDAPTOOL_CHECK4SSL_IF_APPROP );
197*0Sstevel@tonic-gate        }
198*0Sstevel@tonic-gate        else { /* else 2 */
199*0Sstevel@tonic-gate           for ( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) {
200*0Sstevel@tonic-gate              if ( ( dn = ldap_get_dn( ld, e ) ) != NULL ) {
201*0Sstevel@tonic-gate                 datalist = (Element *)malloc(sizeof(Element));
202*0Sstevel@tonic-gate                 datalist->data = dn;
203*0Sstevel@tonic-gate 	        L_Insert(datalist, &HeadNode);
204*0Sstevel@tonic-gate              }
205*0Sstevel@tonic-gate           }
206*0Sstevel@tonic-gate           if ( ((Head *)&HeadNode)->count > 1 ) {
207*0Sstevel@tonic-gate              yestodelete = 0;
208*0Sstevel@tonic-gate              if ( delbypasscmd == 0 ) {
209*0Sstevel@tonic-gate                 printf( gettext("Are you sure you want to delete the entire branch rooted at %s? [no]\n"), (char *)((Element *)(((Head *)&HeadNode)->first))->data);
210*0Sstevel@tonic-gate                 ch = getchar();
211*0Sstevel@tonic-gate                 if ( (ch == 'Y') || (ch == 'y')) {
212*0Sstevel@tonic-gate                    yestodelete = 1;
213*0Sstevel@tonic-gate                 }
214*0Sstevel@tonic-gate              } else {
215*0Sstevel@tonic-gate                   yestodelete = 1;
216*0Sstevel@tonic-gate              }
217*0Sstevel@tonic-gate           } else {
218*0Sstevel@tonic-gate 	       yestodelete = 1;
219*0Sstevel@tonic-gate           }
220*0Sstevel@tonic-gate           if ( yestodelete == 1 ) {
221*0Sstevel@tonic-gate              for ( datalist = ((Head *)&HeadNode)->last; datalist; datalist = datalist->left ) {
222*0Sstevel@tonic-gate                 if (datalist)  {
223*0Sstevel@tonic-gate                    if ((rc = ldaptool_delete_ext_s( ld, (char *)datalist->data, serverctrls, NULL, "ldap_delete" )) == LDAP_SUCCESS && ldaptool_verbose ) {
224*0Sstevel@tonic-gate                       printf( gettext("%s entry removed\n"), (char *)datalist->data );
225*0Sstevel@tonic-gate                    }
226*0Sstevel@tonic-gate                    L_Remove(datalist, (Head *)&HeadNode);
227*0Sstevel@tonic-gate                    ldap_memfree(datalist->data);
228*0Sstevel@tonic-gate                    free ( datalist );
229*0Sstevel@tonic-gate                 }
230*0Sstevel@tonic-gate              }
231*0Sstevel@tonic-gate            } /* end if (yestodelete) */
232*0Sstevel@tonic-gate       } /* else 2 */
233*0Sstevel@tonic-gate     } /* else 1 */
234*0Sstevel@tonic-gate     return (rc);
235*0Sstevel@tonic-gate } /* function end bracket */
236*0Sstevel@tonic-gate #endif
237