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