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