1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
12 #include "ldapconfig.h"
14 static char *binddn = LDAPMODRDN_BINDDN;
15 static char *passwd = LDAPMODRDN_BIND_CRED;
16 static char *base = LDAPMODRDN_BASE;
17 static char *ldaphost = LDAPHOST;
18 static int ldapport = LDAP_PORT;
19 static int not, verbose, contoper;
23 extern int ldap_debug, lber_debug;
24 #endif /* LDAP_DEBUG */
26 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
34 char *usage = "usage: %s [-nvkc] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
35 char *myname,*infile, *p, *entrydn, *rdn, buf[ 4096 ];
37 int rc, i, kerberos, remove, havedn, authmethod;
44 kerberos = not = contoper = verbose = remove = 0;
46 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
48 while (( i = getopt( argc, argv, "kKcnvrh:p:D:w:d:f:" )) != EOF ) {
50 case 'k': /* kerberos bind */
53 case 'K': /* kerberos bind, part one only */
56 case 'c': /* continuous operation mode */
59 case 'h': /* ldap host */
60 ldaphost = strdup( optarg );
62 case 'D': /* bind DN */
63 binddn = strdup( optarg );
65 case 'w': /* password */
66 passwd = strdup( optarg );
70 ldap_debug = lber_debug = atoi( optarg ); /* */
71 #else /* LDAP_DEBUG */
72 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
73 #endif /* LDAP_DEBUG */
75 case 'f': /* read from file */
76 infile = strdup( optarg );
79 ldapport = atoi( optarg );
81 case 'n': /* print adds, don't actually do them */
84 case 'v': /* verbose mode */
87 case 'r': /* remove old RDN */
91 fprintf( stderr, usage, argv[0] );
97 if (argc - optind == 2) {
98 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
102 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
107 } else if ( argc - optind != 0 ) {
108 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
109 fprintf( stderr, usage, argv[0] );
113 if ( infile != NULL ) {
114 if (( fp = fopen( infile, "r" )) == NULL ) {
122 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
123 perror( "ldap_open" );
127 ld->ld_deref = LDAP_DEREF_NEVER; /* this seems prudent */
130 authmethod = LDAP_AUTH_SIMPLE;
131 } else if ( kerberos == 1 ) {
132 authmethod = LDAP_AUTH_KRBV41;
134 authmethod = LDAP_AUTH_KRBV4;
136 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
137 ldap_perror( ld, "ldap_bind" );
143 rc = domodrdn(ld, entrydn, rdn, remove);
144 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
145 if ( *buf != '\0' ) { /* blank lines optional, skip */
146 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
148 if ( havedn ) { /* have DN, get RDN */
149 if (( rdn = strdup( buf )) == NULL ) {
153 rc = domodrdn(ld, entrydn, rdn, remove);
155 } else if ( !havedn ) { /* don't have DN yet */
156 if (( entrydn = strdup( buf )) == NULL ) {
170 domodrdn( ld, dn, rdn, remove )
174 int remove; /* flag: remove old RDN */
179 printf( "modrdn %s:\n\t%s\n", dn, rdn );
181 printf("removing old RDN\n");
183 printf("keeping old RDN\n");
187 i = ldap_modrdn2_s( ld, dn, rdn, remove );
188 if ( i != LDAP_SUCCESS ) {
189 ldap_perror( ld, "ldap_modrdn2_s" );
190 } else if ( verbose ) {
191 printf( "modrdn complete\n" );