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