]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
Install *.schema only
[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 "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                 /* make sure the DN is valid */
61                 if( dn_normalize( e->e_ndn ) == NULL ) {
62                         fprintf( stderr, "%s: bad dn=\"%s\" (line=%d)\n",
63                                 progname, e->e_dn, lineno );
64                         rc = EXIT_FAILURE;
65                         entry_free( e );
66                         if( continuemode ) continue;
67                         break;
68                 }
69
70                 if( !noschemacheck ) {
71                         /* check schema */
72                         const char *text;
73                         if ( entry_schema_check( e, NULL, &text ) != LDAP_SUCCESS ) {
74                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
75                                         progname, e->e_dn, lineno, text );
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_ndn ) != be ) {
84                                 fprintf( stderr, "%s: database not configured to hold dn=\"%s\" (line=%d)\n",
85                                         progname, e->e_dn, lineno );
86                                 rc = EXIT_FAILURE;
87                                 entry_free( e );
88                                 if( continuemode ) continue;
89                                 break;
90                         }
91                 }
92
93                 id = be->be_entry_put( be, e );
94
95                 if( id == NOID ) {
96                         fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d)\n",
97                                 progname, e->e_dn, lineno );
98                         rc = EXIT_FAILURE;
99                         entry_free( e );
100                         if( continuemode ) continue;
101                         break;
102
103                 } else if ( verbose ) {
104                         fprintf( stderr, "added: \"%s\" (%08lx)\n",
105                                 e->e_dn, (long) id );
106                 }
107
108                 entry_free( e );
109         }
110
111         ch_free( buf );
112
113         be->be_entry_close( be );
114
115         if( be->be_sync ) {
116                 be->be_sync( be );
117         }
118
119         slap_tool_destroy();
120         return rc;
121 }