1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
10 #include <ac/unistd.h>
11 extern char *strdup (const char *);
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 [-nvkc] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
37 char *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
39 int rc, i, kerberos, remove, havedn, authmethod;
45 kerberos = not = contoper = verbose = remove = 0;
47 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
49 while (( i = getopt( argc, argv, "kKcnvrh:p:D:w:d:f:" )) != EOF ) {
51 case 'k': /* kerberos bind */
54 case 'K': /* kerberos bind, part one only */
57 case 'c': /* continuous operation mode */
60 case 'h': /* ldap host */
61 ldaphost = strdup( optarg );
63 case 'D': /* bind DN */
64 binddn = strdup( optarg );
66 case 'w': /* password */
67 passwd = strdup( optarg );
71 ldap_debug = lber_debug = atoi( optarg ); /* */
72 #else /* LDAP_DEBUG */
73 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
74 #endif /* LDAP_DEBUG */
76 case 'f': /* read from file */
77 infile = strdup( optarg );
80 ldapport = atoi( optarg );
82 case 'n': /* print adds, don't actually do them */
85 case 'v': /* verbose mode */
88 case 'r': /* remove old RDN */
92 fprintf( stderr, usage, argv[0] );
98 if (argc - optind == 2) {
99 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
103 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
108 } else if ( argc - optind != 0 ) {
109 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
110 fprintf( stderr, usage, argv[0] );
114 if ( infile != NULL ) {
115 if (( fp = fopen( infile, "r" )) == NULL ) {
123 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
124 perror( "ldap_open" );
128 /* this seems prudent */
129 ldap_set_option( ld, LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
132 authmethod = LDAP_AUTH_SIMPLE;
133 } else if ( kerberos == 1 ) {
134 authmethod = LDAP_AUTH_KRBV41;
136 authmethod = LDAP_AUTH_KRBV4;
138 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
139 ldap_perror( ld, "ldap_bind" );
145 rc = domodrdn(ld, entrydn, rdn, remove);
146 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
147 if ( *buf != '\0' ) { /* blank lines optional, skip */
148 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
150 if ( havedn ) { /* have DN, get RDN */
151 if (( rdn = strdup( buf )) == NULL ) {
155 rc = domodrdn(ld, entrydn, rdn, remove);
157 } else if ( !havedn ) { /* don't have DN yet */
158 if (( entrydn = strdup( buf )) == NULL ) {
179 int remove) /* flag: remove old RDN */
184 printf( "modrdn %s:\n\t%s\n", dn, rdn );
186 printf("removing old RDN\n");
188 printf("keeping old RDN\n");
192 i = ldap_modrdn2_s( ld, dn, rdn, remove );
193 if ( i != LDAP_SUCCESS ) {
194 ldap_perror( ld, "ldap_modrdn2_s" );
195 } else if ( verbose ) {
196 printf( "modrdn complete\n" );