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