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 */
43 char *usage = "usage: %s [-nvkc] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
44 char *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
46 int rc, i, kerberos, remove, havedn, authmethod;
52 kerberos = not = contoper = verbose = remove = 0;
54 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
56 while (( i = getopt( argc, argv, "kKcnvrh:p:D:w:d:f:" )) != EOF ) {
58 case 'k': /* kerberos bind */
61 case 'K': /* kerberos bind, part one only */
64 case 'c': /* continuous operation mode */
67 case 'h': /* ldap host */
68 ldaphost = strdup( optarg );
70 case 'D': /* bind DN */
71 binddn = strdup( optarg );
73 case 'w': /* password */
74 passwd = strdup( optarg );
78 ldap_debug = lber_debug = atoi( optarg ); /* */
79 #else /* LDAP_DEBUG */
80 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
81 #endif /* LDAP_DEBUG */
83 case 'f': /* read from file */
84 infile = strdup( optarg );
87 ldapport = atoi( optarg );
89 case 'n': /* print adds, don't actually do them */
92 case 'v': /* verbose mode */
95 case 'r': /* remove old RDN */
99 fprintf( stderr, usage, argv[0] );
105 if (argc - optind == 2) {
106 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
110 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
115 } else if ( argc - optind != 0 ) {
116 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
117 fprintf( stderr, usage, argv[0] );
121 if ( infile != NULL ) {
122 if (( fp = fopen( infile, "r" )) == NULL ) {
130 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
131 perror( "ldap_open" );
135 /* this seems prudent */
136 ldap_set_option( ld, LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
139 authmethod = LDAP_AUTH_SIMPLE;
140 } else if ( kerberos == 1 ) {
141 authmethod = LDAP_AUTH_KRBV41;
143 authmethod = LDAP_AUTH_KRBV4;
145 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
146 ldap_perror( ld, "ldap_bind" );
152 rc = domodrdn(ld, entrydn, rdn, remove);
153 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
154 if ( *buf != '\0' ) { /* blank lines optional, skip */
155 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
157 if ( havedn ) { /* have DN, get RDN */
158 if (( rdn = strdup( buf )) == NULL ) {
162 rc = domodrdn(ld, entrydn, rdn, remove);
164 } else if ( !havedn ) { /* don't have DN yet */
165 if (( entrydn = strdup( buf )) == NULL ) {
183 int remove) /* flag: remove old RDN */
188 printf( "modrdn %s:\n\t%s\n", dn, rdn );
190 printf("removing old RDN\n");
192 printf("keeping old RDN\n");
196 i = ldap_modrdn2_s( ld, dn, rdn, remove );
197 if ( i != LDAP_SUCCESS ) {
198 ldap_perror( ld, "ldap_modrdn2_s" );
199 } else if ( verbose ) {
200 printf( "modrdn complete\n" );