]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
4d3bc54c399289a5ddef1bdc16a1d5678b420b42
[openldap] / clients / tools / ldapdelete.c
1 /* ldapdelete.c - simple program to delete an entry using LDAP */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <ctype.h>
8
9 #include <ac/string.h>
10 #include <ac/unistd.h>
11
12 #include <lber.h>
13 #include <ldap.h>
14
15 static int      not, verbose, contoper;
16
17 #define safe_realloc( ptr, size )       ( ptr == NULL ? malloc( size ) : \
18                                          realloc( ptr, size ))
19
20 static void
21 usage(char *s)
22 {
23     fprintf(stderr, "Usage: %s [options] [dn]...", s);
24     fprintf(stderr, "  -c\t\tcontinuous operation mode\n");
25     fprintf(stderr, "  -D bindnd\tbind dn\n");
26     fprintf(stderr, "  -d level\tdebugging level\n");
27     fprintf(stderr, "  -f file\t\t\n");
28     fprintf(stderr, "  -h host\tldap sever\n");
29 #ifdef HAVE_KERBEROS
30     fprintf(stderr, "  -K\t\tuse Kerberos step 1\n");
31     fprintf(stderr, "  -k\t\tuse Kerberos instead of Simple Password authentication\n");
32 #endif
33     fprintf(stderr, "  -n\t\t make no modifications\n");
34     fprintf(stderr, "  -p port\tldap port\n");
35     fprintf(stderr, "  -v\t\tverbose\n");
36     fprintf(stderr, "  -W\t\tprompt for bind password\n");
37     fprintf(stderr, "  -w passwd\tbind password (for simple authentication)\n");
38     exit(1);
39 }
40
41 static int dodelete LDAP_P((
42     LDAP        *ld,
43     char        *dn));
44
45 int
46 main( int argc, char **argv )
47 {
48     FILE        *fp = NULL;
49     LDAP        *ld = NULL;
50     char        buf[4096];
51     char        *binddn = NULL;
52     char        *passwd = NULL;
53     char        *ldaphost = NULL;
54     int         authmethod = LDAP_AUTH_SIMPLE;
55     int         deref = LDAP_DEREF_NEVER;
56     int         i, rc, want_passwd;
57     int         ldapport = LDAP_PORT;
58
59     rc = not = verbose = contoper = want_passwd = 0;
60
61     while ((i = getopt( argc, argv, "cD:d:f:h:Kknp:vWw:")) != EOF )
62     {
63         switch(i)
64         {
65         case 'c':       /* continuous operation mode */
66             contoper++;
67             break;
68
69         case 'D':       /* bind DN */
70             binddn = strdup(optarg);
71             break;
72
73         case 'd':
74 #ifdef LDAP_DEBUG
75             ldap_debug = lber_debug = atoi(optarg);
76 #else
77             fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
78 #endif
79             break;
80
81         case 'f':       /* read DNs from a file */
82             if ((fp = fopen(optarg, "r")) == NULL)
83             {
84                 perror(optarg);
85                 return(1);
86             }
87             break;
88
89         case 'h':       /* ldap host */
90             ldaphost = strdup(optarg);
91             break;
92
93         case 'K':       /* kerberos bind, part one only */
94 #ifdef HAVE_KERBEROS
95             authmethod = LDAP_AUTH_KRBV41;
96 #else
97             fprintf(stderr, "%s was not compiled with Kerberos support\n", argv[0]);
98 #endif
99             break;
100
101         case 'k':       /* kerberos bind */
102 #ifdef HAVE_KERBEROS
103             authmethod = LDAP_AUTH_KRBV4;
104 #else
105             fprintf(stderr, "%s was not compiled with Kerberos support\n", argv[0]);
106 #endif
107             break;
108
109         case 'n':       /* print deletes, don't actually do them */
110             not++;
111             break;
112
113         case 'p':
114             ldapport = atoi( optarg );
115             break;
116
117         case 'v':       /* verbose mode */
118             verbose++;
119             break;
120
121         case 'W':
122             want_passwd++;
123             break;
124
125         case 'w':       /* password */
126             passwd = strdup(optarg);
127             break;
128
129         default:
130             usage(argv[0]);
131         }
132     }
133
134     if (want_passwd && !passwd)
135         passwd = strdup(getpass("Enter LDAP Password: "));
136
137     if (fp == NULL && optind >= argc)
138         fp = stdin;
139
140     if ((ld = ldap_open(ldaphost, ldapport)) == NULL) {
141         perror("ldap_open");
142         return(1);
143     }
144
145     /* this seems prudent */
146     ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
147
148     if (ldap_bind_s(ld, binddn, passwd, authmethod) != LDAP_SUCCESS) {
149         ldap_perror(ld, "ldap_bind");
150         return(1);
151     }
152
153     if (fp == NULL) {
154         for (; optind < argc; ++optind)
155             rc = dodelete(ld, argv[optind]);
156     } else {
157         rc = 0;
158         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
159             buf[strlen(buf) - 1] = '\0';        /* remove trailing newline */
160             if ( *buf != '\0' )
161                 rc = dodelete( ld, buf );
162         }
163     }
164
165     ldap_unbind(ld);
166
167     return(rc);
168 }
169
170 static int
171 dodelete(
172     LDAP        *ld,
173     char        *dn)
174 {
175     int rc;
176
177     if (verbose)
178         printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
179
180     if (not)
181         rc = LDAP_SUCCESS;
182     else {
183         if ((rc = ldap_delete_s(ld, dn)) != LDAP_SUCCESS)
184             ldap_perror(ld, "ldap_delete");
185         else if (verbose)
186             printf("entry removed\n");
187     }
188
189     return(rc);
190 }