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