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