Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / slapadd.c
blob0413c83e89e80f3d05582e884e7ef8cf5346373c
1 /* $OpenLDAP: pkg/ldap/servers/slapd/slapadd.c,v 1.36.2.7 2008/04/14 21:15:47 quanah Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 The OpenLDAP Foundation.
5 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
6 * Portions Copyright 2003 IBM Corporation.
7 * All rights reserved.
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.
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>.
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
24 #include "portable.h"
26 #include <stdio.h>
28 #include <ac/stdlib.h>
30 #include <ac/ctype.h>
31 #include <ac/string.h>
32 #include <ac/socket.h>
33 #include <ac/unistd.h>
35 #include <lber.h>
36 #include <ldif.h>
37 #include <lutil.h>
39 #include "slapcommon.h"
41 static char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
42 static char maxcsnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE * ( SLAP_SYNC_SID_MAX + 1 ) ];
44 int
45 slapadd( int argc, char **argv )
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";
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;
63 int match;
64 int checkvals;
65 int lineno, nextline;
66 int lmax;
67 int rc = EXIT_SUCCESS;
68 int manage = 0;
70 /* default "000" */
71 csnsid = 0;
73 slap_tool_init( progname, SLAPADD, argc, argv );
75 memset( &opbuf, 0, sizeof(opbuf) );
76 op = &opbuf.ob_op;
77 op->o_hdr = &opbuf.ob_hdr;
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)) )
87 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
88 progname );
89 if ( dryrun ) {
90 fprintf( stderr, "\t(dry) continuing...\n" );
92 } else {
93 exit( EXIT_FAILURE );
97 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
99 lmax = 0;
100 nextline = 0;
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);
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 );
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;
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;
126 if ( lineno < jumpline )
127 continue;
129 e = str2entry2( buf, checkvals );
132 * Initialize text buffer
134 bvtext.bv_len = textlen;
135 bvtext.bv_val = textbuf;
136 bvtext.bv_val[0] = '\0';
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;
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;
157 /* check backend */
158 if( select_backend( &e->e_nname, nosubordinates )
159 != be )
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;
178 Attribute *oc = attr_find( e->e_attrs,
179 slap_schema.si_ad_objectClass );
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;
191 /* check schema */
192 op->o_bd = be;
194 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
195 rc = entry_schema_check( op, e, NULL, manage, 1,
196 &text, textbuf, textlen );
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;
206 textbuf[ 0 ] = '\0';
210 if ( SLAP_LASTMOD(be) ) {
211 time_t now = slap_get_time();
212 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
213 struct berval vals[ 2 ];
215 struct berval name, timestamp;
217 struct berval nvals[ 2 ];
218 struct berval nname;
219 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
221 vals[1].bv_len = 0;
222 vals[1].bv_val = NULL;
224 nvals[1].bv_len = 0;
225 nvals[1].bv_val = NULL;
227 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
228 csn.bv_val = csnbuf;
230 timestamp.bv_val = timebuf;
231 timestamp.bv_len = sizeof(timebuf);
233 slap_timestamp( &now, &timestamp );
235 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
236 BER_BVSTR( &name, SLAPD_ANONYMOUS );
237 nname = name;
238 } else {
239 name = be->be_rootdn;
240 nname = be->be_rootndn;
243 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
244 == NULL )
246 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
247 vals[0].bv_val = uuidbuf;
248 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
251 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
252 == NULL )
254 vals[0] = name;
255 nvals[0] = nname;
256 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
259 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
260 == NULL )
262 vals[0] = timestamp;
263 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
266 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
267 == NULL )
269 vals[0] = csn;
270 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
273 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
274 == NULL )
276 vals[0] = name;
277 nvals[0] = nname;
278 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
281 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
282 == NULL )
284 vals[0] = timestamp;
285 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
288 if ( update_ctxcsn ) {
289 int rc_sid;
291 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
292 assert( attr != NULL );
294 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] );
295 if ( rc_sid < 0 ) {
296 Debug( LDAP_DEBUG_ANY, "%s: could not "
297 "extract SID from entryCSN=%s\n",
298 progname, attr->a_nvals[ 0 ].bv_val, 0 );
300 } else {
301 assert( rc_sid <= SLAP_SYNC_SID_MAX );
303 sid = (unsigned)rc_sid;
304 if ( maxcsn[ sid ].bv_len != 0 ) {
305 match = 0;
306 value_match( &match, slap_schema.si_ad_entryCSN,
307 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
308 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
309 &maxcsn[ sid ], &attr->a_nvals[0], &text );
310 } else {
311 match = -1;
313 if ( match < 0 ) {
314 strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val );
315 maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len;
321 if ( !dryrun ) {
322 id = be->be_entry_put( be, e, &bvtext );
323 if( id == NOID ) {
324 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
325 "(line=%d): %s\n", progname, e->e_dn,
326 lineno, bvtext.bv_val );
327 rc = EXIT_FAILURE;
328 entry_free( e );
329 if( continuemode ) continue;
330 break;
332 if ( verbose )
333 fprintf( stderr, "added: \"%s\" (%08lx)\n",
334 e->e_dn, (long) id );
335 } else {
336 if ( verbose )
337 fprintf( stderr, "added: \"%s\"\n",
338 e->e_dn );
341 entry_free( e );
344 bvtext.bv_len = textlen;
345 bvtext.bv_val = textbuf;
346 bvtext.bv_val[0] = '\0';
348 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
349 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
350 if ( ctxcsn_id == NOID ) {
351 fprintf( stderr, "%s: context entry is missing\n", progname );
352 rc = EXIT_FAILURE;
353 } else {
354 ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
355 if ( ctxcsn_e != NULL ) {
356 Entry *e = entry_dup( ctxcsn_e );
357 int change;
358 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
359 if ( attr ) {
360 int i;
362 change = 0;
364 for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
365 int rc_sid;
367 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
368 if ( rc_sid < 0 ) {
369 Debug( LDAP_DEBUG_ANY,
370 "%s: unable to extract SID "
371 "from #%d contextCSN=%s\n",
372 progname, i,
373 attr->a_nvals[ i ].bv_val );
374 continue;
377 assert( rc_sid <= SLAP_SYNC_SID_MAX );
379 sid = (unsigned)rc_sid;
381 if ( maxcsn[ sid ].bv_len == 0 ) {
382 match = -1;
384 } else {
385 value_match( &match, slap_schema.si_ad_entryCSN,
386 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
387 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
388 &maxcsn[ sid ], &attr->a_nvals[i], &text );
391 if ( match > 0 ) {
392 change = 1;
393 } else {
394 AC_MEMCPY( maxcsn[ sid ].bv_val,
395 attr->a_nvals[ i ].bv_val,
396 attr->a_nvals[ i ].bv_len );
397 maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
398 maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
402 if ( change ) {
403 if ( attr->a_nvals != attr->a_vals ) {
404 ber_bvarray_free( attr->a_nvals );
406 attr->a_nvals = NULL;
407 ber_bvarray_free( attr->a_vals );
408 attr->a_vals = NULL;
409 attr->a_numvals = 0;
411 } else {
412 change = 1;
415 if ( change ) {
416 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
417 if ( maxcsn[ sid ].bv_len ) {
418 attr_merge_one( e, slap_schema.si_ad_contextCSN,
419 &maxcsn[ sid], NULL );
423 ctxcsn_id = be->be_entry_modify( be, e, &bvtext );
424 if( ctxcsn_id == NOID ) {
425 fprintf( stderr, "%s: could not modify ctxcsn\n",
426 progname);
427 rc = EXIT_FAILURE;
428 } else if ( verbose ) {
429 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
430 e->e_dn, (long) ctxcsn_id );
433 entry_free( e );
438 ch_free( buf );
440 if ( !dryrun ) {
441 if( be->be_entry_close( be ) ) {
442 rc = EXIT_FAILURE;
445 if( be->be_sync ) {
446 be->be_sync( be );
450 slap_tool_destroy();
452 return rc;