]> git.sur5r.net Git - openldap/blob - servers/slapd/slapadd.c
better diagnostics
[openldap] / servers / slapd / slapadd.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2009 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_LUTIL_CSNSTR_BUFSIZE ];
44 static char maxcsnbuf[ LDAP_LUTIL_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;
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         lmax = 0;
107         nextline = 0;
108
109         /* enforce schema checking unless not disabled */
110         if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
111                 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
112         }
113
114         if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
115                 fprintf( stderr, "%s: could not open database.\n",
116                         progname );
117                 exit( EXIT_FAILURE );
118         }
119
120         if ( update_ctxcsn ) {
121                 maxcsn[ 0 ].bv_val = maxcsnbuf;
122                 for ( sid = 1; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
123                         maxcsn[ sid ].bv_val = maxcsn[ sid - 1 ].bv_val + LDAP_LUTIL_CSNSTR_BUFSIZE;
124                         maxcsn[ sid ].bv_len = 0;
125                 }
126         }
127
128         if ( enable_meter 
129 #ifdef LDAP_DEBUG
130                 /* tools default to "none" */
131                 && slap_debug == LDAP_DEBUG_NONE
132 #endif
133                 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
134                 && S_ISREG(stat_buf.st_mode) ) {
135                 enable_meter = !lutil_meter_open(
136                         &meter,
137                         &lutil_meter_text_display,
138                         &lutil_meter_linear_estimator,
139                         stat_buf.st_size);
140         } else {
141                 enable_meter = 0;
142         }
143
144         /* nextline is the line number of the end of the current entry */
145         for( lineno=1; ldif_read_record( ldiffp, &nextline, &buf, &lmax );
146                 lineno=nextline+1 )
147         {
148                 BackendDB *bd;
149                 Entry *e;
150
151                 if ( lineno < jumpline )
152                         continue;
153
154                 e = str2entry2( buf, checkvals );
155
156                 if ( enable_meter )
157                         lutil_meter_update( &meter,
158                                          ftell( ldiffp->fp ),
159                                          0);
160
161                 /*
162                  * Initialize text buffer
163                  */
164                 bvtext.bv_len = textlen;
165                 bvtext.bv_val = textbuf;
166                 bvtext.bv_val[0] = '\0';
167
168                 if( e == NULL ) {
169                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
170                                 progname, lineno );
171                         rc = EXIT_FAILURE;
172                         if( continuemode ) continue;
173                         break;
174                 }
175
176                 /* make sure the DN is not empty */
177                 if( BER_BVISEMPTY( &e->e_nname ) &&
178                         !BER_BVISEMPTY( be->be_nsuffix ))
179                 {
180                         fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
181                                 progname, e->e_dn, lineno );
182                         rc = EXIT_FAILURE;
183                         entry_free( e );
184                         if( continuemode ) continue;
185                         break;
186                 }
187
188                 /* check backend */
189                 bd = select_backend( &e->e_nname, nosubordinates );
190                 if ( bd != be ) {
191                         fprintf( stderr, "%s: line %d: "
192                                 "database #%d (%s) not configured to hold \"%s\"",
193                                 progname, lineno,
194                                 dbnum,
195                                 be->be_suffix[0].bv_val,
196                                 e->e_dn );
197                         if ( bd ) {
198                                 BackendDB *bdtmp;
199                                 int dbidx = 0;
200                                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
201                                         if ( bdtmp == bd ) break;
202                                         dbidx++;
203                                 }
204
205                                 assert( bdtmp != NULL );
206                                 
207                                 fprintf( stderr, "; did you mean to use database #%d (%s)?",
208                                         dbidx,
209                                         bd->be_suffix[0].bv_val );
210
211                         } else {
212                                 fprintf( stderr, "; no database configured for that naming context" );
213                         }
214                         fprintf( stderr, "\n" );
215                         rc = EXIT_FAILURE;
216                         entry_free( e );
217                         if( continuemode ) continue;
218                         break;
219                 }
220
221                 {
222                         Attribute *oc = attr_find( e->e_attrs,
223                                 slap_schema.si_ad_objectClass );
224
225                         if( oc == NULL ) {
226                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
227                                         progname, e->e_dn, lineno,
228                                         "no objectClass attribute");
229                                 rc = EXIT_FAILURE;
230                                 entry_free( e );
231                                 if( continuemode ) continue;
232                                 break;
233                         }
234
235                         /* check schema */
236                         op->o_bd = be;
237
238                         if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
239                                 rc = entry_schema_check( op, e, NULL, manage, 1, NULL,
240                                         &text, textbuf, textlen );
241
242                                 if( rc != LDAP_SUCCESS ) {
243                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
244                                                 progname, e->e_dn, lineno, rc, text );
245                                         rc = EXIT_FAILURE;
246                                         entry_free( e );
247                                         if( continuemode ) continue;
248                                         break;
249                                 }
250                                 textbuf[ 0 ] = '\0';
251                         }
252                 }
253
254                 if ( SLAP_LASTMOD(be) ) {
255                         time_t now = slap_get_time();
256                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
257                         struct berval vals[ 2 ];
258
259                         struct berval name, timestamp;
260
261                         struct berval nvals[ 2 ];
262                         struct berval nname;
263                         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
264
265                         vals[1].bv_len = 0;
266                         vals[1].bv_val = NULL;
267
268                         nvals[1].bv_len = 0;
269                         nvals[1].bv_val = NULL;
270
271                         csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
272                         csn.bv_val = csnbuf;
273
274                         timestamp.bv_val = timebuf;
275                         timestamp.bv_len = sizeof(timebuf);
276
277                         slap_timestamp( &now, &timestamp );
278
279                         if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
280                                 BER_BVSTR( &name, SLAPD_ANONYMOUS );
281                                 nname = name;
282                         } else {
283                                 name = be->be_rootdn;
284                                 nname = be->be_rootndn;
285                         }
286
287                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
288                                 == NULL )
289                         {
290                                 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
291                                 vals[0].bv_val = uuidbuf;
292                                 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
293                         }
294
295                         if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
296                                 == NULL )
297                         {
298                                 vals[0] = name;
299                                 nvals[0] = nname;
300                                 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
301                         }
302
303                         if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
304                                 == NULL )
305                         {
306                                 vals[0] = timestamp;
307                                 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
308                         }
309
310                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
311                                 == NULL )
312                         {
313                                 vals[0] = csn;
314                                 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
315                         }
316
317                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
318                                 == NULL )
319                         {
320                                 vals[0] = name;
321                                 nvals[0] = nname;
322                                 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
323                         }
324
325                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
326                                 == NULL )
327                         {
328                                 vals[0] = timestamp;
329                                 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
330                         }
331
332                         if ( update_ctxcsn ) {
333                                 int rc_sid;
334
335                                 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
336                                 assert( attr != NULL );
337
338                                 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] );
339                                 if ( rc_sid < 0 ) {
340                                         Debug( LDAP_DEBUG_ANY, "%s: could not "
341                                                 "extract SID from entryCSN=%s\n",
342                                                 progname, attr->a_nvals[ 0 ].bv_val, 0 );
343
344                                 } else {
345                                         assert( rc_sid <= SLAP_SYNC_SID_MAX );
346
347                                         sid = (unsigned)rc_sid;
348                                         if ( maxcsn[ sid ].bv_len != 0 ) {
349                                                 match = 0;
350                                                 value_match( &match, slap_schema.si_ad_entryCSN,
351                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
352                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
353                                                         &maxcsn[ sid ], &attr->a_nvals[0], &text );
354                                         } else {
355                                                 match = -1;
356                                         }
357                                         if ( match < 0 ) {
358                                                 strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val );
359                                                 maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len;
360                                         }
361                                 }
362                         }
363                 }
364
365                 if ( !dryrun ) {
366                         id = be->be_entry_put( be, e, &bvtext );
367                         if( id == NOID ) {
368                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
369                                                                  "(line=%d): %s\n", progname, e->e_dn,
370                                                                  lineno, bvtext.bv_val );
371                                 rc = EXIT_FAILURE;
372                                 entry_free( e );
373                                 if( continuemode ) continue;
374                                 break;
375                         }
376                         if ( verbose )
377                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
378                                         e->e_dn, (long) id );
379                 } else {
380                         if ( verbose )
381                                 fprintf( stderr, "added: \"%s\"\n",
382                                         e->e_dn );
383                 }
384
385                 entry_free( e );
386         }
387
388         bvtext.bv_len = textlen;
389         bvtext.bv_val = textbuf;
390         bvtext.bv_val[0] = '\0';
391
392         if ( enable_meter ) {
393                 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
394                 lutil_meter_close( &meter );
395         }
396
397         if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
398                 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
399                 if ( ctxcsn_id == NOID ) {
400                         fprintf( stderr, "%s: context entry is missing\n", progname );
401                         rc = EXIT_FAILURE;
402                 } else {
403                         ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
404                         if ( ctxcsn_e != NULL ) {
405                                 Entry *e = entry_dup( ctxcsn_e );
406                                 int change;
407                                 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
408                                 if ( attr ) {
409                                         int             i;
410
411                                         change = 0;
412
413                                         for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
414                                                 int rc_sid;
415
416                                                 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
417                                                 if ( rc_sid < 0 ) {
418                                                         Debug( LDAP_DEBUG_ANY,
419                                                                 "%s: unable to extract SID "
420                                                                 "from #%d contextCSN=%s\n",
421                                                                 progname, i,
422                                                                 attr->a_nvals[ i ].bv_val );
423                                                         continue;
424                                                 }
425
426                                                 assert( rc_sid <= SLAP_SYNC_SID_MAX );
427
428                                                 sid = (unsigned)rc_sid;
429
430                                                 if ( maxcsn[ sid ].bv_len == 0 ) {
431                                                         match = -1;
432
433                                                 } else {
434                                                         value_match( &match, slap_schema.si_ad_entryCSN,
435                                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
436                                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
437                                                                 &maxcsn[ sid ], &attr->a_nvals[i], &text );
438                                                 }
439
440                                                 if ( match > 0 ) {
441                                                         change = 1;
442                                                 } else {
443                                                         AC_MEMCPY( maxcsn[ sid ].bv_val,
444                                                                 attr->a_nvals[ i ].bv_val,
445                                                                 attr->a_nvals[ i ].bv_len );
446                                                         maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
447                                                         maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
448                                                 }
449                                         }
450
451                                         if ( change ) {
452                                                 if ( attr->a_nvals != attr->a_vals ) {
453                                                         ber_bvarray_free( attr->a_nvals );
454                                                 }
455                                                 attr->a_nvals = NULL;
456                                                 ber_bvarray_free( attr->a_vals );
457                                                 attr->a_vals = NULL;
458                                                 attr->a_numvals = 0;
459                                         }
460                                 } else {
461                                         change = 1;
462                                 }
463
464                                 if ( change ) {
465                                         for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
466                                                 if ( maxcsn[ sid ].bv_len ) {
467                                                         attr_merge_one( e, slap_schema.si_ad_contextCSN,
468                                                                 &maxcsn[ sid], NULL );
469                                                 }
470                                         }
471
472                                         ctxcsn_id = be->be_entry_modify( be, e, &bvtext );
473                                         if( ctxcsn_id == NOID ) {
474                                                 fprintf( stderr, "%s: could not modify ctxcsn\n",
475                                                         progname);
476                                                 rc = EXIT_FAILURE;
477                                         } else if ( verbose ) {
478                                                 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
479                                                         e->e_dn, (long) ctxcsn_id );
480                                         }
481                                 }
482                                 entry_free( e );
483                         }
484                 } 
485         }
486
487         ch_free( buf );
488
489         if ( !dryrun ) {
490                 if ( enable_meter ) {
491                         fprintf( stderr, "Closing DB..." );
492                 }
493                 if( be->be_entry_close( be ) ) {
494                         rc = EXIT_FAILURE;
495                 }
496
497                 if( be->be_sync ) {
498                         be->be_sync( be );
499                 }
500                 if ( enable_meter ) {
501                         fprintf( stderr, "\n" );
502                 }
503         }
504
505         if ( slap_tool_destroy())
506                 rc = EXIT_FAILURE;
507
508         return rc;
509 }
510