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 [-nvkWc] [-d debug-level] [-h ldaphost] [-P version] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
36 char *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
38 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug;
41 not = contoper = verbose = remove = want_bindpw = debug = 0;
42 authmethod = LDAP_AUTH_SIMPLE;
45 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
47 while (( i = getopt( argc, argv, "WkKcnvrh:P:p:D:w:d:f:" )) != EOF ) {
49 case 'k': /* kerberos bind */
51 authmethod = LDAP_AUTH_KRBV4;
53 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
56 case 'K': /* kerberos bind, part one only */
58 authmethod = LDAP_AUTH_KRBV41;
60 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
63 case 'c': /* continuous operation mode */
66 case 'h': /* ldap host */
67 ldaphost = strdup( optarg );
69 case 'D': /* bind DN */
70 binddn = strdup( optarg );
72 case 'w': /* password */
73 passwd = strdup( optarg );
76 debug |= atoi( optarg );
78 case 'f': /* read from file */
79 infile = strdup( optarg );
82 ldapport = atoi( optarg );
84 case 'n': /* print adds, don't actually do them */
87 case 'v': /* verbose mode */
90 case 'r': /* remove old RDN */
100 version = LDAP_VERSION2;
103 version = LDAP_VERSION3;
108 fprintf( stderr, usage, argv[0] );
114 if (argc - optind == 2) {
115 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
119 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
124 } else if ( argc - optind != 0 ) {
125 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
126 fprintf( stderr, usage, argv[0] );
130 if ( infile != NULL ) {
131 if (( fp = fopen( infile, "r" )) == NULL ) {
140 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
141 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
144 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
145 perror( "ldap_open" );
149 /* this seems prudent */
151 int deref = LDAP_DEREF_NEVER;
152 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
156 passwd = getpass("Enter LDAP Password: ");
159 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
162 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
163 ldap_perror( ld, "ldap_bind" );
169 rc = domodrdn(ld, entrydn, rdn, remove);
170 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
171 if ( *buf != '\0' ) { /* blank lines optional, skip */
172 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
174 if ( havedn ) { /* have DN, get RDN */
175 if (( rdn = strdup( buf )) == NULL ) {
179 rc = domodrdn(ld, entrydn, rdn, remove);
181 } else if ( !havedn ) { /* don't have DN yet */
182 if (( entrydn = strdup( buf )) == NULL ) {
203 int remove) /* flag: remove old RDN */
208 printf( "modrdn %s:\n\t%s\n", dn, rdn );
210 printf("removing old RDN\n");
212 printf("keeping old RDN\n");
216 i = ldap_modrdn2_s( ld, dn, rdn, remove );
217 if ( i != LDAP_SUCCESS ) {
218 ldap_perror( ld, "ldap_modrdn2_s" );
219 } else if ( verbose ) {
220 printf( "modrdn complete\n" );