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