10Sstevel@tonic-gate /*
2*8097SSreedhar.Chalamalasetti@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public
80Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file
90Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of
100Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS
130Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
140Sstevel@tonic-gate * implied. See the License for the specific language governing
150Sstevel@tonic-gate * rights and limitations under the License.
160Sstevel@tonic-gate *
170Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released
180Sstevel@tonic-gate * March 31, 1998.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape
210Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are
220Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All
230Sstevel@tonic-gate * Rights Reserved.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * Contributor(s):
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate /* ldapdelete.c - simple program to delete an entry using LDAP */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include "ldaptool.h"
310Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
320Sstevel@tonic-gate #include <locale.h>
330Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
340Sstevel@tonic-gate
350Sstevel@tonic-gate static int contoper;
360Sstevel@tonic-gate
370Sstevel@tonic-gate #ifdef later
380Sstevel@tonic-gate static int delbypasscmd, yestodelete;
390Sstevel@tonic-gate #endif
400Sstevel@tonic-gate
410Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
420Sstevel@tonic-gate #define gettext(s) s
430Sstevel@tonic-gate #endif
440Sstevel@tonic-gate
450Sstevel@tonic-gate
460Sstevel@tonic-gate static LDAP *ld;
470Sstevel@tonic-gate
480Sstevel@tonic-gate LDAPMessage *result, *e;
490Sstevel@tonic-gate char *my_filter = "(objectclass=*)";
500Sstevel@tonic-gate
510Sstevel@tonic-gate static int dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls );
520Sstevel@tonic-gate static void options_callback( int option, char *optarg );
530Sstevel@tonic-gate
540Sstevel@tonic-gate static void
usage(void)550Sstevel@tonic-gate usage( void )
560Sstevel@tonic-gate {
570Sstevel@tonic-gate fprintf( stderr, gettext("usage: %s [options] [dn...]\n"), ldaptool_progname );
580Sstevel@tonic-gate fprintf( stderr, gettext("options:\n") );
590Sstevel@tonic-gate ldaptool_common_usage( 0 );
600Sstevel@tonic-gate fprintf( stderr, gettext(" -a\t\tBy-pass confirmation question when deleting a branch\n") );
610Sstevel@tonic-gate fprintf( stderr, gettext( " -c\t\tcontinuous mode (do not stop on errors)\n") );
620Sstevel@tonic-gate fprintf( stderr, gettext( " -f file\tread DNs to delete from file (default: standard input)\n") );
630Sstevel@tonic-gate exit( LDAP_PARAM_ERROR );
640Sstevel@tonic-gate }
650Sstevel@tonic-gate
660Sstevel@tonic-gate
670Sstevel@tonic-gate int
main(int argc,char ** argv)680Sstevel@tonic-gate main( int argc, char **argv )
690Sstevel@tonic-gate {
700Sstevel@tonic-gate char buf[ 4096 ];
710Sstevel@tonic-gate int rc, deref, optind;
720Sstevel@tonic-gate LDAPControl *ldctrl;
730Sstevel@tonic-gate
740Sstevel@tonic-gate #ifdef notdef
750Sstevel@tonic-gate #ifdef HPUX11
760Sstevel@tonic-gate #ifndef __LP64__
770Sstevel@tonic-gate _main( argc, argv);
780Sstevel@tonic-gate #endif /* __LP64_ */
790Sstevel@tonic-gate #endif /* HPUX11 */
800Sstevel@tonic-gate #endif
810Sstevel@tonic-gate
820Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
830Sstevel@tonic-gate char *locale = setlocale(LC_ALL, "");
840Sstevel@tonic-gate textdomain(TEXT_DOMAIN);
850Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
860Sstevel@tonic-gate contoper = 0;
870Sstevel@tonic-gate
880Sstevel@tonic-gate #ifdef later
890Sstevel@tonic-gate delbypasscmd = 0;
900Sstevel@tonic-gate #endif
910Sstevel@tonic-gate
920Sstevel@tonic-gate optind = ldaptool_process_args( argc, argv, "c", 0, options_callback );
930Sstevel@tonic-gate
940Sstevel@tonic-gate if ( optind == -1 ) {
950Sstevel@tonic-gate usage();
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
980Sstevel@tonic-gate if ( ldaptool_fp == NULL && optind >= argc ) {
990Sstevel@tonic-gate ldaptool_fp = stdin;
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate ld = ldaptool_ldap_init( 0 );
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate deref = LDAP_DEREF_NEVER; /* prudent, but probably unnecessary */
1050Sstevel@tonic-gate ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate ldaptool_bind( ld );
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate if (( ldctrl = ldaptool_create_manage_dsait_control()) != NULL ) {
1100Sstevel@tonic-gate ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate if ((ldctrl = ldaptool_create_proxyauth_control(ld)) !=NULL) {
1140Sstevel@tonic-gate ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate if ( ldaptool_fp == NULL ) {
1180Sstevel@tonic-gate for ( ; optind < argc; ++optind ) {
1190Sstevel@tonic-gate char *conv;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate conv = ldaptool_local2UTF8( argv[ optind ] );
1220Sstevel@tonic-gate rc = dodelete( ld, conv, ldaptool_request_ctrls );
1230Sstevel@tonic-gate if( conv != NULL ) {
1240Sstevel@tonic-gate free( conv );
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate } else {
1280Sstevel@tonic-gate rc = 0;
1290Sstevel@tonic-gate while ((rc == 0 || contoper) &&
1300Sstevel@tonic-gate fgets(buf, sizeof(buf), ldaptool_fp) != NULL) {
1310Sstevel@tonic-gate buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
1320Sstevel@tonic-gate if ( *buf != '\0' ) {
1330Sstevel@tonic-gate rc = dodelete( ld, buf, ldaptool_request_ctrls );
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate ldaptool_reset_control_array( ldaptool_request_ctrls );
1390Sstevel@tonic-gate ldaptool_cleanup( ld );
140*8097SSreedhar.Chalamalasetti@Sun.COM
141*8097SSreedhar.Chalamalasetti@Sun.COM /* check for and report output error */
142*8097SSreedhar.Chalamalasetti@Sun.COM fflush( stdout );
143*8097SSreedhar.Chalamalasetti@Sun.COM rc = ldaptool_check_ferror( stdout, rc,
144*8097SSreedhar.Chalamalasetti@Sun.COM gettext("output error (output might be incomplete)") );
1450Sstevel@tonic-gate return( rc );
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate static void
options_callback(int option,char * optarg)1490Sstevel@tonic-gate options_callback( int option, char *optarg )
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate switch( option ) {
1520Sstevel@tonic-gate case 'c': /* continuous operation mode */
1530Sstevel@tonic-gate ++contoper;
1540Sstevel@tonic-gate break;
1550Sstevel@tonic-gate default:
1560Sstevel@tonic-gate usage();
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate static int
dodelete(LDAP * ld,char * dn,LDAPControl ** serverctrls)1610Sstevel@tonic-gate dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls )
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate int rc;
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate if ( ldaptool_verbose ) {
1660Sstevel@tonic-gate printf( gettext("%sdeleting entry %s\n"), ldaptool_not ? "!" : "", dn );
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate if ( ldaptool_not ) {
1690Sstevel@tonic-gate rc = LDAP_SUCCESS;
1700Sstevel@tonic-gate } else if (( rc = ldaptool_delete_ext_s( ld, dn, serverctrls, NULL,
1710Sstevel@tonic-gate "ldap_delete" )) == LDAP_SUCCESS && ldaptool_verbose ) {
1720Sstevel@tonic-gate printf( gettext("entry removed\n") );
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate return( rc );
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate #ifdef later
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate /* This code broke iDS.....it will have to be revisited */
1810Sstevel@tonic-gate static int
dodelete(LDAP * ld,char * dn,LDAPControl ** serverctrls)1820Sstevel@tonic-gate dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls )
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate int rc;
1850Sstevel@tonic-gate Head HeadNode;
1860Sstevel@tonic-gate Element *datalist;
1870Sstevel@tonic-gate char ch;
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate if ( ldaptool_verbose ) {
1900Sstevel@tonic-gate printf( gettext("%sdeleting entry %s\n"), ldaptool_not ? "!" : "", dn );
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate if ( ldaptool_not ) {
1930Sstevel@tonic-gate rc = LDAP_SUCCESS;
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate else { /* else 1 */
1960Sstevel@tonic-gate L_Init(&HeadNode);
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate if ( (rc = ldap_search_s( ld, dn, LDAP_SCOPE_SUBTREE, my_filter, NULL, 0, &result )) != LDAP_SUCCESS ) {
1990Sstevel@tonic-gate ldaptool_print_lderror( ld, "ldap_search", LDAPTOOL_CHECK4SSL_IF_APPROP );
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate else { /* else 2 */
2020Sstevel@tonic-gate for ( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) {
2030Sstevel@tonic-gate if ( ( dn = ldap_get_dn( ld, e ) ) != NULL ) {
2040Sstevel@tonic-gate datalist = (Element *)malloc(sizeof(Element));
2050Sstevel@tonic-gate datalist->data = dn;
2060Sstevel@tonic-gate L_Insert(datalist, &HeadNode);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate if ( ((Head *)&HeadNode)->count > 1 ) {
2100Sstevel@tonic-gate yestodelete = 0;
2110Sstevel@tonic-gate if ( delbypasscmd == 0 ) {
2120Sstevel@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);
2130Sstevel@tonic-gate ch = getchar();
2140Sstevel@tonic-gate if ( (ch == 'Y') || (ch == 'y')) {
2150Sstevel@tonic-gate yestodelete = 1;
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate } else {
2180Sstevel@tonic-gate yestodelete = 1;
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate } else {
2210Sstevel@tonic-gate yestodelete = 1;
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate if ( yestodelete == 1 ) {
2240Sstevel@tonic-gate for ( datalist = ((Head *)&HeadNode)->last; datalist; datalist = datalist->left ) {
2250Sstevel@tonic-gate if (datalist) {
2260Sstevel@tonic-gate if ((rc = ldaptool_delete_ext_s( ld, (char *)datalist->data, serverctrls, NULL, "ldap_delete" )) == LDAP_SUCCESS && ldaptool_verbose ) {
2270Sstevel@tonic-gate printf( gettext("%s entry removed\n"), (char *)datalist->data );
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate L_Remove(datalist, (Head *)&HeadNode);
2300Sstevel@tonic-gate ldap_memfree(datalist->data);
2310Sstevel@tonic-gate free ( datalist );
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate } /* end if (yestodelete) */
2350Sstevel@tonic-gate } /* else 2 */
2360Sstevel@tonic-gate } /* else 1 */
2370Sstevel@tonic-gate return (rc);
2380Sstevel@tonic-gate } /* function end bracket */
2390Sstevel@tonic-gate #endif
240