]> git.sur5r.net Git - openldap/blob - servers/slapd/slapadd.c
Happy New Year!
[openldap] / servers / slapd / slapadd.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2016 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 #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
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, &timestamp );
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                                 char buf[SLAP_TEXT_BUFLEN];
294
295                                 snprintf( buf, sizeof(buf),
296                                         "%s%s%s",
297                                         ( !(got & GOT_UUID) ? slap_schema.si_ad_entryUUID->ad_cname.bv_val : "" ),
298                                         ( !(got & GOT_CSN) ? "," : "" ),
299                                         ( !(got & GOT_CSN) ? slap_schema.si_ad_entryCSN->ad_cname.bv_val : "" ) );
300
301                                 Debug( LDAP_DEBUG_ANY, "%s: warning, missing attrs %s from entry dn=\"%s\"\n",
302                                         progname, buf, e->e_name.bv_val );
303                         }
304
305                         sid = slap_tool_update_ctxcsn_check( progname, e );
306                 }
307                 erec->e = e;
308         }
309         return 1;
310 }
311
312 static void *
313 getrec_thr(void *ctx)
314 {
315         ldap_pvt_thread_mutex_lock( &add_mutex );
316         while (!add_stop) {
317                 trec.rc = getrec0((Erec *)&trec);
318                 trec.ready = 1;
319                 while (trec.ready)
320                         ldap_pvt_thread_cond_wait( &add_cond, &add_mutex );
321                 /* eof or read failure */
322                 if ( trec.rc == 0 || trec.rc == -1 )
323                         break;
324         }
325         ldap_pvt_thread_mutex_unlock( &add_mutex );
326         return NULL;
327 }
328
329 static int ldif_threaded;
330
331 static int
332 getrec(Erec *erec)
333 {
334         int rc;
335         if ( !ldif_threaded )
336                 return getrec0(erec);
337
338         while (!trec.ready)
339                 ldap_pvt_thread_yield();
340         erec->e = trec.e;
341         erec->lineno = trec.lineno;
342         erec->nextline = trec.nextline;
343         trec.ready = 0;
344         rc = trec.rc;
345         ldap_pvt_thread_mutex_lock( &add_mutex );
346         ldap_pvt_thread_mutex_unlock( &add_mutex );
347         ldap_pvt_thread_cond_signal( &add_cond );
348         return rc;
349 }
350
351 int
352 slapadd( int argc, char **argv )
353 {
354         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
355         size_t textlen = sizeof textbuf;
356         Erec erec;
357         struct berval bvtext;
358         ldap_pvt_thread_t thr;
359         ID id;
360         Entry *prev = NULL;
361
362         int ldifrc;
363         int rc = EXIT_SUCCESS;
364
365         struct stat stat_buf;
366
367         /* default "000" */
368         csnsid = 0;
369
370         if ( isatty (2) ) enable_meter = 1;
371         slap_tool_init( progname, SLAPADD, argc, argv );
372
373         if( !be->be_entry_open ||
374                 !be->be_entry_close ||
375                 !be->be_entry_put ||
376                 (update_ctxcsn &&
377                  (!be->be_dn2id_get ||
378                   !be->be_entry_get ||
379                   !be->be_entry_modify)) )
380         {
381                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
382                         progname );
383                 if ( dryrun ) {
384                         fprintf( stderr, "\t(dry) continuing...\n" );
385
386                 } else {
387                         exit( EXIT_FAILURE );
388                 }
389         }
390
391         checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
392
393         /* do not check values in quick mode */
394         if ( slapMode & SLAP_TOOL_QUICK ) {
395                 if ( slapMode & SLAP_TOOL_VALUE_CHECK ) {
396                         fprintf( stderr, "%s: value-check incompatible with quick mode; disabled.\n", progname );
397                         slapMode &= ~SLAP_TOOL_VALUE_CHECK;
398                 }
399         }
400
401         /* enforce schema checking unless not disabled */
402         if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
403                 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
404         }
405
406         if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
407                 fprintf( stderr, "%s: could not open database.\n",
408                         progname );
409                 exit( EXIT_FAILURE );
410         }
411
412         (void)slap_tool_update_ctxcsn_init();
413
414         if ( enable_meter
415 #ifdef LDAP_DEBUG
416                 /* tools default to "none" */
417                 && slap_debug == LDAP_DEBUG_NONE
418 #endif
419                 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
420                 && S_ISREG(stat_buf.st_mode) ) {
421                 enable_meter = !lutil_meter_open(
422                         &meter,
423                         &lutil_meter_text_display,
424                         &lutil_meter_linear_estimator,
425                         stat_buf.st_size);
426         } else {
427                 enable_meter = 0;
428         }
429
430         if ( slap_tool_thread_max > 1 ) {
431                 ldap_pvt_thread_mutex_init( &add_mutex );
432                 ldap_pvt_thread_cond_init( &add_cond );
433                 ldap_pvt_thread_create( &thr, 0, getrec_thr, NULL );
434                 ldif_threaded = 1;
435         }
436
437         erec.nextline = 0;
438         erec.e = NULL;
439
440         for (;;) {
441                 ldifrc = getrec( &erec );
442                 if ( ldifrc < 1 ) {
443                         if ( ldifrc == -2 && continuemode )
444                                 continue;
445                         break;
446                 }
447
448                 if ( !dryrun ) {
449                         /*
450                          * Initialize text buffer
451                          */
452                         bvtext.bv_len = textlen;
453                         bvtext.bv_val = textbuf;
454                         bvtext.bv_val[0] = '\0';
455
456                         id = be->be_entry_put( be, erec.e, &bvtext );
457                         if( id == NOID ) {
458                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
459                                                                  "(line=%lu): %s\n", progname, erec.e->e_dn,
460                                                                  erec.lineno, bvtext.bv_val );
461                                 rc = EXIT_FAILURE;
462                                 if( continuemode ) {
463                                         if ( prev ) entry_free( prev );
464                                         prev = erec.e;
465                                         continue;
466                                 }
467                                 break;
468                         }
469                         if ( verbose )
470                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
471                                         erec.e->e_dn, (long) id );
472                 } else {
473                         if ( verbose )
474                                 fprintf( stderr, "added: \"%s\"\n",
475                                         erec.e->e_dn );
476                 }
477
478                 if ( prev ) entry_free( prev );
479                 prev = erec.e;
480         }
481
482         if ( ldif_threaded ) {
483                 ldap_pvt_thread_mutex_lock( &add_mutex );
484                 add_stop = 1;
485                 trec.ready = 0;
486                 ldap_pvt_thread_cond_signal( &add_cond );
487                 ldap_pvt_thread_mutex_unlock( &add_mutex );
488                 ldap_pvt_thread_join( thr, NULL );
489         }
490         if ( erec.e ) entry_free( erec.e );
491
492         if ( ldifrc < 0 )
493                 rc = EXIT_FAILURE;
494
495         bvtext.bv_len = textlen;
496         bvtext.bv_val = textbuf;
497         bvtext.bv_val[0] = '\0';
498
499         if ( enable_meter ) {
500                 lutil_meter_update( &meter, ftello( ldiffp->fp ), 1);
501                 lutil_meter_close( &meter );
502         }
503
504         if ( rc == EXIT_SUCCESS ) {
505                 rc = slap_tool_update_ctxcsn( progname, sid, &bvtext );
506         }
507
508         ch_free( buf );
509
510         if ( !dryrun ) {
511                 if ( enable_meter ) {
512                         fprintf( stderr, "Closing DB..." );
513                 }
514                 if( be->be_entry_close( be ) ) {
515                         rc = EXIT_FAILURE;
516                 }
517
518                 if( be->be_sync ) {
519                         be->be_sync( be );
520                 }
521                 if ( enable_meter ) {
522                         fprintf( stderr, "\n" );
523                 }
524         }
525
526         if ( slap_tool_destroy())
527                 rc = EXIT_FAILURE;
528
529         return rc;
530 }
531