]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
7c916414317df0a65e8bd987e4b627f6746f360e
[openldap] / clients / tools / ldapmodrdn.c
1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include <ac/ctype.h>
9 #include <ac/string.h>
10 #include <ac/unistd.h>
11
12 #include <lber.h>
13 #include <ldap.h>
14
15 static int      not, verbose, contoper;
16
17 #define safe_realloc( ptr, size )       ( ptr == NULL ? malloc( size ) : \
18                                          realloc( ptr, size ))
19
20 static void
21 usage(char *s)
22 {
23     fprintf(stderr, "Usage: %s [options] [dn]...\n", s);
24     fprintf(stderr, "  -c\t\tcontinuous operation mode\n");
25     fprintf(stderr, "  -D bindnd\tbind dn\n");
26     fprintf(stderr, "  -d level\tdebugging level\n");
27     fprintf(stderr, "  -f file\tread from file\n");
28     fprintf(stderr, "  -h host\tldap sever\n");
29 #ifdef HAVE_KERBEROS
30     fprintf(stderr, "  -K\t\tuse Kerberos step 1\n");
31     fprintf(stderr, "  -k\t\tuse Kerberos instead of Simple Password authentication\n");
32 #endif
33     fprintf(stderr, "  -n\t\tmake no modifications\n");
34     fprintf(stderr, "  -p port\tldap port\n");
35     fprintf(stderr, "  -r\t\tremove old RDN\n");
36     fprintf(stderr, "  -v\t\tverbose\n");
37     fprintf(stderr, "  -W\t\tprompt for bind password\n");
38     fprintf(stderr, "  -w passwd\tbind password (for simple authentication)\n");
39     exit(1);
40 }
41
42 static int domodrdn LDAP_P((
43     LDAP        *ld,
44     char        *dn,
45     char        *rdn,
46     int         remove));       /* flag: remove old RDN */
47
48 int
49 main(int argc, char **argv)
50 {
51     FILE        *fp = NULL;
52     LDAP        *ld = NULL;
53     char        *myname, *infile, *entrydn, *rdn, buf[ 4096 ];
54     char        *binddn = NULL;
55     char        *passwd = NULL;
56     char        *ldaphost = NULL;
57     int         rc, i, remove, havedn, want_passwd;
58     int         authmethod = LDAP_AUTH_SIMPLE;
59     int         ldapport = LDAP_PORT;
60
61     infile = entrydn = rdn = NULL;
62     not = contoper = verbose = remove = want_passwd = 0;
63     myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
64
65     while ((i = getopt(argc, argv, "cD:d:f:h:Kknp:rvWw:")) != EOF)
66     {
67         switch(i)
68         {
69         case 'c':       /* continuous operation mode */
70             contoper++;
71             break;
72
73         case 'D':       /* bind DN */
74             binddn = strdup( optarg );
75             break;
76
77         case 'd':
78 #ifdef LDAP_DEBUG
79             ldap_debug = lber_debug = atoi( optarg );
80 #else /* LDAP_DEBUG */
81             fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
82 #endif /* LDAP_DEBUG */
83             break;
84
85         case 'f':       /* read from file */
86             infile = strdup( optarg );
87             break;
88
89         case 'h':       /* ldap host */
90             ldaphost = strdup( optarg );
91             break;
92
93         case 'K':       /* kerberos bind, part one only */
94 #ifdef HAVE_KERBEROS
95             authmethod = LDAP_AUTH_KRBV41;
96 #else
97             fprintf(stderr, "%s was not compiled with Kerberos support\n", argv[0]);
98 #endif
99             break;
100
101         case 'k':       /* kerberos bind */
102 #ifdef HAVE_KERBEROS
103             authmethod = LDAP_AUTH_KRBV4;
104 #else
105             fprintf(stderr, "%s was not compiled with Kerberos support\n", argv[0]);
106 #endif
107             break;
108
109         case 'n':       /* print adds, don't actually do them */
110             not++;
111             break;
112
113         case 'p':
114             ldapport = atoi( optarg );
115             break;
116
117         case 'r':       /* remove old RDN */
118             remove++;
119             break;
120
121         case 'v':       /* verbose mode */
122             verbose++;
123             break;
124
125         case 'W':
126             want_passwd++;
127             break;
128
129         case 'w':       /* password */
130             passwd = strdup(optarg);
131             break;
132
133         default:
134             usage(argv[0]);
135         }
136     }
137
138     havedn = 0;
139     if (argc - optind == 2)
140     {
141         if (( rdn = strdup( argv[argc - 1] )) == NULL )
142         {
143             perror( "strdup" );
144             return(1);
145         }
146         if (( entrydn = strdup( argv[argc - 2] )) == NULL )
147         {
148             perror( "strdup" );
149             return( 1 );
150         }
151         havedn++;
152     } else if (argc - optind != 0) {
153         fprintf(stderr, "%s: invalid number of arguments, only two allowed\n", myname);
154         usage(argv[0]);
155     }
156
157     if (want_passwd && !passwd)
158         passwd = strdup(getpass("Enter LDAP password: "));
159
160     if (infile != NULL)
161     {
162         if ((fp = fopen( infile, "r" )) == NULL)
163         {
164             perror(infile);
165             return(1);
166         }
167     } else
168         fp = stdin;
169
170     if ((ld = ldap_open(ldaphost, ldapport)) == NULL)
171     {
172         perror("ldap_open");
173         return(1);
174     }
175
176     /* this seems prudent */
177     ldap_set_option(ld, LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
178
179     if (ldap_bind_s(ld, binddn, passwd, authmethod) != LDAP_SUCCESS)
180     {
181         ldap_perror(ld, "ldap_bind");
182         return(1);
183     }
184
185     rc = 0;
186     if (havedn)
187         rc = domodrdn(ld, entrydn, rdn, remove);
188     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
189         if (*buf != '\0') {     /* blank lines optional, skip */
190             buf[strlen(buf) - 1] = '\0';        /* remove nl */
191
192             if (havedn)
193             {
194                 /* have DN, get RDN */
195                 if (( rdn = strdup( buf )) == NULL)
196                 {
197                     perror( "strdup" );
198                     exit( 1 );
199                 }
200                 rc = domodrdn(ld, entrydn, rdn, remove);
201                 havedn = 0;
202             } else if (!havedn) {
203                 /* don't have DN yet */
204                 if (( entrydn = strdup( buf )) == NULL )
205                 {
206                     perror( "strdup" );
207                     exit( 1 );
208                 }
209                 havedn++;
210             }
211         }
212     }
213
214     ldap_unbind(ld);
215
216     return(rc);
217 }
218
219 static int domodrdn(
220     LDAP        *ld,
221     char        *dn,
222     char        *rdn,
223     int         remove) /* flag: remove old RDN */
224 {
225     int rc = LDAP_SUCCESS;
226
227     if (verbose)
228     {
229         printf("modrdn %s:\n\t%s\n", dn, rdn);
230         if (remove)
231             printf("removing old RDN\n");
232         else
233             printf("keeping old RDN\n");
234     }
235
236     if (!not)
237     {
238         rc = ldap_modrdn2_s(ld, dn, rdn, remove);
239         if (rc != LDAP_SUCCESS)
240             ldap_perror(ld, "ldap_modrdn2_s");
241         else if (verbose)
242             printf("modrdn complete\n");
243     }
244
245     return(rc);
246 }