]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
better handling of on-the-fly operational attrs by means of helpers
[openldap] / servers / slapd / tools / slapadd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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;
26         int         lineno;
27         int         lmax;
28         int                     rc = EXIT_SUCCESS;
29
30         const char *text;
31         char textbuf[SLAP_TEXT_BUFLEN];
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         buf = NULL;
46         lmax = 0;
47         lineno = 0;
48
49         if( be->be_entry_open( be, 1 ) != 0 ) {
50                 fprintf( stderr, "%s: could not open database.\n",
51                         progname );
52                 exit( EXIT_FAILURE );
53         }
54
55         while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
56                 ID id;
57                 Entry *e = str2entry( buf );
58
59                 if( e == NULL ) {
60                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
61                                 progname, lineno );
62                         rc = EXIT_FAILURE;
63                         if( continuemode ) continue;
64                         break;
65                 }
66
67                 /* make sure the DN is valid */
68                 if( dn_normalize( e->e_ndn ) == NULL || e->e_ndn[0] == '\0' ) {
69                         fprintf( stderr, "%s: invalid dn=\"%s\" (line=%d)\n",
70                                 progname, e->e_dn, lineno );
71                         rc = EXIT_FAILURE;
72                         entry_free( e );
73                         if( continuemode ) continue;
74                         break;
75                 }
76
77                 /* make sure the DN is not empty */
78                 if( e->e_ndn == '\0' ) {
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_ndn, is_entry_referral(e), nosubs ) != be ) {
89                         fprintf( stderr, "%s: database (%s) not configured to "
90                                 "hold dn=\"%s\" (line=%d)\n",
91                                 progname,
92                                 be ? be->be_suffix[0] : "<none>",
93                                 e->e_dn, lineno );
94                         rc = EXIT_FAILURE;
95                         entry_free( e );
96                         if( continuemode ) continue;
97                         break;
98                 }
99
100                 {
101                         Attribute *sc = attr_find( e->e_attrs,
102                                 slap_schema.si_ad_structuralObjectClass );
103                         Attribute *oc = attr_find( e->e_attrs,
104                                 slap_schema.si_ad_objectClass );
105
106                         if( oc == NULL ) {
107                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
108                                         progname, e->e_dn, lineno,
109                                         "no objectClass attribute");
110                                 rc = EXIT_FAILURE;
111                                 entry_free( e );
112                                 if( continuemode ) continue;
113                                 break;
114                         }
115
116                         if( sc == NULL ) {
117                                 struct berval *vals[2];
118                                 struct berval scbv;
119
120                                 int ret = structural_class(
121                                         oc->a_vals, &scbv, &text, textbuf, textlen );
122
123                                 if( scbv.bv_len == 0 ) {
124                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
125                                         progname, e->e_dn, lineno, text );
126                                         rc = EXIT_FAILURE;
127                                         entry_free( e );
128                                         if( continuemode ) continue;
129                                         break;
130                                 }
131
132                                 vals[0] = &scbv;
133                                 vals[1] = NULL;
134                                 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
135                                         vals );
136                         }
137                 }
138
139                 if( global_schemacheck ) {
140                         /* check schema */
141
142                         rc = entry_schema_check( e, NULL, &text, textbuf, textlen );
143
144                         if( rc != LDAP_SUCCESS ) {
145                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
146                                         progname, e->e_dn, lineno, text );
147                                 rc = EXIT_FAILURE;
148                                 entry_free( e );
149                                 if( continuemode ) continue;
150                                 break;
151                         }
152                 }
153
154                 id = be->be_entry_put( be, e );
155                 if( id == NOID ) {
156                         fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d)\n",
157                                 progname, e->e_dn, lineno );
158                         rc = EXIT_FAILURE;
159                         entry_free( e );
160                         if( continuemode ) continue;
161                         break;
162
163                 }
164                 
165                 if ( verbose ) {
166                         fprintf( stderr, "added: \"%s\" (%08lx)\n",
167                                 e->e_dn, (long) id );
168                 }
169
170                 entry_free( e );
171         }
172
173         ch_free( buf );
174
175         be->be_entry_close( be );
176
177         if( be->be_sync ) {
178                 be->be_sync( be );
179         }
180
181         slap_tool_destroy();
182         return rc;
183 }