]> git.sur5r.net Git - openldap/blob - servers/slapd/slapadd.c
move ctxcsn and schema check code in helpers; also apply to slapmodify (ITS#6737)
[openldap] / servers / slapd / slapadd.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2010 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 #include <lutil_meter.h>
39 #include <sys/stat.h>
40
41 #include "slapcommon.h"
42
43 static char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE ];
44
45 int
46 slapadd( int argc, char **argv )
47 {
48         char *buf = NULL;
49         const char *text;
50         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
51         size_t textlen = sizeof textbuf;
52         const char *progname = "slapadd";
53
54         struct berval csn;
55         unsigned long sid = SLAP_SYNC_SID_MAX + 1;
56         struct berval bvtext;
57         ID id;
58         OperationBuffer opbuf;
59         Operation *op;
60
61         int checkvals;
62         int lineno, nextline, ldifrc;
63         int lmax;
64         int rc = EXIT_SUCCESS;
65
66         int enable_meter = 0;
67         lutil_meter_t meter;
68         struct stat stat_buf;
69
70         /* default "000" */
71         csnsid = 0;
72
73         if ( isatty (2) ) enable_meter = 1;
74         slap_tool_init( progname, SLAPADD, argc, argv );
75
76         memset( &opbuf, 0, sizeof(opbuf) );
77         op = &opbuf.ob_op;
78         op->o_hdr = &opbuf.ob_hdr;
79
80         if( !be->be_entry_open ||
81                 !be->be_entry_close ||
82                 !be->be_entry_put ||
83                 (update_ctxcsn &&
84                  (!be->be_dn2id_get ||
85                   !be->be_entry_get ||
86                   !be->be_entry_modify)) )
87         {
88                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
89                         progname );
90                 if ( dryrun ) {
91                         fprintf( stderr, "\t(dry) continuing...\n" );
92
93                 } else {
94                         exit( EXIT_FAILURE );
95                 }
96         }
97
98         checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
99
100         /* do not check values in quick mode */
101         if ( slapMode & SLAP_TOOL_QUICK ) {
102                 if ( slapMode & SLAP_TOOL_VALUE_CHECK ) {
103                         fprintf( stderr, "%s: value-check incompatible with quick mode; disabled.\n", progname );
104                         slapMode &= ~SLAP_TOOL_VALUE_CHECK;
105                 }
106         }
107
108         lmax = 0;
109         nextline = 0;
110
111         /* enforce schema checking unless not disabled */
112         if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
113                 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
114         }
115
116         if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
117                 fprintf( stderr, "%s: could not open database.\n",
118                         progname );
119                 exit( EXIT_FAILURE );
120         }
121
122         (void)slap_tool_update_ctxcsn_init();
123
124         if ( enable_meter 
125 #ifdef LDAP_DEBUG
126                 /* tools default to "none" */
127                 && slap_debug == LDAP_DEBUG_NONE
128 #endif
129                 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
130                 && S_ISREG(stat_buf.st_mode) ) {
131                 enable_meter = !lutil_meter_open(
132                         &meter,
133                         &lutil_meter_text_display,
134                         &lutil_meter_linear_estimator,
135                         stat_buf.st_size);
136         } else {
137                 enable_meter = 0;
138         }
139
140         /* nextline is the line number of the end of the current entry */
141         for( lineno=1; ( ldifrc = ldif_read_record( ldiffp, &nextline, &buf, &lmax )) > 0;
142                 lineno=nextline+1 )
143         {
144                 BackendDB *bd;
145                 Entry *e;
146
147                 if ( lineno < jumpline )
148                         continue;
149
150                 e = str2entry2( buf, checkvals );
151
152                 if ( enable_meter )
153                         lutil_meter_update( &meter,
154                                          ftell( ldiffp->fp ),
155                                          0);
156
157                 /*
158                  * Initialize text buffer
159                  */
160                 bvtext.bv_len = textlen;
161                 bvtext.bv_val = textbuf;
162                 bvtext.bv_val[0] = '\0';
163
164                 if( e == NULL ) {
165                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
166                                 progname, lineno );
167                         rc = EXIT_FAILURE;
168                         if( continuemode ) continue;
169                         break;
170                 }
171
172                 /* make sure the DN is not empty */
173                 if( BER_BVISEMPTY( &e->e_nname ) &&
174                         !BER_BVISEMPTY( be->be_nsuffix ))
175                 {
176                         fprintf( stderr, "%s: line %d: "
177                                 "cannot add entry with empty dn=\"%s\"",
178                                 progname, lineno, e->e_dn );
179                         bd = select_backend( &e->e_nname, nosubordinates );
180                         if ( bd ) {
181                                 BackendDB *bdtmp;
182                                 int dbidx = 0;
183                                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
184                                         if ( bdtmp == bd ) break;
185                                         dbidx++;
186                                 }
187
188                                 assert( bdtmp != NULL );
189                                 
190                                 fprintf( stderr, "; did you mean to use database #%d (%s)?",
191                                         dbidx,
192                                         bd->be_suffix[0].bv_val );
193
194                         }
195                         fprintf( stderr, "\n" );
196                         rc = EXIT_FAILURE;
197                         entry_free( e );
198                         if( continuemode ) continue;
199                         break;
200                 }
201
202                 /* check backend */
203                 bd = select_backend( &e->e_nname, nosubordinates );
204                 if ( bd != be ) {
205                         fprintf( stderr, "%s: line %d: "
206                                 "database #%d (%s) not configured to hold \"%s\"",
207                                 progname, lineno,
208                                 dbnum,
209                                 be->be_suffix[0].bv_val,
210                                 e->e_dn );
211                         if ( bd ) {
212                                 BackendDB *bdtmp;
213                                 int dbidx = 0;
214                                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
215                                         if ( bdtmp == bd ) break;
216                                         dbidx++;
217                                 }
218
219                                 assert( bdtmp != NULL );
220                                 
221                                 fprintf( stderr, "; did you mean to use database #%d (%s)?",
222                                         dbidx,
223                                         bd->be_suffix[0].bv_val );
224
225                         } else {
226                                 fprintf( stderr, "; no database configured for that naming context" );
227                         }
228                         fprintf( stderr, "\n" );
229                         rc = EXIT_FAILURE;
230                         entry_free( e );
231                         if( continuemode ) continue;
232                         break;
233                 }
234
235                 rc = slap_tool_entry_check( progname, op, e, lineno, &text, textbuf, textlen );
236                 if ( rc != LDAP_SUCCESS ) {
237                         rc = EXIT_FAILURE;
238                         entry_free( e );
239                         if( continuemode ) continue;
240                         break;
241                 }
242
243                 if ( SLAP_LASTMOD(be) ) {
244                         time_t now = slap_get_time();
245                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
246                         struct berval vals[ 2 ];
247
248                         struct berval name, timestamp;
249
250                         struct berval nvals[ 2 ];
251                         struct berval nname;
252                         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
253
254                         enum {
255                                 GOT_NONE = 0x0,
256                                 GOT_CSN = 0x1,
257                                 GOT_UUID = 0x2,
258                                 GOT_ALL = (GOT_CSN|GOT_UUID)
259                         } got = GOT_ALL;
260
261                         vals[1].bv_len = 0;
262                         vals[1].bv_val = NULL;
263
264                         nvals[1].bv_len = 0;
265                         nvals[1].bv_val = NULL;
266
267                         csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
268                         csn.bv_val = csnbuf;
269
270                         timestamp.bv_val = timebuf;
271                         timestamp.bv_len = sizeof(timebuf);
272
273                         slap_timestamp( &now, &timestamp );
274
275                         if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
276                                 BER_BVSTR( &name, SLAPD_ANONYMOUS );
277                                 nname = name;
278                         } else {
279                                 name = be->be_rootdn;
280                                 nname = be->be_rootndn;
281                         }
282
283                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
284                                 == NULL )
285                         {
286                                 got &= ~GOT_UUID;
287                                 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
288                                 vals[0].bv_val = uuidbuf;
289                                 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
290                         }
291
292                         if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
293                                 == NULL )
294                         {
295                                 vals[0] = name;
296                                 nvals[0] = nname;
297                                 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
298                         }
299
300                         if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
301                                 == NULL )
302                         {
303                                 vals[0] = timestamp;
304                                 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
305                         }
306
307                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
308                                 == NULL )
309                         {
310                                 got &= ~GOT_CSN;
311                                 vals[0] = csn;
312                                 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
313                         }
314
315                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
316                                 == NULL )
317                         {
318                                 vals[0] = name;
319                                 nvals[0] = nname;
320                                 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
321                         }
322
323                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
324                                 == NULL )
325                         {
326                                 vals[0] = timestamp;
327                                 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
328                         }
329
330                         if ( SLAP_SINGLE_SHADOW(be) && got != GOT_ALL ) {
331                                 char buf[SLAP_TEXT_BUFLEN];
332
333                                 snprintf( buf, sizeof(buf),
334                                         "%s%s%s",
335                                         ( !(got & GOT_UUID) ? slap_schema.si_ad_entryUUID->ad_cname.bv_val : "" ),
336                                         ( !(got & GOT_CSN) ? "," : "" ),
337                                         ( !(got & GOT_CSN) ? slap_schema.si_ad_entryCSN->ad_cname.bv_val : "" ) );
338
339                                 Debug( LDAP_DEBUG_ANY, "%s: warning, missing attrs %s from entry dn=\"%s\"\n",
340                                         progname, buf, e->e_name.bv_val );
341                         }
342
343                         sid = slap_tool_update_ctxcsn_check( progname, e );
344                 }
345
346                 if ( !dryrun ) {
347                         id = be->be_entry_put( be, e, &bvtext );
348                         if( id == NOID ) {
349                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
350                                                                  "(line=%d): %s\n", progname, e->e_dn,
351                                                                  lineno, bvtext.bv_val );
352                                 rc = EXIT_FAILURE;
353                                 entry_free( e );
354                                 if( continuemode ) continue;
355                                 break;
356                         }
357                         if ( verbose )
358                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
359                                         e->e_dn, (long) id );
360                 } else {
361                         if ( verbose )
362                                 fprintf( stderr, "added: \"%s\"\n",
363                                         e->e_dn );
364                 }
365
366                 entry_free( e );
367         }
368
369         if ( ldifrc < 0 )
370                 rc = EXIT_FAILURE;
371
372         bvtext.bv_len = textlen;
373         bvtext.bv_val = textbuf;
374         bvtext.bv_val[0] = '\0';
375
376         if ( enable_meter ) {
377                 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
378                 lutil_meter_close( &meter );
379         }
380
381         if ( rc == EXIT_SUCCESS ) {
382                 rc = slap_tool_update_ctxcsn( progname, sid, &bvtext );
383         }
384
385         ch_free( buf );
386
387         if ( !dryrun ) {
388                 if ( enable_meter ) {
389                         fprintf( stderr, "Closing DB..." );
390                 }
391                 if( be->be_entry_close( be ) ) {
392                         rc = EXIT_FAILURE;
393                 }
394
395                 if( be->be_sync ) {
396                         be->be_sync( be );
397                 }
398                 if ( enable_meter ) {
399                         fprintf( stderr, "\n" );
400                 }
401         }
402
403         if ( slap_tool_destroy())
404                 rc = EXIT_FAILURE;
405
406         return rc;
407 }
408