1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
10 #include <ac/unistd.h>
15 #include "ldapconfig.h"
17 static char *binddn = LDAPMODRDN_BINDDN;
18 static char *passwd = LDAPMODRDN_BIND_CRED;
19 static char *base = LDAPMODRDN_BASE;
20 static char *ldaphost = LDAPHOST;
21 static int ldapport = LDAP_PORT;
22 static int not, verbose, contoper;
26 extern int ldap_debug, lber_debug;
27 #endif /* LDAP_DEBUG */
29 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
32 static int domodrdn LDAP_P((
36 int remove)); /* flag: remove old RDN */
42 char *usage = "usage: %s [-nvkc] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
43 char *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
45 int rc, i, kerberos, remove, havedn, authmethod;
51 kerberos = not = contoper = verbose = remove = 0;
53 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
55 while (( i = getopt( argc, argv, "kKcnvrh:p:D:w:d:f:" )) != EOF ) {
57 case 'k': /* kerberos bind */
60 case 'K': /* kerberos bind, part one only */
63 case 'c': /* continuous operation mode */
66 case 'h': /* ldap host */
67 ldaphost = strdup( optarg );
69 case 'D': /* bind DN */
70 binddn = strdup( optarg );
72 case 'w': /* password */
73 passwd = strdup( optarg );
77 ldap_debug = lber_debug = atoi( optarg ); /* */
78 #else /* LDAP_DEBUG */
79 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
80 #endif /* LDAP_DEBUG */
82 case 'f': /* read from file */
83 infile = strdup( optarg );
86 ldapport = atoi( optarg );
88 case 'n': /* print adds, don't actually do them */
91 case 'v': /* verbose mode */
94 case 'r': /* remove old RDN */
98 fprintf( stderr, usage, argv[0] );
104 if (argc - optind == 2) {
105 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
109 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
114 } else if ( argc - optind != 0 ) {
115 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
116 fprintf( stderr, usage, argv[0] );
120 if ( infile != NULL ) {
121 if (( fp = fopen( infile, "r" )) == NULL ) {
129 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
130 perror( "ldap_open" );
134 #if LDAP_VERSION > LDAP_VERSION2
135 /* this seems prudent */
136 ldap_set_option( LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
140 authmethod = LDAP_AUTH_SIMPLE;
141 } else if ( kerberos == 1 ) {
142 authmethod = LDAP_AUTH_KRBV41;
144 authmethod = LDAP_AUTH_KRBV4;
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 ) {
184 int remove) /* flag: remove old RDN */
189 printf( "modrdn %s:\n\t%s\n", dn, rdn );
191 printf("removing old RDN\n");
193 printf("keeping old RDN\n");
197 i = ldap_modrdn2_s( ld, dn, rdn, remove );
198 if ( i != LDAP_SUCCESS ) {
199 ldap_perror( ld, "ldap_modrdn2_s" );
200 } else if ( verbose ) {
201 printf( "modrdn complete\n" );