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