]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
Patch: 'ldapmodify -y file' reads password from file (ITS#2031)
[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
38 static char     *prog;
39 static char     *binddn = NULL;
40 static struct berval passwd = { 0, NULL };
41 static char *ldapuri = NULL;
42 static char     *ldaphost = NULL;
43 static int      ldapport = 0;
44 #ifdef HAVE_CYRUS_SASL
45 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
46 static char *sasl_realm = NULL;
47 static char     *sasl_authc_id = NULL;
48 static char     *sasl_authz_id = NULL;
49 static char     *sasl_mech = NULL;
50 static char     *sasl_secprops = NULL;
51 #endif
52 static int      use_tls = 0;
53 static int      ldapadd, not, verbose, contoper, force;
54 static LDAP     *ld = NULL;
55
56 #define LDAPMOD_MAXLINE         4096
57
58 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
59 #define T_VERSION_STR           "version"
60 #define T_REPLICA_STR           "replica"
61 #define T_DN_STR                "dn"
62 #define T_CHANGETYPESTR         "changetype"
63 #define T_ADDCTSTR              "add"
64 #define T_MODIFYCTSTR           "modify"
65 #define T_DELETECTSTR           "delete"
66 #define T_MODRDNCTSTR           "modrdn"
67 #define T_MODDNCTSTR            "moddn"
68 #define T_RENAMECTSTR           "rename"
69 #define T_MODOPADDSTR           "add"
70 #define T_MODOPREPLACESTR       "replace"
71 #define T_MODOPDELETESTR        "delete"
72 #define T_MODSEPSTR             "-"
73 #define T_NEWRDNSTR             "newrdn"
74 #define T_DELETEOLDRDNSTR       "deleteoldrdn"
75 #define T_NEWSUPSTR             "newsuperior"
76
77
78 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
79 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
80 static void addmodifyop LDAP_P((
81         LDAPMod ***pmodsp, int modop,
82         const char *attr,
83         struct berval *value ));
84 static int domodify LDAP_P((
85         const char *dn,
86         LDAPMod **pmods,
87         int newentry ));
88 static int dodelete LDAP_P((
89         const char *dn ));
90 static int dorename LDAP_P((
91         const char *dn,
92         const char *newrdn,
93         const char *newsup,
94         int deleteoldrdn ));
95 static char *read_one_record LDAP_P(( FILE *fp ));
96
97 static void
98 usage( const char *prog )
99 {
100     fprintf( stderr,
101 "Add or modify entries from an LDAP server\n\n"
102 "usage: %s [options]\n"
103 "       The list of desired operations are read from stdin or from the file\n"
104 "       specified by \"-f file\".\n"
105 "Add or modify options:\n"
106 "  -a         add values (default%s)\n"
107 "  -c         continuous operation mode (do not stop on errors)\n"
108 "  -f file    read operations from `file'\n"
109 "  -F         force all changes records to be used\n"
110 "  -S file    write skipped modifications to `file'\n"
111
112 "Common options:\n"
113 "  -d level   set LDAP debugging level to `level'\n"
114 "  -D binddn  bind DN\n"
115 "  -h host    LDAP server\n"
116 "  -H URI     LDAP Uniform Resource Indentifier(s)\n"
117 "  -I         use SASL Interactive mode\n"
118 "  -k         use Kerberos authentication\n"
119 "  -K         like -k, but do only step 1 of the Kerberos bind\n"
120 "  -M         enable Manage DSA IT control (-MM to make critical)\n"
121 "  -n         show what would be done but don't actually update\n"
122 "  -O props   SASL security properties\n"
123 "  -p port    port on LDAP server\n"
124 "  -P version procotol version (default: 3)\n"
125 "  -Q         use SASL Quiet mode\n"
126 "  -R realm   SASL realm\n"
127 "  -U authcid SASL authentication identity\n"
128 "  -v         run in verbose mode (diagnostics to standard output)\n"
129 "  -w passwd  bind passwd (for simple authentication)\n"
130 "  -W         prompt for bind passwd\n"
131 "  -x         Simple authentication\n"
132 "  -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
133 "  -y file    Read passwd from file\n"
134 "  -Y mech    SASL mechanism\n"
135 "  -Z         Start TLS request (-ZZ to require successful response)\n"
136              , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
137
138     exit( EXIT_FAILURE );
139 }
140
141
142 int
143 main( int argc, char **argv )
144 {
145     char                *infile, *rejfile, *rbuf, *start, *rejbuf = NULL;
146     FILE                *fp, *rejfp;
147         char            *matched_msg = NULL, *error_msg = NULL;
148         int             rc, i, authmethod, version, want_bindpw, debug, manageDSAit, referrals;
149         int count, len;
150         char    *pw_file = NULL;
151
152     prog = lutil_progname( "ldapmodify", argc, argv );
153
154     /* Print usage when no parameters */
155     if( argc < 2 ) usage( prog );
156
157         /* strncmp instead of strcmp since NT binaries carry .exe extension */
158     ldapadd = ( strncmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
159
160     infile = NULL;
161     rejfile = NULL;
162     not = verbose = want_bindpw = debug = manageDSAit = referrals = 0;
163     authmethod = -1;
164         version = -1;
165
166     while (( i = getopt( argc, argv, "acrf:F"
167                 "Cd:D:h:H:IkKMnO:p:P:QR:S:U:vw:WxX:y:Y:Z" )) != EOF )
168         {
169         switch( i ) {
170         /* Modify Options */
171         case 'a':       /* add */
172             ldapadd = 1;
173             break;
174         case 'c':       /* continuous operation */
175             contoper = 1;
176             break;
177         case 'f':       /* read from file */
178                 if( infile != NULL ) {
179                         fprintf( stderr, "%s: -f previously specified\n", prog );
180                         return EXIT_FAILURE;
181                 }
182             infile = strdup( optarg );
183             break;
184         case 'F':       /* force all changes records to be used */
185             force = 1;
186             break;
187
188         /* Common Options */
189         case 'C':
190                 referrals++;
191                 break;
192         case 'd':
193             debug |= atoi( optarg );
194             break;
195         case 'D':       /* bind DN */
196                 if( binddn != NULL ) {
197                         fprintf( stderr, "%s: -D previously specified\n", prog );
198                         return EXIT_FAILURE;
199                 }
200             binddn = strdup( optarg );
201             break;
202         case 'h':       /* ldap host */
203                 if( ldapuri != NULL ) {
204                         fprintf( stderr, "%s: -h incompatible with -H\n", prog );
205                         return EXIT_FAILURE;
206                 }
207                 if( ldaphost != NULL ) {
208                         fprintf( stderr, "%s: -h previously specified\n", prog );
209                         return EXIT_FAILURE;
210                 }
211             ldaphost = strdup( optarg );
212             break;
213         case 'H':       /* ldap URI */
214                 if( ldaphost != NULL ) {
215                         fprintf( stderr, "%s: -H incompatible with -h\n", prog );
216                         return EXIT_FAILURE;
217                 }
218                 if( ldapport ) {
219                         fprintf( stderr, "%s: -H incompatible with -p\n", prog );
220                         return EXIT_FAILURE;
221                 }
222                 if( ldapuri != NULL ) {
223                         fprintf( stderr, "%s: -H previously specified\n", prog );
224                         return EXIT_FAILURE;
225                 }
226             ldapuri = strdup( optarg );
227             break;
228         case 'I':
229 #ifdef HAVE_CYRUS_SASL
230                 if( version == LDAP_VERSION2 ) {
231                         fprintf( stderr, "%s: -I incompatible with version %d\n",
232                                 prog, version );
233                         return EXIT_FAILURE;
234                 }
235                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
236                         fprintf( stderr, "%s: incompatible previous "
237                                 "authentication choice\n",
238                                 prog );
239                         return EXIT_FAILURE;
240                 }
241                 authmethod = LDAP_AUTH_SASL;
242                 version = LDAP_VERSION3;
243                 sasl_flags = LDAP_SASL_INTERACTIVE;
244                 break;
245 #else
246                 fprintf( stderr, "%s: was not compiled with SASL support\n",
247                         prog );
248                 return( EXIT_FAILURE );
249 #endif
250         case 'k':       /* kerberos bind */
251 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
252                 if( version > LDAP_VERSION2 ) {
253                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
254                                 prog, version );
255                         return EXIT_FAILURE;
256                 }
257
258                 if( authmethod != -1 ) {
259                         fprintf( stderr, "%s: -k incompatible with previous "
260                                 "authentication choice\n", prog );
261                         return EXIT_FAILURE;
262                 }
263                         
264                 authmethod = LDAP_AUTH_KRBV4;
265 #else
266                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
267                 return EXIT_FAILURE;
268 #endif
269             break;
270         case 'K':       /* kerberos bind, part one only */
271 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
272                 if( version > LDAP_VERSION2 ) {
273                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
274                                 prog, version );
275                         return EXIT_FAILURE;
276                 }
277                 if( authmethod != -1 ) {
278                         fprintf( stderr, "%s: incompatible with previous "
279                                 "authentication choice\n", prog );
280                         return EXIT_FAILURE;
281                 }
282
283                 authmethod = LDAP_AUTH_KRBV41;
284 #else
285                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
286                 return( EXIT_FAILURE );
287 #endif
288             break;
289         case 'M':
290                 /* enable Manage DSA IT */
291                 if( version == LDAP_VERSION2 ) {
292                         fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
293                                 prog, version );
294                         return EXIT_FAILURE;
295                 }
296                 manageDSAit++;
297                 version = LDAP_VERSION3;
298                 break;
299         case 'n':       /* print deletes, don't actually do them */
300             ++not;
301             break;
302         case 'O':
303 #ifdef HAVE_CYRUS_SASL
304                 if( sasl_secprops != NULL ) {
305                         fprintf( stderr, "%s: -O previously specified\n", prog );
306                         return EXIT_FAILURE;
307                 }
308                 if( version == LDAP_VERSION2 ) {
309                         fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
310                                 prog, version );
311                         return EXIT_FAILURE;
312                 }
313                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
314                         fprintf( stderr, "%s: incompatible previous "
315                                 "authentication choice\n", prog );
316                         return EXIT_FAILURE;
317                 }
318                 authmethod = LDAP_AUTH_SASL;
319                 version = LDAP_VERSION3;
320                 sasl_secprops = strdup( optarg );
321 #else
322                 fprintf( stderr, "%s: not compiled with SASL support\n",
323                         prog );
324                 return( EXIT_FAILURE );
325 #endif
326                 break;
327         case 'p':
328                 if( ldapport ) {
329                         fprintf( stderr, "%s: -p previously specified\n", prog );
330                         return EXIT_FAILURE;
331                 }
332             ldapport = atoi( optarg );
333             break;
334         case 'P':
335                 switch( atoi(optarg) ) {
336                 case 2:
337                         if( version == LDAP_VERSION3 ) {
338                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
339                                         prog, version );
340                                 return EXIT_FAILURE;
341                         }
342                         version = LDAP_VERSION2;
343                         break;
344                 case 3:
345                         if( version == LDAP_VERSION2 ) {
346                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
347                                         prog, version );
348                                 return EXIT_FAILURE;
349                         }
350                         version = LDAP_VERSION3;
351                         break;
352                 default:
353                         fprintf( stderr, "%s: protocol version should be 2 or 3\n",
354                                 prog );
355                         usage( prog );
356                         return( EXIT_FAILURE );
357                 } break;
358         case 'Q':
359 #ifdef HAVE_CYRUS_SASL
360                 if( version == LDAP_VERSION2 ) {
361                         fprintf( stderr, "%s: -Q incompatible with version %d\n",
362                                 prog, version );
363                         return EXIT_FAILURE;
364                 }
365                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
366                         fprintf( stderr, "%s: incompatible previous "
367                                 "authentication choice\n",
368                                 prog );
369                         return EXIT_FAILURE;
370                 }
371                 authmethod = LDAP_AUTH_SASL;
372                 version = LDAP_VERSION3;
373                 sasl_flags = LDAP_SASL_QUIET;
374                 break;
375 #else
376                 fprintf( stderr, "%s: not compiled with SASL support\n",
377                         prog );
378                 return( EXIT_FAILURE );
379 #endif
380         case 'r':       /* replace (obsolete) */
381                 break;
382
383         case 'R':
384 #ifdef HAVE_CYRUS_SASL
385                 if( sasl_realm != NULL ) {
386                         fprintf( stderr, "%s: -R previously specified\n", prog );
387                         return EXIT_FAILURE;
388                 }
389                 if( version == LDAP_VERSION2 ) {
390                         fprintf( stderr, "%s: -R incompatible with version %d\n",
391                                 prog, version );
392                         return EXIT_FAILURE;
393                 }
394                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
395                         fprintf( stderr, "%s: incompatible previous "
396                                 "authentication choice\n",
397                                 prog );
398                         return EXIT_FAILURE;
399                 }
400                 authmethod = LDAP_AUTH_SASL;
401                 version = LDAP_VERSION3;
402                 sasl_realm = strdup( optarg );
403 #else
404                 fprintf( stderr, "%s: not compiled with SASL support\n",
405                         prog );
406                 return( EXIT_FAILURE );
407 #endif
408                 break;
409         case 'S':       /* skipped modifications to file */
410                 if( rejfile != NULL ) {
411                         fprintf( stderr, "%s: -S previously specified\n", prog );
412                         return EXIT_FAILURE;
413                 }
414                 rejfile = strdup( optarg );
415                 break;
416         case 'U':
417 #ifdef HAVE_CYRUS_SASL
418                 if( sasl_authc_id != NULL ) {
419                         fprintf( stderr, "%s: -U previously specified\n", prog );
420                         return EXIT_FAILURE;
421                 }
422                 if( version == LDAP_VERSION2 ) {
423                         fprintf( stderr, "%s: -U incompatible with version %d\n",
424                                 prog, version );
425                         return EXIT_FAILURE;
426                 }
427                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
428                         fprintf( stderr, "%s: incompatible previous "
429                                 "authentication choice\n",
430                                 prog );
431                         return EXIT_FAILURE;
432                 }
433                 authmethod = LDAP_AUTH_SASL;
434                 version = LDAP_VERSION3;
435                 sasl_authc_id = 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 'v':       /* verbose mode */
443             verbose++;
444             break;
445         case 'w':       /* password */
446             passwd.bv_val = strdup( optarg );
447                 {
448                         char* p;
449
450                         for( p = optarg; *p != '\0'; p++ ) {
451                                 *p = '\0';
452                         }
453                 }
454                 passwd.bv_len = strlen( passwd.bv_val );
455             break;
456         case 'W':
457                 want_bindpw++;
458                 break;
459         case 'y':
460                 pw_file = optarg;
461                 break;
462         case 'Y':
463 #ifdef HAVE_CYRUS_SASL
464                 if( sasl_mech != NULL ) {
465                         fprintf( stderr, "%s: -Y previously specified\n", prog );
466                         return EXIT_FAILURE;
467                 }
468                 if( version == LDAP_VERSION2 ) {
469                         fprintf( stderr, "%s: -Y incompatible with version %d\n",
470                                 prog, version );
471                         return EXIT_FAILURE;
472                 }
473                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
474                         fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
475                         return EXIT_FAILURE;
476                 }
477                 authmethod = LDAP_AUTH_SASL;
478                 version = LDAP_VERSION3;
479                 sasl_mech = strdup( optarg );
480 #else
481                 fprintf( stderr, "%s: not compiled with SASL support\n",
482                         prog );
483                 return( EXIT_FAILURE );
484 #endif
485                 break;
486         case 'x':
487                 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
488                         fprintf( stderr, "%s: incompatible with previous "
489                                 "authentication choice\n", prog );
490                         return EXIT_FAILURE;
491                 }
492                 authmethod = LDAP_AUTH_SIMPLE;
493                 break;
494         case 'X':
495 #ifdef HAVE_CYRUS_SASL
496                 if( sasl_authz_id != NULL ) {
497                         fprintf( stderr, "%s: -X previously specified\n", prog );
498                         return EXIT_FAILURE;
499                 }
500                 if( version == LDAP_VERSION2 ) {
501                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
502                                 prog, version );
503                         return EXIT_FAILURE;
504                 }
505                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
506                         fprintf( stderr, "%s: -X incompatible with "
507                                 "authentication choice\n", prog );
508                         return EXIT_FAILURE;
509                 }
510                 authmethod = LDAP_AUTH_SASL;
511                 version = LDAP_VERSION3;
512                 sasl_authz_id = strdup( optarg );
513 #else
514                 fprintf( stderr, "%s: not compiled with SASL support\n",
515                         prog );
516                 return( EXIT_FAILURE );
517 #endif
518                 break;
519         case 'Z':
520 #ifdef HAVE_TLS
521                 if( version == LDAP_VERSION2 ) {
522                         fprintf( stderr, "%s: -Z incompatible with version %d\n",
523                                 prog, version );
524                         return EXIT_FAILURE;
525                 }
526                 version = LDAP_VERSION3;
527                 use_tls++;
528 #else
529                 fprintf( stderr, "%s: not compiled with TLS support\n",
530                         prog );
531                 return( EXIT_FAILURE );
532 #endif
533                 break;
534         default:
535                 fprintf( stderr, "%s: unrecognized option -%c\n",
536                         prog, optopt );
537             usage( prog );
538         }
539     }
540
541         if (version == -1) {
542                 version = LDAP_VERSION3;
543         }
544         if (authmethod == -1 && version > LDAP_VERSION2) {
545 #ifdef HAVE_CYRUS_SASL
546                 authmethod = LDAP_AUTH_SASL;
547 #else
548                 authmethod = LDAP_AUTH_SIMPLE;
549 #endif
550         }
551
552         if ( argc != optind )
553         usage( prog );
554
555     if ( rejfile != NULL ) {
556         if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
557             perror( rejfile );
558             return( EXIT_FAILURE );
559         }
560     } else {
561         rejfp = NULL;
562     }
563
564     if ( infile != NULL ) {
565         if (( fp = fopen( infile, "r" )) == NULL ) {
566             perror( infile );
567             return( EXIT_FAILURE );
568         }
569     } else {
570         fp = stdin;
571     }
572
573         if ( debug ) {
574                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
575                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
576                 }
577                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
578                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
579                 }
580                 ldif_debug = debug;
581         }
582
583 #ifdef SIGPIPE
584         (void) SIGNAL( SIGPIPE, SIG_IGN );
585 #endif
586
587     if ( !not ) {
588         if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
589                 if ( verbose ) {
590                         fprintf( stderr, "ldap_init( %s, %d )\n",
591                                 ldaphost != NULL ? ldaphost : "<DEFAULT>",
592                                 ldapport );
593                 }
594
595                 ld = ldap_init( ldaphost, ldapport );
596                 if( ld == NULL ) {
597                         perror("ldapmodify: ldap_init");
598                         return EXIT_FAILURE;
599                 }
600
601         } else {
602                 if ( verbose ) {
603                         fprintf( stderr, "ldap_initialize( %s )\n",
604                                 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
605                 }
606
607                 rc = ldap_initialize( &ld, ldapuri );
608                 if( rc != LDAP_SUCCESS ) {
609                         fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
610                                 rc, ldap_err2string(rc) );
611                         return EXIT_FAILURE;
612                 }
613         }
614
615         /* referrals */
616         if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
617                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
618         {
619                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
620                         referrals ? "on" : "off" );
621                 return EXIT_FAILURE;
622         }
623
624
625         if (version == -1 ) {
626                 version = LDAP_VERSION3;
627         }
628
629         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
630                 != LDAP_OPT_SUCCESS )
631         {
632                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
633                         version );
634                 return EXIT_FAILURE;
635         }
636
637         if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
638                 ldap_perror( ld, "ldap_start_tls" );
639                 if ( use_tls > 1 ) {
640                         return( EXIT_FAILURE );
641                 }
642         }
643
644         if ( pw_file || want_bindpw ) {
645                 if ( pw_file ) {
646                         rc = lutil_get_filed_password( pw_file, &passwd );
647                         if( rc ) return EXIT_FAILURE;
648                 } else {
649                         passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
650                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
651                 }
652         }
653
654         if ( authmethod == LDAP_AUTH_SASL ) {
655 #ifdef HAVE_CYRUS_SASL
656                 void *defaults;
657
658                 if( sasl_secprops != NULL ) {
659                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
660                                 (void *) sasl_secprops );
661                         
662                         if( rc != LDAP_OPT_SUCCESS ) {
663                                 fprintf( stderr,
664                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
665                                         sasl_secprops );
666                                 return( EXIT_FAILURE );
667                         }
668                 }
669                 
670                 defaults = lutil_sasl_defaults( ld,
671                         sasl_mech,
672                         sasl_realm,
673                         sasl_authc_id,
674                         passwd.bv_val,
675                         sasl_authz_id );
676
677                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
678                         sasl_mech, NULL, NULL,
679                         sasl_flags, lutil_sasl_interact, defaults );
680
681                 if( rc != LDAP_SUCCESS ) {
682                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
683                         return( EXIT_FAILURE );
684                 }
685 #else
686                 fprintf( stderr, "%s: not compiled with SASL support\n",
687                         prog );
688                 return( EXIT_FAILURE );
689 #endif
690         }
691         else {
692                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
693                                 != LDAP_SUCCESS ) {
694                         ldap_perror( ld, "ldap_bind" );
695                         return( EXIT_FAILURE );
696                 }
697
698         }
699
700     }
701
702     rc = 0;
703
704         if ( manageDSAit ) {
705                 int err;
706                 LDAPControl c;
707                 LDAPControl *ctrls[2];
708                 ctrls[0] = &c;
709                 ctrls[1] = NULL;
710
711                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
712                 c.ldctl_value.bv_val = NULL;
713                 c.ldctl_value.bv_len = 0;
714                 c.ldctl_iscritical = manageDSAit > 1;
715
716                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
717
718                 if( err != LDAP_OPT_SUCCESS ) {
719                         fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
720                                 c.ldctl_iscritical ? "critical " : "" );
721                         if( c.ldctl_iscritical ) {
722                                 exit( EXIT_FAILURE );
723                         }
724                 }
725         }
726
727         count = 0;
728     while (( rc == 0 || contoper ) &&
729                 ( rbuf = read_one_record( fp )) != NULL ) {
730         count++;
731
732         start = rbuf;
733
734         if ( rejfp ) {
735                 len = strlen( rbuf );
736                 if (( rejbuf = (char *)malloc( len+1 )) == NULL ) {
737                         perror( "realloc" );
738                         exit( EXIT_FAILURE );
739                 }
740                 memcpy( rejbuf, rbuf, len+1 );
741         }
742
743     rc = process_ldif_rec( start, count );
744
745         if ( rc && rejfp ) {
746                 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
747
748                 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
749                 if ( matched_msg != NULL && *matched_msg != '\0' ) {
750                         fprintf( rejfp, ", matched DN: %s", matched_msg );
751                 }
752
753                 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
754                 if ( error_msg != NULL && *error_msg != '\0' ) {
755                         fprintf( rejfp, ", additional info: %s", error_msg );
756                 }
757                 fprintf( rejfp, "\n%s\n", rejbuf );
758         }
759                 if (rejfp) 
760                         free( rejbuf );
761                 free( rbuf );
762     }
763
764     if ( !not ) {
765                 ldap_unbind( ld );
766     }
767
768     if ( rejfp != NULL ) {
769             fclose( rejfp );
770     }
771
772         return( rc );
773 }
774
775
776 static int
777 process_ldif_rec( char *rbuf, int count )
778 {
779     char        *line, *dn, *type, *newrdn, *newsup, *p;
780     int         rc, linenum, modop, replicaport;
781     int         expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
782     int         expect_deleteoldrdn, deleteoldrdn;
783     int         saw_replica, use_record, new_entry, delete_entry, got_all;
784     LDAPMod     **pmods;
785         int version;
786         struct berval val;
787
788     new_entry = ldapadd;
789
790     rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
791     expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
792         expect_sep = expect_ct = 0;
793     linenum = 0;
794         version = 0;
795     deleteoldrdn = 1;
796     use_record = force;
797     pmods = NULL;
798     dn = newrdn = newsup = NULL;
799
800     while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
801         ++linenum;
802
803         if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
804             expect_sep = 0;
805             expect_ct = 1;
806             continue;
807         }
808         
809         if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
810             fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
811                     prog, linenum, dn == NULL ? "" : dn );
812             rc = LDAP_PARAM_ERROR;
813             break;
814         }
815
816         if ( dn == NULL ) {
817             if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
818                 ++saw_replica;
819                 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
820                     replicaport = 0;
821                 } else {
822                     *p++ = '\0';
823                     replicaport = atoi( p );
824                 }
825                 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
826                         replicaport == ldapport ) {
827                     use_record = 1;
828                 }
829             } else if ( count == 1 && linenum == 1 && 
830                         strcasecmp( type, T_VERSION_STR ) == 0 )
831                 {
832                         if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
833                         fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
834                                 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
835                         }
836                         version++;
837
838             } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
839                 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
840                     perror( "strdup" );
841                     exit( EXIT_FAILURE );
842                 }
843                 expect_ct = 1;
844             }
845             goto end_line;      /* skip all lines until we see "dn:" */
846         }
847
848         if ( expect_ct ) {
849             expect_ct = 0;
850             if ( !use_record && saw_replica ) {
851                 printf( "%s: skipping change record for entry: %s\n"
852                         "\t(LDAP host/port does not match replica: lines)\n",
853                         prog, dn );
854                 free( dn );
855                 ber_memfree( type );
856                 ber_memfree( val.bv_val );
857                 return( 0 );
858             }
859
860             if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
861 #ifdef LIBERAL_CHANGETYPE_MODOP
862                 /* trim trailing spaces (and log warning ...) */
863
864                 int icnt;
865                 for ( icnt = val.bv_len; --icnt > 0; ) {
866                     if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
867                         break;
868                     }
869                 }
870
871                 if ( ++icnt != val.bv_len ) {
872                     fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
873                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
874                     val.bv_val[icnt] = '\0';
875                 }
876 #endif /* LIBERAL_CHANGETYPE_MODOP */
877
878                 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
879                         new_entry = 0;
880                         expect_modop = 1;
881                 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
882                         new_entry = 1;
883                 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
884                         || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
885                         || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
886                 {
887                     expect_newrdn = 1;
888                 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
889                     got_all = delete_entry = 1;
890                 } else {
891                     fprintf( stderr,
892                             "%s:  unknown %s \"%s\" (line %d of entry \"%s\")\n",
893                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
894                     rc = LDAP_PARAM_ERROR;
895                 }
896                 goto end_line;
897             } else if ( ldapadd ) {             /*  missing changetype => add */
898                 new_entry = 1;
899                 modop = LDAP_MOD_ADD;
900             } else {
901                 expect_modop = 1;       /* missing changetype => modify */
902             }
903         }
904
905         if ( expect_modop ) {
906 #ifdef LIBERAL_CHANGETYPE_MODOP
907             /* trim trailing spaces (and log warning ...) */
908             
909             int icnt;
910             for ( icnt = val.bv_len; --icnt > 0; ) {
911                 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
912                     break;
913                 }
914             }
915             
916             if ( ++icnt != val.bv_len ) {
917                 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
918                         prog, type, val.bv_val, linenum, dn );
919                 val.bv_val[icnt] = '\0';
920             }
921 #endif /* LIBERAL_CHANGETYPE_MODOP */
922
923             expect_modop = 0;
924             expect_sep = 1;
925             if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
926                 modop = LDAP_MOD_ADD;
927                 goto end_line;
928             } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
929                 modop = LDAP_MOD_REPLACE;
930                 addmodifyop( &pmods, modop, val.bv_val, NULL );
931                 goto end_line;
932             } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
933                 modop = LDAP_MOD_DELETE;
934                 addmodifyop( &pmods, modop, val.bv_val, NULL );
935                 goto end_line;
936             } else {    /* no modify op:  use default */
937                 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
938             }
939         }
940
941         if ( expect_newrdn ) {
942             if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
943                         if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
944                     perror( "strdup" );
945                     exit( EXIT_FAILURE );
946                 }
947                 expect_deleteoldrdn = 1;
948                 expect_newrdn = 0;
949             } else {
950                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
951                         prog, T_NEWRDNSTR, type, linenum, dn );
952                 rc = LDAP_PARAM_ERROR;
953             }
954         } else if ( expect_deleteoldrdn ) {
955             if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
956                 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
957                 expect_deleteoldrdn = 0;
958                 expect_newsup = 1;
959                 got_all = 1;
960             } else {
961                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
962                         prog, T_DELETEOLDRDNSTR, type, linenum, dn );
963                 rc = LDAP_PARAM_ERROR;
964             }
965         } else if ( expect_newsup ) {
966             if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
967                 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
968                     perror( "strdup" );
969                     exit( EXIT_FAILURE );
970                 }
971                 expect_newsup = 0;
972             } else {
973                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
974                         prog, T_NEWSUPSTR, type, linenum, dn );
975                 rc = LDAP_PARAM_ERROR;
976             }
977         } else if ( got_all ) {
978             fprintf( stderr,
979                     "%s: extra lines at end (line %d of entry \"%s\")\n",
980                     prog, linenum, dn );
981             rc = LDAP_PARAM_ERROR;
982         } else {
983                 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
984         }
985
986 end_line:
987         ber_memfree( type );
988         ber_memfree( val.bv_val );
989     }
990
991         if( linenum == 0 ) {
992                 return 0;
993         }
994
995         if( version && linenum == 1 ) {
996                 return 0;
997         }
998
999     if ( rc == 0 ) {
1000         if ( delete_entry ) {
1001             rc = dodelete( dn );
1002         } else if ( newrdn != NULL ) {
1003             rc = dorename( dn, newrdn, newsup, deleteoldrdn );
1004         } else {
1005             rc = domodify( dn, pmods, new_entry );
1006         }
1007
1008         if ( rc == LDAP_SUCCESS ) {
1009             rc = 0;
1010         }
1011     }
1012
1013     if ( dn != NULL ) {
1014         free( dn );
1015     }
1016     if ( newrdn != NULL ) {
1017         free( newrdn );
1018     }
1019     if ( pmods != NULL ) {
1020         ldap_mods_free( pmods, 1 );
1021     }
1022
1023     return( rc );
1024 }
1025
1026
1027 static void
1028 addmodifyop(
1029         LDAPMod ***pmodsp,
1030         int modop,
1031         const char *attr,
1032         struct berval *val )
1033 {
1034         LDAPMod         **pmods;
1035         int                     i, j;
1036
1037         pmods = *pmodsp;
1038         modop |= LDAP_MOD_BVALUES;
1039
1040         i = 0;
1041         if ( pmods != NULL ) {
1042                 for ( ; pmods[ i ] != NULL; ++i ) {
1043                         if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
1044                                 pmods[ i ]->mod_op == modop )
1045                         {
1046                                 break;
1047                         }
1048                 }
1049         }
1050
1051         if ( pmods == NULL || pmods[ i ] == NULL ) {
1052                 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
1053                         sizeof( LDAPMod * ))) == NULL )
1054                 {
1055                         perror( "realloc" );
1056                         exit( EXIT_FAILURE );
1057                 }
1058
1059                 *pmodsp = pmods;
1060                 pmods[ i + 1 ] = NULL;
1061
1062                 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
1063                 if ( pmods[ i ] == NULL ) {
1064                         perror( "calloc" );
1065                         exit( EXIT_FAILURE );
1066                 }
1067
1068                 pmods[ i ]->mod_op = modop;
1069                 pmods[ i ]->mod_type = ber_strdup( attr );
1070                 if ( pmods[ i ]->mod_type == NULL ) {
1071                         perror( "strdup" );
1072                         exit( EXIT_FAILURE );
1073                 }
1074         }
1075
1076         if ( val != NULL ) {
1077                 j = 0;
1078                 if ( pmods[ i ]->mod_bvalues != NULL ) {
1079                         for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1080                                 /* Empty */;
1081                         }
1082                 }
1083
1084                 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
1085                         pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1086                 if ( pmods[ i ]->mod_bvalues == NULL ) {
1087                         perror( "ber_realloc" );
1088                         exit( EXIT_FAILURE );
1089                 }
1090
1091                 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1092                 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1093                 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1094                         perror( "ber_bvdup" );
1095                         exit( EXIT_FAILURE );
1096                 }
1097         }
1098 }
1099
1100
1101 static int
1102 domodify(
1103         const char *dn,
1104         LDAPMod **pmods,
1105         int newentry )
1106 {
1107     int                 i, j, k, notascii, op;
1108     struct berval       *bvp;
1109
1110     if ( pmods == NULL ) {
1111         fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1112                 prog, dn );
1113         return( LDAP_PARAM_ERROR );
1114     } 
1115
1116     for ( i = 0; pmods[ i ] != NULL; ++i ) {
1117         op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1118         if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1119                 fprintf( stderr,
1120                         "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1121                         prog, pmods[i]->mod_type, dn );
1122                 return LDAP_PARAM_ERROR;
1123         }
1124     }
1125
1126     if ( verbose ) {
1127         for ( i = 0; pmods[ i ] != NULL; ++i ) {
1128             op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1129             printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1130                     "replace" : op == LDAP_MOD_ADD ?
1131                     "add" : "delete", pmods[ i ]->mod_type );
1132             if ( pmods[ i ]->mod_bvalues != NULL ) {
1133                 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1134                     bvp = pmods[ i ]->mod_bvalues[ j ];
1135                     notascii = 0;
1136                     for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1137                         if ( !isascii( bvp->bv_val[ k ] )) {
1138                             notascii = 1;
1139                             break;
1140                         }
1141                     }
1142                     if ( notascii ) {
1143                         printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1144                     } else {
1145                         printf( "\t%s\n", bvp->bv_val );
1146                     }
1147                 }
1148             }
1149         }
1150     }
1151
1152     if ( newentry ) {
1153         printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1154     } else {
1155         printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1156     }
1157
1158     if ( !not ) {
1159         if ( newentry ) {
1160             i = ldap_add_s( ld, dn, pmods );
1161         } else {
1162             i = ldap_modify_s( ld, dn, pmods );
1163         }
1164         if ( i != LDAP_SUCCESS ) {
1165                 /* print error message about failed update including DN */
1166                 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
1167                 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1168         } else if ( verbose ) {
1169             printf( "modify complete\n" );
1170         }
1171     } else {
1172         i = LDAP_SUCCESS;
1173     }
1174
1175     putchar( '\n' );
1176
1177     return( i );
1178 }
1179
1180
1181 static int
1182 dodelete(
1183         const char *dn )
1184 {
1185     int rc;
1186
1187     printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1188     if ( !not ) {
1189         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1190                 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
1191                 ldap_perror( ld, "ldap_delete" );
1192         } else if ( verbose ) {
1193             printf( "delete complete" );
1194         }
1195     } else {
1196         rc = LDAP_SUCCESS;
1197     }
1198
1199     putchar( '\n' );
1200
1201     return( rc );
1202 }
1203
1204
1205 static int
1206 dorename(
1207         const char *dn,
1208         const char *newrdn,
1209         const char* newsup,
1210         int deleteoldrdn )
1211 {
1212     int rc;
1213
1214
1215     printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1216     if ( verbose ) {
1217         printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1218                 newrdn, deleteoldrdn ? "do not " : "" );
1219     }
1220     if ( !not ) {
1221         if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1222                 != LDAP_SUCCESS )
1223         {
1224                 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
1225                 ldap_perror( ld, "ldap_modrdn" );
1226         } else {
1227             printf( "modrdn completed\n" );
1228         }
1229     } else {
1230         rc = LDAP_SUCCESS;
1231     }
1232
1233     putchar( '\n' );
1234
1235     return( rc );
1236 }
1237
1238
1239 static char *
1240 read_one_record( FILE *fp )
1241 {
1242     char        *buf, line[ LDAPMOD_MAXLINE ];
1243     int         lcur, lmax;
1244
1245     lcur = lmax = 0;
1246     buf = NULL;
1247
1248     while ( fgets( line, sizeof(line), fp ) != NULL ) {
1249         int len = strlen( line );
1250
1251                 if( len < 2 || ( len == 2 && *line == '\r' )) {
1252                         if( buf == NULL ) {
1253                                 continue;
1254                         } else {
1255                                 break;
1256                         }
1257                 }
1258
1259                 if ( lcur + len + 1 > lmax ) {
1260                         lmax = LDAPMOD_MAXLINE
1261                                 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1262
1263                         if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1264                                 perror( "realloc" );
1265                                 exit( EXIT_FAILURE );
1266                         }
1267                 }
1268
1269                 strcpy( buf + lcur, line );
1270                 lcur += len;
1271     }
1272
1273     return( buf );
1274 }