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