1 /* ldapdelete.c - simple program to delete an entry using LDAP */
10 #include <ac/unistd.h>
15 static int not, verbose, contoper;
17 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
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");
30 fprintf(stderr, " -K\t\tuse Kerberos step 1\n");
31 fprintf(stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n");
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");
41 static int dodelete LDAP_P((
46 main( int argc, char **argv )
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;
59 rc = not = verbose = contoper = want_passwd = 0;
61 while ((i = getopt( argc, argv, "cD:d:f:h:Kknp:vWw:")) != EOF )
65 case 'c': /* continuous operation mode */
69 case 'D': /* bind DN */
70 binddn = strdup(optarg);
75 ldap_debug = lber_debug = atoi(optarg);
77 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
81 case 'f': /* read DNs from a file */
82 if ((fp = fopen(optarg, "r")) == NULL)
89 case 'h': /* ldap host */
90 ldaphost = strdup(optarg);
93 case 'K': /* kerberos bind, part one only */
95 authmethod = LDAP_AUTH_KRBV41;
97 fprintf(stderr, "%s was not compiled with Kerberos support\n", argv[0]);
101 case 'k': /* kerberos bind */
103 authmethod = LDAP_AUTH_KRBV4;
105 fprintf(stderr, "%s was not compiled with Kerberos support\n", argv[0]);
109 case 'n': /* print deletes, don't actually do them */
114 ldapport = atoi( optarg );
117 case 'v': /* verbose mode */
125 case 'w': /* password */
126 passwd = strdup(optarg);
134 if (want_passwd && !passwd)
135 passwd = strdup(getpass("Enter LDAP Password: "));
137 if (fp == NULL && optind >= argc)
140 if ((ld = ldap_open(ldaphost, ldapport)) == NULL) {
145 /* this seems prudent */
146 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
148 if (ldap_bind_s(ld, binddn, passwd, authmethod) != LDAP_SUCCESS) {
149 ldap_perror(ld, "ldap_bind");
154 for (; optind < argc; ++optind)
155 rc = dodelete(ld, argv[optind]);
158 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
159 buf[strlen(buf) - 1] = '\0'; /* remove trailing newline */
161 rc = dodelete( ld, buf );
178 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
183 if ((rc = ldap_delete_s(ld, dn)) != LDAP_SUCCESS)
184 ldap_perror(ld, "ldap_delete");
186 printf("entry removed\n");