]> git.sur5r.net Git - openldap/blob - servers/slapd/slapadd.c
d88e1219713d78daf1dbf5842e512c8672b2c98d
[openldap] / servers / slapd / slapadd.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2010 The OpenLDAP Foundation.
5  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
6  * Portions Copyright 2003 IBM Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Kurt Zeilenga for inclusion
19  * in OpenLDAP Software.  Additional signficant contributors include
20  *    Jong Hyuk Choi
21  *    Pierangelo Masarati
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/stdlib.h>
29
30 #include <ac/ctype.h>
31 #include <ac/string.h>
32 #include <ac/socket.h>
33 #include <ac/unistd.h>
34
35 #include <lber.h>
36 #include <ldif.h>
37 #include <lutil.h>
38 #include <lutil_meter.h>
39 #include <sys/stat.h>
40
41 #include "slapcommon.h"
42
43 static char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE ];
44 static char maxcsnbuf[ LDAP_PVT_CSNSTR_BUFSIZE * ( SLAP_SYNC_SID_MAX + 1 ) ];
45
46 int
47 slapadd( int argc, char **argv )
48 {
49         char *buf = NULL;
50         const char *text;
51         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
52         size_t textlen = sizeof textbuf;
53         const char *progname = "slapadd";
54
55         struct berval csn;
56         struct berval maxcsn[ SLAP_SYNC_SID_MAX + 1 ];
57         unsigned long sid;
58         struct berval bvtext;
59         Attribute *attr;
60         Entry *ctxcsn_e;
61         ID      ctxcsn_id, id;
62         OperationBuffer opbuf;
63         Operation *op;
64
65         int match;
66         int checkvals;
67         int lineno, nextline, ldifrc;
68         int lmax;
69         int rc = EXIT_SUCCESS;
70         int manage = 0; 
71
72         int enable_meter = 0;
73         lutil_meter_t meter;
74         struct stat stat_buf;
75
76         /* default "000" */
77         csnsid = 0;
78
79         if ( isatty (2) ) enable_meter = 1;
80         slap_tool_init( progname, SLAPADD, argc, argv );
81
82         memset( &opbuf, 0, sizeof(opbuf) );
83         op = &opbuf.ob_op;
84         op->o_hdr = &opbuf.ob_hdr;
85
86         if( !be->be_entry_open ||
87                 !be->be_entry_close ||
88                 !be->be_entry_put ||
89                 (update_ctxcsn &&
90                  (!be->be_dn2id_get ||
91                   !be->be_entry_get ||
92                   !be->be_entry_modify)) )
93         {
94                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
95                         progname );
96                 if ( dryrun ) {
97                         fprintf( stderr, "\t(dry) continuing...\n" );
98
99                 } else {
100                         exit( EXIT_FAILURE );
101                 }
102         }
103
104         checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
105
106         /* do not check values in quick mode */
107         if ( slapMode & SLAP_TOOL_QUICK ) {
108                 if ( slapMode & SLAP_TOOL_VALUE_CHECK ) {
109                         fprintf( stderr, "%s: value-check incompatible with quick mode; disabled.\n", progname );
110                         slapMode &= ~SLAP_TOOL_VALUE_CHECK;
111                 }
112         }
113
114         lmax = 0;
115         nextline = 0;
116
117         /* enforce schema checking unless not disabled */
118         if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
119                 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
120         }
121
122         if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
123                 fprintf( stderr, "%s: could not open database.\n",
124                         progname );
125                 exit( EXIT_FAILURE );
126         }
127
128         if ( update_ctxcsn ) {
129                 maxcsn[ 0 ].bv_val = maxcsnbuf;
130                 for ( sid = 1; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
131                         maxcsn[ sid ].bv_val = maxcsn[ sid - 1 ].bv_val + LDAP_PVT_CSNSTR_BUFSIZE;
132                         maxcsn[ sid ].bv_len = 0;
133                 }
134         }
135
136         if ( enable_meter 
137 #ifdef LDAP_DEBUG
138                 /* tools default to "none" */
139                 && slap_debug == LDAP_DEBUG_NONE
140 #endif
141                 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
142                 && S_ISREG(stat_buf.st_mode) ) {
143                 enable_meter = !lutil_meter_open(
144                         &meter,
145                         &lutil_meter_text_display,
146                         &lutil_meter_linear_estimator,
147                         stat_buf.st_size);
148         } else {
149                 enable_meter = 0;
150         }
151
152         /* nextline is the line number of the end of the current entry */
153         for( lineno=1; ( ldifrc = ldif_read_record( ldiffp, &nextline, &buf, &lmax )) > 0;
154                 lineno=nextline+1 )
155         {
156                 BackendDB *bd;
157                 Entry *e;
158
159                 if ( lineno < jumpline )
160                         continue;
161
162                 e = str2entry2( buf, checkvals );
163
164                 if ( enable_meter )
165                         lutil_meter_update( &meter,
166                                          ftell( ldiffp->fp ),
167                                          0);
168
169                 /*
170                  * Initialize text buffer
171                  */
172                 bvtext.bv_len = textlen;
173                 bvtext.bv_val = textbuf;
174                 bvtext.bv_val[0] = '\0';
175
176                 if( e == NULL ) {
177                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
178                                 progname, lineno );
179                         rc = EXIT_FAILURE;
180                         if( continuemode ) continue;
181                         break;
182                 }
183
184                 /* make sure the DN is not empty */
185                 if( BER_BVISEMPTY( &e->e_nname ) &&
186                         !BER_BVISEMPTY( be->be_nsuffix ))
187                 {
188                         fprintf( stderr, "%s: line %d: "
189                                 "cannot add entry with empty dn=\"%s\"",
190                                 progname, lineno, e->e_dn );
191                         bd = select_backend( &e->e_nname, nosubordinates );
192                         if ( bd ) {
193                                 BackendDB *bdtmp;
194                                 int dbidx = 0;
195                                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
196                                         if ( bdtmp == bd ) break;
197                                         dbidx++;
198                                 }
199
200                                 assert( bdtmp != NULL );
201                                 
202                                 fprintf( stderr, "; did you mean to use database #%d (%s)?",
203                                         dbidx,
204                                         bd->be_suffix[0].bv_val );
205
206                         }
207                         fprintf( stderr, "\n" );
208                         rc = EXIT_FAILURE;
209                         entry_free( e );
210                         if( continuemode ) continue;
211                         break;
212                 }
213
214                 /* check backend */
215                 bd = select_backend( &e->e_nname, nosubordinates );
216                 if ( bd != be ) {
217                         fprintf( stderr, "%s: line %d: "
218                                 "database #%d (%s) not configured to hold \"%s\"",
219                                 progname, lineno,
220                                 dbnum,
221                                 be->be_suffix[0].bv_val,
222                                 e->e_dn );
223                         if ( bd ) {
224                                 BackendDB *bdtmp;
225                                 int dbidx = 0;
226                                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
227                                         if ( bdtmp == bd ) break;
228                                         dbidx++;
229                                 }
230
231                                 assert( bdtmp != NULL );
232                                 
233                                 fprintf( stderr, "; did you mean to use database #%d (%s)?",
234                                         dbidx,
235                                         bd->be_suffix[0].bv_val );
236
237                         } else {
238                                 fprintf( stderr, "; no database configured for that naming context" );
239                         }
240                         fprintf( stderr, "\n" );
241                         rc = EXIT_FAILURE;
242                         entry_free( e );
243                         if( continuemode ) continue;
244                         break;
245                 }
246
247                 {
248                         Attribute *oc = attr_find( e->e_attrs,
249                                 slap_schema.si_ad_objectClass );
250
251                         if( oc == NULL ) {
252                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
253                                         progname, e->e_dn, lineno,
254                                         "no objectClass attribute");
255                                 rc = EXIT_FAILURE;
256                                 entry_free( e );
257                                 if( continuemode ) continue;
258                                 break;
259                         }
260
261                         /* check schema */
262                         op->o_bd = be;
263
264                         if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
265                                 rc = entry_schema_check( op, e, NULL, manage, 1, NULL,
266                                         &text, textbuf, textlen );
267
268                                 if( rc != LDAP_SUCCESS ) {
269                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
270                                                 progname, e->e_dn, lineno, rc, text );
271                                         rc = EXIT_FAILURE;
272                                         entry_free( e );
273                                         if( continuemode ) continue;
274                                         break;
275                                 }
276                                 textbuf[ 0 ] = '\0';
277                         }
278
279                         if ( (slapMode & SLAP_TOOL_VALUE_CHECK) != 0) {
280                                 Modifications *ml = NULL;
281
282                                 if ( slap_entry2mods( e, &ml, &text, textbuf, textlen )
283                                         != LDAP_SUCCESS )
284                                 {
285                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
286                                                 progname, e->e_dn, lineno, rc, text );
287                                         rc = EXIT_FAILURE;
288                                         entry_free( e );
289                                         if( continuemode ) continue;
290                                         break;
291                                 }
292                                 textbuf[ 0 ] = '\0';
293
294                                 rc = slap_mods_check( op, ml, &text, textbuf, textlen, NULL );
295                                 slap_mods_free( ml, 1 );
296                                 if ( rc != LDAP_SUCCESS ) {
297                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
298                                                 progname, e->e_dn, lineno, rc, text );
299                                         rc = EXIT_FAILURE;
300                                         entry_free( e );
301                                         if( continuemode ) continue;
302                                         break;
303                                 }
304                                 textbuf[ 0 ] = '\0';
305                         }
306                 }
307
308                 if ( SLAP_LASTMOD(be) ) {
309                         time_t now = slap_get_time();
310                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
311                         struct berval vals[ 2 ];
312
313                         struct berval name, timestamp;
314
315                         struct berval nvals[ 2 ];
316                         struct berval nname;
317                         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
318
319                         enum {
320                                 GOT_NONE = 0x0,
321                                 GOT_CSN = 0x1,
322                                 GOT_UUID = 0x2,
323                                 GOT_ALL = (GOT_CSN|GOT_UUID)
324                         } got = GOT_ALL;
325
326                         vals[1].bv_len = 0;
327                         vals[1].bv_val = NULL;
328
329                         nvals[1].bv_len = 0;
330                         nvals[1].bv_val = NULL;
331
332                         csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
333                         csn.bv_val = csnbuf;
334
335                         timestamp.bv_val = timebuf;
336                         timestamp.bv_len = sizeof(timebuf);
337
338                         slap_timestamp( &now, &timestamp );
339
340                         if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
341                                 BER_BVSTR( &name, SLAPD_ANONYMOUS );
342                                 nname = name;
343                         } else {
344                                 name = be->be_rootdn;
345                                 nname = be->be_rootndn;
346                         }
347
348                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
349                                 == NULL )
350                         {
351                                 got &= ~GOT_UUID;
352                                 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
353                                 vals[0].bv_val = uuidbuf;
354                                 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
355                         }
356
357                         if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
358                                 == NULL )
359                         {
360                                 vals[0] = name;
361                                 nvals[0] = nname;
362                                 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
363                         }
364
365                         if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
366                                 == NULL )
367                         {
368                                 vals[0] = timestamp;
369                                 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
370                         }
371
372                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
373                                 == NULL )
374                         {
375                                 got &= ~GOT_CSN;
376                                 vals[0] = csn;
377                                 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
378                         }
379
380                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
381                                 == NULL )
382                         {
383                                 vals[0] = name;
384                                 nvals[0] = nname;
385                                 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
386                         }
387
388                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
389                                 == NULL )
390                         {
391                                 vals[0] = timestamp;
392                                 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
393                         }
394
395                         if ( SLAP_SINGLE_SHADOW(be) && got != GOT_ALL ) {
396                                 char buf[SLAP_TEXT_BUFLEN];
397
398                                 snprintf( buf, sizeof(buf),
399                                         "%s%s%s",
400                                         ( !(got & GOT_UUID) ? slap_schema.si_ad_entryUUID->ad_cname.bv_val : "" ),
401                                         ( !(got & GOT_CSN) ? "," : "" ),
402                                         ( !(got & GOT_CSN) ? slap_schema.si_ad_entryCSN->ad_cname.bv_val : "" ) );
403
404                                 Debug( LDAP_DEBUG_ANY, "%s: warning, missing attrs %s from entry dn=\"%s\"\n",
405                                         progname, buf, e->e_name.bv_val );
406                         }
407
408                         if ( update_ctxcsn ) {
409                                 int rc_sid;
410
411                                 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
412                                 assert( attr != NULL );
413
414                                 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] );
415                                 if ( rc_sid < 0 ) {
416                                         Debug( LDAP_DEBUG_ANY, "%s: could not "
417                                                 "extract SID from entryCSN=%s, entry dn=\"%s\"\n",
418                                                 progname, attr->a_nvals[ 0 ].bv_val, e->e_name.bv_val );
419
420                                 } else {
421                                         assert( rc_sid <= SLAP_SYNC_SID_MAX );
422
423                                         sid = (unsigned)rc_sid;
424                                         if ( maxcsn[ sid ].bv_len != 0 ) {
425                                                 match = 0;
426                                                 value_match( &match, slap_schema.si_ad_entryCSN,
427                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
428                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
429                                                         &maxcsn[ sid ], &attr->a_nvals[0], &text );
430                                         } else {
431                                                 match = -1;
432                                         }
433                                         if ( match < 0 ) {
434                                                 strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val );
435                                                 maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len;
436                                         }
437                                 }
438                         }
439                 }
440
441                 if ( !dryrun ) {
442                         id = be->be_entry_put( be, e, &bvtext );
443                         if( id == NOID ) {
444                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
445                                                                  "(line=%d): %s\n", progname, e->e_dn,
446                                                                  lineno, bvtext.bv_val );
447                                 rc = EXIT_FAILURE;
448                                 entry_free( e );
449                                 if( continuemode ) continue;
450                                 break;
451                         }
452                         if ( verbose )
453                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
454                                         e->e_dn, (long) id );
455                 } else {
456                         if ( verbose )
457                                 fprintf( stderr, "added: \"%s\"\n",
458                                         e->e_dn );
459                 }
460
461                 entry_free( e );
462         }
463
464         if ( ldifrc < 0 )
465                 rc = EXIT_FAILURE;
466
467         bvtext.bv_len = textlen;
468         bvtext.bv_val = textbuf;
469         bvtext.bv_val[0] = '\0';
470
471         if ( enable_meter ) {
472                 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
473                 lutil_meter_close( &meter );
474         }
475
476         if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
477                 struct berval ctxdn;
478                 if ( SLAP_SYNC_SUBENTRY( be )) {
479                         build_new_dn( &ctxdn, &be->be_nsuffix[0],
480                                 (struct berval *)&slap_ldapsync_cn_bv, NULL );
481                 } else {
482                         ctxdn = be->be_nsuffix[0];
483                 }
484                 ctxcsn_id = be->be_dn2id_get( be, &ctxdn );
485                 if ( ctxcsn_id == NOID ) {
486                         if ( SLAP_SYNC_SUBENTRY( be )) {
487                                 ctxcsn_e = slap_create_context_csn_entry( be, NULL );
488                                 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
489                                         if ( maxcsn[ sid ].bv_len ) {
490                                                 attr_merge_one( ctxcsn_e, slap_schema.si_ad_contextCSN,
491                                                         &maxcsn[ sid ], NULL );
492                                         }
493                                 }
494                                 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
495                                 if ( ctxcsn_id == NOID ) {
496                                         fprintf( stderr, "%s: couldn't create context entry\n", progname );
497                                         rc = EXIT_FAILURE;
498                                 }
499                         } else {
500                                 fprintf( stderr, "%s: context entry is missing\n", progname );
501                                 rc = EXIT_FAILURE;
502                         }
503                 } else {
504                         ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
505                         if ( ctxcsn_e != NULL ) {
506                                 Entry *e = entry_dup( ctxcsn_e );
507                                 int change;
508                                 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
509                                 if ( attr ) {
510                                         int             i;
511
512                                         change = 0;
513
514                                         for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
515                                                 int rc_sid;
516
517                                                 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
518                                                 if ( rc_sid < 0 ) {
519                                                         Debug( LDAP_DEBUG_ANY,
520                                                                 "%s: unable to extract SID "
521                                                                 "from #%d contextCSN=%s\n",
522                                                                 progname, i,
523                                                                 attr->a_nvals[ i ].bv_val );
524                                                         continue;
525                                                 }
526
527                                                 assert( rc_sid <= SLAP_SYNC_SID_MAX );
528
529                                                 sid = (unsigned)rc_sid;
530
531                                                 if ( maxcsn[ sid ].bv_len == 0 ) {
532                                                         match = -1;
533
534                                                 } else {
535                                                         value_match( &match, slap_schema.si_ad_entryCSN,
536                                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
537                                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
538                                                                 &maxcsn[ sid ], &attr->a_nvals[i], &text );
539                                                 }
540
541                                                 if ( match > 0 ) {
542                                                         change = 1;
543                                                 } else {
544                                                         AC_MEMCPY( maxcsn[ sid ].bv_val,
545                                                                 attr->a_nvals[ i ].bv_val,
546                                                                 attr->a_nvals[ i ].bv_len );
547                                                         maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
548                                                         maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
549                                                 }
550                                         }
551
552                                         if ( change ) {
553                                                 if ( attr->a_nvals != attr->a_vals ) {
554                                                         ber_bvarray_free( attr->a_nvals );
555                                                 }
556                                                 attr->a_nvals = NULL;
557                                                 ber_bvarray_free( attr->a_vals );
558                                                 attr->a_vals = NULL;
559                                                 attr->a_numvals = 0;
560                                         }
561                                 } else {
562                                         change = 1;
563                                 }
564
565                                 if ( change ) {
566                                         for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
567                                                 if ( maxcsn[ sid ].bv_len ) {
568                                                         attr_merge_one( e, slap_schema.si_ad_contextCSN,
569                                                                 &maxcsn[ sid], NULL );
570                                                 }
571                                         }
572
573                                         ctxcsn_id = be->be_entry_modify( be, e, &bvtext );
574                                         if( ctxcsn_id == NOID ) {
575                                                 fprintf( stderr, "%s: could not modify ctxcsn\n",
576                                                         progname);
577                                                 rc = EXIT_FAILURE;
578                                         } else if ( verbose ) {
579                                                 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
580                                                         e->e_dn, (long) ctxcsn_id );
581                                         }
582                                 }
583                                 entry_free( e );
584                         }
585                 } 
586         }
587
588         ch_free( buf );
589
590         if ( !dryrun ) {
591                 if ( enable_meter ) {
592                         fprintf( stderr, "Closing DB..." );
593                 }
594                 if( be->be_entry_close( be ) ) {
595                         rc = EXIT_FAILURE;
596                 }
597
598                 if( be->be_sync ) {
599                         be->be_sync( be );
600                 }
601                 if ( enable_meter ) {
602                         fprintf( stderr, "\n" );
603                 }
604         }
605
606         if ( slap_tool_destroy())
607                 rc = EXIT_FAILURE;
608
609         return rc;
610 }
611