]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
87059a57b558b1c796fabc6beeffac39fae5d4af
[openldap] / servers / slapd / tools / slapadd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9
10 #include <ac/stdlib.h>
11
12 #include <ac/ctype.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15 #include <ac/unistd.h>
16
17 #include <lber.h>
18 #include <ldif.h>
19 #include <lutil.h>
20
21 #include "slapcommon.h"
22
23 static char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
24
25 int
26 main( int argc, char **argv )
27 {
28         char            *buf = NULL;
29         int         lineno;
30         int         lmax;
31         int                     rc = EXIT_SUCCESS;
32
33         const char *text;
34         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
35         size_t textlen = sizeof textbuf;
36
37         struct berval csn;
38 #ifdef NEW_LOGGING
39         lutil_log_initialize(argc, argv );
40 #endif
41         slap_tool_init( "slapadd", SLAPADD, argc, argv );
42
43         if( !be->be_entry_open ||
44                 !be->be_entry_close ||
45                 !be->be_entry_put )
46         {
47                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
48                         progname );
49                 exit( EXIT_FAILURE );
50         }
51
52         lmax = 0;
53         lineno = 0;
54
55         if( be->be_entry_open( be, 1 ) != 0 ) {
56                 fprintf( stderr, "%s: could not open database.\n",
57                         progname );
58                 exit( EXIT_FAILURE );
59         }
60
61         while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
62                 Entry *e = str2entry( buf );
63                 struct berval bvtext;
64
65                 /*
66                  * Initialize text buffer
67                  */
68                 bvtext.bv_len = textlen;
69                 bvtext.bv_val = textbuf;
70                 bvtext.bv_val[0] = '\0';
71
72                 if( e == NULL ) {
73                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
74                                 progname, lineno );
75                         rc = EXIT_FAILURE;
76                         if( continuemode ) continue;
77                         break;
78                 }
79
80                 /* make sure the DN is not empty */
81                 if( !e->e_nname.bv_len ) {
82                         fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
83                                 progname, e->e_dn, lineno );
84                         rc = EXIT_FAILURE;
85                         entry_free( e );
86                         if( continuemode ) continue;
87                         break;
88                 }
89
90                 /* check backend */
91                 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
92                         != be )
93                 {
94                         fprintf( stderr, "%s: line %d: "
95                                 "database (%s) not configured to hold \"%s\"\n",
96                                 progname, lineno,
97                                 be ? be->be_suffix[0].bv_val : "<none>",
98                                 e->e_dn );
99                         fprintf( stderr, "%s: line %d: "
100                                 "database (%s) not configured to hold \"%s\"\n",
101                                 progname, lineno,
102                                 be ? be->be_nsuffix[0].bv_val : "<none>",
103                                 e->e_ndn );
104                         rc = EXIT_FAILURE;
105                         entry_free( e );
106                         if( continuemode ) continue;
107                         break;
108                 }
109
110                 if( global_schemacheck ) {
111                         Attribute *sc = attr_find( e->e_attrs,
112                                 slap_schema.si_ad_structuralObjectClass );
113                         Attribute *oc = attr_find( e->e_attrs,
114                                 slap_schema.si_ad_objectClass );
115
116                         if( oc == NULL ) {
117                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
118                                         progname, e->e_dn, lineno,
119                                         "no objectClass attribute");
120                                 rc = EXIT_FAILURE;
121                                 entry_free( e );
122                                 if( continuemode ) continue;
123                                 break;
124                         }
125
126                         if( sc == NULL ) {
127                                 struct berval vals[2];
128
129                                 rc = structural_class( oc->a_vals, vals,
130                                         NULL, &text, textbuf, textlen );
131
132                                 if( rc != LDAP_SUCCESS ) {
133                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
134                                                 progname, e->e_dn, lineno, rc, text );
135                                         rc = EXIT_FAILURE;
136                                         entry_free( e );
137                                         if( continuemode ) continue;
138                                         break;
139                                 }
140
141                                 vals[1].bv_len = 0;
142                                 vals[1].bv_val = NULL;
143
144                                 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
145                                         vals, NULL /* FIXME */ );
146                         }
147
148                         /* check schema */
149                         rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
150
151                         if( rc != LDAP_SUCCESS ) {
152                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
153                                         progname, e->e_dn, lineno, rc, text );
154                                 rc = EXIT_FAILURE;
155                                 entry_free( e );
156                                 if( continuemode ) continue;
157                                 break;
158                         }
159                 }
160
161                 if ( SLAP_LASTMOD(be) ) {
162                         struct tm *ltm;
163                         time_t now = slap_get_time();
164                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
165                         struct berval vals[ 2 ];
166
167                         struct berval name, timestamp;
168
169                         struct berval nvals[ 2 ];
170                         struct berval nname;
171                         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
172
173                         vals[1].bv_len = 0;
174                         vals[1].bv_val = NULL;
175
176                         nvals[1].bv_len = 0;
177                         nvals[1].bv_val = NULL;
178
179                         ltm = gmtime(&now);
180                         lutil_gentime( timebuf, sizeof(timebuf), ltm );
181
182                         csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
183                         csn.bv_val = csnbuf;
184
185                         timestamp.bv_val = timebuf;
186                         timestamp.bv_len = strlen(timebuf);
187
188                         if ( be->be_rootndn.bv_len == 0 ) {
189                                 name.bv_val = SLAPD_ANONYMOUS;
190                                 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
191                                 nname.bv_val = SLAPD_ANONYMOUS;
192                                 nname.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
193                         } else {
194                                 name = be->be_rootdn;
195                                 nname = be->be_rootndn;
196                         }
197
198                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
199                                 == NULL )
200                         {
201                                 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
202                                 vals[0].bv_val = uuidbuf;
203                                 attr_merge( e, slap_schema.si_ad_entryUUID, vals, NULL );
204                         }
205
206                         if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
207                                 == NULL )
208                         {
209                                 vals[0] = name;
210                                 nvals[0] = nname;
211                                 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
212                         }
213
214                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
215                                 == NULL )
216                         {
217                                 vals[0] = name;
218                                 nvals[0] = nname;
219                                 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
220                         }
221
222                         if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
223                                 == NULL )
224                         {
225                                 vals[0] = timestamp;
226                                 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
227                         }
228
229                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
230                                 == NULL )
231                         {
232                                 vals[0] = timestamp;
233                                 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
234                         }
235
236                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
237                                 == NULL )
238                         {
239                                 vals[0] = csn;
240                                 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
241                         }
242                 }
243
244                 if (!dryrun) {
245                         ID id = be->be_entry_put( be, e, &bvtext );
246                         if( id == NOID ) {
247                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d): %s\n",
248                                         progname, e->e_dn, lineno, bvtext.bv_val );
249                                 rc = EXIT_FAILURE;
250                                 entry_free( e );
251                                 if( continuemode ) continue;
252                                 break;
253                         }
254
255                         if ( verbose ) {
256                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
257                                         e->e_dn, (long) id );
258                         }
259                 } else {
260                         if ( verbose ) {
261                                 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
262                         }
263                 }
264
265                 entry_free( e );
266         }
267
268         if ( SLAP_LASTMOD(be) && update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH && csn.bv_len > 0 ) {
269                 Entry *ctxcsn_e;
270                 ID      ctxcsn_id;
271                 struct berval   ctxcsn_rdn = { 0, NULL };
272                 struct berval   ctxcsn_ndn = { 0, NULL };
273                 int ret;
274                 struct berval bvtext;
275                 Attribute *attr;
276
277                 bvtext.bv_len = textlen;
278                 bvtext.bv_val = textbuf;
279                 bvtext.bv_val[0] = '\0';
280
281                 ber_str2bv( "cn=ldapsync", strlen( "cn=ldapsync" ), 0, &ctxcsn_rdn );
282                 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0], &ctxcsn_rdn );
283                 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
284                 
285                 if ( ctxcsn_id == NOID ) {
286                         ctxcsn_e = slap_create_context_csn_entry( be, &csn );
287                         ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
288                         if( ctxcsn_id == NOID ) {
289                                 fprintf( stderr, "%s: could not add ctxcsn subentry\n", progname);
290                                 rc = EXIT_FAILURE;
291                         }
292                         if ( verbose ) {
293                                 fprintf( stderr, "added: \"%s\" (%08lx)\n", ctxcsn_e->e_dn, (long) ctxcsn_id );
294                         }
295                         entry_free( ctxcsn_e );
296                 } else {
297                         ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
298                         if ( ret == LDAP_SUCCESS ) {
299                                 attr = attr_find( ctxcsn_e->e_attrs, slap_schema.si_ad_contextCSN );
300                                 attr->a_vals[0] = csn;
301                                 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
302                                 if( ctxcsn_id == NOID ) {
303                                         fprintf( stderr, "%s: could not modify ctxcsn subentry\n", progname);
304                                         rc = EXIT_FAILURE;
305                                 }
306                                 if ( verbose ) {
307                                         fprintf( stderr, "modified: \"%s\" (%08lx)\n", ctxcsn_e->e_dn, (long) ctxcsn_id );
308                                 }
309                         } else {
310                                 fprintf( stderr, "%s: could not modify ctxcsn subentry\n", progname);
311                                 rc = EXIT_FAILURE;
312                         }
313                 }
314         }
315
316         ch_free( buf );
317
318         if( be->be_entry_close( be )) rc = EXIT_FAILURE;
319
320         if( be->be_sync ) {
321                 be->be_sync( be );
322         }
323
324         slap_tool_destroy();
325         return rc;
326 }