]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
Split out schema initialization of builtin syntax/matching rule
[openldap] / servers / slapd / tools / slapadd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 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 "slapcommon.h"
18
19 int
20 main( int argc, char **argv )
21 {
22         char            *buf;
23         int         lineno;
24         int         lmax;
25         int                     rc = EXIT_SUCCESS;
26
27         slap_tool_init( "slapadd", SLAPADD, argc, argv );
28
29         if( !be->be_entry_open &&
30                 !be->be_entry_close &&
31                 !be->be_entry_put )
32         {
33                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
34                         progname );
35                 exit( EXIT_FAILURE );
36         }
37
38         buf = NULL;
39         lmax = 0;
40         lineno = 0;
41
42         if( be->be_entry_open( be, 1 ) != 0 ) {
43                 fprintf( stderr, "%s: could not open database.\n",
44                         progname );
45                 exit( EXIT_FAILURE );
46         }
47
48         while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
49                 ID id;
50                 Entry *e = str2entry( buf );
51
52                 if( e == NULL ) {
53                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
54                                 progname, lineno );
55                         rc = EXIT_FAILURE;
56                         if( continuemode ) continue;
57                         break;
58                 }
59
60                 if( !noschemacheck ) {
61                         /* make sure the DN is valid */
62                         if( dn_normalize( e->e_ndn ) == NULL ) {
63                                 fprintf( stderr, "%s: bad dn=\"%s\" (line=%d)\n",
64                                         progname, e->e_dn, lineno );
65                                 rc = EXIT_FAILURE;
66                                 entry_free( e );
67                                 if( continuemode ) continue;
68                                 break;
69                         }
70
71                         /* check schema */
72                         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
73                                 fprintf( stderr, "%s: schema violation in entry dn=\"%s\" (line=%d)\n",
74                                         progname, e->e_dn, lineno );
75                                 rc = EXIT_FAILURE;
76                                 entry_free( e );
77                                 if( continuemode ) continue;
78                                 break;
79                         }
80
81                         /* check backend */
82                         if( select_backend( e->e_ndn ) != be ) {
83                                 fprintf( stderr, "%s: database not configured to hold dn=\"%s\" (line=%d)\n",
84                                         progname, e->e_dn, lineno );
85                                 rc = EXIT_FAILURE;
86                                 entry_free( e );
87                                 if( continuemode ) continue;
88                                 break;
89                         }
90                 }
91
92                 id = be->be_entry_put( be, e );
93
94                 if( id == NOID ) {
95                         fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d)\n",
96                                 progname, e->e_dn, lineno );
97                         rc = EXIT_FAILURE;
98                         entry_free( e );
99                         if( continuemode ) continue;
100                         break;
101
102                 } else if ( verbose ) {
103                         fprintf( stderr, "added: \"%s\" (%08lx)\n",
104                                 e->e_dn, (long) id );
105                 }
106
107                 entry_free( e );
108         }
109
110         free( buf );
111
112         be->be_entry_close( be );
113
114         if( be->be_sync ) {
115                 be->be_sync( be );
116         }
117
118         slap_tool_destroy();
119         return rc;
120 }