]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
use ldap_charray_*() instead of charray_*()
[openldap] / servers / slapd / tools / slapadd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 int
24 main( int argc, char **argv )
25 {
26         char            *buf = NULL;
27         int         lineno;
28         int         lmax;
29         int                     rc = EXIT_SUCCESS;
30
31         const char *text;
32         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
33         size_t textlen = sizeof textbuf;
34
35         struct berval name, timestamp, csn;
36         char timebuf[22];
37         char csnbuf[64];
38
39 #ifdef NEW_LOGGING
40         lutil_log_initialize(argc, argv );
41 #endif
42         slap_tool_init( "slapadd", SLAPADD, argc, argv );
43
44         if( !be->be_entry_open ||
45                 !be->be_entry_close ||
46                 !be->be_entry_put )
47         {
48                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
49                         progname );
50                 exit( EXIT_FAILURE );
51         }
52
53         lmax = 0;
54         lineno = 0;
55
56         if( be->be_entry_open( be, 1 ) != 0 ) {
57                 fprintf( stderr, "%s: could not open database.\n",
58                         progname );
59                 exit( EXIT_FAILURE );
60         }
61
62         while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
63                 Entry *e = str2entry( buf );
64                 struct berval bvtext;
65
66                 bvtext.bv_len = textlen;
67                 bvtext.bv_val = textbuf;
68
69                 if( e == NULL ) {
70                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
71                                 progname, lineno );
72                         rc = EXIT_FAILURE;
73                         if( continuemode ) continue;
74                         break;
75                 }
76
77                 /* make sure the DN is not empty */
78                 if( !e->e_nname.bv_len ) {
79                         fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
80                                 progname, e->e_dn, lineno );
81                         rc = EXIT_FAILURE;
82                         entry_free( e );
83                         if( continuemode ) continue;
84                         break;
85                 }
86
87                 /* check backend */
88                 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
89                         != be )
90                 {
91                         fprintf( stderr, "%s: line %d: "
92                                 "database (%s) not configured to hold \"%s\"\n",
93                                 progname, lineno,
94                                 be ? be->be_suffix[0].bv_val : "<none>",
95                                 e->e_dn );
96                         fprintf( stderr, "%s: line %d: "
97                                 "database (%s) not configured to hold \"%s\"\n",
98                                 progname, lineno,
99                                 be ? be->be_nsuffix[0].bv_val : "<none>",
100                                 e->e_ndn );
101                         rc = EXIT_FAILURE;
102                         entry_free( e );
103                         if( continuemode ) continue;
104                         break;
105                 }
106
107                 {
108                         Attribute *sc = attr_find( e->e_attrs,
109                                 slap_schema.si_ad_structuralObjectClass );
110                         Attribute *oc = attr_find( e->e_attrs,
111                                 slap_schema.si_ad_objectClass );
112
113                         if( oc == NULL ) {
114                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
115                                         progname, e->e_dn, lineno,
116                                         "no objectClass attribute");
117                                 rc = EXIT_FAILURE;
118                                 entry_free( e );
119                                 if( continuemode ) continue;
120                                 break;
121                         }
122
123                         if( sc == NULL ) {
124                                 struct berval vals[2];
125
126                                 /* int ret = */ 
127                                         structural_class( oc->a_vals, vals,
128                                         NULL, &text, textbuf, textlen );
129
130                                 if( vals[0].bv_len == 0 ) {
131                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
132                                         progname, e->e_dn, lineno, text );
133                                         rc = EXIT_FAILURE;
134                                         entry_free( e );
135                                         if( continuemode ) continue;
136                                         break;
137                                 }
138
139                                 vals[1].bv_val = NULL;
140                                 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
141                                         vals );
142                         }
143                 }
144
145                 if ( SLAP_LASTMOD(be) ) {
146                         struct tm *ltm;
147                         time_t now = slap_get_time();
148
149                         ltm = gmtime(&now);
150                         lutil_gentime( timebuf, sizeof(timebuf), ltm );
151
152                         csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
153                         csn.bv_val = csnbuf;
154
155                         timestamp.bv_val = timebuf;
156                         timestamp.bv_len = strlen(timebuf);
157
158                         if ( be->be_rootndn.bv_len == 0 ) {
159                                 name.bv_val = SLAPD_ANONYMOUS;
160                                 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
161                         } else {
162                                 name = be->be_rootndn;
163                         }
164                 }
165
166                 if( global_schemacheck ) {
167                         /* check schema */
168
169                         rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
170
171                         if( rc != LDAP_SUCCESS ) {
172                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
173                                         progname, e->e_dn, lineno, text );
174                                 rc = EXIT_FAILURE;
175                                 entry_free( e );
176                                 if( continuemode ) continue;
177                                 break;
178                         }
179                 }
180
181                 if ( SLAP_LASTMOD(be) ) {
182                         char uuidbuf[40];
183                         struct berval vals[2];
184
185                         vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
186                         vals[0].bv_val = uuidbuf;
187                         vals[1].bv_len = 0;
188                         vals[1].bv_val = NULL;
189                         attr_merge( e, slap_schema.si_ad_entryUUID, vals );
190
191                         ber_dupbv( &vals[0], &name );
192                         vals[1].bv_len = 0;
193                         vals[1].bv_val = NULL;
194                         attr_merge( e, slap_schema.si_ad_creatorsName, vals);
195
196                         ber_dupbv( &vals[0], &name );
197                         vals[1].bv_len = 0;
198                         vals[1].bv_val = NULL;
199                         attr_merge( e, slap_schema.si_ad_modifiersName, vals);
200
201                         ber_dupbv( &vals[0], &timestamp );
202                         vals[1].bv_len = 0;
203                         vals[1].bv_val = NULL;
204                         attr_merge( e, slap_schema.si_ad_createTimestamp, vals );
205
206                         ber_dupbv( &vals[0], &timestamp );
207                         vals[1].bv_len = 0;
208                         vals[1].bv_val = NULL;
209                         attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals );
210
211                         ber_dupbv( &vals[0], &csn );
212                         vals[1].bv_len = 0;
213                         vals[1].bv_val = NULL;
214                         attr_merge( e, slap_schema.si_ad_entryCSN, vals );
215                 }
216
217                 if (!dryrun) {
218                         ID id = be->be_entry_put( be, e, &bvtext );
219                         if( id == NOID ) {
220                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d): %s\n",
221                                         progname, e->e_dn, lineno, bvtext.bv_val );
222                                 rc = EXIT_FAILURE;
223                                 entry_free( e );
224                                 if( continuemode ) continue;
225                                 break;
226                         }
227                 
228                         if ( verbose ) {
229                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
230                                         e->e_dn, (long) id );
231                         }
232                 } else {
233                         if ( verbose ) {
234                                 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
235                         }
236                 }
237
238                 entry_free( e );
239         }
240
241         ch_free( buf );
242
243         be->be_entry_close( be );
244
245         if( be->be_sync ) {
246                 be->be_sync( be );
247         }
248
249         slap_tool_destroy();
250         return rc;
251 }