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