xref: /titanic_41/usr/src/cmd/ldap/common/ldapdelete.c (revision 95c2d3023b88b9097d9822eb47ace5466e6d1cf4)
1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
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/
11  *
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.
16  *
17  * The Original Code is Mozilla Communicator client code, released
18  * March 31, 1998.
19  *
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.
24  *
25  * Contributor(s):
26  */
27 
28 /* ldapdelete.c - simple program to delete an entry using LDAP */
29 
30 #include "ldaptool.h"
31 #ifdef SOLARIS_LDAP_CMD
32 #include <locale.h>
33 #endif	/* SOLARIS_LDAP_CMD */
34 
35 static int	contoper;
36 
37 #ifdef later
38 static int	delbypasscmd, yestodelete;
39 #endif
40 
41 #ifndef SOLARIS_LDAP_CMD
42 #define gettext(s) s
43 #endif
44 
45 
46 static LDAP	*ld;
47 
48 LDAPMessage *result, *e;
49 char *my_filter = "(objectclass=*)";
50 
51 static int dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls );
52 static void options_callback( int option, char *optarg );
53 
54 static void
55 usage( void )
56 {
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 );
64 }
65 
66 
67 int
68 main( int argc, char **argv )
69 {
70     char	buf[ 4096 ];
71     int		rc, deref, optind;
72     LDAPControl	*ldctrl;
73 
74 #ifdef notdef
75 #ifdef HPUX11
76 #ifndef __LP64__
77 	_main( argc, argv);
78 #endif /* __LP64_ */
79 #endif /* HPUX11 */
80 #endif
81 
82 #ifdef SOLARIS_LDAP_CMD
83     char *locale = setlocale(LC_ALL, "");
84     textdomain(TEXT_DOMAIN);
85 #endif	/* SOLARIS_LDAP_CMD */
86     contoper = 0;
87 
88 #ifdef later
89     delbypasscmd = 0;
90 #endif
91 
92     optind = ldaptool_process_args( argc, argv, "c", 0, options_callback );
93 
94     if ( optind == -1 ) {
95 	usage();
96     }
97 
98     if ( ldaptool_fp == NULL && optind >= argc ) {
99 	ldaptool_fp = stdin;
100     }
101 
102     ld = ldaptool_ldap_init( 0 );
103 
104     deref = LDAP_DEREF_NEVER;	/* prudent, but probably unnecessary */
105     ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
106 
107     ldaptool_bind( ld );
108 
109     if (( ldctrl = ldaptool_create_manage_dsait_control()) != NULL ) {
110 	ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
111     }
112 
113     if ((ldctrl = ldaptool_create_proxyauth_control(ld)) !=NULL) {
114 	ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
115     }
116 
117     if ( ldaptool_fp == NULL ) {
118 	for ( ; optind < argc; ++optind ) {
119             char *conv;
120 
121             conv = ldaptool_local2UTF8( argv[ optind ] );
122 	    rc = dodelete( ld, conv, ldaptool_request_ctrls );
123             if( conv != NULL ) {
124                 free( conv );
125             }
126 	}
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 );
134 	    }
135 	}
136     }
137 
138     ldaptool_reset_control_array( ldaptool_request_ctrls );
139     ldaptool_cleanup( ld );
140 
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 );
146 }
147 
148 static void
149 options_callback( int option, char *optarg )
150 {
151     switch( option ) {
152     case 'c':	/* continuous operation mode */
153 	++contoper;
154 	break;
155     default:
156 	usage();
157     }
158 }
159 
160 static int
161 dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls )
162 {
163     int         rc;
164 
165     if ( ldaptool_verbose ) {
166         printf( gettext("%sdeleting entry %s\n"), ldaptool_not ? "!" : "", dn );
167     }
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") );
173     }
174 
175     return( rc );
176 }
177 
178 #ifdef later
179 
180 /* This code broke iDS.....it will have to be revisited */
181 static int
182 dodelete( LDAP *ld, char *dn, LDAPControl **serverctrls )
183 {
184     int rc;
185     Head HeadNode;
186     Element *datalist;
187     char ch;
188 
189     if ( ldaptool_verbose ) {
190         printf( gettext("%sdeleting entry %s\n"), ldaptool_not ? "!" : "", dn );
191     }
192     if ( ldaptool_not ) {
193         rc = LDAP_SUCCESS;
194     }
195     else { /* else 1 */
196        L_Init(&HeadNode);
197 
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 );
200        }
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);
207              }
208           }
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;
216                 }
217              } else {
218                   yestodelete = 1;
219              }
220           } else {
221 	       yestodelete = 1;
222           }
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 );
228                    }
229                    L_Remove(datalist, (Head *)&HeadNode);
230                    ldap_memfree(datalist->data);
231                    free ( datalist );
232                 }
233              }
234            } /* end if (yestodelete) */
235       } /* else 2 */
236     } /* else 1 */
237     return (rc);
238 } /* function end bracket */
239 #endif
240