]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
cddbf8192e73fc0a45a0d582370d8136de2578f5
[openldap] / clients / tools / ldapmodify.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* ldapmodify.c - generic program to modify or add entries using LDAP */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/stdlib.h>
13
14 #include <ac/ctype.h>
15 #include <ac/signal.h>
16 #include <ac/string.h>
17 #include <ac/unistd.h>
18
19 #ifdef HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22
23 #ifdef HAVE_SYS_FILE_H
24 #include <sys/file.h>
25 #endif
26 #ifdef HAVE_FCNTL_H
27 #include <fcntl.h>
28 #endif
29
30 #include <ldap.h>
31
32 #include "lutil.h"
33 #include "lutil_ldap.h"
34 #include "ldif.h"
35 #include "ldap_defaults.h"
36 #include "ldap_log.h"
37 #include "ldap_pvt.h"
38
39 static char     *prog;
40 static char     *binddn = NULL;
41 static struct berval passwd = { 0, NULL };
42 static char *ldapuri = NULL;
43 static char     *ldaphost = NULL;
44 static int      ldapport = 0;
45 #ifdef HAVE_CYRUS_SASL
46 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
47 static char *sasl_realm = NULL;
48 static char     *sasl_authc_id = NULL;
49 static char     *sasl_authz_id = NULL;
50 static char     *sasl_mech = NULL;
51 static char     *sasl_secprops = NULL;
52 #endif
53 static int      use_tls = 0;
54 static int      ldapadd, not, verbose, contoper, force;
55 static LDAP     *ld = NULL;
56
57 #define LDAPMOD_MAXLINE         4096
58
59 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
60 #define T_VERSION_STR           "version"
61 #define T_REPLICA_STR           "replica"
62 #define T_DN_STR                "dn"
63 #define T_CONTROL_STR           "control"
64 #define T_CHANGETYPESTR         "changetype"
65 #define T_ADDCTSTR              "add"
66 #define T_MODIFYCTSTR           "modify"
67 #define T_DELETECTSTR           "delete"
68 #define T_MODRDNCTSTR           "modrdn"
69 #define T_MODDNCTSTR            "moddn"
70 #define T_RENAMECTSTR           "rename"
71 #define T_MODOPADDSTR           "add"
72 #define T_MODOPREPLACESTR       "replace"
73 #define T_MODOPDELETESTR        "delete"
74 #define T_MODSEPSTR             "-"
75 #define T_NEWRDNSTR             "newrdn"
76 #define T_DELETEOLDRDNSTR       "deleteoldrdn"
77 #define T_NEWSUPSTR             "newsuperior"
78
79
80 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
81 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
82 static int parse_ldif_control LDAP_P(( char *line, LDAPControl ***pctrls ));
83 static void addmodifyop LDAP_P((
84         LDAPMod ***pmodsp, int modop,
85         const char *attr,
86         struct berval *value ));
87 static int domodify LDAP_P((
88         const char *dn,
89         LDAPMod **pmods,
90     LDAPControl **pctrls,
91         int newentry ));
92 static int dodelete LDAP_P((
93         const char *dn,
94     LDAPControl **pctrls ));
95 static int dorename LDAP_P((
96         const char *dn,
97         const char *newrdn,
98         const char *newsup,
99         int deleteoldrdn,
100     LDAPControl **pctrls ));
101 static char *read_one_record LDAP_P(( FILE *fp ));
102
103 static void
104 usage( const char *prog )
105 {
106     fprintf( stderr,
107 "Add or modify entries from an LDAP server\n\n"
108 "usage: %s [options]\n"
109 "       The list of desired operations are read from stdin or from the file\n"
110 "       specified by \"-f file\".\n"
111 "Add or modify options:\n"
112 "  -a         add values (default%s)\n"
113 "  -c         continuous operation mode (do not stop on errors)\n"
114 "  -F         force all changes records to be used\n"
115 "  -S file    write skipped modifications to `file'\n"
116
117 "Common options:\n"
118 "  -d level   set LDAP debugging level to `level'\n"
119 "  -D binddn  bind DN\n"
120 "  -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
121 "             [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n"
122 "             [!]manageDSAit   (alternate form, see -M)\n"
123 "             [!]noop\n"
124 "  -f file    read operations from `file'\n"
125 "  -h host    LDAP server\n"
126 "  -H URI     LDAP Uniform Resource Indentifier(s)\n"
127 "  -I         use SASL Interactive mode\n"
128 "  -k         use Kerberos authentication\n"
129 "  -K         like -k, but do only step 1 of the Kerberos bind\n"
130 "  -M         enable Manage DSA IT control (-MM to make critical)\n"
131 "  -n         show what would be done but don't actually update\n"
132 "  -O props   SASL security properties\n"
133 "  -p port    port on LDAP server\n"
134 "  -P version procotol version (default: 3)\n"
135 "  -Q         use SASL Quiet mode\n"
136 "  -R realm   SASL realm\n"
137 "  -U authcid SASL authentication identity\n"
138 "  -v         run in verbose mode (diagnostics to standard output)\n"
139 "  -w passwd  bind passwd (for simple authentication)\n"
140 "  -W         prompt for bind passwd\n"
141 "  -x         Simple authentication\n"
142 "  -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
143 "  -y file    Read passwd from file\n"
144 "  -Y mech    SASL mechanism\n"
145 "  -Z         Start TLS request (-ZZ to require successful response)\n"
146              , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
147
148     exit( EXIT_FAILURE );
149 }
150
151
152 int
153 main( int argc, char **argv )
154 {
155     char                *infile, *rejfile, *rbuf, *start, *rejbuf = NULL;
156     FILE                *fp, *rejfp;
157         char            *matched_msg = NULL, *error_msg = NULL;
158         int             rc, retval, i, authmethod, version, want_bindpw;
159         int             debug, manageDSAit, noop, referrals;
160         int count, len;
161         char    *pw_file = NULL;
162         char    *control, *cvalue;
163         char    *authzid = NULL;
164         int             crit;
165
166     prog = lutil_progname( "ldapmodify", argc, argv );
167
168     /* Print usage when no parameters */
169     if( argc < 2 ) usage( prog );
170
171         /* strncmp instead of strcmp since NT binaries carry .exe extension */
172     ldapadd = ( strncmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
173
174     infile = NULL;
175     rejfile = NULL;
176     not = verbose = want_bindpw = debug = manageDSAit = noop = referrals = 0;
177     authmethod = -1;
178         version = -1;
179
180     while (( i = getopt( argc, argv, "acrf:E:F"
181                 "Cd:D:e:h:H:IkKMnO:p:P:QR:S:U:vw:WxX:y:Y:Z" )) != EOF )
182         {
183         switch( i ) {
184         /* Modify Options */
185         case 'a':       /* add */
186             ldapadd = 1;
187             break;
188         case 'c':       /* continuous operation */
189             contoper = 1;
190             break;
191         case 'E': /* modify controls */
192                 if( version == LDAP_VERSION2 ) {
193                         fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
194                                 prog, version );
195                         return EXIT_FAILURE;
196                 }
197
198                 /* should be extended to support comma separated list of
199                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
200                  */
201
202                 crit = 0;
203                 cvalue = NULL;
204                 if( optarg[0] == '!' ) {
205                         crit = 1;
206                         optarg++;
207                 }
208
209                 control = ber_strdup( optarg );
210                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
211                         *cvalue++ = '\0';
212                 }
213                 fprintf( stderr, "Invalid modify control name: %s\n", control );
214                 usage(prog);
215                 return EXIT_FAILURE;
216         case 'f':       /* read from file */
217                 if( infile != NULL ) {
218                         fprintf( stderr, "%s: -f previously specified\n", prog );
219                         return EXIT_FAILURE;
220                 }
221             infile = ber_strdup( optarg );
222             break;
223         case 'F':       /* force all changes records to be used */
224             force = 1;
225             break;
226
227         /* Common Options */
228         case 'C':
229                 referrals++;
230                 break;
231         case 'd':
232             debug |= atoi( optarg );
233             break;
234         case 'D':       /* bind DN */
235                 if( binddn != NULL ) {
236                         fprintf( stderr, "%s: -D previously specified\n", prog );
237                         return EXIT_FAILURE;
238                 }
239             binddn = ber_strdup( optarg );
240             break;
241         case 'e': /* general controls */
242                 if( version == LDAP_VERSION2 ) {
243                         fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
244                                 prog, version );
245                         return EXIT_FAILURE;
246                 }
247
248                 /* should be extended to support comma separated list of
249                  *      [!]key[=value] parameters, e.g.  -e !foo,bar=567
250                  */
251
252                 crit = 0;
253                 cvalue = NULL;
254                 if( optarg[0] == '!' ) {
255                         crit = 1;
256                         optarg++;
257                 }
258
259                 control = ber_strdup( optarg );
260                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
261                         *cvalue++ = '\0';
262                 }
263
264                 if ( strcasecmp( control, "authzid" ) == 0 ) {
265                         if( authzid != NULL ) {
266                                 fprintf( stderr, "authzid control previously specified\n");
267                                 return EXIT_FAILURE;
268                         }
269                         if( cvalue == NULL ) {
270                                 fprintf( stderr, "authzid: control value expected\n" );
271                                 usage(prog);
272                                 return EXIT_FAILURE;
273                         }
274                         if( !crit ) {
275                                 fprintf( stderr, "authzid: must be marked critical\n" );
276                                 usage(prog);
277                                 return EXIT_FAILURE;
278                         }
279
280                         assert( authzid == NULL );
281                         authzid = control;
282                         break;
283                         
284                 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
285                         if( manageDSAit ) {
286                                 fprintf( stderr, "manageDSAit control previously specified\n");
287                                 return EXIT_FAILURE;
288                         }
289                         if( cvalue != NULL ) {
290                                 fprintf( stderr, "manageDSAit: no control value expected\n" );
291                                 usage(prog);
292                                 return EXIT_FAILURE;
293                         }
294
295                         manageDSAit = 1 + crit;
296                         break;
297                         
298                 } else if ( strcasecmp( control, "noop" ) == 0 ) {
299                         if( noop ) {
300                                 fprintf( stderr, "noop control previously specified\n");
301                                 return EXIT_FAILURE;
302                         }
303                         if( cvalue != NULL ) {
304                                 fprintf( stderr, "noop: no control value expected\n" );
305                                 usage(prog);
306                                 return EXIT_FAILURE;
307                         }
308
309                         noop = 1 + crit;
310                         break;
311
312                 } else {
313                         fprintf( stderr, "Invalid general control name: %s\n", control );
314                         usage(prog);
315                         return EXIT_FAILURE;
316                 }
317         case 'h':       /* ldap host */
318                 if( ldapuri != NULL ) {
319                         fprintf( stderr, "%s: -h incompatible with -H\n", prog );
320                         return EXIT_FAILURE;
321                 }
322                 if( ldaphost != NULL ) {
323                         fprintf( stderr, "%s: -h previously specified\n", prog );
324                         return EXIT_FAILURE;
325                 }
326             ldaphost = ber_strdup( optarg );
327             break;
328         case 'H':       /* ldap URI */
329                 if( ldaphost != NULL ) {
330                         fprintf( stderr, "%s: -H incompatible with -h\n", prog );
331                         return EXIT_FAILURE;
332                 }
333                 if( ldapport ) {
334                         fprintf( stderr, "%s: -H incompatible with -p\n", prog );
335                         return EXIT_FAILURE;
336                 }
337                 if( ldapuri != NULL ) {
338                         fprintf( stderr, "%s: -H previously specified\n", prog );
339                         return EXIT_FAILURE;
340                 }
341             ldapuri = ber_strdup( optarg );
342             break;
343         case 'I':
344 #ifdef HAVE_CYRUS_SASL
345                 if( version == LDAP_VERSION2 ) {
346                         fprintf( stderr, "%s: -I incompatible with version %d\n",
347                                 prog, version );
348                         return EXIT_FAILURE;
349                 }
350                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
351                         fprintf( stderr, "%s: incompatible previous "
352                                 "authentication choice\n",
353                                 prog );
354                         return EXIT_FAILURE;
355                 }
356                 authmethod = LDAP_AUTH_SASL;
357                 version = LDAP_VERSION3;
358                 sasl_flags = LDAP_SASL_INTERACTIVE;
359                 break;
360 #else
361                 fprintf( stderr, "%s: was not compiled with SASL support\n",
362                         prog );
363                 return( EXIT_FAILURE );
364 #endif
365         case 'k':       /* kerberos bind */
366 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
367                 if( version > LDAP_VERSION2 ) {
368                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
369                                 prog, version );
370                         return EXIT_FAILURE;
371                 }
372
373                 if( authmethod != -1 ) {
374                         fprintf( stderr, "%s: -k incompatible with previous "
375                                 "authentication choice\n", prog );
376                         return EXIT_FAILURE;
377                 }
378                         
379                 authmethod = LDAP_AUTH_KRBV4;
380 #else
381                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
382                 return EXIT_FAILURE;
383 #endif
384             break;
385         case 'K':       /* kerberos bind, part one only */
386 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
387                 if( version > LDAP_VERSION2 ) {
388                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
389                                 prog, version );
390                         return EXIT_FAILURE;
391                 }
392                 if( authmethod != -1 ) {
393                         fprintf( stderr, "%s: incompatible with previous "
394                                 "authentication choice\n", prog );
395                         return EXIT_FAILURE;
396                 }
397
398                 authmethod = LDAP_AUTH_KRBV41;
399 #else
400                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
401                 return( EXIT_FAILURE );
402 #endif
403             break;
404         case 'M':
405                 /* enable Manage DSA IT */
406                 if( version == LDAP_VERSION2 ) {
407                         fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
408                                 prog, version );
409                         return EXIT_FAILURE;
410                 }
411                 manageDSAit++;
412                 version = LDAP_VERSION3;
413                 break;
414         case 'n':       /* print deletes, don't actually do them */
415             ++not;
416             break;
417         case 'O':
418 #ifdef HAVE_CYRUS_SASL
419                 if( sasl_secprops != NULL ) {
420                         fprintf( stderr, "%s: -O previously specified\n", prog );
421                         return EXIT_FAILURE;
422                 }
423                 if( version == LDAP_VERSION2 ) {
424                         fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
425                                 prog, version );
426                         return EXIT_FAILURE;
427                 }
428                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
429                         fprintf( stderr, "%s: incompatible previous "
430                                 "authentication choice\n", prog );
431                         return EXIT_FAILURE;
432                 }
433                 authmethod = LDAP_AUTH_SASL;
434                 version = LDAP_VERSION3;
435                 sasl_secprops = ber_strdup( optarg );
436 #else
437                 fprintf( stderr, "%s: not compiled with SASL support\n",
438                         prog );
439                 return( EXIT_FAILURE );
440 #endif
441                 break;
442         case 'p':
443                 if( ldapport ) {
444                         fprintf( stderr, "%s: -p previously specified\n", prog );
445                         return EXIT_FAILURE;
446                 }
447             ldapport = atoi( optarg );
448             break;
449         case 'P':
450                 switch( atoi(optarg) ) {
451                 case 2:
452                         if( version == LDAP_VERSION3 ) {
453                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
454                                         prog, version );
455                                 return EXIT_FAILURE;
456                         }
457                         version = LDAP_VERSION2;
458                         break;
459                 case 3:
460                         if( version == LDAP_VERSION2 ) {
461                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
462                                         prog, version );
463                                 return EXIT_FAILURE;
464                         }
465                         version = LDAP_VERSION3;
466                         break;
467                 default:
468                         fprintf( stderr, "%s: protocol version should be 2 or 3\n",
469                                 prog );
470                         usage( prog );
471                         return( EXIT_FAILURE );
472                 } break;
473         case 'Q':
474 #ifdef HAVE_CYRUS_SASL
475                 if( version == LDAP_VERSION2 ) {
476                         fprintf( stderr, "%s: -Q incompatible with version %d\n",
477                                 prog, version );
478                         return EXIT_FAILURE;
479                 }
480                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
481                         fprintf( stderr, "%s: incompatible previous "
482                                 "authentication choice\n",
483                                 prog );
484                         return EXIT_FAILURE;
485                 }
486                 authmethod = LDAP_AUTH_SASL;
487                 version = LDAP_VERSION3;
488                 sasl_flags = LDAP_SASL_QUIET;
489                 break;
490 #else
491                 fprintf( stderr, "%s: not compiled with SASL support\n",
492                         prog );
493                 return( EXIT_FAILURE );
494 #endif
495         case 'r':       /* replace (obsolete) */
496                 break;
497
498         case 'R':
499 #ifdef HAVE_CYRUS_SASL
500                 if( sasl_realm != NULL ) {
501                         fprintf( stderr, "%s: -R previously specified\n", prog );
502                         return EXIT_FAILURE;
503                 }
504                 if( version == LDAP_VERSION2 ) {
505                         fprintf( stderr, "%s: -R incompatible with version %d\n",
506                                 prog, version );
507                         return EXIT_FAILURE;
508                 }
509                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
510                         fprintf( stderr, "%s: incompatible previous "
511                                 "authentication choice\n",
512                                 prog );
513                         return EXIT_FAILURE;
514                 }
515                 authmethod = LDAP_AUTH_SASL;
516                 version = LDAP_VERSION3;
517                 sasl_realm = ber_strdup( optarg );
518 #else
519                 fprintf( stderr, "%s: not compiled with SASL support\n",
520                         prog );
521                 return( EXIT_FAILURE );
522 #endif
523                 break;
524         case 'S':       /* skipped modifications to file */
525                 if( rejfile != NULL ) {
526                         fprintf( stderr, "%s: -S previously specified\n", prog );
527                         return EXIT_FAILURE;
528                 }
529                 rejfile = ber_strdup( optarg );
530                 break;
531         case 'U':
532 #ifdef HAVE_CYRUS_SASL
533                 if( sasl_authc_id != NULL ) {
534                         fprintf( stderr, "%s: -U previously specified\n", prog );
535                         return EXIT_FAILURE;
536                 }
537                 if( version == LDAP_VERSION2 ) {
538                         fprintf( stderr, "%s: -U incompatible with version %d\n",
539                                 prog, version );
540                         return EXIT_FAILURE;
541                 }
542                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
543                         fprintf( stderr, "%s: incompatible previous "
544                                 "authentication choice\n",
545                                 prog );
546                         return EXIT_FAILURE;
547                 }
548                 authmethod = LDAP_AUTH_SASL;
549                 version = LDAP_VERSION3;
550                 sasl_authc_id = ber_strdup( optarg );
551 #else
552                 fprintf( stderr, "%s: not compiled with SASL support\n",
553                         prog );
554                 return( EXIT_FAILURE );
555 #endif
556                 break;
557         case 'v':       /* verbose mode */
558             verbose++;
559             break;
560         case 'w':       /* password */
561             passwd.bv_val = ber_strdup( optarg );
562                 {
563                         char* p;
564
565                         for( p = optarg; *p != '\0'; p++ ) {
566                                 *p = '\0';
567                         }
568                 }
569                 passwd.bv_len = strlen( passwd.bv_val );
570             break;
571         case 'W':
572                 want_bindpw++;
573                 break;
574         case 'y':
575                 pw_file = optarg;
576                 break;
577         case 'Y':
578 #ifdef HAVE_CYRUS_SASL
579                 if( sasl_mech != NULL ) {
580                         fprintf( stderr, "%s: -Y previously specified\n", prog );
581                         return EXIT_FAILURE;
582                 }
583                 if( version == LDAP_VERSION2 ) {
584                         fprintf( stderr, "%s: -Y incompatible with version %d\n",
585                                 prog, version );
586                         return EXIT_FAILURE;
587                 }
588                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
589                         fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
590                         return EXIT_FAILURE;
591                 }
592                 authmethod = LDAP_AUTH_SASL;
593                 version = LDAP_VERSION3;
594                 sasl_mech = ber_strdup( optarg );
595 #else
596                 fprintf( stderr, "%s: not compiled with SASL support\n",
597                         prog );
598                 return( EXIT_FAILURE );
599 #endif
600                 break;
601         case 'x':
602                 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
603                         fprintf( stderr, "%s: incompatible with previous "
604                                 "authentication choice\n", prog );
605                         return EXIT_FAILURE;
606                 }
607                 authmethod = LDAP_AUTH_SIMPLE;
608                 break;
609         case 'X':
610 #ifdef HAVE_CYRUS_SASL
611                 if( sasl_authz_id != NULL ) {
612                         fprintf( stderr, "%s: -X previously specified\n", prog );
613                         return EXIT_FAILURE;
614                 }
615                 if( version == LDAP_VERSION2 ) {
616                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
617                                 prog, version );
618                         return EXIT_FAILURE;
619                 }
620                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
621                         fprintf( stderr, "%s: -X incompatible with "
622                                 "authentication choice\n", prog );
623                         return EXIT_FAILURE;
624                 }
625                 authmethod = LDAP_AUTH_SASL;
626                 version = LDAP_VERSION3;
627                 sasl_authz_id = ber_strdup( optarg );
628 #else
629                 fprintf( stderr, "%s: not compiled with SASL support\n",
630                         prog );
631                 return( EXIT_FAILURE );
632 #endif
633                 break;
634         case 'Z':
635 #ifdef HAVE_TLS
636                 if( version == LDAP_VERSION2 ) {
637                         fprintf( stderr, "%s: -Z incompatible with version %d\n",
638                                 prog, version );
639                         return EXIT_FAILURE;
640                 }
641                 version = LDAP_VERSION3;
642                 use_tls++;
643 #else
644                 fprintf( stderr, "%s: not compiled with TLS support\n",
645                         prog );
646                 return( EXIT_FAILURE );
647 #endif
648                 break;
649         default:
650                 fprintf( stderr, "%s: unrecognized option -%c\n",
651                         prog, optopt );
652             usage( prog );
653         }
654     }
655
656         if (version == -1) {
657                 version = LDAP_VERSION3;
658         }
659         if (authmethod == -1 && version > LDAP_VERSION2) {
660 #ifdef HAVE_CYRUS_SASL
661                 authmethod = LDAP_AUTH_SASL;
662 #else
663                 authmethod = LDAP_AUTH_SIMPLE;
664 #endif
665         }
666
667         if ( argc != optind )
668         usage( prog );
669
670     if ( rejfile != NULL ) {
671         if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
672             perror( rejfile );
673             return( EXIT_FAILURE );
674         }
675     } else {
676         rejfp = NULL;
677     }
678
679     if ( infile != NULL ) {
680         if (( fp = fopen( infile, "r" )) == NULL ) {
681             perror( infile );
682             return( EXIT_FAILURE );
683         }
684     } else {
685         fp = stdin;
686     }
687
688         if ( debug ) {
689                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
690                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
691                 }
692                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
693                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
694                 }
695                 ldif_debug = debug;
696         }
697
698 #ifdef SIGPIPE
699         (void) SIGNAL( SIGPIPE, SIG_IGN );
700 #endif
701
702     if ( !not ) {
703         if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
704                 if ( verbose ) {
705                         fprintf( stderr, "ldap_init( %s, %d )\n",
706                                 ldaphost != NULL ? ldaphost : "<DEFAULT>",
707                                 ldapport );
708                 }
709
710                 ld = ldap_init( ldaphost, ldapport );
711                 if( ld == NULL ) {
712                         perror("ldapmodify: ldap_init");
713                         return EXIT_FAILURE;
714                 }
715
716         } else {
717                 if ( verbose ) {
718                         fprintf( stderr, "ldap_initialize( %s )\n",
719                                 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
720                 }
721
722                 rc = ldap_initialize( &ld, ldapuri );
723                 if( rc != LDAP_SUCCESS ) {
724                         fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
725                                 rc, ldap_err2string(rc) );
726                         return EXIT_FAILURE;
727                 }
728         }
729
730         /* referrals */
731         if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
732                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
733         {
734                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
735                         referrals ? "on" : "off" );
736                 return EXIT_FAILURE;
737         }
738
739
740         if (version == -1 ) {
741                 version = LDAP_VERSION3;
742         }
743
744         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
745                 != LDAP_OPT_SUCCESS )
746         {
747                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
748                         version );
749                 return EXIT_FAILURE;
750         }
751
752         if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
753                 ldap_perror( ld, "ldap_start_tls" );
754                 if ( use_tls > 1 ) {
755                         return( EXIT_FAILURE );
756                 }
757         }
758
759         if ( pw_file || want_bindpw ) {
760                 if ( pw_file ) {
761                         rc = lutil_get_filed_password( pw_file, &passwd );
762                         if( rc ) return EXIT_FAILURE;
763                 } else {
764                         passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
765                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
766                 }
767         }
768
769         if ( authmethod == LDAP_AUTH_SASL ) {
770 #ifdef HAVE_CYRUS_SASL
771                 void *defaults;
772
773                 if( sasl_secprops != NULL ) {
774                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
775                                 (void *) sasl_secprops );
776                         
777                         if( rc != LDAP_OPT_SUCCESS ) {
778                                 fprintf( stderr,
779                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
780                                         sasl_secprops );
781                                 return( EXIT_FAILURE );
782                         }
783                 }
784                 
785                 defaults = lutil_sasl_defaults( ld,
786                         sasl_mech,
787                         sasl_realm,
788                         sasl_authc_id,
789                         passwd.bv_val,
790                         sasl_authz_id );
791
792                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
793                         sasl_mech, NULL, NULL,
794                         sasl_flags, lutil_sasl_interact, defaults );
795
796                 if( rc != LDAP_SUCCESS ) {
797                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
798                         return( EXIT_FAILURE );
799                 }
800 #else
801                 fprintf( stderr, "%s: not compiled with SASL support\n",
802                         prog );
803                 return( EXIT_FAILURE );
804 #endif
805         }
806         else {
807                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
808                                 != LDAP_SUCCESS ) {
809                         ldap_perror( ld, "ldap_bind" );
810                         return( EXIT_FAILURE );
811                 }
812
813         }
814
815     }
816
817     rc = 0;
818
819         if ( authzid || manageDSAit || noop ) {
820                 int err, crit=0, i=0;
821                 LDAPControl c[3];
822                 LDAPControl *ctrls[4];
823
824                 if ( authzid ) {
825                         c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
826                         c[i].ldctl_value.bv_val = authzid;
827                         c[i].ldctl_value.bv_len = strlen( authzid );
828                         c[i].ldctl_iscritical = 1;
829
830                         if( c[i].ldctl_iscritical ) crit++;
831                         ctrls[i] = &c[i];
832                         ctrls[++i] = NULL;
833                 }
834
835                 if ( manageDSAit ) {
836                         c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
837                         c[i].ldctl_value.bv_val = NULL;
838                         c[i].ldctl_value.bv_len = 0;
839                         c[i].ldctl_iscritical = manageDSAit > 1;
840
841                         if( c[i].ldctl_iscritical ) crit++;
842                         ctrls[i] = &c[i];
843                         ctrls[++i] = NULL;
844                 }
845
846                 if ( noop ) {
847                         c[i].ldctl_oid = LDAP_CONTROL_NOOP;
848                         c[i].ldctl_value.bv_val = NULL;
849                         c[i].ldctl_value.bv_len = 0;
850                         c[i].ldctl_iscritical = noop > 1;
851
852                         if( c[i].ldctl_iscritical ) crit++;
853                         ctrls[i] = &c[i];
854                         ctrls[++i] = NULL;
855                 }
856         
857                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
858
859                 if( err != LDAP_OPT_SUCCESS ) {
860                         fprintf( stderr, "Could not set %scontrols\n",
861                                 crit ? "critical " : "" );
862                         if ( crit ) {
863                                 return EXIT_FAILURE;
864                         }
865                 }
866         }
867
868         count = 0;
869         retval = 0;
870     while (( rc == 0 || contoper ) &&
871                 ( rbuf = read_one_record( fp )) != NULL ) {
872         count++;
873
874         start = rbuf;
875
876         if ( rejfp ) {
877                 len = strlen( rbuf );
878                 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
879                         perror( "malloc" );
880                         exit( EXIT_FAILURE );
881                 }
882                 memcpy( rejbuf, rbuf, len+1 );
883         }
884
885     rc = process_ldif_rec( start, count );
886
887         if ( rc )
888                 retval = rc;
889         if ( rc && rejfp ) {
890                 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
891
892                 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
893                 if ( matched_msg != NULL && *matched_msg != '\0' ) {
894                         fprintf( rejfp, ", matched DN: %s", matched_msg );
895                 }
896
897                 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
898                 if ( error_msg != NULL && *error_msg != '\0' ) {
899                         fprintf( rejfp, ", additional info: %s", error_msg );
900                 }
901                 fprintf( rejfp, "\n%s\n", rejbuf );
902         }
903                 if (rejfp) 
904                         free( rejbuf );
905                 free( rbuf );
906     }
907
908     if ( !not ) {
909                 ldap_unbind( ld );
910     }
911
912     if ( rejfp != NULL ) {
913             fclose( rejfp );
914     }
915
916     return( retval );
917 }
918
919
920 static int
921 process_ldif_rec( char *rbuf, int count )
922 {
923     char        *line, *dn, *type, *newrdn, *newsup, *p;
924     int         rc, linenum, modop, replicaport;
925     int         expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
926     int         expect_deleteoldrdn, deleteoldrdn;
927     int         saw_replica, use_record, new_entry, delete_entry, got_all;
928     LDAPMod     **pmods;
929         int version;
930         struct berval val;
931     LDAPControl **pctrls;
932
933     new_entry = ldapadd;
934
935     rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
936     expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
937         expect_sep = expect_ct = 0;
938     linenum = 0;
939         version = 0;
940     deleteoldrdn = 1;
941     use_record = force;
942     pmods = NULL;
943     pctrls = NULL;
944     dn = newrdn = newsup = NULL;
945
946     while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
947         ++linenum;
948
949         if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
950             expect_sep = 0;
951             expect_ct = 1;
952             continue;
953         }
954         
955         if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
956             fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
957                     prog, linenum, dn == NULL ? "" : dn );
958             rc = LDAP_PARAM_ERROR;
959             break;
960         }
961
962         if ( dn == NULL ) {
963             if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
964                 ++saw_replica;
965                 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
966                     replicaport = 0;
967                 } else {
968                     *p++ = '\0';
969                     replicaport = atoi( p );
970                 }
971                 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
972                         replicaport == ldapport ) {
973                     use_record = 1;
974                 }
975             } else if ( count == 1 && linenum == 1 && 
976                         strcasecmp( type, T_VERSION_STR ) == 0 )
977                 {
978                         if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
979                         fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
980                                 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
981                         }
982                         version++;
983
984             } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
985                 if (( dn = ber_strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
986                     perror( "strdup" );
987                     exit( EXIT_FAILURE );
988                 }
989                 expect_ct = 1;
990             }
991             goto end_line;      /* skip all lines until we see "dn:" */
992         }
993
994         if ( expect_ct ) {
995         
996         /* Check for "control" tag after dn and before changetype. */
997         if (strcasecmp(type, T_CONTROL_STR) == 0) {
998             /* Parse and add it to the list of controls */
999             rc = parse_ldif_control( line, &pctrls );
1000             if (rc != 0) {
1001                         fprintf( stderr, "%s: Error processing %s line, line %d: %s\n",
1002                                 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
1003             }
1004             goto end_line;
1005         }
1006         
1007             expect_ct = 0;
1008             if ( !use_record && saw_replica ) {
1009                 printf( "%s: skipping change record for entry: %s\n"
1010                         "\t(LDAP host/port does not match replica: lines)\n",
1011                         prog, dn );
1012                 free( dn );
1013                 ber_memfree( type );
1014                 ber_memfree( val.bv_val );
1015                 return( 0 );
1016             }
1017
1018             if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
1019 #ifdef LIBERAL_CHANGETYPE_MODOP
1020                 /* trim trailing spaces (and log warning ...) */
1021
1022                 int icnt;
1023                 for ( icnt = val.bv_len; --icnt > 0; ) {
1024                     if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
1025                         break;
1026                     }
1027                 }
1028
1029                 if ( ++icnt != val.bv_len ) {
1030                     fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
1031                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
1032                     val.bv_val[icnt] = '\0';
1033                 }
1034 #endif /* LIBERAL_CHANGETYPE_MODOP */
1035
1036                 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
1037                         new_entry = 0;
1038                         expect_modop = 1;
1039                 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
1040                         new_entry = 1;
1041                 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
1042                         || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
1043                         || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
1044                 {
1045                     expect_newrdn = 1;
1046                 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
1047                     got_all = delete_entry = 1;
1048                 } else {
1049                     fprintf( stderr,
1050                             "%s:  unknown %s \"%s\" (line %d of entry \"%s\")\n",
1051                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
1052                     rc = LDAP_PARAM_ERROR;
1053                 }
1054                 goto end_line;
1055             } else if ( ldapadd ) {             /*  missing changetype => add */
1056                 new_entry = 1;
1057                 modop = LDAP_MOD_ADD;
1058             } else {
1059                 expect_modop = 1;       /* missing changetype => modify */
1060             }
1061         }
1062
1063         if ( expect_modop ) {
1064 #ifdef LIBERAL_CHANGETYPE_MODOP
1065             /* trim trailing spaces (and log warning ...) */
1066             
1067             int icnt;
1068             for ( icnt = val.bv_len; --icnt > 0; ) {
1069                 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
1070                     break;
1071                 }
1072             }
1073             
1074             if ( ++icnt != val.bv_len ) {
1075                 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
1076                         prog, type, val.bv_val, linenum, dn );
1077                 val.bv_val[icnt] = '\0';
1078             }
1079 #endif /* LIBERAL_CHANGETYPE_MODOP */
1080
1081             expect_modop = 0;
1082             expect_sep = 1;
1083             if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
1084                 modop = LDAP_MOD_ADD;
1085                 goto end_line;
1086             } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
1087                 modop = LDAP_MOD_REPLACE;
1088                 addmodifyop( &pmods, modop, val.bv_val, NULL );
1089                 goto end_line;
1090             } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
1091                 modop = LDAP_MOD_DELETE;
1092                 addmodifyop( &pmods, modop, val.bv_val, NULL );
1093                 goto end_line;
1094             } else {    /* no modify op:  use default */
1095                 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
1096             }
1097         }
1098
1099         if ( expect_newrdn ) {
1100             if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
1101                         if (( newrdn = ber_strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
1102                     perror( "strdup" );
1103                     exit( EXIT_FAILURE );
1104                 }
1105                 expect_deleteoldrdn = 1;
1106                 expect_newrdn = 0;
1107             } else {
1108                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1109                         prog, T_NEWRDNSTR, type, linenum, dn );
1110                 rc = LDAP_PARAM_ERROR;
1111             }
1112         } else if ( expect_deleteoldrdn ) {
1113             if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
1114                 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
1115                 expect_deleteoldrdn = 0;
1116                 expect_newsup = 1;
1117                 got_all = 1;
1118             } else {
1119                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1120                         prog, T_DELETEOLDRDNSTR, type, linenum, dn );
1121                 rc = LDAP_PARAM_ERROR;
1122             }
1123         } else if ( expect_newsup ) {
1124             if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
1125                 if (( newsup = ber_strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
1126                     perror( "strdup" );
1127                     exit( EXIT_FAILURE );
1128                 }
1129                 expect_newsup = 0;
1130             } else {
1131                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1132                         prog, T_NEWSUPSTR, type, linenum, dn );
1133                 rc = LDAP_PARAM_ERROR;
1134             }
1135         } else if ( got_all ) {
1136             fprintf( stderr,
1137                     "%s: extra lines at end (line %d of entry \"%s\")\n",
1138                     prog, linenum, dn );
1139             rc = LDAP_PARAM_ERROR;
1140         } else {
1141                 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
1142         }
1143
1144 end_line:
1145         ber_memfree( type );
1146         ber_memfree( val.bv_val );
1147     }
1148
1149         if( linenum == 0 ) {
1150                 return 0;
1151         }
1152
1153         if( version && linenum == 1 ) {
1154                 return 0;
1155         }
1156
1157     /* If default controls are set (as with -M option) and controls are
1158        specified in the LDIF file, we must add the default controls to
1159        the list of controls sent with the ldap operation.
1160     */
1161     if ( rc == 0 ) {
1162         if (pctrls) {
1163             LDAPControl **defctrls = NULL;   /* Default server controls */
1164             LDAPControl **newctrls = NULL;
1165             ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
1166             if (defctrls) {
1167                 int npc=0;                  /* Number of LDIF controls */
1168                 int ndefc=0;                /* Number of default controls */
1169                 while (pctrls[npc])         /* Count LDIF controls */
1170                     npc++; 
1171                 while (defctrls[ndefc])     /* Count default controls */
1172                     ndefc++;
1173                 newctrls = ber_memrealloc(pctrls, (npc+ndefc+1)*sizeof(LDAPControl*));
1174                 if (newctrls == NULL)
1175                     rc = LDAP_NO_MEMORY;
1176                 else {
1177                     int i;
1178                     pctrls = newctrls;
1179                     for (i=npc; i<npc+ndefc; i++) {
1180                         pctrls[i] = ldap_control_dup(defctrls[i-npc]);
1181                         if (pctrls[i] == NULL) {
1182                             rc = LDAP_NO_MEMORY;
1183                             break;
1184                         }
1185                     }
1186                     pctrls[npc+ndefc] = NULL;
1187                     ldap_controls_free(defctrls);  /* Must be freed by library */
1188                 }
1189             }
1190         }
1191     }
1192
1193
1194     if ( rc == 0 ) {
1195         if ( delete_entry ) {
1196             rc = dodelete( dn, pctrls );
1197         } else if ( newrdn != NULL ) {
1198             rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
1199         } else {
1200             rc = domodify( dn, pmods, pctrls, new_entry );
1201         }
1202
1203         if ( rc == LDAP_SUCCESS ) {
1204             rc = 0;
1205         }
1206     }
1207
1208     if ( dn != NULL ) {
1209         free( dn );
1210     }
1211     if ( newrdn != NULL ) {
1212         free( newrdn );
1213     }
1214     if ( pmods != NULL ) {
1215         ldap_mods_free( pmods, 1 );
1216     }
1217
1218     if (pctrls != NULL) {
1219         ldap_controls_free( pctrls );
1220     }
1221
1222     return( rc );
1223 }
1224
1225 /* Parse an LDIF control line of the form
1226       control:  oid  [true/false]  [: value]              or
1227       control:  oid  [true/false]  [:: base64-value]      or
1228       control:  oid  [true/false]  [:< url]
1229    The control is added to the list of controls in *ppctrls.
1230 */      
1231 static int
1232 parse_ldif_control( char *line, 
1233                     LDAPControl ***ppctrls )
1234 {
1235     char *oid = NULL;
1236     int criticality = 0;   /* Default is false if not present */
1237     char *type=NULL;
1238     char *val = NULL;
1239     ber_len_t value_len = 0;
1240     int i, rc=0;
1241     char *s, *oidStart, *pcolon;
1242     LDAPControl *newctrl = NULL;
1243     LDAPControl **pctrls = NULL;
1244
1245     if (ppctrls) {
1246         pctrls = *ppctrls;
1247     }
1248     s = line + strlen(T_CONTROL_STR);  /* Skip over "control" */
1249     pcolon = s;                        /* Save this position for later */
1250     if (*s++ != ':')                   /* Make sure colon follows */
1251         return ( LDAP_PARAM_ERROR );
1252     while (*s && isspace(*s))  s++;    /* Skip white space before OID */
1253
1254     /* OID should come next. Validate and extract it. */
1255     if (*s == 0)
1256         return ( LDAP_PARAM_ERROR );
1257     oidStart = s;
1258     while (isdigit(*s) || *s == '.')  s++;    /* OID should be digits or . */
1259     if (s == oidStart) 
1260         return ( LDAP_PARAM_ERROR );   /* OID was not present */
1261     if (*s) {                          /* End of OID should be space or NULL */
1262         if (!isspace(*s))
1263             return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
1264         *s++ = 0;                    /* Replace space with null to terminate */
1265     }
1266
1267     
1268     oid = ber_strdup(oidStart);
1269     if (oid == NULL)
1270         return ( LDAP_NO_MEMORY );
1271
1272     /* Optional Criticality field is next. */
1273     while (*s && isspace(*s))  s++;   /* Skip white space before criticality */
1274     if (strncasecmp(s, "true", 4) == 0) {
1275         criticality = 1;
1276         s += 4;
1277     } 
1278     else if (strncasecmp(s, "false", 5) == 0) {
1279         criticality = 0;
1280         s += 5;
1281     }
1282
1283     /* Optional value field is next */
1284     while (*s && isspace(*s))  s++;    /* Skip white space before value */
1285     if (*s) {
1286         if (*s != ':') {           /* If value is present, must start with : */
1287             rc = LDAP_PARAM_ERROR;
1288             goto cleanup;
1289         }
1290
1291         /* Shift value down over OID and criticality so it's in the form
1292              control: value
1293              control:: base64-value
1294              control:< url
1295            Then we can use ldif_parse_line to extract and decode the value
1296         */
1297         while ( (*pcolon++ = *s++) != 0)     /* Shift value */
1298             ;
1299         rc = ldif_parse_line(line, &type, &val, &value_len);
1300         if (type)  ber_memfree(type);   /* Don't need this field*/
1301         if (rc < 0) {
1302             rc = LDAP_PARAM_ERROR;
1303             goto cleanup;
1304         }
1305     }
1306
1307     /* Create a new LDAPControl structure. */
1308     newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
1309     if ( newctrl == NULL ) {
1310         rc = LDAP_NO_MEMORY;
1311         goto cleanup;
1312     }
1313     newctrl->ldctl_oid = oid;
1314     oid = NULL;
1315     newctrl->ldctl_iscritical = criticality;
1316     newctrl->ldctl_value.bv_len = value_len;
1317     newctrl->ldctl_value.bv_val = val;
1318     val = NULL;
1319
1320     /* Add the new control to the passed-in list of controls. */
1321     i = 0;
1322     if (pctrls) {
1323         while ( pctrls[i] )      /* Count the # of controls passed in */
1324             i++;
1325     }
1326     /* Allocate 1 more slot for the new control and 1 for the NULL. */
1327     pctrls = (LDAPControl **)ber_memrealloc(pctrls, (i+2)*(sizeof(LDAPControl *)));
1328     if (pctrls == NULL) {
1329         rc = LDAP_NO_MEMORY;
1330         goto cleanup;
1331     }
1332     pctrls[i] = newctrl;
1333     newctrl = NULL;
1334     pctrls[i+1] = NULL;
1335     *ppctrls = pctrls;
1336
1337 cleanup:
1338     if (newctrl) {
1339         if (newctrl->ldctl_oid)
1340             ber_memfree(newctrl->ldctl_oid);
1341         if (newctrl->ldctl_value.bv_val)
1342             ber_memfree(newctrl->ldctl_value.bv_val);
1343         ber_memfree(newctrl);
1344     }
1345     if (val)
1346         ber_memfree(val);
1347     if (oid)
1348         ber_memfree(oid);
1349
1350     return( rc );
1351 }
1352
1353
1354 static void
1355 addmodifyop(
1356         LDAPMod ***pmodsp,
1357         int modop,
1358         const char *attr,
1359         struct berval *val )
1360 {
1361         LDAPMod         **pmods;
1362         int                     i, j;
1363
1364         pmods = *pmodsp;
1365         modop |= LDAP_MOD_BVALUES;
1366
1367         i = 0;
1368         if ( pmods != NULL ) {
1369                 for ( ; pmods[ i ] != NULL; ++i ) {
1370                         if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
1371                                 pmods[ i ]->mod_op == modop )
1372                         {
1373                                 break;
1374                         }
1375                 }
1376         }
1377
1378         if ( pmods == NULL || pmods[ i ] == NULL ) {
1379                 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
1380                         sizeof( LDAPMod * ))) == NULL )
1381                 {
1382                         perror( "realloc" );
1383                         exit( EXIT_FAILURE );
1384                 }
1385
1386                 *pmodsp = pmods;
1387                 pmods[ i + 1 ] = NULL;
1388
1389                 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
1390                 if ( pmods[ i ] == NULL ) {
1391                         perror( "calloc" );
1392                         exit( EXIT_FAILURE );
1393                 }
1394
1395                 pmods[ i ]->mod_op = modop;
1396                 pmods[ i ]->mod_type = ber_strdup( attr );
1397                 if ( pmods[ i ]->mod_type == NULL ) {
1398                         perror( "strdup" );
1399                         exit( EXIT_FAILURE );
1400                 }
1401         }
1402
1403         if ( val != NULL ) {
1404                 j = 0;
1405                 if ( pmods[ i ]->mod_bvalues != NULL ) {
1406                         for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1407                                 /* Empty */;
1408                         }
1409                 }
1410
1411                 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
1412                         pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1413                 if ( pmods[ i ]->mod_bvalues == NULL ) {
1414                         perror( "ber_realloc" );
1415                         exit( EXIT_FAILURE );
1416                 }
1417
1418                 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1419                 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1420                 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1421                         perror( "ber_bvdup" );
1422                         exit( EXIT_FAILURE );
1423                 }
1424         }
1425 }
1426
1427
1428 static int
1429 domodify(
1430         const char *dn,
1431         LDAPMod **pmods,
1432     LDAPControl **pctrls,
1433         int newentry )
1434 {
1435     int                 i, j, k, notascii, op;
1436     struct berval       *bvp;
1437
1438     if ( pmods == NULL ) {
1439         fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1440                 prog, dn );
1441         return( LDAP_PARAM_ERROR );
1442     } 
1443
1444     for ( i = 0; pmods[ i ] != NULL; ++i ) {
1445         op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1446         if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1447                 fprintf( stderr,
1448                         "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1449                         prog, pmods[i]->mod_type, dn );
1450                 return LDAP_PARAM_ERROR;
1451         }
1452     }
1453
1454     if ( verbose ) {
1455         for ( i = 0; pmods[ i ] != NULL; ++i ) {
1456             op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1457             printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1458                     "replace" : op == LDAP_MOD_ADD ?
1459                     "add" : "delete", pmods[ i ]->mod_type );
1460             if ( pmods[ i ]->mod_bvalues != NULL ) {
1461                 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1462                     bvp = pmods[ i ]->mod_bvalues[ j ];
1463                     notascii = 0;
1464                     for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1465                         if ( !isascii( bvp->bv_val[ k ] )) {
1466                             notascii = 1;
1467                             break;
1468                         }
1469                     }
1470                     if ( notascii ) {
1471                         printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1472                     } else {
1473                         printf( "\t%s\n", bvp->bv_val );
1474                     }
1475                 }
1476             }
1477         }
1478     }
1479
1480     if ( newentry ) {
1481         printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1482     } else {
1483         printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1484     }
1485
1486     if ( !not ) {
1487         if ( newentry ) {
1488             i = ldap_add_ext_s( ld, dn, pmods, pctrls, NULL );
1489         } else {
1490             i = ldap_modify_ext_s( ld, dn, pmods, pctrls, NULL );
1491         }
1492         if ( i != LDAP_SUCCESS ) {
1493                 /* print error message about failed update including DN */
1494                 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
1495                 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1496         } else if ( verbose ) {
1497             printf( "modify complete\n" );
1498         }
1499     } else {
1500         i = LDAP_SUCCESS;
1501     }
1502
1503     putchar( '\n' );
1504
1505     return( i );
1506 }
1507
1508
1509 static int
1510 dodelete(
1511         const char *dn,
1512     LDAPControl **pctrls )
1513 {
1514     int rc;
1515
1516     printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1517     if ( !not ) {
1518         if (( rc = ldap_delete_ext_s( ld, dn, pctrls, NULL )) != LDAP_SUCCESS ) {
1519                 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
1520                 ldap_perror( ld, "ldap_delete" );
1521         } else if ( verbose ) {
1522             printf( "delete complete" );
1523         }
1524     } else {
1525         rc = LDAP_SUCCESS;
1526     }
1527
1528     putchar( '\n' );
1529
1530     return( rc );
1531 }
1532
1533
1534 static int
1535 dorename(
1536         const char *dn,
1537         const char *newrdn,
1538         const char* newsup,
1539         int deleteoldrdn,
1540     LDAPControl **pctrls )
1541 {
1542     int rc;
1543
1544
1545     printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1546     if ( verbose ) {
1547         printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1548                 newrdn, deleteoldrdn ? "do not " : "" );
1549     }
1550     if ( !not ) {
1551         if (( rc = ldap_rename_s( ld, dn, newrdn, newsup, deleteoldrdn, pctrls, NULL ))
1552                 != LDAP_SUCCESS ) {
1553                 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
1554                 ldap_perror( ld, "ldap_modrdn" );
1555         } else {
1556             printf( "modrdn completed\n" );
1557         }
1558     } else {
1559         rc = LDAP_SUCCESS;
1560     }
1561
1562     putchar( '\n' );
1563
1564     return( rc );
1565 }
1566
1567
1568 static char *
1569 read_one_record( FILE *fp )
1570 {
1571     char        *buf, line[ LDAPMOD_MAXLINE ];
1572     int         lcur, lmax;
1573
1574     lcur = lmax = 0;
1575     buf = NULL;
1576
1577     while ( fgets( line, sizeof(line), fp ) != NULL ) {
1578         int len = strlen( line );
1579
1580                 if( len < 2 || ( len == 2 && *line == '\r' )) {
1581                         if( buf == NULL ) {
1582                                 continue;
1583                         } else {
1584                                 break;
1585                         }
1586                 }
1587
1588                 if ( lcur + len + 1 > lmax ) {
1589                         lmax = LDAPMOD_MAXLINE
1590                                 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1591
1592                         if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
1593                                 perror( "realloc" );
1594                                 exit( EXIT_FAILURE );
1595                         }
1596                 }
1597
1598                 strcpy( buf + lcur, line );
1599                 lcur += len;
1600     }
1601
1602     return( buf );
1603 }
1604
1605