]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
147d06def1f79ee4bb1bb3457db4c22bbc2cd639
[openldap] / servers / slapd / tools / slapadd.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 #include "portable.h"
6
7 #include <stdio.h>
8
9 #include <ac/stdlib.h>
10
11 #include <ac/ctype.h>
12 #include <ac/string.h>
13 #include <ac/socket.h>
14 #include <ac/unistd.h>
15
16 #include "slapcommon.h"
17
18 int
19 main( int argc, char **argv )
20 {
21         char            *buf;
22         int         lineno;
23         int         lmax;
24         int                     rc = EXIT_SUCCESS;
25
26         slap_tool_init( "slapadd", SLAPADD, argc, argv );
27
28         if( !be->be_entry_open &&
29                 !be->be_entry_close &&
30                 !be->be_entry_put )
31         {
32                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
33                         progname );
34                 exit( EXIT_FAILURE );
35         }
36
37         buf = NULL;
38         lmax = 0;
39
40         if( be->be_entry_open( be, 1 ) != 0 ) {
41                 fprintf( stderr, "%s: could not open database.\n",
42                         progname );
43                 exit( EXIT_FAILURE );
44         }
45
46         while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
47                 ID id;
48                 Entry *e = str2entry( buf );
49
50                 if( e == NULL ) {
51                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
52                                 progname, lineno );
53                         rc = EXIT_FAILURE;
54                         if( continuemode ) continue;
55                         break;
56                 }
57
58                 if( !noschemacheck ) {
59                         /* make sure the DN is valid */
60                         if( dn_normalize_case( e->e_ndn ) == NULL ) {
61                                 fprintf( stderr, "%s: bad dn=\"%s\" (line=%d)\n",
62                                         progname, e->e_dn, lineno );
63                                 rc = EXIT_FAILURE;
64                                 entry_free( e );
65                                 if( continuemode ) continue;
66                                 break;
67                         }
68
69                         /* check schema */
70                         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
71                                 fprintf( stderr, "%s: entry dn=\"%s\" violates schema violation (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_ndn ) != be ) {
81                                 fprintf( stderr, "%s: database not configured to hold dn=\"%s\" (line=%d)\n",
82                                         progname, e->e_dn, lineno );
83                                 rc = EXIT_FAILURE;
84                                 entry_free( e );
85                                 if( continuemode ) continue;
86                                 break;
87                         }
88                 }
89
90                 id = be->be_entry_put( be, e );
91
92                 if( id == NOID ) {
93                         fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d)\n",
94                                 progname, e->e_dn, lineno );
95                         rc = EXIT_FAILURE;
96                         entry_free( e );
97                         if( continuemode ) continue;
98                         break;
99
100                 } else if ( verbose ) {
101                         fprintf( stderr, "added: \"%s\" (%08lx)\n",
102                                 e->e_dn, (long) id );
103                 }
104
105                 entry_free( e );
106         }
107
108         free( buf );
109
110         be->be_entry_close( be );
111
112         if( be->be_sync ) {
113                 be->be_sync( be );
114         }
115
116         slap_tool_destroy();
117         return rc;
118 }