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