1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1998-2021 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 significant 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 #ifdef _WIN32
44 # ifdef __WIN64__
45 # define ftello(fp) _ftelli64(fp)
46 # else
47 /* Ideally we would use _ftelli64 but that was only available
48 * starting in MSVCR80.DLL. The approach used here is inaccurate
49 * because returning the underlying file handle's file pointer
50 * doesn't take the stdio buffer offset into account. But, it
51 * works with all versions of MSVCRT.
52 */
53 # define ftello(fp) _telli64(fileno(fp))
54 # endif
55 #endif
56
57 extern int slap_DN_strict; /* dn.c */
58
59 static char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE ];
60
61 typedef struct Erec {
62 Entry *e;
63 unsigned long lineno;
64 unsigned long nextline;
65 } Erec;
66
67 typedef struct Trec {
68 Entry *e;
69 unsigned long lineno;
70 unsigned long nextline;
71 int rc;
72 int ready;
73 } Trec;
74
75 static Trec trec;
76 static unsigned long sid = SLAP_SYNC_SID_MAX + 1;
77 static int checkvals;
78 static int enable_meter;
79 static lutil_meter_t meter;
80 static const char *progname = "slapadd";
81 static OperationBuffer opbuf;
82 static char *buf;
83 static int lmax;
84
85 static ldap_pvt_thread_mutex_t add_mutex;
86 static ldap_pvt_thread_cond_t add_cond;
87 static int add_stop;
88
89 /* returns:
90 * 1: got a record
91 * 0: EOF
92 * -1: read failure
93 * -2: parse failure
94 */
95 static int
getrec0(Erec * erec)96 getrec0(Erec *erec)
97 {
98 const char *text;
99 int ldifrc;
100 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
101 size_t textlen = sizeof textbuf;
102 struct berval csn;
103 Operation *op = &opbuf.ob_op;
104 op->o_hdr = &opbuf.ob_hdr;
105
106 again:
107 erec->lineno = erec->nextline+1;
108 /* nextline is the line number of the end of the current entry */
109 ldifrc = ldif_read_record( ldiffp, &erec->nextline, &buf, &lmax );
110 if (ldifrc < 1)
111 return ldifrc < 0 ? -1 : 0;
112 {
113 BackendDB *bd;
114 Entry *e;
115 int prev_DN_strict;
116
117 if ( erec->lineno < jumpline )
118 goto again;
119
120 if ( !dbnum ) {
121 prev_DN_strict = slap_DN_strict;
122 slap_DN_strict = 0;
123 }
124 e = str2entry2( buf, checkvals );
125 if ( !dbnum ) {
126 slap_DN_strict = prev_DN_strict;
127 }
128
129 if ( enable_meter )
130 lutil_meter_update( &meter,
131 ftello( ldiffp->fp ),
132 0);
133
134 if( e == NULL ) {
135 fprintf( stderr, "%s: could not parse entry (line=%lu)\n",
136 progname, erec->lineno );
137 return -2;
138 }
139
140 /* make sure the DN is not empty */
141 if( BER_BVISEMPTY( &e->e_nname ) &&
142 !BER_BVISEMPTY( be->be_nsuffix ))
143 {
144 fprintf( stderr, "%s: line %lu: "
145 "cannot add entry with empty dn=\"%s\"",
146 progname, erec->lineno, e->e_dn );
147 bd = select_backend( &e->e_nname, nosubordinates );
148 if ( bd ) {
149 BackendDB *bdtmp;
150 int dbidx = 0;
151 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
152 if ( bdtmp == bd ) break;
153 dbidx++;
154 }
155
156 assert( bdtmp != NULL );
157
158 fprintf( stderr, "; did you mean to use database #%d (%s)?",
159 dbidx,
160 bd->be_suffix[0].bv_val );
161
162 }
163 fprintf( stderr, "\n" );
164 entry_free( e );
165 return -2;
166 }
167
168 /* check backend */
169 bd = select_backend( &e->e_nname, nosubordinates );
170 if ( bd != be ) {
171 fprintf( stderr, "%s: line %lu: "
172 "database #%d (%s) not configured to hold \"%s\"",
173 progname, erec->lineno,
174 dbnum,
175 be->be_suffix[0].bv_val,
176 e->e_dn );
177 if ( bd ) {
178 BackendDB *bdtmp;
179 int dbidx = 0;
180 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
181 if ( bdtmp == bd ) break;
182 dbidx++;
183 }
184
185 assert( bdtmp != NULL );
186
187 fprintf( stderr, "; did you mean to use database #%d (%s)?",
188 dbidx,
189 bd->be_suffix[0].bv_val );
190
191 } else {
192 fprintf( stderr, "; no database configured for that naming context" );
193 }
194 fprintf( stderr, "\n" );
195 entry_free( e );
196 return -2;
197 }
198
199 if ( slap_tool_entry_check( progname, op, e, erec->lineno, &text, textbuf, textlen ) !=
200 LDAP_SUCCESS ) {
201 entry_free( e );
202 return -2;
203 }
204
205 if ( SLAP_LASTMOD(be) ) {
206 time_t now = slap_get_time();
207 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
208 struct berval vals[ 2 ];
209
210 struct berval name, timestamp;
211
212 struct berval nvals[ 2 ];
213 struct berval nname;
214 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
215
216 enum {
217 GOT_NONE = 0x0,
218 GOT_CSN = 0x1,
219 GOT_UUID = 0x2,
220 GOT_ALL = (GOT_CSN|GOT_UUID)
221 } got = GOT_ALL;
222
223 vals[1].bv_len = 0;
224 vals[1].bv_val = NULL;
225
226 nvals[1].bv_len = 0;
227 nvals[1].bv_val = NULL;
228
229 csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
230 csn.bv_val = csnbuf;
231
232 timestamp.bv_val = timebuf;
233 timestamp.bv_len = sizeof(timebuf);
234
235 slap_timestamp( &now, ×tamp );
236
237 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
238 BER_BVSTR( &name, SLAPD_ANONYMOUS );
239 nname = name;
240 } else {
241 name = be->be_rootdn;
242 nname = be->be_rootndn;
243 }
244
245 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
246 == NULL )
247 {
248 got &= ~GOT_UUID;
249 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
250 vals[0].bv_val = uuidbuf;
251 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
252 }
253
254 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
255 == NULL )
256 {
257 vals[0] = name;
258 nvals[0] = nname;
259 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
260 }
261
262 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
263 == NULL )
264 {
265 vals[0] = timestamp;
266 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
267 }
268
269 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
270 == NULL )
271 {
272 got &= ~GOT_CSN;
273 vals[0] = csn;
274 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
275 }
276
277 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
278 == NULL )
279 {
280 vals[0] = name;
281 nvals[0] = nname;
282 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
283 }
284
285 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
286 == NULL )
287 {
288 vals[0] = timestamp;
289 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
290 }
291
292 if ( SLAP_SINGLE_SHADOW(be) && got != GOT_ALL ) {
293 Debug(LDAP_DEBUG_ANY,
294 "%s: warning, missing attrs %s%s%s from entry dn=\"%s\"\n",
295 progname,
296 (!(got & GOT_UUID) ? slap_schema.si_ad_entryUUID->ad_cname.bv_val : ""),
297 (!(got & GOT_CSN) ? "," : ""),
298 (!(got & GOT_CSN) ? slap_schema.si_ad_entryCSN->ad_cname.bv_val : ""),
299 e->e_name.bv_val );
300 }
301
302 sid = slap_tool_update_ctxcsn_check( progname, e );
303 }
304 erec->e = e;
305 }
306 return 1;
307 }
308
309 static void *
getrec_thr(void * ctx)310 getrec_thr(void *ctx)
311 {
312 ldap_pvt_thread_mutex_lock( &add_mutex );
313 while (!add_stop) {
314 trec.rc = getrec0((Erec *)&trec);
315 trec.ready = 1;
316 while (trec.ready)
317 ldap_pvt_thread_cond_wait( &add_cond, &add_mutex );
318 /* eof or read failure */
319 if ( trec.rc == 0 || trec.rc == -1 )
320 break;
321 }
322 ldap_pvt_thread_mutex_unlock( &add_mutex );
323 return NULL;
324 }
325
326 static int ldif_threaded;
327
328 static int
getrec(Erec * erec)329 getrec(Erec *erec)
330 {
331 int rc;
332 if ( !ldif_threaded )
333 return getrec0(erec);
334
335 while (!trec.ready)
336 ldap_pvt_thread_yield();
337 erec->e = trec.e;
338 erec->lineno = trec.lineno;
339 erec->nextline = trec.nextline;
340 trec.ready = 0;
341 rc = trec.rc;
342 ldap_pvt_thread_mutex_lock( &add_mutex );
343 ldap_pvt_thread_mutex_unlock( &add_mutex );
344 ldap_pvt_thread_cond_signal( &add_cond );
345 return rc;
346 }
347
348 int
slapadd(int argc,char ** argv)349 slapadd( int argc, char **argv )
350 {
351 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
352 size_t textlen = sizeof textbuf;
353 Erec erec;
354 struct berval bvtext;
355 ldap_pvt_thread_t thr;
356 ID id;
357 Entry *prev = NULL;
358
359 int ldifrc;
360 int rc = EXIT_SUCCESS;
361
362 struct stat stat_buf;
363
364 /* default "000" */
365 csnsid = 0;
366
367 if ( isatty (2) ) enable_meter = 1;
368 slap_tool_init( progname, SLAPADD, argc, argv );
369
370 if( !be->be_entry_open ||
371 !be->be_entry_close ||
372 !be->be_entry_put ||
373 (update_ctxcsn &&
374 (!be->be_dn2id_get ||
375 !be->be_entry_get ||
376 !be->be_entry_modify)) )
377 {
378 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
379 progname );
380 if ( dryrun ) {
381 fprintf( stderr, "\t(dry) continuing...\n" );
382
383 } else {
384 exit( EXIT_FAILURE );
385 }
386 }
387
388 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
389
390 /* do not check values in quick mode */
391 if ( slapMode & SLAP_TOOL_QUICK ) {
392 if ( slapMode & SLAP_TOOL_VALUE_CHECK ) {
393 fprintf( stderr, "%s: value-check incompatible with quick mode; disabled.\n", progname );
394 slapMode &= ~SLAP_TOOL_VALUE_CHECK;
395 }
396 }
397
398 /* enforce schema checking unless not disabled */
399 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
400 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
401 }
402
403 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
404 fprintf( stderr, "%s: could not open database.\n",
405 progname );
406 exit( EXIT_FAILURE );
407 }
408
409 (void)slap_tool_update_ctxcsn_init();
410
411 if ( enable_meter
412 #ifdef LDAP_DEBUG
413 /* tools default to "none" */
414 && slap_debug == LDAP_DEBUG_NONE
415 #endif
416 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
417 && S_ISREG(stat_buf.st_mode) ) {
418 enable_meter = !lutil_meter_open(
419 &meter,
420 &lutil_meter_text_display,
421 &lutil_meter_linear_estimator,
422 stat_buf.st_size);
423 } else {
424 enable_meter = 0;
425 }
426
427 if ( slap_tool_thread_max > 1 ) {
428 ldap_pvt_thread_mutex_init( &add_mutex );
429 ldap_pvt_thread_cond_init( &add_cond );
430 ldap_pvt_thread_create( &thr, 0, getrec_thr, NULL );
431 ldif_threaded = 1;
432 }
433
434 erec.nextline = 0;
435 erec.e = NULL;
436
437 for (;;) {
438 ldifrc = getrec( &erec );
439 if ( ldifrc < 1 ) {
440 if ( ldifrc == -2 && continuemode )
441 continue;
442 break;
443 }
444
445 if ( !dryrun ) {
446 /*
447 * Initialize text buffer
448 */
449 bvtext.bv_len = textlen;
450 bvtext.bv_val = textbuf;
451 bvtext.bv_val[0] = '\0';
452
453 id = be->be_entry_put( be, erec.e, &bvtext );
454 if( id == NOID ) {
455 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
456 "(line=%lu): %s\n", progname, erec.e->e_dn,
457 erec.lineno, bvtext.bv_val );
458 rc = EXIT_FAILURE;
459 if( continuemode ) {
460 if ( prev ) entry_free( prev );
461 prev = erec.e;
462 continue;
463 }
464 break;
465 }
466 if ( verbose )
467 fprintf( stderr, "added: \"%s\" (%08lx)\n",
468 erec.e->e_dn, (long) id );
469 } else {
470 if ( verbose )
471 fprintf( stderr, "added: \"%s\"\n",
472 erec.e->e_dn );
473 }
474
475 if ( prev ) entry_free( prev );
476 prev = erec.e;
477 }
478
479 if ( ldif_threaded ) {
480 ldap_pvt_thread_mutex_lock( &add_mutex );
481 add_stop = 1;
482 trec.ready = 0;
483 ldap_pvt_thread_cond_signal( &add_cond );
484 ldap_pvt_thread_mutex_unlock( &add_mutex );
485 ldap_pvt_thread_join( thr, NULL );
486 }
487 if ( erec.e ) entry_free( erec.e );
488
489 if ( ldifrc < 0 )
490 rc = EXIT_FAILURE;
491
492 bvtext.bv_len = textlen;
493 bvtext.bv_val = textbuf;
494 bvtext.bv_val[0] = '\0';
495
496 if ( enable_meter ) {
497 lutil_meter_update( &meter, ftello( ldiffp->fp ), 1);
498 lutil_meter_close( &meter );
499 }
500
501 if ( rc == EXIT_SUCCESS ) {
502 rc = slap_tool_update_ctxcsn( progname, sid, &bvtext );
503 }
504
505 ch_free( buf );
506
507 if ( !dryrun ) {
508 if ( enable_meter ) {
509 fprintf( stderr, "Closing DB..." );
510 }
511 if( be->be_entry_close( be ) ) {
512 rc = EXIT_FAILURE;
513 }
514
515 if( be->be_sync ) {
516 be->be_sync( be );
517 }
518 if ( enable_meter ) {
519 fprintf( stderr, "\n" );
520 }
521 }
522
523 if ( slap_tool_destroy())
524 rc = EXIT_FAILURE;
525
526 return rc;
527 }
528
529