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