]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
Implement slapcat -s <dn>: Only dump a subtree of the database.
[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 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 #ifdef NEW_LOGGING
36         lutil_log_initialize(argc, argv );
37 #endif
38         slap_tool_init( "slapadd", SLAPADD, argc, argv );
39
40         if( !be->be_entry_open ||
41                 !be->be_entry_close ||
42                 !be->be_entry_put )
43         {
44                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
45                         progname );
46                 exit( EXIT_FAILURE );
47         }
48
49         lmax = 0;
50         lineno = 0;
51
52         if( be->be_entry_open( be, 1 ) != 0 ) {
53                 fprintf( stderr, "%s: could not open database.\n",
54                         progname );
55                 exit( EXIT_FAILURE );
56         }
57
58         while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
59                 Entry *e = str2entry( buf );
60                 struct berval bvtext;
61
62                 /*
63                  * Initialize text buffer
64                  */
65                 bvtext.bv_len = textlen;
66                 bvtext.bv_val = textbuf;
67                 bvtext.bv_val[0] = '\0';
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                 if( global_schemacheck ) {
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                                 rc = structural_class( oc->a_vals, vals,
127                                         NULL, &text, textbuf, textlen );
128
129                                 if( rc != LDAP_SUCCESS ) {
130                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
131                                                 progname, e->e_dn, lineno, rc, text );
132                                         rc = EXIT_FAILURE;
133                                         entry_free( e );
134                                         if( continuemode ) continue;
135                                         break;
136                                 }
137
138                                 vals[1].bv_len = 0;
139                                 vals[1].bv_val = NULL;
140
141                                 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
142                                         vals, NULL /* FIXME */ );
143                         }
144
145                         /* check schema */
146                         rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
147
148                         if( rc != LDAP_SUCCESS ) {
149                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
150                                         progname, e->e_dn, lineno, rc, text );
151                                 rc = EXIT_FAILURE;
152                                 entry_free( e );
153                                 if( continuemode ) continue;
154                                 break;
155                         }
156                 }
157
158                 if ( SLAP_LASTMOD(be) ) {
159                         struct tm *ltm;
160                         time_t now = slap_get_time();
161                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
162                         struct berval vals[ 2 ];
163
164                         struct berval name, timestamp, csn;
165
166                         struct berval nvals[ 2 ];
167                         struct berval nname;
168                         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
169                         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
170
171                         vals[1].bv_len = 0;
172                         vals[1].bv_val = NULL;
173
174                         nvals[1].bv_len = 0;
175                         nvals[1].bv_val = NULL;
176
177                         ltm = gmtime(&now);
178                         lutil_gentime( timebuf, sizeof(timebuf), ltm );
179
180                         csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
181                         csn.bv_val = csnbuf;
182
183                         timestamp.bv_val = timebuf;
184                         timestamp.bv_len = strlen(timebuf);
185
186                         if ( be->be_rootndn.bv_len == 0 ) {
187                                 name.bv_val = SLAPD_ANONYMOUS;
188                                 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
189                                 nname.bv_val = SLAPD_ANONYMOUS;
190                                 nname.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
191                         } else {
192                                 name = be->be_rootdn;
193                                 nname = be->be_rootndn;
194                         }
195
196                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
197                                 == NULL )
198                         {
199                                 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
200                                 vals[0].bv_val = uuidbuf;
201                                 attr_merge( e, slap_schema.si_ad_entryUUID, vals, NULL );
202                         }
203
204                         if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
205                                 == NULL )
206                         {
207                                 vals[0] = name;
208                                 nvals[0] = nname;
209                                 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
210                         }
211
212                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
213                                 == NULL )
214                         {
215                                 vals[0] = name;
216                                 nvals[0] = nname;
217                                 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
218                         }
219
220                         if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
221                                 == NULL )
222                         {
223                                 vals[0] = timestamp;
224                                 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
225                         }
226
227                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
228                                 == NULL )
229                         {
230                                 vals[0] = timestamp;
231                                 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
232                         }
233
234                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
235                                 == NULL )
236                         {
237                                 vals[0] = csn;
238                                 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
239                         }
240                 }
241
242                 if (!dryrun) {
243                         ID id = be->be_entry_put( be, e, &bvtext );
244                         if( id == NOID ) {
245                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d): %s\n",
246                                         progname, e->e_dn, lineno, bvtext.bv_val );
247                                 rc = EXIT_FAILURE;
248                                 entry_free( e );
249                                 if( continuemode ) continue;
250                                 break;
251                         }
252                 
253                         if ( verbose ) {
254                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
255                                         e->e_dn, (long) id );
256                         }
257                 } else {
258                         if ( verbose ) {
259                                 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
260                         }
261                 }
262
263                 entry_free( e );
264         }
265
266         ch_free( buf );
267
268         if( be->be_entry_close( be )) rc = EXIT_FAILURE;
269
270         if( be->be_sync ) {
271                 be->be_sync( be );
272         }
273
274         slap_tool_destroy();
275         return rc;
276 }