]> git.sur5r.net Git - openldap/blob - servers/slapd/slapadd.c
788100e615605c8b0d9a4f94986762ede874844a
[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;
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         lmax = 0;
84         lineno = 0;
85
86         if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
87                 fprintf( stderr, "%s: could not open database.\n",
88                         progname );
89                 exit( EXIT_FAILURE );
90         }
91
92         if ( update_ctxcsn ) {
93                 maxcsn.bv_val = maxcsnbuf;
94                 maxcsn.bv_len = 0;
95         }
96
97         while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
98                 Entry *e = str2entry( buf );
99
100                 /*
101                  * Initialize text buffer
102                  */
103                 bvtext.bv_len = textlen;
104                 bvtext.bv_val = textbuf;
105                 bvtext.bv_val[0] = '\0';
106
107                 if( e == NULL ) {
108                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
109                                 progname, lineno );
110                         rc = EXIT_FAILURE;
111                         if( continuemode ) continue;
112                         break;
113                 }
114
115                 /* make sure the DN is not empty */
116                 if( !e->e_nname.bv_len ) {
117                         fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
118                                 progname, e->e_dn, lineno );
119                         rc = EXIT_FAILURE;
120                         entry_free( e );
121                         if( continuemode ) continue;
122                         break;
123                 }
124
125                 /* check backend */
126                 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
127                         != be )
128                 {
129                         fprintf( stderr, "%s: line %d: "
130                                 "database (%s) not configured to hold \"%s\"\n",
131                                 progname, lineno,
132                                 be ? be->be_suffix[0].bv_val : "<none>",
133                                 e->e_dn );
134                         fprintf( stderr, "%s: line %d: "
135                                 "database (%s) not configured to hold \"%s\"\n",
136                                 progname, lineno,
137                                 be ? be->be_nsuffix[0].bv_val : "<none>",
138                                 e->e_ndn );
139                         rc = EXIT_FAILURE;
140                         entry_free( e );
141                         if( continuemode ) continue;
142                         break;
143                 }
144
145                 if( global_schemacheck ) {
146                         Attribute *sc = attr_find( e->e_attrs,
147                                 slap_schema.si_ad_structuralObjectClass );
148                         Attribute *oc = attr_find( e->e_attrs,
149                                 slap_schema.si_ad_objectClass );
150
151                         if( oc == NULL ) {
152                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
153                                         progname, e->e_dn, lineno,
154                                         "no objectClass attribute");
155                                 rc = EXIT_FAILURE;
156                                 entry_free( e );
157                                 if( continuemode ) continue;
158                                 break;
159                         }
160
161                         if( sc == NULL ) {
162                                 struct berval vals[2];
163
164                                 rc = structural_class( oc->a_vals, vals,
165                                         NULL, &text, textbuf, textlen );
166
167                                 if( rc != LDAP_SUCCESS ) {
168                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
169                                                 progname, e->e_dn, lineno, rc, text );
170                                         rc = EXIT_FAILURE;
171                                         entry_free( e );
172                                         if( continuemode ) continue;
173                                         break;
174                                 }
175
176                                 vals[1].bv_len = 0;
177                                 vals[1].bv_val = NULL;
178
179                                 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
180                                         vals, NULL /* FIXME */ );
181                         }
182
183                         /* check schema */
184                         rc = entry_schema_check( be, e, NULL, &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                 if ( SLAP_LASTMOD(be) ) {
197                         struct tm *ltm;
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                         ltm = gmtime(&now);
215                         lutil_gentime( timebuf, sizeof(timebuf), ltm );
216
217                         csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
218                         csn.bv_val = csnbuf;
219
220                         timestamp.bv_val = timebuf;
221                         timestamp.bv_len = strlen(timebuf);
222
223                         if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
224                                 BER_BVSTR( &name, SLAPD_ANONYMOUS );
225                                 nname = name;
226                         } else {
227                                 name = be->be_rootdn;
228                                 nname = be->be_rootndn;
229                         }
230
231                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
232                                 == NULL )
233                         {
234                                 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
235                                 vals[0].bv_val = uuidbuf;
236                                 attr_merge_normalize_one( e,
237                                                         slap_schema.si_ad_entryUUID, vals, NULL );
238                         }
239
240                         if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
241                                 == NULL )
242                         {
243                                 vals[0] = name;
244                                 nvals[0] = nname;
245                                 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
246                         }
247
248                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
249                                 == NULL )
250                         {
251                                 vals[0] = name;
252                                 nvals[0] = nname;
253                                 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
254                         }
255
256                         if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
257                                 == NULL )
258                         {
259                                 vals[0] = timestamp;
260                                 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
261                         }
262
263                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
264                                 == NULL )
265                         {
266                                 vals[0] = timestamp;
267                                 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
268                         }
269
270                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
271                                 == NULL )
272                         {
273                                 vals[0] = csn;
274                                 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
275                         }
276
277                         if ( update_ctxcsn ) {
278                                 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
279                                 if ( maxcsn.bv_len != 0 ) {
280                                         match = 0;
281                                         value_match( &match, slap_schema.si_ad_entryCSN,
282                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
283                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
284                                                 &maxcsn, &attr->a_nvals[0], &text );
285                                 } else {
286                                         match = -1;
287                                 }
288                                 if ( match < 0 ) {
289                                         strcpy( maxcsn.bv_val, attr->a_nvals[0].bv_val );
290                                         maxcsn.bv_len = attr->a_nvals[0].bv_len;
291                                 }
292                         }
293                 }
294
295                 if ( !dryrun ) {
296                         id = be->be_entry_put( be, e, &bvtext );
297                         if( id == NOID ) {
298                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
299                                                                  "(line=%d): %s\n", progname, e->e_dn,
300                                                                  lineno, bvtext.bv_val );
301                                 rc = EXIT_FAILURE;
302                                 entry_free( e );
303                                 if( continuemode ) continue;
304                                 break;
305                         }
306                 }
307
308                 if ( verbose ) {
309                         if ( dryrun ) {
310                                 fprintf( stderr, "added: \"%s\"\n",
311                                         e->e_dn );
312                         } else {
313                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
314                                         e->e_dn, (long) id );
315                         }
316                 }
317
318 done:;
319                 entry_free( e );
320         }
321
322         bvtext.bv_len = textlen;
323         bvtext.bv_val = textbuf;
324         bvtext.bv_val[0] = '\0';
325
326         if ( update_ctxcsn && !dryrun && maxcsn.bv_len ) {
327                 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
328                 if ( ctxcsn_id == NOID ) {
329                         fprintf( stderr, "%s: context entry is missing\n", progname );
330                         rc = EXIT_FAILURE;
331                 } else {
332                         ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
333                         if ( ret == LDAP_SUCCESS ) {
334                                 attr = attr_find( ctxcsn_e->e_attrs,
335                                                                         slap_schema.si_ad_contextCSN );
336                                 value_match( &match, slap_schema.si_ad_entryCSN,
337                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
338                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
339                                         &maxcsn, &attr->a_nvals[0], &text );
340                                 if ( match > 0 ) {
341                                         AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
342                                         attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
343                                         attr->a_vals[0].bv_len = maxcsn.bv_len;
344                                 
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