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