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