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