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