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