Back to index

lightning-sunbird  0.9+nobinonly
ldapdelete.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /* ldapdelete.c - simple program to delete an entry using LDAP */
00039 
00040 #include "ldaptool.h"
00041 
00042 static int    contoper;
00043 static LDAP   *ld;
00044 
00045 static int dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls );
00046 static void options_callback( int option, char *optarg );
00047 
00048 
00049 static void
00050 usage( void )
00051 {
00052     fprintf( stderr, "usage: %s [options] [dn...]\n", ldaptool_progname );
00053     fprintf( stderr, "options:\n" );
00054     ldaptool_common_usage( 0 );
00055     fprintf( stderr, "    -c\t\tcontinuous mode (do not stop on errors)\n" );
00056     fprintf( stderr, "    -f file\tread DNs to delete from file (default: standard input)\n" );
00057     exit( LDAP_PARAM_ERROR );
00058 }
00059 
00060 
00061 int
00062 main( int argc, char **argv )
00063 {
00064     char      buf[ 4096 ];
00065     int              rc, deref, optind;
00066     LDAPControl      *ldctrl;
00067 
00068 #ifdef notdef
00069 #ifdef HPUX11
00070 #ifndef __LP64__
00071        _main( argc, argv);
00072 #endif /* __LP64_ */
00073 #endif /* HPUX11 */
00074 #endif
00075 
00076     contoper = 0;
00077 
00078     optind = ldaptool_process_args( argc, argv, "c", 0, options_callback );
00079        
00080     if ( optind == -1 ) {
00081               usage();
00082     }
00083 
00084     if ( ldaptool_fp == NULL && optind >= argc ) {
00085        ldaptool_fp = stdin;
00086     }
00087 
00088     ld = ldaptool_ldap_init( 0 );
00089 
00090     deref = LDAP_DEREF_NEVER;      /* prudent, but probably unnecessary */
00091     ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
00092 
00093     ldaptool_bind( ld );
00094 
00095     if (( ldctrl = ldaptool_create_manage_dsait_control()) != NULL ) {
00096        ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
00097     } 
00098 
00099     if ((ldctrl = ldaptool_create_proxyauth_control(ld)) !=NULL) {
00100        ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
00101     }
00102 
00103     if ( ldaptool_fp == NULL ) {
00104        for ( ; optind < argc; ++optind ) {
00105             char *conv;
00106 
00107             conv = ldaptool_local2UTF8( argv[ optind ] );
00108            rc = dodelete( ld, conv, ldaptool_request_ctrls );
00109             if( conv != NULL )
00110                 free( conv );
00111        }
00112     } else {
00113        rc = 0;
00114        while ((rc == 0 || contoper) &&
00115               fgets(buf, sizeof(buf), ldaptool_fp) != NULL) {
00116            buf[ strlen( buf ) - 1 ] = '\0';      /* remove trailing newline */
00117            if ( *buf != '\0' ) {
00118               rc = dodelete( ld, buf, ldaptool_request_ctrls );
00119            }
00120        }
00121     }
00122 
00123     ldaptool_reset_control_array( ldaptool_request_ctrls );
00124     ldaptool_cleanup( ld );
00125     return( rc );
00126 }
00127 
00128 static void
00129 options_callback( int option, char *optarg )
00130 {
00131     switch( option ) {
00132     case 'c': /* continuous operation mode */
00133        ++contoper;
00134        break;
00135     default:
00136        usage();
00137     }
00138 }
00139 
00140 
00141 static int
00142 dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls )
00143 {
00144     int              rc;
00145 
00146     if ( ldaptool_verbose ) {
00147        printf( "%sdeleting entry %s\n", ldaptool_not ? "!" : "", dn );
00148     }
00149     if ( ldaptool_not ) {
00150        rc = LDAP_SUCCESS;
00151     } else if (( rc = ldaptool_delete_ext_s( ld, dn, serverctrls, NULL,
00152            "ldap_delete" )) == LDAP_SUCCESS && ldaptool_verbose ) {
00153        printf( "entry removed\n" );
00154     }
00155 
00156     return( rc );
00157 }