1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
10 #include "ldapconfig.h"
12 static char *binddn = LDAPMODRDN_BINDDN;
13 static char *base = LDAPMODRDN_BASE;
14 static char *passwd = NULL;
15 static char *ldaphost = LDAPHOST;
16 static int ldapport = LDAP_PORT;
17 static int not, verbose, contoper;
21 extern int ldap_debug, lber_debug;
22 #endif /* LDAP_DEBUG */
24 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
32 char *usage = "usage: %s [-nvkc] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
33 char *myname,*infile, *p, *entrydn, *rdn, buf[ 4096 ];
35 int rc, i, kerberos, remove, havedn, authmethod;
42 kerberos = not = contoper = verbose = remove = 0;
44 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
46 while (( i = getopt( argc, argv, "kKcnvrh:p:D:w:d:f:" )) != EOF ) {
48 case 'k': /* kerberos bind */
51 case 'K': /* kerberos bind, part one only */
54 case 'c': /* continuous operation mode */
57 case 'h': /* ldap host */
58 ldaphost = strdup( optarg );
60 case 'D': /* bind DN */
61 binddn = strdup( optarg );
63 case 'w': /* password */
64 passwd = strdup( optarg );
68 ldap_debug = lber_debug = atoi( optarg ); /* */
69 #else /* LDAP_DEBUG */
70 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
71 #endif /* LDAP_DEBUG */
73 case 'f': /* read from file */
74 infile = strdup( optarg );
77 ldapport = atoi( optarg );
79 case 'n': /* print adds, don't actually do them */
82 case 'v': /* verbose mode */
85 case 'r': /* remove old RDN */
89 fprintf( stderr, usage, argv[0] );
95 if (argc - optind == 2) {
96 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
100 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
105 } else if ( argc - optind != 0 ) {
106 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
107 fprintf( stderr, usage, argv[0] );
111 if ( infile != NULL ) {
112 if (( fp = fopen( infile, "r" )) == NULL ) {
120 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
121 perror( "ldap_open" );
125 ld->ld_deref = LDAP_DEREF_NEVER; /* this seems prudent */
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 ) {
168 domodrdn( ld, dn, rdn, remove )
172 int remove; /* flag: remove old RDN */
177 printf( "modrdn %s:\n\t%s\n", dn, rdn );
179 printf("removing old RDN\n");
181 printf("keeping old RDN\n");
185 i = ldap_modrdn2_s( ld, dn, rdn, remove );
186 if ( i != LDAP_SUCCESS ) {
187 ldap_perror( ld, "ldap_modrdn2_s" );
188 } else if ( verbose ) {
189 printf( "modrdn complete\n" );