1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
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 ) : \
37 char *usage = "usage: %s [-nvkc] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
38 char *myname,*infile, *p, *entrydn, *rdn, buf[ 4096 ];
40 int rc, i, kerberos, remove, havedn, authmethod;
47 kerberos = not = contoper = verbose = remove = 0;
49 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
51 while (( i = getopt( argc, argv, "kKcnvrh:p:D:w:d:f:" )) != EOF ) {
53 case 'k': /* kerberos bind */
56 case 'K': /* kerberos bind, part one only */
59 case 'c': /* continuous operation mode */
62 case 'h': /* ldap host */
63 ldaphost = strdup( optarg );
65 case 'D': /* bind DN */
66 binddn = strdup( optarg );
68 case 'w': /* password */
69 passwd = strdup( optarg );
73 ldap_debug = lber_debug = atoi( optarg ); /* */
74 #else /* LDAP_DEBUG */
75 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
76 #endif /* LDAP_DEBUG */
78 case 'f': /* read from file */
79 infile = strdup( optarg );
82 ldapport = atoi( optarg );
84 case 'n': /* print adds, don't actually do them */
87 case 'v': /* verbose mode */
90 case 'r': /* remove old RDN */
94 fprintf( stderr, usage, argv[0] );
100 if (argc - optind == 2) {
101 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
105 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
110 } else if ( argc - optind != 0 ) {
111 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
112 fprintf( stderr, usage, argv[0] );
116 if ( infile != NULL ) {
117 if (( fp = fopen( infile, "r" )) == NULL ) {
125 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
126 perror( "ldap_open" );
130 ld->ld_deref = LDAP_DEREF_NEVER; /* this seems prudent */
133 authmethod = LDAP_AUTH_SIMPLE;
134 } else if ( kerberos == 1 ) {
135 authmethod = LDAP_AUTH_KRBV41;
137 authmethod = LDAP_AUTH_KRBV4;
139 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
140 ldap_perror( ld, "ldap_bind" );
146 rc = domodrdn(ld, entrydn, rdn, remove);
147 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
148 if ( *buf != '\0' ) { /* blank lines optional, skip */
149 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
151 if ( havedn ) { /* have DN, get RDN */
152 if (( rdn = strdup( buf )) == NULL ) {
156 rc = domodrdn(ld, entrydn, rdn, remove);
158 } else if ( !havedn ) { /* don't have DN yet */
159 if (( entrydn = strdup( buf )) == NULL ) {
173 domodrdn( ld, dn, rdn, remove )
177 int remove; /* flag: remove old RDN */
182 printf( "modrdn %s:\n\t%s\n", dn, rdn );
184 printf("removing old RDN\n");
186 printf("keeping old RDN\n");
190 i = ldap_modrdn2_s( ld, dn, rdn, remove );
191 if ( i != LDAP_SUCCESS ) {
192 ldap_perror( ld, "ldap_modrdn2_s" );
193 } else if ( verbose ) {
194 printf( "modrdn complete\n" );