17c478bd9Sstevel@tonic-gate /*
2ffc33b84SSreedhar Chalamalasetti - Sun Microsystems - Bangalore India  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
6*55fea89dSDan Cross /*
77c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
87c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
97c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
107c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
11*55fea89dSDan Cross  *
127c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
137c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
147c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
157c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
16*55fea89dSDan Cross  *
177c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
187c478bd9Sstevel@tonic-gate  * March 31, 1998.
19*55fea89dSDan Cross  *
207c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
217c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
227c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
237c478bd9Sstevel@tonic-gate  * Rights Reserved.
24*55fea89dSDan Cross  *
25*55fea89dSDan Cross  * Contributor(s):
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /* ldapdelete.c - simple program to delete an entry using LDAP */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include "ldaptool.h"
317c478bd9Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
327c478bd9Sstevel@tonic-gate #include <locale.h>
337c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate static int	contoper;
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifdef later
387c478bd9Sstevel@tonic-gate static int	delbypasscmd, yestodelete;
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
427c478bd9Sstevel@tonic-gate #define gettext(s) s
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static LDAP	*ld;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate LDAPMessage *result, *e;
497c478bd9Sstevel@tonic-gate char *my_filter = "(objectclass=*)";
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static int dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls );
527c478bd9Sstevel@tonic-gate static void options_callback( int option, char *optarg );
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static void
usage(void)557c478bd9Sstevel@tonic-gate usage( void )
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("usage: %s [options] [dn...]\n"), ldaptool_progname );
587c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("options:\n") );
597c478bd9Sstevel@tonic-gate     ldaptool_common_usage( 0 );
607c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -a\t\tBy-pass confirmation question when deleting a branch\n") );
617c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext( "    -c\t\tcontinuous mode (do not stop on errors)\n") );
627c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext( "    -f file\tread DNs to delete from file (default: standard input)\n") );
637c478bd9Sstevel@tonic-gate     exit( LDAP_PARAM_ERROR );
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)687c478bd9Sstevel@tonic-gate main( int argc, char **argv )
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate     char	buf[ 4096 ];
717c478bd9Sstevel@tonic-gate     int		rc, deref, optind;
727c478bd9Sstevel@tonic-gate     LDAPControl	*ldctrl;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #ifdef notdef
757c478bd9Sstevel@tonic-gate #ifdef HPUX11
767c478bd9Sstevel@tonic-gate #ifndef __LP64__
777c478bd9Sstevel@tonic-gate 	_main( argc, argv);
787c478bd9Sstevel@tonic-gate #endif /* __LP64_ */
797c478bd9Sstevel@tonic-gate #endif /* HPUX11 */
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
837c478bd9Sstevel@tonic-gate     char *locale = setlocale(LC_ALL, "");
847c478bd9Sstevel@tonic-gate     textdomain(TEXT_DOMAIN);
857c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
867c478bd9Sstevel@tonic-gate     contoper = 0;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #ifdef later
897c478bd9Sstevel@tonic-gate     delbypasscmd = 0;
907c478bd9Sstevel@tonic-gate #endif
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate     optind = ldaptool_process_args( argc, argv, "c", 0, options_callback );
93*55fea89dSDan Cross 
947c478bd9Sstevel@tonic-gate     if ( optind == -1 ) {
957c478bd9Sstevel@tonic-gate 	usage();
967c478bd9Sstevel@tonic-gate     }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate     if ( ldaptool_fp == NULL && optind >= argc ) {
997c478bd9Sstevel@tonic-gate 	ldaptool_fp = stdin;
1007c478bd9Sstevel@tonic-gate     }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate     ld = ldaptool_ldap_init( 0 );
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate     deref = LDAP_DEREF_NEVER;	/* prudent, but probably unnecessary */
1057c478bd9Sstevel@tonic-gate     ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate     ldaptool_bind( ld );
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate     if (( ldctrl = ldaptool_create_manage_dsait_control()) != NULL ) {
1107c478bd9Sstevel@tonic-gate 	ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
111*55fea89dSDan Cross     }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate     if ((ldctrl = ldaptool_create_proxyauth_control(ld)) !=NULL) {
1147c478bd9Sstevel@tonic-gate 	ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
1157c478bd9Sstevel@tonic-gate     }
116*55fea89dSDan Cross 
1177c478bd9Sstevel@tonic-gate     if ( ldaptool_fp == NULL ) {
1187c478bd9Sstevel@tonic-gate 	for ( ; optind < argc; ++optind ) {
1197c478bd9Sstevel@tonic-gate             char *conv;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate             conv = ldaptool_local2UTF8( argv[ optind ] );
1227c478bd9Sstevel@tonic-gate 	    rc = dodelete( ld, conv, ldaptool_request_ctrls );
1237c478bd9Sstevel@tonic-gate             if( conv != NULL ) {
1247c478bd9Sstevel@tonic-gate                 free( conv );
1257c478bd9Sstevel@tonic-gate             }
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate     } else {
1287c478bd9Sstevel@tonic-gate 	rc = 0;
1297c478bd9Sstevel@tonic-gate 	while ((rc == 0 || contoper) &&
1307c478bd9Sstevel@tonic-gate 		fgets(buf, sizeof(buf), ldaptool_fp) != NULL) {
1317c478bd9Sstevel@tonic-gate 	    buf[ strlen( buf ) - 1 ] = '\0';	/* remove trailing newline */
1327c478bd9Sstevel@tonic-gate 	    if ( *buf != '\0' ) {
1337c478bd9Sstevel@tonic-gate 	          rc = dodelete( ld, buf, ldaptool_request_ctrls );
1347c478bd9Sstevel@tonic-gate 	    }
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate     }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate     ldaptool_reset_control_array( ldaptool_request_ctrls );
1397c478bd9Sstevel@tonic-gate     ldaptool_cleanup( ld );
140ffc33b84SSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 
141ffc33b84SSreedhar Chalamalasetti - Sun Microsystems - Bangalore India     /* check for and report output error */
142ffc33b84SSreedhar Chalamalasetti - Sun Microsystems - Bangalore India     fflush( stdout );
143ffc33b84SSreedhar Chalamalasetti - Sun Microsystems - Bangalore India     rc = ldaptool_check_ferror( stdout, rc,
144ffc33b84SSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		gettext("output error (output might be incomplete)") );
1457c478bd9Sstevel@tonic-gate     return( rc );
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static void
options_callback(int option,char * optarg)1497c478bd9Sstevel@tonic-gate options_callback( int option, char *optarg )
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate     switch( option ) {
1527c478bd9Sstevel@tonic-gate     case 'c':	/* continuous operation mode */
1537c478bd9Sstevel@tonic-gate 	++contoper;
1547c478bd9Sstevel@tonic-gate 	break;
1557c478bd9Sstevel@tonic-gate     default:
1567c478bd9Sstevel@tonic-gate 	usage();
1577c478bd9Sstevel@tonic-gate     }
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static int
dodelete(LDAP * ld,char * dn,LDAPControl ** serverctrls)1617c478bd9Sstevel@tonic-gate dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls )
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate     int         rc;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate     if ( ldaptool_verbose ) {
1667c478bd9Sstevel@tonic-gate         printf( gettext("%sdeleting entry %s\n"), ldaptool_not ? "!" : "", dn );
1677c478bd9Sstevel@tonic-gate     }
1687c478bd9Sstevel@tonic-gate     if ( ldaptool_not ) {
1697c478bd9Sstevel@tonic-gate         rc = LDAP_SUCCESS;
1707c478bd9Sstevel@tonic-gate     } else if (( rc = ldaptool_delete_ext_s( ld, dn, serverctrls, NULL,
1717c478bd9Sstevel@tonic-gate             "ldap_delete" )) == LDAP_SUCCESS && ldaptool_verbose ) {
1727c478bd9Sstevel@tonic-gate         printf( gettext("entry removed\n") );
1737c478bd9Sstevel@tonic-gate     }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate     return( rc );
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate #ifdef later
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /* This code broke iDS.....it will have to be revisited */
1817c478bd9Sstevel@tonic-gate static int
dodelete(LDAP * ld,char * dn,LDAPControl ** serverctrls)1827c478bd9Sstevel@tonic-gate dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls )
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate     int rc;
185*55fea89dSDan Cross     Head HeadNode;
1867c478bd9Sstevel@tonic-gate     Element *datalist;
1877c478bd9Sstevel@tonic-gate     char ch;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate     if ( ldaptool_verbose ) {
1907c478bd9Sstevel@tonic-gate         printf( gettext("%sdeleting entry %s\n"), ldaptool_not ? "!" : "", dn );
1917c478bd9Sstevel@tonic-gate     }
1927c478bd9Sstevel@tonic-gate     if ( ldaptool_not ) {
1937c478bd9Sstevel@tonic-gate         rc = LDAP_SUCCESS;
194*55fea89dSDan Cross     }
1957c478bd9Sstevel@tonic-gate     else { /* else 1 */
1967c478bd9Sstevel@tonic-gate        L_Init(&HeadNode);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate        if ( (rc = ldap_search_s( ld, dn, LDAP_SCOPE_SUBTREE, my_filter, NULL, 0, &result )) != LDAP_SUCCESS ) {
1997c478bd9Sstevel@tonic-gate           ldaptool_print_lderror( ld, "ldap_search", LDAPTOOL_CHECK4SSL_IF_APPROP );
2007c478bd9Sstevel@tonic-gate        }
2017c478bd9Sstevel@tonic-gate        else { /* else 2 */
2027c478bd9Sstevel@tonic-gate           for ( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) {
2037c478bd9Sstevel@tonic-gate              if ( ( dn = ldap_get_dn( ld, e ) ) != NULL ) {
2047c478bd9Sstevel@tonic-gate                 datalist = (Element *)malloc(sizeof(Element));
2057c478bd9Sstevel@tonic-gate                 datalist->data = dn;
2067c478bd9Sstevel@tonic-gate 	        L_Insert(datalist, &HeadNode);
2077c478bd9Sstevel@tonic-gate              }
2087c478bd9Sstevel@tonic-gate           }
2097c478bd9Sstevel@tonic-gate           if ( ((Head *)&HeadNode)->count > 1 ) {
2107c478bd9Sstevel@tonic-gate              yestodelete = 0;
2117c478bd9Sstevel@tonic-gate              if ( delbypasscmd == 0 ) {
2127c478bd9Sstevel@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);
2137c478bd9Sstevel@tonic-gate                 ch = getchar();
2147c478bd9Sstevel@tonic-gate                 if ( (ch == 'Y') || (ch == 'y')) {
2157c478bd9Sstevel@tonic-gate                    yestodelete = 1;
2167c478bd9Sstevel@tonic-gate                 }
2177c478bd9Sstevel@tonic-gate              } else {
2187c478bd9Sstevel@tonic-gate                   yestodelete = 1;
2197c478bd9Sstevel@tonic-gate              }
2207c478bd9Sstevel@tonic-gate           } else {
2217c478bd9Sstevel@tonic-gate 	       yestodelete = 1;
2227c478bd9Sstevel@tonic-gate           }
2237c478bd9Sstevel@tonic-gate           if ( yestodelete == 1 ) {
2247c478bd9Sstevel@tonic-gate              for ( datalist = ((Head *)&HeadNode)->last; datalist; datalist = datalist->left ) {
2257c478bd9Sstevel@tonic-gate                 if (datalist)  {
2267c478bd9Sstevel@tonic-gate                    if ((rc = ldaptool_delete_ext_s( ld, (char *)datalist->data, serverctrls, NULL, "ldap_delete" )) == LDAP_SUCCESS && ldaptool_verbose ) {
2277c478bd9Sstevel@tonic-gate                       printf( gettext("%s entry removed\n"), (char *)datalist->data );
2287c478bd9Sstevel@tonic-gate                    }
2297c478bd9Sstevel@tonic-gate                    L_Remove(datalist, (Head *)&HeadNode);
2307c478bd9Sstevel@tonic-gate                    ldap_memfree(datalist->data);
2317c478bd9Sstevel@tonic-gate                    free ( datalist );
2327c478bd9Sstevel@tonic-gate                 }
2337c478bd9Sstevel@tonic-gate              }
2347c478bd9Sstevel@tonic-gate            } /* end if (yestodelete) */
2357c478bd9Sstevel@tonic-gate       } /* else 2 */
2367c478bd9Sstevel@tonic-gate     } /* else 1 */
2377c478bd9Sstevel@tonic-gate     return (rc);
2387c478bd9Sstevel@tonic-gate } /* function end bracket */
2397c478bd9Sstevel@tonic-gate #endif
240