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