1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
10 #include <ac/unistd.h>
15 static char *binddn = NULL;
16 static char *passwd = NULL;
17 static char *base = NULL;
18 static char *ldaphost = NULL;
19 static int ldapport = 0;
20 static int not, verbose, contoper;
23 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
26 static int domodrdn LDAP_P((
30 int remove)); /* flag: remove old RDN */
33 main(int argc, char **argv)
35 char *usage = "usage: %s [-nvkWc] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
36 char *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
38 int rc, i, remove, havedn, authmethod, want_bindpw, debug;
41 not = contoper = verbose = remove = want_bindpw = debug = 0;
42 authmethod = LDAP_AUTH_SIMPLE;
44 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
46 while (( i = getopt( argc, argv, "WkKcnvrh:p:D:w:d:f:" )) != EOF ) {
48 case 'k': /* kerberos bind */
50 authmethod = LDAP_AUTH_KRBV4;
52 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
55 case 'K': /* kerberos bind, part one only */
57 authmethod = LDAP_AUTH_KRBV41;
59 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
62 case 'c': /* continuous operation mode */
65 case 'h': /* ldap host */
66 ldaphost = strdup( optarg );
68 case 'D': /* bind DN */
69 binddn = strdup( optarg );
71 case 'w': /* password */
72 passwd = strdup( optarg );
75 debug |= atoi( optarg );
77 case 'f': /* read from file */
78 infile = strdup( optarg );
81 ldapport = atoi( optarg );
83 case 'n': /* print adds, don't actually do them */
86 case 'v': /* verbose mode */
89 case 'r': /* remove old RDN */
96 fprintf( stderr, usage, argv[0] );
102 if (argc - optind == 2) {
103 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
107 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
112 } else if ( argc - optind != 0 ) {
113 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
114 fprintf( stderr, usage, argv[0] );
118 if ( infile != NULL ) {
119 if (( fp = fopen( infile, "r" )) == NULL ) {
128 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
129 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
132 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
133 perror( "ldap_open" );
137 /* this seems prudent */
139 int deref = LDAP_DEREF_NEVER;
140 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
144 passwd = getpass("Enter LDAP Password: ");
146 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
147 ldap_perror( ld, "ldap_bind" );
153 rc = domodrdn(ld, entrydn, rdn, remove);
154 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
155 if ( *buf != '\0' ) { /* blank lines optional, skip */
156 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
158 if ( havedn ) { /* have DN, get RDN */
159 if (( rdn = strdup( buf )) == NULL ) {
163 rc = domodrdn(ld, entrydn, rdn, remove);
165 } else if ( !havedn ) { /* don't have DN yet */
166 if (( entrydn = strdup( buf )) == NULL ) {
187 int remove) /* flag: remove old RDN */
192 printf( "modrdn %s:\n\t%s\n", dn, rdn );
194 printf("removing old RDN\n");
196 printf("keeping old RDN\n");
200 i = ldap_modrdn2_s( ld, dn, rdn, remove );
201 if ( i != LDAP_SUCCESS ) {
202 ldap_perror( ld, "ldap_modrdn2_s" );
203 } else if ( verbose ) {
204 printf( "modrdn complete\n" );