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