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