1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
10 #include <ac/string.h>
11 #include <ac/unistd.h>
16 static char *binddn = NULL;
17 static char *passwd = NULL;
18 static char *base = NULL;
19 static char *ldaphost = NULL;
20 static int ldapport = 0;
21 static int not, verbose, contoper;
24 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
27 static int domodrdn LDAP_P((
31 int remove)); /* flag: remove old RDN */
34 main(int argc, char **argv)
36 char *usage = "usage: %s [-nvkWc] [-d debug-level] [-h ldaphost] [-P version] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
37 char *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
39 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug;
42 not = contoper = verbose = remove = want_bindpw = debug = 0;
43 authmethod = LDAP_AUTH_SIMPLE;
46 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
48 while (( i = getopt( argc, argv, "WkKcnvrh:P:p:D:w:d:f:" )) != EOF ) {
50 case 'k': /* kerberos bind */
52 authmethod = LDAP_AUTH_KRBV4;
54 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
57 case 'K': /* kerberos bind, part one only */
59 authmethod = LDAP_AUTH_KRBV41;
61 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
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 );
77 debug |= atoi( optarg );
79 case 'f': /* read from file */
80 infile = strdup( optarg );
83 ldapport = atoi( optarg );
85 case 'n': /* print adds, don't actually do them */
88 case 'v': /* verbose mode */
91 case 'r': /* remove old RDN */
101 version = LDAP_VERSION2;
104 version = LDAP_VERSION3;
109 fprintf( stderr, usage, argv[0] );
115 if (argc - optind == 2) {
116 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
120 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
125 } else if ( argc - optind != 0 ) {
126 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
127 fprintf( stderr, usage, argv[0] );
131 if ( infile != NULL ) {
132 if (( fp = fopen( infile, "r" )) == NULL ) {
141 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
142 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
146 (void) SIGNAL( SIGPIPE, SIG_IGN );
149 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
150 perror( "ldap_init" );
154 /* this seems prudent */
156 int deref = LDAP_DEREF_NEVER;
157 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
161 passwd = getpass("Enter LDAP Password: ");
164 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
167 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
168 ldap_perror( ld, "ldap_bind" );
174 rc = domodrdn(ld, entrydn, rdn, remove);
175 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
176 if ( *buf != '\0' ) { /* blank lines optional, skip */
177 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
179 if ( havedn ) { /* have DN, get RDN */
180 if (( rdn = strdup( buf )) == NULL ) {
184 rc = domodrdn(ld, entrydn, rdn, remove);
186 } else if ( !havedn ) { /* don't have DN yet */
187 if (( entrydn = strdup( buf )) == NULL ) {
208 int remove) /* flag: remove old RDN */
213 printf( "modrdn %s:\n\t%s\n", dn, rdn );
215 printf("removing old RDN\n");
217 printf("keeping old RDN\n");
221 i = ldap_modrdn2_s( ld, dn, rdn, remove );
222 if ( i != LDAP_SUCCESS ) {
223 ldap_perror( ld, "ldap_modrdn2_s" );
224 } else if ( verbose ) {
225 printf( "modrdn complete\n" );