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