]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
Fix cargv leak in recursive read_config()
[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
20 #include "slapcommon.h"
21
22 int
23 main( int argc, char **argv )
24 {
25         char            *buf = NULL;
26         int         lineno;
27         int         lmax;
28         int                     rc = EXIT_SUCCESS;
29
30         const char *text;
31         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
32         size_t textlen = sizeof textbuf;
33
34 #ifdef NEW_LOGGING
35         lutil_log_initialize(argc, argv );
36 #endif
37         slap_tool_init( "slapadd", SLAPADD, argc, argv );
38
39         if( !be->be_entry_open ||
40                 !be->be_entry_close ||
41                 !be->be_entry_put )
42         {
43                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
44                         progname );
45                 exit( EXIT_FAILURE );
46         }
47
48         lmax = 0;
49         lineno = 0;
50
51         if( be->be_entry_open( be, 1 ) != 0 ) {
52                 fprintf( stderr, "%s: could not open database.\n",
53                         progname );
54                 exit( EXIT_FAILURE );
55         }
56
57         while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
58                 Entry *e = str2entry( buf );
59                 struct berval bvtext;
60
61                 bvtext.bv_len = textlen;
62                 bvtext.bv_val = textbuf;
63
64                 if( e == NULL ) {
65                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
66                                 progname, lineno );
67                         rc = EXIT_FAILURE;
68                         if( continuemode ) continue;
69                         break;
70                 }
71
72                 /* make sure the DN is not empty */
73                 if( !e->e_nname.bv_len ) {
74                         fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
75                                 progname, e->e_dn, lineno );
76                         rc = EXIT_FAILURE;
77                         entry_free( e );
78                         if( continuemode ) continue;
79                         break;
80                 }
81
82                 /* check backend */
83                 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
84                         != be )
85                 {
86                         fprintf( stderr, "%s: line %d: "
87                                 "database (%s) not configured to hold \"%s\"\n",
88                                 progname, lineno,
89                                 be ? be->be_suffix[0].bv_val : "<none>",
90                                 e->e_dn );
91                         fprintf( stderr, "%s: line %d: "
92                                 "database (%s) not configured to hold \"%s\"\n",
93                                 progname, lineno,
94                                 be ? be->be_nsuffix[0].bv_val : "<none>",
95                                 e->e_ndn );
96                         rc = EXIT_FAILURE;
97                         entry_free( e );
98                         if( continuemode ) continue;
99                         break;
100                 }
101
102                 {
103                         Attribute *sc = attr_find( e->e_attrs,
104                                 slap_schema.si_ad_structuralObjectClass );
105                         Attribute *oc = attr_find( e->e_attrs,
106                                 slap_schema.si_ad_objectClass );
107
108                         if( oc == NULL ) {
109                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
110                                         progname, e->e_dn, lineno,
111                                         "no objectClass attribute");
112                                 rc = EXIT_FAILURE;
113                                 entry_free( e );
114                                 if( continuemode ) continue;
115                                 break;
116                         }
117
118                         if( sc == NULL ) {
119                                 struct berval vals[2];
120
121                                 /* int ret = */ 
122                                         structural_class( oc->a_vals, vals,
123                                         NULL, &text, textbuf, textlen );
124
125                                 if( vals[0].bv_len == 0 ) {
126                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
127                                         progname, e->e_dn, lineno, 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
140                 if( global_schemacheck ) {
141                         /* check schema */
142
143                         rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
144
145                         if( rc != LDAP_SUCCESS ) {
146                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
147                                         progname, e->e_dn, lineno, text );
148                                 rc = EXIT_FAILURE;
149                                 entry_free( e );
150                                 if( continuemode ) continue;
151                                 break;
152                         }
153                 }
154
155                 if (!dryrun) {
156                         ID id = be->be_entry_put( be, e, &bvtext );
157                         if( id == NOID ) {
158                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d): %s\n",
159                                         progname, e->e_dn, lineno, bvtext.bv_val );
160                                 rc = EXIT_FAILURE;
161                                 entry_free( e );
162                                 if( continuemode ) continue;
163                                 break;
164                         }
165                 
166                         if ( verbose ) {
167                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
168                                         e->e_dn, (long) id );
169                         }
170                 } else {
171                         if ( verbose ) {
172                                 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
173                         }
174                 }
175
176                 entry_free( e );
177         }
178
179         ch_free( buf );
180
181         be->be_entry_close( be );
182
183         if( be->be_sync ) {
184                 be->be_sync( be );
185         }
186
187         slap_tool_destroy();
188         return rc;
189 }