]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
Plug leaks
[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 #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                 bvtext.bv_len = textlen;
63                 bvtext.bv_val = textbuf;
64
65                 if( e == NULL ) {
66                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
67                                 progname, lineno );
68                         rc = EXIT_FAILURE;
69                         if( continuemode ) continue;
70                         break;
71                 }
72
73                 /* make sure the DN is not empty */
74                 if( !e->e_nname.bv_len ) {
75                         fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
76                                 progname, e->e_dn, lineno );
77                         rc = EXIT_FAILURE;
78                         entry_free( e );
79                         if( continuemode ) continue;
80                         break;
81                 }
82
83                 /* check backend */
84                 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
85                         != be )
86                 {
87                         fprintf( stderr, "%s: line %d: "
88                                 "database (%s) not configured to hold \"%s\"\n",
89                                 progname, lineno,
90                                 be ? be->be_suffix[0].bv_val : "<none>",
91                                 e->e_dn );
92                         fprintf( stderr, "%s: line %d: "
93                                 "database (%s) not configured to hold \"%s\"\n",
94                                 progname, lineno,
95                                 be ? be->be_nsuffix[0].bv_val : "<none>",
96                                 e->e_ndn );
97                         rc = EXIT_FAILURE;
98                         entry_free( e );
99                         if( continuemode ) continue;
100                         break;
101                 }
102
103                 if( global_schemacheck ) {
104                         Attribute *sc = attr_find( e->e_attrs,
105                                 slap_schema.si_ad_structuralObjectClass );
106                         Attribute *oc = attr_find( e->e_attrs,
107                                 slap_schema.si_ad_objectClass );
108
109                         if( oc == NULL ) {
110                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
111                                         progname, e->e_dn, lineno,
112                                         "no objectClass attribute");
113                                 rc = EXIT_FAILURE;
114                                 entry_free( e );
115                                 if( continuemode ) continue;
116                                 break;
117                         }
118
119                         if( sc == NULL ) {
120                                 struct berval vals[2];
121
122                                 rc = structural_class( oc->a_vals, vals,
123                                         NULL, &text, textbuf, textlen );
124
125                                 if( rc != LDAP_SUCCESS ) {
126                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
127                                                 progname, e->e_dn, lineno, rc, text );
128                                         rc = EXIT_FAILURE;
129                                         entry_free( e );
130                                         if( continuemode ) continue;
131                                         break;
132                                 }
133
134                                 vals[1].bv_val = NULL;
135                                 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
136                                         vals );
137                         }
138
139                         /* check schema */
140                         rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
141
142                         if( rc != LDAP_SUCCESS ) {
143                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
144                                         progname, e->e_dn, lineno, rc, text );
145                                 rc = EXIT_FAILURE;
146                                 entry_free( e );
147                                 if( continuemode ) continue;
148                                 break;
149                         }
150                 }
151
152                 if ( SLAP_LASTMOD(be) ) {
153                         struct tm *ltm;
154                         time_t now = slap_get_time();
155                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
156                         struct berval vals[ 2 ];
157
158                         struct berval name, timestamp, csn;
159                         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
160                         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
161
162                         ltm = gmtime(&now);
163                         lutil_gentime( timebuf, sizeof(timebuf), ltm );
164
165                         csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
166                         csn.bv_val = csnbuf;
167
168                         timestamp.bv_val = timebuf;
169                         timestamp.bv_len = strlen(timebuf);
170
171                         if ( be->be_rootndn.bv_len == 0 ) {
172                                 name.bv_val = SLAPD_ANONYMOUS;
173                                 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
174                         } else {
175                                 name = be->be_rootndn;
176                         }
177
178                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
179                                 == NULL )
180                         {
181                                 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
182                                 vals[0].bv_val = uuidbuf;
183                                 vals[1].bv_len = 0;
184                                 vals[1].bv_val = NULL;
185                                 attr_merge( e, slap_schema.si_ad_entryUUID, vals );
186                         }
187
188                         if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
189                                 == NULL )
190                         {
191                                 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
197                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
198                                 == NULL )
199                         {
200                                 vals[0] = name;
201                                 vals[1].bv_len = 0;
202                                 vals[1].bv_val = NULL;
203                                 attr_merge( e, slap_schema.si_ad_modifiersName, vals);
204                         }
205
206                         if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
207                                 == NULL )
208                         {
209                                 vals[0] = timestamp;
210                                 vals[1].bv_len = 0;
211                                 vals[1].bv_val = NULL;
212                                 attr_merge( e, slap_schema.si_ad_createTimestamp, vals );
213                         }
214
215                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
216                                 == NULL )
217                         {
218                                 vals[0] = timestamp;
219                                 vals[1].bv_len = 0;
220                                 vals[1].bv_val = NULL;
221                                 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals );
222                         }
223
224                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
225                                 == NULL )
226                         {
227                                 vals[0] = csn;
228                                 vals[1].bv_len = 0;
229                                 vals[1].bv_val = NULL;
230                                 attr_merge( e, slap_schema.si_ad_entryCSN, vals );
231                         }
232                 }
233
234                 if (!dryrun) {
235                         ID id = be->be_entry_put( be, e, &bvtext );
236                         if( id == NOID ) {
237                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d): %s\n",
238                                         progname, e->e_dn, lineno, bvtext.bv_val );
239                                 rc = EXIT_FAILURE;
240                                 entry_free( e );
241                                 if( continuemode ) continue;
242                                 break;
243                         }
244                 
245                         if ( verbose ) {
246                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
247                                         e->e_dn, (long) id );
248                         }
249                 } else {
250                         if ( verbose ) {
251                                 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
252                         }
253                 }
254
255                 entry_free( e );
256         }
257
258         ch_free( buf );
259
260         be->be_entry_close( be );
261
262         if( be->be_sync ) {
263                 be->be_sync( be );
264         }
265
266         slap_tool_destroy();
267         return rc;
268 }