]> git.sur5r.net Git - openldap/blob - servers/slapd/slapadd.c
cleanup previous commit
[openldap] / servers / slapd / slapadd.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 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
39 #include "slapcommon.h"
40
41 static char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
42 static char maxcsnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
43 static const char *progname = "slapadd";
44
45 static ldap_pvt_thread_cond_t put_cond1;
46 static ldap_pvt_thread_cond_t put_cond2;
47 static ldap_pvt_thread_mutex_t put_mutex1;
48 static ldap_pvt_thread_mutex_t put_mutex2;
49 static Entry *put_e;
50 static struct berval bvtext;
51 static int put_lineno;
52 static int put_rc;
53
54 static int use_thread = 0;      /*FIXME need a new switch for this */
55
56 static void *do_put(void *ptr)
57 {
58         ID id;
59         Entry *e;
60         int lineno;
61
62         ldap_pvt_thread_mutex_lock( &put_mutex1 );
63         do {
64                 ldap_pvt_thread_cond_wait( &put_cond1, &put_mutex1 );
65                 if ( put_rc ) {
66                         break;
67                 }
68                 ldap_pvt_thread_mutex_lock( &put_mutex2 );
69                 ldap_pvt_thread_cond_signal( &put_cond2 );
70                 ldap_pvt_thread_mutex_unlock( &put_mutex2 );
71
72                 e = put_e;
73                 lineno = put_lineno;
74
75                 if ( !dryrun ) {
76                         id = be->be_entry_put( be, e, &bvtext );
77                         if( id == NOID ) {
78                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
79                                                                  "(line=%d): %s\n", progname, e->e_dn,
80                                                                  lineno, bvtext.bv_val );
81                                 entry_free( e );
82                                 if ( continuemode ) continue;
83                                 put_rc = EXIT_FAILURE;
84                                 break;
85                         }
86                 }
87
88                 if ( verbose ) {
89                         if ( dryrun ) {
90                                 fprintf( stderr, "added: \"%s\"\n",
91                                         e->e_dn );
92                         } else {
93                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
94                                         e->e_dn, (long) id );
95                         }
96                 }
97
98                 entry_free( e );
99
100         } while (1);
101         ldap_pvt_thread_mutex_unlock( &put_mutex1 );
102 }
103
104 int
105 slapadd( int argc, char **argv )
106 {
107         char            *buf = NULL;
108         int         lineno;
109         int         lmax;
110         int                     rc = EXIT_SUCCESS;
111
112         const char *text;
113         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
114         size_t textlen = sizeof textbuf;
115
116         struct berval csn;
117         struct berval maxcsn;
118         int match;
119         Attribute *attr;
120         Entry *ctxcsn_e;
121         ID      ctxcsn_id, id;
122         int ret;
123         int i, checkvals;
124         struct berval mc;
125         ldap_pvt_thread_t put_tid;
126
127         slap_tool_init( progname, SLAPADD, argc, argv );
128
129         if( !be->be_entry_open ||
130                 !be->be_entry_close ||
131                 !be->be_entry_put )
132         {
133                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
134                         progname );
135                 if ( dryrun ) {
136                         fprintf( stderr, "\t(dry) continuing...\n" );
137
138                 } else {
139                         exit( EXIT_FAILURE );
140                 }
141         }
142
143         checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
144
145         if ( use_thread ) {
146                 ldap_pvt_thread_initialize();
147                 ldap_pvt_thread_cond_init( &put_cond1 );
148                 ldap_pvt_thread_cond_init( &put_cond2 );
149                 ldap_pvt_thread_mutex_init( &put_mutex1 );
150                 ldap_pvt_thread_mutex_init( &put_mutex2 );
151                 rc = ldap_pvt_thread_create( &put_tid, 0, do_put, NULL );
152                 if ( rc ) {
153                         fprintf( stderr, "%s: could not create thread.\n",
154                                 progname );
155                         exit( EXIT_FAILURE );
156                 }
157                 ldap_pvt_thread_mutex_lock( &put_mutex2 );
158         }
159
160         lmax = 0;
161         lineno = 0;
162
163         if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
164                 fprintf( stderr, "%s: could not open database.\n",
165                         progname );
166                 exit( EXIT_FAILURE );
167         }
168
169         if ( update_ctxcsn ) {
170                 maxcsn.bv_val = maxcsnbuf;
171                 maxcsn.bv_len = 0;
172         }
173
174         while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
175                 Entry *e = str2entry2( buf, checkvals );
176
177                 /*
178                  * Initialize text buffer
179                  */
180                 bvtext.bv_len = textlen;
181                 bvtext.bv_val = textbuf;
182                 bvtext.bv_val[0] = '\0';
183
184                 if( e == NULL ) {
185                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
186                                 progname, lineno );
187                         rc = EXIT_FAILURE;
188                         if( continuemode ) continue;
189                         break;
190                 }
191
192                 /* make sure the DN is not empty */
193                 if( !e->e_nname.bv_len ) {
194                         fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
195                                 progname, e->e_dn, lineno );
196                         rc = EXIT_FAILURE;
197                         entry_free( e );
198                         if( continuemode ) continue;
199                         break;
200                 }
201
202                 /* check backend */
203                 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
204                         != be )
205                 {
206                         fprintf( stderr, "%s: line %d: "
207                                 "database (%s) not configured to hold \"%s\"\n",
208                                 progname, lineno,
209                                 be ? be->be_suffix[0].bv_val : "<none>",
210                                 e->e_dn );
211                         fprintf( stderr, "%s: line %d: "
212                                 "database (%s) not configured to hold \"%s\"\n",
213                                 progname, lineno,
214                                 be ? be->be_nsuffix[0].bv_val : "<none>",
215                                 e->e_ndn );
216                         rc = EXIT_FAILURE;
217                         entry_free( e );
218                         if( continuemode ) continue;
219                         break;
220                 }
221
222                 if( global_schemacheck ) {
223                         Attribute *sc = attr_find( e->e_attrs,
224                                 slap_schema.si_ad_structuralObjectClass );
225                         Attribute *oc = attr_find( e->e_attrs,
226                                 slap_schema.si_ad_objectClass );
227
228                         if( oc == NULL ) {
229                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
230                                         progname, e->e_dn, lineno,
231                                         "no objectClass attribute");
232                                 rc = EXIT_FAILURE;
233                                 entry_free( e );
234                                 if( continuemode ) continue;
235                                 break;
236                         }
237
238                         if( sc == NULL ) {
239                                 struct berval vals[2];
240
241                                 rc = structural_class( oc->a_vals, vals,
242                                         NULL, &text, textbuf, textlen );
243
244                                 if( rc != LDAP_SUCCESS ) {
245                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
246                                                 progname, e->e_dn, lineno, rc, text );
247                                         rc = EXIT_FAILURE;
248                                         entry_free( e );
249                                         if( continuemode ) continue;
250                                         break;
251                                 }
252
253                                 vals[1].bv_len = 0;
254                                 vals[1].bv_val = NULL;
255
256                                 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
257                                         vals, NULL /* FIXME */ );
258                         }
259
260                         /* check schema */
261                         rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
262
263                         if( rc != LDAP_SUCCESS ) {
264                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
265                                         progname, e->e_dn, lineno, rc, text );
266                                 rc = EXIT_FAILURE;
267                                 entry_free( e );
268                                 if( continuemode ) continue;
269                                 break;
270                         }
271                 }
272
273                 if ( SLAP_LASTMOD(be) ) {
274                         struct tm *ltm;
275                         time_t now = slap_get_time();
276                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
277                         struct berval vals[ 2 ];
278
279                         struct berval name, timestamp;
280
281                         struct berval nvals[ 2 ];
282                         struct berval nname;
283                         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
284
285                         vals[1].bv_len = 0;
286                         vals[1].bv_val = NULL;
287
288                         nvals[1].bv_len = 0;
289                         nvals[1].bv_val = NULL;
290
291                         ltm = gmtime(&now);
292                         lutil_gentime( timebuf, sizeof(timebuf), ltm );
293
294                         csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
295                         csn.bv_val = csnbuf;
296
297                         timestamp.bv_val = timebuf;
298                         timestamp.bv_len = strlen(timebuf);
299
300                         if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
301                                 BER_BVSTR( &name, SLAPD_ANONYMOUS );
302                                 nname = name;
303                         } else {
304                                 name = be->be_rootdn;
305                                 nname = be->be_rootndn;
306                         }
307
308                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
309                                 == NULL )
310                         {
311                                 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
312                                 vals[0].bv_val = uuidbuf;
313                                 attr_merge_normalize_one( e,
314                                                         slap_schema.si_ad_entryUUID, vals, NULL );
315                         }
316
317                         if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
318                                 == NULL )
319                         {
320                                 vals[0] = name;
321                                 nvals[0] = nname;
322                                 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
323                         }
324
325                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
326                                 == NULL )
327                         {
328                                 vals[0] = name;
329                                 nvals[0] = nname;
330                                 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
331                         }
332
333                         if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
334                                 == NULL )
335                         {
336                                 vals[0] = timestamp;
337                                 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
338                         }
339
340                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
341                                 == NULL )
342                         {
343                                 vals[0] = timestamp;
344                                 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
345                         }
346
347                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
348                                 == NULL )
349                         {
350                                 vals[0] = csn;
351                                 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
352                         }
353
354                         if ( update_ctxcsn ) {
355                                 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
356                                 if ( maxcsn.bv_len != 0 ) {
357                                         match = 0;
358                                         value_match( &match, slap_schema.si_ad_entryCSN,
359                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
360                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
361                                                 &maxcsn, &attr->a_nvals[0], &text );
362                                 } else {
363                                         match = -1;
364                                 }
365                                 if ( match < 0 ) {
366                                         strcpy( maxcsn.bv_val, attr->a_nvals[0].bv_val );
367                                         maxcsn.bv_len = attr->a_nvals[0].bv_len;
368                                 }
369                         }
370                 }
371
372                 if ( use_thread ) {
373                         ldap_pvt_thread_mutex_lock( &put_mutex1 );
374                         if (put_rc) {
375                                 rc = put_rc;
376                                 ldap_pvt_thread_mutex_unlock( &put_mutex1 );
377                                 break;
378                         }
379                         put_e = e;
380                         put_lineno = lineno;
381                         ldap_pvt_thread_cond_signal( &put_cond1 );
382                         ldap_pvt_thread_mutex_unlock( &put_mutex1 );
383                         /* Make sure writer wakes up */
384                         ldap_pvt_thread_cond_wait( &put_cond2, &put_mutex2 );
385                         continue;
386                 }
387
388                 if ( !dryrun ) {
389                         id = be->be_entry_put( be, e, &bvtext );
390                         if( id == NOID ) {
391                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
392                                                                  "(line=%d): %s\n", progname, e->e_dn,
393                                                                  lineno, bvtext.bv_val );
394                                 rc = EXIT_FAILURE;
395                                 entry_free( e );
396                                 if( continuemode ) continue;
397                                 break;
398                         }
399                 }
400
401                 if ( verbose ) {
402                         if ( dryrun ) {
403                                 fprintf( stderr, "added: \"%s\"\n",
404                                         e->e_dn );
405                         } else {
406                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
407                                         e->e_dn, (long) id );
408                         }
409                 }
410
411                 entry_free( e );
412         }
413
414         if ( use_thread ) {
415                 ldap_pvt_thread_mutex_unlock( &put_mutex2 );
416                 ldap_pvt_thread_mutex_lock( &put_mutex1 );
417                 /* Tell child thread to stop if it hasn't aborted */
418                 if ( !put_rc ) {
419                         put_rc = EXIT_FAILURE;
420                         ldap_pvt_thread_cond_signal( &put_cond1 );
421                 }
422                 ldap_pvt_thread_mutex_unlock( &put_mutex1 );
423                 ldap_pvt_thread_join( put_tid, NULL );
424                 ldap_pvt_thread_mutex_destroy( &put_mutex2 );
425                 ldap_pvt_thread_mutex_destroy( &put_mutex1 );
426                 ldap_pvt_thread_cond_destroy( &put_cond2 );
427                 ldap_pvt_thread_cond_destroy( &put_cond1 );
428         }
429
430         bvtext.bv_len = textlen;
431         bvtext.bv_val = textbuf;
432         bvtext.bv_val[0] = '\0';
433
434         if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && maxcsn.bv_len ) {
435                 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
436                 if ( ctxcsn_id == NOID ) {
437                         fprintf( stderr, "%s: context entry is missing\n", progname );
438                         rc = EXIT_FAILURE;
439                 } else {
440                         ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
441                         if ( ret == LDAP_SUCCESS ) {
442                                 attr = attr_find( ctxcsn_e->e_attrs,
443                                                                         slap_schema.si_ad_contextCSN );
444                                 value_match( &match, slap_schema.si_ad_entryCSN,
445                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
446                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
447                                         &maxcsn, &attr->a_nvals[0], &text );
448                                 if ( match > 0 ) {
449                                         AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
450                                         attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
451                                         attr->a_vals[0].bv_len = maxcsn.bv_len;
452                                 
453                                         ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
454                                         if( ctxcsn_id == NOID ) {
455                                                 fprintf( stderr, "%s: could not modify ctxcsn\n",
456                                                                                 progname);
457                                                 rc = EXIT_FAILURE;
458                                         } else if ( verbose ) {
459                                                 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
460                                                                                  ctxcsn_e->e_dn, (long) ctxcsn_id );
461                                         }
462                                 }
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         slap_tool_destroy();
480
481         return rc;
482 }
483