1 /* ldapmodrdn.c - generic program to modify an entry's RDN 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]...\n", 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\tread from file\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\tmake no modifications\n");
34 fprintf(stderr, " -p port\tldap port\n");
35 fprintf(stderr, " -r\t\tremove old RDN\n");
36 fprintf(stderr, " -v\t\tverbose\n");
37 fprintf(stderr, " -W\t\tprompt for bind password\n");
38 fprintf(stderr, " -w passwd\tbind password (for simple authentication)\n");
42 static int domodrdn LDAP_P((
46 int remove)); /* flag: remove old RDN */
49 main(int argc, char **argv)
53 char *myname, *infile, *entrydn, *rdn, buf[ 4096 ];
56 char *ldaphost = NULL;
57 int rc, i, remove, havedn, want_passwd;
58 int authmethod = LDAP_AUTH_SIMPLE;
59 int ldapport = LDAP_PORT;
61 infile = entrydn = rdn = NULL;
62 not = contoper = verbose = remove = want_passwd = 0;
63 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
65 while ((i = getopt(argc, argv, "cD:d:f:h:Kknp:rvWw:")) != EOF)
69 case 'c': /* continuous operation mode */
73 case 'D': /* bind DN */
74 binddn = strdup( optarg );
79 ldap_debug = lber_debug = atoi( optarg );
80 #else /* LDAP_DEBUG */
81 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
82 #endif /* LDAP_DEBUG */
85 case 'f': /* read from file */
86 infile = strdup( optarg );
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 adds, don't actually do them */
114 ldapport = atoi( optarg );
117 case 'r': /* remove old RDN */
121 case 'v': /* verbose mode */
129 case 'w': /* password */
130 passwd = strdup(optarg);
139 if (argc - optind == 2)
141 if (( rdn = strdup( argv[argc - 1] )) == NULL )
146 if (( entrydn = strdup( argv[argc - 2] )) == NULL )
152 } else if (argc - optind != 0) {
153 fprintf(stderr, "%s: invalid number of arguments, only two allowed\n", myname);
157 if (want_passwd && !passwd)
158 passwd = strdup(getpass("Enter LDAP password: "));
162 if ((fp = fopen( infile, "r" )) == NULL)
170 if ((ld = ldap_open(ldaphost, ldapport)) == NULL)
176 /* this seems prudent */
177 ldap_set_option(ld, LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
179 if (ldap_bind_s(ld, binddn, passwd, authmethod) != LDAP_SUCCESS)
181 ldap_perror(ld, "ldap_bind");
187 rc = domodrdn(ld, entrydn, rdn, remove);
188 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
189 if (*buf != '\0') { /* blank lines optional, skip */
190 buf[strlen(buf) - 1] = '\0'; /* remove nl */
194 /* have DN, get RDN */
195 if (( rdn = strdup( buf )) == NULL)
200 rc = domodrdn(ld, entrydn, rdn, remove);
202 } else if (!havedn) {
203 /* don't have DN yet */
204 if (( entrydn = strdup( buf )) == NULL )
223 int remove) /* flag: remove old RDN */
225 int rc = LDAP_SUCCESS;
229 printf("modrdn %s:\n\t%s\n", dn, rdn);
231 printf("removing old RDN\n");
233 printf("keeping old RDN\n");
238 rc = ldap_modrdn2_s(ld, dn, rdn, remove);
239 if (rc != LDAP_SUCCESS)
240 ldap_perror(ld, "ldap_modrdn2_s");
242 printf("modrdn complete\n");