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 [-nvkc] [-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, kerberos, remove, havedn, authmethod;
41 kerberos = not = contoper = verbose = remove = 0;
43 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
45 while (( i = getopt( argc, argv, "kKcnvrh:p:D:w:d:f:" )) != EOF ) {
47 case 'k': /* kerberos bind */
50 case 'K': /* kerberos bind, part one only */
53 case 'c': /* continuous operation mode */
56 case 'h': /* ldap host */
57 ldaphost = strdup( optarg );
59 case 'D': /* bind DN */
60 binddn = strdup( optarg );
62 case 'w': /* password */
63 passwd = strdup( optarg );
67 ldap_debug = lber_debug = atoi( optarg ); /* */
68 #else /* LDAP_DEBUG */
69 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
70 #endif /* LDAP_DEBUG */
72 case 'f': /* read from file */
73 infile = strdup( optarg );
76 ldapport = atoi( optarg );
78 case 'n': /* print adds, don't actually do them */
81 case 'v': /* verbose mode */
84 case 'r': /* remove old RDN */
88 fprintf( stderr, usage, argv[0] );
94 if (argc - optind == 2) {
95 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
99 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
104 } else if ( argc - optind != 0 ) {
105 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
106 fprintf( stderr, usage, argv[0] );
110 if ( infile != NULL ) {
111 if (( fp = fopen( infile, "r" )) == NULL ) {
119 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
120 perror( "ldap_open" );
124 /* this seems prudent */
125 ldap_set_option( ld, LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
128 authmethod = LDAP_AUTH_SIMPLE;
129 } else if ( kerberos == 1 ) {
130 authmethod = LDAP_AUTH_KRBV41;
132 authmethod = LDAP_AUTH_KRBV4;
134 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
135 ldap_perror( ld, "ldap_bind" );
141 rc = domodrdn(ld, entrydn, rdn, remove);
142 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
143 if ( *buf != '\0' ) { /* blank lines optional, skip */
144 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
146 if ( havedn ) { /* have DN, get RDN */
147 if (( rdn = strdup( buf )) == NULL ) {
151 rc = domodrdn(ld, entrydn, rdn, remove);
153 } else if ( !havedn ) { /* don't have DN yet */
154 if (( entrydn = strdup( buf )) == NULL ) {
175 int remove) /* flag: remove old RDN */
180 printf( "modrdn %s:\n\t%s\n", dn, rdn );
182 printf("removing old RDN\n");
184 printf("keeping old RDN\n");
188 i = ldap_modrdn2_s( ld, dn, rdn, remove );
189 if ( i != LDAP_SUCCESS ) {
190 ldap_perror( ld, "ldap_modrdn2_s" );
191 } else if ( verbose ) {
192 printf( "modrdn complete\n" );