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