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